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 (!access_ok(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 (!access_ok(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 (!access_ok(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 (!access_ok(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 (! sas_ss_flags(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))
361 force_sigsegv(sig, current);
364 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
365 sigset_t *set, struct pt_regs * regs)
368 rt_sigframe __user *frame;
370 frame = get_sigframe(ka, regs, sizeof(rt_sigframe));
371 if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe)))
374 if (copy_siginfo_to_user(&frame->info, info))
377 /* Create the ucontext. */
378 err |= __put_user(0, &frame->uc.uc_flags);
379 err |= __put_user(NULL, &frame->uc.uc_link);
380 err |= __put_user((void __user *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
381 err |= __put_user(sas_ss_flags(regs->gprs[15]),
382 &frame->uc.uc_stack.ss_flags);
383 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
384 err |= save_sigregs(regs, &frame->uc.uc_mcontext);
385 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
389 /* Set up to return from userspace. If provided, use a stub
390 already in userspace. */
391 if (ka->sa.sa_flags & SA_RESTORER) {
392 regs->gprs[14] = (unsigned long)
393 ka->sa.sa_restorer | PSW_ADDR_AMODE;
395 regs->gprs[14] = (unsigned long)
396 frame->retcode | PSW_ADDR_AMODE;
397 err |= __put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
398 (u16 __user *)(frame->retcode));
401 /* Set up backchain. */
402 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
405 /* Set up registers for signal handler */
406 regs->gprs[15] = (unsigned long) frame;
407 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
409 regs->gprs[2] = map_signal(sig);
410 regs->gprs[3] = (unsigned long) &frame->info;
411 regs->gprs[4] = (unsigned long) &frame->uc;
415 force_sigsegv(sig, current);
419 * OK, we're invoking a handler
423 handle_signal(unsigned long sig, struct k_sigaction *ka,
424 siginfo_t *info, sigset_t *oldset, struct pt_regs * regs)
426 /* Set up the stack frame */
427 if (ka->sa.sa_flags & SA_SIGINFO)
428 setup_rt_frame(sig, ka, info, oldset, regs);
430 setup_frame(sig, ka, oldset, regs);
432 spin_lock_irq(¤t->sighand->siglock);
433 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
434 if (!(ka->sa.sa_flags & SA_NODEFER))
435 sigaddset(¤t->blocked,sig);
437 spin_unlock_irq(¤t->sighand->siglock);
441 * Note that 'init' is a special process: it doesn't get signals it doesn't
442 * want to handle. Thus you cannot kill init even with a SIGKILL even by
445 * Note that we go through the signals twice: once to check the signals that
446 * the kernel can handle, and then we build all the user-level signal handling
447 * stack-frames in one go after that.
449 int do_signal(struct pt_regs *regs, sigset_t *oldset)
451 unsigned long retval = 0, continue_addr = 0, restart_addr = 0;
454 struct k_sigaction ka;
457 * We want the common case to go fast, which
458 * is why we may in certain cases get here from
459 * kernel mode. Just return without doing anything
462 if (!user_mode(regs))
466 oldset = ¤t->blocked;
468 /* Are we from a system call? */
469 if (regs->trap == __LC_SVC_OLD_PSW) {
470 continue_addr = regs->psw.addr;
471 restart_addr = continue_addr - regs->ilc;
472 retval = regs->gprs[2];
474 /* Prepare for system call restart. We do this here so that a
475 debugger will see the already changed PSW. */
476 if (retval == -ERESTARTNOHAND ||
477 retval == -ERESTARTSYS ||
478 retval == -ERESTARTNOINTR) {
479 regs->gprs[2] = regs->orig_gpr2;
480 regs->psw.addr = restart_addr;
481 } else if (retval == -ERESTART_RESTARTBLOCK) {
482 regs->gprs[2] = -EINTR;
486 /* Get signal to deliver. When running under ptrace, at this point
487 the debugger may change all our registers ... */
488 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
490 /* Depending on the signal settings we may need to revert the
491 decision to restart the system call. */
492 if (signr > 0 && regs->psw.addr == restart_addr) {
493 if (retval == -ERESTARTNOHAND
494 || (retval == -ERESTARTSYS
495 && !(current->sighand->action[signr-1].sa.sa_flags
497 regs->gprs[2] = -EINTR;
498 regs->psw.addr = continue_addr;
503 /* Whee! Actually deliver the signal. */
504 #ifdef CONFIG_S390_SUPPORT
505 if (test_thread_flag(TIF_31BIT)) {
506 extern void handle_signal32(unsigned long sig,
507 struct k_sigaction *ka,
510 struct pt_regs *regs);
511 handle_signal32(signr, &ka, &info, oldset, regs);
515 handle_signal(signr, &ka, &info, oldset, regs);
519 /* Restart a different system call. */
520 if (retval == -ERESTART_RESTARTBLOCK
521 && regs->psw.addr == continue_addr) {
522 regs->gprs[2] = __NR_restart_syscall;
523 set_thread_flag(TIF_RESTART_SVC);