2 * linux/arch/alpha/kernel/signal.c
4 * Copyright (C) 1995 Linus Torvalds
6 * 1997-11-02 Modified for POSIX.1b signals by Richard Henderson
9 #include <linux/sched.h>
10 #include <linux/kernel.h>
11 #include <linux/signal.h>
12 #include <linux/errno.h>
13 #include <linux/wait.h>
14 #include <linux/ptrace.h>
15 #include <linux/unistd.h>
17 #include <linux/smp.h>
18 #include <linux/stddef.h>
19 #include <linux/tty.h>
20 #include <linux/binfmts.h>
21 #include <linux/bitops.h>
23 #include <asm/uaccess.h>
24 #include <asm/sigcontext.h>
25 #include <asm/ucontext.h>
32 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
34 asmlinkage void ret_from_sys_call(void);
35 static int do_signal(sigset_t *, struct pt_regs *, struct switch_stack *,
36 unsigned long, unsigned long);
40 * The OSF/1 sigprocmask calling sequence is different from the
41 * C sigprocmask() sequence..
48 * We change the range to -1 .. 1 in order to let gcc easily
49 * use the conditional move instructions.
51 * Note that we don't need to acquire the kernel lock for SMP
52 * operation, as all of this is local to this thread.
54 asmlinkage unsigned long
55 do_osf_sigprocmask(int how, unsigned long newmask, struct pt_regs *regs)
57 unsigned long oldmask = -EINVAL;
59 if ((unsigned long)how-1 <= 2) {
60 long sign = how-2; /* -1 .. 1 */
61 unsigned long block, unblock;
63 newmask &= _BLOCKABLE;
64 spin_lock_irq(¤t->sighand->siglock);
65 oldmask = current->blocked.sig[0];
67 unblock = oldmask & ~newmask;
68 block = oldmask | newmask;
73 if (_NSIG_WORDS > 1 && sign > 0)
74 sigemptyset(¤t->blocked);
75 current->blocked.sig[0] = newmask;
77 spin_unlock_irq(¤t->sighand->siglock);
79 regs->r0 = 0; /* special no error return */
85 osf_sigaction(int sig, const struct osf_sigaction __user *act,
86 struct osf_sigaction __user *oact)
88 struct k_sigaction new_ka, old_ka;
93 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
94 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
95 __get_user(new_ka.sa.sa_flags, &act->sa_flags))
97 __get_user(mask, &act->sa_mask);
98 siginitset(&new_ka.sa.sa_mask, mask);
99 new_ka.ka_restorer = NULL;
102 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
105 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
106 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
107 __put_user(old_ka.sa.sa_flags, &oact->sa_flags))
109 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
116 sys_rt_sigaction(int sig, const struct sigaction __user *act,
117 struct sigaction __user *oact,
118 size_t sigsetsize, void __user *restorer)
120 struct k_sigaction new_ka, old_ka;
123 /* XXX: Don't preclude handling different sized sigset_t's. */
124 if (sigsetsize != sizeof(sigset_t))
128 new_ka.ka_restorer = restorer;
129 if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
133 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
136 if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
144 * Atomically swap in the new signal mask, and wait for a signal.
147 do_sigsuspend(old_sigset_t mask, struct pt_regs *regs, struct switch_stack *sw)
152 spin_lock_irq(¤t->sighand->siglock);
153 oldset = current->blocked;
154 siginitset(¤t->blocked, mask);
156 spin_unlock_irq(¤t->sighand->siglock);
158 /* Indicate EINTR on return from any possible signal handler,
159 which will not come back through here, but via sigreturn. */
164 current->state = TASK_INTERRUPTIBLE;
166 if (do_signal(&oldset, regs, sw, 0, 0))
172 do_rt_sigsuspend(sigset_t __user *uset, size_t sigsetsize,
173 struct pt_regs *regs, struct switch_stack *sw)
175 sigset_t oldset, set;
177 /* XXX: Don't preclude handling different sized sigset_t's. */
178 if (sigsetsize != sizeof(sigset_t))
180 if (copy_from_user(&set, uset, sizeof(set)))
183 sigdelsetmask(&set, ~_BLOCKABLE);
184 spin_lock_irq(¤t->sighand->siglock);
185 oldset = current->blocked;
186 current->blocked = set;
188 spin_unlock_irq(¤t->sighand->siglock);
190 /* Indicate EINTR on return from any possible signal handler,
191 which will not come back through here, but via sigreturn. */
196 current->state = TASK_INTERRUPTIBLE;
198 if (do_signal(&oldset, regs, sw, 0, 0))
204 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
206 return do_sigaltstack(uss, uoss, rdusp());
210 * Do a signal return; undo the signal stack.
214 # error "Non SA_SIGINFO frame needs rearranging"
219 struct sigcontext sc;
220 unsigned int retcode[3];
227 unsigned int retcode[3];
230 /* If this changes, userland unwinders that Know Things about our signal
231 frame will break. Do not undertake lightly. It also implies an ABI
232 change wrt the size of siginfo_t, which may cause some pain. */
233 extern char compile_time_assert
234 [offsetof(struct rt_sigframe, uc.uc_mcontext) == 176 ? 1 : -1];
236 #define INSN_MOV_R30_R16 0x47fe0410
237 #define INSN_LDI_R0 0x201f0000
238 #define INSN_CALLSYS 0x00000083
241 restore_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
242 struct switch_stack *sw)
245 long i, err = __get_user(regs->pc, &sc->sc_pc);
247 sw->r26 = (unsigned long) ret_from_sys_call;
249 err |= __get_user(regs->r0, sc->sc_regs+0);
250 err |= __get_user(regs->r1, sc->sc_regs+1);
251 err |= __get_user(regs->r2, sc->sc_regs+2);
252 err |= __get_user(regs->r3, sc->sc_regs+3);
253 err |= __get_user(regs->r4, sc->sc_regs+4);
254 err |= __get_user(regs->r5, sc->sc_regs+5);
255 err |= __get_user(regs->r6, sc->sc_regs+6);
256 err |= __get_user(regs->r7, sc->sc_regs+7);
257 err |= __get_user(regs->r8, sc->sc_regs+8);
258 err |= __get_user(sw->r9, sc->sc_regs+9);
259 err |= __get_user(sw->r10, sc->sc_regs+10);
260 err |= __get_user(sw->r11, sc->sc_regs+11);
261 err |= __get_user(sw->r12, sc->sc_regs+12);
262 err |= __get_user(sw->r13, sc->sc_regs+13);
263 err |= __get_user(sw->r14, sc->sc_regs+14);
264 err |= __get_user(sw->r15, sc->sc_regs+15);
265 err |= __get_user(regs->r16, sc->sc_regs+16);
266 err |= __get_user(regs->r17, sc->sc_regs+17);
267 err |= __get_user(regs->r18, sc->sc_regs+18);
268 err |= __get_user(regs->r19, sc->sc_regs+19);
269 err |= __get_user(regs->r20, sc->sc_regs+20);
270 err |= __get_user(regs->r21, sc->sc_regs+21);
271 err |= __get_user(regs->r22, sc->sc_regs+22);
272 err |= __get_user(regs->r23, sc->sc_regs+23);
273 err |= __get_user(regs->r24, sc->sc_regs+24);
274 err |= __get_user(regs->r25, sc->sc_regs+25);
275 err |= __get_user(regs->r26, sc->sc_regs+26);
276 err |= __get_user(regs->r27, sc->sc_regs+27);
277 err |= __get_user(regs->r28, sc->sc_regs+28);
278 err |= __get_user(regs->gp, sc->sc_regs+29);
279 err |= __get_user(usp, sc->sc_regs+30);
282 for (i = 0; i < 31; i++)
283 err |= __get_user(sw->fp[i], sc->sc_fpregs+i);
284 err |= __get_user(sw->fp[31], &sc->sc_fpcr);
289 /* Note that this syscall is also used by setcontext(3) to install
290 a given sigcontext. This because it's impossible to set *all*
291 registers and transfer control from userland. */
294 do_sigreturn(struct sigcontext __user *sc, struct pt_regs *regs,
295 struct switch_stack *sw)
299 /* Verify that it's a good sigcontext before using it */
300 if (!access_ok(VERIFY_READ, sc, sizeof(*sc)))
302 if (__get_user(set.sig[0], &sc->sc_mask))
305 sigdelsetmask(&set, ~_BLOCKABLE);
306 spin_lock_irq(¤t->sighand->siglock);
307 current->blocked = set;
309 spin_unlock_irq(¤t->sighand->siglock);
311 if (restore_sigcontext(sc, regs, sw))
314 /* Send SIGTRAP if we're single-stepping: */
315 if (ptrace_cancel_bpt (current)) {
318 info.si_signo = SIGTRAP;
320 info.si_code = TRAP_BRKPT;
321 info.si_addr = (void __user *) regs->pc;
323 send_sig_info(SIGTRAP, &info, current);
328 force_sig(SIGSEGV, current);
332 do_rt_sigreturn(struct rt_sigframe __user *frame, struct pt_regs *regs,
333 struct switch_stack *sw)
337 /* Verify that it's a good ucontext_t before using it */
338 if (!access_ok(VERIFY_READ, &frame->uc, sizeof(frame->uc)))
340 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
343 sigdelsetmask(&set, ~_BLOCKABLE);
344 spin_lock_irq(¤t->sighand->siglock);
345 current->blocked = set;
347 spin_unlock_irq(¤t->sighand->siglock);
349 if (restore_sigcontext(&frame->uc.uc_mcontext, regs, sw))
352 /* Send SIGTRAP if we're single-stepping: */
353 if (ptrace_cancel_bpt (current)) {
356 info.si_signo = SIGTRAP;
358 info.si_code = TRAP_BRKPT;
359 info.si_addr = (void __user *) regs->pc;
361 send_sig_info(SIGTRAP, &info, current);
366 force_sig(SIGSEGV, current);
371 * Set up a signal frame.
374 static inline void __user *
375 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
377 if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && ! sas_ss_flags(sp))
378 sp = current->sas_ss_sp + current->sas_ss_size;
380 return (void __user *)((sp - frame_size) & -32ul);
384 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
385 struct switch_stack *sw, unsigned long mask, unsigned long sp)
389 err |= __put_user(on_sig_stack((unsigned long)sc), &sc->sc_onstack);
390 err |= __put_user(mask, &sc->sc_mask);
391 err |= __put_user(regs->pc, &sc->sc_pc);
392 err |= __put_user(8, &sc->sc_ps);
394 err |= __put_user(regs->r0 , sc->sc_regs+0);
395 err |= __put_user(regs->r1 , sc->sc_regs+1);
396 err |= __put_user(regs->r2 , sc->sc_regs+2);
397 err |= __put_user(regs->r3 , sc->sc_regs+3);
398 err |= __put_user(regs->r4 , sc->sc_regs+4);
399 err |= __put_user(regs->r5 , sc->sc_regs+5);
400 err |= __put_user(regs->r6 , sc->sc_regs+6);
401 err |= __put_user(regs->r7 , sc->sc_regs+7);
402 err |= __put_user(regs->r8 , sc->sc_regs+8);
403 err |= __put_user(sw->r9 , sc->sc_regs+9);
404 err |= __put_user(sw->r10 , sc->sc_regs+10);
405 err |= __put_user(sw->r11 , sc->sc_regs+11);
406 err |= __put_user(sw->r12 , sc->sc_regs+12);
407 err |= __put_user(sw->r13 , sc->sc_regs+13);
408 err |= __put_user(sw->r14 , sc->sc_regs+14);
409 err |= __put_user(sw->r15 , sc->sc_regs+15);
410 err |= __put_user(regs->r16, sc->sc_regs+16);
411 err |= __put_user(regs->r17, sc->sc_regs+17);
412 err |= __put_user(regs->r18, sc->sc_regs+18);
413 err |= __put_user(regs->r19, sc->sc_regs+19);
414 err |= __put_user(regs->r20, sc->sc_regs+20);
415 err |= __put_user(regs->r21, sc->sc_regs+21);
416 err |= __put_user(regs->r22, sc->sc_regs+22);
417 err |= __put_user(regs->r23, sc->sc_regs+23);
418 err |= __put_user(regs->r24, sc->sc_regs+24);
419 err |= __put_user(regs->r25, sc->sc_regs+25);
420 err |= __put_user(regs->r26, sc->sc_regs+26);
421 err |= __put_user(regs->r27, sc->sc_regs+27);
422 err |= __put_user(regs->r28, sc->sc_regs+28);
423 err |= __put_user(regs->gp , sc->sc_regs+29);
424 err |= __put_user(sp, sc->sc_regs+30);
425 err |= __put_user(0, sc->sc_regs+31);
427 for (i = 0; i < 31; i++)
428 err |= __put_user(sw->fp[i], sc->sc_fpregs+i);
429 err |= __put_user(0, sc->sc_fpregs+31);
430 err |= __put_user(sw->fp[31], &sc->sc_fpcr);
432 err |= __put_user(regs->trap_a0, &sc->sc_traparg_a0);
433 err |= __put_user(regs->trap_a1, &sc->sc_traparg_a1);
434 err |= __put_user(regs->trap_a2, &sc->sc_traparg_a2);
440 setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
441 struct pt_regs *regs, struct switch_stack * sw)
443 unsigned long oldsp, r26, err = 0;
444 struct sigframe __user *frame;
447 frame = get_sigframe(ka, oldsp, sizeof(*frame));
448 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
451 err |= setup_sigcontext(&frame->sc, regs, sw, set->sig[0], oldsp);
455 /* Set up to return from userspace. If provided, use a stub
456 already in userspace. */
457 if (ka->ka_restorer) {
458 r26 = (unsigned long) ka->ka_restorer;
460 err |= __put_user(INSN_MOV_R30_R16, frame->retcode+0);
461 err |= __put_user(INSN_LDI_R0+__NR_sigreturn, frame->retcode+1);
462 err |= __put_user(INSN_CALLSYS, frame->retcode+2);
464 r26 = (unsigned long) frame->retcode;
467 /* Check that everything was written properly. */
471 /* "Return" to the handler */
473 regs->r27 = regs->pc = (unsigned long) ka->sa.sa_handler;
474 regs->r16 = sig; /* a0: signal number */
475 regs->r17 = 0; /* a1: exception code */
476 regs->r18 = (unsigned long) &frame->sc; /* a2: sigcontext pointer */
477 wrusp((unsigned long) frame);
480 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
481 current->comm, current->pid, frame, regs->pc, regs->r26);
487 force_sigsegv(sig, current);
491 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
492 sigset_t *set, struct pt_regs *regs, struct switch_stack * sw)
494 unsigned long oldsp, r26, err = 0;
495 struct rt_sigframe __user *frame;
498 frame = get_sigframe(ka, oldsp, sizeof(*frame));
499 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
502 err |= copy_siginfo_to_user(&frame->info, info);
504 /* Create the ucontext. */
505 err |= __put_user(0, &frame->uc.uc_flags);
506 err |= __put_user(0, &frame->uc.uc_link);
507 err |= __put_user(set->sig[0], &frame->uc.uc_osf_sigmask);
508 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
509 err |= __put_user(sas_ss_flags(oldsp), &frame->uc.uc_stack.ss_flags);
510 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
511 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, sw,
513 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
517 /* Set up to return from userspace. If provided, use a stub
518 already in userspace. */
519 if (ka->ka_restorer) {
520 r26 = (unsigned long) ka->ka_restorer;
522 err |= __put_user(INSN_MOV_R30_R16, frame->retcode+0);
523 err |= __put_user(INSN_LDI_R0+__NR_rt_sigreturn,
525 err |= __put_user(INSN_CALLSYS, frame->retcode+2);
527 r26 = (unsigned long) frame->retcode;
533 /* "Return" to the handler */
535 regs->r27 = regs->pc = (unsigned long) ka->sa.sa_handler;
536 regs->r16 = sig; /* a0: signal number */
537 regs->r17 = (unsigned long) &frame->info; /* a1: siginfo pointer */
538 regs->r18 = (unsigned long) &frame->uc; /* a2: ucontext pointer */
539 wrusp((unsigned long) frame);
542 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
543 current->comm, current->pid, frame, regs->pc, regs->r26);
549 force_sigsegv(sig, current);
554 * OK, we're invoking a handler.
557 handle_signal(int sig, struct k_sigaction *ka, siginfo_t *info,
558 sigset_t *oldset, struct pt_regs * regs, struct switch_stack *sw)
560 if (ka->sa.sa_flags & SA_SIGINFO)
561 setup_rt_frame(sig, ka, info, oldset, regs, sw);
563 setup_frame(sig, ka, oldset, regs, sw);
565 if (ka->sa.sa_flags & SA_RESETHAND)
566 ka->sa.sa_handler = SIG_DFL;
568 spin_lock_irq(¤t->sighand->siglock);
569 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
570 if (!(ka->sa.sa_flags & SA_NODEFER))
571 sigaddset(¤t->blocked,sig);
573 spin_unlock_irq(¤t->sighand->siglock);
577 syscall_restart(unsigned long r0, unsigned long r19,
578 struct pt_regs *regs, struct k_sigaction *ka)
582 if (!(ka->sa.sa_flags & SA_RESTART)) {
589 regs->r0 = r0; /* reset v0 and a3 and replay syscall */
593 case ERESTART_RESTARTBLOCK:
594 current_thread_info()->restart_block.fn = do_no_restart_syscall;
602 * Note that 'init' is a special process: it doesn't get signals it doesn't
603 * want to handle. Thus you cannot kill init even with a SIGKILL even by
606 * Note that we go through the signals twice: once to check the signals that
607 * the kernel can handle, and then we build all the user-level signal handling
608 * stack-frames in one go after that.
610 * "r0" and "r19" are the registers we need to restore for system call
611 * restart. "r0" is also used as an indicator whether we can restart at
612 * all (if we get here from anything but a syscall return, it will be 0)
615 do_signal(sigset_t *oldset, struct pt_regs * regs, struct switch_stack * sw,
616 unsigned long r0, unsigned long r19)
620 unsigned long single_stepping = ptrace_cancel_bpt(current);
621 struct k_sigaction ka;
624 oldset = ¤t->blocked;
626 /* This lets the debugger run, ... */
627 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
628 /* ... so re-check the single stepping. */
629 single_stepping |= ptrace_cancel_bpt(current);
632 /* Whee! Actually deliver the signal. */
633 if (r0) syscall_restart(r0, r19, regs, &ka);
634 handle_signal(signr, &ka, &info, oldset, regs, sw);
636 ptrace_set_bpt(current); /* re-set bpt */
645 /* Reset v0 and a3 and replay syscall. */
650 case ERESTART_RESTARTBLOCK:
651 /* Force v0 to the restart syscall and reply. */
652 regs->r0 = __NR_restart_syscall;
658 ptrace_set_bpt(current); /* re-set breakpoint */
664 do_notify_resume(sigset_t *oldset, struct pt_regs *regs,
665 struct switch_stack *sw, unsigned long r0,
666 unsigned long r19, unsigned long thread_info_flags)
668 if (thread_info_flags & _TIF_SIGPENDING)
669 do_signal(oldset, regs, sw, r0, r19);