blob: f6722427203dec90025c786dbffe0aab74bdb170 [file] [log] [blame]
David S. Miller5526b7e2008-04-27 02:26:36 -07001/* linux/arch/sparc/kernel/signal.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx)
6 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/sched.h>
10#include <linux/kernel.h>
11#include <linux/signal.h>
12#include <linux/errno.h>
13#include <linux/wait.h>
14#include <linux/ptrace.h>
15#include <linux/unistd.h>
16#include <linux/mm.h>
17#include <linux/tty.h>
18#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/binfmts.h> /* do_coredum */
20#include <linux/bitops.h>
David S. Miller5a157d52008-07-27 03:38:53 -070021#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <asm/uaccess.h>
24#include <asm/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/pgalloc.h>
26#include <asm/pgtable.h>
27#include <asm/cacheflush.h> /* flush_sig_insns */
David Howellsd550bbd2012-03-28 18:30:03 +010028#include <asm/switch_to.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
David S. Miller55984732011-08-20 17:14:54 -070030#include "sigutil.h"
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
33
34extern void fpsave(unsigned long *fpregs, unsigned long *fsr,
35 void *fpqueue, unsigned long *fpqdepth);
36extern void fpload(unsigned long *fpregs, unsigned long *fsr);
37
David S. Miller5526b7e2008-04-27 02:26:36 -070038struct signal_frame {
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct sparc_stackf ss;
Sam Ravnborg4d7b92a2009-01-02 19:32:59 -080040 __siginfo32_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 __siginfo_fpu_t __user *fpu_save;
42 unsigned long insns[2] __attribute__ ((aligned (8)));
43 unsigned int extramask[_NSIG_WORDS - 1];
44 unsigned int extra_size; /* Should be 0 */
David S. Miller55984732011-08-20 17:14:54 -070045 __siginfo_rwin_t __user *rwin_save;
46} __attribute__((aligned(8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48struct rt_signal_frame {
49 struct sparc_stackf ss;
50 siginfo_t info;
51 struct pt_regs regs;
52 sigset_t mask;
53 __siginfo_fpu_t __user *fpu_save;
54 unsigned int insns[2];
55 stack_t stack;
56 unsigned int extra_size; /* Should be 0 */
David S. Miller55984732011-08-20 17:14:54 -070057 __siginfo_rwin_t __user *rwin_save;
58} __attribute__((aligned(8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/* Align macros */
David S. Miller5526b7e2008-04-27 02:26:36 -070061#define SF_ALIGNEDSZ (((sizeof(struct signal_frame) + 7) & (~7)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define RT_ALIGNEDSZ (((sizeof(struct rt_signal_frame) + 7) & (~7)))
63
David S. Miller2d7d5f02006-01-19 02:42:49 -080064static int _sigpause_common(old_sigset_t set)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Matt Flemingfaddf592011-08-11 14:57:02 +010066 sigset_t blocked;
Matt Flemingfaddf592011-08-11 14:57:02 +010067 siginitset(&blocked, set);
Al Viro68f3f162012-05-21 21:42:32 -040068 return sigsuspend(&blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
David S. Miller2d7d5f02006-01-19 02:42:49 -080071asmlinkage int sys_sigsuspend(old_sigset_t set)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
David S. Miller2d7d5f02006-01-19 02:42:49 -080073 return _sigpause_common(set);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
David S. Miller5526b7e2008-04-27 02:26:36 -070076asmlinkage void do_sigreturn(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
David S. Miller5526b7e2008-04-27 02:26:36 -070078 struct signal_frame __user *sf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 unsigned long up_psr, pc, npc;
80 sigset_t set;
81 __siginfo_fpu_t __user *fpu_save;
David S. Miller55984732011-08-20 17:14:54 -070082 __siginfo_rwin_t __user *rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 int err;
84
David S. Miller5526b7e2008-04-27 02:26:36 -070085 /* Always make any pending restarted system calls return -EINTR */
86 current_thread_info()->restart_block.fn = do_no_restart_syscall;
87
88 synchronize_user_stack();
89
90 sf = (struct signal_frame __user *) regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 /* 1. Make sure we are not getting garbage from the user */
93 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
94 goto segv_and_exit;
95
96 if (((unsigned long) sf) & 3)
97 goto segv_and_exit;
98
99 err = __get_user(pc, &sf->info.si_regs.pc);
100 err |= __get_user(npc, &sf->info.si_regs.npc);
101
102 if ((pc | npc) & 3)
103 goto segv_and_exit;
104
105 /* 2. Restore the state */
106 up_psr = regs->psr;
107 err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
108
109 /* User can only change condition codes and FPU enabling in %psr. */
110 regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
111 | (regs->psr & (PSR_ICC | PSR_EF));
112
David S. Miller28e61032008-05-11 02:07:19 -0700113 /* Prevent syscall restart. */
114 pt_regs_clear_syscall(regs);
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 err |= __get_user(fpu_save, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (fpu_save)
118 err |= restore_fpu_state(regs, fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700119 err |= __get_user(rwin_save, &sf->rwin_save);
120 if (rwin_save)
121 err |= restore_rwin_state(rwin_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 /* This is pretty much atomic, no amount locking would prevent
124 * the races which exist anyways.
125 */
126 err |= __get_user(set.sig[0], &sf->info.si_mask);
127 err |= __copy_from_user(&set.sig[1], &sf->extramask,
128 (_NSIG_WORDS-1) * sizeof(unsigned int));
129
130 if (err)
131 goto segv_and_exit;
132
133 sigdelsetmask(&set, ~_BLOCKABLE);
Matt Flemingfaddf592011-08-11 14:57:02 +0100134 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return;
136
137segv_and_exit:
138 force_sig(SIGSEGV, current);
139}
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
142{
143 struct rt_signal_frame __user *sf;
144 unsigned int psr, pc, npc;
145 __siginfo_fpu_t __user *fpu_save;
David S. Miller55984732011-08-20 17:14:54 -0700146 __siginfo_rwin_t __user *rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 mm_segment_t old_fs;
148 sigset_t set;
149 stack_t st;
150 int err;
151
152 synchronize_user_stack();
153 sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
154 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
155 (((unsigned long) sf) & 0x03))
156 goto segv;
157
158 err = __get_user(pc, &sf->regs.pc);
159 err |= __get_user(npc, &sf->regs.npc);
160 err |= ((pc | npc) & 0x03);
161
162 err |= __get_user(regs->y, &sf->regs.y);
163 err |= __get_user(psr, &sf->regs.psr);
164
165 err |= __copy_from_user(&regs->u_regs[UREG_G1],
166 &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
167
168 regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
169
David S. Miller28e61032008-05-11 02:07:19 -0700170 /* Prevent syscall restart. */
171 pt_regs_clear_syscall(regs);
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 err |= __get_user(fpu_save, &sf->fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700174 if (!err && fpu_save)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 err |= restore_fpu_state(regs, fpu_save);
176 err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
177
178 err |= __copy_from_user(&st, &sf->stack, sizeof(stack_t));
179
180 if (err)
181 goto segv;
182
183 regs->pc = pc;
184 regs->npc = npc;
185
186 /* It is more difficult to avoid calling this function than to
187 * call it and ignore errors.
188 */
189 old_fs = get_fs();
190 set_fs(KERNEL_DS);
191 do_sigaltstack((const stack_t __user *) &st, NULL, (unsigned long)sf);
192 set_fs(old_fs);
193
David S. Miller55984732011-08-20 17:14:54 -0700194 err |= __get_user(rwin_save, &sf->rwin_save);
195 if (!err && rwin_save) {
196 if (restore_rwin_state(rwin_save))
197 goto segv;
198 }
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 sigdelsetmask(&set, ~_BLOCKABLE);
Matt Flemingfaddf592011-08-11 14:57:02 +0100201 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return;
203segv:
204 force_sig(SIGSEGV, current);
205}
206
207/* Checks if the fp is valid */
208static inline int invalid_frame_pointer(void __user *fp, int fplen)
209{
Sam Ravnborg3c23a762012-05-11 11:35:14 +0000210 if ((((unsigned long) fp) & 7) || !__access_ok((unsigned long)fp, fplen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return 1;
Sam Ravnborg3c23a762012-05-11 11:35:14 +0000212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return 0;
214}
215
216static inline void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
217{
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700218 unsigned long sp = regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700220 /*
221 * If we are on the alternate signal stack and would overflow it, don't.
222 * Return an always-bogus address instead so we will die with SIGSEGV.
223 */
224 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
225 return (void __user *) -1L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* This is the X/Open sanctioned signal stack switching. */
228 if (sa->sa_flags & SA_ONSTACK) {
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700229 if (sas_ss_flags(sp) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 sp = current->sas_ss_sp + current->sas_ss_size;
231 }
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700232
David S. Millerf036d9f2010-02-09 16:18:40 -0800233 sp -= framesize;
234
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700235 /* Always align the stack frame. This handles two cases. First,
236 * sigaltstack need not be mindful of platform specific stack
237 * alignment. Second, if we took this signal because the stack
238 * is not aligned properly, we'd like to take the signal cleanly
239 * and report that.
240 */
David S. Millerf036d9f2010-02-09 16:18:40 -0800241 sp &= ~15UL;
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700242
David S. Millerf036d9f2010-02-09 16:18:40 -0800243 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
David S. Miller392c2182010-09-21 21:41:12 -0700246static int setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
247 int signo, sigset_t *oldset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
David S. Miller5526b7e2008-04-27 02:26:36 -0700249 struct signal_frame __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700250 int sigframe_size, err, wsaved;
251 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /* 1. Make sure everything is clean */
254 synchronize_user_stack();
255
David S. Miller55984732011-08-20 17:14:54 -0700256 wsaved = current_thread_info()->w_saved;
257
258 sigframe_size = sizeof(*sf);
259 if (used_math())
260 sigframe_size += sizeof(__siginfo_fpu_t);
261 if (wsaved)
262 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
David S. Miller5526b7e2008-04-27 02:26:36 -0700264 sf = (struct signal_frame __user *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 get_sigframe(&ka->sa, regs, sigframe_size);
266
267 if (invalid_frame_pointer(sf, sigframe_size))
268 goto sigill_and_return;
269
David S. Miller55984732011-08-20 17:14:54 -0700270 tail = sf + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 /* 2. Save the current process state */
273 err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
274
275 err |= __put_user(0, &sf->extra_size);
276
277 if (used_math()) {
David S. Miller55984732011-08-20 17:14:54 -0700278 __siginfo_fpu_t __user *fp = tail;
279 tail += sizeof(*fp);
280 err |= save_fpu_state(regs, fp);
281 err |= __put_user(fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 } else {
283 err |= __put_user(0, &sf->fpu_save);
284 }
David S. Miller55984732011-08-20 17:14:54 -0700285 if (wsaved) {
286 __siginfo_rwin_t __user *rwp = tail;
287 tail += sizeof(*rwp);
288 err |= save_rwin_state(wsaved, rwp);
289 err |= __put_user(rwp, &sf->rwin_save);
290 } else {
291 err |= __put_user(0, &sf->rwin_save);
292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 err |= __put_user(oldset->sig[0], &sf->info.si_mask);
295 err |= __copy_to_user(sf->extramask, &oldset->sig[1],
296 (_NSIG_WORDS - 1) * sizeof(unsigned int));
David S. Miller55984732011-08-20 17:14:54 -0700297 if (!wsaved) {
298 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
299 sizeof(struct reg_window32));
300 } else {
301 struct reg_window32 *rp;
302
303 rp = &current_thread_info()->reg_window[wsaved - 1];
304 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (err)
307 goto sigsegv;
308
309 /* 3. signal handler back-trampoline and parameters */
310 regs->u_regs[UREG_FP] = (unsigned long) sf;
311 regs->u_regs[UREG_I0] = signo;
312 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
313 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
314
315 /* 4. signal handler */
316 regs->pc = (unsigned long) ka->sa.sa_handler;
317 regs->npc = (regs->pc + 4);
318
319 /* 5. return to kernel instructions */
320 if (ka->ka_restorer)
321 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
322 else {
323 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
324
325 /* mov __NR_sigreturn, %g1 */
326 err |= __put_user(0x821020d8, &sf->insns[0]);
327
328 /* t 0x10 */
329 err |= __put_user(0x91d02010, &sf->insns[1]);
330 if (err)
331 goto sigsegv;
332
333 /* Flush instruction space. */
334 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
335 }
David S. Miller392c2182010-09-21 21:41:12 -0700336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338sigill_and_return:
339 do_exit(SIGILL);
David S. Miller392c2182010-09-21 21:41:12 -0700340 return -EINVAL;
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342sigsegv:
343 force_sigsegv(signo, current);
David S. Miller392c2182010-09-21 21:41:12 -0700344 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
David S. Miller392c2182010-09-21 21:41:12 -0700347static int setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
348 int signo, sigset_t *oldset, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct rt_signal_frame __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700351 int sigframe_size, wsaved;
352 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 unsigned int psr;
354 int err;
355
356 synchronize_user_stack();
David S. Miller55984732011-08-20 17:14:54 -0700357 wsaved = current_thread_info()->w_saved;
358 sigframe_size = sizeof(*sf);
359 if (used_math())
360 sigframe_size += sizeof(__siginfo_fpu_t);
361 if (wsaved)
362 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 sf = (struct rt_signal_frame __user *)
364 get_sigframe(&ka->sa, regs, sigframe_size);
365 if (invalid_frame_pointer(sf, sigframe_size))
366 goto sigill;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
David S. Miller55984732011-08-20 17:14:54 -0700368 tail = sf + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 err = __put_user(regs->pc, &sf->regs.pc);
370 err |= __put_user(regs->npc, &sf->regs.npc);
371 err |= __put_user(regs->y, &sf->regs.y);
372 psr = regs->psr;
373 if (used_math())
374 psr |= PSR_EF;
375 err |= __put_user(psr, &sf->regs.psr);
376 err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
377 err |= __put_user(0, &sf->extra_size);
378
379 if (psr & PSR_EF) {
David S. Miller55984732011-08-20 17:14:54 -0700380 __siginfo_fpu_t *fp = tail;
381 tail += sizeof(*fp);
382 err |= save_fpu_state(regs, fp);
383 err |= __put_user(fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 } else {
385 err |= __put_user(0, &sf->fpu_save);
386 }
David S. Miller55984732011-08-20 17:14:54 -0700387 if (wsaved) {
388 __siginfo_rwin_t *rwp = tail;
389 tail += sizeof(*rwp);
390 err |= save_rwin_state(wsaved, rwp);
391 err |= __put_user(rwp, &sf->rwin_save);
392 } else {
393 err |= __put_user(0, &sf->rwin_save);
394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
396
397 /* Setup sigaltstack */
398 err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
399 err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
400 err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
401
David S. Miller55984732011-08-20 17:14:54 -0700402 if (!wsaved) {
403 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
404 sizeof(struct reg_window32));
405 } else {
406 struct reg_window32 *rp;
407
408 rp = &current_thread_info()->reg_window[wsaved - 1];
409 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 err |= copy_siginfo_to_user(&sf->info, info);
413
414 if (err)
415 goto sigsegv;
416
417 regs->u_regs[UREG_FP] = (unsigned long) sf;
418 regs->u_regs[UREG_I0] = signo;
419 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
420 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
421
422 regs->pc = (unsigned long) ka->sa.sa_handler;
423 regs->npc = (regs->pc + 4);
424
425 if (ka->ka_restorer)
426 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
427 else {
428 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
429
430 /* mov __NR_sigreturn, %g1 */
431 err |= __put_user(0x821020d8, &sf->insns[0]);
432
433 /* t 0x10 */
434 err |= __put_user(0x91d02010, &sf->insns[1]);
435 if (err)
436 goto sigsegv;
437
438 /* Flush instruction space. */
439 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
440 }
David S. Miller392c2182010-09-21 21:41:12 -0700441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443sigill:
444 do_exit(SIGILL);
David S. Miller392c2182010-09-21 21:41:12 -0700445 return -EINVAL;
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447sigsegv:
448 force_sigsegv(signo, current);
David S. Miller392c2182010-09-21 21:41:12 -0700449 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Al Viroa610d6e2012-05-21 23:42:15 -0400452static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453handle_signal(unsigned long signr, struct k_sigaction *ka,
Al Virob7f9a112012-05-02 09:59:21 -0400454 siginfo_t *info, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Al Virob7f9a112012-05-02 09:59:21 -0400456 sigset_t *oldset = sigmask_to_save();
David S. Miller392c2182010-09-21 21:41:12 -0700457 int err;
458
David S. Millerec98c6b2008-04-20 02:14:23 -0700459 if (ka->sa.sa_flags & SA_SIGINFO)
David S. Miller392c2182010-09-21 21:41:12 -0700460 err = setup_rt_frame(ka, regs, signr, oldset, info);
David S. Millerec98c6b2008-04-20 02:14:23 -0700461 else
David S. Miller392c2182010-09-21 21:41:12 -0700462 err = setup_frame(ka, regs, signr, oldset);
463
464 if (err)
Al Viroa610d6e2012-05-21 23:42:15 -0400465 return;
David S. Millerec98c6b2008-04-20 02:14:23 -0700466
Matt Flemingce24d8a2012-03-21 16:33:46 -0700467 block_sigmask(ka, signr);
David S. Miller392c2182010-09-21 21:41:12 -0700468 tracehook_signal_handler(signr, info, ka, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
471static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
472 struct sigaction *sa)
473{
474 switch(regs->u_regs[UREG_I0]) {
475 case ERESTART_RESTARTBLOCK:
476 case ERESTARTNOHAND:
477 no_system_call_restart:
478 regs->u_regs[UREG_I0] = EINTR;
479 regs->psr |= PSR_C;
480 break;
481 case ERESTARTSYS:
482 if (!(sa->sa_flags & SA_RESTART))
483 goto no_system_call_restart;
484 /* fallthrough */
485 case ERESTARTNOINTR:
486 regs->u_regs[UREG_I0] = orig_i0;
487 regs->pc -= 4;
488 regs->npc -= 4;
489 }
490}
491
492/* Note that 'init' is a special process: it doesn't get signals it doesn't
493 * want to handle. Thus you cannot kill init even with a SIGKILL even by
494 * mistake.
495 */
David S. Miller5a157d52008-07-27 03:38:53 -0700496static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct k_sigaction ka;
David S. Miller28e61032008-05-11 02:07:19 -0700499 int restart_syscall;
David S. Miller28e61032008-05-11 02:07:19 -0700500 siginfo_t info;
501 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
David S. Miller1d299bc2011-11-14 20:32:16 -0800503 /* It's a lot of work and synchronization to add a new ptrace
504 * register for GDB to save and restore in order to get
505 * orig_i0 correct for syscall restarts when debugging.
506 *
David S. Millere88d2462011-11-15 12:57:00 -0800507 * Although it should be the case that most of the global
508 * registers are volatile across a system call, glibc already
509 * depends upon that fact that we preserve them. So we can't
510 * just use any global register to save away the orig_i0 value.
511 *
512 * In particular %g2, %g3, %g4, and %g5 are all assumed to be
513 * preserved across a system call trap by various pieces of
514 * code in glibc.
515 *
516 * %g7 is used as the "thread register". %g6 is not used in
517 * any fixed manner. %g6 is used as a scratch register and
518 * a compiler temporary, but it's value is never used across
519 * a system call. Therefore %g6 is usable for orig_i0 storage.
David S. Miller1d299bc2011-11-14 20:32:16 -0800520 */
David S. Miller28e61032008-05-11 02:07:19 -0700521 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
David S. Millere88d2462011-11-15 12:57:00 -0800522 regs->u_regs[UREG_G6] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
David S. Miller28e61032008-05-11 02:07:19 -0700524 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
525
526 /* If the debugger messes with the program counter, it clears
527 * the software "in syscall" bit, directing us to not perform
528 * a syscall restart.
529 */
David S. Miller1d299bc2011-11-14 20:32:16 -0800530 restart_syscall = 0;
531 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C)) {
532 restart_syscall = 1;
David S. Millere88d2462011-11-15 12:57:00 -0800533 orig_i0 = regs->u_regs[UREG_G6];
David S. Miller1d299bc2011-11-14 20:32:16 -0800534 }
535
David S. Miller28e61032008-05-11 02:07:19 -0700536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (signr > 0) {
David S. Miller28e61032008-05-11 02:07:19 -0700538 if (restart_syscall)
539 syscall_restart(orig_i0, regs, &ka.sa);
Al Viroa610d6e2012-05-21 23:42:15 -0400540 handle_signal(signr, &ka, &info, regs);
David S. Miller2d7d5f02006-01-19 02:42:49 -0800541 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
David S. Miller28e61032008-05-11 02:07:19 -0700543 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
545 regs->u_regs[UREG_I0] == ERESTARTSYS ||
546 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
547 /* replay the system call when we are done */
David S. Miller28e61032008-05-11 02:07:19 -0700548 regs->u_regs[UREG_I0] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 regs->pc -= 4;
550 regs->npc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700551 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
David S. Miller28e61032008-05-11 02:07:19 -0700553 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
555 regs->u_regs[UREG_G1] = __NR_restart_syscall;
556 regs->pc -= 4;
557 regs->npc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700558 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
David S. Miller2d7d5f02006-01-19 02:42:49 -0800560
561 /* if there's no signal to deliver, we just put the saved sigmask
562 * back
563 */
Al Viro51a7b442012-05-21 23:33:55 -0400564 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
David S. Miller5a157d52008-07-27 03:38:53 -0700567void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0,
568 unsigned long thread_info_flags)
569{
Al Viro6fd84c02012-05-23 15:28:58 -0400570 if (thread_info_flags & _TIF_SIGPENDING)
David S. Miller5a157d52008-07-27 03:38:53 -0700571 do_signal(regs, orig_i0);
572 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
573 clear_thread_flag(TIF_NOTIFY_RESUME);
574 tracehook_notify_resume(regs);
575 }
576}
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578asmlinkage int
579do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
580 unsigned long sp)
581{
582 int ret = -EFAULT;
583
584 /* First see if old state is wanted. */
585 if (ossptr) {
586 if (put_user(current->sas_ss_sp + current->sas_ss_size,
587 &ossptr->the_stack) ||
588 __put_user(on_sig_stack(sp), &ossptr->cur_status))
589 goto out;
590 }
591
592 /* Now see if we want to update the new state. */
593 if (ssptr) {
594 char *ss_sp;
595
596 if (get_user(ss_sp, &ssptr->the_stack))
597 goto out;
598 /* If the current stack was set with sigaltstack, don't
599 swap stacks while we are on it. */
600 ret = -EPERM;
601 if (current->sas_ss_sp && on_sig_stack(sp))
602 goto out;
603
604 /* Since we don't know the extent of the stack, and we don't
605 track onstack-ness, but rather calculate it, we must
606 presume a size. Ho hum this interface is lossy. */
607 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
608 current->sas_ss_size = SIGSTKSZ;
609 }
610 ret = 0;
611out:
612 return ret;
613}