blob: a5c9627f4db9df635a01373edbaeff215d9c52ae [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>
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -030023#include <linux/uaccess.h>
24
Harvey Harrison1a176802008-02-08 12:09:59 -080025#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/ucontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/i387.h>
28#include <asm/proto.h>
Oliver Korpillac5924b72005-05-27 12:52:55 -070029#include <asm/ia32_unistd.h>
Tim Hockine02e68d2007-07-21 17:10:36 +020030#include <asm/mce.h>
Roland McGrath4dfcbb92008-04-19 15:37:09 -070031#include <asm/syscall.h>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053032#include <asm/syscalls.h>
Harvey Harrison123a6342008-02-08 12:10:00 -080033#include "sigframe.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
36
Harvey Harrison1a176802008-02-08 12:09:59 -080037#define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
38 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
39 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
40 X86_EFLAGS_CF)
41
42#ifdef CONFIG_X86_32
43# define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
44#else
45# define FIX_EFLAGS __FIX_EFLAGS
46#endif
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048asmlinkage long
Linus Torvalds1da177e2005-04-16 15:20:36 -070049sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
50 struct pt_regs *regs)
51{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010052 return do_sigaltstack(uss, uoss, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
Hiroshi Shimamotoa2e8d3d2008-10-02 22:09:20 -070055#define COPY(x) { \
56 err |= __get_user(regs->x, &sc->x); \
57}
58
59#define COPY_SEG_STRICT(seg) { \
60 unsigned short tmp; \
61 err |= __get_user(tmp, &sc->seg); \
62 regs->seg = tmp | 3; \
63}
64
Linus Torvaldsb30f3ae2008-07-24 15:43:44 -070065/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * Do a signal return; undo the signal stack.
67 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static int
Harvey Harrison866bc132008-02-08 12:10:02 -080069restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
70 unsigned long *pax)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Hiroshi Shimamoto69e13ad2008-10-02 22:18:47 -070072 void __user *buf;
73 unsigned int tmpflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 unsigned int err = 0;
75
76 /* Always make any pending restarted system calls return -EINTR */
77 current_thread_info()->restart_block.fn = do_no_restart_syscall;
78
H. Peter Anvin742fa542008-01-30 13:30:56 +010079 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
80 COPY(dx); COPY(cx); COPY(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 COPY(r8);
82 COPY(r9);
83 COPY(r10);
84 COPY(r11);
85 COPY(r12);
86 COPY(r13);
87 COPY(r14);
88 COPY(r15);
89
Bryan Forde4e5d322005-11-05 17:25:54 +010090 /* Kernel saves and restores only the CS segment register on signals,
91 * which is the bare minimum needed to allow mixed 32/64-bit code.
92 * App's signal handler can save/restore other segments if needed. */
Hiroshi Shimamotoa2e8d3d2008-10-02 22:09:20 -070093 COPY_SEG_STRICT(cs);
Bryan Forde4e5d322005-11-05 17:25:54 +010094
Hiroshi Shimamoto69e13ad2008-10-02 22:18:47 -070095 err |= __get_user(tmpflags, &sc->flags);
96 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
97 regs->orig_ax = -1; /* disable syscall checks */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Hiroshi Shimamoto69e13ad2008-10-02 22:18:47 -070099 err |= __get_user(buf, &sc->fpstate);
100 err |= restore_i387_xstate(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Harvey Harrison866bc132008-02-08 12:10:02 -0800102 err |= __get_user(*pax, &sc->ax);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Hiroshi Shimamotoe6babb62008-09-12 17:03:31 -0700106static long do_rt_sigreturn(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 struct rt_sigframe __user *frame;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100109 unsigned long ax;
Hiroshi Shimamotoe6babb62008-09-12 17:03:31 -0700110 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Harvey Harrison2d19c452008-02-08 12:10:00 -0800112 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
113 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 goto badframe;
Harvey Harrison2d19c452008-02-08 12:10:00 -0800115 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 sigdelsetmask(&set, ~_BLOCKABLE);
119 spin_lock_irq(&current->sighand->siglock);
120 current->blocked = set;
121 recalc_sigpending();
122 spin_unlock_irq(&current->sighand->siglock);
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300123
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100124 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 goto badframe;
126
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100127 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 goto badframe;
129
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100130 return ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132badframe:
Hiroshi Shimamotoe6babb62008-09-12 17:03:31 -0700133 signal_fault(regs, frame, "rt_sigreturn");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return 0;
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300135}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Hiroshi Shimamotoe6babb62008-09-12 17:03:31 -0700137asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
138{
139 return do_rt_sigreturn(regs);
140}
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/*
143 * Set up a signal frame.
144 */
145
146static inline int
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300147setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
148 unsigned long mask, struct task_struct *me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Bryan Forde4e5d322005-11-05 17:25:54 +0100152 err |= __put_user(regs->cs, &sc->cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 err |= __put_user(0, &sc->gs);
154 err |= __put_user(0, &sc->fs);
155
H. Peter Anvin742fa542008-01-30 13:30:56 +0100156 err |= __put_user(regs->di, &sc->di);
157 err |= __put_user(regs->si, &sc->si);
158 err |= __put_user(regs->bp, &sc->bp);
159 err |= __put_user(regs->sp, &sc->sp);
160 err |= __put_user(regs->bx, &sc->bx);
161 err |= __put_user(regs->dx, &sc->dx);
162 err |= __put_user(regs->cx, &sc->cx);
163 err |= __put_user(regs->ax, &sc->ax);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 err |= __put_user(regs->r8, &sc->r8);
165 err |= __put_user(regs->r9, &sc->r9);
166 err |= __put_user(regs->r10, &sc->r10);
167 err |= __put_user(regs->r11, &sc->r11);
168 err |= __put_user(regs->r12, &sc->r12);
169 err |= __put_user(regs->r13, &sc->r13);
170 err |= __put_user(regs->r14, &sc->r14);
171 err |= __put_user(regs->r15, &sc->r15);
172 err |= __put_user(me->thread.trap_no, &sc->trapno);
173 err |= __put_user(me->thread.error_code, &sc->err);
H. Peter Anvin742fa542008-01-30 13:30:56 +0100174 err |= __put_user(regs->ip, &sc->ip);
175 err |= __put_user(regs->flags, &sc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 err |= __put_user(mask, &sc->oldmask);
177 err |= __put_user(me->thread.cr2, &sc->cr2);
178
179 return err;
180}
181
182/*
183 * Determine which stack to use..
184 */
185
186static void __user *
187get_stack(struct k_sigaction *ka, struct pt_regs *regs, unsigned long size)
188{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100189 unsigned long sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 /* Default to using normal stack - redzone*/
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100192 sp = regs->sp - 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 /* This is the X/Open sanctioned signal stack switching. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (ka->sa.sa_flags & SA_ONSTACK) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100196 if (sas_ss_flags(sp) == 0)
197 sp = current->sas_ss_sp + current->sas_ss_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700200 return (void __user *)round_down(sp - size, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700203static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
204 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 struct rt_sigframe __user *frame;
Suresh Siddhaab513702008-07-29 10:29:22 -0700207 void __user *fp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 int err = 0;
209 struct task_struct *me = current;
210
211 if (used_math()) {
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700212 fp = get_stack(ka, regs, sig_xstate_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 frame = (void __user *)round_down(
214 (unsigned long)fp - sizeof(struct rt_sigframe), 16) - 8;
215
Suresh Siddhaab513702008-07-29 10:29:22 -0700216 if (save_i387_xstate(fp) < 0)
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700217 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 } else
219 frame = get_stack(ka, regs, sizeof(struct rt_sigframe)) - 8;
220
221 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700222 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300224 if (ka->sa.sa_flags & SA_SIGINFO) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700225 if (copy_siginfo_to_user(&frame->info, info))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700226 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* Create the ucontext. */
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700230 if (cpu_has_xsave)
231 err |= __put_user(UC_FP_XSTATE, &frame->uc.uc_flags);
232 else
233 err |= __put_user(0, &frame->uc.uc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 err |= __put_user(0, &frame->uc.uc_link);
235 err |= __put_user(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100236 err |= __put_user(sas_ss_flags(regs->sp),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 &frame->uc.uc_stack.ss_flags);
238 err |= __put_user(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
239 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0], me);
240 err |= __put_user(fp, &frame->uc.uc_mcontext.fpstate);
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300241 if (sizeof(*set) == 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 __put_user(set->sig[0], &frame->uc.uc_sigmask.sig[0]);
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300243 __put_user(set->sig[1], &frame->uc.uc_sigmask.sig[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 } else
245 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
246
247 /* Set up to return from userspace. If provided, use a stub
248 already in userspace. */
249 /* x86-64 should always use SA_RESTORER. */
250 if (ka->sa.sa_flags & SA_RESTORER) {
251 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
252 } else {
253 /* could use a vstub here */
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700254 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256
257 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700258 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /* Set up registers for signal handler */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100261 regs->di = sig;
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300262 /* In case the signal handler was declared without prototypes */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100263 regs->ax = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 /* This also works for non SA_SIGINFO handlers because they expect the
266 next argument after the signal number on the stack. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100267 regs->si = (unsigned long)&frame->info;
268 regs->dx = (unsigned long)&frame->uc;
269 regs->ip = (unsigned long) ka->sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100271 regs->sp = (unsigned long)frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Bryan Forde4e5d322005-11-05 17:25:54 +0100273 /* Set up the CS register to run signal handlers in 64-bit mode,
274 even if the handler happens to be interrupting 32-bit code. */
275 regs->cs = __USER_CS;
276
Andi Kleen1d001df2006-09-26 10:52:26 +0200277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/*
281 * OK, we're invoking a handler
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300282 */
Hiroshi Shimamotob94fd692008-09-24 19:12:54 -0700283static int signr_convert(int sig)
284{
285 return sig;
286}
287
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700288#ifdef CONFIG_IA32_EMULATION
289#define is_ia32 test_thread_flag(TIF_IA32)
290#else
291#define is_ia32 0
292#endif
293
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700294static int
295setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
296 sigset_t *set, struct pt_regs *regs)
297{
Hiroshi Shimamotob94fd692008-09-24 19:12:54 -0700298 int usig = signr_convert(sig);
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700299 int ret;
300
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700301 /* Set up the stack frame */
302 if (is_ia32) {
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700303 if (ka->sa.sa_flags & SA_SIGINFO)
Hiroshi Shimamotob94fd692008-09-24 19:12:54 -0700304 ret = ia32_setup_rt_frame(usig, ka, info, set, regs);
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700305 else
Hiroshi Shimamotob94fd692008-09-24 19:12:54 -0700306 ret = ia32_setup_frame(usig, ka, set, regs);
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700307 } else
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700308 ret = __setup_rt_frame(sig, ka, info, set, regs);
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700309
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700310 if (ret) {
311 force_sigsegv(sig, current);
312 return -EFAULT;
313 }
314
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700315 return ret;
316}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Roland McGrath0928d6e2005-06-23 00:08:37 -0700318static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800320 sigset_t *oldset, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Roland McGrath0928d6e2005-06-23 00:08:37 -0700322 int ret;
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /* Are we from a system call? */
Roland McGrath4dfcbb92008-04-19 15:37:09 -0700325 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 /* If so, check system call restarting.. */
Roland McGrath4dfcbb92008-04-19 15:37:09 -0700327 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800328 case -ERESTART_RESTARTBLOCK:
329 case -ERESTARTNOHAND:
330 regs->ax = -EINTR;
331 break;
332
333 case -ERESTARTSYS:
334 if (!(ka->sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100335 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800337 }
338 /* fallthrough */
339 case -ERESTARTNOINTR:
340 regs->ax = regs->orig_ax;
341 regs->ip -= 2;
342 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344 }
345
Andi Kleend61915d2005-04-16 15:25:00 -0700346 /*
Roland McGrathe1f28772008-01-30 13:30:50 +0100347 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
348 * flag so that register information in the sigcontext is correct.
Andi Kleend61915d2005-04-16 15:25:00 -0700349 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100350 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
Roland McGrathe1f28772008-01-30 13:30:50 +0100351 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100352 regs->flags &= ~X86_EFLAGS_TF;
Andi Kleend61915d2005-04-16 15:25:00 -0700353
Roland McGrath0928d6e2005-06-23 00:08:37 -0700354 ret = setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Hiroshi Shimamoto764e8d12008-09-09 17:22:12 -0700356 if (ret)
357 return ret;
Roland McGrath55928e32008-04-19 14:27:56 -0700358
Hiroshi Shimamoto86d32372008-09-23 17:22:32 -0700359#ifdef CONFIG_X86_64
Hiroshi Shimamoto764e8d12008-09-09 17:22:12 -0700360 /*
361 * This has nothing to do with segment registers,
362 * despite the name. This magic affects uaccess.h
363 * macros' behavior. Reset it to the normal setting.
364 */
365 set_fs(USER_DS);
Hiroshi Shimamoto86d32372008-09-23 17:22:32 -0700366#endif
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700367
Hiroshi Shimamoto764e8d12008-09-09 17:22:12 -0700368 /*
369 * Clear the direction flag as per the ABI for function entry.
370 */
371 regs->flags &= ~X86_EFLAGS_DF;
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700372
Hiroshi Shimamoto764e8d12008-09-09 17:22:12 -0700373 /*
374 * Clear TF when entering the signal handler, but
375 * notify any tracer that was single-stepping it.
376 * The tracer may want to single-step inside the
377 * handler too.
378 */
379 regs->flags &= ~X86_EFLAGS_TF;
Roland McGrath36a03302008-03-14 17:46:38 -0700380
Hiroshi Shimamoto764e8d12008-09-09 17:22:12 -0700381 spin_lock_irq(&current->sighand->siglock);
382 sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
383 if (!(ka->sa.sa_flags & SA_NODEFER))
384 sigaddset(&current->blocked, sig);
385 recalc_sigpending();
386 spin_unlock_irq(&current->sighand->siglock);
Roland McGrath0928d6e2005-06-23 00:08:37 -0700387
Hiroshi Shimamoto764e8d12008-09-09 17:22:12 -0700388 tracehook_signal_handler(sig, info, ka, regs,
389 test_thread_flag(TIF_SINGLESTEP));
390
391 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700394#define NR_restart_syscall \
395 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396/*
397 * Note that 'init' is a special process: it doesn't get signals it doesn't
398 * want to handle. Thus you cannot kill init even with a SIGKILL even by
399 * mistake.
400 */
Andi Kleen1d001df2006-09-26 10:52:26 +0200401static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 struct k_sigaction ka;
404 siginfo_t info;
405 int signr;
Andi Kleen1d001df2006-09-26 10:52:26 +0200406 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /*
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800409 * We want the common case to go fast, which is why we may in certain
410 * cases get here from kernel mode. Just return without doing anything
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 * if so.
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800412 * X86_32: vm86 regs switched out by assembly code before reaching
413 * here, so testing against kernel CS suffices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700415 if (!user_mode(regs))
Andi Kleen1d001df2006-09-26 10:52:26 +0200416 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700418 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
Andi Kleen1d001df2006-09-26 10:52:26 +0200419 oldset = &current->saved_sigmask;
420 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 oldset = &current->blocked;
422
423 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
424 if (signr > 0) {
Hiroshi Shimamoto5fd93332008-09-23 17:19:44 -0700425 /*
426 * Re-enable any watchpoints before delivering the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * signal to user space. The processor register will
428 * have been cleared if the watchpoint triggered
429 * inside the kernel.
430 */
431 if (current->thread.debugreg7)
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700432 set_debugreg(current->thread.debugreg7, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Hiroshi Shimamoto5fd93332008-09-23 17:19:44 -0700434 /* Whee! Actually deliver the signal. */
Andi Kleen1d001df2006-09-26 10:52:26 +0200435 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700436 /*
437 * A signal was successfully delivered; the saved
Andi Kleen1d001df2006-09-26 10:52:26 +0200438 * sigmask will have been stored in the signal frame,
439 * and will be restored by sigreturn, so we can simply
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700440 * clear the TS_RESTORE_SIGMASK flag.
441 */
442 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
Andi Kleen1d001df2006-09-26 10:52:26 +0200443 }
444 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /* Did we come from a system call? */
Roland McGrath4dfcbb92008-04-19 15:37:09 -0700448 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /* Restart the system call - no handlers present */
Roland McGrath4dfcbb92008-04-19 15:37:09 -0700450 switch (syscall_get_error(current, regs)) {
Andi Kleen1d001df2006-09-26 10:52:26 +0200451 case -ERESTARTNOHAND:
452 case -ERESTARTSYS:
453 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100454 regs->ax = regs->orig_ax;
455 regs->ip -= 2;
Andi Kleen1d001df2006-09-26 10:52:26 +0200456 break;
Hiroshi Shimamoto5fd93332008-09-23 17:19:44 -0700457
Andi Kleen1d001df2006-09-26 10:52:26 +0200458 case -ERESTART_RESTARTBLOCK:
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700459 regs->ax = NR_restart_syscall;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100460 regs->ip -= 2;
Andi Kleen1d001df2006-09-26 10:52:26 +0200461 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463 }
Andi Kleen1d001df2006-09-26 10:52:26 +0200464
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800465 /*
466 * If there's no signal to deliver, we just put the saved sigmask
467 * back.
468 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700469 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
470 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
Andi Kleen1d001df2006-09-26 10:52:26 +0200471 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700475/*
476 * notification of userspace execution resumption
477 * - triggered by the TIF_WORK_MASK flags
478 */
479void
480do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700482#if defined(CONFIG_X86_64) && defined(CONFIG_X86_MCE)
Tim Hockine02e68d2007-07-21 17:10:36 +0200483 /* notify userspace of pending MCEs */
484 if (thread_info_flags & _TIF_MCE_NOTIFY)
485 mce_notify_user();
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700486#endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
Tim Hockine02e68d2007-07-21 17:10:36 +0200487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 /* deal with pending signal delivery */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700489 if (thread_info_flags & _TIF_SIGPENDING)
Andi Kleen1d001df2006-09-26 10:52:26 +0200490 do_signal(regs);
Roland McGrath59e52132008-04-19 19:10:57 -0700491
492 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
493 clear_thread_flag(TIF_NOTIFY_RESUME);
494 tracehook_notify_resume(regs);
495 }
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700496
497#ifdef CONFIG_X86_32
498 clear_thread_flag(TIF_IRET);
499#endif /* CONFIG_X86_32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
502void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300503{
504 struct task_struct *me = current;
Hiroshi Shimamotob2994ef2008-09-09 17:18:50 -0700505
Andi Kleen03252912008-01-30 13:33:18 +0100506 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotob2994ef2008-09-09 17:18:50 -0700507 printk(KERN_INFO
508 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
509 me->comm, me->pid, where, frame,
510 regs->ip, regs->sp, regs->orig_ax);
Andi Kleen03252912008-01-30 13:33:18 +0100511 print_vma_addr(" in ", regs->ip);
Hiroshi Shimamotob2994ef2008-09-09 17:18:50 -0700512 printk(KERN_CONT "\n");
Andi Kleen03252912008-01-30 13:33:18 +0100513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Gustavo F. Padovancaa007d2008-07-29 02:48:54 -0300515 force_sig(SIGSEGV, me);
516}