2 * Architecture-specific signal handling support.
4 * Copyright (C) 1999-2003 Hewlett-Packard Co
5 * David Mosberger-Tang <davidm@hpl.hp.com>
7 * Derived from i386 and Alpha versions.
10 #include <linux/config.h>
11 #include <linux/errno.h>
12 #include <linux/kernel.h>
14 #include <linux/ptrace.h>
15 #include <linux/sched.h>
16 #include <linux/signal.h>
17 #include <linux/smp.h>
18 #include <linux/smp_lock.h>
19 #include <linux/stddef.h>
20 #include <linux/tty.h>
21 #include <linux/binfmts.h>
22 #include <linux/unistd.h>
23 #include <linux/wait.h>
26 #include <asm/intrinsics.h>
27 #include <asm/uaccess.h>
29 #include <asm/sigcontext.h>
34 #define STACK_ALIGN 16 /* minimal alignment for stack pointer */
35 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
38 # define PUT_SIGSET(k,u) __copy_to_user((u)->sig, (k)->sig, sizeof(sigset_t))
39 # define GET_SIGSET(k,u) __copy_from_user((k)->sig, (u)->sig, sizeof(sigset_t))
41 # define PUT_SIGSET(k,u) __put_user((k)->sig[0], &(u)->sig[0])
42 # define GET_SIGSET(k,u) __get_user((k)->sig[0], &(u)->sig[0])
47 * Don't let GCC uses f16-f31 so that when we setup/restore the registers in the signal
48 * context in __kernel_sigtramp(), we can be sure that registers f16-f31 contain user-level
51 register double f16 asm ("f16"); register double f17 asm ("f17");
52 register double f18 asm ("f18"); register double f19 asm ("f19");
53 register double f20 asm ("f20"); register double f21 asm ("f21");
54 register double f22 asm ("f22"); register double f23 asm ("f23");
56 register double f24 asm ("f24"); register double f25 asm ("f25");
57 register double f26 asm ("f26"); register double f27 asm ("f27");
58 register double f28 asm ("f28"); register double f29 asm ("f29");
59 register double f30 asm ("f30"); register double f31 asm ("f31");
63 ia64_rt_sigsuspend (sigset_t *uset, size_t sigsetsize, struct sigscratch *scr)
67 /* XXX: Don't preclude handling different sized sigset_t's. */
68 if (sigsetsize != sizeof(sigset_t))
71 if (!access_ok(VERIFY_READ, uset, sigsetsize))
74 if (GET_SIGSET(&set, uset))
77 sigdelsetmask(&set, ~_BLOCKABLE);
79 spin_lock_irq(¤t->sighand->siglock);
81 oldset = current->blocked;
82 current->blocked = set;
85 spin_unlock_irq(¤t->sighand->siglock);
88 * The return below usually returns to the signal handler. We need to
89 * pre-set the correct error code here to ensure that the right values
90 * get saved in sigcontext by ia64_do_signal.
96 current->state = TASK_INTERRUPTIBLE;
98 if (ia64_do_signal(&oldset, scr, 1))
104 sys_sigaltstack (const stack_t *uss, stack_t *uoss, long arg2, long arg3, long arg4,
105 long arg5, long arg6, long arg7, long stack)
107 struct pt_regs *pt = (struct pt_regs *) &stack;
109 return do_sigaltstack(uss, uoss, pt->r12);
113 restore_sigcontext (struct sigcontext *sc, struct sigscratch *scr)
115 unsigned long ip, flags, nat, um, cfm;
118 /* Always make any pending restarted system calls return -EINTR */
119 current_thread_info()->restart_block.fn = do_no_restart_syscall;
121 /* restore scratch that always needs gets updated during signal delivery: */
122 err = __get_user(flags, &sc->sc_flags);
123 err |= __get_user(nat, &sc->sc_nat);
124 err |= __get_user(ip, &sc->sc_ip); /* instruction pointer */
125 err |= __get_user(cfm, &sc->sc_cfm);
126 err |= __get_user(um, &sc->sc_um); /* user mask */
127 err |= __get_user(scr->pt.ar_rsc, &sc->sc_ar_rsc);
128 err |= __get_user(scr->pt.ar_unat, &sc->sc_ar_unat);
129 err |= __get_user(scr->pt.ar_fpsr, &sc->sc_ar_fpsr);
130 err |= __get_user(scr->pt.ar_pfs, &sc->sc_ar_pfs);
131 err |= __get_user(scr->pt.pr, &sc->sc_pr); /* predicates */
132 err |= __get_user(scr->pt.b0, &sc->sc_br[0]); /* b0 (rp) */
133 err |= __get_user(scr->pt.b6, &sc->sc_br[6]); /* b6 */
134 err |= __copy_from_user(&scr->pt.r1, &sc->sc_gr[1], 8); /* r1 */
135 err |= __copy_from_user(&scr->pt.r8, &sc->sc_gr[8], 4*8); /* r8-r11 */
136 err |= __copy_from_user(&scr->pt.r12, &sc->sc_gr[12], 2*8); /* r12-r13 */
137 err |= __copy_from_user(&scr->pt.r15, &sc->sc_gr[15], 8); /* r15 */
139 scr->pt.cr_ifs = cfm | (1UL << 63);
141 /* establish new instruction pointer: */
142 scr->pt.cr_iip = ip & ~0x3UL;
143 ia64_psr(&scr->pt)->ri = ip & 0x3;
144 scr->pt.cr_ipsr = (scr->pt.cr_ipsr & ~IA64_PSR_UM) | (um & IA64_PSR_UM);
146 scr->scratch_unat = ia64_put_scratch_nat_bits(&scr->pt, nat);
148 if (!(flags & IA64_SC_FLAG_IN_SYSCALL)) {
149 /* Restore most scratch-state only when not in syscall. */
150 err |= __get_user(scr->pt.ar_ccv, &sc->sc_ar_ccv); /* ar.ccv */
151 err |= __get_user(scr->pt.b7, &sc->sc_br[7]); /* b7 */
152 err |= __get_user(scr->pt.r14, &sc->sc_gr[14]); /* r14 */
153 err |= __copy_from_user(&scr->pt.ar_csd, &sc->sc_ar25, 2*8); /* ar.csd & ar.ssd */
154 err |= __copy_from_user(&scr->pt.r2, &sc->sc_gr[2], 2*8); /* r2-r3 */
155 err |= __copy_from_user(&scr->pt.r16, &sc->sc_gr[16], 16*8); /* r16-r31 */
158 if ((flags & IA64_SC_FLAG_FPH_VALID) != 0) {
159 struct ia64_psr *psr = ia64_psr(&scr->pt);
161 __copy_from_user(current->thread.fph, &sc->sc_fr[32], 96*16);
162 psr->mfh = 0; /* drop signal handler's fph contents... */
164 ia64_drop_fpu(current);
166 /* We already own the local fph, otherwise psr->dfh wouldn't be 0. */
167 __ia64_load_fpu(current->thread.fph);
168 ia64_set_local_fpu_owner(current);
175 copy_siginfo_to_user (siginfo_t *to, siginfo_t *from)
177 if (!access_ok(VERIFY_WRITE, to, sizeof(siginfo_t)))
179 if (from->si_code < 0) {
180 if (__copy_to_user(to, from, sizeof(siginfo_t)))
187 * If you change siginfo_t structure, please be sure this code is fixed
188 * accordingly. It should never copy any pad contained in the structure
189 * to avoid security leaks, but must copy the generic 3 ints plus the
190 * relevant union member.
192 err = __put_user(from->si_signo, &to->si_signo);
193 err |= __put_user(from->si_errno, &to->si_errno);
194 err |= __put_user((short)from->si_code, &to->si_code);
195 switch (from->si_code >> 16) {
196 case __SI_FAULT >> 16:
197 err |= __put_user(from->si_flags, &to->si_flags);
198 err |= __put_user(from->si_isr, &to->si_isr);
199 case __SI_POLL >> 16:
200 err |= __put_user(from->si_addr, &to->si_addr);
201 err |= __put_user(from->si_imm, &to->si_imm);
203 case __SI_TIMER >> 16:
204 err |= __put_user(from->si_tid, &to->si_tid);
205 err |= __put_user(from->si_overrun, &to->si_overrun);
206 err |= __put_user(from->si_ptr, &to->si_ptr);
208 case __SI_RT >> 16: /* Not generated by the kernel as of now. */
209 case __SI_MESGQ >> 16:
210 err |= __put_user(from->si_uid, &to->si_uid);
211 err |= __put_user(from->si_pid, &to->si_pid);
212 err |= __put_user(from->si_ptr, &to->si_ptr);
214 case __SI_CHLD >> 16:
215 err |= __put_user(from->si_utime, &to->si_utime);
216 err |= __put_user(from->si_stime, &to->si_stime);
217 err |= __put_user(from->si_status, &to->si_status);
219 err |= __put_user(from->si_uid, &to->si_uid);
220 err |= __put_user(from->si_pid, &to->si_pid);
228 ia64_rt_sigreturn (struct sigscratch *scr)
230 extern char ia64_strace_leave_kernel, ia64_leave_kernel;
231 struct sigcontext *sc;
236 sc = &((struct sigframe *) (scr->pt.r12 + 16))->sc;
239 * When we return to the previously executing context, r8 and r10 have already
240 * been setup the way we want them. Indeed, if the signal wasn't delivered while
241 * in a system call, we must not touch r8 or r10 as otherwise user-level state
242 * could be corrupted.
244 retval = (long) &ia64_leave_kernel;
245 if (test_thread_flag(TIF_SYSCALL_TRACE))
247 * strace expects to be notified after sigreturn returns even though the
248 * context to which we return may not be in the middle of a syscall.
249 * Thus, the return-value that strace displays for sigreturn is
252 retval = (long) &ia64_strace_leave_kernel;
254 if (!access_ok(VERIFY_READ, sc, sizeof(*sc)))
257 if (GET_SIGSET(&set, &sc->sc_mask))
260 sigdelsetmask(&set, ~_BLOCKABLE);
262 spin_lock_irq(¤t->sighand->siglock);
264 current->blocked = set;
267 spin_unlock_irq(¤t->sighand->siglock);
269 if (restore_sigcontext(sc, scr))
273 printk("SIG return (%s:%d): sp=%lx ip=%lx\n",
274 current->comm, current->pid, scr->pt.r12, scr->pt.cr_iip);
277 * It is more difficult to avoid calling this function than to
278 * call it and ignore errors.
280 do_sigaltstack(&sc->sc_stack, 0, scr->pt.r12);
284 si.si_signo = SIGSEGV;
286 si.si_code = SI_KERNEL;
287 si.si_pid = current->pid;
288 si.si_uid = current->uid;
290 force_sig_info(SIGSEGV, &si, current);
295 * This does just the minimum required setup of sigcontext.
296 * Specifically, it only installs data that is either not knowable at
297 * the user-level or that gets modified before execution in the
298 * trampoline starts. Everything else is done at the user-level.
301 setup_sigcontext (struct sigcontext *sc, sigset_t *mask, struct sigscratch *scr)
303 unsigned long flags = 0, ifs, cfm, nat;
306 ifs = scr->pt.cr_ifs;
308 if (on_sig_stack((unsigned long) sc))
309 flags |= IA64_SC_FLAG_ONSTACK;
310 if ((ifs & (1UL << 63)) == 0) {
311 /* if cr_ifs isn't valid, we got here through a syscall */
312 flags |= IA64_SC_FLAG_IN_SYSCALL;
313 cfm = scr->ar_pfs & ((1UL << 38) - 1);
315 cfm = ifs & ((1UL << 38) - 1);
316 ia64_flush_fph(current);
317 if ((current->thread.flags & IA64_THREAD_FPH_VALID)) {
318 flags |= IA64_SC_FLAG_FPH_VALID;
319 __copy_to_user(&sc->sc_fr[32], current->thread.fph, 96*16);
322 nat = ia64_get_scratch_nat_bits(&scr->pt, scr->scratch_unat);
324 err = __put_user(flags, &sc->sc_flags);
325 err |= __put_user(nat, &sc->sc_nat);
326 err |= PUT_SIGSET(mask, &sc->sc_mask);
327 err |= __put_user(cfm, &sc->sc_cfm);
328 err |= __put_user(scr->pt.cr_ipsr & IA64_PSR_UM, &sc->sc_um);
329 err |= __put_user(scr->pt.ar_rsc, &sc->sc_ar_rsc);
330 err |= __put_user(scr->pt.ar_unat, &sc->sc_ar_unat); /* ar.unat */
331 err |= __put_user(scr->pt.ar_fpsr, &sc->sc_ar_fpsr); /* ar.fpsr */
332 err |= __put_user(scr->pt.ar_pfs, &sc->sc_ar_pfs);
333 err |= __put_user(scr->pt.pr, &sc->sc_pr); /* predicates */
334 err |= __put_user(scr->pt.b0, &sc->sc_br[0]); /* b0 (rp) */
335 err |= __put_user(scr->pt.b6, &sc->sc_br[6]); /* b6 */
336 err |= __copy_to_user(&sc->sc_gr[1], &scr->pt.r1, 8); /* r1 */
337 err |= __copy_to_user(&sc->sc_gr[8], &scr->pt.r8, 4*8); /* r8-r11 */
338 err |= __copy_to_user(&sc->sc_gr[12], &scr->pt.r12, 2*8); /* r12-r13 */
339 err |= __copy_to_user(&sc->sc_gr[15], &scr->pt.r15, 8); /* r15 */
340 err |= __put_user(scr->pt.cr_iip + ia64_psr(&scr->pt)->ri, &sc->sc_ip);
342 if (flags & IA64_SC_FLAG_IN_SYSCALL) {
343 /* Clear scratch registers if the signal interrupted a system call. */
344 err |= __put_user(0, &sc->sc_ar_ccv); /* ar.ccv */
345 err |= __put_user(0, &sc->sc_br[7]); /* b7 */
346 err |= __put_user(0, &sc->sc_gr[14]); /* r14 */
347 err |= __clear_user(&sc->sc_ar25, 2*8); /* ar.csd & ar.ssd */
348 err |= __clear_user(&sc->sc_gr[2], 2*8); /* r2-r3 */
349 err |= __clear_user(&sc->sc_gr[16], 16*8); /* r16-r31 */
351 /* Copy scratch regs to sigcontext if the signal didn't interrupt a syscall. */
352 err |= __put_user(scr->pt.ar_ccv, &sc->sc_ar_ccv); /* ar.ccv */
353 err |= __put_user(scr->pt.b7, &sc->sc_br[7]); /* b7 */
354 err |= __put_user(scr->pt.r14, &sc->sc_gr[14]); /* r14 */
355 err |= __copy_to_user(&scr->pt.ar_csd, &sc->sc_ar25, 2*8); /* ar.csd & ar.ssd */
356 err |= __copy_to_user(&sc->sc_gr[2], &scr->pt.r2, 2*8); /* r2-r3 */
357 err |= __copy_to_user(&sc->sc_gr[16], &scr->pt.r16, 16*8); /* r16-r31 */
363 * Check whether the register-backing store is already on the signal stack.
366 rbs_on_sig_stack (unsigned long bsp)
368 return (bsp - current->sas_ss_sp < current->sas_ss_size);
372 setup_frame (int sig, struct k_sigaction *ka, siginfo_t *info, sigset_t *set,
373 struct sigscratch *scr)
375 extern char __kernel_sigtramp[];
376 unsigned long tramp_addr, new_rbs = 0;
377 struct sigframe *frame;
381 frame = (void *) scr->pt.r12;
382 tramp_addr = (unsigned long) __kernel_sigtramp;
383 if ((ka->sa.sa_flags & SA_ONSTACK) && sas_ss_flags((unsigned long) frame) == 0) {
384 frame = (void *) ((current->sas_ss_sp + current->sas_ss_size)
385 & ~(STACK_ALIGN - 1));
387 * We need to check for the register stack being on the signal stack
388 * separately, because it's switched separately (memory stack is switched
389 * in the kernel, register stack is switched in the signal trampoline).
391 if (!rbs_on_sig_stack(scr->pt.ar_bspstore))
392 new_rbs = (current->sas_ss_sp + sizeof(long) - 1) & ~(sizeof(long) - 1);
394 frame = (void *) frame - ((sizeof(*frame) + STACK_ALIGN - 1) & ~(STACK_ALIGN - 1));
396 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
399 err = __put_user(sig, &frame->arg0);
400 err |= __put_user(&frame->info, &frame->arg1);
401 err |= __put_user(&frame->sc, &frame->arg2);
402 err |= __put_user(new_rbs, &frame->sc.sc_rbs_base);
403 err |= __put_user(0, &frame->sc.sc_loadrs); /* initialize to zero */
404 err |= __put_user(ka->sa.sa_handler, &frame->handler);
406 err |= copy_siginfo_to_user(&frame->info, info);
408 err |= __put_user(current->sas_ss_sp, &frame->sc.sc_stack.ss_sp);
409 err |= __put_user(current->sas_ss_size, &frame->sc.sc_stack.ss_size);
410 err |= __put_user(sas_ss_flags(scr->pt.r12), &frame->sc.sc_stack.ss_flags);
411 err |= setup_sigcontext(&frame->sc, set, scr);
416 scr->pt.r12 = (unsigned long) frame - 16; /* new stack pointer */
417 scr->pt.ar_fpsr = FPSR_DEFAULT; /* reset fpsr for signal handler */
418 scr->pt.cr_iip = tramp_addr;
419 ia64_psr(&scr->pt)->ri = 0; /* start executing in first slot */
420 ia64_psr(&scr->pt)->be = 0; /* force little-endian byte-order */
422 * Force the interruption function mask to zero. This has no effect when a
423 * system-call got interrupted by a signal (since, in that case, scr->pt_cr_ifs is
424 * ignored), but it has the desirable effect of making it possible to deliver a
425 * signal with an incomplete register frame (which happens when a mandatory RSE
426 * load faults). Furthermore, it has no negative effect on the getting the user's
427 * dirty partition preserved, because that's governed by scr->pt.loadrs.
429 scr->pt.cr_ifs = (1UL << 63);
432 * Note: this affects only the NaT bits of the scratch regs (the ones saved in
433 * pt_regs), which is exactly what we want.
435 scr->scratch_unat = 0; /* ensure NaT bits of r12 is clear */
438 printk("SIG deliver (%s:%d): sig=%d sp=%lx ip=%lx handler=%p\n",
439 current->comm, current->pid, sig, scr->pt.r12, frame->sc.sc_ip, frame->handler);
445 ka->sa.sa_handler = SIG_DFL;
446 si.si_signo = SIGSEGV;
448 si.si_code = SI_KERNEL;
449 si.si_pid = current->pid;
450 si.si_uid = current->uid;
452 force_sig_info(SIGSEGV, &si, current);
457 handle_signal (unsigned long sig, struct k_sigaction *ka, siginfo_t *info, sigset_t *oldset,
458 struct sigscratch *scr)
460 if (IS_IA32_PROCESS(&scr->pt)) {
461 /* send signal to IA-32 process */
462 if (!ia32_setup_frame1(sig, ka, info, oldset, &scr->pt))
465 /* send signal to IA-64 process */
466 if (!setup_frame(sig, ka, info, oldset, scr))
469 if (ka->sa.sa_flags & SA_ONESHOT)
470 ka->sa.sa_handler = SIG_DFL;
472 if (!(ka->sa.sa_flags & SA_NODEFER)) {
473 spin_lock_irq(¤t->sighand->siglock);
475 sigorsets(¤t->blocked, ¤t->blocked, &ka->sa.sa_mask);
476 sigaddset(¤t->blocked, sig);
479 spin_unlock_irq(¤t->sighand->siglock);
485 * Note that `init' is a special process: it doesn't get signals it doesn't want to
486 * handle. Thus you cannot kill init even with a SIGKILL even by mistake.
489 ia64_do_signal (sigset_t *oldset, struct sigscratch *scr, long in_syscall)
491 struct k_sigaction *ka;
493 long restart = in_syscall;
494 long errno = scr->pt.r8;
495 # define ERR_CODE(c) (IS_IA32_PROCESS(&scr->pt) ? -(c) : (c))
498 * In the ia64_leave_kernel code path, we want the common case to go fast, which
499 * is why we may in certain cases get here from kernel mode. Just return without
500 * doing anything if so.
502 if (!user_mode(&scr->pt))
506 oldset = ¤t->blocked;
509 * This only loops in the rare cases of handle_signal() failing, in which case we
510 * need to push through a forced SIGSEGV.
513 int signr = get_signal_to_deliver(&info, &scr->pt, NULL);
516 * get_signal_to_deliver() may have run a debugger (via notify_parent())
517 * and the debugger may have modified the state (e.g., to arrange for an
518 * inferior call), thus it's important to check for restarting _after_
519 * get_signal_to_deliver().
521 if (IS_IA32_PROCESS(&scr->pt)) {
528 } else if ((long) scr->pt.r10 != -1)
530 * A system calls has to be restarted only if one of the error codes
531 * ERESTARTNOHAND, ERESTARTSYS, or ERESTARTNOINTR is returned. If r10
532 * isn't -1 then r8 doesn't hold an error code and we don't need to
533 * restart the syscall, so we can clear the "restart" flag here.
540 ka = ¤t->sighand->action[signr - 1];
542 if (unlikely(restart)) {
544 case ERESTART_RESTARTBLOCK:
546 scr->pt.r8 = ERR_CODE(EINTR);
547 /* note: scr->pt.r10 is already -1 */
551 if ((ka->sa.sa_flags & SA_RESTART) == 0) {
552 scr->pt.r8 = ERR_CODE(EINTR);
553 /* note: scr->pt.r10 is already -1 */
557 if (IS_IA32_PROCESS(&scr->pt)) {
558 scr->pt.r8 = scr->pt.r1;
561 ia64_decrement_ip(&scr->pt);
562 restart = 0; /* don't restart twice if handle_signal() fails... */
567 * Whee! Actually deliver the signal. If the delivery failed, we need to
568 * continue to iterate in this loop so we can deliver the SIGSEGV...
570 if (handle_signal(signr, ka, &info, oldset, scr))
574 /* Did we come from a system call? */
576 /* Restart the system call - no handlers present */
577 if (errno == ERESTARTNOHAND || errno == ERESTARTSYS || errno == ERESTARTNOINTR
578 || errno == ERESTART_RESTARTBLOCK)
580 if (IS_IA32_PROCESS(&scr->pt)) {
581 scr->pt.r8 = scr->pt.r1;
583 if (errno == ERESTART_RESTARTBLOCK)
584 scr->pt.r8 = 0; /* x86 version of __NR_restart_syscall */
587 * Note: the syscall number is in r15 which is saved in
588 * pt_regs so all we need to do here is adjust ip so that
589 * the "break" instruction gets re-executed.
591 ia64_decrement_ip(&scr->pt);
592 if (errno == ERESTART_RESTARTBLOCK)
593 scr->pt.r15 = __NR_restart_syscall;