blob: 48b0f57b65f7e8f0820ba7f60d2df9e4823f2449 [file] [log] [blame]
Adrian Bunkb00dc832008-05-19 16:52:27 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * arch/sparc64/kernel/signal.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
David S. Miller0a4949c42008-07-31 20:40:46 -07005 * Copyright (C) 1995, 2008 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx)
7 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
8 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
9 */
10
David S. Miller09337f52008-04-26 03:17:12 -070011#ifdef CONFIG_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/compat.h> /* for compat_old_sigset_t */
13#endif
14#include <linux/sched.h>
15#include <linux/kernel.h>
16#include <linux/signal.h>
17#include <linux/errno.h>
18#include <linux/wait.h>
19#include <linux/ptrace.h>
Roland McGrathe35a8922008-04-20 15:06:49 -070020#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/unistd.h>
22#include <linux/mm.h>
23#include <linux/tty.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/binfmts.h>
25#include <linux/bitops.h>
26
27#include <asm/uaccess.h>
28#include <asm/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/pgtable.h>
30#include <asm/fpumacro.h>
31#include <asm/uctx.h>
32#include <asm/siginfo.h>
33#include <asm/visasm.h>
David Howellsd550bbd2012-03-28 18:30:03 +010034#include <asm/switch_to.h>
35#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
David S. Miller062ea6d2008-03-26 01:52:18 -070037#include "entry.h"
38#include "systbls.h"
David S. Miller55984732011-08-20 17:14:54 -070039#include "sigutil.h"
David S. Miller062ea6d2008-03-26 01:52:18 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/* {set, get}context() needed for 64-bit SparcLinux userland. */
44asmlinkage void sparc64_set_context(struct pt_regs *regs)
45{
46 struct ucontext __user *ucp = (struct ucontext __user *)
47 regs->u_regs[UREG_I0];
48 mc_gregset_t __user *grp;
49 unsigned long pc, npc, tstate;
50 unsigned long fp, i7;
51 unsigned char fenab;
52 int err;
53
54 flush_user_windows();
55 if (get_thread_wsaved() ||
56 (((unsigned long)ucp) & (sizeof(unsigned long)-1)) ||
57 (!__access_ok(ucp, sizeof(*ucp))))
58 goto do_sigsegv;
59 grp = &ucp->uc_mcontext.mc_gregs;
60 err = __get_user(pc, &((*grp)[MC_PC]));
61 err |= __get_user(npc, &((*grp)[MC_NPC]));
62 if (err || ((pc | npc) & 3))
63 goto do_sigsegv;
64 if (regs->u_regs[UREG_I1]) {
65 sigset_t set;
66
67 if (_NSIG_WORDS == 1) {
68 if (__get_user(set.sig[0], &ucp->uc_sigmask.sig[0]))
69 goto do_sigsegv;
70 } else {
71 if (__copy_from_user(&set, &ucp->uc_sigmask, sizeof(sigset_t)))
72 goto do_sigsegv;
73 }
74 sigdelsetmask(&set, ~_BLOCKABLE);
Matt Flemingfaddf592011-08-11 14:57:02 +010075 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77 if (test_thread_flag(TIF_32BIT)) {
78 pc &= 0xffffffff;
79 npc &= 0xffffffff;
80 }
81 regs->tpc = pc;
82 regs->tnpc = npc;
83 err |= __get_user(regs->y, &((*grp)[MC_Y]));
84 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
85 regs->tstate &= ~(TSTATE_ASI | TSTATE_ICC | TSTATE_XCC);
86 regs->tstate |= (tstate & (TSTATE_ASI | TSTATE_ICC | TSTATE_XCC));
87 err |= __get_user(regs->u_regs[UREG_G1], (&(*grp)[MC_G1]));
88 err |= __get_user(regs->u_regs[UREG_G2], (&(*grp)[MC_G2]));
89 err |= __get_user(regs->u_regs[UREG_G3], (&(*grp)[MC_G3]));
90 err |= __get_user(regs->u_regs[UREG_G4], (&(*grp)[MC_G4]));
91 err |= __get_user(regs->u_regs[UREG_G5], (&(*grp)[MC_G5]));
92 err |= __get_user(regs->u_regs[UREG_G6], (&(*grp)[MC_G6]));
David S. Miller0a4949c42008-07-31 20:40:46 -070093
94 /* Skip %g7 as that's the thread register in userspace. */
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 err |= __get_user(regs->u_regs[UREG_I0], (&(*grp)[MC_O0]));
97 err |= __get_user(regs->u_regs[UREG_I1], (&(*grp)[MC_O1]));
98 err |= __get_user(regs->u_regs[UREG_I2], (&(*grp)[MC_O2]));
99 err |= __get_user(regs->u_regs[UREG_I3], (&(*grp)[MC_O3]));
100 err |= __get_user(regs->u_regs[UREG_I4], (&(*grp)[MC_O4]));
101 err |= __get_user(regs->u_regs[UREG_I5], (&(*grp)[MC_O5]));
102 err |= __get_user(regs->u_regs[UREG_I6], (&(*grp)[MC_O6]));
103 err |= __get_user(regs->u_regs[UREG_I7], (&(*grp)[MC_O7]));
104
105 err |= __get_user(fp, &(ucp->uc_mcontext.mc_fp));
106 err |= __get_user(i7, &(ucp->uc_mcontext.mc_i7));
107 err |= __put_user(fp,
108 (&(((struct reg_window __user *)(STACK_BIAS+regs->u_regs[UREG_I6]))->ins[6])));
109 err |= __put_user(i7,
110 (&(((struct reg_window __user *)(STACK_BIAS+regs->u_regs[UREG_I6]))->ins[7])));
111
112 err |= __get_user(fenab, &(ucp->uc_mcontext.mc_fpregs.mcfpu_enab));
113 if (fenab) {
114 unsigned long *fpregs = current_thread_info()->fpregs;
115 unsigned long fprs;
116
117 fprs_write(0);
118 err |= __get_user(fprs, &(ucp->uc_mcontext.mc_fpregs.mcfpu_fprs));
119 if (fprs & FPRS_DL)
120 err |= copy_from_user(fpregs,
121 &(ucp->uc_mcontext.mc_fpregs.mcfpu_fregs),
122 (sizeof(unsigned int) * 32));
123 if (fprs & FPRS_DU)
124 err |= copy_from_user(fpregs+16,
125 ((unsigned long __user *)&(ucp->uc_mcontext.mc_fpregs.mcfpu_fregs))+16,
126 (sizeof(unsigned int) * 32));
127 err |= __get_user(current_thread_info()->xfsr[0],
128 &(ucp->uc_mcontext.mc_fpregs.mcfpu_fsr));
129 err |= __get_user(current_thread_info()->gsr[0],
130 &(ucp->uc_mcontext.mc_fpregs.mcfpu_gsr));
131 regs->tstate &= ~TSTATE_PEF;
132 }
133 if (err)
134 goto do_sigsegv;
135
136 return;
137do_sigsegv:
138 force_sig(SIGSEGV, current);
139}
140
141asmlinkage void sparc64_get_context(struct pt_regs *regs)
142{
143 struct ucontext __user *ucp = (struct ucontext __user *)
144 regs->u_regs[UREG_I0];
145 mc_gregset_t __user *grp;
146 mcontext_t __user *mcp;
147 unsigned long fp, i7;
148 unsigned char fenab;
149 int err;
150
151 synchronize_user_stack();
152 if (get_thread_wsaved() || clear_user(ucp, sizeof(*ucp)))
153 goto do_sigsegv;
154
155#if 1
156 fenab = 0; /* IMO get_context is like any other system call, thus modifies FPU state -jj */
157#else
158 fenab = (current_thread_info()->fpsaved[0] & FPRS_FEF);
159#endif
160
161 mcp = &ucp->uc_mcontext;
162 grp = &mcp->mc_gregs;
163
164 /* Skip over the trap instruction, first. */
165 if (test_thread_flag(TIF_32BIT)) {
166 regs->tpc = (regs->tnpc & 0xffffffff);
167 regs->tnpc = (regs->tnpc + 4) & 0xffffffff;
168 } else {
169 regs->tpc = regs->tnpc;
170 regs->tnpc += 4;
171 }
172 err = 0;
173 if (_NSIG_WORDS == 1)
174 err |= __put_user(current->blocked.sig[0],
175 (unsigned long __user *)&ucp->uc_sigmask);
176 else
177 err |= __copy_to_user(&ucp->uc_sigmask, &current->blocked,
178 sizeof(sigset_t));
179
180 err |= __put_user(regs->tstate, &((*grp)[MC_TSTATE]));
181 err |= __put_user(regs->tpc, &((*grp)[MC_PC]));
182 err |= __put_user(regs->tnpc, &((*grp)[MC_NPC]));
183 err |= __put_user(regs->y, &((*grp)[MC_Y]));
184 err |= __put_user(regs->u_regs[UREG_G1], &((*grp)[MC_G1]));
185 err |= __put_user(regs->u_regs[UREG_G2], &((*grp)[MC_G2]));
186 err |= __put_user(regs->u_regs[UREG_G3], &((*grp)[MC_G3]));
187 err |= __put_user(regs->u_regs[UREG_G4], &((*grp)[MC_G4]));
188 err |= __put_user(regs->u_regs[UREG_G5], &((*grp)[MC_G5]));
189 err |= __put_user(regs->u_regs[UREG_G6], &((*grp)[MC_G6]));
190 err |= __put_user(regs->u_regs[UREG_G7], &((*grp)[MC_G7]));
191 err |= __put_user(regs->u_regs[UREG_I0], &((*grp)[MC_O0]));
192 err |= __put_user(regs->u_regs[UREG_I1], &((*grp)[MC_O1]));
193 err |= __put_user(regs->u_regs[UREG_I2], &((*grp)[MC_O2]));
194 err |= __put_user(regs->u_regs[UREG_I3], &((*grp)[MC_O3]));
195 err |= __put_user(regs->u_regs[UREG_I4], &((*grp)[MC_O4]));
196 err |= __put_user(regs->u_regs[UREG_I5], &((*grp)[MC_O5]));
197 err |= __put_user(regs->u_regs[UREG_I6], &((*grp)[MC_O6]));
198 err |= __put_user(regs->u_regs[UREG_I7], &((*grp)[MC_O7]));
199
200 err |= __get_user(fp,
201 (&(((struct reg_window __user *)(STACK_BIAS+regs->u_regs[UREG_I6]))->ins[6])));
202 err |= __get_user(i7,
203 (&(((struct reg_window __user *)(STACK_BIAS+regs->u_regs[UREG_I6]))->ins[7])));
204 err |= __put_user(fp, &(mcp->mc_fp));
205 err |= __put_user(i7, &(mcp->mc_i7));
206
207 err |= __put_user(fenab, &(mcp->mc_fpregs.mcfpu_enab));
208 if (fenab) {
209 unsigned long *fpregs = current_thread_info()->fpregs;
210 unsigned long fprs;
211
212 fprs = current_thread_info()->fpsaved[0];
213 if (fprs & FPRS_DL)
214 err |= copy_to_user(&(mcp->mc_fpregs.mcfpu_fregs), fpregs,
215 (sizeof(unsigned int) * 32));
216 if (fprs & FPRS_DU)
217 err |= copy_to_user(
218 ((unsigned long __user *)&(mcp->mc_fpregs.mcfpu_fregs))+16, fpregs+16,
219 (sizeof(unsigned int) * 32));
220 err |= __put_user(current_thread_info()->xfsr[0], &(mcp->mc_fpregs.mcfpu_fsr));
221 err |= __put_user(current_thread_info()->gsr[0], &(mcp->mc_fpregs.mcfpu_gsr));
222 err |= __put_user(fprs, &(mcp->mc_fpregs.mcfpu_fprs));
223 }
224 if (err)
225 goto do_sigsegv;
226
227 return;
228do_sigsegv:
229 force_sig(SIGSEGV, current);
230}
231
232struct rt_signal_frame {
233 struct sparc_stackf ss;
234 siginfo_t info;
235 struct pt_regs regs;
236 __siginfo_fpu_t __user *fpu_save;
237 stack_t stack;
238 sigset_t mask;
David S. Miller55984732011-08-20 17:14:54 -0700239 __siginfo_rwin_t *rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240};
241
David S. Miller2d7d5f02006-01-19 02:42:49 -0800242static long _sigpause_common(old_sigset_t set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Matt Flemingfaddf592011-08-11 14:57:02 +0100244 sigset_t blocked;
245
David S. Miller2d7d5f02006-01-19 02:42:49 -0800246 current->saved_sigmask = current->blocked;
Matt Flemingfaddf592011-08-11 14:57:02 +0100247
248 set &= _BLOCKABLE;
249 siginitset(&blocked, set);
250 set_current_blocked(&blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
David S. Miller2d7d5f02006-01-19 02:42:49 -0800252 current->state = TASK_INTERRUPTIBLE;
253 schedule();
David S. Miller9a28dbf2008-05-12 22:45:15 -0700254
255 set_restore_sigmask();
256
David S. Miller2d7d5f02006-01-19 02:42:49 -0800257 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
David S. Miller2d7d5f02006-01-19 02:42:49 -0800260asmlinkage long sys_sigpause(unsigned int set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
David S. Miller2d7d5f02006-01-19 02:42:49 -0800262 return _sigpause_common(set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
David S. Miller2d7d5f02006-01-19 02:42:49 -0800265asmlinkage long sys_sigsuspend(old_sigset_t set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
David S. Miller2d7d5f02006-01-19 02:42:49 -0800267 return _sigpause_common(set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270void do_rt_sigreturn(struct pt_regs *regs)
271{
272 struct rt_signal_frame __user *sf;
273 unsigned long tpc, tnpc, tstate;
274 __siginfo_fpu_t __user *fpu_save;
David S. Miller55984732011-08-20 17:14:54 -0700275 __siginfo_rwin_t __user *rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 int err;
278
279 /* Always make any pending restarted system calls return -EINTR */
280 current_thread_info()->restart_block.fn = do_no_restart_syscall;
281
282 synchronize_user_stack ();
283 sf = (struct rt_signal_frame __user *)
284 (regs->u_regs [UREG_FP] + STACK_BIAS);
285
286 /* 1. Make sure we are not getting garbage from the user */
287 if (((unsigned long) sf) & 3)
288 goto segv;
289
290 err = get_user(tpc, &sf->regs.tpc);
291 err |= __get_user(tnpc, &sf->regs.tnpc);
292 if (test_thread_flag(TIF_32BIT)) {
293 tpc &= 0xffffffff;
294 tnpc &= 0xffffffff;
295 }
296 err |= ((tpc | tnpc) & 3);
297
298 /* 2. Restore the state */
299 err |= __get_user(regs->y, &sf->regs.y);
300 err |= __get_user(tstate, &sf->regs.tstate);
301 err |= copy_from_user(regs->u_regs, sf->regs.u_regs, sizeof(regs->u_regs));
302
303 /* User can only change condition codes and %asi in %tstate. */
304 regs->tstate &= ~(TSTATE_ASI | TSTATE_ICC | TSTATE_XCC);
305 regs->tstate |= (tstate & (TSTATE_ASI | TSTATE_ICC | TSTATE_XCC));
306
307 err |= __get_user(fpu_save, &sf->fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700308 if (!err && fpu_save)
309 err |= restore_fpu_state(regs, fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
Oleg Nesterov62715ec2007-07-17 14:37:54 -0700312 err |= do_sigaltstack(&sf->stack, NULL, (unsigned long)sf);
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (err)
315 goto segv;
Oleg Nesterov62715ec2007-07-17 14:37:54 -0700316
David S. Miller55984732011-08-20 17:14:54 -0700317 err |= __get_user(rwin_save, &sf->rwin_save);
318 if (!err && rwin_save) {
319 if (restore_rwin_state(rwin_save))
320 goto segv;
321 }
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 regs->tpc = tpc;
324 regs->tnpc = tnpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
David S. Miller2678fef2008-05-01 03:30:22 -0700326 /* Prevent syscall restart. */
David S. Miller28e61032008-05-11 02:07:19 -0700327 pt_regs_clear_syscall(regs);
David S. Miller2678fef2008-05-01 03:30:22 -0700328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 sigdelsetmask(&set, ~_BLOCKABLE);
Matt Flemingfaddf592011-08-11 14:57:02 +0100330 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return;
332segv:
333 force_sig(SIGSEGV, current);
334}
335
336/* Checks if the fp is valid */
David S. Miller55984732011-08-20 17:14:54 -0700337static int invalid_frame_pointer(void __user *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
David S. Millerf036d9f2010-02-09 16:18:40 -0800339 if (((unsigned long) fp) & 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return 1;
341 return 0;
342}
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344static inline void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, unsigned long framesize)
345{
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700346 unsigned long sp = regs->u_regs[UREG_FP] + STACK_BIAS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700348 /*
349 * If we are on the alternate signal stack and would overflow it, don't.
350 * Return an always-bogus address instead so we will die with SIGSEGV.
351 */
352 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
353 return (void __user *) -1L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 /* This is the X/Open sanctioned signal stack switching. */
356 if (ka->sa.sa_flags & SA_ONSTACK) {
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700357 if (sas_ss_flags(sp) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 sp = current->sas_ss_sp + current->sas_ss_size;
359 }
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700360
David S. Millerf036d9f2010-02-09 16:18:40 -0800361 sp -= framesize;
362
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700363 /* Always align the stack frame. This handles two cases. First,
364 * sigaltstack need not be mindful of platform specific stack
365 * alignment. Second, if we took this signal because the stack
366 * is not aligned properly, we'd like to take the signal cleanly
367 * and report that.
368 */
David S. Millerf036d9f2010-02-09 16:18:40 -0800369 sp &= ~15UL;
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700370
David S. Millerf036d9f2010-02-09 16:18:40 -0800371 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
David S. Miller392c2182010-09-21 21:41:12 -0700374static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
376 int signo, sigset_t *oldset, siginfo_t *info)
377{
378 struct rt_signal_frame __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700379 int wsaved, err, sf_size;
380 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 /* 1. Make sure everything is clean */
383 synchronize_user_stack();
384 save_and_clear_fpu();
385
David S. Miller55984732011-08-20 17:14:54 -0700386 wsaved = get_thread_wsaved();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
David S. Miller55984732011-08-20 17:14:54 -0700388 sf_size = sizeof(struct rt_signal_frame);
389 if (current_thread_info()->fpsaved[0] & FPRS_FEF)
390 sf_size += sizeof(__siginfo_fpu_t);
391 if (wsaved)
392 sf_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 sf = (struct rt_signal_frame __user *)
David S. Miller55984732011-08-20 17:14:54 -0700394 get_sigframe(ka, regs, sf_size);
395
396 if (invalid_frame_pointer (sf))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 goto sigill;
398
David S. Miller55984732011-08-20 17:14:54 -0700399 tail = (sf + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /* 2. Save the current process state */
402 err = copy_to_user(&sf->regs, regs, sizeof (*regs));
403
404 if (current_thread_info()->fpsaved[0] & FPRS_FEF) {
David S. Miller55984732011-08-20 17:14:54 -0700405 __siginfo_fpu_t __user *fpu_save = tail;
406 tail += sizeof(__siginfo_fpu_t);
407 err |= save_fpu_state(regs, fpu_save);
408 err |= __put_user((u64)fpu_save, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 } else {
410 err |= __put_user(0, &sf->fpu_save);
411 }
David S. Miller55984732011-08-20 17:14:54 -0700412 if (wsaved) {
413 __siginfo_rwin_t __user *rwin_save = tail;
414 tail += sizeof(__siginfo_rwin_t);
415 err |= save_rwin_state(wsaved, rwin_save);
416 err |= __put_user((u64)rwin_save, &sf->rwin_save);
417 set_thread_wsaved(0);
418 } else {
419 err |= __put_user(0, &sf->rwin_save);
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 /* Setup sigaltstack */
423 err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
424 err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
425 err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
426
427 err |= copy_to_user(&sf->mask, oldset, sizeof(sigset_t));
428
David S. Miller55984732011-08-20 17:14:54 -0700429 if (!wsaved) {
430 err |= copy_in_user((u64 __user *)sf,
431 (u64 __user *)(regs->u_regs[UREG_FP] +
432 STACK_BIAS),
433 sizeof(struct reg_window));
434 } else {
435 struct reg_window *rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
David S. Miller55984732011-08-20 17:14:54 -0700437 rp = &current_thread_info()->reg_window[wsaved - 1];
438 err |= copy_to_user(sf, rp, sizeof(struct reg_window));
439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (info)
441 err |= copy_siginfo_to_user(&sf->info, info);
442 else {
443 err |= __put_user(signo, &sf->info.si_signo);
444 err |= __put_user(SI_NOINFO, &sf->info.si_code);
445 }
446 if (err)
447 goto sigsegv;
448
449 /* 3. signal handler back-trampoline and parameters */
450 regs->u_regs[UREG_FP] = ((unsigned long) sf) - STACK_BIAS;
451 regs->u_regs[UREG_I0] = signo;
452 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
453
454 /* The sigcontext is passed in this way because of how it
455 * is defined in GLIBC's /usr/include/bits/sigcontext.h
456 * for sparc64. It includes the 128 bytes of siginfo_t.
457 */
458 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
459
460 /* 5. signal handler */
461 regs->tpc = (unsigned long) ka->sa.sa_handler;
462 regs->tnpc = (regs->tpc + 4);
463 if (test_thread_flag(TIF_32BIT)) {
464 regs->tpc &= 0xffffffff;
465 regs->tnpc &= 0xffffffff;
466 }
467 /* 4. return to kernel instructions */
468 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
David S. Miller392c2182010-09-21 21:41:12 -0700469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471sigill:
472 do_exit(SIGILL);
David S. Miller392c2182010-09-21 21:41:12 -0700473 return -EINVAL;
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475sigsegv:
476 force_sigsegv(signo, current);
David S. Miller392c2182010-09-21 21:41:12 -0700477 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
David S. Miller392c2182010-09-21 21:41:12 -0700480static inline int handle_signal(unsigned long signr, struct k_sigaction *ka,
481 siginfo_t *info,
482 sigset_t *oldset, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
David S. Miller392c2182010-09-21 21:41:12 -0700484 int err;
485
486 err = setup_rt_frame(ka, regs, signr, oldset,
487 (ka->sa.sa_flags & SA_SIGINFO) ? info : NULL);
488 if (err)
489 return err;
David S. Miller392c2182010-09-21 21:41:12 -0700490
Matt Flemingce24d8a2012-03-21 16:33:46 -0700491 block_sigmask(ka, signr);
David S. Miller392c2182010-09-21 21:41:12 -0700492 tracehook_signal_handler(signr, info, ka, regs, 0);
493
494 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
David S. Miller28e61032008-05-11 02:07:19 -0700498 struct sigaction *sa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 switch (regs->u_regs[UREG_I0]) {
501 case ERESTART_RESTARTBLOCK:
502 case ERESTARTNOHAND:
503 no_system_call_restart:
504 regs->u_regs[UREG_I0] = EINTR;
505 regs->tstate |= (TSTATE_ICARRY|TSTATE_XCARRY);
506 break;
507 case ERESTARTSYS:
508 if (!(sa->sa_flags & SA_RESTART))
509 goto no_system_call_restart;
510 /* fallthrough */
511 case ERESTARTNOINTR:
512 regs->u_regs[UREG_I0] = orig_i0;
513 regs->tpc -= 4;
514 regs->tnpc -= 4;
515 }
516}
517
518/* Note that 'init' is a special process: it doesn't get signals it doesn't
519 * want to handle. Thus you cannot kill init even with a SIGKILL even by
520 * mistake.
521 */
David S. Miller238468b2008-04-24 03:01:48 -0700522static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 struct k_sigaction ka;
David S. Miller28e61032008-05-11 02:07:19 -0700525 int restart_syscall;
David S. Miller2d7d5f02006-01-19 02:42:49 -0800526 sigset_t *oldset;
David S. Miller238468b2008-04-24 03:01:48 -0700527 siginfo_t info;
David S. Miller90888812008-04-27 14:52:51 -0700528 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
David S. Miller1d299bc2011-11-14 20:32:16 -0800530 /* It's a lot of work and synchronization to add a new ptrace
531 * register for GDB to save and restore in order to get
532 * orig_i0 correct for syscall restarts when debugging.
533 *
David S. Millere88d2462011-11-15 12:57:00 -0800534 * Although it should be the case that most of the global
535 * registers are volatile across a system call, glibc already
536 * depends upon that fact that we preserve them. So we can't
537 * just use any global register to save away the orig_i0 value.
538 *
539 * In particular %g2, %g3, %g4, and %g5 are all assumed to be
540 * preserved across a system call trap by various pieces of
541 * code in glibc.
542 *
543 * %g7 is used as the "thread register". %g6 is not used in
544 * any fixed manner. %g6 is used as a scratch register and
545 * a compiler temporary, but it's value is never used across
546 * a system call. Therefore %g6 is usable for orig_i0 storage.
David S. Miller1d299bc2011-11-14 20:32:16 -0800547 */
David S. Miller2678fef2008-05-01 03:30:22 -0700548 if (pt_regs_is_syscall(regs) &&
David S. Miller1d299bc2011-11-14 20:32:16 -0800549 (regs->tstate & (TSTATE_XCARRY | TSTATE_ICARRY)))
David S. Millere88d2462011-11-15 12:57:00 -0800550 regs->u_regs[UREG_G6] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
David S. Miller9a28dbf2008-05-12 22:45:15 -0700552 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
David S. Miller2d7d5f02006-01-19 02:42:49 -0800553 oldset = &current->saved_sigmask;
554 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 oldset = &current->blocked;
556
David S. Miller09337f52008-04-26 03:17:12 -0700557#ifdef CONFIG_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (test_thread_flag(TIF_32BIT)) {
David S. Miller1d299bc2011-11-14 20:32:16 -0800559 extern void do_signal32(sigset_t *, struct pt_regs *);
560 do_signal32(oldset, regs);
David S. Miller2d7d5f02006-01-19 02:42:49 -0800561 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563#endif
564
David S. Miller28e61032008-05-11 02:07:19 -0700565 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
566
David S. Miller1d299bc2011-11-14 20:32:16 -0800567 restart_syscall = 0;
568 if (pt_regs_is_syscall(regs) &&
569 (regs->tstate & (TSTATE_XCARRY | TSTATE_ICARRY))) {
570 restart_syscall = 1;
David S. Millere88d2462011-11-15 12:57:00 -0800571 orig_i0 = regs->u_regs[UREG_G6];
David S. Miller1d299bc2011-11-14 20:32:16 -0800572 }
David S. Miller28e61032008-05-11 02:07:19 -0700573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (signr > 0) {
David S. Miller28e61032008-05-11 02:07:19 -0700575 if (restart_syscall)
576 syscall_restart(orig_i0, regs, &ka.sa);
David S. Miller392c2182010-09-21 21:41:12 -0700577 if (handle_signal(signr, &ka, &info, oldset, regs) == 0) {
578 /* A signal was successfully delivered; the saved
579 * sigmask will have been stored in the signal frame,
580 * and will be restored by sigreturn, so we can simply
581 * clear the TS_RESTORE_SIGMASK flag.
582 */
583 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
584 }
David S. Miller2d7d5f02006-01-19 02:42:49 -0800585 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
David S. Miller28e61032008-05-11 02:07:19 -0700587 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
589 regs->u_regs[UREG_I0] == ERESTARTSYS ||
590 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
591 /* replay the system call when we are done */
David S. Miller28e61032008-05-11 02:07:19 -0700592 regs->u_regs[UREG_I0] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 regs->tpc -= 4;
594 regs->tnpc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700595 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
David S. Miller28e61032008-05-11 02:07:19 -0700597 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
599 regs->u_regs[UREG_G1] = __NR_restart_syscall;
600 regs->tpc -= 4;
601 regs->tnpc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700602 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
David S. Miller2d7d5f02006-01-19 02:42:49 -0800604
David S. Miller9a28dbf2008-05-12 22:45:15 -0700605 /* If there's no signal to deliver, we just put the saved sigmask
David S. Miller2d7d5f02006-01-19 02:42:49 -0800606 * back
607 */
David S. Miller9a28dbf2008-05-12 22:45:15 -0700608 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
609 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
David S. Miller27f20dc2011-10-12 12:27:35 -0700610 set_current_blocked(&current->saved_sigmask);
David S. Miller2d7d5f02006-01-19 02:42:49 -0800611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
David S. Miller7697daa2008-04-24 03:15:22 -0700614void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0, unsigned long thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
David S. Miller9a28dbf2008-05-12 22:45:15 -0700616 if (thread_info_flags & _TIF_SIGPENDING)
David S. Miller238468b2008-04-24 03:01:48 -0700617 do_signal(regs, orig_i0);
Roland McGrathe35a8922008-04-20 15:06:49 -0700618 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
619 clear_thread_flag(TIF_NOTIFY_RESUME);
620 tracehook_notify_resume(regs);
David Howellsee18d642009-09-02 09:14:21 +0100621 if (current->replacement_session_keyring)
622 key_replace_session_keyring();
Roland McGrathe35a8922008-04-20 15:06:49 -0700623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
David Howellsee18d642009-09-02 09:14:21 +0100625