blob: 04ede8f04add3f397ca46c8ebee249d85c92213c [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{
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 set &= _BLOCKABLE;
66 spin_lock_irq(&current->sighand->siglock);
David S. Miller2d7d5f02006-01-19 02:42:49 -080067 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 siginitset(&current->blocked, set);
69 recalc_sigpending();
70 spin_unlock_irq(&current->sighand->siglock);
71
David S. Miller2d7d5f02006-01-19 02:42:49 -080072 current->state = TASK_INTERRUPTIBLE;
73 schedule();
74 set_thread_flag(TIF_RESTORE_SIGMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
David S. Miller2d7d5f02006-01-19 02:42:49 -080076 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
David S. Miller2d7d5f02006-01-19 02:42:49 -080079asmlinkage int sys_sigsuspend(old_sigset_t set)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
David S. Miller2d7d5f02006-01-19 02:42:49 -080081 return _sigpause_common(set);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
David S. Miller5526b7e2008-04-27 02:26:36 -070084asmlinkage void do_sigreturn(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
David S. Miller5526b7e2008-04-27 02:26:36 -070086 struct signal_frame __user *sf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 unsigned long up_psr, pc, npc;
88 sigset_t set;
89 __siginfo_fpu_t __user *fpu_save;
David S. Miller55984732011-08-20 17:14:54 -070090 __siginfo_rwin_t __user *rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 int err;
92
David S. Miller5526b7e2008-04-27 02:26:36 -070093 /* Always make any pending restarted system calls return -EINTR */
94 current_thread_info()->restart_block.fn = do_no_restart_syscall;
95
96 synchronize_user_stack();
97
98 sf = (struct signal_frame __user *) regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 /* 1. Make sure we are not getting garbage from the user */
101 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
102 goto segv_and_exit;
103
104 if (((unsigned long) sf) & 3)
105 goto segv_and_exit;
106
107 err = __get_user(pc, &sf->info.si_regs.pc);
108 err |= __get_user(npc, &sf->info.si_regs.npc);
109
110 if ((pc | npc) & 3)
111 goto segv_and_exit;
112
113 /* 2. Restore the state */
114 up_psr = regs->psr;
115 err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
116
117 /* User can only change condition codes and FPU enabling in %psr. */
118 regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
119 | (regs->psr & (PSR_ICC | PSR_EF));
120
David S. Miller28e61032008-05-11 02:07:19 -0700121 /* Prevent syscall restart. */
122 pt_regs_clear_syscall(regs);
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 err |= __get_user(fpu_save, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (fpu_save)
126 err |= restore_fpu_state(regs, fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700127 err |= __get_user(rwin_save, &sf->rwin_save);
128 if (rwin_save)
129 err |= restore_rwin_state(rwin_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 /* This is pretty much atomic, no amount locking would prevent
132 * the races which exist anyways.
133 */
134 err |= __get_user(set.sig[0], &sf->info.si_mask);
135 err |= __copy_from_user(&set.sig[1], &sf->extramask,
136 (_NSIG_WORDS-1) * sizeof(unsigned int));
137
138 if (err)
139 goto segv_and_exit;
140
141 sigdelsetmask(&set, ~_BLOCKABLE);
142 spin_lock_irq(&current->sighand->siglock);
143 current->blocked = set;
144 recalc_sigpending();
145 spin_unlock_irq(&current->sighand->siglock);
146 return;
147
148segv_and_exit:
149 force_sig(SIGSEGV, current);
150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
153{
154 struct rt_signal_frame __user *sf;
155 unsigned int psr, pc, npc;
156 __siginfo_fpu_t __user *fpu_save;
David S. Miller55984732011-08-20 17:14:54 -0700157 __siginfo_rwin_t __user *rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 mm_segment_t old_fs;
159 sigset_t set;
160 stack_t st;
161 int err;
162
163 synchronize_user_stack();
164 sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
165 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
166 (((unsigned long) sf) & 0x03))
167 goto segv;
168
169 err = __get_user(pc, &sf->regs.pc);
170 err |= __get_user(npc, &sf->regs.npc);
171 err |= ((pc | npc) & 0x03);
172
173 err |= __get_user(regs->y, &sf->regs.y);
174 err |= __get_user(psr, &sf->regs.psr);
175
176 err |= __copy_from_user(&regs->u_regs[UREG_G1],
177 &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
178
179 regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
180
David S. Miller28e61032008-05-11 02:07:19 -0700181 /* Prevent syscall restart. */
182 pt_regs_clear_syscall(regs);
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 err |= __get_user(fpu_save, &sf->fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700185 if (!err && fpu_save)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 err |= restore_fpu_state(regs, fpu_save);
187 err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
188
189 err |= __copy_from_user(&st, &sf->stack, sizeof(stack_t));
190
191 if (err)
192 goto segv;
193
194 regs->pc = pc;
195 regs->npc = npc;
196
197 /* It is more difficult to avoid calling this function than to
198 * call it and ignore errors.
199 */
200 old_fs = get_fs();
201 set_fs(KERNEL_DS);
202 do_sigaltstack((const stack_t __user *) &st, NULL, (unsigned long)sf);
203 set_fs(old_fs);
204
David S. Miller55984732011-08-20 17:14:54 -0700205 err |= __get_user(rwin_save, &sf->rwin_save);
206 if (!err && rwin_save) {
207 if (restore_rwin_state(rwin_save))
208 goto segv;
209 }
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 sigdelsetmask(&set, ~_BLOCKABLE);
212 spin_lock_irq(&current->sighand->siglock);
213 current->blocked = set;
214 recalc_sigpending();
215 spin_unlock_irq(&current->sighand->siglock);
216 return;
217segv:
218 force_sig(SIGSEGV, current);
219}
220
221/* Checks if the fp is valid */
222static inline int invalid_frame_pointer(void __user *fp, int fplen)
223{
224 if ((((unsigned long) fp) & 7) ||
225 !__access_ok((unsigned long)fp, fplen) ||
226 ((sparc_cpu_model == sun4 || sparc_cpu_model == sun4c) &&
227 ((unsigned long) fp < 0xe0000000 && (unsigned long) fp >= 0x20000000)))
228 return 1;
229
230 return 0;
231}
232
233static inline void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
234{
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700235 unsigned long sp = regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700237 /*
238 * If we are on the alternate signal stack and would overflow it, don't.
239 * Return an always-bogus address instead so we will die with SIGSEGV.
240 */
241 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
242 return (void __user *) -1L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 /* This is the X/Open sanctioned signal stack switching. */
245 if (sa->sa_flags & SA_ONSTACK) {
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700246 if (sas_ss_flags(sp) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 sp = current->sas_ss_sp + current->sas_ss_size;
248 }
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700249
David S. Millerf036d9f2010-02-09 16:18:40 -0800250 sp -= framesize;
251
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700252 /* Always align the stack frame. This handles two cases. First,
253 * sigaltstack need not be mindful of platform specific stack
254 * alignment. Second, if we took this signal because the stack
255 * is not aligned properly, we'd like to take the signal cleanly
256 * and report that.
257 */
David S. Millerf036d9f2010-02-09 16:18:40 -0800258 sp &= ~15UL;
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700259
David S. Millerf036d9f2010-02-09 16:18:40 -0800260 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
David S. Miller392c2182010-09-21 21:41:12 -0700263static int setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
264 int signo, sigset_t *oldset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
David S. Miller5526b7e2008-04-27 02:26:36 -0700266 struct signal_frame __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700267 int sigframe_size, err, wsaved;
268 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 /* 1. Make sure everything is clean */
271 synchronize_user_stack();
272
David S. Miller55984732011-08-20 17:14:54 -0700273 wsaved = current_thread_info()->w_saved;
274
275 sigframe_size = sizeof(*sf);
276 if (used_math())
277 sigframe_size += sizeof(__siginfo_fpu_t);
278 if (wsaved)
279 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
David S. Miller5526b7e2008-04-27 02:26:36 -0700281 sf = (struct signal_frame __user *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 get_sigframe(&ka->sa, regs, sigframe_size);
283
284 if (invalid_frame_pointer(sf, sigframe_size))
285 goto sigill_and_return;
286
David S. Miller55984732011-08-20 17:14:54 -0700287 tail = sf + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 /* 2. Save the current process state */
290 err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
291
292 err |= __put_user(0, &sf->extra_size);
293
294 if (used_math()) {
David S. Miller55984732011-08-20 17:14:54 -0700295 __siginfo_fpu_t __user *fp = tail;
296 tail += sizeof(*fp);
297 err |= save_fpu_state(regs, fp);
298 err |= __put_user(fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 } else {
300 err |= __put_user(0, &sf->fpu_save);
301 }
David S. Miller55984732011-08-20 17:14:54 -0700302 if (wsaved) {
303 __siginfo_rwin_t __user *rwp = tail;
304 tail += sizeof(*rwp);
305 err |= save_rwin_state(wsaved, rwp);
306 err |= __put_user(rwp, &sf->rwin_save);
307 } else {
308 err |= __put_user(0, &sf->rwin_save);
309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 err |= __put_user(oldset->sig[0], &sf->info.si_mask);
312 err |= __copy_to_user(sf->extramask, &oldset->sig[1],
313 (_NSIG_WORDS - 1) * sizeof(unsigned int));
David S. Miller55984732011-08-20 17:14:54 -0700314 if (!wsaved) {
315 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
316 sizeof(struct reg_window32));
317 } else {
318 struct reg_window32 *rp;
319
320 rp = &current_thread_info()->reg_window[wsaved - 1];
321 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (err)
324 goto sigsegv;
325
326 /* 3. signal handler back-trampoline and parameters */
327 regs->u_regs[UREG_FP] = (unsigned long) sf;
328 regs->u_regs[UREG_I0] = signo;
329 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
330 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
331
332 /* 4. signal handler */
333 regs->pc = (unsigned long) ka->sa.sa_handler;
334 regs->npc = (regs->pc + 4);
335
336 /* 5. return to kernel instructions */
337 if (ka->ka_restorer)
338 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
339 else {
340 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
341
342 /* mov __NR_sigreturn, %g1 */
343 err |= __put_user(0x821020d8, &sf->insns[0]);
344
345 /* t 0x10 */
346 err |= __put_user(0x91d02010, &sf->insns[1]);
347 if (err)
348 goto sigsegv;
349
350 /* Flush instruction space. */
351 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
352 }
David S. Miller392c2182010-09-21 21:41:12 -0700353 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355sigill_and_return:
356 do_exit(SIGILL);
David S. Miller392c2182010-09-21 21:41:12 -0700357 return -EINVAL;
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359sigsegv:
360 force_sigsegv(signo, current);
David S. Miller392c2182010-09-21 21:41:12 -0700361 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
David S. Miller392c2182010-09-21 21:41:12 -0700364static int setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
365 int signo, sigset_t *oldset, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 struct rt_signal_frame __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700368 int sigframe_size, wsaved;
369 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 unsigned int psr;
371 int err;
372
373 synchronize_user_stack();
David S. Miller55984732011-08-20 17:14:54 -0700374 wsaved = current_thread_info()->w_saved;
375 sigframe_size = sizeof(*sf);
376 if (used_math())
377 sigframe_size += sizeof(__siginfo_fpu_t);
378 if (wsaved)
379 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 sf = (struct rt_signal_frame __user *)
381 get_sigframe(&ka->sa, regs, sigframe_size);
382 if (invalid_frame_pointer(sf, sigframe_size))
383 goto sigill;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
David S. Miller55984732011-08-20 17:14:54 -0700385 tail = sf + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 err = __put_user(regs->pc, &sf->regs.pc);
387 err |= __put_user(regs->npc, &sf->regs.npc);
388 err |= __put_user(regs->y, &sf->regs.y);
389 psr = regs->psr;
390 if (used_math())
391 psr |= PSR_EF;
392 err |= __put_user(psr, &sf->regs.psr);
393 err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
394 err |= __put_user(0, &sf->extra_size);
395
396 if (psr & PSR_EF) {
David S. Miller55984732011-08-20 17:14:54 -0700397 __siginfo_fpu_t *fp = tail;
398 tail += sizeof(*fp);
399 err |= save_fpu_state(regs, fp);
400 err |= __put_user(fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 } else {
402 err |= __put_user(0, &sf->fpu_save);
403 }
David S. Miller55984732011-08-20 17:14:54 -0700404 if (wsaved) {
405 __siginfo_rwin_t *rwp = tail;
406 tail += sizeof(*rwp);
407 err |= save_rwin_state(wsaved, rwp);
408 err |= __put_user(rwp, &sf->rwin_save);
409 } else {
410 err |= __put_user(0, &sf->rwin_save);
411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
413
414 /* Setup sigaltstack */
415 err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
416 err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
417 err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
418
David S. Miller55984732011-08-20 17:14:54 -0700419 if (!wsaved) {
420 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
421 sizeof(struct reg_window32));
422 } else {
423 struct reg_window32 *rp;
424
425 rp = &current_thread_info()->reg_window[wsaved - 1];
426 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 err |= copy_siginfo_to_user(&sf->info, info);
430
431 if (err)
432 goto sigsegv;
433
434 regs->u_regs[UREG_FP] = (unsigned long) sf;
435 regs->u_regs[UREG_I0] = signo;
436 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
437 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
438
439 regs->pc = (unsigned long) ka->sa.sa_handler;
440 regs->npc = (regs->pc + 4);
441
442 if (ka->ka_restorer)
443 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
444 else {
445 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
446
447 /* mov __NR_sigreturn, %g1 */
448 err |= __put_user(0x821020d8, &sf->insns[0]);
449
450 /* t 0x10 */
451 err |= __put_user(0x91d02010, &sf->insns[1]);
452 if (err)
453 goto sigsegv;
454
455 /* Flush instruction space. */
456 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
457 }
David S. Miller392c2182010-09-21 21:41:12 -0700458 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460sigill:
461 do_exit(SIGILL);
David S. Miller392c2182010-09-21 21:41:12 -0700462 return -EINVAL;
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464sigsegv:
465 force_sigsegv(signo, current);
David S. Miller392c2182010-09-21 21:41:12 -0700466 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
David S. Miller392c2182010-09-21 21:41:12 -0700469static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470handle_signal(unsigned long signr, struct k_sigaction *ka,
David S. Millerec98c6b2008-04-20 02:14:23 -0700471 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
David S. Miller392c2182010-09-21 21:41:12 -0700473 int err;
474
David S. Millerec98c6b2008-04-20 02:14:23 -0700475 if (ka->sa.sa_flags & SA_SIGINFO)
David S. Miller392c2182010-09-21 21:41:12 -0700476 err = setup_rt_frame(ka, regs, signr, oldset, info);
David S. Millerec98c6b2008-04-20 02:14:23 -0700477 else
David S. Miller392c2182010-09-21 21:41:12 -0700478 err = setup_frame(ka, regs, signr, oldset);
479
480 if (err)
481 return err;
David S. Millerec98c6b2008-04-20 02:14:23 -0700482
Steven Rostedt69be8f12005-08-29 11:44:09 -0400483 spin_lock_irq(&current->sighand->siglock);
484 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
485 if (!(ka->sa.sa_flags & SA_NOMASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 sigaddset(&current->blocked, signr);
Steven Rostedt69be8f12005-08-29 11:44:09 -0400487 recalc_sigpending();
488 spin_unlock_irq(&current->sighand->siglock);
David S. Miller392c2182010-09-21 21:41:12 -0700489
490 tracehook_signal_handler(signr, info, ka, regs, 0);
491
492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
495static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
496 struct sigaction *sa)
497{
498 switch(regs->u_regs[UREG_I0]) {
499 case ERESTART_RESTARTBLOCK:
500 case ERESTARTNOHAND:
501 no_system_call_restart:
502 regs->u_regs[UREG_I0] = EINTR;
503 regs->psr |= PSR_C;
504 break;
505 case ERESTARTSYS:
506 if (!(sa->sa_flags & SA_RESTART))
507 goto no_system_call_restart;
508 /* fallthrough */
509 case ERESTARTNOINTR:
510 regs->u_regs[UREG_I0] = orig_i0;
511 regs->pc -= 4;
512 regs->npc -= 4;
513 }
514}
515
516/* Note that 'init' is a special process: it doesn't get signals it doesn't
517 * want to handle. Thus you cannot kill init even with a SIGKILL even by
518 * mistake.
519 */
David S. Miller5a157d52008-07-27 03:38:53 -0700520static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 struct k_sigaction ka;
David S. Miller28e61032008-05-11 02:07:19 -0700523 int restart_syscall;
David S. Miller2d7d5f02006-01-19 02:42:49 -0800524 sigset_t *oldset;
David S. Miller28e61032008-05-11 02:07:19 -0700525 siginfo_t info;
526 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
David S. Miller28e61032008-05-11 02:07:19 -0700528 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
529 restart_syscall = 1;
530 else
531 restart_syscall = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
David S. Miller2d7d5f02006-01-19 02:42:49 -0800533 if (test_thread_flag(TIF_RESTORE_SIGMASK))
534 oldset = &current->saved_sigmask;
535 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 oldset = &current->blocked;
537
David S. Miller28e61032008-05-11 02:07:19 -0700538 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
539
540 /* If the debugger messes with the program counter, it clears
541 * the software "in syscall" bit, directing us to not perform
542 * a syscall restart.
543 */
544 if (restart_syscall && !pt_regs_is_syscall(regs))
545 restart_syscall = 0;
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (signr > 0) {
David S. Miller28e61032008-05-11 02:07:19 -0700548 if (restart_syscall)
549 syscall_restart(orig_i0, regs, &ka.sa);
David S. Miller392c2182010-09-21 21:41:12 -0700550 if (handle_signal(signr, &ka, &info, oldset, regs) == 0) {
551 /* a signal was successfully delivered; the saved
552 * sigmask will have been stored in the signal frame,
553 * and will be restored by sigreturn, so we can simply
554 * clear the TIF_RESTORE_SIGMASK flag.
555 */
556 if (test_thread_flag(TIF_RESTORE_SIGMASK))
557 clear_thread_flag(TIF_RESTORE_SIGMASK);
558 }
David S. Miller2d7d5f02006-01-19 02:42:49 -0800559 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
David S. Miller28e61032008-05-11 02:07:19 -0700561 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
563 regs->u_regs[UREG_I0] == ERESTARTSYS ||
564 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
565 /* replay the system call when we are done */
David S. Miller28e61032008-05-11 02:07:19 -0700566 regs->u_regs[UREG_I0] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 regs->pc -= 4;
568 regs->npc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700569 pt_regs_clear_syscall(regs);
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] == ERESTART_RESTARTBLOCK) {
573 regs->u_regs[UREG_G1] = __NR_restart_syscall;
574 regs->pc -= 4;
575 regs->npc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700576 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
David S. Miller2d7d5f02006-01-19 02:42:49 -0800578
579 /* if there's no signal to deliver, we just put the saved sigmask
580 * back
581 */
582 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
583 clear_thread_flag(TIF_RESTORE_SIGMASK);
584 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
David S. Miller5a157d52008-07-27 03:38:53 -0700588void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0,
589 unsigned long thread_info_flags)
590{
591 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
592 do_signal(regs, orig_i0);
593 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
594 clear_thread_flag(TIF_NOTIFY_RESUME);
595 tracehook_notify_resume(regs);
David Howellsee18d642009-09-02 09:14:21 +0100596 if (current->replacement_session_keyring)
597 key_replace_session_keyring();
David S. Miller5a157d52008-07-27 03:38:53 -0700598 }
599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601asmlinkage int
602do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
603 unsigned long sp)
604{
605 int ret = -EFAULT;
606
607 /* First see if old state is wanted. */
608 if (ossptr) {
609 if (put_user(current->sas_ss_sp + current->sas_ss_size,
610 &ossptr->the_stack) ||
611 __put_user(on_sig_stack(sp), &ossptr->cur_status))
612 goto out;
613 }
614
615 /* Now see if we want to update the new state. */
616 if (ssptr) {
617 char *ss_sp;
618
619 if (get_user(ss_sp, &ssptr->the_stack))
620 goto out;
621 /* If the current stack was set with sigaltstack, don't
622 swap stacks while we are on it. */
623 ret = -EPERM;
624 if (current->sas_ss_sp && on_sig_stack(sp))
625 goto out;
626
627 /* Since we don't know the extent of the stack, and we don't
628 track onstack-ness, but rather calculate it, we must
629 presume a size. Ho hum this interface is lossy. */
630 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
631 current->sas_ss_size = SIGSTKSZ;
632 }
633 ret = 0;
634out:
635 return ret;
636}