2 * arch/s390/kernel/signal.c
5 * Copyright (C) 1999,2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
6 * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
8 * Based on Intel version
10 * Copyright (C) 1991, 1992 Linus Torvalds
12 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
15 #include <linux/config.h>
16 #include <linux/sched.h>
18 #include <linux/smp.h>
19 #include <linux/smp_lock.h>
20 #include <linux/kernel.h>
21 #include <linux/signal.h>
22 #include <linux/errno.h>
23 #include <linux/wait.h>
24 #include <linux/ptrace.h>
25 #include <linux/unistd.h>
26 #include <linux/stddef.h>
27 #include <linux/tty.h>
28 #include <linux/personality.h>
29 #include <linux/binfmts.h>
30 #include <asm/ucontext.h>
31 #include <asm/uaccess.h>
32 #include <asm/lowcore.h>
34 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
39 __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
43 __u8 retcode[S390_SYSCALL_SIZE];
48 __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
49 __u8 retcode[S390_SYSCALL_SIZE];
54 int do_signal(struct pt_regs *regs, sigset_t *oldset);
57 * Atomically swap in the new signal mask, and wait for a signal.
60 sys_sigsuspend(struct pt_regs * regs, int history0, int history1,
66 spin_lock_irq(¤t->sighand->siglock);
67 saveset = current->blocked;
68 siginitset(¤t->blocked, mask);
70 spin_unlock_irq(¤t->sighand->siglock);
71 regs->gprs[2] = -EINTR;
74 set_current_state(TASK_INTERRUPTIBLE);
76 if (do_signal(regs, &saveset))
82 sys_rt_sigsuspend(struct pt_regs *regs, sigset_t __user *unewset,
85 sigset_t saveset, newset;
87 /* XXX: Don't preclude handling different sized sigset_t's. */
88 if (sigsetsize != sizeof(sigset_t))
91 if (copy_from_user(&newset, unewset, sizeof(newset)))
93 sigdelsetmask(&newset, ~_BLOCKABLE);
95 spin_lock_irq(¤t->sighand->siglock);
96 saveset = current->blocked;
97 current->blocked = newset;
99 spin_unlock_irq(¤t->sighand->siglock);
100 regs->gprs[2] = -EINTR;
103 set_current_state(TASK_INTERRUPTIBLE);
105 if (do_signal(regs, &saveset))
111 sys_sigaction(int sig, const struct old_sigaction __user *act,
112 struct old_sigaction __user *oact)
114 struct k_sigaction new_ka, old_ka;
119 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
120 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
121 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
123 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
124 __get_user(mask, &act->sa_mask);
125 siginitset(&new_ka.sa.sa_mask, mask);
128 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
131 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
132 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
133 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
135 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
136 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
143 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
144 struct pt_regs *regs)
146 return do_sigaltstack(uss, uoss, regs->gprs[15]);
151 /* Returns non-zero on fault. */
152 static int save_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
154 unsigned long old_mask = regs->psw.mask;
157 save_access_regs(current->thread.acrs);
159 /* Copy a 'clean' PSW mask to the user to avoid leaking
160 information about whether PER is currently on. */
161 regs->psw.mask = PSW_MASK_MERGE(PSW_USER_BITS, regs->psw.mask);
162 err = __copy_to_user(&sregs->regs.psw, ®s->psw,
163 sizeof(sregs->regs.psw)+sizeof(sregs->regs.gprs));
164 regs->psw.mask = old_mask;
167 err = __copy_to_user(&sregs->regs.acrs, current->thread.acrs,
168 sizeof(sregs->regs.acrs));
172 * We have to store the fp registers to current->thread.fp_regs
173 * to merge them with the emulated registers.
175 save_fp_regs(¤t->thread.fp_regs);
176 return __copy_to_user(&sregs->fpregs, ¤t->thread.fp_regs,
177 sizeof(s390_fp_regs));
180 /* Returns positive number on error */
181 static int restore_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
183 unsigned long old_mask = regs->psw.mask;
186 /* Alwys make any pending restarted system call return -EINTR */
187 current_thread_info()->restart_block.fn = do_no_restart_syscall;
189 err = __copy_from_user(®s->psw, &sregs->regs.psw,
190 sizeof(sregs->regs.psw)+sizeof(sregs->regs.gprs));
191 regs->psw.mask = PSW_MASK_MERGE(old_mask, regs->psw.mask);
192 regs->psw.addr |= PSW_ADDR_AMODE;
195 err = __copy_from_user(¤t->thread.acrs, &sregs->regs.acrs,
196 sizeof(sregs->regs.acrs));
199 restore_access_regs(current->thread.acrs);
201 err = __copy_from_user(¤t->thread.fp_regs, &sregs->fpregs,
202 sizeof(s390_fp_regs));
203 current->thread.fp_regs.fpc &= FPC_VALID_MASK;
207 restore_fp_regs(¤t->thread.fp_regs);
208 regs->trap = -1; /* disable syscall checks */
212 asmlinkage long sys_sigreturn(struct pt_regs *regs)
214 sigframe __user *frame = (sigframe __user *)regs->gprs[15];
217 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
219 if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE))
222 sigdelsetmask(&set, ~_BLOCKABLE);
223 spin_lock_irq(¤t->sighand->siglock);
224 current->blocked = set;
226 spin_unlock_irq(¤t->sighand->siglock);
228 if (restore_sigregs(regs, &frame->sregs))
231 return regs->gprs[2];
234 force_sig(SIGSEGV, current);
238 asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
240 rt_sigframe __user *frame = (rt_sigframe __user *)regs->gprs[15];
243 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
245 if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set)))
248 sigdelsetmask(&set, ~_BLOCKABLE);
249 spin_lock_irq(¤t->sighand->siglock);
250 current->blocked = set;
252 spin_unlock_irq(¤t->sighand->siglock);
254 if (restore_sigregs(regs, &frame->uc.uc_mcontext))
257 /* It is more difficult to avoid calling this function than to
258 call it and ignore errors. */
259 do_sigaltstack(&frame->uc.uc_stack, NULL, regs->gprs[15]);
260 return regs->gprs[2];
263 force_sig(SIGSEGV, current);
268 * Set up a signal frame.
273 * Determine which stack to use..
275 static inline void __user *
276 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
280 /* Default to using normal stack */
283 /* This is the X/Open sanctioned signal stack switching. */
284 if (ka->sa.sa_flags & SA_ONSTACK) {
285 if (! on_sig_stack(sp))
286 sp = current->sas_ss_sp + current->sas_ss_size;
289 /* This is the legacy signal stack switching. */
290 else if (!user_mode(regs) &&
291 !(ka->sa.sa_flags & SA_RESTORER) &&
292 ka->sa.sa_restorer) {
293 sp = (unsigned long) ka->sa.sa_restorer;
296 return (void __user *)((sp - frame_size) & -8ul);
299 static inline int map_signal(int sig)
301 if (current_thread_info()->exec_domain
302 && current_thread_info()->exec_domain->signal_invmap
304 return current_thread_info()->exec_domain->signal_invmap[sig];
309 static void setup_frame(int sig, struct k_sigaction *ka,
310 sigset_t *set, struct pt_regs * regs)
312 sigframe __user *frame;
314 frame = get_sigframe(ka, regs, sizeof(sigframe));
315 if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe)))
318 if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE))
321 if (save_sigregs(regs, &frame->sregs))
323 if (__put_user(&frame->sregs, &frame->sc.sregs))
326 /* Set up to return from userspace. If provided, use a stub
327 already in userspace. */
328 if (ka->sa.sa_flags & SA_RESTORER) {
329 regs->gprs[14] = (unsigned long)
330 ka->sa.sa_restorer | PSW_ADDR_AMODE;
332 regs->gprs[14] = (unsigned long)
333 frame->retcode | PSW_ADDR_AMODE;
334 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn,
335 (u16 __user *)(frame->retcode)))
339 /* Set up backchain. */
340 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
343 /* Set up registers for signal handler */
344 regs->gprs[15] = (unsigned long) frame;
345 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
347 regs->gprs[2] = map_signal(sig);
348 regs->gprs[3] = (unsigned long) &frame->sc;
350 /* We forgot to include these in the sigcontext.
351 To avoid breaking binary compatibility, they are passed as args. */
352 regs->gprs[4] = current->thread.trap_no;
353 regs->gprs[5] = current->thread.prot_addr;
355 /* Place signal number on stack to allow backtrace from handler. */
356 if (__put_user(regs->gprs[2], (int __user *) &frame->signo))
362 ka->sa.sa_handler = SIG_DFL;
363 force_sig(SIGSEGV, current);
366 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
367 sigset_t *set, struct pt_regs * regs)
370 rt_sigframe __user *frame;
372 frame = get_sigframe(ka, regs, sizeof(rt_sigframe));
373 if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe)))
376 if (copy_siginfo_to_user(&frame->info, info))
379 /* Create the ucontext. */
380 err |= __put_user(0, &frame->uc.uc_flags);
381 err |= __put_user(0, &frame->uc.uc_link);
382 err |= __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
383 err |= __put_user(sas_ss_flags(regs->gprs[15]),
384 &frame->uc.uc_stack.ss_flags);
385 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
386 err |= save_sigregs(regs, &frame->uc.uc_mcontext);
387 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
391 /* Set up to return from userspace. If provided, use a stub
392 already in userspace. */
393 if (ka->sa.sa_flags & SA_RESTORER) {
394 regs->gprs[14] = (unsigned long)
395 ka->sa.sa_restorer | PSW_ADDR_AMODE;
397 regs->gprs[14] = (unsigned long)
398 frame->retcode | PSW_ADDR_AMODE;
399 err |= __put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
400 (u16 __user *)(frame->retcode));
403 /* Set up backchain. */
404 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
407 /* Set up registers for signal handler */
408 regs->gprs[15] = (unsigned long) frame;
409 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
411 regs->gprs[2] = map_signal(sig);
412 regs->gprs[3] = (unsigned long) &frame->info;
413 regs->gprs[4] = (unsigned long) &frame->uc;
418 ka->sa.sa_handler = SIG_DFL;
419 force_sig(SIGSEGV, current);
423 * OK, we're invoking a handler
427 handle_signal(unsigned long sig, siginfo_t *info, sigset_t *oldset,
428 struct pt_regs * regs)
430 struct k_sigaction *ka = ¤t->sighand->action[sig-1];
432 /* Set up the stack frame */
433 if (ka->sa.sa_flags & SA_SIGINFO)
434 setup_rt_frame(sig, ka, info, oldset, regs);
436 setup_frame(sig, ka, oldset, regs);
438 if (ka->sa.sa_flags & SA_ONESHOT)
439 ka->sa.sa_handler = SIG_DFL;
441 if (!(ka->sa.sa_flags & SA_NODEFER)) {
442 spin_lock_irq(¤t->sighand->siglock);
443 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
444 sigaddset(¤t->blocked,sig);
446 spin_unlock_irq(¤t->sighand->siglock);
451 * Note that 'init' is a special process: it doesn't get signals it doesn't
452 * want to handle. Thus you cannot kill init even with a SIGKILL even by
455 * Note that we go through the signals twice: once to check the signals that
456 * the kernel can handle, and then we build all the user-level signal handling
457 * stack-frames in one go after that.
459 int do_signal(struct pt_regs *regs, sigset_t *oldset)
461 unsigned long retval = 0, continue_addr = 0, restart_addr = 0;
466 * We want the common case to go fast, which
467 * is why we may in certain cases get here from
468 * kernel mode. Just return without doing anything
471 if (!user_mode(regs))
475 oldset = ¤t->blocked;
477 /* Are we from a system call? */
478 if (regs->trap == __LC_SVC_OLD_PSW) {
479 continue_addr = regs->psw.addr;
480 restart_addr = continue_addr - regs->ilc;
481 retval = regs->gprs[2];
483 /* Prepare for system call restart. We do this here so that a
484 debugger will see the already changed PSW. */
485 if (retval == -ERESTARTNOHAND ||
486 retval == -ERESTARTSYS ||
487 retval == -ERESTARTNOINTR) {
488 regs->gprs[2] = regs->orig_gpr2;
489 regs->psw.addr = restart_addr;
490 } else if (retval == -ERESTART_RESTARTBLOCK) {
491 regs->gprs[2] = -EINTR;
495 /* Get signal to deliver. When running under ptrace, at this point
496 the debugger may change all our registers ... */
497 signr = get_signal_to_deliver(&info, regs, NULL);
499 /* Depending on the signal settings we may need to revert the
500 decision to restart the system call. */
501 if (signr > 0 && regs->psw.addr == restart_addr) {
502 if (retval == -ERESTARTNOHAND
503 || (retval == -ERESTARTSYS
504 && !(current->sighand->action[signr-1].sa.sa_flags
506 regs->gprs[2] = -EINTR;
507 regs->psw.addr = continue_addr;
512 /* Whee! Actually deliver the signal. */
513 #ifdef CONFIG_S390_SUPPORT
514 if (test_thread_flag(TIF_31BIT)) {
515 extern void handle_signal32(unsigned long sig,
518 struct pt_regs *regs);
519 handle_signal32(signr, &info, oldset, regs);
523 handle_signal(signr, &info, oldset, regs);
527 /* Restart a different system call. */
528 if (retval == -ERESTART_RESTARTBLOCK
529 && regs->psw.addr == continue_addr) {
530 regs->gprs[2] = __NR_restart_syscall;
531 set_thread_flag(TIF_RESTART_SVC);