Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / arch / s390 / kernel / signal.c
blob2d421d90fada708248c8e678be34d92b08c0cef0
1 /*
2 * arch/s390/kernel/signal.c
4 * Copyright (C) IBM Corp. 1999,2006
5 * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
7 * Based on Intel version
8 *
9 * Copyright (C) 1991, 1992 Linus Torvalds
11 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
14 #include <linux/sched.h>
15 #include <linux/mm.h>
16 #include <linux/smp.h>
17 #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>
27 #include <linux/tracehook.h>
28 #include <linux/syscalls.h>
29 #include <linux/compat.h>
30 #include <asm/ucontext.h>
31 #include <asm/uaccess.h>
32 #include <asm/lowcore.h>
33 #include "entry.h"
35 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
38 typedef struct
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;
47 typedef struct
49 __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
50 __u8 retcode[S390_SYSCALL_SIZE];
51 struct siginfo info;
52 struct ucontext uc;
53 } rt_sigframe;
56 * Atomically swap in the new signal mask, and wait for a signal.
58 SYSCALL_DEFINE3(sigsuspend, int, history0, int, history1, old_sigset_t, mask)
60 sigset_t blocked;
62 current->saved_sigmask = current->blocked;
63 mask &= _BLOCKABLE;
64 siginitset(&blocked, mask);
65 set_current_blocked(&blocked);
66 set_current_state(TASK_INTERRUPTIBLE);
67 schedule();
68 set_restore_sigmask();
69 return -ERESTARTNOHAND;
72 SYSCALL_DEFINE3(sigaction, int, sig, const struct old_sigaction __user *, act,
73 struct old_sigaction __user *, oact)
75 struct k_sigaction new_ka, old_ka;
76 int ret;
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) ||
82 __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))
85 return -EFAULT;
86 siginitset(&new_ka.sa.sa_mask, mask);
89 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
91 if (!ret && oact) {
92 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
93 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
94 __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))
97 return -EFAULT;
100 return ret;
103 SYSCALL_DEFINE2(sigaltstack, const stack_t __user *, uss,
104 stack_t __user *, uoss)
106 struct pt_regs *regs = task_pt_regs(current);
107 return do_sigaltstack(uss, uoss, regs->gprs[15]);
110 /* Returns non-zero on fault. */
111 static int save_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
113 _sigregs user_sregs;
115 save_access_regs(current->thread.acrs);
117 /* Copy a 'clean' PSW mask to the user to avoid leaking
118 information about whether PER is currently on. */
119 user_sregs.regs.psw.mask = psw_user_bits |
120 (regs->psw.mask & PSW_MASK_USER);
121 user_sregs.regs.psw.addr = regs->psw.addr;
122 memcpy(&user_sregs.regs.gprs, &regs->gprs, sizeof(sregs->regs.gprs));
123 memcpy(&user_sregs.regs.acrs, current->thread.acrs,
124 sizeof(sregs->regs.acrs));
126 * We have to store the fp registers to current->thread.fp_regs
127 * to merge them with the emulated registers.
129 save_fp_regs(&current->thread.fp_regs);
130 memcpy(&user_sregs.fpregs, &current->thread.fp_regs,
131 sizeof(s390_fp_regs));
132 return __copy_to_user(sregs, &user_sregs, sizeof(_sigregs));
135 /* Returns positive number on error */
136 static int restore_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
138 int err;
139 _sigregs user_sregs;
141 /* Alwys make any pending restarted system call return -EINTR */
142 current_thread_info()->restart_block.fn = do_no_restart_syscall;
144 err = __copy_from_user(&user_sregs, sregs, sizeof(_sigregs));
145 if (err)
146 return err;
147 /* Use regs->psw.mask instead of psw_user_bits to preserve PER bit. */
148 regs->psw.mask = (regs->psw.mask & ~PSW_MASK_USER) |
149 (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;
154 memcpy(&regs->gprs, &user_sregs.regs.gprs, sizeof(sregs->regs.gprs));
155 memcpy(&current->thread.acrs, &user_sregs.regs.acrs,
156 sizeof(sregs->regs.acrs));
157 restore_access_regs(current->thread.acrs);
159 memcpy(&current->thread.fp_regs, &user_sregs.fpregs,
160 sizeof(s390_fp_regs));
161 current->thread.fp_regs.fpc &= FPC_VALID_MASK;
163 restore_fp_regs(&current->thread.fp_regs);
164 clear_thread_flag(TIF_SYSCALL); /* No longer in a system call */
165 return 0;
168 SYSCALL_DEFINE0(sigreturn)
170 struct pt_regs *regs = task_pt_regs(current);
171 sigframe __user *frame = (sigframe __user *)regs->gprs[15];
172 sigset_t set;
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;
178 sigdelsetmask(&set, ~_BLOCKABLE);
179 set_current_blocked(&set);
180 if (restore_sigregs(regs, &frame->sregs))
181 goto badframe;
182 return regs->gprs[2];
183 badframe:
184 force_sig(SIGSEGV, current);
185 return 0;
188 SYSCALL_DEFINE0(rt_sigreturn)
190 struct pt_regs *regs = task_pt_regs(current);
191 rt_sigframe __user *frame = (rt_sigframe __user *)regs->gprs[15];
192 sigset_t set;
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;
198 sigdelsetmask(&set, ~_BLOCKABLE);
199 set_current_blocked(&set);
200 if (restore_sigregs(regs, &frame->uc.uc_mcontext))
201 goto badframe;
202 if (do_sigaltstack(&frame->uc.uc_stack, NULL,
203 regs->gprs[15]) == -EFAULT)
204 goto badframe;
205 return regs->gprs[2];
206 badframe:
207 force_sig(SIGSEGV, current);
208 return 0;
212 * Set up a signal frame.
217 * Determine which stack to use..
219 static inline void __user *
220 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
222 unsigned long sp;
224 /* Default to using normal stack */
225 sp = regs->gprs[15];
227 /* 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;
231 /* 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;
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;
244 return (void __user *)((sp - frame_size) & -8ul);
247 static inline int map_signal(int sig)
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;
257 static int setup_frame(int sig, struct k_sigaction *ka,
258 sigset_t *set, struct pt_regs * regs)
260 sigframe __user *frame;
262 frame = get_sigframe(ka, regs, sizeof(sigframe));
263 if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe)))
264 goto give_sigsegv;
266 if (frame == (void __user *) -1UL)
267 goto give_sigsegv;
269 if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE))
270 goto give_sigsegv;
272 if (save_sigregs(regs, &frame->sregs))
273 goto give_sigsegv;
274 if (__put_user(&frame->sregs, &frame->sc.sregs))
275 goto give_sigsegv;
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;
290 /* Set up backchain. */
291 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
292 goto give_sigsegv;
294 /* Set up registers for signal handler */
295 regs->gprs[15] = (unsigned long) frame;
296 regs->psw.mask |= PSW_MASK_EA | PSW_MASK_BA; /* 64 bit amode */
297 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
299 regs->gprs[2] = map_signal(sig);
300 regs->gprs[3] = (unsigned long) &frame->sc;
302 /* We forgot to include these in the sigcontext.
303 To avoid breaking binary compatibility, they are passed as args. */
304 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;
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;
315 return 0;
317 give_sigsegv:
318 force_sigsegv(sig, current);
319 return -EFAULT;
322 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
323 sigset_t *set, struct pt_regs * regs)
325 int err = 0;
326 rt_sigframe __user *frame;
328 frame = get_sigframe(ka, regs, sizeof(rt_sigframe));
329 if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe)))
330 goto give_sigsegv;
332 if (frame == (void __user *) -1UL)
333 goto give_sigsegv;
335 if (copy_siginfo_to_user(&frame->info, info))
336 goto give_sigsegv;
338 /* Create the ucontext. */
339 err |= __put_user(0, &frame->uc.uc_flags);
340 err |= __put_user(NULL, &frame->uc.uc_link);
341 err |= __put_user((void __user *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
342 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;
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;
358 if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
359 (u16 __user *)(frame->retcode)))
360 goto give_sigsegv;
363 /* Set up backchain. */
364 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
365 goto give_sigsegv;
367 /* Set up registers for signal handler */
368 regs->gprs[15] = (unsigned long) frame;
369 regs->psw.mask |= PSW_MASK_EA | PSW_MASK_BA; /* 64 bit amode */
370 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
372 regs->gprs[2] = map_signal(sig);
373 regs->gprs[3] = (unsigned long) &frame->info;
374 regs->gprs[4] = (unsigned long) &frame->uc;
375 regs->gprs[5] = task_thread_info(current)->last_break;
376 return 0;
378 give_sigsegv:
379 force_sigsegv(sig, current);
380 return -EFAULT;
383 static int handle_signal(unsigned long sig, struct k_sigaction *ka,
384 siginfo_t *info, sigset_t *oldset,
385 struct pt_regs *regs)
387 sigset_t blocked;
388 int ret;
390 /* Set up the stack frame */
391 if (ka->sa.sa_flags & SA_SIGINFO)
392 ret = setup_rt_frame(sig, ka, info, oldset, regs);
393 else
394 ret = setup_frame(sig, ka, oldset, regs);
395 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;
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.
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.
413 void do_signal(struct pt_regs *regs)
415 siginfo_t info;
416 int signr;
417 struct k_sigaction ka;
418 sigset_t *oldset;
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.
426 if (!user_mode(regs))
427 return;
429 if (test_thread_flag(TIF_RESTORE_SIGMASK))
430 oldset = &current->saved_sigmask;
431 else
432 oldset = &current->blocked;
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.
439 current_thread_info()->system_call =
440 test_thread_flag(TIF_SYSCALL) ? regs->int_code : 0;
441 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
443 if (signr > 0) {
444 /* Whee! Actually deliver the signal. */
445 if (current_thread_info()->system_call) {
446 regs->int_code = current_thread_info()->system_call;
447 /* 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;
458 /* fallthrough */
459 case -ERESTARTNOINTR:
460 regs->gprs[2] = regs->orig_gpr2;
461 regs->psw.addr =
462 __rewind_psw(regs->psw,
463 regs->int_code >> 16);
464 break;
467 /* No longer in a system call */
468 clear_thread_flag(TIF_SYSCALL);
470 if ((is_compat_task() ?
471 handle_signal32(signr, &ka, &info, oldset, regs) :
472 handle_signal(signr, &ka, &info, oldset, regs)) == 0) {
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.
479 if (test_thread_flag(TIF_RESTORE_SIGMASK))
480 clear_thread_flag(TIF_RESTORE_SIGMASK);
483 * Let tracing know that we've done the handler setup.
485 tracehook_signal_handler(signr, &info, &ka, regs,
486 test_thread_flag(TIF_SINGLE_STEP));
488 return;
491 /* No handlers present - check for system call restart */
492 clear_thread_flag(TIF_SYSCALL);
493 if (current_thread_info()->system_call) {
494 regs->int_code = current_thread_info()->system_call;
495 switch (regs->gprs[2]) {
496 case -ERESTART_RESTARTBLOCK:
497 /* Restart with sys_restart_syscall */
498 regs->int_code = __NR_restart_syscall;
499 /* 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;
505 set_thread_flag(TIF_SYSCALL);
506 break;
511 * If there's no signal to deliver, we just put the saved sigmask back.
513 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
514 clear_thread_flag(TIF_RESTORE_SIGMASK);
515 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
519 void do_notify_resume(struct pt_regs *regs)
521 clear_thread_flag(TIF_NOTIFY_RESUME);
522 tracehook_notify_resume(regs);
523 if (current->replacement_session_keyring)
524 key_replace_session_keyring();