blob: 3beb2db88c5affce6605e42197d7181dde4236a8 [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>
Roland McGrath36a03302008-03-14 17:46:38 -070018#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/unistd.h>
20#include <linux/stddef.h>
21#include <linux/personality.h>
22#include <linux/compiler.h>
Harvey Harrison1a176802008-02-08 12:09:59 -080023#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/ucontext.h>
25#include <asm/uaccess.h>
26#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
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
Linus Torvaldsb30f3ae2008-07-24 15:43:44 -070057/*
58 * Signal frame handlers.
59 */
60
61static inline int save_i387(struct _fpstate __user *buf)
62{
63 struct task_struct *tsk = current;
64 int err = 0;
65
66 BUILD_BUG_ON(sizeof(struct user_i387_struct) !=
67 sizeof(tsk->thread.xstate->fxsave));
68
69 if ((unsigned long)buf % 16)
70 printk("save_i387: bad fpstate %p\n", buf);
71
72 if (!used_math())
73 return 0;
74 clear_used_math(); /* trigger finit */
75 if (task_thread_info(tsk)->status & TS_USEDFPU) {
76 err = save_i387_checking((struct i387_fxsave_struct __user *)
77 buf);
78 if (err)
79 return err;
80 task_thread_info(tsk)->status &= ~TS_USEDFPU;
81 stts();
82 } else {
83 if (__copy_to_user(buf, &tsk->thread.xstate->fxsave,
84 sizeof(struct i387_fxsave_struct)))
85 return -1;
86 }
87 return 1;
88}
89
90/*
91 * This restores directly out of user space. Exceptions are handled.
92 */
93static inline int restore_i387(struct _fpstate __user *buf)
94{
95 struct task_struct *tsk = current;
96 int err;
97
98 if (!used_math()) {
99 err = init_fpu(tsk);
100 if (err)
101 return err;
102 }
103
104 if (!(task_thread_info(current)->status & TS_USEDFPU)) {
105 clts();
106 task_thread_info(current)->status |= TS_USEDFPU;
107 }
108 return restore_fpu_checking((__force struct i387_fxsave_struct *)buf);
109}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111/*
112 * Do a signal return; undo the signal stack.
113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static int
Harvey Harrison866bc132008-02-08 12:10:02 -0800115restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
116 unsigned long *pax)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 unsigned int err = 0;
119
120 /* Always make any pending restarted system calls return -EINTR */
121 current_thread_info()->restart_block.fn = do_no_restart_syscall;
122
123#define COPY(x) err |= __get_user(regs->x, &sc->x)
124
H. Peter Anvin742fa542008-01-30 13:30:56 +0100125 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
126 COPY(dx); COPY(cx); COPY(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 COPY(r8);
128 COPY(r9);
129 COPY(r10);
130 COPY(r11);
131 COPY(r12);
132 COPY(r13);
133 COPY(r14);
134 COPY(r15);
135
Bryan Forde4e5d322005-11-05 17:25:54 +0100136 /* Kernel saves and restores only the CS segment register on signals,
137 * which is the bare minimum needed to allow mixed 32/64-bit code.
138 * App's signal handler can save/restore other segments if needed. */
139 {
140 unsigned cs;
141 err |= __get_user(cs, &sc->cs);
142 regs->cs = cs | 3; /* Force into user mode */
143 }
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 {
146 unsigned int tmpflags;
H. Peter Anvin742fa542008-01-30 13:30:56 +0100147 err |= __get_user(tmpflags, &sc->flags);
Harvey Harrison1a176802008-02-08 12:09:59 -0800148 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100149 regs->orig_ax = -1; /* disable syscall checks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151
152 {
153 struct _fpstate __user * buf;
154 err |= __get_user(buf, &sc->fpstate);
155
156 if (buf) {
157 if (!access_ok(VERIFY_READ, buf, sizeof(*buf)))
158 goto badframe;
159 err |= restore_i387(buf);
160 } else {
161 struct task_struct *me = current;
162 if (used_math()) {
163 clear_fpu(me);
164 clear_used_math();
165 }
166 }
167 }
168
Harvey Harrison866bc132008-02-08 12:10:02 -0800169 err |= __get_user(*pax, &sc->ax);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return err;
171
172badframe:
173 return 1;
174}
175
176asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
177{
178 struct rt_sigframe __user *frame;
179 sigset_t set;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100180 unsigned long ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Harvey Harrison2d19c452008-02-08 12:10:00 -0800182 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
183 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 goto badframe;
Harvey Harrison2d19c452008-02-08 12:10:00 -0800185 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 sigdelsetmask(&set, ~_BLOCKABLE);
189 spin_lock_irq(&current->sighand->siglock);
190 current->blocked = set;
191 recalc_sigpending();
192 spin_unlock_irq(&current->sighand->siglock);
193
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100194 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 goto badframe;
196
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100197 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 goto badframe;
199
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100200 return ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202badframe:
203 signal_fault(regs,frame,"sigreturn");
204 return 0;
205}
206
207/*
208 * Set up a signal frame.
209 */
210
211static inline int
212setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, unsigned long mask, struct task_struct *me)
213{
214 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Bryan Forde4e5d322005-11-05 17:25:54 +0100216 err |= __put_user(regs->cs, &sc->cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 err |= __put_user(0, &sc->gs);
218 err |= __put_user(0, &sc->fs);
219
H. Peter Anvin742fa542008-01-30 13:30:56 +0100220 err |= __put_user(regs->di, &sc->di);
221 err |= __put_user(regs->si, &sc->si);
222 err |= __put_user(regs->bp, &sc->bp);
223 err |= __put_user(regs->sp, &sc->sp);
224 err |= __put_user(regs->bx, &sc->bx);
225 err |= __put_user(regs->dx, &sc->dx);
226 err |= __put_user(regs->cx, &sc->cx);
227 err |= __put_user(regs->ax, &sc->ax);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 err |= __put_user(regs->r8, &sc->r8);
229 err |= __put_user(regs->r9, &sc->r9);
230 err |= __put_user(regs->r10, &sc->r10);
231 err |= __put_user(regs->r11, &sc->r11);
232 err |= __put_user(regs->r12, &sc->r12);
233 err |= __put_user(regs->r13, &sc->r13);
234 err |= __put_user(regs->r14, &sc->r14);
235 err |= __put_user(regs->r15, &sc->r15);
236 err |= __put_user(me->thread.trap_no, &sc->trapno);
237 err |= __put_user(me->thread.error_code, &sc->err);
H. Peter Anvin742fa542008-01-30 13:30:56 +0100238 err |= __put_user(regs->ip, &sc->ip);
239 err |= __put_user(regs->flags, &sc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 err |= __put_user(mask, &sc->oldmask);
241 err |= __put_user(me->thread.cr2, &sc->cr2);
242
243 return err;
244}
245
246/*
247 * Determine which stack to use..
248 */
249
250static void __user *
251get_stack(struct k_sigaction *ka, struct pt_regs *regs, unsigned long size)
252{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100253 unsigned long sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 /* Default to using normal stack - redzone*/
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100256 sp = regs->sp - 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /* This is the X/Open sanctioned signal stack switching. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (ka->sa.sa_flags & SA_ONSTACK) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100260 if (sas_ss_flags(sp) == 0)
261 sp = current->sas_ss_sp + current->sas_ss_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100264 return (void __user *)round_down(sp - size, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
Roland McGrath0928d6e2005-06-23 00:08:37 -0700267static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 sigset_t *set, struct pt_regs * regs)
269{
270 struct rt_sigframe __user *frame;
271 struct _fpstate __user *fp = NULL;
272 int err = 0;
273 struct task_struct *me = current;
274
275 if (used_math()) {
276 fp = get_stack(ka, regs, sizeof(struct _fpstate));
277 frame = (void __user *)round_down(
278 (unsigned long)fp - sizeof(struct rt_sigframe), 16) - 8;
279
280 if (!access_ok(VERIFY_WRITE, fp, sizeof(struct _fpstate)))
281 goto give_sigsegv;
282
283 if (save_i387(fp) < 0)
284 err |= -1;
285 } else
286 frame = get_stack(ka, regs, sizeof(struct rt_sigframe)) - 8;
287
288 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
289 goto give_sigsegv;
290
291 if (ka->sa.sa_flags & SA_SIGINFO) {
292 err |= copy_siginfo_to_user(&frame->info, info);
293 if (err)
294 goto give_sigsegv;
295 }
296
297 /* Create the ucontext. */
298 err |= __put_user(0, &frame->uc.uc_flags);
299 err |= __put_user(0, &frame->uc.uc_link);
300 err |= __put_user(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100301 err |= __put_user(sas_ss_flags(regs->sp),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 &frame->uc.uc_stack.ss_flags);
303 err |= __put_user(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
304 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0], me);
305 err |= __put_user(fp, &frame->uc.uc_mcontext.fpstate);
306 if (sizeof(*set) == 16) {
307 __put_user(set->sig[0], &frame->uc.uc_sigmask.sig[0]);
308 __put_user(set->sig[1], &frame->uc.uc_sigmask.sig[1]);
309 } else
310 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
311
312 /* Set up to return from userspace. If provided, use a stub
313 already in userspace. */
314 /* x86-64 should always use SA_RESTORER. */
315 if (ka->sa.sa_flags & SA_RESTORER) {
316 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
317 } else {
318 /* could use a vstub here */
319 goto give_sigsegv;
320 }
321
322 if (err)
323 goto give_sigsegv;
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /* Set up registers for signal handler */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100326 regs->di = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /* In case the signal handler was declared without prototypes */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100328 regs->ax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 /* This also works for non SA_SIGINFO handlers because they expect the
331 next argument after the signal number on the stack. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100332 regs->si = (unsigned long)&frame->info;
333 regs->dx = (unsigned long)&frame->uc;
334 regs->ip = (unsigned long) ka->sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100336 regs->sp = (unsigned long)frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Bryan Forde4e5d322005-11-05 17:25:54 +0100338 /* Set up the CS register to run signal handlers in 64-bit mode,
339 even if the handler happens to be interrupting 32-bit code. */
340 regs->cs = __USER_CS;
341
Andi Kleen1d001df2006-09-26 10:52:26 +0200342 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344give_sigsegv:
345 force_sigsegv(sig, current);
Andi Kleen1d001df2006-09-26 10:52:26 +0200346 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349/*
Roland McGrath40f09332008-02-28 19:57:07 -0800350 * Return -1L or the syscall number that @regs is executing.
351 */
352static long current_syscall(struct pt_regs *regs)
353{
354 /*
355 * We always sign-extend a -1 value being set here,
356 * so this is always either -1L or a syscall number.
357 */
358 return regs->orig_ax;
359}
360
361/*
362 * Return a value that is -EFOO if the system call in @regs->orig_ax
363 * returned an error. This only works for @regs from @current.
364 */
365static long current_syscall_ret(struct pt_regs *regs)
366{
367#ifdef CONFIG_IA32_EMULATION
368 if (test_thread_flag(TIF_IA32))
369 /*
370 * Sign-extend the value so (int)-EFOO becomes (long)-EFOO
371 * and will match correctly in comparisons.
372 */
373 return (int) regs->ax;
374#endif
375 return regs->ax;
376}
377
378/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 * OK, we're invoking a handler
380 */
381
Roland McGrath0928d6e2005-06-23 00:08:37 -0700382static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800384 sigset_t *oldset, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Roland McGrath0928d6e2005-06-23 00:08:37 -0700386 int ret;
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /* Are we from a system call? */
Roland McGrath40f09332008-02-28 19:57:07 -0800389 if (current_syscall(regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /* If so, check system call restarting.. */
Roland McGrath40f09332008-02-28 19:57:07 -0800391 switch (current_syscall_ret(regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800392 case -ERESTART_RESTARTBLOCK:
393 case -ERESTARTNOHAND:
394 regs->ax = -EINTR;
395 break;
396
397 case -ERESTARTSYS:
398 if (!(ka->sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100399 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800401 }
402 /* fallthrough */
403 case -ERESTARTNOINTR:
404 regs->ax = regs->orig_ax;
405 regs->ip -= 2;
406 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408 }
409
Andi Kleend61915d2005-04-16 15:25:00 -0700410 /*
Roland McGrathe1f28772008-01-30 13:30:50 +0100411 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
412 * flag so that register information in the sigcontext is correct.
Andi Kleend61915d2005-04-16 15:25:00 -0700413 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100414 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
Roland McGrathe1f28772008-01-30 13:30:50 +0100415 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100416 regs->flags &= ~X86_EFLAGS_TF;
Andi Kleend61915d2005-04-16 15:25:00 -0700417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418#ifdef CONFIG_IA32_EMULATION
419 if (test_thread_flag(TIF_IA32)) {
420 if (ka->sa.sa_flags & SA_SIGINFO)
Roland McGrath0928d6e2005-06-23 00:08:37 -0700421 ret = ia32_setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 else
Roland McGrath0928d6e2005-06-23 00:08:37 -0700423 ret = ia32_setup_frame(sig, ka, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 } else
425#endif
Roland McGrath0928d6e2005-06-23 00:08:37 -0700426 ret = setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Andi Kleen1d001df2006-09-26 10:52:26 +0200428 if (ret == 0) {
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700429 /*
Roland McGrath55928e32008-04-19 14:27:56 -0700430 * This has nothing to do with segment registers,
431 * despite the name. This magic affects uaccess.h
432 * macros' behavior. Reset it to the normal setting.
433 */
434 set_fs(USER_DS);
435
436 /*
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700437 * Clear the direction flag as per the ABI for function entry.
438 */
439 regs->flags &= ~X86_EFLAGS_DF;
440
441 /*
442 * Clear TF when entering the signal handler, but
443 * notify any tracer that was single-stepping it.
444 * The tracer may want to single-step inside the
445 * handler too.
446 */
447 regs->flags &= ~X86_EFLAGS_TF;
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 spin_lock_irq(&current->sighand->siglock);
450 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
Steven Rostedt69be8f12005-08-29 11:44:09 -0400451 if (!(ka->sa.sa_flags & SA_NODEFER))
452 sigaddset(&current->blocked,sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 recalc_sigpending();
454 spin_unlock_irq(&current->sighand->siglock);
Roland McGrath36a03302008-03-14 17:46:38 -0700455
456 tracehook_signal_handler(sig, info, ka, regs,
457 test_thread_flag(TIF_SINGLESTEP));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
Roland McGrath0928d6e2005-06-23 00:08:37 -0700459
460 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
463/*
464 * Note that 'init' is a special process: it doesn't get signals it doesn't
465 * want to handle. Thus you cannot kill init even with a SIGKILL even by
466 * mistake.
467 */
Andi Kleen1d001df2006-09-26 10:52:26 +0200468static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 struct k_sigaction ka;
471 siginfo_t info;
472 int signr;
Andi Kleen1d001df2006-09-26 10:52:26 +0200473 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 /*
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800476 * We want the common case to go fast, which is why we may in certain
477 * cases get here from kernel mode. Just return without doing anything
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 * if so.
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800479 * X86_32: vm86 regs switched out by assembly code before reaching
480 * here, so testing against kernel CS suffices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700482 if (!user_mode(regs))
Andi Kleen1d001df2006-09-26 10:52:26 +0200483 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700485 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
Andi Kleen1d001df2006-09-26 10:52:26 +0200486 oldset = &current->saved_sigmask;
487 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 oldset = &current->blocked;
489
490 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
491 if (signr > 0) {
Simon Arlott676b1852007-10-20 01:25:36 +0200492 /* Re-enable any watchpoints before delivering the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 * signal to user space. The processor register will
494 * have been cleared if the watchpoint triggered
495 * inside the kernel.
496 */
497 if (current->thread.debugreg7)
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700498 set_debugreg(current->thread.debugreg7, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 /* Whee! Actually deliver the signal. */
Andi Kleen1d001df2006-09-26 10:52:26 +0200501 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700502 /*
503 * A signal was successfully delivered; the saved
Andi Kleen1d001df2006-09-26 10:52:26 +0200504 * sigmask will have been stored in the signal frame,
505 * and will be restored by sigreturn, so we can simply
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700506 * clear the TS_RESTORE_SIGMASK flag.
507 */
508 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
Andi Kleen1d001df2006-09-26 10:52:26 +0200509 }
510 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /* Did we come from a system call? */
Roland McGrath40f09332008-02-28 19:57:07 -0800514 if (current_syscall(regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /* Restart the system call - no handlers present */
Roland McGrath40f09332008-02-28 19:57:07 -0800516 switch (current_syscall_ret(regs)) {
Andi Kleen1d001df2006-09-26 10:52:26 +0200517 case -ERESTARTNOHAND:
518 case -ERESTARTSYS:
519 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100520 regs->ax = regs->orig_ax;
521 regs->ip -= 2;
Andi Kleen1d001df2006-09-26 10:52:26 +0200522 break;
523 case -ERESTART_RESTARTBLOCK:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100524 regs->ax = test_thread_flag(TIF_IA32) ?
Andi Kleen607a1682005-05-20 14:27:58 -0700525 __NR_ia32_restart_syscall :
526 __NR_restart_syscall;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100527 regs->ip -= 2;
Andi Kleen1d001df2006-09-26 10:52:26 +0200528 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530 }
Andi Kleen1d001df2006-09-26 10:52:26 +0200531
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800532 /*
533 * If there's no signal to deliver, we just put the saved sigmask
534 * back.
535 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700536 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
537 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
Andi Kleen1d001df2006-09-26 10:52:26 +0200538 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800542void do_notify_resume(struct pt_regs *regs, void *unused,
543 __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Tim Hockine02e68d2007-07-21 17:10:36 +0200545#ifdef CONFIG_X86_MCE
546 /* notify userspace of pending MCEs */
547 if (thread_info_flags & _TIF_MCE_NOTIFY)
548 mce_notify_user();
549#endif /* CONFIG_X86_MCE */
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* deal with pending signal delivery */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700552 if (thread_info_flags & _TIF_SIGPENDING)
Andi Kleen1d001df2006-09-26 10:52:26 +0200553 do_signal(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
556void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
557{
558 struct task_struct *me = current;
Andi Kleen03252912008-01-30 13:33:18 +0100559 if (show_unhandled_signals && printk_ratelimit()) {
560 printk("%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100561 me->comm,me->pid,where,frame,regs->ip,regs->sp,regs->orig_ax);
Andi Kleen03252912008-01-30 13:33:18 +0100562 print_vma_addr(" in ", regs->ip);
563 printk("\n");
564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 force_sig(SIGSEGV, me);
567}