blob: 74f58e211ac2fe50be38ecffeeec320ba104ba14 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/s390/kernel/signal.c
3 *
Heiko Carstens54dfe5d2006-02-01 03:06:38 -08004 * Copyright (C) IBM Corp. 1999,2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
6 *
7 * Based on Intel version
8 *
9 * Copyright (C) 1991, 1992 Linus Torvalds
10 *
11 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
12 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/sched.h>
15#include <linux/mm.h>
16#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/kernel.h>
18#include <linux/signal.h>
19#include <linux/errno.h>
20#include <linux/wait.h>
21#include <linux/ptrace.h>
22#include <linux/unistd.h>
23#include <linux/stddef.h>
24#include <linux/tty.h>
25#include <linux/personality.h>
26#include <linux/binfmts.h>
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +020027#include <linux/tracehook.h>
Heiko Carstens26689452009-01-14 14:14:36 +010028#include <linux/syscalls.h>
Heiko Carstens77575912009-06-12 10:26:25 +020029#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/ucontext.h>
31#include <asm/uaccess.h>
32#include <asm/lowcore.h>
David Howellsa0616cd2012-03-28 18:30:02 +010033#include <asm/switch_to.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020034#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
37
38
39typedef struct
40{
41 __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
42 struct sigcontext sc;
43 _sigregs sregs;
44 int signo;
45 __u8 retcode[S390_SYSCALL_SIZE];
46} sigframe;
47
48typedef struct
49{
50 __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
51 __u8 retcode[S390_SYSCALL_SIZE];
52 struct siginfo info;
53 struct ucontext uc;
54} rt_sigframe;
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/*
57 * Atomically swap in the new signal mask, and wait for a signal.
58 */
Heiko Carstens26689452009-01-14 14:14:36 +010059SYSCALL_DEFINE3(sigsuspend, int, history0, int, history1, old_sigset_t, mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Heiko Carstens391c62f2011-08-03 16:44:26 +020061 sigset_t blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Heiko Carstens391c62f2011-08-03 16:44:26 +020063 current->saved_sigmask = current->blocked;
64 mask &= _BLOCKABLE;
65 siginitset(&blocked, mask);
66 set_current_blocked(&blocked);
Martin Schwidefsky0b4d7892010-01-27 10:12:37 +010067 set_current_state(TASK_INTERRUPTIBLE);
Heiko Carstens54dfe5d2006-02-01 03:06:38 -080068 schedule();
Heiko Carstens9e8ed3a2011-08-03 16:44:32 +020069 set_restore_sigmask();
Heiko Carstens54dfe5d2006-02-01 03:06:38 -080070 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
Heiko Carstens26689452009-01-14 14:14:36 +010073SYSCALL_DEFINE3(sigaction, int, sig, const struct old_sigaction __user *, act,
74 struct old_sigaction __user *, oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 struct k_sigaction new_ka, old_ka;
77 int ret;
78
79 if (act) {
80 old_sigset_t mask;
81 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
82 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
Heiko Carstens12bae232006-10-27 12:39:22 +020083 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
84 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
85 __get_user(mask, &act->sa_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 siginitset(&new_ka.sa.sa_mask, mask);
88 }
89
90 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
91
92 if (!ret && oact) {
93 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
94 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
Heiko Carstens12bae232006-10-27 12:39:22 +020095 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
96 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
97 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100
101 return ret;
102}
103
Heiko Carstens26689452009-01-14 14:14:36 +0100104SYSCALL_DEFINE2(sigaltstack, const stack_t __user *, uss,
105 stack_t __user *, uoss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200107 struct pt_regs *regs = task_pt_regs(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return do_sigaltstack(uss, uoss, regs->gprs[15]);
109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/* Returns non-zero on fault. */
112static int save_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
113{
Gerald Schaefer6837a8c2006-09-20 15:59:39 +0200114 _sigregs user_sregs;
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 save_access_regs(current->thread.acrs);
117
118 /* Copy a 'clean' PSW mask to the user to avoid leaking
119 information about whether PER is currently on. */
Martin Schwidefskyd4e81b32011-10-30 15:16:51 +0100120 user_sregs.regs.psw.mask = psw_user_bits |
121 (regs->psw.mask & PSW_MASK_USER);
Martin Schwidefskyb05e3702006-10-04 20:01:58 +0200122 user_sregs.regs.psw.addr = regs->psw.addr;
123 memcpy(&user_sregs.regs.gprs, &regs->gprs, sizeof(sregs->regs.gprs));
Gerald Schaefer6837a8c2006-09-20 15:59:39 +0200124 memcpy(&user_sregs.regs.acrs, current->thread.acrs,
125 sizeof(sregs->regs.acrs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 /*
127 * We have to store the fp registers to current->thread.fp_regs
128 * to merge them with the emulated registers.
129 */
130 save_fp_regs(&current->thread.fp_regs);
Gerald Schaefer6837a8c2006-09-20 15:59:39 +0200131 memcpy(&user_sregs.fpregs, &current->thread.fp_regs,
132 sizeof(s390_fp_regs));
133 return __copy_to_user(sregs, &user_sregs, sizeof(_sigregs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136/* Returns positive number on error */
137static int restore_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
138{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 int err;
Gerald Schaefer6837a8c2006-09-20 15:59:39 +0200140 _sigregs user_sregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 /* Alwys make any pending restarted system call return -EINTR */
143 current_thread_info()->restart_block.fn = do_no_restart_syscall;
144
Gerald Schaefer6837a8c2006-09-20 15:59:39 +0200145 err = __copy_from_user(&user_sregs, sregs, sizeof(_sigregs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (err)
147 return err;
Martin Schwidefskyd4e81b32011-10-30 15:16:51 +0100148 /* Use regs->psw.mask instead of psw_user_bits to preserve PER bit. */
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100149 regs->psw.mask = (regs->psw.mask & ~PSW_MASK_USER) |
Martin Schwidefskyd4e81b32011-10-30 15:16:51 +0100150 (user_sregs.regs.psw.mask & PSW_MASK_USER);
Martin Schwidefsky37a42f92012-11-07 10:44:08 +0100151 /* Check for invalid user address space control. */
152 if ((regs->psw.mask & PSW_MASK_ASC) >= (psw_kernel_bits & PSW_MASK_ASC))
153 regs->psw.mask = (psw_user_bits & PSW_MASK_ASC) |
154 (regs->psw.mask & ~PSW_MASK_ASC);
Martin Schwidefskyd4e81b32011-10-30 15:16:51 +0100155 /* Check for invalid amode */
156 if (regs->psw.mask & PSW_MASK_EA)
157 regs->psw.mask |= PSW_MASK_BA;
158 regs->psw.addr = user_sregs.regs.psw.addr;
Martin Schwidefskyb05e3702006-10-04 20:01:58 +0200159 memcpy(&regs->gprs, &user_sregs.regs.gprs, sizeof(sregs->regs.gprs));
Gerald Schaefer6837a8c2006-09-20 15:59:39 +0200160 memcpy(&current->thread.acrs, &user_sregs.regs.acrs,
161 sizeof(sregs->regs.acrs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 restore_access_regs(current->thread.acrs);
163
Gerald Schaefer6837a8c2006-09-20 15:59:39 +0200164 memcpy(&current->thread.fp_regs, &user_sregs.fpregs,
165 sizeof(s390_fp_regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 current->thread.fp_regs.fpc &= FPC_VALID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 restore_fp_regs(&current->thread.fp_regs);
Martin Schwidefskyb6ef5bb2011-10-30 15:16:49 +0100169 clear_thread_flag(TIF_SYSCALL); /* No longer in a system call */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return 0;
171}
172
Heiko Carstens26689452009-01-14 14:14:36 +0100173SYSCALL_DEFINE0(sigreturn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200175 struct pt_regs *regs = task_pt_regs(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 sigframe __user *frame = (sigframe __user *)regs->gprs[15];
177 sigset_t set;
178
179 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
180 goto badframe;
181 if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE))
182 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 sigdelsetmask(&set, ~_BLOCKABLE);
Heiko Carstens391c62f2011-08-03 16:44:26 +0200184 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (restore_sigregs(regs, &frame->sregs))
186 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return regs->gprs[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188badframe:
189 force_sig(SIGSEGV, current);
190 return 0;
191}
192
Heiko Carstens26689452009-01-14 14:14:36 +0100193SYSCALL_DEFINE0(rt_sigreturn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200195 struct pt_regs *regs = task_pt_regs(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 rt_sigframe __user *frame = (rt_sigframe __user *)regs->gprs[15];
197 sigset_t set;
198
199 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
200 goto badframe;
201 if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set)))
202 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 sigdelsetmask(&set, ~_BLOCKABLE);
Heiko Carstens391c62f2011-08-03 16:44:26 +0200204 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (restore_sigregs(regs, &frame->uc.uc_mcontext))
206 goto badframe;
Cedric Le Goater4e3df372006-01-06 00:19:10 -0800207 if (do_sigaltstack(&frame->uc.uc_stack, NULL,
208 regs->gprs[15]) == -EFAULT)
209 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return regs->gprs[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211badframe:
212 force_sig(SIGSEGV, current);
213 return 0;
214}
215
216/*
217 * Set up a signal frame.
218 */
219
220
221/*
222 * Determine which stack to use..
223 */
224static inline void __user *
225get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
226{
227 unsigned long sp;
228
229 /* Default to using normal stack */
230 sp = regs->gprs[15];
231
Heiko Carstensde553432008-04-17 07:45:57 +0200232 /* Overflow on alternate signal stack gives SIGSEGV. */
233 if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL))
234 return (void __user *) -1UL;
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 /* This is the X/Open sanctioned signal stack switching. */
237 if (ka->sa.sa_flags & SA_ONSTACK) {
238 if (! sas_ss_flags(sp))
239 sp = current->sas_ss_sp + current->sas_ss_size;
240 }
241
242 /* This is the legacy signal stack switching. */
243 else if (!user_mode(regs) &&
244 !(ka->sa.sa_flags & SA_RESTORER) &&
245 ka->sa.sa_restorer) {
246 sp = (unsigned long) ka->sa.sa_restorer;
247 }
248
249 return (void __user *)((sp - frame_size) & -8ul);
250}
251
252static inline int map_signal(int sig)
253{
254 if (current_thread_info()->exec_domain
255 && current_thread_info()->exec_domain->signal_invmap
256 && sig < 32)
257 return current_thread_info()->exec_domain->signal_invmap[sig];
258 else
259 return sig;
260}
261
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800262static int setup_frame(int sig, struct k_sigaction *ka,
263 sigset_t *set, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 sigframe __user *frame;
266
267 frame = get_sigframe(ka, regs, sizeof(sigframe));
268 if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe)))
269 goto give_sigsegv;
270
Heiko Carstensde553432008-04-17 07:45:57 +0200271 if (frame == (void __user *) -1UL)
272 goto give_sigsegv;
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE))
275 goto give_sigsegv;
276
277 if (save_sigregs(regs, &frame->sregs))
278 goto give_sigsegv;
279 if (__put_user(&frame->sregs, &frame->sc.sregs))
280 goto give_sigsegv;
281
282 /* Set up to return from userspace. If provided, use a stub
283 already in userspace. */
284 if (ka->sa.sa_flags & SA_RESTORER) {
285 regs->gprs[14] = (unsigned long)
286 ka->sa.sa_restorer | PSW_ADDR_AMODE;
287 } else {
288 regs->gprs[14] = (unsigned long)
289 frame->retcode | PSW_ADDR_AMODE;
290 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn,
291 (u16 __user *)(frame->retcode)))
292 goto give_sigsegv;
293 }
294
295 /* Set up backchain. */
296 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
297 goto give_sigsegv;
298
299 /* Set up registers for signal handler */
300 regs->gprs[15] = (unsigned long) frame;
Martin Schwidefsky37a42f92012-11-07 10:44:08 +0100301 /* Force default amode and default user address space control. */
302 regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA |
303 (psw_user_bits & PSW_MASK_ASC) |
304 (regs->psw.mask & ~PSW_MASK_ASC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
306
307 regs->gprs[2] = map_signal(sig);
308 regs->gprs[3] = (unsigned long) &frame->sc;
309
310 /* We forgot to include these in the sigcontext.
311 To avoid breaking binary compatibility, they are passed as args. */
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100312 if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
313 sig == SIGTRAP || sig == SIGFPE) {
314 /* set extra registers only for synchronous signals */
315 regs->gprs[4] = regs->int_code & 127;
316 regs->gprs[5] = regs->int_parm_long;
317 regs->gprs[6] = task_thread_info(current)->last_break;
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 /* Place signal number on stack to allow backtrace from handler. */
321 if (__put_user(regs->gprs[2], (int __user *) &frame->signo))
322 goto give_sigsegv;
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325give_sigsegv:
326 force_sigsegv(sig, current);
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800327 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800330static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 sigset_t *set, struct pt_regs * regs)
332{
333 int err = 0;
334 rt_sigframe __user *frame;
335
336 frame = get_sigframe(ka, regs, sizeof(rt_sigframe));
337 if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe)))
338 goto give_sigsegv;
339
Heiko Carstensde553432008-04-17 07:45:57 +0200340 if (frame == (void __user *) -1UL)
341 goto give_sigsegv;
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (copy_siginfo_to_user(&frame->info, info))
344 goto give_sigsegv;
345
346 /* Create the ucontext. */
347 err |= __put_user(0, &frame->uc.uc_flags);
Al Viroc2814472005-09-29 00:16:02 +0100348 err |= __put_user(NULL, &frame->uc.uc_link);
349 err |= __put_user((void __user *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 err |= __put_user(sas_ss_flags(regs->gprs[15]),
351 &frame->uc.uc_stack.ss_flags);
352 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
353 err |= save_sigregs(regs, &frame->uc.uc_mcontext);
354 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
355 if (err)
356 goto give_sigsegv;
357
358 /* Set up to return from userspace. If provided, use a stub
359 already in userspace. */
360 if (ka->sa.sa_flags & SA_RESTORER) {
361 regs->gprs[14] = (unsigned long)
362 ka->sa.sa_restorer | PSW_ADDR_AMODE;
363 } else {
364 regs->gprs[14] = (unsigned long)
365 frame->retcode | PSW_ADDR_AMODE;
Heiko Carstensb44df332006-05-01 12:16:15 -0700366 if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
367 (u16 __user *)(frame->retcode)))
368 goto give_sigsegv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370
371 /* Set up backchain. */
372 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
373 goto give_sigsegv;
374
375 /* Set up registers for signal handler */
376 regs->gprs[15] = (unsigned long) frame;
Martin Schwidefsky37a42f92012-11-07 10:44:08 +0100377 /* Force default amode and default user address space control. */
378 regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA |
379 (psw_user_bits & PSW_MASK_ASC) |
380 (regs->psw.mask & ~PSW_MASK_ASC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
382
383 regs->gprs[2] = map_signal(sig);
384 regs->gprs[3] = (unsigned long) &frame->info;
385 regs->gprs[4] = (unsigned long) &frame->uc;
Martin Schwidefsky86f25522010-05-17 10:00:05 +0200386 regs->gprs[5] = task_thread_info(current)->last_break;
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800387 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389give_sigsegv:
390 force_sigsegv(sig, current);
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800391 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Heiko Carstens391c62f2011-08-03 16:44:26 +0200394static int handle_signal(unsigned long sig, struct k_sigaction *ka,
395 siginfo_t *info, sigset_t *oldset,
396 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800398 int ret;
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 /* Set up the stack frame */
401 if (ka->sa.sa_flags & SA_SIGINFO)
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800402 ret = setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 else
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800404 ret = setup_frame(sig, ka, oldset, regs);
Heiko Carstens391c62f2011-08-03 16:44:26 +0200405 if (ret)
406 return ret;
Matt Flemingad252ff2012-03-11 11:59:33 -0400407 block_sigmask(ka, sig);
Heiko Carstens391c62f2011-08-03 16:44:26 +0200408 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
411/*
412 * Note that 'init' is a special process: it doesn't get signals it doesn't
413 * want to handle. Thus you cannot kill init even with a SIGKILL even by
414 * mistake.
415 *
416 * Note that we go through the signals twice: once to check the signals that
417 * the kernel can handle, and then we build all the user-level signal handling
418 * stack-frames in one go after that.
419 */
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800420void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 siginfo_t info;
423 int signr;
424 struct k_sigaction ka;
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800425 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 /*
428 * We want the common case to go fast, which
429 * is why we may in certain cases get here from
430 * kernel mode. Just return without doing anything
431 * if so.
432 */
433 if (!user_mode(regs))
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800434 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800436 if (test_thread_flag(TIF_RESTORE_SIGMASK))
437 oldset = &current->saved_sigmask;
438 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 oldset = &current->blocked;
440
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100441 /*
442 * Get signal to deliver. When running under ptrace, at this point
443 * the debugger may change all our registers, including the system
444 * call information.
445 */
Martin Schwidefskyb6ef5bb2011-10-30 15:16:49 +0100446 current_thread_info()->system_call =
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100447 test_thread_flag(TIF_SYSCALL) ? regs->int_code : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 if (signr > 0) {
451 /* Whee! Actually deliver the signal. */
Martin Schwidefskyb6ef5bb2011-10-30 15:16:49 +0100452 if (current_thread_info()->system_call) {
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100453 regs->int_code = current_thread_info()->system_call;
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100454 /* Check for system call restarting. */
455 switch (regs->gprs[2]) {
456 case -ERESTART_RESTARTBLOCK:
457 case -ERESTARTNOHAND:
458 regs->gprs[2] = -EINTR;
459 break;
460 case -ERESTARTSYS:
461 if (!(ka.sa.sa_flags & SA_RESTART)) {
462 regs->gprs[2] = -EINTR;
463 break;
464 }
465 /* fallthrough */
466 case -ERESTARTNOINTR:
467 regs->gprs[2] = regs->orig_gpr2;
Martin Schwidefskyccf45ca2011-10-30 15:16:48 +0100468 regs->psw.addr =
469 __rewind_psw(regs->psw,
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100470 regs->int_code >> 16);
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100471 break;
472 }
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100473 }
Martin Schwidefskyd9ae6772011-12-01 13:32:15 +0100474 /* No longer in a system call */
475 clear_thread_flag(TIF_SYSCALL);
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100476
477 if ((is_compat_task() ?
478 handle_signal32(signr, &ka, &info, oldset, regs) :
479 handle_signal(signr, &ka, &info, oldset, regs)) == 0) {
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800480 /*
481 * A signal was successfully delivered; the saved
482 * sigmask will have been stored in the signal frame,
483 * and will be restored by sigreturn, so we can simply
484 * clear the TIF_RESTORE_SIGMASK flag.
485 */
486 if (test_thread_flag(TIF_RESTORE_SIGMASK))
487 clear_thread_flag(TIF_RESTORE_SIGMASK);
Roland McGrath0ac30be2008-01-26 14:11:22 +0100488
489 /*
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200490 * Let tracing know that we've done the handler setup.
491 */
492 tracehook_signal_handler(signr, &info, &ka, regs,
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100493 test_thread_flag(TIF_SINGLE_STEP));
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800494 }
495 return;
496 }
497
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100498 /* No handlers present - check for system call restart */
Martin Schwidefskyd9ae6772011-12-01 13:32:15 +0100499 clear_thread_flag(TIF_SYSCALL);
Martin Schwidefskyb6ef5bb2011-10-30 15:16:49 +0100500 if (current_thread_info()->system_call) {
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100501 regs->int_code = current_thread_info()->system_call;
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100502 switch (regs->gprs[2]) {
503 case -ERESTART_RESTARTBLOCK:
504 /* Restart with sys_restart_syscall */
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100505 regs->int_code = __NR_restart_syscall;
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100506 /* fallthrough */
507 case -ERESTARTNOHAND:
508 case -ERESTARTSYS:
509 case -ERESTARTNOINTR:
510 /* Restart system call with magic TIF bit. */
511 regs->gprs[2] = regs->orig_gpr2;
Martin Schwidefskyb6ef5bb2011-10-30 15:16:49 +0100512 set_thread_flag(TIF_SYSCALL);
513 break;
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100514 }
515 }
516
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800517 /*
518 * If there's no signal to deliver, we just put the saved sigmask back.
519 */
520 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
521 clear_thread_flag(TIF_RESTORE_SIGMASK);
522 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200525
526void do_notify_resume(struct pt_regs *regs)
527{
528 clear_thread_flag(TIF_NOTIFY_RESUME);
529 tracehook_notify_resume(regs);
David Howellsee18d642009-09-02 09:14:21 +0100530 if (current->replacement_session_keyring)
531 key_replace_session_keyring();
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200532}