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];
42 __u8 retcode[S390_SYSCALL_SIZE];
47 __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
48 __u8 retcode[S390_SYSCALL_SIZE];
53 int do_signal(struct pt_regs *regs, sigset_t *oldset);
56 * Atomically swap in the new signal mask, and wait for a signal.
59 sys_sigsuspend(struct pt_regs * regs, int history0, int history1,
65 spin_lock_irq(¤t->sighand->siglock);
66 saveset = current->blocked;
67 siginitset(¤t->blocked, mask);
69 spin_unlock_irq(¤t->sighand->siglock);
70 regs->gprs[2] = -EINTR;
73 set_current_state(TASK_INTERRUPTIBLE);
75 if (do_signal(regs, &saveset))
81 sys_rt_sigsuspend(struct pt_regs * regs,sigset_t *unewset, size_t sigsetsize)
83 sigset_t saveset, newset;
85 /* XXX: Don't preclude handling different sized sigset_t's. */
86 if (sigsetsize != sizeof(sigset_t))
89 if (copy_from_user(&newset, unewset, sizeof(newset)))
91 sigdelsetmask(&newset, ~_BLOCKABLE);
93 spin_lock_irq(¤t->sighand->siglock);
94 saveset = current->blocked;
95 current->blocked = newset;
97 spin_unlock_irq(¤t->sighand->siglock);
98 regs->gprs[2] = -EINTR;
101 set_current_state(TASK_INTERRUPTIBLE);
103 if (do_signal(regs, &saveset))
109 sys_sigaction(int sig, const struct old_sigaction *act,
110 struct old_sigaction *oact)
112 struct k_sigaction new_ka, old_ka;
117 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
118 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
119 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
121 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
122 __get_user(mask, &act->sa_mask);
123 siginitset(&new_ka.sa.sa_mask, mask);
126 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
129 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
130 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
131 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
133 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
134 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
141 sys_sigaltstack(const stack_t *uss, stack_t *uoss, struct pt_regs *regs)
143 return do_sigaltstack(uss, uoss, regs->gprs[15]);
148 /* Returns non-zero on fault. */
149 static int save_sigregs(struct pt_regs *regs, _sigregs *sregs)
151 unsigned long old_mask = regs->psw.mask;
154 save_access_regs(current->thread.acrs);
156 /* Copy a 'clean' PSW mask to the user to avoid leaking
157 information about whether PER is currently on. */
158 regs->psw.mask = PSW_MASK_MERGE(PSW_USER_BITS, regs->psw.mask);
159 err = __copy_to_user(&sregs->regs.psw, ®s->psw,
160 sizeof(sregs->regs.psw)+sizeof(sregs->regs.gprs));
161 regs->psw.mask = old_mask;
164 err = __copy_to_user(&sregs->regs.acrs, current->thread.acrs,
165 sizeof(sregs->regs.acrs));
169 * We have to store the fp registers to current->thread.fp_regs
170 * to merge them with the emulated registers.
172 save_fp_regs(¤t->thread.fp_regs);
173 return __copy_to_user(&sregs->fpregs, ¤t->thread.fp_regs,
174 sizeof(s390_fp_regs));
177 /* Returns positive number on error */
178 static int restore_sigregs(struct pt_regs *regs, _sigregs *sregs)
180 unsigned long old_mask = regs->psw.mask;
183 /* Alwys make any pending restarted system call return -EINTR */
184 current_thread_info()->restart_block.fn = do_no_restart_syscall;
186 err = __copy_from_user(®s->psw, &sregs->regs.psw,
187 sizeof(sregs->regs.psw)+sizeof(sregs->regs.gprs));
188 regs->psw.mask = PSW_MASK_MERGE(old_mask, regs->psw.mask);
189 regs->psw.addr |= PSW_ADDR_AMODE;
192 err = __copy_from_user(¤t->thread.acrs, &sregs->regs.acrs,
193 sizeof(sregs->regs.acrs));
196 restore_access_regs(current->thread.acrs);
198 err = __copy_from_user(¤t->thread.fp_regs, &sregs->fpregs,
199 sizeof(s390_fp_regs));
200 current->thread.fp_regs.fpc &= FPC_VALID_MASK;
204 restore_fp_regs(¤t->thread.fp_regs);
205 regs->trap = -1; /* disable syscall checks */
209 asmlinkage long sys_sigreturn(struct pt_regs *regs)
211 sigframe *frame = (sigframe *)regs->gprs[15];
214 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
216 if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE))
219 sigdelsetmask(&set, ~_BLOCKABLE);
220 spin_lock_irq(¤t->sighand->siglock);
221 current->blocked = set;
223 spin_unlock_irq(¤t->sighand->siglock);
225 if (restore_sigregs(regs, &frame->sregs))
228 return regs->gprs[2];
231 force_sig(SIGSEGV, current);
235 asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
237 rt_sigframe *frame = (rt_sigframe *)regs->gprs[15];
240 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
242 if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set)))
245 sigdelsetmask(&set, ~_BLOCKABLE);
246 spin_lock_irq(¤t->sighand->siglock);
247 current->blocked = set;
249 spin_unlock_irq(¤t->sighand->siglock);
251 if (restore_sigregs(regs, &frame->uc.uc_mcontext))
254 /* It is more difficult to avoid calling this function than to
255 call it and ignore errors. */
256 do_sigaltstack(&frame->uc.uc_stack, NULL, regs->gprs[15]);
257 return regs->gprs[2];
260 force_sig(SIGSEGV, current);
265 * Set up a signal frame.
270 * Determine which stack to use..
273 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
277 /* Default to using normal stack */
280 /* This is the X/Open sanctioned signal stack switching. */
281 if (ka->sa.sa_flags & SA_ONSTACK) {
282 if (! on_sig_stack(sp))
283 sp = current->sas_ss_sp + current->sas_ss_size;
286 /* This is the legacy signal stack switching. */
287 else if (!user_mode(regs) &&
288 !(ka->sa.sa_flags & SA_RESTORER) &&
289 ka->sa.sa_restorer) {
290 sp = (unsigned long) ka->sa.sa_restorer;
293 return (void *)((sp - frame_size) & -8ul);
296 static inline int map_signal(int sig)
298 if (current_thread_info()->exec_domain
299 && current_thread_info()->exec_domain->signal_invmap
301 return current_thread_info()->exec_domain->signal_invmap[sig];
306 static void setup_frame(int sig, struct k_sigaction *ka,
307 sigset_t *set, struct pt_regs * regs)
309 sigframe *frame = get_sigframe(ka, regs, sizeof(sigframe));
310 if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe)))
313 if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE))
316 if (save_sigregs(regs, &frame->sregs))
318 if (__put_user(&frame->sregs, &frame->sc.sregs))
321 /* Set up to return from userspace. If provided, use a stub
322 already in userspace. */
323 if (ka->sa.sa_flags & SA_RESTORER) {
324 regs->gprs[14] = (unsigned long)
325 ka->sa.sa_restorer | PSW_ADDR_AMODE;
327 regs->gprs[14] = (unsigned long)
328 frame->retcode | PSW_ADDR_AMODE;
329 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn,
330 (u16 *)(frame->retcode)))
334 /* Set up backchain. */
335 if (__put_user(regs->gprs[15], (addr_t *) frame))
338 /* Set up registers for signal handler */
339 regs->gprs[15] = (unsigned long) frame;
340 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
342 regs->gprs[2] = map_signal(sig);
343 regs->gprs[3] = (unsigned long) &frame->sc;
345 /* We forgot to include these in the sigcontext.
346 To avoid breaking binary compatibility, they are passed as args. */
347 regs->gprs[4] = current->thread.trap_no;
348 regs->gprs[5] = current->thread.prot_addr;
353 ka->sa.sa_handler = SIG_DFL;
354 force_sig(SIGSEGV, current);
357 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
358 sigset_t *set, struct pt_regs * regs)
361 rt_sigframe *frame = get_sigframe(ka, regs, sizeof(rt_sigframe));
362 if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe)))
365 if (copy_siginfo_to_user(&frame->info, info))
368 /* Create the ucontext. */
369 err |= __put_user(0, &frame->uc.uc_flags);
370 err |= __put_user(0, &frame->uc.uc_link);
371 err |= __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
372 err |= __put_user(sas_ss_flags(regs->gprs[15]),
373 &frame->uc.uc_stack.ss_flags);
374 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
375 err |= save_sigregs(regs, &frame->uc.uc_mcontext);
376 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
380 /* Set up to return from userspace. If provided, use a stub
381 already in userspace. */
382 if (ka->sa.sa_flags & SA_RESTORER) {
383 regs->gprs[14] = (unsigned long)
384 ka->sa.sa_restorer | PSW_ADDR_AMODE;
386 regs->gprs[14] = (unsigned long)
387 frame->retcode | PSW_ADDR_AMODE;
388 err |= __put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
389 (u16 *)(frame->retcode));
392 /* Set up backchain. */
393 if (__put_user(regs->gprs[15], (addr_t *) frame))
396 /* Set up registers for signal handler */
397 regs->gprs[15] = (unsigned long) frame;
398 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
400 regs->gprs[2] = map_signal(sig);
401 regs->gprs[3] = (unsigned long) &frame->info;
402 regs->gprs[4] = (unsigned long) &frame->uc;
407 ka->sa.sa_handler = SIG_DFL;
408 force_sig(SIGSEGV, current);
412 * OK, we're invoking a handler
416 handle_signal(unsigned long sig, siginfo_t *info, sigset_t *oldset,
417 struct pt_regs * regs)
419 struct k_sigaction *ka = ¤t->sighand->action[sig-1];
421 /* Set up the stack frame */
422 if (ka->sa.sa_flags & SA_SIGINFO)
423 setup_rt_frame(sig, ka, info, oldset, regs);
425 setup_frame(sig, ka, oldset, regs);
427 if (ka->sa.sa_flags & SA_ONESHOT)
428 ka->sa.sa_handler = SIG_DFL;
430 if (!(ka->sa.sa_flags & SA_NODEFER)) {
431 spin_lock_irq(¤t->sighand->siglock);
432 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
433 sigaddset(¤t->blocked,sig);
435 spin_unlock_irq(¤t->sighand->siglock);
440 * Note that 'init' is a special process: it doesn't get signals it doesn't
441 * want to handle. Thus you cannot kill init even with a SIGKILL even by
444 * Note that we go through the signals twice: once to check the signals that
445 * the kernel can handle, and then we build all the user-level signal handling
446 * stack-frames in one go after that.
448 int do_signal(struct pt_regs *regs, sigset_t *oldset)
450 unsigned long retval = 0, continue_addr = 0, restart_addr = 0;
455 * We want the common case to go fast, which
456 * is why we may in certain cases get here from
457 * kernel mode. Just return without doing anything
460 if (!user_mode(regs))
464 oldset = ¤t->blocked;
466 /* Are we from a system call? */
467 if (regs->trap == __LC_SVC_OLD_PSW) {
468 continue_addr = regs->psw.addr;
469 restart_addr = continue_addr - regs->ilc;
470 retval = regs->gprs[2];
472 /* Prepare for system call restart. We do this here so that a
473 debugger will see the already changed PSW. */
474 if (retval == -ERESTARTNOHAND ||
475 retval == -ERESTARTSYS ||
476 retval == -ERESTARTNOINTR) {
477 regs->gprs[2] = regs->orig_gpr2;
478 regs->psw.addr = restart_addr;
479 } else if (retval == -ERESTART_RESTARTBLOCK) {
480 regs->gprs[2] = -EINTR;
484 /* Get signal to deliver. When running under ptrace, at this point
485 the debugger may change all our registers ... */
486 signr = get_signal_to_deliver(&info, regs, NULL);
488 /* Depending on the signal settings we may need to revert the
489 decision to restart the system call. */
490 if (signr > 0 && regs->psw.addr == restart_addr) {
491 if (retval == -ERESTARTNOHAND
492 || (retval == -ERESTARTSYS
493 && !(current->sighand->action[signr-1].sa.sa_flags
495 regs->gprs[2] = -EINTR;
496 regs->psw.addr = continue_addr;
501 /* Whee! Actually deliver the signal. */
502 #ifdef CONFIG_S390_SUPPORT
503 if (test_thread_flag(TIF_31BIT)) {
504 extern void handle_signal32(unsigned long sig,
507 struct pt_regs *regs);
508 handle_signal32(signr, &info, oldset, regs);
512 handle_signal(signr, &info, oldset, regs);
516 /* Restart a different system call. */
517 if (retval == -ERESTART_RESTARTBLOCK
518 && regs->psw.addr == continue_addr) {
519 regs->gprs[2] = __NR_restart_syscall;
520 set_thread_flag(TIF_RESTART_SVC);