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
9 * Copyright (C) 1991, 1992 Linus Torvalds
11 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
14 #include <linux/sched.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>
35 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
40 __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
44 __u8 retcode[S390_SYSCALL_SIZE];
49 __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
50 __u8 retcode[S390_SYSCALL_SIZE];
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)
62 current->saved_sigmask = current->blocked;
64 siginitset(&blocked, mask);
65 set_current_blocked(&blocked);
66 set_current_state(TASK_INTERRUPTIBLE);
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;
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))
86 siginitset(&new_ka.sa.sa_mask, mask);
89 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
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))
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)
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, ®s->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(¤t->thread.fp_regs);
130 memcpy(&user_sregs.fpregs, ¤t->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)
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));
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 user address space control. */
151 if ((regs->psw.mask & PSW_MASK_ASC) >= (psw_kernel_bits & PSW_MASK_ASC))
152 regs->psw.mask = (psw_user_bits & PSW_MASK_ASC) |
153 (regs->psw.mask & ~PSW_MASK_ASC);
154 /* Check for invalid amode */
155 if (regs->psw.mask & PSW_MASK_EA)
156 regs->psw.mask |= PSW_MASK_BA;
157 regs->psw.addr = user_sregs.regs.psw.addr;
158 memcpy(®s->gprs, &user_sregs.regs.gprs, sizeof(sregs->regs.gprs));
159 memcpy(¤t->thread.acrs, &user_sregs.regs.acrs,
160 sizeof(sregs->regs.acrs));
161 restore_access_regs(current->thread.acrs);
163 memcpy(¤t->thread.fp_regs, &user_sregs.fpregs,
164 sizeof(s390_fp_regs));
165 current->thread.fp_regs.fpc &= FPC_VALID_MASK;
167 restore_fp_regs(¤t->thread.fp_regs);
168 clear_thread_flag(TIF_SYSCALL); /* No longer in a system call */
172 SYSCALL_DEFINE0(sigreturn)
174 struct pt_regs *regs = task_pt_regs(current);
175 sigframe __user *frame = (sigframe __user *)regs->gprs[15];
178 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
180 if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE))
182 sigdelsetmask(&set, ~_BLOCKABLE);
183 set_current_blocked(&set);
184 if (restore_sigregs(regs, &frame->sregs))
186 return regs->gprs[2];
188 force_sig(SIGSEGV, current);
192 SYSCALL_DEFINE0(rt_sigreturn)
194 struct pt_regs *regs = task_pt_regs(current);
195 rt_sigframe __user *frame = (rt_sigframe __user *)regs->gprs[15];
198 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
200 if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set)))
202 sigdelsetmask(&set, ~_BLOCKABLE);
203 set_current_blocked(&set);
204 if (restore_sigregs(regs, &frame->uc.uc_mcontext))
206 if (do_sigaltstack(&frame->uc.uc_stack, NULL,
207 regs->gprs[15]) == -EFAULT)
209 return regs->gprs[2];
211 force_sig(SIGSEGV, current);
216 * Set up a signal frame.
221 * Determine which stack to use..
223 static inline void __user *
224 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
228 /* Default to using normal stack */
231 /* Overflow on alternate signal stack gives SIGSEGV. */
232 if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL))
233 return (void __user *) -1UL;
235 /* This is the X/Open sanctioned signal stack switching. */
236 if (ka->sa.sa_flags & SA_ONSTACK) {
237 if (! sas_ss_flags(sp))
238 sp = current->sas_ss_sp + current->sas_ss_size;
241 /* This is the legacy signal stack switching. */
242 else if (!user_mode(regs) &&
243 !(ka->sa.sa_flags & SA_RESTORER) &&
244 ka->sa.sa_restorer) {
245 sp = (unsigned long) ka->sa.sa_restorer;
248 return (void __user *)((sp - frame_size) & -8ul);
251 static inline int map_signal(int sig)
253 if (current_thread_info()->exec_domain
254 && current_thread_info()->exec_domain->signal_invmap
256 return current_thread_info()->exec_domain->signal_invmap[sig];
261 static int setup_frame(int sig, struct k_sigaction *ka,
262 sigset_t *set, struct pt_regs * regs)
264 sigframe __user *frame;
266 frame = get_sigframe(ka, regs, sizeof(sigframe));
267 if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe)))
270 if (frame == (void __user *) -1UL)
273 if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE))
276 if (save_sigregs(regs, &frame->sregs))
278 if (__put_user(&frame->sregs, &frame->sc.sregs))
281 /* Set up to return from userspace. If provided, use a stub
282 already in userspace. */
283 if (ka->sa.sa_flags & SA_RESTORER) {
284 regs->gprs[14] = (unsigned long)
285 ka->sa.sa_restorer | PSW_ADDR_AMODE;
287 regs->gprs[14] = (unsigned long)
288 frame->retcode | PSW_ADDR_AMODE;
289 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn,
290 (u16 __user *)(frame->retcode)))
294 /* Set up backchain. */
295 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
298 /* Set up registers for signal handler */
299 regs->gprs[15] = (unsigned long) frame;
300 /* Force default amode and default user address space control. */
301 regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA |
302 (psw_user_bits & PSW_MASK_ASC) |
303 (regs->psw.mask & ~PSW_MASK_ASC);
304 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
306 regs->gprs[2] = map_signal(sig);
307 regs->gprs[3] = (unsigned long) &frame->sc;
309 /* We forgot to include these in the sigcontext.
310 To avoid breaking binary compatibility, they are passed as args. */
311 regs->gprs[4] = current->thread.trap_no;
312 regs->gprs[5] = current->thread.prot_addr;
313 regs->gprs[6] = task_thread_info(current)->last_break;
315 /* Place signal number on stack to allow backtrace from handler. */
316 if (__put_user(regs->gprs[2], (int __user *) &frame->signo))
321 force_sigsegv(sig, current);
325 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
326 sigset_t *set, struct pt_regs * regs)
329 rt_sigframe __user *frame;
331 frame = get_sigframe(ka, regs, sizeof(rt_sigframe));
332 if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe)))
335 if (frame == (void __user *) -1UL)
338 if (copy_siginfo_to_user(&frame->info, info))
341 /* Create the ucontext. */
342 err |= __put_user(0, &frame->uc.uc_flags);
343 err |= __put_user(NULL, &frame->uc.uc_link);
344 err |= __put_user((void __user *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
345 err |= __put_user(sas_ss_flags(regs->gprs[15]),
346 &frame->uc.uc_stack.ss_flags);
347 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
348 err |= save_sigregs(regs, &frame->uc.uc_mcontext);
349 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
353 /* Set up to return from userspace. If provided, use a stub
354 already in userspace. */
355 if (ka->sa.sa_flags & SA_RESTORER) {
356 regs->gprs[14] = (unsigned long)
357 ka->sa.sa_restorer | PSW_ADDR_AMODE;
359 regs->gprs[14] = (unsigned long)
360 frame->retcode | PSW_ADDR_AMODE;
361 if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
362 (u16 __user *)(frame->retcode)))
366 /* Set up backchain. */
367 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
370 /* Set up registers for signal handler */
371 regs->gprs[15] = (unsigned long) frame;
372 /* Force default amode and default user address space control. */
373 regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA |
374 (psw_user_bits & PSW_MASK_ASC) |
375 (regs->psw.mask & ~PSW_MASK_ASC);
376 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
378 regs->gprs[2] = map_signal(sig);
379 regs->gprs[3] = (unsigned long) &frame->info;
380 regs->gprs[4] = (unsigned long) &frame->uc;
381 regs->gprs[5] = task_thread_info(current)->last_break;
385 force_sigsegv(sig, current);
389 static int handle_signal(unsigned long sig, struct k_sigaction *ka,
390 siginfo_t *info, sigset_t *oldset,
391 struct pt_regs *regs)
396 /* Set up the stack frame */
397 if (ka->sa.sa_flags & SA_SIGINFO)
398 ret = setup_rt_frame(sig, ka, info, oldset, regs);
400 ret = setup_frame(sig, ka, oldset, regs);
403 sigorsets(&blocked, ¤t->blocked, &ka->sa.sa_mask);
404 if (!(ka->sa.sa_flags & SA_NODEFER))
405 sigaddset(&blocked, sig);
406 set_current_blocked(&blocked);
411 * Note that 'init' is a special process: it doesn't get signals it doesn't
412 * want to handle. Thus you cannot kill init even with a SIGKILL even by
415 * Note that we go through the signals twice: once to check the signals that
416 * the kernel can handle, and then we build all the user-level signal handling
417 * stack-frames in one go after that.
419 void do_signal(struct pt_regs *regs)
423 struct k_sigaction ka;
427 * We want the common case to go fast, which
428 * is why we may in certain cases get here from
429 * kernel mode. Just return without doing anything
432 if (!user_mode(regs))
435 if (test_thread_flag(TIF_RESTORE_SIGMASK))
436 oldset = ¤t->saved_sigmask;
438 oldset = ¤t->blocked;
441 * Get signal to deliver. When running under ptrace, at this point
442 * the debugger may change all our registers, including the system
445 current_thread_info()->system_call =
446 test_thread_flag(TIF_SYSCALL) ? regs->svc_code : 0;
447 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
450 /* Whee! Actually deliver the signal. */
451 if (current_thread_info()->system_call) {
452 regs->svc_code = current_thread_info()->system_call;
453 /* Check for system call restarting. */
454 switch (regs->gprs[2]) {
455 case -ERESTART_RESTARTBLOCK:
456 case -ERESTARTNOHAND:
457 regs->gprs[2] = -EINTR;
460 if (!(ka.sa.sa_flags & SA_RESTART)) {
461 regs->gprs[2] = -EINTR;
465 case -ERESTARTNOINTR:
466 regs->gprs[2] = regs->orig_gpr2;
468 __rewind_psw(regs->psw,
469 regs->svc_code >> 16);
473 /* No longer in a system call */
474 clear_thread_flag(TIF_SYSCALL);
476 if ((is_compat_task() ?
477 handle_signal32(signr, &ka, &info, oldset, regs) :
478 handle_signal(signr, &ka, &info, oldset, regs)) == 0) {
480 * A signal was successfully delivered; the saved
481 * sigmask will have been stored in the signal frame,
482 * and will be restored by sigreturn, so we can simply
483 * clear the TIF_RESTORE_SIGMASK flag.
485 if (test_thread_flag(TIF_RESTORE_SIGMASK))
486 clear_thread_flag(TIF_RESTORE_SIGMASK);
489 * Let tracing know that we've done the handler setup.
491 tracehook_signal_handler(signr, &info, &ka, regs,
492 test_thread_flag(TIF_SINGLE_STEP));
497 /* No handlers present - check for system call restart */
498 clear_thread_flag(TIF_SYSCALL);
499 if (current_thread_info()->system_call) {
500 regs->svc_code = current_thread_info()->system_call;
501 switch (regs->gprs[2]) {
502 case -ERESTART_RESTARTBLOCK:
503 /* Restart with sys_restart_syscall */
504 regs->svc_code = __NR_restart_syscall;
506 case -ERESTARTNOHAND:
508 case -ERESTARTNOINTR:
509 /* Restart system call with magic TIF bit. */
510 regs->gprs[2] = regs->orig_gpr2;
511 set_thread_flag(TIF_SYSCALL);
517 * If there's no signal to deliver, we just put the saved sigmask back.
519 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
520 clear_thread_flag(TIF_RESTORE_SIGMASK);
521 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
525 void do_notify_resume(struct pt_regs *regs)
527 clear_thread_flag(TIF_NOTIFY_RESUME);
528 tracehook_notify_resume(regs);
529 if (current->replacement_session_keyring)
530 key_replace_session_keyring();