2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
5 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
6 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
7 * 2000-2002 x86-64 support by Andi Kleen
10 #include <linux/sched.h>
12 #include <linux/smp.h>
13 #include <linux/kernel.h>
14 #include <linux/signal.h>
15 #include <linux/errno.h>
16 #include <linux/wait.h>
17 #include <linux/ptrace.h>
18 #include <linux/tracehook.h>
19 #include <linux/unistd.h>
20 #include <linux/stddef.h>
21 #include <linux/personality.h>
22 #include <linux/compiler.h>
23 #include <asm/processor.h>
24 #include <asm/ucontext.h>
25 #include <asm/uaccess.h>
27 #include <asm/proto.h>
28 #include <asm/ia32_unistd.h>
30 #include <asm/syscall.h>
31 #include <asm/syscalls.h>
34 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
36 #define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
37 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
38 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
42 # define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
44 # define FIX_EFLAGS __FIX_EFLAGS
47 int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
48 sigset_t *set, struct pt_regs * regs);
49 int ia32_setup_frame(int sig, struct k_sigaction *ka,
50 sigset_t *set, struct pt_regs * regs);
53 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
56 return do_sigaltstack(uss, uoss, regs->sp);
60 * Signal frame handlers.
63 static inline int save_i387(struct _fpstate __user *buf)
65 struct task_struct *tsk = current;
68 BUILD_BUG_ON(sizeof(struct user_i387_struct) !=
69 sizeof(tsk->thread.xstate->fxsave));
71 if ((unsigned long)buf % 16)
72 printk("save_i387: bad fpstate %p\n", buf);
76 clear_used_math(); /* trigger finit */
77 if (task_thread_info(tsk)->status & TS_USEDFPU) {
78 err = save_i387_checking((struct i387_fxsave_struct __user *)
82 task_thread_info(tsk)->status &= ~TS_USEDFPU;
85 if (__copy_to_user(buf, &tsk->thread.xstate->fxsave,
86 sizeof(struct i387_fxsave_struct)))
93 * This restores directly out of user space. Exceptions are handled.
95 static inline int restore_i387(struct _fpstate __user *buf)
97 struct task_struct *tsk = current;
106 if (!(task_thread_info(current)->status & TS_USEDFPU)) {
108 task_thread_info(current)->status |= TS_USEDFPU;
110 err = restore_fpu_checking((__force struct i387_fxsave_struct *)buf);
113 * Encountered an error while doing the restore from the
114 * user buffer, clear the fpu state.
123 * Do a signal return; undo the signal stack.
126 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
129 unsigned int err = 0;
131 /* Always make any pending restarted system calls return -EINTR */
132 current_thread_info()->restart_block.fn = do_no_restart_syscall;
134 #define COPY(x) err |= __get_user(regs->x, &sc->x)
136 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
137 COPY(dx); COPY(cx); COPY(ip);
147 /* Kernel saves and restores only the CS segment register on signals,
148 * which is the bare minimum needed to allow mixed 32/64-bit code.
149 * App's signal handler can save/restore other segments if needed. */
152 err |= __get_user(cs, &sc->cs);
153 regs->cs = cs | 3; /* Force into user mode */
157 unsigned int tmpflags;
158 err |= __get_user(tmpflags, &sc->flags);
159 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
160 regs->orig_ax = -1; /* disable syscall checks */
164 struct _fpstate __user * buf;
165 err |= __get_user(buf, &sc->fpstate);
168 if (!access_ok(VERIFY_READ, buf, sizeof(*buf)))
170 err |= restore_i387(buf);
172 struct task_struct *me = current;
180 err |= __get_user(*pax, &sc->ax);
187 asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
189 struct rt_sigframe __user *frame;
193 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
194 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
196 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
199 sigdelsetmask(&set, ~_BLOCKABLE);
200 spin_lock_irq(¤t->sighand->siglock);
201 current->blocked = set;
203 spin_unlock_irq(¤t->sighand->siglock);
205 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
208 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
214 signal_fault(regs,frame,"sigreturn");
219 * Set up a signal frame.
223 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, unsigned long mask, struct task_struct *me)
227 err |= __put_user(regs->cs, &sc->cs);
228 err |= __put_user(0, &sc->gs);
229 err |= __put_user(0, &sc->fs);
231 err |= __put_user(regs->di, &sc->di);
232 err |= __put_user(regs->si, &sc->si);
233 err |= __put_user(regs->bp, &sc->bp);
234 err |= __put_user(regs->sp, &sc->sp);
235 err |= __put_user(regs->bx, &sc->bx);
236 err |= __put_user(regs->dx, &sc->dx);
237 err |= __put_user(regs->cx, &sc->cx);
238 err |= __put_user(regs->ax, &sc->ax);
239 err |= __put_user(regs->r8, &sc->r8);
240 err |= __put_user(regs->r9, &sc->r9);
241 err |= __put_user(regs->r10, &sc->r10);
242 err |= __put_user(regs->r11, &sc->r11);
243 err |= __put_user(regs->r12, &sc->r12);
244 err |= __put_user(regs->r13, &sc->r13);
245 err |= __put_user(regs->r14, &sc->r14);
246 err |= __put_user(regs->r15, &sc->r15);
247 err |= __put_user(me->thread.trap_no, &sc->trapno);
248 err |= __put_user(me->thread.error_code, &sc->err);
249 err |= __put_user(regs->ip, &sc->ip);
250 err |= __put_user(regs->flags, &sc->flags);
251 err |= __put_user(mask, &sc->oldmask);
252 err |= __put_user(me->thread.cr2, &sc->cr2);
258 * Determine which stack to use..
262 get_stack(struct k_sigaction *ka, struct pt_regs *regs, unsigned long size)
266 /* Default to using normal stack - redzone*/
269 /* This is the X/Open sanctioned signal stack switching. */
270 if (ka->sa.sa_flags & SA_ONSTACK) {
271 if (sas_ss_flags(sp) == 0)
272 sp = current->sas_ss_sp + current->sas_ss_size;
275 return (void __user *)round_down(sp - size, 16);
278 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
279 sigset_t *set, struct pt_regs * regs)
281 struct rt_sigframe __user *frame;
282 struct _fpstate __user *fp = NULL;
284 struct task_struct *me = current;
287 fp = get_stack(ka, regs, sizeof(struct _fpstate));
288 frame = (void __user *)round_down(
289 (unsigned long)fp - sizeof(struct rt_sigframe), 16) - 8;
291 if (!access_ok(VERIFY_WRITE, fp, sizeof(struct _fpstate)))
294 if (save_i387(fp) < 0)
297 frame = get_stack(ka, regs, sizeof(struct rt_sigframe)) - 8;
299 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
302 if (ka->sa.sa_flags & SA_SIGINFO) {
303 err |= copy_siginfo_to_user(&frame->info, info);
308 /* Create the ucontext. */
309 err |= __put_user(0, &frame->uc.uc_flags);
310 err |= __put_user(0, &frame->uc.uc_link);
311 err |= __put_user(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
312 err |= __put_user(sas_ss_flags(regs->sp),
313 &frame->uc.uc_stack.ss_flags);
314 err |= __put_user(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
315 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0], me);
316 err |= __put_user(fp, &frame->uc.uc_mcontext.fpstate);
317 if (sizeof(*set) == 16) {
318 __put_user(set->sig[0], &frame->uc.uc_sigmask.sig[0]);
319 __put_user(set->sig[1], &frame->uc.uc_sigmask.sig[1]);
321 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
323 /* Set up to return from userspace. If provided, use a stub
324 already in userspace. */
325 /* x86-64 should always use SA_RESTORER. */
326 if (ka->sa.sa_flags & SA_RESTORER) {
327 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
329 /* could use a vstub here */
336 /* Set up registers for signal handler */
338 /* In case the signal handler was declared without prototypes */
341 /* This also works for non SA_SIGINFO handlers because they expect the
342 next argument after the signal number on the stack. */
343 regs->si = (unsigned long)&frame->info;
344 regs->dx = (unsigned long)&frame->uc;
345 regs->ip = (unsigned long) ka->sa.sa_handler;
347 regs->sp = (unsigned long)frame;
349 /* Set up the CS register to run signal handlers in 64-bit mode,
350 even if the handler happens to be interrupting 32-bit code. */
351 regs->cs = __USER_CS;
356 force_sigsegv(sig, current);
361 * OK, we're invoking a handler
365 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
366 sigset_t *oldset, struct pt_regs *regs)
370 /* Are we from a system call? */
371 if (syscall_get_nr(current, regs) >= 0) {
372 /* If so, check system call restarting.. */
373 switch (syscall_get_error(current, regs)) {
374 case -ERESTART_RESTARTBLOCK:
375 case -ERESTARTNOHAND:
380 if (!(ka->sa.sa_flags & SA_RESTART)) {
385 case -ERESTARTNOINTR:
386 regs->ax = regs->orig_ax;
393 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
394 * flag so that register information in the sigcontext is correct.
396 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
397 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
398 regs->flags &= ~X86_EFLAGS_TF;
400 #ifdef CONFIG_IA32_EMULATION
401 if (test_thread_flag(TIF_IA32)) {
402 if (ka->sa.sa_flags & SA_SIGINFO)
403 ret = ia32_setup_rt_frame(sig, ka, info, oldset, regs);
405 ret = ia32_setup_frame(sig, ka, oldset, regs);
408 ret = setup_rt_frame(sig, ka, info, oldset, regs);
412 * This has nothing to do with segment registers,
413 * despite the name. This magic affects uaccess.h
414 * macros' behavior. Reset it to the normal setting.
419 * Clear the direction flag as per the ABI for function entry.
421 regs->flags &= ~X86_EFLAGS_DF;
424 * Clear TF when entering the signal handler, but
425 * notify any tracer that was single-stepping it.
426 * The tracer may want to single-step inside the
429 regs->flags &= ~X86_EFLAGS_TF;
431 spin_lock_irq(¤t->sighand->siglock);
432 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
433 if (!(ka->sa.sa_flags & SA_NODEFER))
434 sigaddset(¤t->blocked,sig);
436 spin_unlock_irq(¤t->sighand->siglock);
438 tracehook_signal_handler(sig, info, ka, regs,
439 test_thread_flag(TIF_SINGLESTEP));
446 * Note that 'init' is a special process: it doesn't get signals it doesn't
447 * want to handle. Thus you cannot kill init even with a SIGKILL even by
450 static void do_signal(struct pt_regs *regs)
452 struct k_sigaction ka;
458 * We want the common case to go fast, which is why we may in certain
459 * cases get here from kernel mode. Just return without doing anything
461 * X86_32: vm86 regs switched out by assembly code before reaching
462 * here, so testing against kernel CS suffices.
464 if (!user_mode(regs))
467 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
468 oldset = ¤t->saved_sigmask;
470 oldset = ¤t->blocked;
472 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
474 /* Re-enable any watchpoints before delivering the
475 * signal to user space. The processor register will
476 * have been cleared if the watchpoint triggered
479 if (current->thread.debugreg7)
480 set_debugreg(current->thread.debugreg7, 7);
482 /* Whee! Actually deliver the signal. */
483 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
485 * A signal was successfully delivered; the saved
486 * sigmask will have been stored in the signal frame,
487 * and will be restored by sigreturn, so we can simply
488 * clear the TS_RESTORE_SIGMASK flag.
490 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
495 /* Did we come from a system call? */
496 if (syscall_get_nr(current, regs) >= 0) {
497 /* Restart the system call - no handlers present */
498 switch (syscall_get_error(current, regs)) {
499 case -ERESTARTNOHAND:
501 case -ERESTARTNOINTR:
502 regs->ax = regs->orig_ax;
505 case -ERESTART_RESTARTBLOCK:
506 regs->ax = test_thread_flag(TIF_IA32) ?
507 __NR_ia32_restart_syscall :
508 __NR_restart_syscall;
515 * If there's no signal to deliver, we just put the saved sigmask
518 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
519 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
520 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
524 void do_notify_resume(struct pt_regs *regs, void *unused,
525 __u32 thread_info_flags)
527 #ifdef CONFIG_X86_MCE
528 /* notify userspace of pending MCEs */
529 if (thread_info_flags & _TIF_MCE_NOTIFY)
531 #endif /* CONFIG_X86_MCE */
533 /* deal with pending signal delivery */
534 if (thread_info_flags & _TIF_SIGPENDING)
537 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
538 clear_thread_flag(TIF_NOTIFY_RESUME);
539 tracehook_notify_resume(regs);
543 void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
545 struct task_struct *me = current;
546 if (show_unhandled_signals && printk_ratelimit()) {
547 printk("%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
548 me->comm,me->pid,where,frame,regs->ip,regs->sp,regs->orig_ax);
549 print_vma_addr(" in ", regs->ip);
553 force_sig(SIGSEGV, me);