blob: 1e750e415d7add86854f0c522c1148898d9e1b59 [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;
67
David S. Miller2d7d5f02006-01-19 02:42:49 -080068 current->saved_sigmask = current->blocked;
Matt Flemingfaddf592011-08-11 14:57:02 +010069
70 set &= _BLOCKABLE;
71 siginitset(&blocked, set);
72 set_current_blocked(&blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
David S. Miller2d7d5f02006-01-19 02:42:49 -080074 current->state = TASK_INTERRUPTIBLE;
75 schedule();
76 set_thread_flag(TIF_RESTORE_SIGMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
David S. Miller2d7d5f02006-01-19 02:42:49 -080078 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
David S. Miller2d7d5f02006-01-19 02:42:49 -080081asmlinkage int sys_sigsuspend(old_sigset_t set)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
David S. Miller2d7d5f02006-01-19 02:42:49 -080083 return _sigpause_common(set);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
David S. Miller5526b7e2008-04-27 02:26:36 -070086asmlinkage void do_sigreturn(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
David S. Miller5526b7e2008-04-27 02:26:36 -070088 struct signal_frame __user *sf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 unsigned long up_psr, pc, npc;
90 sigset_t set;
91 __siginfo_fpu_t __user *fpu_save;
David S. Miller55984732011-08-20 17:14:54 -070092 __siginfo_rwin_t __user *rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 int err;
94
David S. Miller5526b7e2008-04-27 02:26:36 -070095 /* Always make any pending restarted system calls return -EINTR */
96 current_thread_info()->restart_block.fn = do_no_restart_syscall;
97
98 synchronize_user_stack();
99
100 sf = (struct signal_frame __user *) regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 /* 1. Make sure we are not getting garbage from the user */
103 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
104 goto segv_and_exit;
105
106 if (((unsigned long) sf) & 3)
107 goto segv_and_exit;
108
109 err = __get_user(pc, &sf->info.si_regs.pc);
110 err |= __get_user(npc, &sf->info.si_regs.npc);
111
112 if ((pc | npc) & 3)
113 goto segv_and_exit;
114
115 /* 2. Restore the state */
116 up_psr = regs->psr;
117 err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
118
119 /* User can only change condition codes and FPU enabling in %psr. */
120 regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
121 | (regs->psr & (PSR_ICC | PSR_EF));
122
David S. Miller28e61032008-05-11 02:07:19 -0700123 /* Prevent syscall restart. */
124 pt_regs_clear_syscall(regs);
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 err |= __get_user(fpu_save, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (fpu_save)
128 err |= restore_fpu_state(regs, fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700129 err |= __get_user(rwin_save, &sf->rwin_save);
130 if (rwin_save)
131 err |= restore_rwin_state(rwin_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 /* This is pretty much atomic, no amount locking would prevent
134 * the races which exist anyways.
135 */
136 err |= __get_user(set.sig[0], &sf->info.si_mask);
137 err |= __copy_from_user(&set.sig[1], &sf->extramask,
138 (_NSIG_WORDS-1) * sizeof(unsigned int));
139
140 if (err)
141 goto segv_and_exit;
142
143 sigdelsetmask(&set, ~_BLOCKABLE);
Matt Flemingfaddf592011-08-11 14:57:02 +0100144 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return;
146
147segv_and_exit:
148 force_sig(SIGSEGV, current);
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
152{
153 struct rt_signal_frame __user *sf;
154 unsigned int psr, pc, npc;
155 __siginfo_fpu_t __user *fpu_save;
David S. Miller55984732011-08-20 17:14:54 -0700156 __siginfo_rwin_t __user *rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 mm_segment_t old_fs;
158 sigset_t set;
159 stack_t st;
160 int err;
161
162 synchronize_user_stack();
163 sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
164 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
165 (((unsigned long) sf) & 0x03))
166 goto segv;
167
168 err = __get_user(pc, &sf->regs.pc);
169 err |= __get_user(npc, &sf->regs.npc);
170 err |= ((pc | npc) & 0x03);
171
172 err |= __get_user(regs->y, &sf->regs.y);
173 err |= __get_user(psr, &sf->regs.psr);
174
175 err |= __copy_from_user(&regs->u_regs[UREG_G1],
176 &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
177
178 regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
179
David S. Miller28e61032008-05-11 02:07:19 -0700180 /* Prevent syscall restart. */
181 pt_regs_clear_syscall(regs);
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 err |= __get_user(fpu_save, &sf->fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700184 if (!err && fpu_save)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 err |= restore_fpu_state(regs, fpu_save);
186 err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
187
188 err |= __copy_from_user(&st, &sf->stack, sizeof(stack_t));
189
190 if (err)
191 goto segv;
192
193 regs->pc = pc;
194 regs->npc = npc;
195
196 /* It is more difficult to avoid calling this function than to
197 * call it and ignore errors.
198 */
199 old_fs = get_fs();
200 set_fs(KERNEL_DS);
201 do_sigaltstack((const stack_t __user *) &st, NULL, (unsigned long)sf);
202 set_fs(old_fs);
203
David S. Miller55984732011-08-20 17:14:54 -0700204 err |= __get_user(rwin_save, &sf->rwin_save);
205 if (!err && rwin_save) {
206 if (restore_rwin_state(rwin_save))
207 goto segv;
208 }
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 sigdelsetmask(&set, ~_BLOCKABLE);
Matt Flemingfaddf592011-08-11 14:57:02 +0100211 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return;
213segv:
214 force_sig(SIGSEGV, current);
215}
216
217/* Checks if the fp is valid */
218static inline int invalid_frame_pointer(void __user *fp, int fplen)
219{
220 if ((((unsigned long) fp) & 7) ||
221 !__access_ok((unsigned long)fp, fplen) ||
222 ((sparc_cpu_model == sun4 || sparc_cpu_model == sun4c) &&
223 ((unsigned long) fp < 0xe0000000 && (unsigned long) fp >= 0x20000000)))
224 return 1;
225
226 return 0;
227}
228
229static inline void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
230{
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700231 unsigned long sp = regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700233 /*
234 * If we are on the alternate signal stack and would overflow it, don't.
235 * Return an always-bogus address instead so we will die with SIGSEGV.
236 */
237 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
238 return (void __user *) -1L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 /* This is the X/Open sanctioned signal stack switching. */
241 if (sa->sa_flags & SA_ONSTACK) {
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700242 if (sas_ss_flags(sp) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 sp = current->sas_ss_sp + current->sas_ss_size;
244 }
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700245
David S. Millerf036d9f2010-02-09 16:18:40 -0800246 sp -= framesize;
247
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700248 /* Always align the stack frame. This handles two cases. First,
249 * sigaltstack need not be mindful of platform specific stack
250 * alignment. Second, if we took this signal because the stack
251 * is not aligned properly, we'd like to take the signal cleanly
252 * and report that.
253 */
David S. Millerf036d9f2010-02-09 16:18:40 -0800254 sp &= ~15UL;
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700255
David S. Millerf036d9f2010-02-09 16:18:40 -0800256 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
David S. Miller392c2182010-09-21 21:41:12 -0700259static int setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
260 int signo, sigset_t *oldset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
David S. Miller5526b7e2008-04-27 02:26:36 -0700262 struct signal_frame __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700263 int sigframe_size, err, wsaved;
264 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 /* 1. Make sure everything is clean */
267 synchronize_user_stack();
268
David S. Miller55984732011-08-20 17:14:54 -0700269 wsaved = current_thread_info()->w_saved;
270
271 sigframe_size = sizeof(*sf);
272 if (used_math())
273 sigframe_size += sizeof(__siginfo_fpu_t);
274 if (wsaved)
275 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
David S. Miller5526b7e2008-04-27 02:26:36 -0700277 sf = (struct signal_frame __user *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 get_sigframe(&ka->sa, regs, sigframe_size);
279
280 if (invalid_frame_pointer(sf, sigframe_size))
281 goto sigill_and_return;
282
David S. Miller55984732011-08-20 17:14:54 -0700283 tail = sf + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 /* 2. Save the current process state */
286 err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
287
288 err |= __put_user(0, &sf->extra_size);
289
290 if (used_math()) {
David S. Miller55984732011-08-20 17:14:54 -0700291 __siginfo_fpu_t __user *fp = tail;
292 tail += sizeof(*fp);
293 err |= save_fpu_state(regs, fp);
294 err |= __put_user(fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 } else {
296 err |= __put_user(0, &sf->fpu_save);
297 }
David S. Miller55984732011-08-20 17:14:54 -0700298 if (wsaved) {
299 __siginfo_rwin_t __user *rwp = tail;
300 tail += sizeof(*rwp);
301 err |= save_rwin_state(wsaved, rwp);
302 err |= __put_user(rwp, &sf->rwin_save);
303 } else {
304 err |= __put_user(0, &sf->rwin_save);
305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 err |= __put_user(oldset->sig[0], &sf->info.si_mask);
308 err |= __copy_to_user(sf->extramask, &oldset->sig[1],
309 (_NSIG_WORDS - 1) * sizeof(unsigned int));
David S. Miller55984732011-08-20 17:14:54 -0700310 if (!wsaved) {
311 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
312 sizeof(struct reg_window32));
313 } else {
314 struct reg_window32 *rp;
315
316 rp = &current_thread_info()->reg_window[wsaved - 1];
317 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (err)
320 goto sigsegv;
321
322 /* 3. signal handler back-trampoline and parameters */
323 regs->u_regs[UREG_FP] = (unsigned long) sf;
324 regs->u_regs[UREG_I0] = signo;
325 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
326 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
327
328 /* 4. signal handler */
329 regs->pc = (unsigned long) ka->sa.sa_handler;
330 regs->npc = (regs->pc + 4);
331
332 /* 5. return to kernel instructions */
333 if (ka->ka_restorer)
334 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
335 else {
336 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
337
338 /* mov __NR_sigreturn, %g1 */
339 err |= __put_user(0x821020d8, &sf->insns[0]);
340
341 /* t 0x10 */
342 err |= __put_user(0x91d02010, &sf->insns[1]);
343 if (err)
344 goto sigsegv;
345
346 /* Flush instruction space. */
347 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
348 }
David S. Miller392c2182010-09-21 21:41:12 -0700349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351sigill_and_return:
352 do_exit(SIGILL);
David S. Miller392c2182010-09-21 21:41:12 -0700353 return -EINVAL;
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355sigsegv:
356 force_sigsegv(signo, current);
David S. Miller392c2182010-09-21 21:41:12 -0700357 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
David S. Miller392c2182010-09-21 21:41:12 -0700360static int setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
361 int signo, sigset_t *oldset, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct rt_signal_frame __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700364 int sigframe_size, wsaved;
365 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 unsigned int psr;
367 int err;
368
369 synchronize_user_stack();
David S. Miller55984732011-08-20 17:14:54 -0700370 wsaved = current_thread_info()->w_saved;
371 sigframe_size = sizeof(*sf);
372 if (used_math())
373 sigframe_size += sizeof(__siginfo_fpu_t);
374 if (wsaved)
375 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 sf = (struct rt_signal_frame __user *)
377 get_sigframe(&ka->sa, regs, sigframe_size);
378 if (invalid_frame_pointer(sf, sigframe_size))
379 goto sigill;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
David S. Miller55984732011-08-20 17:14:54 -0700381 tail = sf + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 err = __put_user(regs->pc, &sf->regs.pc);
383 err |= __put_user(regs->npc, &sf->regs.npc);
384 err |= __put_user(regs->y, &sf->regs.y);
385 psr = regs->psr;
386 if (used_math())
387 psr |= PSR_EF;
388 err |= __put_user(psr, &sf->regs.psr);
389 err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
390 err |= __put_user(0, &sf->extra_size);
391
392 if (psr & PSR_EF) {
David S. Miller55984732011-08-20 17:14:54 -0700393 __siginfo_fpu_t *fp = tail;
394 tail += sizeof(*fp);
395 err |= save_fpu_state(regs, fp);
396 err |= __put_user(fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 } else {
398 err |= __put_user(0, &sf->fpu_save);
399 }
David S. Miller55984732011-08-20 17:14:54 -0700400 if (wsaved) {
401 __siginfo_rwin_t *rwp = tail;
402 tail += sizeof(*rwp);
403 err |= save_rwin_state(wsaved, rwp);
404 err |= __put_user(rwp, &sf->rwin_save);
405 } else {
406 err |= __put_user(0, &sf->rwin_save);
407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
409
410 /* Setup sigaltstack */
411 err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
412 err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
413 err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
414
David S. Miller55984732011-08-20 17:14:54 -0700415 if (!wsaved) {
416 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
417 sizeof(struct reg_window32));
418 } else {
419 struct reg_window32 *rp;
420
421 rp = &current_thread_info()->reg_window[wsaved - 1];
422 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 err |= copy_siginfo_to_user(&sf->info, info);
426
427 if (err)
428 goto sigsegv;
429
430 regs->u_regs[UREG_FP] = (unsigned long) sf;
431 regs->u_regs[UREG_I0] = signo;
432 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
433 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
434
435 regs->pc = (unsigned long) ka->sa.sa_handler;
436 regs->npc = (regs->pc + 4);
437
438 if (ka->ka_restorer)
439 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
440 else {
441 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
442
443 /* mov __NR_sigreturn, %g1 */
444 err |= __put_user(0x821020d8, &sf->insns[0]);
445
446 /* t 0x10 */
447 err |= __put_user(0x91d02010, &sf->insns[1]);
448 if (err)
449 goto sigsegv;
450
451 /* Flush instruction space. */
452 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
453 }
David S. Miller392c2182010-09-21 21:41:12 -0700454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456sigill:
457 do_exit(SIGILL);
David S. Miller392c2182010-09-21 21:41:12 -0700458 return -EINVAL;
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460sigsegv:
461 force_sigsegv(signo, current);
David S. Miller392c2182010-09-21 21:41:12 -0700462 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
David S. Miller392c2182010-09-21 21:41:12 -0700465static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466handle_signal(unsigned long signr, struct k_sigaction *ka,
David S. Millerec98c6b2008-04-20 02:14:23 -0700467 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
David S. Miller392c2182010-09-21 21:41:12 -0700469 int err;
470
David S. Millerec98c6b2008-04-20 02:14:23 -0700471 if (ka->sa.sa_flags & SA_SIGINFO)
David S. Miller392c2182010-09-21 21:41:12 -0700472 err = setup_rt_frame(ka, regs, signr, oldset, info);
David S. Millerec98c6b2008-04-20 02:14:23 -0700473 else
David S. Miller392c2182010-09-21 21:41:12 -0700474 err = setup_frame(ka, regs, signr, oldset);
475
476 if (err)
477 return err;
David S. Millerec98c6b2008-04-20 02:14:23 -0700478
Matt Flemingce24d8a2012-03-21 16:33:46 -0700479 block_sigmask(ka, signr);
David S. Miller392c2182010-09-21 21:41:12 -0700480 tracehook_signal_handler(signr, info, ka, regs, 0);
481
482 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
485static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
486 struct sigaction *sa)
487{
488 switch(regs->u_regs[UREG_I0]) {
489 case ERESTART_RESTARTBLOCK:
490 case ERESTARTNOHAND:
491 no_system_call_restart:
492 regs->u_regs[UREG_I0] = EINTR;
493 regs->psr |= PSR_C;
494 break;
495 case ERESTARTSYS:
496 if (!(sa->sa_flags & SA_RESTART))
497 goto no_system_call_restart;
498 /* fallthrough */
499 case ERESTARTNOINTR:
500 regs->u_regs[UREG_I0] = orig_i0;
501 regs->pc -= 4;
502 regs->npc -= 4;
503 }
504}
505
506/* Note that 'init' is a special process: it doesn't get signals it doesn't
507 * want to handle. Thus you cannot kill init even with a SIGKILL even by
508 * mistake.
509 */
David S. Miller5a157d52008-07-27 03:38:53 -0700510static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 struct k_sigaction ka;
David S. Miller28e61032008-05-11 02:07:19 -0700513 int restart_syscall;
David S. Miller2d7d5f02006-01-19 02:42:49 -0800514 sigset_t *oldset;
David S. Miller28e61032008-05-11 02:07:19 -0700515 siginfo_t info;
516 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
David S. Miller1d299bc2011-11-14 20:32:16 -0800518 /* It's a lot of work and synchronization to add a new ptrace
519 * register for GDB to save and restore in order to get
520 * orig_i0 correct for syscall restarts when debugging.
521 *
David S. Millere88d2462011-11-15 12:57:00 -0800522 * Although it should be the case that most of the global
523 * registers are volatile across a system call, glibc already
524 * depends upon that fact that we preserve them. So we can't
525 * just use any global register to save away the orig_i0 value.
526 *
527 * In particular %g2, %g3, %g4, and %g5 are all assumed to be
528 * preserved across a system call trap by various pieces of
529 * code in glibc.
530 *
531 * %g7 is used as the "thread register". %g6 is not used in
532 * any fixed manner. %g6 is used as a scratch register and
533 * a compiler temporary, but it's value is never used across
534 * a system call. Therefore %g6 is usable for orig_i0 storage.
David S. Miller1d299bc2011-11-14 20:32:16 -0800535 */
David S. Miller28e61032008-05-11 02:07:19 -0700536 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
David S. Millere88d2462011-11-15 12:57:00 -0800537 regs->u_regs[UREG_G6] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
David S. Miller2d7d5f02006-01-19 02:42:49 -0800539 if (test_thread_flag(TIF_RESTORE_SIGMASK))
540 oldset = &current->saved_sigmask;
541 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 oldset = &current->blocked;
543
David S. Miller28e61032008-05-11 02:07:19 -0700544 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
545
546 /* If the debugger messes with the program counter, it clears
547 * the software "in syscall" bit, directing us to not perform
548 * a syscall restart.
549 */
David S. Miller1d299bc2011-11-14 20:32:16 -0800550 restart_syscall = 0;
551 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C)) {
552 restart_syscall = 1;
David S. Millere88d2462011-11-15 12:57:00 -0800553 orig_i0 = regs->u_regs[UREG_G6];
David S. Miller1d299bc2011-11-14 20:32:16 -0800554 }
555
David S. Miller28e61032008-05-11 02:07:19 -0700556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (signr > 0) {
David S. Miller28e61032008-05-11 02:07:19 -0700558 if (restart_syscall)
559 syscall_restart(orig_i0, regs, &ka.sa);
David S. Miller392c2182010-09-21 21:41:12 -0700560 if (handle_signal(signr, &ka, &info, oldset, regs) == 0) {
561 /* a signal was successfully delivered; the saved
562 * sigmask will have been stored in the signal frame,
563 * and will be restored by sigreturn, so we can simply
564 * clear the TIF_RESTORE_SIGMASK flag.
565 */
566 if (test_thread_flag(TIF_RESTORE_SIGMASK))
567 clear_thread_flag(TIF_RESTORE_SIGMASK);
568 }
David S. Miller2d7d5f02006-01-19 02:42:49 -0800569 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
David S. Miller28e61032008-05-11 02:07:19 -0700571 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
573 regs->u_regs[UREG_I0] == ERESTARTSYS ||
574 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
575 /* replay the system call when we are done */
David S. Miller28e61032008-05-11 02:07:19 -0700576 regs->u_regs[UREG_I0] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 regs->pc -= 4;
578 regs->npc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700579 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
David S. Miller28e61032008-05-11 02:07:19 -0700581 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
583 regs->u_regs[UREG_G1] = __NR_restart_syscall;
584 regs->pc -= 4;
585 regs->npc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700586 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
David S. Miller2d7d5f02006-01-19 02:42:49 -0800588
589 /* if there's no signal to deliver, we just put the saved sigmask
590 * back
591 */
592 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
593 clear_thread_flag(TIF_RESTORE_SIGMASK);
David S. Miller27f20dc2011-10-12 12:27:35 -0700594 set_current_blocked(&current->saved_sigmask);
David S. Miller2d7d5f02006-01-19 02:42:49 -0800595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
David S. Miller5a157d52008-07-27 03:38:53 -0700598void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0,
599 unsigned long thread_info_flags)
600{
601 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
602 do_signal(regs, orig_i0);
603 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
604 clear_thread_flag(TIF_NOTIFY_RESUME);
605 tracehook_notify_resume(regs);
David Howellsee18d642009-09-02 09:14:21 +0100606 if (current->replacement_session_keyring)
607 key_replace_session_keyring();
David S. Miller5a157d52008-07-27 03:38:53 -0700608 }
609}
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611asmlinkage int
612do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
613 unsigned long sp)
614{
615 int ret = -EFAULT;
616
617 /* First see if old state is wanted. */
618 if (ossptr) {
619 if (put_user(current->sas_ss_sp + current->sas_ss_size,
620 &ossptr->the_stack) ||
621 __put_user(on_sig_stack(sp), &ossptr->cur_status))
622 goto out;
623 }
624
625 /* Now see if we want to update the new state. */
626 if (ssptr) {
627 char *ss_sp;
628
629 if (get_user(ss_sp, &ssptr->the_stack))
630 goto out;
631 /* If the current stack was set with sigaltstack, don't
632 swap stacks while we are on it. */
633 ret = -EPERM;
634 if (current->sas_ss_sp && on_sig_stack(sp))
635 goto out;
636
637 /* Since we don't know the extent of the stack, and we don't
638 track onstack-ness, but rather calculate it, we must
639 presume a size. Ho hum this interface is lossy. */
640 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
641 current->sas_ss_size = SIGSTKSZ;
642 }
643 ret = 0;
644out:
645 return ret;
646}