blob: b95a0a609053ea65dd2d5614fd5ba3483919fef6 [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>
Harvey Harrison1a176802008-02-08 12:09:59 -080022#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/ucontext.h>
24#include <asm/uaccess.h>
25#include <asm/i387.h>
26#include <asm/proto.h>
Oliver Korpillac5924b72005-05-27 12:52:55 -070027#include <asm/ia32_unistd.h>
Tim Hockine02e68d2007-07-21 17:10:36 +020028#include <asm/mce.h>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053029#include <asm/syscalls.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
Roland McGrath0928d6e2005-06-23 00:08:37 -070045int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 sigset_t *set, struct pt_regs * regs);
Roland McGrath0928d6e2005-06-23 00:08:37 -070047int ia32_setup_frame(int sig, struct k_sigaction *ka,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 sigset_t *set, struct pt_regs * regs);
49
50asmlinkage long
Linus Torvalds1da177e2005-04-16 15:20:36 -070051sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
52 struct pt_regs *regs)
53{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010054 return do_sigaltstack(uss, uoss, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
57
58/*
59 * Do a signal return; undo the signal stack.
60 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static int
Harvey Harrison866bc132008-02-08 12:10:02 -080062restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
63 unsigned long *pax)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 unsigned int err = 0;
66
67 /* Always make any pending restarted system calls return -EINTR */
68 current_thread_info()->restart_block.fn = do_no_restart_syscall;
69
70#define COPY(x) err |= __get_user(regs->x, &sc->x)
71
H. Peter Anvin742fa542008-01-30 13:30:56 +010072 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
73 COPY(dx); COPY(cx); COPY(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 COPY(r8);
75 COPY(r9);
76 COPY(r10);
77 COPY(r11);
78 COPY(r12);
79 COPY(r13);
80 COPY(r14);
81 COPY(r15);
82
Bryan Forde4e5d322005-11-05 17:25:54 +010083 /* Kernel saves and restores only the CS segment register on signals,
84 * which is the bare minimum needed to allow mixed 32/64-bit code.
85 * App's signal handler can save/restore other segments if needed. */
86 {
87 unsigned cs;
88 err |= __get_user(cs, &sc->cs);
89 regs->cs = cs | 3; /* Force into user mode */
90 }
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 {
93 unsigned int tmpflags;
H. Peter Anvin742fa542008-01-30 13:30:56 +010094 err |= __get_user(tmpflags, &sc->flags);
Harvey Harrison1a176802008-02-08 12:09:59 -080095 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010096 regs->orig_ax = -1; /* disable syscall checks */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
98
99 {
100 struct _fpstate __user * buf;
101 err |= __get_user(buf, &sc->fpstate);
102
103 if (buf) {
104 if (!access_ok(VERIFY_READ, buf, sizeof(*buf)))
105 goto badframe;
106 err |= restore_i387(buf);
107 } else {
108 struct task_struct *me = current;
109 if (used_math()) {
110 clear_fpu(me);
111 clear_used_math();
112 }
113 }
114 }
115
Harvey Harrison866bc132008-02-08 12:10:02 -0800116 err |= __get_user(*pax, &sc->ax);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return err;
118
119badframe:
120 return 1;
121}
122
123asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
124{
125 struct rt_sigframe __user *frame;
126 sigset_t set;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100127 unsigned long ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Harvey Harrison2d19c452008-02-08 12:10:00 -0800129 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
130 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 goto badframe;
Harvey Harrison2d19c452008-02-08 12:10:00 -0800132 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 sigdelsetmask(&set, ~_BLOCKABLE);
136 spin_lock_irq(&current->sighand->siglock);
137 current->blocked = set;
138 recalc_sigpending();
139 spin_unlock_irq(&current->sighand->siglock);
140
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100141 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 goto badframe;
143
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100144 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 goto badframe;
146
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100147 return ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149badframe:
150 signal_fault(regs,frame,"sigreturn");
151 return 0;
152}
153
154/*
155 * Set up a signal frame.
156 */
157
158static inline int
159setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, unsigned long mask, struct task_struct *me)
160{
161 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Bryan Forde4e5d322005-11-05 17:25:54 +0100163 err |= __put_user(regs->cs, &sc->cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 err |= __put_user(0, &sc->gs);
165 err |= __put_user(0, &sc->fs);
166
H. Peter Anvin742fa542008-01-30 13:30:56 +0100167 err |= __put_user(regs->di, &sc->di);
168 err |= __put_user(regs->si, &sc->si);
169 err |= __put_user(regs->bp, &sc->bp);
170 err |= __put_user(regs->sp, &sc->sp);
171 err |= __put_user(regs->bx, &sc->bx);
172 err |= __put_user(regs->dx, &sc->dx);
173 err |= __put_user(regs->cx, &sc->cx);
174 err |= __put_user(regs->ax, &sc->ax);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 err |= __put_user(regs->r8, &sc->r8);
176 err |= __put_user(regs->r9, &sc->r9);
177 err |= __put_user(regs->r10, &sc->r10);
178 err |= __put_user(regs->r11, &sc->r11);
179 err |= __put_user(regs->r12, &sc->r12);
180 err |= __put_user(regs->r13, &sc->r13);
181 err |= __put_user(regs->r14, &sc->r14);
182 err |= __put_user(regs->r15, &sc->r15);
183 err |= __put_user(me->thread.trap_no, &sc->trapno);
184 err |= __put_user(me->thread.error_code, &sc->err);
H. Peter Anvin742fa542008-01-30 13:30:56 +0100185 err |= __put_user(regs->ip, &sc->ip);
186 err |= __put_user(regs->flags, &sc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 err |= __put_user(mask, &sc->oldmask);
188 err |= __put_user(me->thread.cr2, &sc->cr2);
189
190 return err;
191}
192
193/*
194 * Determine which stack to use..
195 */
196
197static void __user *
198get_stack(struct k_sigaction *ka, struct pt_regs *regs, unsigned long size)
199{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100200 unsigned long sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 /* Default to using normal stack - redzone*/
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100203 sp = regs->sp - 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 /* This is the X/Open sanctioned signal stack switching. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (ka->sa.sa_flags & SA_ONSTACK) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100207 if (sas_ss_flags(sp) == 0)
208 sp = current->sas_ss_sp + current->sas_ss_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100211 return (void __user *)round_down(sp - size, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Roland McGrath0928d6e2005-06-23 00:08:37 -0700214static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 sigset_t *set, struct pt_regs * regs)
216{
217 struct rt_sigframe __user *frame;
218 struct _fpstate __user *fp = NULL;
219 int err = 0;
220 struct task_struct *me = current;
221
222 if (used_math()) {
223 fp = get_stack(ka, regs, sizeof(struct _fpstate));
224 frame = (void __user *)round_down(
225 (unsigned long)fp - sizeof(struct rt_sigframe), 16) - 8;
226
227 if (!access_ok(VERIFY_WRITE, fp, sizeof(struct _fpstate)))
228 goto give_sigsegv;
229
230 if (save_i387(fp) < 0)
231 err |= -1;
232 } else
233 frame = get_stack(ka, regs, sizeof(struct rt_sigframe)) - 8;
234
235 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
236 goto give_sigsegv;
237
238 if (ka->sa.sa_flags & SA_SIGINFO) {
239 err |= copy_siginfo_to_user(&frame->info, info);
240 if (err)
241 goto give_sigsegv;
242 }
243
244 /* Create the ucontext. */
245 err |= __put_user(0, &frame->uc.uc_flags);
246 err |= __put_user(0, &frame->uc.uc_link);
247 err |= __put_user(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100248 err |= __put_user(sas_ss_flags(regs->sp),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 &frame->uc.uc_stack.ss_flags);
250 err |= __put_user(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
251 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0], me);
252 err |= __put_user(fp, &frame->uc.uc_mcontext.fpstate);
253 if (sizeof(*set) == 16) {
254 __put_user(set->sig[0], &frame->uc.uc_sigmask.sig[0]);
255 __put_user(set->sig[1], &frame->uc.uc_sigmask.sig[1]);
256 } else
257 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
258
259 /* Set up to return from userspace. If provided, use a stub
260 already in userspace. */
261 /* x86-64 should always use SA_RESTORER. */
262 if (ka->sa.sa_flags & SA_RESTORER) {
263 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
264 } else {
265 /* could use a vstub here */
266 goto give_sigsegv;
267 }
268
269 if (err)
270 goto give_sigsegv;
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 /* Set up registers for signal handler */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100273 regs->di = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /* In case the signal handler was declared without prototypes */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100275 regs->ax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 /* This also works for non SA_SIGINFO handlers because they expect the
278 next argument after the signal number on the stack. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100279 regs->si = (unsigned long)&frame->info;
280 regs->dx = (unsigned long)&frame->uc;
281 regs->ip = (unsigned long) ka->sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100283 regs->sp = (unsigned long)frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Bryan Forde4e5d322005-11-05 17:25:54 +0100285 /* Set up the CS register to run signal handlers in 64-bit mode,
286 even if the handler happens to be interrupting 32-bit code. */
287 regs->cs = __USER_CS;
288
Andi Kleen1d001df2006-09-26 10:52:26 +0200289 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291give_sigsegv:
292 force_sigsegv(sig, current);
Andi Kleen1d001df2006-09-26 10:52:26 +0200293 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296/*
Roland McGrath40f09332008-02-28 19:57:07 -0800297 * Return -1L or the syscall number that @regs is executing.
298 */
299static long current_syscall(struct pt_regs *regs)
300{
301 /*
302 * We always sign-extend a -1 value being set here,
303 * so this is always either -1L or a syscall number.
304 */
305 return regs->orig_ax;
306}
307
308/*
309 * Return a value that is -EFOO if the system call in @regs->orig_ax
310 * returned an error. This only works for @regs from @current.
311 */
312static long current_syscall_ret(struct pt_regs *regs)
313{
314#ifdef CONFIG_IA32_EMULATION
315 if (test_thread_flag(TIF_IA32))
316 /*
317 * Sign-extend the value so (int)-EFOO becomes (long)-EFOO
318 * and will match correctly in comparisons.
319 */
320 return (int) regs->ax;
321#endif
322 return regs->ax;
323}
324
325/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 * OK, we're invoking a handler
327 */
328
Roland McGrath0928d6e2005-06-23 00:08:37 -0700329static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800331 sigset_t *oldset, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Roland McGrath0928d6e2005-06-23 00:08:37 -0700333 int ret;
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /* Are we from a system call? */
Roland McGrath40f09332008-02-28 19:57:07 -0800336 if (current_syscall(regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 /* If so, check system call restarting.. */
Roland McGrath40f09332008-02-28 19:57:07 -0800338 switch (current_syscall_ret(regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800339 case -ERESTART_RESTARTBLOCK:
340 case -ERESTARTNOHAND:
341 regs->ax = -EINTR;
342 break;
343
344 case -ERESTARTSYS:
345 if (!(ka->sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100346 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800348 }
349 /* fallthrough */
350 case -ERESTARTNOINTR:
351 regs->ax = regs->orig_ax;
352 regs->ip -= 2;
353 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355 }
356
Andi Kleend61915d2005-04-16 15:25:00 -0700357 /*
Roland McGrathe1f28772008-01-30 13:30:50 +0100358 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
359 * flag so that register information in the sigcontext is correct.
Andi Kleend61915d2005-04-16 15:25:00 -0700360 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100361 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
Roland McGrathe1f28772008-01-30 13:30:50 +0100362 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100363 regs->flags &= ~X86_EFLAGS_TF;
Andi Kleend61915d2005-04-16 15:25:00 -0700364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365#ifdef CONFIG_IA32_EMULATION
366 if (test_thread_flag(TIF_IA32)) {
367 if (ka->sa.sa_flags & SA_SIGINFO)
Roland McGrath0928d6e2005-06-23 00:08:37 -0700368 ret = ia32_setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 else
Roland McGrath0928d6e2005-06-23 00:08:37 -0700370 ret = ia32_setup_frame(sig, ka, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 } else
372#endif
Roland McGrath0928d6e2005-06-23 00:08:37 -0700373 ret = setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Andi Kleen1d001df2006-09-26 10:52:26 +0200375 if (ret == 0) {
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700376 /*
Roland McGrath55928e32008-04-19 14:27:56 -0700377 * This has nothing to do with segment registers,
378 * despite the name. This magic affects uaccess.h
379 * macros' behavior. Reset it to the normal setting.
380 */
381 set_fs(USER_DS);
382
383 /*
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700384 * Clear the direction flag as per the ABI for function entry.
385 */
386 regs->flags &= ~X86_EFLAGS_DF;
387
388 /*
389 * Clear TF when entering the signal handler, but
390 * notify any tracer that was single-stepping it.
391 * The tracer may want to single-step inside the
392 * handler too.
393 */
394 regs->flags &= ~X86_EFLAGS_TF;
395 if (test_thread_flag(TIF_SINGLESTEP))
396 ptrace_notify(SIGTRAP);
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 spin_lock_irq(&current->sighand->siglock);
399 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
Steven Rostedt69be8f12005-08-29 11:44:09 -0400400 if (!(ka->sa.sa_flags & SA_NODEFER))
401 sigaddset(&current->blocked,sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 recalc_sigpending();
403 spin_unlock_irq(&current->sighand->siglock);
404 }
Roland McGrath0928d6e2005-06-23 00:08:37 -0700405
406 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409/*
410 * Note that 'init' is a special process: it doesn't get signals it doesn't
411 * want to handle. Thus you cannot kill init even with a SIGKILL even by
412 * mistake.
413 */
Andi Kleen1d001df2006-09-26 10:52:26 +0200414static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 struct k_sigaction ka;
417 siginfo_t info;
418 int signr;
Andi Kleen1d001df2006-09-26 10:52:26 +0200419 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 /*
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800422 * We want the common case to go fast, which is why we may in certain
423 * cases get here from kernel mode. Just return without doing anything
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 * if so.
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800425 * X86_32: vm86 regs switched out by assembly code before reaching
426 * here, so testing against kernel CS suffices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700428 if (!user_mode(regs))
Andi Kleen1d001df2006-09-26 10:52:26 +0200429 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700431 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
Andi Kleen1d001df2006-09-26 10:52:26 +0200432 oldset = &current->saved_sigmask;
433 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 oldset = &current->blocked;
435
436 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
437 if (signr > 0) {
Simon Arlott676b1852007-10-20 01:25:36 +0200438 /* Re-enable any watchpoints before delivering the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 * signal to user space. The processor register will
440 * have been cleared if the watchpoint triggered
441 * inside the kernel.
442 */
443 if (current->thread.debugreg7)
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700444 set_debugreg(current->thread.debugreg7, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /* Whee! Actually deliver the signal. */
Andi Kleen1d001df2006-09-26 10:52:26 +0200447 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700448 /*
449 * A signal was successfully delivered; the saved
Andi Kleen1d001df2006-09-26 10:52:26 +0200450 * sigmask will have been stored in the signal frame,
451 * and will be restored by sigreturn, so we can simply
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700452 * clear the TS_RESTORE_SIGMASK flag.
453 */
454 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
Andi Kleen1d001df2006-09-26 10:52:26 +0200455 }
456 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 /* Did we come from a system call? */
Roland McGrath40f09332008-02-28 19:57:07 -0800460 if (current_syscall(regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /* Restart the system call - no handlers present */
Roland McGrath40f09332008-02-28 19:57:07 -0800462 switch (current_syscall_ret(regs)) {
Andi Kleen1d001df2006-09-26 10:52:26 +0200463 case -ERESTARTNOHAND:
464 case -ERESTARTSYS:
465 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100466 regs->ax = regs->orig_ax;
467 regs->ip -= 2;
Andi Kleen1d001df2006-09-26 10:52:26 +0200468 break;
469 case -ERESTART_RESTARTBLOCK:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100470 regs->ax = test_thread_flag(TIF_IA32) ?
Andi Kleen607a1682005-05-20 14:27:58 -0700471 __NR_ia32_restart_syscall :
472 __NR_restart_syscall;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100473 regs->ip -= 2;
Andi Kleen1d001df2006-09-26 10:52:26 +0200474 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476 }
Andi Kleen1d001df2006-09-26 10:52:26 +0200477
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800478 /*
479 * If there's no signal to deliver, we just put the saved sigmask
480 * back.
481 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700482 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
483 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
Andi Kleen1d001df2006-09-26 10:52:26 +0200484 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800488void do_notify_resume(struct pt_regs *regs, void *unused,
489 __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Tim Hockine02e68d2007-07-21 17:10:36 +0200491#ifdef CONFIG_X86_MCE
492 /* notify userspace of pending MCEs */
493 if (thread_info_flags & _TIF_MCE_NOTIFY)
494 mce_notify_user();
495#endif /* CONFIG_X86_MCE */
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 /* deal with pending signal delivery */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700498 if (thread_info_flags & _TIF_SIGPENDING)
Andi Kleen1d001df2006-09-26 10:52:26 +0200499 do_signal(regs);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100500
501 if (thread_info_flags & _TIF_HRTICK_RESCHED)
502 hrtick_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
506{
507 struct task_struct *me = current;
Andi Kleen03252912008-01-30 13:33:18 +0100508 if (show_unhandled_signals && printk_ratelimit()) {
509 printk("%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100510 me->comm,me->pid,where,frame,regs->ip,regs->sp,regs->orig_ax);
Andi Kleen03252912008-01-30 13:33:18 +0100511 print_vma_addr(" in ", regs->ip);
512 printk("\n");
513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 force_sig(SIGSEGV, me);
516}