blob: 2d421d90fada708248c8e678be34d92b08c0cef0 [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>
Heiko Carstensa8061702008-04-17 07:46:26 +020033#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
36
37
38typedef struct
39{
40 __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
41 struct sigcontext sc;
42 _sigregs sregs;
43 int signo;
44 __u8 retcode[S390_SYSCALL_SIZE];
45} sigframe;
46
47typedef struct
48{
49 __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
50 __u8 retcode[S390_SYSCALL_SIZE];
51 struct siginfo info;
52 struct ucontext uc;
53} rt_sigframe;
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/*
56 * Atomically swap in the new signal mask, and wait for a signal.
57 */
Heiko Carstens26689452009-01-14 14:14:36 +010058SYSCALL_DEFINE3(sigsuspend, int, history0, int, history1, old_sigset_t, mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Heiko Carstens391c62f2011-08-03 16:44:26 +020060 sigset_t blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Heiko Carstens391c62f2011-08-03 16:44:26 +020062 current->saved_sigmask = current->blocked;
63 mask &= _BLOCKABLE;
64 siginitset(&blocked, mask);
65 set_current_blocked(&blocked);
Martin Schwidefsky0b4d7892010-01-27 10:12:37 +010066 set_current_state(TASK_INTERRUPTIBLE);
Heiko Carstens54dfe5d2006-02-01 03:06:38 -080067 schedule();
Heiko Carstens9e8ed3a2011-08-03 16:44:32 +020068 set_restore_sigmask();
Heiko Carstens54dfe5d2006-02-01 03:06:38 -080069 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
Heiko Carstens26689452009-01-14 14:14:36 +010072SYSCALL_DEFINE3(sigaction, int, sig, const struct old_sigaction __user *, act,
73 struct old_sigaction __user *, oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 struct k_sigaction new_ka, old_ka;
76 int ret;
77
78 if (act) {
79 old_sigset_t mask;
80 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
81 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
Heiko Carstens12bae232006-10-27 12:39:22 +020082 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
83 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
84 __get_user(mask, &act->sa_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 siginitset(&new_ka.sa.sa_mask, mask);
87 }
88
89 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
90
91 if (!ret && oact) {
92 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
93 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
Heiko Carstens12bae232006-10-27 12:39:22 +020094 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
95 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
96 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99
100 return ret;
101}
102
Heiko Carstens26689452009-01-14 14:14:36 +0100103SYSCALL_DEFINE2(sigaltstack, const stack_t __user *, uss,
104 stack_t __user *, uoss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200106 struct pt_regs *regs = task_pt_regs(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return do_sigaltstack(uss, uoss, regs->gprs[15]);
108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/* Returns non-zero on fault. */
111static int save_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
112{
Gerald Schaefer6837a8c2006-09-20 15:59:39 +0200113 _sigregs user_sregs;
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 save_access_regs(current->thread.acrs);
116
117 /* Copy a 'clean' PSW mask to the user to avoid leaking
118 information about whether PER is currently on. */
Martin Schwidefskyd4e81b32011-10-30 15:16:51 +0100119 user_sregs.regs.psw.mask = psw_user_bits |
120 (regs->psw.mask & PSW_MASK_USER);
Martin Schwidefskyb05e3702006-10-04 20:01:58 +0200121 user_sregs.regs.psw.addr = regs->psw.addr;
122 memcpy(&user_sregs.regs.gprs, &regs->gprs, sizeof(sregs->regs.gprs));
Gerald Schaefer6837a8c2006-09-20 15:59:39 +0200123 memcpy(&user_sregs.regs.acrs, current->thread.acrs,
124 sizeof(sregs->regs.acrs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 /*
126 * We have to store the fp registers to current->thread.fp_regs
127 * to merge them with the emulated registers.
128 */
129 save_fp_regs(&current->thread.fp_regs);
Gerald Schaefer6837a8c2006-09-20 15:59:39 +0200130 memcpy(&user_sregs.fpregs, &current->thread.fp_regs,
131 sizeof(s390_fp_regs));
132 return __copy_to_user(sregs, &user_sregs, sizeof(_sigregs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135/* Returns positive number on error */
136static int restore_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
137{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 int err;
Gerald Schaefer6837a8c2006-09-20 15:59:39 +0200139 _sigregs user_sregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 /* Alwys make any pending restarted system call return -EINTR */
142 current_thread_info()->restart_block.fn = do_no_restart_syscall;
143
Gerald Schaefer6837a8c2006-09-20 15:59:39 +0200144 err = __copy_from_user(&user_sregs, sregs, sizeof(_sigregs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (err)
146 return err;
Martin Schwidefskyd4e81b32011-10-30 15:16:51 +0100147 /* Use regs->psw.mask instead of psw_user_bits to preserve PER bit. */
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100148 regs->psw.mask = (regs->psw.mask & ~PSW_MASK_USER) |
Martin Schwidefskyd4e81b32011-10-30 15:16:51 +0100149 (user_sregs.regs.psw.mask & PSW_MASK_USER);
150 /* Check for invalid amode */
151 if (regs->psw.mask & PSW_MASK_EA)
152 regs->psw.mask |= PSW_MASK_BA;
153 regs->psw.addr = user_sregs.regs.psw.addr;
Martin Schwidefskyb05e3702006-10-04 20:01:58 +0200154 memcpy(&regs->gprs, &user_sregs.regs.gprs, sizeof(sregs->regs.gprs));
Gerald Schaefer6837a8c2006-09-20 15:59:39 +0200155 memcpy(&current->thread.acrs, &user_sregs.regs.acrs,
156 sizeof(sregs->regs.acrs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 restore_access_regs(current->thread.acrs);
158
Gerald Schaefer6837a8c2006-09-20 15:59:39 +0200159 memcpy(&current->thread.fp_regs, &user_sregs.fpregs,
160 sizeof(s390_fp_regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 current->thread.fp_regs.fpc &= FPC_VALID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 restore_fp_regs(&current->thread.fp_regs);
Martin Schwidefskyb6ef5bb2011-10-30 15:16:49 +0100164 clear_thread_flag(TIF_SYSCALL); /* No longer in a system call */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return 0;
166}
167
Heiko Carstens26689452009-01-14 14:14:36 +0100168SYSCALL_DEFINE0(sigreturn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200170 struct pt_regs *regs = task_pt_regs(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 sigframe __user *frame = (sigframe __user *)regs->gprs[15];
172 sigset_t set;
173
174 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
175 goto badframe;
176 if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE))
177 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 sigdelsetmask(&set, ~_BLOCKABLE);
Heiko Carstens391c62f2011-08-03 16:44:26 +0200179 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (restore_sigregs(regs, &frame->sregs))
181 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return regs->gprs[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183badframe:
184 force_sig(SIGSEGV, current);
185 return 0;
186}
187
Heiko Carstens26689452009-01-14 14:14:36 +0100188SYSCALL_DEFINE0(rt_sigreturn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Martin Schwidefsky03ff9a22007-04-27 16:01:40 +0200190 struct pt_regs *regs = task_pt_regs(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 rt_sigframe __user *frame = (rt_sigframe __user *)regs->gprs[15];
192 sigset_t set;
193
194 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
195 goto badframe;
196 if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set)))
197 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 sigdelsetmask(&set, ~_BLOCKABLE);
Heiko Carstens391c62f2011-08-03 16:44:26 +0200199 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (restore_sigregs(regs, &frame->uc.uc_mcontext))
201 goto badframe;
Cedric Le Goater4e3df372006-01-06 00:19:10 -0800202 if (do_sigaltstack(&frame->uc.uc_stack, NULL,
203 regs->gprs[15]) == -EFAULT)
204 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return regs->gprs[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206badframe:
207 force_sig(SIGSEGV, current);
208 return 0;
209}
210
211/*
212 * Set up a signal frame.
213 */
214
215
216/*
217 * Determine which stack to use..
218 */
219static inline void __user *
220get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
221{
222 unsigned long sp;
223
224 /* Default to using normal stack */
225 sp = regs->gprs[15];
226
Heiko Carstensde553432008-04-17 07:45:57 +0200227 /* Overflow on alternate signal stack gives SIGSEGV. */
228 if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL))
229 return (void __user *) -1UL;
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /* This is the X/Open sanctioned signal stack switching. */
232 if (ka->sa.sa_flags & SA_ONSTACK) {
233 if (! sas_ss_flags(sp))
234 sp = current->sas_ss_sp + current->sas_ss_size;
235 }
236
237 /* This is the legacy signal stack switching. */
238 else if (!user_mode(regs) &&
239 !(ka->sa.sa_flags & SA_RESTORER) &&
240 ka->sa.sa_restorer) {
241 sp = (unsigned long) ka->sa.sa_restorer;
242 }
243
244 return (void __user *)((sp - frame_size) & -8ul);
245}
246
247static inline int map_signal(int sig)
248{
249 if (current_thread_info()->exec_domain
250 && current_thread_info()->exec_domain->signal_invmap
251 && sig < 32)
252 return current_thread_info()->exec_domain->signal_invmap[sig];
253 else
254 return sig;
255}
256
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800257static int setup_frame(int sig, struct k_sigaction *ka,
258 sigset_t *set, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 sigframe __user *frame;
261
262 frame = get_sigframe(ka, regs, sizeof(sigframe));
263 if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe)))
264 goto give_sigsegv;
265
Heiko Carstensde553432008-04-17 07:45:57 +0200266 if (frame == (void __user *) -1UL)
267 goto give_sigsegv;
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE))
270 goto give_sigsegv;
271
272 if (save_sigregs(regs, &frame->sregs))
273 goto give_sigsegv;
274 if (__put_user(&frame->sregs, &frame->sc.sregs))
275 goto give_sigsegv;
276
277 /* Set up to return from userspace. If provided, use a stub
278 already in userspace. */
279 if (ka->sa.sa_flags & SA_RESTORER) {
280 regs->gprs[14] = (unsigned long)
281 ka->sa.sa_restorer | PSW_ADDR_AMODE;
282 } else {
283 regs->gprs[14] = (unsigned long)
284 frame->retcode | PSW_ADDR_AMODE;
285 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn,
286 (u16 __user *)(frame->retcode)))
287 goto give_sigsegv;
288 }
289
290 /* Set up backchain. */
291 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
292 goto give_sigsegv;
293
294 /* Set up registers for signal handler */
295 regs->gprs[15] = (unsigned long) frame;
Martin Schwidefskyd4e81b32011-10-30 15:16:51 +0100296 regs->psw.mask |= PSW_MASK_EA | PSW_MASK_BA; /* 64 bit amode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
298
299 regs->gprs[2] = map_signal(sig);
300 regs->gprs[3] = (unsigned long) &frame->sc;
301
302 /* We forgot to include these in the sigcontext.
303 To avoid breaking binary compatibility, they are passed as args. */
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100304 if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
305 sig == SIGTRAP || sig == SIGFPE) {
306 /* set extra registers only for synchronous signals */
307 regs->gprs[4] = regs->int_code & 127;
308 regs->gprs[5] = regs->int_parm_long;
309 regs->gprs[6] = task_thread_info(current)->last_break;
310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 /* Place signal number on stack to allow backtrace from handler. */
313 if (__put_user(regs->gprs[2], (int __user *) &frame->signo))
314 goto give_sigsegv;
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800315 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317give_sigsegv:
318 force_sigsegv(sig, current);
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800319 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800322static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 sigset_t *set, struct pt_regs * regs)
324{
325 int err = 0;
326 rt_sigframe __user *frame;
327
328 frame = get_sigframe(ka, regs, sizeof(rt_sigframe));
329 if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe)))
330 goto give_sigsegv;
331
Heiko Carstensde553432008-04-17 07:45:57 +0200332 if (frame == (void __user *) -1UL)
333 goto give_sigsegv;
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (copy_siginfo_to_user(&frame->info, info))
336 goto give_sigsegv;
337
338 /* Create the ucontext. */
339 err |= __put_user(0, &frame->uc.uc_flags);
Al Viroc2814472005-09-29 00:16:02 +0100340 err |= __put_user(NULL, &frame->uc.uc_link);
341 err |= __put_user((void __user *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 err |= __put_user(sas_ss_flags(regs->gprs[15]),
343 &frame->uc.uc_stack.ss_flags);
344 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
345 err |= save_sigregs(regs, &frame->uc.uc_mcontext);
346 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
347 if (err)
348 goto give_sigsegv;
349
350 /* Set up to return from userspace. If provided, use a stub
351 already in userspace. */
352 if (ka->sa.sa_flags & SA_RESTORER) {
353 regs->gprs[14] = (unsigned long)
354 ka->sa.sa_restorer | PSW_ADDR_AMODE;
355 } else {
356 regs->gprs[14] = (unsigned long)
357 frame->retcode | PSW_ADDR_AMODE;
Heiko Carstensb44df3342006-05-01 12:16:15 -0700358 if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
359 (u16 __user *)(frame->retcode)))
360 goto give_sigsegv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362
363 /* Set up backchain. */
364 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
365 goto give_sigsegv;
366
367 /* Set up registers for signal handler */
368 regs->gprs[15] = (unsigned long) frame;
Martin Schwidefskyd4e81b32011-10-30 15:16:51 +0100369 regs->psw.mask |= PSW_MASK_EA | PSW_MASK_BA; /* 64 bit amode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
371
372 regs->gprs[2] = map_signal(sig);
373 regs->gprs[3] = (unsigned long) &frame->info;
374 regs->gprs[4] = (unsigned long) &frame->uc;
Martin Schwidefsky86f25522010-05-17 10:00:05 +0200375 regs->gprs[5] = task_thread_info(current)->last_break;
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378give_sigsegv:
379 force_sigsegv(sig, current);
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800380 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
Heiko Carstens391c62f2011-08-03 16:44:26 +0200383static int handle_signal(unsigned long sig, struct k_sigaction *ka,
384 siginfo_t *info, sigset_t *oldset,
385 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Heiko Carstens391c62f2011-08-03 16:44:26 +0200387 sigset_t blocked;
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800388 int ret;
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /* Set up the stack frame */
391 if (ka->sa.sa_flags & SA_SIGINFO)
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800392 ret = setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 else
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800394 ret = setup_frame(sig, ka, oldset, regs);
Heiko Carstens391c62f2011-08-03 16:44:26 +0200395 if (ret)
396 return ret;
397 sigorsets(&blocked, &current->blocked, &ka->sa.sa_mask);
398 if (!(ka->sa.sa_flags & SA_NODEFER))
399 sigaddset(&blocked, sig);
400 set_current_blocked(&blocked);
401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
404/*
405 * Note that 'init' is a special process: it doesn't get signals it doesn't
406 * want to handle. Thus you cannot kill init even with a SIGKILL even by
407 * mistake.
408 *
409 * Note that we go through the signals twice: once to check the signals that
410 * the kernel can handle, and then we build all the user-level signal handling
411 * stack-frames in one go after that.
412 */
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800413void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 siginfo_t info;
416 int signr;
417 struct k_sigaction ka;
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800418 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 /*
421 * We want the common case to go fast, which
422 * is why we may in certain cases get here from
423 * kernel mode. Just return without doing anything
424 * if so.
425 */
426 if (!user_mode(regs))
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800427 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800429 if (test_thread_flag(TIF_RESTORE_SIGMASK))
430 oldset = &current->saved_sigmask;
431 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 oldset = &current->blocked;
433
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100434 /*
435 * Get signal to deliver. When running under ptrace, at this point
436 * the debugger may change all our registers, including the system
437 * call information.
438 */
Martin Schwidefskyb6ef5bb2011-10-30 15:16:49 +0100439 current_thread_info()->system_call =
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100440 test_thread_flag(TIF_SYSCALL) ? regs->int_code : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 if (signr > 0) {
444 /* Whee! Actually deliver the signal. */
Martin Schwidefskyb6ef5bb2011-10-30 15:16:49 +0100445 if (current_thread_info()->system_call) {
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100446 regs->int_code = current_thread_info()->system_call;
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100447 /* Check for system call restarting. */
448 switch (regs->gprs[2]) {
449 case -ERESTART_RESTARTBLOCK:
450 case -ERESTARTNOHAND:
451 regs->gprs[2] = -EINTR;
452 break;
453 case -ERESTARTSYS:
454 if (!(ka.sa.sa_flags & SA_RESTART)) {
455 regs->gprs[2] = -EINTR;
456 break;
457 }
458 /* fallthrough */
459 case -ERESTARTNOINTR:
460 regs->gprs[2] = regs->orig_gpr2;
Martin Schwidefskyccf45ca2011-10-30 15:16:48 +0100461 regs->psw.addr =
462 __rewind_psw(regs->psw,
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100463 regs->int_code >> 16);
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100464 break;
465 }
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100466 }
Martin Schwidefskyd9ae6772011-12-01 13:32:15 +0100467 /* No longer in a system call */
468 clear_thread_flag(TIF_SYSCALL);
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100469
470 if ((is_compat_task() ?
471 handle_signal32(signr, &ka, &info, oldset, regs) :
472 handle_signal(signr, &ka, &info, oldset, regs)) == 0) {
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800473 /*
474 * A signal was successfully delivered; the saved
475 * sigmask will have been stored in the signal frame,
476 * and will be restored by sigreturn, so we can simply
477 * clear the TIF_RESTORE_SIGMASK flag.
478 */
479 if (test_thread_flag(TIF_RESTORE_SIGMASK))
480 clear_thread_flag(TIF_RESTORE_SIGMASK);
Roland McGrath0ac30be2008-01-26 14:11:22 +0100481
482 /*
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200483 * Let tracing know that we've done the handler setup.
484 */
485 tracehook_signal_handler(signr, &info, &ka, regs,
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100486 test_thread_flag(TIF_SINGLE_STEP));
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800487 }
488 return;
489 }
490
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100491 /* No handlers present - check for system call restart */
Martin Schwidefskyd9ae6772011-12-01 13:32:15 +0100492 clear_thread_flag(TIF_SYSCALL);
Martin Schwidefskyb6ef5bb2011-10-30 15:16:49 +0100493 if (current_thread_info()->system_call) {
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100494 regs->int_code = current_thread_info()->system_call;
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100495 switch (regs->gprs[2]) {
496 case -ERESTART_RESTARTBLOCK:
497 /* Restart with sys_restart_syscall */
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100498 regs->int_code = __NR_restart_syscall;
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100499 /* fallthrough */
500 case -ERESTARTNOHAND:
501 case -ERESTARTSYS:
502 case -ERESTARTNOINTR:
503 /* Restart system call with magic TIF bit. */
504 regs->gprs[2] = regs->orig_gpr2;
Martin Schwidefskyb6ef5bb2011-10-30 15:16:49 +0100505 set_thread_flag(TIF_SYSCALL);
506 break;
Martin Schwidefsky20b40a72011-10-30 15:16:47 +0100507 }
508 }
509
Heiko Carstens54dfe5d2006-02-01 03:06:38 -0800510 /*
511 * If there's no signal to deliver, we just put the saved sigmask back.
512 */
513 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
514 clear_thread_flag(TIF_RESTORE_SIGMASK);
515 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200518
519void do_notify_resume(struct pt_regs *regs)
520{
521 clear_thread_flag(TIF_NOTIFY_RESUME);
522 tracehook_notify_resume(regs);
David Howellsee18d642009-09-02 09:14:21 +0100523 if (current->replacement_session_keyring)
524 key_replace_session_keyring();
Martin Schwidefsky753c4dd2008-10-10 21:33:20 +0200525}