2 * linux/arch/x86_64/kernel/signal.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 2000, 2001, 2002 Andi Kleen 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-2002 x86-64 support by Andi Kleen
12 #include <linux/sched.h>
14 #include <linux/smp.h>
15 #include <linux/smp_lock.h>
16 #include <linux/kernel.h>
17 #include <linux/signal.h>
18 #include <linux/errno.h>
19 #include <linux/wait.h>
20 #include <linux/ptrace.h>
21 #include <linux/unistd.h>
22 #include <linux/stddef.h>
23 #include <linux/personality.h>
24 #include <linux/compiler.h>
25 #include <asm/ucontext.h>
26 #include <asm/uaccess.h>
28 #include <asm/proto.h>
29 #include <asm/ia32_unistd.h>
31 /* #define DEBUG_SIG 1 */
33 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
35 int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
36 sigset_t *set, struct pt_regs * regs);
37 int ia32_setup_frame(int sig, struct k_sigaction *ka,
38 sigset_t *set, struct pt_regs * regs);
41 sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize, struct pt_regs *regs)
43 sigset_t saveset, newset;
45 /* XXX: Don't preclude handling different sized sigset_t's. */
46 if (sigsetsize != sizeof(sigset_t))
49 if (copy_from_user(&newset, unewset, sizeof(newset)))
51 sigdelsetmask(&newset, ~_BLOCKABLE);
53 spin_lock_irq(¤t->sighand->siglock);
54 saveset = current->blocked;
55 current->blocked = newset;
57 spin_unlock_irq(¤t->sighand->siglock);
59 printk("rt_sigsuspend savset(%lx) newset(%lx) regs(%p) rip(%lx)\n",
60 saveset, newset, regs, regs->rip);
64 current->state = TASK_INTERRUPTIBLE;
66 if (do_signal(regs, &saveset))
72 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
75 return do_sigaltstack(uss, uoss, regs->rsp);
80 * Do a signal return; undo the signal stack.
85 char __user *pretcode;
91 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, unsigned long *prax)
95 /* Always make any pending restarted system calls return -EINTR */
96 current_thread_info()->restart_block.fn = do_no_restart_syscall;
98 #define COPY(x) err |= __get_user(regs->x, &sc->x)
100 COPY(rdi); COPY(rsi); COPY(rbp); COPY(rsp); COPY(rbx);
101 COPY(rdx); COPY(rcx); COPY(rip);
111 /* Kernel saves and restores only the CS segment register on signals,
112 * which is the bare minimum needed to allow mixed 32/64-bit code.
113 * App's signal handler can save/restore other segments if needed. */
116 err |= __get_user(cs, &sc->cs);
117 regs->cs = cs | 3; /* Force into user mode */
121 unsigned int tmpflags;
122 err |= __get_user(tmpflags, &sc->eflags);
123 regs->eflags = (regs->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
124 regs->orig_rax = -1; /* disable syscall checks */
128 struct _fpstate __user * buf;
129 err |= __get_user(buf, &sc->fpstate);
132 if (!access_ok(VERIFY_READ, buf, sizeof(*buf)))
134 err |= restore_i387(buf);
136 struct task_struct *me = current;
144 err |= __get_user(*prax, &sc->rax);
151 asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
153 struct rt_sigframe __user *frame;
157 frame = (struct rt_sigframe __user *)(regs->rsp - 8);
158 if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) {
161 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) {
165 sigdelsetmask(&set, ~_BLOCKABLE);
166 spin_lock_irq(¤t->sighand->siglock);
167 current->blocked = set;
169 spin_unlock_irq(¤t->sighand->siglock);
171 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &eax))
175 printk("%d sigreturn rip:%lx rsp:%lx frame:%p rax:%lx\n",current->pid,regs.rip,regs.rsp,frame,eax);
178 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->rsp) == -EFAULT)
184 signal_fault(regs,frame,"sigreturn");
189 * Set up a signal frame.
193 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, unsigned long mask, struct task_struct *me)
197 err |= __put_user(regs->cs, &sc->cs);
198 err |= __put_user(0, &sc->gs);
199 err |= __put_user(0, &sc->fs);
201 err |= __put_user(regs->rdi, &sc->rdi);
202 err |= __put_user(regs->rsi, &sc->rsi);
203 err |= __put_user(regs->rbp, &sc->rbp);
204 err |= __put_user(regs->rsp, &sc->rsp);
205 err |= __put_user(regs->rbx, &sc->rbx);
206 err |= __put_user(regs->rdx, &sc->rdx);
207 err |= __put_user(regs->rcx, &sc->rcx);
208 err |= __put_user(regs->rax, &sc->rax);
209 err |= __put_user(regs->r8, &sc->r8);
210 err |= __put_user(regs->r9, &sc->r9);
211 err |= __put_user(regs->r10, &sc->r10);
212 err |= __put_user(regs->r11, &sc->r11);
213 err |= __put_user(regs->r12, &sc->r12);
214 err |= __put_user(regs->r13, &sc->r13);
215 err |= __put_user(regs->r14, &sc->r14);
216 err |= __put_user(regs->r15, &sc->r15);
217 err |= __put_user(me->thread.trap_no, &sc->trapno);
218 err |= __put_user(me->thread.error_code, &sc->err);
219 err |= __put_user(regs->rip, &sc->rip);
220 err |= __put_user(regs->eflags, &sc->eflags);
221 err |= __put_user(mask, &sc->oldmask);
222 err |= __put_user(me->thread.cr2, &sc->cr2);
228 * Determine which stack to use..
232 get_stack(struct k_sigaction *ka, struct pt_regs *regs, unsigned long size)
236 /* Default to using normal stack - redzone*/
237 rsp = regs->rsp - 128;
239 /* This is the X/Open sanctioned signal stack switching. */
240 if (ka->sa.sa_flags & SA_ONSTACK) {
241 if (sas_ss_flags(rsp) == 0)
242 rsp = current->sas_ss_sp + current->sas_ss_size;
245 return (void __user *)round_down(rsp - size, 16);
248 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
249 sigset_t *set, struct pt_regs * regs)
251 struct rt_sigframe __user *frame;
252 struct _fpstate __user *fp = NULL;
254 struct task_struct *me = current;
257 fp = get_stack(ka, regs, sizeof(struct _fpstate));
258 frame = (void __user *)round_down(
259 (unsigned long)fp - sizeof(struct rt_sigframe), 16) - 8;
261 if (!access_ok(VERIFY_WRITE, fp, sizeof(struct _fpstate)))
264 if (save_i387(fp) < 0)
267 frame = get_stack(ka, regs, sizeof(struct rt_sigframe)) - 8;
269 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
272 if (ka->sa.sa_flags & SA_SIGINFO) {
273 err |= copy_siginfo_to_user(&frame->info, info);
278 /* Create the ucontext. */
279 err |= __put_user(0, &frame->uc.uc_flags);
280 err |= __put_user(0, &frame->uc.uc_link);
281 err |= __put_user(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
282 err |= __put_user(sas_ss_flags(regs->rsp),
283 &frame->uc.uc_stack.ss_flags);
284 err |= __put_user(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
285 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0], me);
286 err |= __put_user(fp, &frame->uc.uc_mcontext.fpstate);
287 if (sizeof(*set) == 16) {
288 __put_user(set->sig[0], &frame->uc.uc_sigmask.sig[0]);
289 __put_user(set->sig[1], &frame->uc.uc_sigmask.sig[1]);
291 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
293 /* Set up to return from userspace. If provided, use a stub
294 already in userspace. */
295 /* x86-64 should always use SA_RESTORER. */
296 if (ka->sa.sa_flags & SA_RESTORER) {
297 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
299 /* could use a vstub here */
307 printk("%d old rip %lx old rsp %lx old rax %lx\n", current->pid,regs->rip,regs->rsp,regs->rax);
310 /* Set up registers for signal handler */
312 struct exec_domain *ed = current_thread_info()->exec_domain;
313 if (unlikely(ed && ed->signal_invmap && sig < 32))
314 sig = ed->signal_invmap[sig];
317 /* In case the signal handler was declared without prototypes */
320 /* This also works for non SA_SIGINFO handlers because they expect the
321 next argument after the signal number on the stack. */
322 regs->rsi = (unsigned long)&frame->info;
323 regs->rdx = (unsigned long)&frame->uc;
324 regs->rip = (unsigned long) ka->sa.sa_handler;
326 regs->rsp = (unsigned long)frame;
328 /* Set up the CS register to run signal handlers in 64-bit mode,
329 even if the handler happens to be interrupting 32-bit code. */
330 regs->cs = __USER_CS;
332 /* This, by contrast, has nothing to do with segment registers -
333 see include/asm-x86_64/uaccess.h for details. */
336 regs->eflags &= ~TF_MASK;
337 if (test_thread_flag(TIF_SINGLESTEP))
338 ptrace_notify(SIGTRAP);
340 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
341 current->comm, current->pid, frame, regs->rip, frame->pretcode);
347 force_sigsegv(sig, current);
352 * OK, we're invoking a handler
356 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
357 sigset_t *oldset, struct pt_regs *regs)
362 printk("handle_signal pid:%d sig:%lu rip:%lx rsp:%lx regs=%p\n",
364 regs->rip, regs->rsp, regs);
367 /* Are we from a system call? */
368 if ((long)regs->orig_rax >= 0) {
369 /* If so, check system call restarting.. */
371 case -ERESTART_RESTARTBLOCK:
372 case -ERESTARTNOHAND:
377 if (!(ka->sa.sa_flags & SA_RESTART)) {
382 case -ERESTARTNOINTR:
383 regs->rax = regs->orig_rax;
390 * If TF is set due to a debugger (PT_DTRACE), clear the TF
391 * flag so that register information in the sigcontext is
394 if (unlikely(regs->eflags & TF_MASK)) {
395 if (likely(current->ptrace & PT_DTRACE)) {
396 current->ptrace &= ~PT_DTRACE;
397 regs->eflags &= ~TF_MASK;
401 #ifdef CONFIG_IA32_EMULATION
402 if (test_thread_flag(TIF_IA32)) {
403 if (ka->sa.sa_flags & SA_SIGINFO)
404 ret = ia32_setup_rt_frame(sig, ka, info, oldset, regs);
406 ret = ia32_setup_frame(sig, ka, oldset, regs);
409 ret = setup_rt_frame(sig, ka, info, oldset, regs);
412 spin_lock_irq(¤t->sighand->siglock);
413 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
414 if (!(ka->sa.sa_flags & SA_NODEFER))
415 sigaddset(¤t->blocked,sig);
417 spin_unlock_irq(¤t->sighand->siglock);
424 * Note that 'init' is a special process: it doesn't get signals it doesn't
425 * want to handle. Thus you cannot kill init even with a SIGKILL even by
428 int do_signal(struct pt_regs *regs, sigset_t *oldset)
430 struct k_sigaction ka;
435 * We want the common case to go fast, which
436 * is why we may in certain cases get here from
437 * kernel mode. Just return without doing anything
440 if (!user_mode(regs))
444 oldset = ¤t->blocked;
446 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
448 /* Reenable any watchpoints before delivering the
449 * signal to user space. The processor register will
450 * have been cleared if the watchpoint triggered
453 if (current->thread.debugreg7)
454 set_debugreg(current->thread.debugreg7, 7);
456 /* Whee! Actually deliver the signal. */
457 return handle_signal(signr, &info, &ka, oldset, regs);
460 /* Did we come from a system call? */
461 if ((long)regs->orig_rax >= 0) {
462 /* Restart the system call - no handlers present */
463 long res = regs->rax;
464 if (res == -ERESTARTNOHAND ||
465 res == -ERESTARTSYS ||
466 res == -ERESTARTNOINTR) {
467 regs->rax = regs->orig_rax;
470 if (regs->rax == (unsigned long)-ERESTART_RESTARTBLOCK) {
471 regs->rax = test_thread_flag(TIF_IA32) ?
472 __NR_ia32_restart_syscall :
473 __NR_restart_syscall;
480 void do_notify_resume(struct pt_regs *regs, sigset_t *oldset, __u32 thread_info_flags)
483 printk("do_notify_resume flags:%x rip:%lx rsp:%lx caller:%lx pending:%lx\n",
484 thread_info_flags, regs->rip, regs->rsp, __builtin_return_address(0),signal_pending(current));
487 /* Pending single-step? */
488 if (thread_info_flags & _TIF_SINGLESTEP) {
489 regs->eflags |= TF_MASK;
490 clear_thread_flag(TIF_SINGLESTEP);
493 /* deal with pending signal delivery */
494 if (thread_info_flags & _TIF_SIGPENDING)
495 do_signal(regs,oldset);
498 void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
500 struct task_struct *me = current;
502 printk("%s[%d] bad frame in %s frame:%p rip:%lx rsp:%lx orax:%lx\n",
503 me->comm,me->pid,where,frame,regs->rip,regs->rsp,regs->orig_rax);
505 force_sig(SIGSEGV, me);