blob: cf34eb37fbeed58d4309bf7f5557358213f7e8fb [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
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080053#define COPY(x) { \
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080054 get_user_ex(regs->x, &sc->x); \
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080055}
56
57#define COPY_SEG(seg) { \
58 unsigned short tmp; \
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080059 get_user_ex(tmp, &sc->seg); \
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080060 regs->seg = tmp; \
61}
62
63#define COPY_SEG_CPL3(seg) { \
64 unsigned short tmp; \
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080065 get_user_ex(tmp, &sc->seg); \
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080066 regs->seg = tmp | 3; \
67}
68
69#define GET_SEG(seg) { \
70 unsigned short tmp; \
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080071 get_user_ex(tmp, &sc->seg); \
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080072 loadsegment(seg, tmp); \
73}
74
75static int
76restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
77 unsigned long *pax)
78{
79 void __user *buf;
80 unsigned int tmpflags;
81 unsigned int err = 0;
82
83 /* Always make any pending restarted system calls return -EINTR */
84 current_thread_info()->restart_block.fn = do_no_restart_syscall;
85
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080086 get_user_try {
87
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080088#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080089 GET_SEG(gs);
90 COPY_SEG(fs);
91 COPY_SEG(es);
92 COPY_SEG(ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080093#endif /* CONFIG_X86_32 */
94
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080095 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
96 COPY(dx); COPY(cx); COPY(ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080097
98#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080099 COPY(r8);
100 COPY(r9);
101 COPY(r10);
102 COPY(r11);
103 COPY(r12);
104 COPY(r13);
105 COPY(r14);
106 COPY(r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800107#endif /* CONFIG_X86_64 */
108
109#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800110 COPY_SEG_CPL3(cs);
111 COPY_SEG_CPL3(ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800112#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800113 /* Kernel saves and restores only the CS segment register on signals,
114 * which is the bare minimum needed to allow mixed 32/64-bit code.
115 * App's signal handler can save/restore other segments if needed. */
116 COPY_SEG_CPL3(cs);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800117#endif /* CONFIG_X86_32 */
118
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800119 get_user_ex(tmpflags, &sc->flags);
120 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
121 regs->orig_ax = -1; /* disable syscall checks */
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800122
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800123 get_user_ex(buf, &sc->fpstate);
124 err |= restore_i387_xstate(buf);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800125
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800126 get_user_ex(*pax, &sc->ax);
127 } get_user_catch(err);
128
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800129 return err;
130}
131
132static int
133setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
134 struct pt_regs *regs, unsigned long mask)
135{
136 int err = 0;
137
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800138 put_user_try {
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800139
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800140#ifdef CONFIG_X86_32
141 {
142 unsigned int tmp;
143
144 savesegment(gs, tmp);
145 put_user_ex(tmp, (unsigned int __user *)&sc->gs);
146 }
147 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
148 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
149 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800150#endif /* CONFIG_X86_32 */
151
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800152 put_user_ex(regs->di, &sc->di);
153 put_user_ex(regs->si, &sc->si);
154 put_user_ex(regs->bp, &sc->bp);
155 put_user_ex(regs->sp, &sc->sp);
156 put_user_ex(regs->bx, &sc->bx);
157 put_user_ex(regs->dx, &sc->dx);
158 put_user_ex(regs->cx, &sc->cx);
159 put_user_ex(regs->ax, &sc->ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800160#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800161 put_user_ex(regs->r8, &sc->r8);
162 put_user_ex(regs->r9, &sc->r9);
163 put_user_ex(regs->r10, &sc->r10);
164 put_user_ex(regs->r11, &sc->r11);
165 put_user_ex(regs->r12, &sc->r12);
166 put_user_ex(regs->r13, &sc->r13);
167 put_user_ex(regs->r14, &sc->r14);
168 put_user_ex(regs->r15, &sc->r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800169#endif /* CONFIG_X86_64 */
170
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800171 put_user_ex(current->thread.trap_no, &sc->trapno);
172 put_user_ex(current->thread.error_code, &sc->err);
173 put_user_ex(regs->ip, &sc->ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800174#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800175 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
176 put_user_ex(regs->flags, &sc->flags);
177 put_user_ex(regs->sp, &sc->sp_at_signal);
178 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800179#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800180 put_user_ex(regs->flags, &sc->flags);
181 put_user_ex(regs->cs, &sc->cs);
182 put_user_ex(0, &sc->gs);
183 put_user_ex(0, &sc->fs);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800184#endif /* CONFIG_X86_32 */
185
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800186 put_user_ex(fpstate, &sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800187
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800188 /* non-iBCS2 extensions.. */
189 put_user_ex(mask, &sc->oldmask);
190 put_user_ex(current->thread.cr2, &sc->cr2);
191 } put_user_catch(err);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800192
193 return err;
194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 * Set up a signal frame.
198 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800199#ifdef CONFIG_X86_32
200static const struct {
201 u16 poplmovl;
202 u32 val;
203 u16 int80;
204} __attribute__((packed)) retcode = {
205 0xb858, /* popl %eax; movl $..., %eax */
206 __NR_sigreturn,
207 0x80cd, /* int $0x80 */
208};
209
210static const struct {
211 u8 movl;
212 u32 val;
213 u16 int80;
214 u8 pad;
215} __attribute__((packed)) rt_retcode = {
216 0xb8, /* movl $..., %eax */
217 __NR_rt_sigreturn,
218 0x80cd, /* int $0x80 */
219 0
220};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222/*
223 * Determine which stack to use..
224 */
225static inline void __user *
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700226get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
Suresh Siddhaab513702008-07-29 10:29:22 -0700227 void **fpstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100229 unsigned long sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 /* Default to using normal stack */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100232 sp = regs->sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Roland McGrath83bd0102008-01-30 13:30:06 +0100234 /*
235 * If we are on the alternate signal stack and would overflow it, don't.
236 * Return an always-bogus address instead so we will die with SIGSEGV.
237 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100238 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
Roland McGrath83bd0102008-01-30 13:30:06 +0100239 return (void __user *) -1L;
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /* This is the X/Open sanctioned signal stack switching. */
242 if (ka->sa.sa_flags & SA_ONSTACK) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100243 if (sas_ss_flags(sp) == 0)
244 sp = current->sas_ss_sp + current->sas_ss_size;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100245 } else {
246 /* This is the legacy signal stack switching. */
247 if ((regs->ss & 0xffff) != __USER_DS &&
248 !(ka->sa.sa_flags & SA_RESTORER) &&
249 ka->sa.sa_restorer)
250 sp = (unsigned long) ka->sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700253 if (used_math()) {
254 sp = sp - sig_xstate_size;
255 *fpstate = (struct _fpstate *) sp;
Hiroshi Shimamoto4b336692008-11-05 18:30:25 -0800256 if (save_i387_xstate(*fpstate) < 0)
257 return (void __user *)-1L;
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700258 }
259
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100260 sp -= frame_size;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100261 /*
262 * Align the stack pointer according to the i386 ABI,
263 * i.e. so that on function entry ((sp + 4) & 15) == 0.
264 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100265 sp = ((sp + 4) & -16ul) - 4;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100266
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100267 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Ingo Molnar7e907f42008-03-06 10:33:08 +0100270static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700271__setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
272 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100275 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700277 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700279 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700282 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700284 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700285 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700287 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700288 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700291 if (__copy_to_user(&frame->extramask, &set->sig[1],
292 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700293 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700296 if (current->mm->context.vdso)
Roland McGrath6c3652e2008-01-30 13:30:42 +0100297 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100298 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100299 restorer = &frame->retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (ka->sa.sa_flags & SA_RESTORER)
301 restorer = ka->sa.sa_restorer;
302
303 /* Set up to return from userspace. */
304 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100307 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 *
309 * WE DO NOT USE IT ANY MORE! It's only left here for historical
310 * reasons and because gdb uses it as a signature to notice
311 * signal handler stack frames.
312 */
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -0800313 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700316 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100319 regs->sp = (unsigned long)frame;
320 regs->ip = (unsigned long)ka->sa.sa_handler;
321 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800322 regs->dx = 0;
323 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100325 regs->ds = __USER_DS;
326 regs->es = __USER_DS;
327 regs->ss = __USER_DS;
328 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
David Howells283828f2006-01-18 17:44:00 -0800330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700333static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
334 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100337 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700339 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700341 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700344 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800346 put_user_try {
347 put_user_ex(sig, &frame->sig);
348 put_user_ex(&frame->info, &frame->pinfo);
349 put_user_ex(&frame->uc, &frame->puc);
350 err |= copy_siginfo_to_user(&frame->info, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800352 /* Create the ucontext. */
353 if (cpu_has_xsave)
354 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
355 else
356 put_user_ex(0, &frame->uc.uc_flags);
357 put_user_ex(0, &frame->uc.uc_link);
358 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
359 put_user_ex(sas_ss_flags(regs->sp),
360 &frame->uc.uc_stack.ss_flags);
361 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
362 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
363 regs, set->sig[0]);
364 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800366 /* Set up to return from userspace. */
367 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
368 if (ka->sa.sa_flags & SA_RESTORER)
369 restorer = ka->sa.sa_restorer;
370 put_user_ex(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100371
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800372 /*
373 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
374 *
375 * WE DO NOT USE IT ANY MORE! It's only left here for historical
376 * reasons and because gdb uses it as a signature to notice
377 * signal handler stack frames.
378 */
379 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
380 } put_user_catch(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700383 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100386 regs->sp = (unsigned long)frame;
387 regs->ip = (unsigned long)ka->sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700388 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100389 regs->dx = (unsigned long)&frame->info;
390 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100392 regs->ds = __USER_DS;
393 regs->es = __USER_DS;
394 regs->ss = __USER_DS;
395 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
David Howells283828f2006-01-18 17:44:00 -0800397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800399#else /* !CONFIG_X86_32 */
400/*
401 * Determine which stack to use..
402 */
403static void __user *
404get_stack(struct k_sigaction *ka, unsigned long sp, unsigned long size)
405{
406 /* Default to using normal stack - redzone*/
407 sp -= 128;
408
409 /* This is the X/Open sanctioned signal stack switching. */
410 if (ka->sa.sa_flags & SA_ONSTACK) {
411 if (sas_ss_flags(sp) == 0)
412 sp = current->sas_ss_sp + current->sas_ss_size;
413 }
414
415 return (void __user *)round_down(sp - size, 64);
416}
417
418static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
419 sigset_t *set, struct pt_regs *regs)
420{
421 struct rt_sigframe __user *frame;
422 void __user *fp = NULL;
423 int err = 0;
424 struct task_struct *me = current;
425
426 if (used_math()) {
427 fp = get_stack(ka, regs->sp, sig_xstate_size);
428 frame = (void __user *)round_down(
429 (unsigned long)fp - sizeof(struct rt_sigframe), 16) - 8;
430
431 if (save_i387_xstate(fp) < 0)
432 return -EFAULT;
433 } else
434 frame = get_stack(ka, regs->sp, sizeof(struct rt_sigframe)) - 8;
435
436 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
437 return -EFAULT;
438
439 if (ka->sa.sa_flags & SA_SIGINFO) {
440 if (copy_siginfo_to_user(&frame->info, info))
441 return -EFAULT;
442 }
443
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800444 put_user_try {
445 /* Create the ucontext. */
446 if (cpu_has_xsave)
447 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
448 else
449 put_user_ex(0, &frame->uc.uc_flags);
450 put_user_ex(0, &frame->uc.uc_link);
451 put_user_ex(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
452 put_user_ex(sas_ss_flags(regs->sp),
453 &frame->uc.uc_stack.ss_flags);
454 put_user_ex(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
455 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
456 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800457
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800458 /* Set up to return from userspace. If provided, use a stub
459 already in userspace. */
460 /* x86-64 should always use SA_RESTORER. */
461 if (ka->sa.sa_flags & SA_RESTORER) {
462 put_user_ex(ka->sa.sa_restorer, &frame->pretcode);
463 } else {
464 /* could use a vstub here */
465 err |= -EFAULT;
466 }
467 } put_user_catch(err);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800468
469 if (err)
470 return -EFAULT;
471
472 /* Set up registers for signal handler */
473 regs->di = sig;
474 /* In case the signal handler was declared without prototypes */
475 regs->ax = 0;
476
477 /* This also works for non SA_SIGINFO handlers because they expect the
478 next argument after the signal number on the stack. */
479 regs->si = (unsigned long)&frame->info;
480 regs->dx = (unsigned long)&frame->uc;
481 regs->ip = (unsigned long) ka->sa.sa_handler;
482
483 regs->sp = (unsigned long)frame;
484
485 /* Set up the CS register to run signal handlers in 64-bit mode,
486 even if the handler happens to be interrupting 32-bit code. */
487 regs->cs = __USER_CS;
488
489 return 0;
490}
491#endif /* CONFIG_X86_32 */
492
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800493#ifdef CONFIG_X86_32
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800494/*
495 * Atomically swap in the new signal mask, and wait for a signal.
496 */
497asmlinkage int
498sys_sigsuspend(int history0, int history1, old_sigset_t mask)
499{
500 mask &= _BLOCKABLE;
501 spin_lock_irq(&current->sighand->siglock);
502 current->saved_sigmask = current->blocked;
503 siginitset(&current->blocked, mask);
504 recalc_sigpending();
505 spin_unlock_irq(&current->sighand->siglock);
506
507 current->state = TASK_INTERRUPTIBLE;
508 schedule();
509 set_restore_sigmask();
510
511 return -ERESTARTNOHAND;
512}
513
514asmlinkage int
515sys_sigaction(int sig, const struct old_sigaction __user *act,
516 struct old_sigaction __user *oact)
517{
518 struct k_sigaction new_ka, old_ka;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800519 int ret = 0;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800520
521 if (act) {
522 old_sigset_t mask;
523
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800524 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800525 return -EFAULT;
526
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800527 get_user_try {
528 get_user_ex(new_ka.sa.sa_handler, &act->sa_handler);
529 get_user_ex(new_ka.sa.sa_flags, &act->sa_flags);
530 get_user_ex(mask, &act->sa_mask);
531 get_user_ex(new_ka.sa.sa_restorer, &act->sa_restorer);
532 } get_user_catch(ret);
533
534 if (ret)
535 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800536 siginitset(&new_ka.sa.sa_mask, mask);
537 }
538
539 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
540
541 if (!ret && oact) {
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800542 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800543 return -EFAULT;
544
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800545 put_user_try {
546 put_user_ex(old_ka.sa.sa_handler, &oact->sa_handler);
547 put_user_ex(old_ka.sa.sa_flags, &oact->sa_flags);
548 put_user_ex(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
549 put_user_ex(old_ka.sa.sa_restorer, &oact->sa_restorer);
550 } put_user_catch(ret);
551
552 if (ret)
553 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800554 }
555
556 return ret;
557}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800558#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800559
560#ifdef CONFIG_X86_32
561asmlinkage int sys_sigaltstack(unsigned long bx)
562{
563 /*
564 * This is needed to make gcc realize it doesn't own the
565 * "struct pt_regs"
566 */
567 struct pt_regs *regs = (struct pt_regs *)&bx;
568 const stack_t __user *uss = (const stack_t __user *)bx;
569 stack_t __user *uoss = (stack_t __user *)regs->cx;
570
571 return do_sigaltstack(uss, uoss, regs->sp);
572}
573#else /* !CONFIG_X86_32 */
574asmlinkage long
575sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
576 struct pt_regs *regs)
577{
578 return do_sigaltstack(uss, uoss, regs->sp);
579}
580#endif /* CONFIG_X86_32 */
581
582/*
583 * Do a signal return; undo the signal stack.
584 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800585#ifdef CONFIG_X86_32
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800586asmlinkage unsigned long sys_sigreturn(unsigned long __unused)
587{
588 struct sigframe __user *frame;
589 struct pt_regs *regs;
590 unsigned long ax;
591 sigset_t set;
592
593 regs = (struct pt_regs *) &__unused;
594 frame = (struct sigframe __user *)(regs->sp - 8);
595
596 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
597 goto badframe;
598 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
599 && __copy_from_user(&set.sig[1], &frame->extramask,
600 sizeof(frame->extramask))))
601 goto badframe;
602
603 sigdelsetmask(&set, ~_BLOCKABLE);
604 spin_lock_irq(&current->sighand->siglock);
605 current->blocked = set;
606 recalc_sigpending();
607 spin_unlock_irq(&current->sighand->siglock);
608
609 if (restore_sigcontext(regs, &frame->sc, &ax))
610 goto badframe;
611 return ax;
612
613badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800614 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800615
616 return 0;
617}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800618#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800619
620static long do_rt_sigreturn(struct pt_regs *regs)
621{
622 struct rt_sigframe __user *frame;
623 unsigned long ax;
624 sigset_t set;
625
626 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
627 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
628 goto badframe;
629 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
630 goto badframe;
631
632 sigdelsetmask(&set, ~_BLOCKABLE);
633 spin_lock_irq(&current->sighand->siglock);
634 current->blocked = set;
635 recalc_sigpending();
636 spin_unlock_irq(&current->sighand->siglock);
637
638 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
639 goto badframe;
640
641 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
642 goto badframe;
643
644 return ax;
645
646badframe:
647 signal_fault(regs, frame, "rt_sigreturn");
648 return 0;
649}
650
651#ifdef CONFIG_X86_32
Hiroshi Shimamoto42174582008-12-05 17:17:09 -0800652asmlinkage int sys_rt_sigreturn(struct pt_regs regs)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800653{
Hiroshi Shimamoto42174582008-12-05 17:17:09 -0800654 return do_rt_sigreturn(&regs);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800655}
656#else /* !CONFIG_X86_32 */
657asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
658{
659 return do_rt_sigreturn(regs);
660}
661#endif /* CONFIG_X86_32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663/*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100664 * OK, we're invoking a handler:
665 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700666static int signr_convert(int sig)
667{
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700668#ifdef CONFIG_X86_32
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700669 struct thread_info *info = current_thread_info();
670
671 if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
672 return info->exec_domain->signal_invmap[sig];
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700673#endif /* CONFIG_X86_32 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700674 return sig;
675}
676
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700677#ifdef CONFIG_X86_32
678
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700679#define is_ia32 1
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700680#define ia32_setup_frame __setup_frame
681#define ia32_setup_rt_frame __setup_rt_frame
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700682
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700683#else /* !CONFIG_X86_32 */
684
685#ifdef CONFIG_IA32_EMULATION
686#define is_ia32 test_thread_flag(TIF_IA32)
687#else /* !CONFIG_IA32_EMULATION */
688#define is_ia32 0
689#endif /* CONFIG_IA32_EMULATION */
690
Hiroshi Shimamotof5223762008-12-17 18:47:17 -0800691int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
692 sigset_t *set, struct pt_regs *regs);
693int ia32_setup_frame(int sig, struct k_sigaction *ka,
694 sigset_t *set, struct pt_regs *regs);
695
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700696#endif /* CONFIG_X86_32 */
697
Roland McGrath7c1def12005-06-23 00:08:21 -0700698static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700699setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
700 sigset_t *set, struct pt_regs *regs)
701{
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700702 int usig = signr_convert(sig);
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700703 int ret;
704
705 /* Set up the stack frame */
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700706 if (is_ia32) {
707 if (ka->sa.sa_flags & SA_SIGINFO)
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700708 ret = ia32_setup_rt_frame(usig, ka, info, set, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700709 else
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700710 ret = ia32_setup_frame(usig, ka, set, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700711 } else
712 ret = __setup_rt_frame(sig, ka, info, set, regs);
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700713
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700714 if (ret) {
715 force_sigsegv(sig, current);
716 return -EFAULT;
717 }
718
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700719 return ret;
720}
721
722static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800724 sigset_t *oldset, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Roland McGrath7c1def12005-06-23 00:08:21 -0700726 int ret;
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700729 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700731 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800732 case -ERESTART_RESTARTBLOCK:
733 case -ERESTARTNOHAND:
734 regs->ax = -EINTR;
735 break;
736
737 case -ERESTARTSYS:
738 if (!(ka->sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100739 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800741 }
742 /* fallthrough */
743 case -ERESTARTNOINTR:
744 regs->ax = regs->orig_ax;
745 regs->ip -= 2;
746 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748 }
749
750 /*
Roland McGrathe1f28772008-01-30 13:30:50 +0100751 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
752 * flag so that register information in the sigcontext is correct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100754 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
Roland McGrathe1f28772008-01-30 13:30:50 +0100755 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100756 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700758 ret = setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Ingo Molnar7e907f42008-03-06 10:33:08 +0100760 if (ret)
761 return ret;
Roland McGrath7c1def12005-06-23 00:08:21 -0700762
Hiroshi Shimamoto86d32372008-09-23 17:22:32 -0700763#ifdef CONFIG_X86_64
764 /*
765 * This has nothing to do with segment registers,
766 * despite the name. This magic affects uaccess.h
767 * macros' behavior. Reset it to the normal setting.
768 */
769 set_fs(USER_DS);
770#endif
771
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700772 /*
773 * Clear the direction flag as per the ABI for function entry.
774 */
775 regs->flags &= ~X86_EFLAGS_DF;
776
777 /*
778 * Clear TF when entering the signal handler, but
779 * notify any tracer that was single-stepping it.
780 * The tracer may want to single-step inside the
781 * handler too.
782 */
783 regs->flags &= ~X86_EFLAGS_TF;
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700784
Ingo Molnar7e907f42008-03-06 10:33:08 +0100785 spin_lock_irq(&current->sighand->siglock);
786 sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
787 if (!(ka->sa.sa_flags & SA_NODEFER))
788 sigaddset(&current->blocked, sig);
789 recalc_sigpending();
790 spin_unlock_irq(&current->sighand->siglock);
791
Roland McGrath36a03302008-03-14 17:46:38 -0700792 tracehook_signal_handler(sig, info, ka, regs,
793 test_thread_flag(TIF_SINGLESTEP));
794
Ingo Molnar7e907f42008-03-06 10:33:08 +0100795 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700798#ifdef CONFIG_X86_32
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700799#define NR_restart_syscall __NR_restart_syscall
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700800#else /* !CONFIG_X86_32 */
801#define NR_restart_syscall \
802 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
803#endif /* CONFIG_X86_32 */
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805/*
806 * Note that 'init' is a special process: it doesn't get signals it doesn't
807 * want to handle. Thus you cannot kill init even with a SIGKILL even by
808 * mistake.
809 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100810static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800812 struct k_sigaction ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 siginfo_t info;
814 int signr;
David Howells283828f2006-01-18 17:44:00 -0800815 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 /*
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800818 * We want the common case to go fast, which is why we may in certain
819 * cases get here from kernel mode. Just return without doing anything
820 * if so.
821 * X86_32: vm86 regs switched out by assembly code before reaching
822 * here, so testing against kernel CS suffices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 */
Vincent Hanquez717b5942005-06-23 00:08:45 -0700824 if (!user_mode(regs))
David Howells283828f2006-01-18 17:44:00 -0800825 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700827 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
David Howells283828f2006-01-18 17:44:00 -0800828 oldset = &current->saved_sigmask;
829 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 oldset = &current->blocked;
831
832 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
833 if (signr > 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100834 /*
835 * Re-enable any watchpoints before delivering the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 * signal to user space. The processor register will
837 * have been cleared if the watchpoint triggered
838 * inside the kernel.
839 */
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800840 if (current->thread.debugreg7)
Roland McGrath0f534092008-01-30 13:30:59 +0100841 set_debugreg(current->thread.debugreg7, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Ingo Molnar7e907f42008-03-06 10:33:08 +0100843 /* Whee! Actually deliver the signal. */
David Howells283828f2006-01-18 17:44:00 -0800844 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100845 /*
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700846 * A signal was successfully delivered; the saved
David Howells283828f2006-01-18 17:44:00 -0800847 * sigmask will have been stored in the signal frame,
848 * and will be restored by sigreturn, so we can simply
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700849 * clear the TS_RESTORE_SIGMASK flag.
Ingo Molnar7e907f42008-03-06 10:33:08 +0100850 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700851 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
David Howells283828f2006-01-18 17:44:00 -0800852 }
David Howells283828f2006-01-18 17:44:00 -0800853 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700857 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700859 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800860 case -ERESTARTNOHAND:
861 case -ERESTARTSYS:
862 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100863 regs->ax = regs->orig_ax;
864 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800865 break;
866
867 case -ERESTART_RESTARTBLOCK:
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700868 regs->ax = NR_restart_syscall;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100869 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800870 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872 }
David Howells283828f2006-01-18 17:44:00 -0800873
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800874 /*
875 * If there's no signal to deliver, we just put the saved sigmask
876 * back.
877 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700878 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
879 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
David Howells283828f2006-01-18 17:44:00 -0800880 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
884/*
885 * notification of userspace execution resumption
David Howells283828f2006-01-18 17:44:00 -0800886 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100888void
889do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700891#if defined(CONFIG_X86_64) && defined(CONFIG_X86_MCE)
892 /* notify userspace of pending MCEs */
893 if (thread_info_flags & _TIF_MCE_NOTIFY)
894 mce_notify_user();
895#endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 /* deal with pending signal delivery */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700898 if (thread_info_flags & _TIF_SIGPENDING)
David Howells283828f2006-01-18 17:44:00 -0800899 do_signal(regs);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100900
Roland McGrath59e52132008-04-19 19:10:57 -0700901 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
902 clear_thread_flag(TIF_NOTIFY_RESUME);
903 tracehook_notify_resume(regs);
904 }
905
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700906#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 clear_thread_flag(TIF_IRET);
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700908#endif /* CONFIG_X86_32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700910
911void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
912{
913 struct task_struct *me = current;
914
915 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800916 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700917 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800918 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700919 me->comm, me->pid, where, frame,
920 regs->ip, regs->sp, regs->orig_ax);
921 print_vma_addr(" in ", regs->ip);
922 printk(KERN_CONT "\n");
923 }
924
925 force_sig(SIGSEGV, me);
926}