blob: 87a9c2f28d99af8115775fef563e3988e2e3f2cb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
4 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10#include <linux/sched.h>
11#include <linux/mm.h>
12#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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/unistd.h>
19#include <linux/stddef.h>
20#include <linux/personality.h>
21#include <linux/compiler.h>
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -030022#include <linux/uaccess.h>
23
Harvey Harrison1a176802008-02-08 12:09:59 -080024#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/ucontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/i387.h>
27#include <asm/proto.h>
Oliver Korpillac5924b72005-05-27 12:52:55 -070028#include <asm/ia32_unistd.h>
Tim Hockine02e68d2007-07-21 17:10:36 +020029#include <asm/mce.h>
Harvey Harrison123a6342008-02-08 12:10:00 -080030#include "sigframe.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
33
Harvey Harrison1a176802008-02-08 12:09:59 -080034#define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
35 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
36 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
37 X86_EFLAGS_CF)
38
39#ifdef CONFIG_X86_32
40# define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
41#else
42# define FIX_EFLAGS __FIX_EFLAGS
43#endif
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045asmlinkage long
Linus Torvalds1da177e2005-04-16 15:20:36 -070046sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
47 struct pt_regs *regs)
48{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010049 return do_sigaltstack(uss, uoss, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
Linus Torvaldsb30f3ae2008-07-24 15:43:44 -070052/*
53 * Signal frame handlers.
54 */
55
56static inline int save_i387(struct _fpstate __user *buf)
57{
58 struct task_struct *tsk = current;
59 int err = 0;
60
61 BUILD_BUG_ON(sizeof(struct user_i387_struct) !=
62 sizeof(tsk->thread.xstate->fxsave));
63
64 if ((unsigned long)buf % 16)
65 printk("save_i387: bad fpstate %p\n", buf);
66
67 if (!used_math())
68 return 0;
69 clear_used_math(); /* trigger finit */
70 if (task_thread_info(tsk)->status & TS_USEDFPU) {
71 err = save_i387_checking((struct i387_fxsave_struct __user *)
72 buf);
73 if (err)
74 return err;
75 task_thread_info(tsk)->status &= ~TS_USEDFPU;
76 stts();
77 } else {
78 if (__copy_to_user(buf, &tsk->thread.xstate->fxsave,
79 sizeof(struct i387_fxsave_struct)))
80 return -1;
81 }
82 return 1;
83}
84
85/*
86 * This restores directly out of user space. Exceptions are handled.
87 */
88static inline int restore_i387(struct _fpstate __user *buf)
89{
90 struct task_struct *tsk = current;
91 int err;
92
93 if (!used_math()) {
94 err = init_fpu(tsk);
95 if (err)
96 return err;
97 }
98
99 if (!(task_thread_info(current)->status & TS_USEDFPU)) {
100 clts();
101 task_thread_info(current)->status |= TS_USEDFPU;
102 }
103 return restore_fpu_checking((__force struct i387_fxsave_struct *)buf);
104}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/*
107 * Do a signal return; undo the signal stack.
108 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static int
Harvey Harrison866bc132008-02-08 12:10:02 -0800110restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
111 unsigned long *pax)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 unsigned int err = 0;
114
115 /* Always make any pending restarted system calls return -EINTR */
116 current_thread_info()->restart_block.fn = do_no_restart_syscall;
117
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300118#define COPY(x) (err |= __get_user(regs->x, &sc->x))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
H. Peter Anvin742fa542008-01-30 13:30:56 +0100120 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
121 COPY(dx); COPY(cx); COPY(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 COPY(r8);
123 COPY(r9);
124 COPY(r10);
125 COPY(r11);
126 COPY(r12);
127 COPY(r13);
128 COPY(r14);
129 COPY(r15);
130
Bryan Forde4e5d322005-11-05 17:25:54 +0100131 /* Kernel saves and restores only the CS segment register on signals,
132 * which is the bare minimum needed to allow mixed 32/64-bit code.
133 * App's signal handler can save/restore other segments if needed. */
134 {
135 unsigned cs;
136 err |= __get_user(cs, &sc->cs);
137 regs->cs = cs | 3; /* Force into user mode */
138 }
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 {
141 unsigned int tmpflags;
H. Peter Anvin742fa542008-01-30 13:30:56 +0100142 err |= __get_user(tmpflags, &sc->flags);
Harvey Harrison1a176802008-02-08 12:09:59 -0800143 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100144 regs->orig_ax = -1; /* disable syscall checks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146
147 {
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300148 struct _fpstate __user *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 err |= __get_user(buf, &sc->fpstate);
150
151 if (buf) {
152 if (!access_ok(VERIFY_READ, buf, sizeof(*buf)))
153 goto badframe;
154 err |= restore_i387(buf);
155 } else {
156 struct task_struct *me = current;
157 if (used_math()) {
158 clear_fpu(me);
159 clear_used_math();
160 }
161 }
162 }
163
Harvey Harrison866bc132008-02-08 12:10:02 -0800164 err |= __get_user(*pax, &sc->ax);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return err;
166
167badframe:
168 return 1;
169}
170
171asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
172{
173 struct rt_sigframe __user *frame;
174 sigset_t set;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100175 unsigned long ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Harvey Harrison2d19c452008-02-08 12:10:00 -0800177 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
178 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 goto badframe;
Harvey Harrison2d19c452008-02-08 12:10:00 -0800180 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 sigdelsetmask(&set, ~_BLOCKABLE);
184 spin_lock_irq(&current->sighand->siglock);
185 current->blocked = set;
186 recalc_sigpending();
187 spin_unlock_irq(&current->sighand->siglock);
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300188
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100189 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 goto badframe;
191
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100192 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 goto badframe;
194
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100195 return ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197badframe:
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300198 signal_fault(regs, frame, "sigreturn");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return 0;
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300200}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202/*
203 * Set up a signal frame.
204 */
205
206static inline int
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300207setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
208 unsigned long mask, struct task_struct *me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Bryan Forde4e5d322005-11-05 17:25:54 +0100212 err |= __put_user(regs->cs, &sc->cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 err |= __put_user(0, &sc->gs);
214 err |= __put_user(0, &sc->fs);
215
H. Peter Anvin742fa542008-01-30 13:30:56 +0100216 err |= __put_user(regs->di, &sc->di);
217 err |= __put_user(regs->si, &sc->si);
218 err |= __put_user(regs->bp, &sc->bp);
219 err |= __put_user(regs->sp, &sc->sp);
220 err |= __put_user(regs->bx, &sc->bx);
221 err |= __put_user(regs->dx, &sc->dx);
222 err |= __put_user(regs->cx, &sc->cx);
223 err |= __put_user(regs->ax, &sc->ax);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 err |= __put_user(regs->r8, &sc->r8);
225 err |= __put_user(regs->r9, &sc->r9);
226 err |= __put_user(regs->r10, &sc->r10);
227 err |= __put_user(regs->r11, &sc->r11);
228 err |= __put_user(regs->r12, &sc->r12);
229 err |= __put_user(regs->r13, &sc->r13);
230 err |= __put_user(regs->r14, &sc->r14);
231 err |= __put_user(regs->r15, &sc->r15);
232 err |= __put_user(me->thread.trap_no, &sc->trapno);
233 err |= __put_user(me->thread.error_code, &sc->err);
H. Peter Anvin742fa542008-01-30 13:30:56 +0100234 err |= __put_user(regs->ip, &sc->ip);
235 err |= __put_user(regs->flags, &sc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 err |= __put_user(mask, &sc->oldmask);
237 err |= __put_user(me->thread.cr2, &sc->cr2);
238
239 return err;
240}
241
242/*
243 * Determine which stack to use..
244 */
245
246static void __user *
247get_stack(struct k_sigaction *ka, struct pt_regs *regs, unsigned long size)
248{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100249 unsigned long sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 /* Default to using normal stack - redzone*/
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100252 sp = regs->sp - 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 /* This is the X/Open sanctioned signal stack switching. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (ka->sa.sa_flags & SA_ONSTACK) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100256 if (sas_ss_flags(sp) == 0)
257 sp = current->sas_ss_sp + current->sas_ss_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100260 return (void __user *)round_down(sp - size, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Roland McGrath0928d6e2005-06-23 00:08:37 -0700263static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300264 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 struct rt_sigframe __user *frame;
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300267 struct _fpstate __user *fp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 int err = 0;
269 struct task_struct *me = current;
270
271 if (used_math()) {
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300272 fp = get_stack(ka, regs, sizeof(struct _fpstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 frame = (void __user *)round_down(
274 (unsigned long)fp - sizeof(struct rt_sigframe), 16) - 8;
275
276 if (!access_ok(VERIFY_WRITE, fp, sizeof(struct _fpstate)))
277 goto give_sigsegv;
278
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300279 if (save_i387(fp) < 0)
280 err |= -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 } else
282 frame = get_stack(ka, regs, sizeof(struct rt_sigframe)) - 8;
283
284 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
285 goto give_sigsegv;
286
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300287 if (ka->sa.sa_flags & SA_SIGINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 err |= copy_siginfo_to_user(&frame->info, info);
289 if (err)
290 goto give_sigsegv;
291 }
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 /* Create the ucontext. */
294 err |= __put_user(0, &frame->uc.uc_flags);
295 err |= __put_user(0, &frame->uc.uc_link);
296 err |= __put_user(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100297 err |= __put_user(sas_ss_flags(regs->sp),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 &frame->uc.uc_stack.ss_flags);
299 err |= __put_user(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
300 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0], me);
301 err |= __put_user(fp, &frame->uc.uc_mcontext.fpstate);
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300302 if (sizeof(*set) == 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 __put_user(set->sig[0], &frame->uc.uc_sigmask.sig[0]);
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300304 __put_user(set->sig[1], &frame->uc.uc_sigmask.sig[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 } else
306 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
307
308 /* Set up to return from userspace. If provided, use a stub
309 already in userspace. */
310 /* x86-64 should always use SA_RESTORER. */
311 if (ka->sa.sa_flags & SA_RESTORER) {
312 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
313 } else {
314 /* could use a vstub here */
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300315 goto give_sigsegv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317
318 if (err)
319 goto give_sigsegv;
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 /* Set up registers for signal handler */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100322 regs->di = sig;
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300323 /* In case the signal handler was declared without prototypes */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100324 regs->ax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 /* This also works for non SA_SIGINFO handlers because they expect the
327 next argument after the signal number on the stack. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100328 regs->si = (unsigned long)&frame->info;
329 regs->dx = (unsigned long)&frame->uc;
330 regs->ip = (unsigned long) ka->sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100332 regs->sp = (unsigned long)frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Bryan Forde4e5d322005-11-05 17:25:54 +0100334 /* Set up the CS register to run signal handlers in 64-bit mode,
335 even if the handler happens to be interrupting 32-bit code. */
336 regs->cs = __USER_CS;
337
Andi Kleen1d001df2006-09-26 10:52:26 +0200338 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340give_sigsegv:
341 force_sigsegv(sig, current);
Andi Kleen1d001df2006-09-26 10:52:26 +0200342 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345/*
Roland McGrath40f09332008-02-28 19:57:07 -0800346 * Return -1L or the syscall number that @regs is executing.
347 */
348static long current_syscall(struct pt_regs *regs)
349{
350 /*
351 * We always sign-extend a -1 value being set here,
352 * so this is always either -1L or a syscall number.
353 */
354 return regs->orig_ax;
355}
356
357/*
358 * Return a value that is -EFOO if the system call in @regs->orig_ax
359 * returned an error. This only works for @regs from @current.
360 */
361static long current_syscall_ret(struct pt_regs *regs)
362{
363#ifdef CONFIG_IA32_EMULATION
364 if (test_thread_flag(TIF_IA32))
365 /*
366 * Sign-extend the value so (int)-EFOO becomes (long)-EFOO
367 * and will match correctly in comparisons.
368 */
369 return (int) regs->ax;
370#endif
371 return regs->ax;
372}
373
374/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 * OK, we're invoking a handler
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300376 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Roland McGrath0928d6e2005-06-23 00:08:37 -0700378static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800380 sigset_t *oldset, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Roland McGrath0928d6e2005-06-23 00:08:37 -0700382 int ret;
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 /* Are we from a system call? */
Roland McGrath40f09332008-02-28 19:57:07 -0800385 if (current_syscall(regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /* If so, check system call restarting.. */
Roland McGrath40f09332008-02-28 19:57:07 -0800387 switch (current_syscall_ret(regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800388 case -ERESTART_RESTARTBLOCK:
389 case -ERESTARTNOHAND:
390 regs->ax = -EINTR;
391 break;
392
393 case -ERESTARTSYS:
394 if (!(ka->sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100395 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800397 }
398 /* fallthrough */
399 case -ERESTARTNOINTR:
400 regs->ax = regs->orig_ax;
401 regs->ip -= 2;
402 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404 }
405
Andi Kleend61915d2005-04-16 15:25:00 -0700406 /*
Roland McGrathe1f28772008-01-30 13:30:50 +0100407 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
408 * flag so that register information in the sigcontext is correct.
Andi Kleend61915d2005-04-16 15:25:00 -0700409 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100410 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
Roland McGrathe1f28772008-01-30 13:30:50 +0100411 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100412 regs->flags &= ~X86_EFLAGS_TF;
Andi Kleend61915d2005-04-16 15:25:00 -0700413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414#ifdef CONFIG_IA32_EMULATION
415 if (test_thread_flag(TIF_IA32)) {
416 if (ka->sa.sa_flags & SA_SIGINFO)
Roland McGrath0928d6e2005-06-23 00:08:37 -0700417 ret = ia32_setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 else
Roland McGrath0928d6e2005-06-23 00:08:37 -0700419 ret = ia32_setup_frame(sig, ka, oldset, regs);
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300420 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421#endif
Roland McGrath0928d6e2005-06-23 00:08:37 -0700422 ret = setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Andi Kleen1d001df2006-09-26 10:52:26 +0200424 if (ret == 0) {
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700425 /*
Roland McGrath55928e32008-04-19 14:27:56 -0700426 * This has nothing to do with segment registers,
427 * despite the name. This magic affects uaccess.h
428 * macros' behavior. Reset it to the normal setting.
429 */
430 set_fs(USER_DS);
431
432 /*
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700433 * Clear the direction flag as per the ABI for function entry.
434 */
435 regs->flags &= ~X86_EFLAGS_DF;
436
437 /*
438 * Clear TF when entering the signal handler, but
439 * notify any tracer that was single-stepping it.
440 * The tracer may want to single-step inside the
441 * handler too.
442 */
443 regs->flags &= ~X86_EFLAGS_TF;
444 if (test_thread_flag(TIF_SINGLESTEP))
445 ptrace_notify(SIGTRAP);
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 spin_lock_irq(&current->sighand->siglock);
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300448 sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
Steven Rostedt69be8f12005-08-29 11:44:09 -0400449 if (!(ka->sa.sa_flags & SA_NODEFER))
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300450 sigaddset(&current->blocked, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 recalc_sigpending();
452 spin_unlock_irq(&current->sighand->siglock);
453 }
Roland McGrath0928d6e2005-06-23 00:08:37 -0700454
455 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458/*
459 * Note that 'init' is a special process: it doesn't get signals it doesn't
460 * want to handle. Thus you cannot kill init even with a SIGKILL even by
461 * mistake.
462 */
Andi Kleen1d001df2006-09-26 10:52:26 +0200463static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 struct k_sigaction ka;
466 siginfo_t info;
467 int signr;
Andi Kleen1d001df2006-09-26 10:52:26 +0200468 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 /*
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800471 * We want the common case to go fast, which is why we may in certain
472 * cases get here from kernel mode. Just return without doing anything
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 * if so.
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800474 * X86_32: vm86 regs switched out by assembly code before reaching
475 * here, so testing against kernel CS suffices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700477 if (!user_mode(regs))
Andi Kleen1d001df2006-09-26 10:52:26 +0200478 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700480 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
Andi Kleen1d001df2006-09-26 10:52:26 +0200481 oldset = &current->saved_sigmask;
482 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 oldset = &current->blocked;
484
485 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
486 if (signr > 0) {
Simon Arlott676b1852007-10-20 01:25:36 +0200487 /* Re-enable any watchpoints before delivering the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 * signal to user space. The processor register will
489 * have been cleared if the watchpoint triggered
490 * inside the kernel.
491 */
492 if (current->thread.debugreg7)
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700493 set_debugreg(current->thread.debugreg7, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 /* Whee! Actually deliver the signal. */
Andi Kleen1d001df2006-09-26 10:52:26 +0200496 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700497 /*
498 * A signal was successfully delivered; the saved
Andi Kleen1d001df2006-09-26 10:52:26 +0200499 * sigmask will have been stored in the signal frame,
500 * and will be restored by sigreturn, so we can simply
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700501 * clear the TS_RESTORE_SIGMASK flag.
502 */
503 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
Andi Kleen1d001df2006-09-26 10:52:26 +0200504 }
505 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /* Did we come from a system call? */
Roland McGrath40f09332008-02-28 19:57:07 -0800509 if (current_syscall(regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /* Restart the system call - no handlers present */
Roland McGrath40f09332008-02-28 19:57:07 -0800511 switch (current_syscall_ret(regs)) {
Andi Kleen1d001df2006-09-26 10:52:26 +0200512 case -ERESTARTNOHAND:
513 case -ERESTARTSYS:
514 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100515 regs->ax = regs->orig_ax;
516 regs->ip -= 2;
Andi Kleen1d001df2006-09-26 10:52:26 +0200517 break;
518 case -ERESTART_RESTARTBLOCK:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100519 regs->ax = test_thread_flag(TIF_IA32) ?
Andi Kleen607a1682005-05-20 14:27:58 -0700520 __NR_ia32_restart_syscall :
521 __NR_restart_syscall;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100522 regs->ip -= 2;
Andi Kleen1d001df2006-09-26 10:52:26 +0200523 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525 }
Andi Kleen1d001df2006-09-26 10:52:26 +0200526
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800527 /*
528 * If there's no signal to deliver, we just put the saved sigmask
529 * back.
530 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700531 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
532 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
Andi Kleen1d001df2006-09-26 10:52:26 +0200533 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800537void do_notify_resume(struct pt_regs *regs, void *unused,
538 __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Tim Hockine02e68d2007-07-21 17:10:36 +0200540#ifdef CONFIG_X86_MCE
541 /* notify userspace of pending MCEs */
542 if (thread_info_flags & _TIF_MCE_NOTIFY)
543 mce_notify_user();
544#endif /* CONFIG_X86_MCE */
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* deal with pending signal delivery */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700547 if (thread_info_flags & _TIF_SIGPENDING)
Andi Kleen1d001df2006-09-26 10:52:26 +0200548 do_signal(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
551void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300552{
553 struct task_struct *me = current;
Andi Kleen03252912008-01-30 13:33:18 +0100554 if (show_unhandled_signals && printk_ratelimit()) {
555 printk("%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300556 me->comm, me->pid, where, frame, regs->ip,
557 regs->sp, regs->orig_ax);
Andi Kleen03252912008-01-30 13:33:18 +0100558 print_vma_addr(" in ", regs->ip);
559 printk("\n");
560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300562 force_sig(SIGSEGV, me);
563}