2 * linux/arch/x86_64/kernel/signal.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 2000, 2001 SuSE Labs
7 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
8 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
9 * 2000-2001 x86-64 support by Andi Kleen
11 * $Id: signal.c,v 1.1.1.1 2005/04/11 02:50:14 jack Exp $
14 #include <linux/sched.h>
16 #include <linux/smp.h>
17 #include <linux/smp_lock.h>
18 #include <linux/kernel.h>
19 #include <linux/signal.h>
20 #include <linux/errno.h>
21 #include <linux/wait.h>
22 #include <linux/ptrace.h>
23 #include <linux/unistd.h>
24 #include <linux/stddef.h>
25 #include <linux/tty.h>
26 #include <linux/personality.h>
27 #include <linux/compiler.h>
28 #include <linux/binfmts.h>
29 #include <asm/ucontext.h>
30 #include <asm/uaccess.h>
32 #include <asm/proto.h>
36 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
38 asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset);
40 void ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
41 sigset_t *set, struct pt_regs * regs);
42 void ia32_setup_frame(int sig, struct k_sigaction *ka,
43 sigset_t *set, struct pt_regs * regs);
44 void signal_fault(struct pt_regs *regs, void *frame, char *where);
45 extern int exception_trace;
47 int copy_siginfo_to_user(siginfo_t *to, siginfo_t *from)
49 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
51 if (from->si_code < 0)
52 return __copy_to_user(to, from, sizeof(siginfo_t));
56 /* If you change siginfo_t structure, please be sure
57 this code is fixed accordingly.
58 It should never copy any pad contained in the structure
59 to avoid security leaks, but must copy the generic
60 3 ints plus the relevant union member. */
61 err = __put_user(from->si_signo, &to->si_signo);
62 err |= __put_user(from->si_errno, &to->si_errno);
63 err |= __put_user((short)from->si_code, &to->si_code);
64 /* First 32bits of unions are always present. */
65 err |= __put_user(from->si_pid, &to->si_pid);
66 switch (from->si_code >> 16) {
68 err |= __put_user(from->si_utime, &to->si_utime);
69 err |= __put_user(from->si_stime, &to->si_stime);
70 err |= __put_user(from->si_status, &to->si_status);
72 err |= __put_user(from->si_uid, &to->si_uid);
74 /* case __SI_RT: This is not generated by the kernel as of now. */
81 sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize, struct pt_regs regs)
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->sigmask_lock);
94 saveset = current->blocked;
95 current->blocked = newset;
96 recalc_sigpending(current);
97 spin_unlock_irq(¤t->sigmask_lock);
99 printk("rt_sigsuspend savset(%lx) newset(%lx) regs(%p) rip(%lx)\n",
100 saveset, newset, ®s, regs.rip);
104 current->state = TASK_INTERRUPTIBLE;
106 if (do_signal(®s, &saveset))
112 sys_sigaltstack(const stack_t *uss, stack_t *uoss, struct pt_regs regs)
114 return do_sigaltstack(uss, uoss, regs.rsp);
119 * Do a signal return; undo the signal stack.
130 restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc, unsigned long *prax)
132 unsigned int err = 0;
135 #define COPY(x) err |= __get_user(regs->x, &sc->x)
136 #define COPY_CANON(x) \
138 if ((regs->x >> 48) != 0 && (regs->x >> 48) != 0xffff) \
142 unsigned int seg, oldseg;
143 err |= __get_user(seg, &sc->gs);
144 asm("movl %%gs,%0" : "=r" (oldseg));
149 err |= __get_user(seg, &sc->fs);
154 COPY(rdi); COPY(rsi); COPY(rbp); COPY_CANON(rsp); COPY(rbx);
155 COPY(rdx); COPY(rcx); COPY_CANON(rip);
165 /* do not copy CS/SS because 64bit should not need it.
166 also need IRET exception handling anyways. */
169 unsigned int tmpflags;
170 err |= __get_user(tmpflags, &sc->eflags);
171 regs->eflags = (regs->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
172 regs->orig_rax = -1; /* disable syscall checks */
176 struct _fpstate * buf;
177 err |= __get_user(buf, &sc->fpstate);
179 if (unlikely(verify_area(VERIFY_READ, buf, sizeof(*buf))))
181 err |= restore_i387(buf);
185 err |= __get_user(*prax, &sc->rax);
190 asmlinkage long sys_rt_sigreturn(struct pt_regs regs)
192 struct rt_sigframe *frame = (struct rt_sigframe *)(regs.rsp - 8);
197 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
199 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
202 sigdelsetmask(&set, ~_BLOCKABLE);
203 spin_lock_irq(¤t->sigmask_lock);
204 current->blocked = set;
205 recalc_sigpending(current);
206 spin_unlock_irq(¤t->sigmask_lock);
208 if (restore_sigcontext(®s, &frame->uc.uc_mcontext, &eax))
212 printk("%d sigreturn rip:%lx rsp:%lx frame:%p rax:%lx\n",current->pid,regs.rip,regs.rsp,frame,eax);
215 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
217 /* It is more difficult to avoid calling this function than to
218 call it and ignore errors. */
219 do_sigaltstack(&st, NULL, regs.rsp);
224 signal_fault(®s, frame, "rt_sigreturn");
229 * Set up a signal frame.
233 setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs, unsigned long mask)
236 struct task_struct *me = current;
239 __asm__("movl %%gs,%0" : "=r"(tmp): "0"(tmp));
240 err |= __put_user(tmp, (unsigned int *)&sc->gs);
241 __asm__("movl %%fs,%0" : "=r"(tmp): "0"(tmp));
242 err |= __put_user(tmp, (unsigned int *)&sc->fs);
244 err |= __put_user(regs->rdi, &sc->rdi);
245 err |= __put_user(regs->rsi, &sc->rsi);
246 err |= __put_user(regs->rbp, &sc->rbp);
247 err |= __put_user(regs->rsp, &sc->rsp);
248 err |= __put_user(regs->rbx, &sc->rbx);
249 err |= __put_user(regs->rdx, &sc->rdx);
250 err |= __put_user(regs->rcx, &sc->rcx);
251 err |= __put_user(regs->rax, &sc->rax);
252 err |= __put_user(regs->r8, &sc->r8);
253 err |= __put_user(regs->r9, &sc->r9);
254 err |= __put_user(regs->r10, &sc->r10);
255 err |= __put_user(regs->r11, &sc->r11);
256 err |= __put_user(regs->r12, &sc->r12);
257 err |= __put_user(regs->r13, &sc->r13);
258 err |= __put_user(regs->r14, &sc->r14);
259 err |= __put_user(regs->r15, &sc->r15);
260 err |= __put_user(me->thread.trap_no, &sc->trapno);
261 err |= __put_user(me->thread.error_code, &sc->err);
262 err |= __put_user(regs->rip, &sc->rip);
263 err |= __put_user(regs->cs, &sc->cs);
264 err |= __put_user(regs->eflags, &sc->eflags);
265 err |= __put_user(mask, &sc->oldmask);
266 err |= __put_user(me->thread.cr2, &sc->cr2);
272 get_stack(struct k_sigaction *ka, struct pt_regs *regs, unsigned long size)
276 /* Default to using normal stack - redzone*/
277 rsp = regs->rsp - 128;
279 /* This is the X/Open sanctioned signal stack switching. */
280 /* may need to subtract redzone there too */
281 if (ka->sa.sa_flags & SA_ONSTACK) {
282 if (sas_ss_flags(rsp) == 0)
283 rsp = current->sas_ss_sp + current->sas_ss_size;
286 return (void *)round_down(rsp - size, 16);
289 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
290 sigset_t *set, struct pt_regs * regs)
292 struct rt_sigframe *frame;
296 if (current->used_math) {
297 fp = get_stack(ka, regs, sizeof(struct _fpstate));
298 frame = (void *)round_down((unsigned long)fp - sizeof(struct rt_sigframe), 16) - 8;
300 if (!access_ok(VERIFY_WRITE, fp, sizeof(struct _fpstate))) {
303 if (save_i387(fp) < 0)
306 frame = get_stack(ka, regs, sizeof(struct rt_sigframe)) - 8;
310 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) {
315 if (ka->sa.sa_flags & SA_SIGINFO) {
316 err |= copy_siginfo_to_user(&frame->info, info);
321 /* Create the ucontext. */
322 err |= __put_user(0, &frame->uc.uc_flags);
323 err |= __put_user(0, &frame->uc.uc_link);
324 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
325 err |= __put_user(sas_ss_flags(regs->rsp),
326 &frame->uc.uc_stack.ss_flags);
327 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
328 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
329 err |= __put_user(fp, &frame->uc.uc_mcontext.fpstate);
330 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
332 /* Set up to return from userspace. If provided, use a stub
333 already in userspace. */
334 /* x86-64 should always use SA_RESTORER. */
335 if (ka->sa.sa_flags & SA_RESTORER) {
336 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
338 printk("%s forgot to set SA_RESTORER for signal %d.\n", current->comm, sig);
346 printk("%d old rip %lx old rsp %lx old rax %lx\n", current->pid,regs->rip,regs->rsp,regs->rax);
349 /* Set up registers for signal handler */
351 struct exec_domain *ed = current->exec_domain;
352 if (unlikely(ed && ed->signal_invmap && sig < 32))
353 sig = ed->signal_invmap[sig];
357 /* could reload DS/ES to __USER_DS here, but assume for now
358 that 64bit does not care */
360 /* In case the signal handler was declared without prototypes */
363 /* This also works for non SA_SIGINFO handlers because they expect the
364 next argument after the signal number on the stack. */
365 regs->rsi = (unsigned long)&frame->info;
366 regs->rdx = (unsigned long)&frame->uc;
367 regs->rsp = (unsigned long) frame;
368 regs->rip = (unsigned long) ka->sa.sa_handler;
369 regs->cs = __USER_CS;
370 regs->ss = __USER_DS;
373 regs->eflags &= ~TF_MASK;
376 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
377 current->comm, current->pid, frame, regs->rip, frame->pretcode);
384 ka->sa.sa_handler = SIG_DFL;
385 signal_fault(regs, frame, "signal deliver");
389 * OK, we're invoking a handler
393 handle_signal(unsigned long sig, struct k_sigaction *ka,
394 siginfo_t *info, sigset_t *oldset, struct pt_regs * regs)
397 printk("handle_signal pid:%d sig:%lu rip:%lx rsp:%lx regs=%p\n", current->pid, sig,
398 regs->rip, regs->rsp, regs);
401 /* Are we from a system call? */
402 if ((long)regs->orig_rax >= 0) {
403 /* If so, check system call restarting.. */
405 case -ERESTARTNOHAND:
410 if (!(ka->sa.sa_flags & SA_RESTART)) {
415 case -ERESTARTNOINTR:
416 regs->rax = regs->orig_rax;
422 #ifdef CONFIG_IA32_EMULATION
423 if (current->thread.flags & THREAD_IA32) {
424 if (ka->sa.sa_flags & SA_SIGINFO)
425 ia32_setup_rt_frame(sig, ka, info, oldset, regs);
427 ia32_setup_frame(sig, ka, oldset, regs);
430 setup_rt_frame(sig, ka, info, oldset, regs);
432 if (ka->sa.sa_flags & SA_ONESHOT)
433 ka->sa.sa_handler = SIG_DFL;
435 if (!(ka->sa.sa_flags & SA_NODEFER)) {
436 spin_lock_irq(¤t->sigmask_lock);
437 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
438 sigaddset(¤t->blocked,sig);
439 recalc_sigpending(current);
440 spin_unlock_irq(¤t->sigmask_lock);
445 * Note that 'init' is a special process: it doesn't get signals it doesn't
446 * want to handle. Thus you cannot kill init even with a SIGKILL even by
449 int do_signal(struct pt_regs *regs, sigset_t *oldset)
452 struct k_sigaction *ka;
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 ((regs->cs & 3) != 3) {
465 oldset = ¤t->blocked;
470 spin_lock_irq(¤t->sigmask_lock);
471 signr = dequeue_signal(¤t->blocked, &info);
472 spin_unlock_irq(¤t->sigmask_lock);
478 if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
479 /* Let the debugger run. */
480 current->exit_code = signr;
481 current->state = TASK_STOPPED;
482 notify_parent(current, SIGCHLD);
485 /* We're back. Did the debugger cancel the sig? */
486 if (!(signr = current->exit_code))
488 current->exit_code = 0;
490 /* The debugger continued. Ignore SIGSTOP. */
491 if (signr == SIGSTOP)
494 /* Update the siginfo structure. Is this good? */
495 if (signr != info.si_signo) {
496 info.si_signo = signr;
498 info.si_code = SI_USER;
499 info.si_pid = current->p_pptr->pid;
500 info.si_uid = current->p_pptr->uid;
503 /* If the (new) signal is now blocked, requeue it. */
504 if (sigismember(¤t->blocked, signr)) {
505 send_sig_info(signr, &info, current);
510 ka = ¤t->sig->action[signr-1];
511 if (ka->sa.sa_handler == SIG_IGN) {
512 if (signr != SIGCHLD)
514 /* Check for SIGCHLD: it's special. */
515 while (sys_wait4(-1, NULL, WNOHANG, NULL) > 0)
520 if (ka->sa.sa_handler == SIG_DFL) {
521 int exit_code = signr;
523 /* Init gets no signals it doesn't want. */
524 if (current->pid == 1)
528 case SIGCONT: case SIGCHLD: case SIGWINCH: case SIGURG:
531 case SIGTSTP: case SIGTTIN: case SIGTTOU:
532 if (is_orphaned_pgrp(current->pgrp))
537 struct signal_struct *sig;
538 current->state = TASK_STOPPED;
539 current->exit_code = signr;
540 sig = current->p_pptr->sig;
541 if (sig && !(sig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
542 notify_parent(current, SIGCHLD);
547 case SIGQUIT: case SIGILL: case SIGTRAP:
548 case SIGABRT: case SIGFPE: case SIGSEGV:
549 case SIGBUS: case SIGSYS: case SIGXCPU: case SIGXFSZ:
550 if (do_coredump(signr, regs))
555 sig_exit(signr, exit_code, &info);
560 /* Reenable any watchpoints before delivering the
561 * signal to user space. The processor register will
562 * have been cleared if the watchpoint triggered
565 asm volatile("movq %0,%%db7"::"r"(current->thread.debugreg[7]));
566 /* Whee! Actually deliver the signal. */
567 handle_signal(signr, ka, &info, oldset, regs);
571 /* Did we come from a system call? */
572 if (regs->orig_rax >= 0) {
573 /* Restart the system call - no handlers present */
574 if (regs->rax == -ERESTARTNOHAND ||
575 regs->rax == -ERESTARTSYS ||
576 regs->rax == -ERESTARTNOINTR) {
577 regs->rax = regs->orig_rax;
585 void signal_fault(struct pt_regs *regs, void *frame, char *where)
587 struct task_struct *me = current;
589 printk("%s[%d] bad frame in %s frame:%p rip:%lx rsp:%lx orax:%lx\n",
590 me->comm,me->pid,where,frame,regs->rip,regs->rsp,regs->orig_rax);
592 force_sig(SIGSEGV, me);