blob: 030087a63fb6551ecbfa2a4a1371716b584df0be [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 */
28
David S. Miller55984732011-08-20 17:14:54 -070029#include "sigutil.h"
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
32
33extern void fpsave(unsigned long *fpregs, unsigned long *fsr,
34 void *fpqueue, unsigned long *fpqdepth);
35extern void fpload(unsigned long *fpregs, unsigned long *fsr);
36
David S. Miller5526b7e2008-04-27 02:26:36 -070037struct signal_frame {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct sparc_stackf ss;
Sam Ravnborg4d7b92a2009-01-02 19:32:59 -080039 __siginfo32_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 __siginfo_fpu_t __user *fpu_save;
41 unsigned long insns[2] __attribute__ ((aligned (8)));
42 unsigned int extramask[_NSIG_WORDS - 1];
43 unsigned int extra_size; /* Should be 0 */
David S. Miller55984732011-08-20 17:14:54 -070044 __siginfo_rwin_t __user *rwin_save;
45} __attribute__((aligned(8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47struct rt_signal_frame {
48 struct sparc_stackf ss;
49 siginfo_t info;
50 struct pt_regs regs;
51 sigset_t mask;
52 __siginfo_fpu_t __user *fpu_save;
53 unsigned int insns[2];
54 stack_t stack;
55 unsigned int extra_size; /* Should be 0 */
David S. Miller55984732011-08-20 17:14:54 -070056 __siginfo_rwin_t __user *rwin_save;
57} __attribute__((aligned(8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59/* Align macros */
David S. Miller5526b7e2008-04-27 02:26:36 -070060#define SF_ALIGNEDSZ (((sizeof(struct signal_frame) + 7) & (~7)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define RT_ALIGNEDSZ (((sizeof(struct rt_signal_frame) + 7) & (~7)))
62
David S. Miller2d7d5f02006-01-19 02:42:49 -080063static int _sigpause_common(old_sigset_t set)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Matt Flemingfaddf592011-08-11 14:57:02 +010065 sigset_t blocked;
66
David S. Miller2d7d5f02006-01-19 02:42:49 -080067 current->saved_sigmask = current->blocked;
Matt Flemingfaddf592011-08-11 14:57:02 +010068
69 set &= _BLOCKABLE;
70 siginitset(&blocked, set);
71 set_current_blocked(&blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
David S. Miller2d7d5f02006-01-19 02:42:49 -080073 current->state = TASK_INTERRUPTIBLE;
74 schedule();
75 set_thread_flag(TIF_RESTORE_SIGMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
David S. Miller2d7d5f02006-01-19 02:42:49 -080077 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
David S. Miller2d7d5f02006-01-19 02:42:49 -080080asmlinkage int sys_sigsuspend(old_sigset_t set)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
David S. Miller2d7d5f02006-01-19 02:42:49 -080082 return _sigpause_common(set);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
David S. Miller5526b7e2008-04-27 02:26:36 -070085asmlinkage void do_sigreturn(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
David S. Miller5526b7e2008-04-27 02:26:36 -070087 struct signal_frame __user *sf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 unsigned long up_psr, pc, npc;
89 sigset_t set;
90 __siginfo_fpu_t __user *fpu_save;
David S. Miller55984732011-08-20 17:14:54 -070091 __siginfo_rwin_t __user *rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 int err;
93
David S. Miller5526b7e2008-04-27 02:26:36 -070094 /* Always make any pending restarted system calls return -EINTR */
95 current_thread_info()->restart_block.fn = do_no_restart_syscall;
96
97 synchronize_user_stack();
98
99 sf = (struct signal_frame __user *) regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 /* 1. Make sure we are not getting garbage from the user */
102 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
103 goto segv_and_exit;
104
105 if (((unsigned long) sf) & 3)
106 goto segv_and_exit;
107
108 err = __get_user(pc, &sf->info.si_regs.pc);
109 err |= __get_user(npc, &sf->info.si_regs.npc);
110
111 if ((pc | npc) & 3)
112 goto segv_and_exit;
113
114 /* 2. Restore the state */
115 up_psr = regs->psr;
116 err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
117
118 /* User can only change condition codes and FPU enabling in %psr. */
119 regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
120 | (regs->psr & (PSR_ICC | PSR_EF));
121
David S. Miller28e61032008-05-11 02:07:19 -0700122 /* Prevent syscall restart. */
123 pt_regs_clear_syscall(regs);
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 err |= __get_user(fpu_save, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (fpu_save)
127 err |= restore_fpu_state(regs, fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700128 err |= __get_user(rwin_save, &sf->rwin_save);
129 if (rwin_save)
130 err |= restore_rwin_state(rwin_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 /* This is pretty much atomic, no amount locking would prevent
133 * the races which exist anyways.
134 */
135 err |= __get_user(set.sig[0], &sf->info.si_mask);
136 err |= __copy_from_user(&set.sig[1], &sf->extramask,
137 (_NSIG_WORDS-1) * sizeof(unsigned int));
138
139 if (err)
140 goto segv_and_exit;
141
142 sigdelsetmask(&set, ~_BLOCKABLE);
Matt Flemingfaddf592011-08-11 14:57:02 +0100143 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return;
145
146segv_and_exit:
147 force_sig(SIGSEGV, current);
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
151{
152 struct rt_signal_frame __user *sf;
153 unsigned int psr, pc, npc;
154 __siginfo_fpu_t __user *fpu_save;
David S. Miller55984732011-08-20 17:14:54 -0700155 __siginfo_rwin_t __user *rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 mm_segment_t old_fs;
157 sigset_t set;
158 stack_t st;
159 int err;
160
161 synchronize_user_stack();
162 sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
163 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
164 (((unsigned long) sf) & 0x03))
165 goto segv;
166
167 err = __get_user(pc, &sf->regs.pc);
168 err |= __get_user(npc, &sf->regs.npc);
169 err |= ((pc | npc) & 0x03);
170
171 err |= __get_user(regs->y, &sf->regs.y);
172 err |= __get_user(psr, &sf->regs.psr);
173
174 err |= __copy_from_user(&regs->u_regs[UREG_G1],
175 &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
176
177 regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
178
David S. Miller28e61032008-05-11 02:07:19 -0700179 /* Prevent syscall restart. */
180 pt_regs_clear_syscall(regs);
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 err |= __get_user(fpu_save, &sf->fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700183 if (!err && fpu_save)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 err |= restore_fpu_state(regs, fpu_save);
185 err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
186
187 err |= __copy_from_user(&st, &sf->stack, sizeof(stack_t));
188
189 if (err)
190 goto segv;
191
192 regs->pc = pc;
193 regs->npc = npc;
194
195 /* It is more difficult to avoid calling this function than to
196 * call it and ignore errors.
197 */
198 old_fs = get_fs();
199 set_fs(KERNEL_DS);
200 do_sigaltstack((const stack_t __user *) &st, NULL, (unsigned long)sf);
201 set_fs(old_fs);
202
David S. Miller55984732011-08-20 17:14:54 -0700203 err |= __get_user(rwin_save, &sf->rwin_save);
204 if (!err && rwin_save) {
205 if (restore_rwin_state(rwin_save))
206 goto segv;
207 }
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 sigdelsetmask(&set, ~_BLOCKABLE);
Matt Flemingfaddf592011-08-11 14:57:02 +0100210 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return;
212segv:
213 force_sig(SIGSEGV, current);
214}
215
216/* Checks if the fp is valid */
217static inline int invalid_frame_pointer(void __user *fp, int fplen)
218{
219 if ((((unsigned long) fp) & 7) ||
220 !__access_ok((unsigned long)fp, fplen) ||
221 ((sparc_cpu_model == sun4 || sparc_cpu_model == sun4c) &&
222 ((unsigned long) fp < 0xe0000000 && (unsigned long) fp >= 0x20000000)))
223 return 1;
224
225 return 0;
226}
227
228static inline void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
229{
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700230 unsigned long sp = regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700232 /*
233 * If we are on the alternate signal stack and would overflow it, don't.
234 * Return an always-bogus address instead so we will die with SIGSEGV.
235 */
236 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
237 return (void __user *) -1L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 /* This is the X/Open sanctioned signal stack switching. */
240 if (sa->sa_flags & SA_ONSTACK) {
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700241 if (sas_ss_flags(sp) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 sp = current->sas_ss_sp + current->sas_ss_size;
243 }
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700244
David S. Millerf036d9f2010-02-09 16:18:40 -0800245 sp -= framesize;
246
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700247 /* Always align the stack frame. This handles two cases. First,
248 * sigaltstack need not be mindful of platform specific stack
249 * alignment. Second, if we took this signal because the stack
250 * is not aligned properly, we'd like to take the signal cleanly
251 * and report that.
252 */
David S. Millerf036d9f2010-02-09 16:18:40 -0800253 sp &= ~15UL;
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700254
David S. Millerf036d9f2010-02-09 16:18:40 -0800255 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
David S. Miller392c2182010-09-21 21:41:12 -0700258static int setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
259 int signo, sigset_t *oldset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
David S. Miller5526b7e2008-04-27 02:26:36 -0700261 struct signal_frame __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700262 int sigframe_size, err, wsaved;
263 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 /* 1. Make sure everything is clean */
266 synchronize_user_stack();
267
David S. Miller55984732011-08-20 17:14:54 -0700268 wsaved = current_thread_info()->w_saved;
269
270 sigframe_size = sizeof(*sf);
271 if (used_math())
272 sigframe_size += sizeof(__siginfo_fpu_t);
273 if (wsaved)
274 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
David S. Miller5526b7e2008-04-27 02:26:36 -0700276 sf = (struct signal_frame __user *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 get_sigframe(&ka->sa, regs, sigframe_size);
278
279 if (invalid_frame_pointer(sf, sigframe_size))
280 goto sigill_and_return;
281
David S. Miller55984732011-08-20 17:14:54 -0700282 tail = sf + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 /* 2. Save the current process state */
285 err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
286
287 err |= __put_user(0, &sf->extra_size);
288
289 if (used_math()) {
David S. Miller55984732011-08-20 17:14:54 -0700290 __siginfo_fpu_t __user *fp = tail;
291 tail += sizeof(*fp);
292 err |= save_fpu_state(regs, fp);
293 err |= __put_user(fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 } else {
295 err |= __put_user(0, &sf->fpu_save);
296 }
David S. Miller55984732011-08-20 17:14:54 -0700297 if (wsaved) {
298 __siginfo_rwin_t __user *rwp = tail;
299 tail += sizeof(*rwp);
300 err |= save_rwin_state(wsaved, rwp);
301 err |= __put_user(rwp, &sf->rwin_save);
302 } else {
303 err |= __put_user(0, &sf->rwin_save);
304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 err |= __put_user(oldset->sig[0], &sf->info.si_mask);
307 err |= __copy_to_user(sf->extramask, &oldset->sig[1],
308 (_NSIG_WORDS - 1) * sizeof(unsigned int));
David S. Miller55984732011-08-20 17:14:54 -0700309 if (!wsaved) {
310 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
311 sizeof(struct reg_window32));
312 } else {
313 struct reg_window32 *rp;
314
315 rp = &current_thread_info()->reg_window[wsaved - 1];
316 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (err)
319 goto sigsegv;
320
321 /* 3. signal handler back-trampoline and parameters */
322 regs->u_regs[UREG_FP] = (unsigned long) sf;
323 regs->u_regs[UREG_I0] = signo;
324 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
325 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
326
327 /* 4. signal handler */
328 regs->pc = (unsigned long) ka->sa.sa_handler;
329 regs->npc = (regs->pc + 4);
330
331 /* 5. return to kernel instructions */
332 if (ka->ka_restorer)
333 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
334 else {
335 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
336
337 /* mov __NR_sigreturn, %g1 */
338 err |= __put_user(0x821020d8, &sf->insns[0]);
339
340 /* t 0x10 */
341 err |= __put_user(0x91d02010, &sf->insns[1]);
342 if (err)
343 goto sigsegv;
344
345 /* Flush instruction space. */
346 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
347 }
David S. Miller392c2182010-09-21 21:41:12 -0700348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350sigill_and_return:
351 do_exit(SIGILL);
David S. Miller392c2182010-09-21 21:41:12 -0700352 return -EINVAL;
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354sigsegv:
355 force_sigsegv(signo, current);
David S. Miller392c2182010-09-21 21:41:12 -0700356 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
David S. Miller392c2182010-09-21 21:41:12 -0700359static int setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
360 int signo, sigset_t *oldset, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 struct rt_signal_frame __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700363 int sigframe_size, wsaved;
364 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 unsigned int psr;
366 int err;
367
368 synchronize_user_stack();
David S. Miller55984732011-08-20 17:14:54 -0700369 wsaved = current_thread_info()->w_saved;
370 sigframe_size = sizeof(*sf);
371 if (used_math())
372 sigframe_size += sizeof(__siginfo_fpu_t);
373 if (wsaved)
374 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 sf = (struct rt_signal_frame __user *)
376 get_sigframe(&ka->sa, regs, sigframe_size);
377 if (invalid_frame_pointer(sf, sigframe_size))
378 goto sigill;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
David S. Miller55984732011-08-20 17:14:54 -0700380 tail = sf + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 err = __put_user(regs->pc, &sf->regs.pc);
382 err |= __put_user(regs->npc, &sf->regs.npc);
383 err |= __put_user(regs->y, &sf->regs.y);
384 psr = regs->psr;
385 if (used_math())
386 psr |= PSR_EF;
387 err |= __put_user(psr, &sf->regs.psr);
388 err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
389 err |= __put_user(0, &sf->extra_size);
390
391 if (psr & PSR_EF) {
David S. Miller55984732011-08-20 17:14:54 -0700392 __siginfo_fpu_t *fp = tail;
393 tail += sizeof(*fp);
394 err |= save_fpu_state(regs, fp);
395 err |= __put_user(fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 } else {
397 err |= __put_user(0, &sf->fpu_save);
398 }
David S. Miller55984732011-08-20 17:14:54 -0700399 if (wsaved) {
400 __siginfo_rwin_t *rwp = tail;
401 tail += sizeof(*rwp);
402 err |= save_rwin_state(wsaved, rwp);
403 err |= __put_user(rwp, &sf->rwin_save);
404 } else {
405 err |= __put_user(0, &sf->rwin_save);
406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
408
409 /* Setup sigaltstack */
410 err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
411 err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
412 err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
413
David S. Miller55984732011-08-20 17:14:54 -0700414 if (!wsaved) {
415 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
416 sizeof(struct reg_window32));
417 } else {
418 struct reg_window32 *rp;
419
420 rp = &current_thread_info()->reg_window[wsaved - 1];
421 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 err |= copy_siginfo_to_user(&sf->info, info);
425
426 if (err)
427 goto sigsegv;
428
429 regs->u_regs[UREG_FP] = (unsigned long) sf;
430 regs->u_regs[UREG_I0] = signo;
431 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
432 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
433
434 regs->pc = (unsigned long) ka->sa.sa_handler;
435 regs->npc = (regs->pc + 4);
436
437 if (ka->ka_restorer)
438 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
439 else {
440 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
441
442 /* mov __NR_sigreturn, %g1 */
443 err |= __put_user(0x821020d8, &sf->insns[0]);
444
445 /* t 0x10 */
446 err |= __put_user(0x91d02010, &sf->insns[1]);
447 if (err)
448 goto sigsegv;
449
450 /* Flush instruction space. */
451 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
452 }
David S. Miller392c2182010-09-21 21:41:12 -0700453 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455sigill:
456 do_exit(SIGILL);
David S. Miller392c2182010-09-21 21:41:12 -0700457 return -EINVAL;
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459sigsegv:
460 force_sigsegv(signo, current);
David S. Miller392c2182010-09-21 21:41:12 -0700461 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
David S. Miller392c2182010-09-21 21:41:12 -0700464static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465handle_signal(unsigned long signr, struct k_sigaction *ka,
David S. Millerec98c6b2008-04-20 02:14:23 -0700466 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Matt Flemingfaddf592011-08-11 14:57:02 +0100468 sigset_t blocked;
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 Flemingfaddf592011-08-11 14:57:02 +0100479 sigorsets(&blocked, &current->blocked, &ka->sa.sa_mask);
Steven Rostedt69be8f12005-08-29 11:44:09 -0400480 if (!(ka->sa.sa_flags & SA_NOMASK))
Matt Flemingfaddf592011-08-11 14:57:02 +0100481 sigaddset(&blocked, signr);
482 set_current_blocked(&blocked);
David S. Miller392c2182010-09-21 21:41:12 -0700483
484 tracehook_signal_handler(signr, info, ka, regs, 0);
485
486 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
489static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
490 struct sigaction *sa)
491{
492 switch(regs->u_regs[UREG_I0]) {
493 case ERESTART_RESTARTBLOCK:
494 case ERESTARTNOHAND:
495 no_system_call_restart:
496 regs->u_regs[UREG_I0] = EINTR;
497 regs->psr |= PSR_C;
498 break;
499 case ERESTARTSYS:
500 if (!(sa->sa_flags & SA_RESTART))
501 goto no_system_call_restart;
502 /* fallthrough */
503 case ERESTARTNOINTR:
504 regs->u_regs[UREG_I0] = orig_i0;
505 regs->pc -= 4;
506 regs->npc -= 4;
507 }
508}
509
510/* Note that 'init' is a special process: it doesn't get signals it doesn't
511 * want to handle. Thus you cannot kill init even with a SIGKILL even by
512 * mistake.
513 */
David S. Miller5a157d52008-07-27 03:38:53 -0700514static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct k_sigaction ka;
David S. Miller28e61032008-05-11 02:07:19 -0700517 int restart_syscall;
David S. Miller2d7d5f02006-01-19 02:42:49 -0800518 sigset_t *oldset;
David S. Miller28e61032008-05-11 02:07:19 -0700519 siginfo_t info;
520 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
David S. Miller28e61032008-05-11 02:07:19 -0700522 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
523 restart_syscall = 1;
524 else
525 restart_syscall = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
David S. Miller2d7d5f02006-01-19 02:42:49 -0800527 if (test_thread_flag(TIF_RESTORE_SIGMASK))
528 oldset = &current->saved_sigmask;
529 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 oldset = &current->blocked;
531
David S. Miller28e61032008-05-11 02:07:19 -0700532 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
533
534 /* If the debugger messes with the program counter, it clears
535 * the software "in syscall" bit, directing us to not perform
536 * a syscall restart.
537 */
538 if (restart_syscall && !pt_regs_is_syscall(regs))
539 restart_syscall = 0;
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (signr > 0) {
David S. Miller28e61032008-05-11 02:07:19 -0700542 if (restart_syscall)
543 syscall_restart(orig_i0, regs, &ka.sa);
David S. Miller392c2182010-09-21 21:41:12 -0700544 if (handle_signal(signr, &ka, &info, oldset, regs) == 0) {
545 /* a signal was successfully delivered; the saved
546 * sigmask will have been stored in the signal frame,
547 * and will be restored by sigreturn, so we can simply
548 * clear the TIF_RESTORE_SIGMASK flag.
549 */
550 if (test_thread_flag(TIF_RESTORE_SIGMASK))
551 clear_thread_flag(TIF_RESTORE_SIGMASK);
552 }
David S. Miller2d7d5f02006-01-19 02:42:49 -0800553 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
David S. Miller28e61032008-05-11 02:07:19 -0700555 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
557 regs->u_regs[UREG_I0] == ERESTARTSYS ||
558 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
559 /* replay the system call when we are done */
David S. Miller28e61032008-05-11 02:07:19 -0700560 regs->u_regs[UREG_I0] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 regs->pc -= 4;
562 regs->npc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700563 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
David S. Miller28e61032008-05-11 02:07:19 -0700565 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
567 regs->u_regs[UREG_G1] = __NR_restart_syscall;
568 regs->pc -= 4;
569 regs->npc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700570 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
David S. Miller2d7d5f02006-01-19 02:42:49 -0800572
573 /* if there's no signal to deliver, we just put the saved sigmask
574 * back
575 */
576 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
577 clear_thread_flag(TIF_RESTORE_SIGMASK);
578 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
David S. Miller5a157d52008-07-27 03:38:53 -0700582void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0,
583 unsigned long thread_info_flags)
584{
585 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
586 do_signal(regs, orig_i0);
587 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
588 clear_thread_flag(TIF_NOTIFY_RESUME);
589 tracehook_notify_resume(regs);
David Howellsee18d642009-09-02 09:14:21 +0100590 if (current->replacement_session_keyring)
591 key_replace_session_keyring();
David S. Miller5a157d52008-07-27 03:38:53 -0700592 }
593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595asmlinkage int
596do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
597 unsigned long sp)
598{
599 int ret = -EFAULT;
600
601 /* First see if old state is wanted. */
602 if (ossptr) {
603 if (put_user(current->sas_ss_sp + current->sas_ss_size,
604 &ossptr->the_stack) ||
605 __put_user(on_sig_stack(sp), &ossptr->cur_status))
606 goto out;
607 }
608
609 /* Now see if we want to update the new state. */
610 if (ssptr) {
611 char *ss_sp;
612
613 if (get_user(ss_sp, &ssptr->the_stack))
614 goto out;
615 /* If the current stack was set with sigaltstack, don't
616 swap stacks while we are on it. */
617 ret = -EPERM;
618 if (current->sas_ss_sp && on_sig_stack(sp))
619 goto out;
620
621 /* Since we don't know the extent of the stack, and we don't
622 track onstack-ness, but rather calculate it, we must
623 presume a size. Ho hum this interface is lossy. */
624 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
625 current->sas_ss_size = SIGSTKSZ;
626 }
627 ret = 0;
628out:
629 return ret;
630}