blob: 82d37c77b0fb9c69bf6cb2f4693e22f38d250272 [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
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -08003 * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
6 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -08007 * 2000-2002 x86-64 support by Andi Kleen
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Ingo Molnar7e907f42008-03-06 10:33:08 +010010#include <linux/sched.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010011#include <linux/mm.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080012#include <linux/smp.h>
13#include <linux/kernel.h>
14#include <linux/signal.h>
15#include <linux/errno.h>
16#include <linux/wait.h>
17#include <linux/ptrace.h>
18#include <linux/tracehook.h>
19#include <linux/unistd.h>
20#include <linux/stddef.h>
21#include <linux/personality.h>
22#include <linux/uaccess.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/processor.h>
25#include <asm/ucontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/i387.h>
Roland McGrath6c3652e2008-01-30 13:30:42 +010027#include <asm/vdso.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080028
29#ifdef CONFIG_X86_64
30#include <asm/proto.h>
31#include <asm/ia32_unistd.h>
32#include <asm/mce.h>
33#endif /* CONFIG_X86_64 */
34
Hiroshi Shimamotobb579252008-09-05 16:26:55 -070035#include <asm/syscall.h>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053036#include <asm/syscalls.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010037
Hiroshi Shimamoto41af86f2008-12-17 18:50:32 -080038#include <asm/sigframe.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
41
Harvey Harrison1a176802008-02-08 12:09:59 -080042#define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
43 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
44 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
45 X86_EFLAGS_CF)
46
47#ifdef CONFIG_X86_32
48# define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
49#else
50# define FIX_EFLAGS __FIX_EFLAGS
51#endif
52
Tejun Heod9a89a22009-02-09 22:17:40 +090053#define COPY(x) do { \
54 get_user_ex(regs->x, &sc->x); \
55} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080056
Tejun Heod9a89a22009-02-09 22:17:40 +090057#define GET_SEG(seg) ({ \
58 unsigned short tmp; \
59 get_user_ex(tmp, &sc->seg); \
60 tmp; \
61})
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080062
Tejun Heod9a89a22009-02-09 22:17:40 +090063#define COPY_SEG(seg) do { \
64 regs->seg = GET_SEG(seg); \
65} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080066
Tejun Heod9a89a22009-02-09 22:17:40 +090067#define COPY_SEG_CPL3(seg) do { \
68 regs->seg = GET_SEG(seg) | 3; \
69} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080070
71static int
72restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
73 unsigned long *pax)
74{
75 void __user *buf;
76 unsigned int tmpflags;
77 unsigned int err = 0;
78
79 /* Always make any pending restarted system calls return -EINTR */
80 current_thread_info()->restart_block.fn = do_no_restart_syscall;
81
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080082 get_user_try {
83
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080084#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +090085 set_user_gs(regs, GET_SEG(gs));
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080086 COPY_SEG(fs);
87 COPY_SEG(es);
88 COPY_SEG(ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080089#endif /* CONFIG_X86_32 */
90
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080091 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
92 COPY(dx); COPY(cx); COPY(ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080093
94#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080095 COPY(r8);
96 COPY(r9);
97 COPY(r10);
98 COPY(r11);
99 COPY(r12);
100 COPY(r13);
101 COPY(r14);
102 COPY(r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800103#endif /* CONFIG_X86_64 */
104
105#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800106 COPY_SEG_CPL3(cs);
107 COPY_SEG_CPL3(ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800108#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800109 /* Kernel saves and restores only the CS segment register on signals,
110 * which is the bare minimum needed to allow mixed 32/64-bit code.
111 * App's signal handler can save/restore other segments if needed. */
112 COPY_SEG_CPL3(cs);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800113#endif /* CONFIG_X86_32 */
114
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800115 get_user_ex(tmpflags, &sc->flags);
116 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
117 regs->orig_ax = -1; /* disable syscall checks */
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800118
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800119 get_user_ex(buf, &sc->fpstate);
120 err |= restore_i387_xstate(buf);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800121
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800122 get_user_ex(*pax, &sc->ax);
123 } get_user_catch(err);
124
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800125 return err;
126}
127
128static int
129setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
130 struct pt_regs *regs, unsigned long mask)
131{
132 int err = 0;
133
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800134 put_user_try {
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800135
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800136#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900137 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800138 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
139 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
140 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800141#endif /* CONFIG_X86_32 */
142
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800143 put_user_ex(regs->di, &sc->di);
144 put_user_ex(regs->si, &sc->si);
145 put_user_ex(regs->bp, &sc->bp);
146 put_user_ex(regs->sp, &sc->sp);
147 put_user_ex(regs->bx, &sc->bx);
148 put_user_ex(regs->dx, &sc->dx);
149 put_user_ex(regs->cx, &sc->cx);
150 put_user_ex(regs->ax, &sc->ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800151#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800152 put_user_ex(regs->r8, &sc->r8);
153 put_user_ex(regs->r9, &sc->r9);
154 put_user_ex(regs->r10, &sc->r10);
155 put_user_ex(regs->r11, &sc->r11);
156 put_user_ex(regs->r12, &sc->r12);
157 put_user_ex(regs->r13, &sc->r13);
158 put_user_ex(regs->r14, &sc->r14);
159 put_user_ex(regs->r15, &sc->r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800160#endif /* CONFIG_X86_64 */
161
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800162 put_user_ex(current->thread.trap_no, &sc->trapno);
163 put_user_ex(current->thread.error_code, &sc->err);
164 put_user_ex(regs->ip, &sc->ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800165#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800166 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
167 put_user_ex(regs->flags, &sc->flags);
168 put_user_ex(regs->sp, &sc->sp_at_signal);
169 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800170#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800171 put_user_ex(regs->flags, &sc->flags);
172 put_user_ex(regs->cs, &sc->cs);
173 put_user_ex(0, &sc->gs);
174 put_user_ex(0, &sc->fs);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800175#endif /* CONFIG_X86_32 */
176
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800177 put_user_ex(fpstate, &sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800178
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800179 /* non-iBCS2 extensions.. */
180 put_user_ex(mask, &sc->oldmask);
181 put_user_ex(current->thread.cr2, &sc->cr2);
182 } put_user_catch(err);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800183
184 return err;
185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * Set up a signal frame.
189 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800190#ifdef CONFIG_X86_32
191static const struct {
192 u16 poplmovl;
193 u32 val;
194 u16 int80;
195} __attribute__((packed)) retcode = {
196 0xb858, /* popl %eax; movl $..., %eax */
197 __NR_sigreturn,
198 0x80cd, /* int $0x80 */
199};
200
201static const struct {
202 u8 movl;
203 u32 val;
204 u16 int80;
205 u8 pad;
206} __attribute__((packed)) rt_retcode = {
207 0xb8, /* movl $..., %eax */
208 __NR_rt_sigreturn,
209 0x80cd, /* int $0x80 */
210 0
211};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213/*
214 * Determine which stack to use..
215 */
216static inline void __user *
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700217get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
Hiroshi Shimamoto144b0712009-02-27 10:27:04 -0800218 void __user **fpstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100220 unsigned long sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 /* Default to using normal stack */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100223 sp = regs->sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Roland McGrath83bd0102008-01-30 13:30:06 +0100225 /*
226 * If we are on the alternate signal stack and would overflow it, don't.
227 * Return an always-bogus address instead so we will die with SIGSEGV.
228 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100229 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
Roland McGrath83bd0102008-01-30 13:30:06 +0100230 return (void __user *) -1L;
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /* This is the X/Open sanctioned signal stack switching. */
233 if (ka->sa.sa_flags & SA_ONSTACK) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100234 if (sas_ss_flags(sp) == 0)
235 sp = current->sas_ss_sp + current->sas_ss_size;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100236 } else {
237 /* This is the legacy signal stack switching. */
238 if ((regs->ss & 0xffff) != __USER_DS &&
239 !(ka->sa.sa_flags & SA_RESTORER) &&
240 ka->sa.sa_restorer)
241 sp = (unsigned long) ka->sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700244 if (used_math()) {
245 sp = sp - sig_xstate_size;
Hiroshi Shimamoto144b0712009-02-27 10:27:04 -0800246 *fpstate = (void __user *) sp;
Hiroshi Shimamoto4b336692008-11-05 18:30:25 -0800247 if (save_i387_xstate(*fpstate) < 0)
248 return (void __user *)-1L;
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700249 }
250
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100251 sp -= frame_size;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100252 /*
253 * Align the stack pointer according to the i386 ABI,
254 * i.e. so that on function entry ((sp + 4) & 15) == 0.
255 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100256 sp = ((sp + 4) & -16ul) - 4;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100257
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100258 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Ingo Molnar7e907f42008-03-06 10:33:08 +0100261static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700262__setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
263 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100266 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700268 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700270 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700273 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700275 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700276 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700278 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700279 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700282 if (__copy_to_user(&frame->extramask, &set->sig[1],
283 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700284 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700287 if (current->mm->context.vdso)
Roland McGrath6c3652e2008-01-30 13:30:42 +0100288 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100289 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100290 restorer = &frame->retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (ka->sa.sa_flags & SA_RESTORER)
292 restorer = ka->sa.sa_restorer;
293
294 /* Set up to return from userspace. */
295 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100298 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 *
300 * WE DO NOT USE IT ANY MORE! It's only left here for historical
301 * reasons and because gdb uses it as a signature to notice
302 * signal handler stack frames.
303 */
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -0800304 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700307 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100310 regs->sp = (unsigned long)frame;
311 regs->ip = (unsigned long)ka->sa.sa_handler;
312 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800313 regs->dx = 0;
314 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100316 regs->ds = __USER_DS;
317 regs->es = __USER_DS;
318 regs->ss = __USER_DS;
319 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
David Howells283828f2006-01-18 17:44:00 -0800321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700324static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
325 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100328 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700330 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700332 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700335 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800337 put_user_try {
338 put_user_ex(sig, &frame->sig);
339 put_user_ex(&frame->info, &frame->pinfo);
340 put_user_ex(&frame->uc, &frame->puc);
341 err |= copy_siginfo_to_user(&frame->info, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800343 /* Create the ucontext. */
344 if (cpu_has_xsave)
345 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
346 else
347 put_user_ex(0, &frame->uc.uc_flags);
348 put_user_ex(0, &frame->uc.uc_link);
349 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
350 put_user_ex(sas_ss_flags(regs->sp),
351 &frame->uc.uc_stack.ss_flags);
352 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
353 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
354 regs, set->sig[0]);
355 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800357 /* Set up to return from userspace. */
358 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
359 if (ka->sa.sa_flags & SA_RESTORER)
360 restorer = ka->sa.sa_restorer;
361 put_user_ex(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100362
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800363 /*
364 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
365 *
366 * WE DO NOT USE IT ANY MORE! It's only left here for historical
367 * reasons and because gdb uses it as a signature to notice
368 * signal handler stack frames.
369 */
370 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
371 } put_user_catch(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700374 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100377 regs->sp = (unsigned long)frame;
378 regs->ip = (unsigned long)ka->sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700379 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100380 regs->dx = (unsigned long)&frame->info;
381 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100383 regs->ds = __USER_DS;
384 regs->es = __USER_DS;
385 regs->ss = __USER_DS;
386 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
David Howells283828f2006-01-18 17:44:00 -0800388 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800390#else /* !CONFIG_X86_32 */
391/*
392 * Determine which stack to use..
393 */
394static void __user *
Hiroshi Shimamoto97286a22009-02-27 10:28:48 -0800395get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
396 void __user **fpstate)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800397{
Hiroshi Shimamoto97286a22009-02-27 10:28:48 -0800398 unsigned long sp;
399
400 /* Default to using normal stack - redzone */
401 sp = regs->sp - 128;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800402
403 /* This is the X/Open sanctioned signal stack switching. */
404 if (ka->sa.sa_flags & SA_ONSTACK) {
405 if (sas_ss_flags(sp) == 0)
406 sp = current->sas_ss_sp + current->sas_ss_size;
407 }
408
Hiroshi Shimamoto97286a22009-02-27 10:28:48 -0800409 if (used_math()) {
410 sp -= sig_xstate_size;
411 *fpstate = (void __user *)round_down(sp, 64);
412 if (save_i387_xstate(*fpstate) < 0)
413 return (void __user *) -1L;
414
415 sp -= frame_size;
416 return (void __user *)round_down(sp, 16) - 8;
417 }
418
419 sp -= frame_size;
420 return (void __user *)round_down(sp, 64) - 8;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800421}
422
423static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
424 sigset_t *set, struct pt_regs *regs)
425{
426 struct rt_sigframe __user *frame;
427 void __user *fp = NULL;
428 int err = 0;
429 struct task_struct *me = current;
430
Hiroshi Shimamoto97286a22009-02-27 10:28:48 -0800431 frame = get_sigframe(ka, regs, sizeof(struct rt_sigframe), &fp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800432
433 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
434 return -EFAULT;
435
436 if (ka->sa.sa_flags & SA_SIGINFO) {
437 if (copy_siginfo_to_user(&frame->info, info))
438 return -EFAULT;
439 }
440
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800441 put_user_try {
442 /* Create the ucontext. */
443 if (cpu_has_xsave)
444 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
445 else
446 put_user_ex(0, &frame->uc.uc_flags);
447 put_user_ex(0, &frame->uc.uc_link);
448 put_user_ex(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
449 put_user_ex(sas_ss_flags(regs->sp),
450 &frame->uc.uc_stack.ss_flags);
451 put_user_ex(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
452 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
453 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800454
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800455 /* Set up to return from userspace. If provided, use a stub
456 already in userspace. */
457 /* x86-64 should always use SA_RESTORER. */
458 if (ka->sa.sa_flags & SA_RESTORER) {
459 put_user_ex(ka->sa.sa_restorer, &frame->pretcode);
460 } else {
461 /* could use a vstub here */
462 err |= -EFAULT;
463 }
464 } put_user_catch(err);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800465
466 if (err)
467 return -EFAULT;
468
469 /* Set up registers for signal handler */
470 regs->di = sig;
471 /* In case the signal handler was declared without prototypes */
472 regs->ax = 0;
473
474 /* This also works for non SA_SIGINFO handlers because they expect the
475 next argument after the signal number on the stack. */
476 regs->si = (unsigned long)&frame->info;
477 regs->dx = (unsigned long)&frame->uc;
478 regs->ip = (unsigned long) ka->sa.sa_handler;
479
480 regs->sp = (unsigned long)frame;
481
482 /* Set up the CS register to run signal handlers in 64-bit mode,
483 even if the handler happens to be interrupting 32-bit code. */
484 regs->cs = __USER_CS;
485
486 return 0;
487}
488#endif /* CONFIG_X86_32 */
489
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800490#ifdef CONFIG_X86_32
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800491/*
492 * Atomically swap in the new signal mask, and wait for a signal.
493 */
494asmlinkage int
495sys_sigsuspend(int history0, int history1, old_sigset_t mask)
496{
497 mask &= _BLOCKABLE;
498 spin_lock_irq(&current->sighand->siglock);
499 current->saved_sigmask = current->blocked;
500 siginitset(&current->blocked, mask);
501 recalc_sigpending();
502 spin_unlock_irq(&current->sighand->siglock);
503
504 current->state = TASK_INTERRUPTIBLE;
505 schedule();
506 set_restore_sigmask();
507
508 return -ERESTARTNOHAND;
509}
510
511asmlinkage int
512sys_sigaction(int sig, const struct old_sigaction __user *act,
513 struct old_sigaction __user *oact)
514{
515 struct k_sigaction new_ka, old_ka;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800516 int ret = 0;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800517
518 if (act) {
519 old_sigset_t mask;
520
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800521 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800522 return -EFAULT;
523
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800524 get_user_try {
525 get_user_ex(new_ka.sa.sa_handler, &act->sa_handler);
526 get_user_ex(new_ka.sa.sa_flags, &act->sa_flags);
527 get_user_ex(mask, &act->sa_mask);
528 get_user_ex(new_ka.sa.sa_restorer, &act->sa_restorer);
529 } get_user_catch(ret);
530
531 if (ret)
532 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800533 siginitset(&new_ka.sa.sa_mask, mask);
534 }
535
536 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
537
538 if (!ret && oact) {
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800539 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800540 return -EFAULT;
541
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800542 put_user_try {
543 put_user_ex(old_ka.sa.sa_handler, &oact->sa_handler);
544 put_user_ex(old_ka.sa.sa_flags, &oact->sa_flags);
545 put_user_ex(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
546 put_user_ex(old_ka.sa.sa_restorer, &oact->sa_restorer);
547 } put_user_catch(ret);
548
549 if (ret)
550 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800551 }
552
553 return ret;
554}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800555#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800556
557#ifdef CONFIG_X86_32
Brian Gerstb12bdaf2009-02-11 16:43:58 -0500558int sys_sigaltstack(struct pt_regs *regs)
559{
560 const stack_t __user *uss = (const stack_t __user *)regs->bx;
561 stack_t __user *uoss = (stack_t __user *)regs->cx;
562
563 return do_sigaltstack(uss, uoss, regs->sp);
564}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800565#else /* !CONFIG_X86_32 */
566asmlinkage long
567sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
568 struct pt_regs *regs)
569{
570 return do_sigaltstack(uss, uoss, regs->sp);
571}
Brian Gerstb12bdaf2009-02-11 16:43:58 -0500572#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800573
574/*
575 * Do a signal return; undo the signal stack.
576 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800577#ifdef CONFIG_X86_32
Brian Gerstb12bdaf2009-02-11 16:43:58 -0500578unsigned long sys_sigreturn(struct pt_regs *regs)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800579{
580 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800581 unsigned long ax;
582 sigset_t set;
583
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800584 frame = (struct sigframe __user *)(regs->sp - 8);
585
586 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
587 goto badframe;
588 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
589 && __copy_from_user(&set.sig[1], &frame->extramask,
590 sizeof(frame->extramask))))
591 goto badframe;
592
593 sigdelsetmask(&set, ~_BLOCKABLE);
594 spin_lock_irq(&current->sighand->siglock);
595 current->blocked = set;
596 recalc_sigpending();
597 spin_unlock_irq(&current->sighand->siglock);
598
599 if (restore_sigcontext(regs, &frame->sc, &ax))
600 goto badframe;
601 return ax;
602
603badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800604 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800605
606 return 0;
607}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800608#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800609
H. Peter Anvin74452502009-02-11 16:31:40 -0800610long sys_rt_sigreturn(struct pt_regs *regs)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800611{
612 struct rt_sigframe __user *frame;
613 unsigned long ax;
614 sigset_t set;
615
616 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
617 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
618 goto badframe;
619 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
620 goto badframe;
621
622 sigdelsetmask(&set, ~_BLOCKABLE);
623 spin_lock_irq(&current->sighand->siglock);
624 current->blocked = set;
625 recalc_sigpending();
626 spin_unlock_irq(&current->sighand->siglock);
627
628 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
629 goto badframe;
630
631 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
632 goto badframe;
633
634 return ax;
635
636badframe:
637 signal_fault(regs, frame, "rt_sigreturn");
638 return 0;
639}
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641/*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100642 * OK, we're invoking a handler:
643 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700644static int signr_convert(int sig)
645{
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700646#ifdef CONFIG_X86_32
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700647 struct thread_info *info = current_thread_info();
648
649 if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
650 return info->exec_domain->signal_invmap[sig];
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700651#endif /* CONFIG_X86_32 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700652 return sig;
653}
654
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700655#ifdef CONFIG_X86_32
656
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700657#define is_ia32 1
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700658#define ia32_setup_frame __setup_frame
659#define ia32_setup_rt_frame __setup_rt_frame
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700660
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700661#else /* !CONFIG_X86_32 */
662
663#ifdef CONFIG_IA32_EMULATION
664#define is_ia32 test_thread_flag(TIF_IA32)
665#else /* !CONFIG_IA32_EMULATION */
666#define is_ia32 0
667#endif /* CONFIG_IA32_EMULATION */
668
Hiroshi Shimamotof5223762008-12-17 18:47:17 -0800669int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
670 sigset_t *set, struct pt_regs *regs);
671int ia32_setup_frame(int sig, struct k_sigaction *ka,
672 sigset_t *set, struct pt_regs *regs);
673
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700674#endif /* CONFIG_X86_32 */
675
Roland McGrath7c1def12005-06-23 00:08:21 -0700676static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700677setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
678 sigset_t *set, struct pt_regs *regs)
679{
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700680 int usig = signr_convert(sig);
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700681 int ret;
682
683 /* Set up the stack frame */
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700684 if (is_ia32) {
685 if (ka->sa.sa_flags & SA_SIGINFO)
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700686 ret = ia32_setup_rt_frame(usig, ka, info, set, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700687 else
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700688 ret = ia32_setup_frame(usig, ka, set, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700689 } else
690 ret = __setup_rt_frame(sig, ka, info, set, regs);
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700691
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700692 if (ret) {
693 force_sigsegv(sig, current);
694 return -EFAULT;
695 }
696
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700697 return ret;
698}
699
700static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800702 sigset_t *oldset, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Roland McGrath7c1def12005-06-23 00:08:21 -0700704 int ret;
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700707 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700709 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800710 case -ERESTART_RESTARTBLOCK:
711 case -ERESTARTNOHAND:
712 regs->ax = -EINTR;
713 break;
714
715 case -ERESTARTSYS:
716 if (!(ka->sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100717 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800719 }
720 /* fallthrough */
721 case -ERESTARTNOINTR:
722 regs->ax = regs->orig_ax;
723 regs->ip -= 2;
724 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726 }
727
728 /*
Roland McGrathe1f28772008-01-30 13:30:50 +0100729 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
730 * flag so that register information in the sigcontext is correct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100732 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
Roland McGrathe1f28772008-01-30 13:30:50 +0100733 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100734 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700736 ret = setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Ingo Molnar7e907f42008-03-06 10:33:08 +0100738 if (ret)
739 return ret;
Roland McGrath7c1def12005-06-23 00:08:21 -0700740
Hiroshi Shimamoto86d32372008-09-23 17:22:32 -0700741#ifdef CONFIG_X86_64
742 /*
743 * This has nothing to do with segment registers,
744 * despite the name. This magic affects uaccess.h
745 * macros' behavior. Reset it to the normal setting.
746 */
747 set_fs(USER_DS);
748#endif
749
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700750 /*
751 * Clear the direction flag as per the ABI for function entry.
752 */
753 regs->flags &= ~X86_EFLAGS_DF;
754
755 /*
756 * Clear TF when entering the signal handler, but
757 * notify any tracer that was single-stepping it.
758 * The tracer may want to single-step inside the
759 * handler too.
760 */
761 regs->flags &= ~X86_EFLAGS_TF;
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700762
Ingo Molnar7e907f42008-03-06 10:33:08 +0100763 spin_lock_irq(&current->sighand->siglock);
764 sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
765 if (!(ka->sa.sa_flags & SA_NODEFER))
766 sigaddset(&current->blocked, sig);
767 recalc_sigpending();
768 spin_unlock_irq(&current->sighand->siglock);
769
Roland McGrath36a03302008-03-14 17:46:38 -0700770 tracehook_signal_handler(sig, info, ka, regs,
771 test_thread_flag(TIF_SINGLESTEP));
772
Ingo Molnar7e907f42008-03-06 10:33:08 +0100773 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700776#ifdef CONFIG_X86_32
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700777#define NR_restart_syscall __NR_restart_syscall
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700778#else /* !CONFIG_X86_32 */
779#define NR_restart_syscall \
780 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
781#endif /* CONFIG_X86_32 */
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783/*
784 * Note that 'init' is a special process: it doesn't get signals it doesn't
785 * want to handle. Thus you cannot kill init even with a SIGKILL even by
786 * mistake.
787 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100788static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800790 struct k_sigaction ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 siginfo_t info;
792 int signr;
David Howells283828f2006-01-18 17:44:00 -0800793 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 /*
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800796 * We want the common case to go fast, which is why we may in certain
797 * cases get here from kernel mode. Just return without doing anything
798 * if so.
799 * X86_32: vm86 regs switched out by assembly code before reaching
800 * here, so testing against kernel CS suffices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 */
Vincent Hanquez717b5942005-06-23 00:08:45 -0700802 if (!user_mode(regs))
David Howells283828f2006-01-18 17:44:00 -0800803 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700805 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
David Howells283828f2006-01-18 17:44:00 -0800806 oldset = &current->saved_sigmask;
807 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 oldset = &current->blocked;
809
810 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
811 if (signr > 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100812 /*
813 * Re-enable any watchpoints before delivering the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 * signal to user space. The processor register will
815 * have been cleared if the watchpoint triggered
816 * inside the kernel.
817 */
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800818 if (current->thread.debugreg7)
Roland McGrath0f534092008-01-30 13:30:59 +0100819 set_debugreg(current->thread.debugreg7, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Ingo Molnar7e907f42008-03-06 10:33:08 +0100821 /* Whee! Actually deliver the signal. */
David Howells283828f2006-01-18 17:44:00 -0800822 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100823 /*
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700824 * A signal was successfully delivered; the saved
David Howells283828f2006-01-18 17:44:00 -0800825 * sigmask will have been stored in the signal frame,
826 * and will be restored by sigreturn, so we can simply
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700827 * clear the TS_RESTORE_SIGMASK flag.
Ingo Molnar7e907f42008-03-06 10:33:08 +0100828 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700829 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
David Howells283828f2006-01-18 17:44:00 -0800830 }
David Howells283828f2006-01-18 17:44:00 -0800831 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700835 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700837 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800838 case -ERESTARTNOHAND:
839 case -ERESTARTSYS:
840 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100841 regs->ax = regs->orig_ax;
842 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800843 break;
844
845 case -ERESTART_RESTARTBLOCK:
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700846 regs->ax = NR_restart_syscall;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100847 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800848 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850 }
David Howells283828f2006-01-18 17:44:00 -0800851
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800852 /*
853 * If there's no signal to deliver, we just put the saved sigmask
854 * back.
855 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700856 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
857 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
David Howells283828f2006-01-18 17:44:00 -0800858 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
862/*
863 * notification of userspace execution resumption
David Howells283828f2006-01-18 17:44:00 -0800864 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100866void
867do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700869#if defined(CONFIG_X86_64) && defined(CONFIG_X86_MCE)
870 /* notify userspace of pending MCEs */
871 if (thread_info_flags & _TIF_MCE_NOTIFY)
872 mce_notify_user();
873#endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 /* deal with pending signal delivery */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700876 if (thread_info_flags & _TIF_SIGPENDING)
David Howells283828f2006-01-18 17:44:00 -0800877 do_signal(regs);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100878
Roland McGrath59e52132008-04-19 19:10:57 -0700879 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
880 clear_thread_flag(TIF_NOTIFY_RESUME);
881 tracehook_notify_resume(regs);
882 }
883
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700884#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 clear_thread_flag(TIF_IRET);
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700886#endif /* CONFIG_X86_32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700888
889void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
890{
891 struct task_struct *me = current;
892
893 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800894 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700895 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800896 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700897 me->comm, me->pid, where, frame,
898 regs->ip, regs->sp, regs->orig_ax);
899 print_vma_addr(" in ", regs->ip);
900 printk(KERN_CONT "\n");
901 }
902
903 force_sig(SIGSEGV, me);
904}