blob: f7dd6c44c042e7decf3feec38d61729aa1a4984d [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 *
4 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
5 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
6 */
7
Ingo Molnar7e907f42008-03-06 10:33:08 +01008#include <linux/sched.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +01009#include <linux/mm.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080010#include <linux/smp.h>
11#include <linux/kernel.h>
12#include <linux/signal.h>
13#include <linux/errno.h>
14#include <linux/wait.h>
15#include <linux/ptrace.h>
16#include <linux/tracehook.h>
17#include <linux/unistd.h>
18#include <linux/stddef.h>
19#include <linux/personality.h>
20#include <linux/uaccess.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/processor.h>
23#include <asm/ucontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/i387.h>
Roland McGrath6c3652e2008-01-30 13:30:42 +010025#include <asm/vdso.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080026
27#ifdef CONFIG_X86_64
28#include <asm/proto.h>
29#include <asm/ia32_unistd.h>
30#include <asm/mce.h>
31#endif /* CONFIG_X86_64 */
32
Hiroshi Shimamotobb579252008-09-05 16:26:55 -070033#include <asm/syscall.h>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053034#include <asm/syscalls.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010035
Harvey Harrison123a6342008-02-08 12:10:00 -080036#include "sigframe.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
39
Harvey Harrison1a176802008-02-08 12:09:59 -080040#define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
41 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
42 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
43 X86_EFLAGS_CF)
44
45#ifdef CONFIG_X86_32
46# define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
47#else
48# define FIX_EFLAGS __FIX_EFLAGS
49#endif
50
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -080051static const struct {
52 u16 poplmovl;
53 u32 val;
54 u16 int80;
55} __attribute__((packed)) retcode = {
56 0xb858, /* popl %eax; movl $..., %eax */
57 __NR_sigreturn,
58 0x80cd, /* int $0x80 */
59};
60
61static const struct {
62 u8 movl;
63 u32 val;
64 u16 int80;
65 u8 pad;
66} __attribute__((packed)) rt_retcode = {
67 0xb8, /* movl $..., %eax */
68 __NR_rt_sigreturn,
69 0x80cd, /* int $0x80 */
70 0
71};
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/*
74 * Atomically swap in the new signal mask, and wait for a signal.
75 */
76asmlinkage int
77sys_sigsuspend(int history0, int history1, old_sigset_t mask)
78{
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 mask &= _BLOCKABLE;
80 spin_lock_irq(&current->sighand->siglock);
David Howells283828f2006-01-18 17:44:00 -080081 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 siginitset(&current->blocked, mask);
83 recalc_sigpending();
84 spin_unlock_irq(&current->sighand->siglock);
85
David Howells283828f2006-01-18 17:44:00 -080086 current->state = TASK_INTERRUPTIBLE;
87 schedule();
Roland McGrath5a8da0e2008-04-30 00:53:10 -070088 set_restore_sigmask();
Ingo Molnar7e907f42008-03-06 10:33:08 +010089
David Howells283828f2006-01-18 17:44:00 -080090 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Ingo Molnar7e907f42008-03-06 10:33:08 +010093asmlinkage int
Linus Torvalds1da177e2005-04-16 15:20:36 -070094sys_sigaction(int sig, const struct old_sigaction __user *act,
95 struct old_sigaction __user *oact)
96{
97 struct k_sigaction new_ka, old_ka;
98 int ret;
99
100 if (act) {
101 old_sigset_t mask;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
104 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
105 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
106 return -EFAULT;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
109 __get_user(mask, &act->sa_mask);
110 siginitset(&new_ka.sa.sa_mask, mask);
111 }
112
113 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
114
115 if (!ret && oact) {
116 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
117 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
118 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
119 return -EFAULT;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
122 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
123 }
124
125 return ret;
126}
127
Hiroshi Shimamoto666ac7b2008-11-21 17:38:25 -0800128#ifdef CONFIG_X86_32
Ingo Molnar7e907f42008-03-06 10:33:08 +0100129asmlinkage int sys_sigaltstack(unsigned long bx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Ingo Molnar7e907f42008-03-06 10:33:08 +0100131 /*
132 * This is needed to make gcc realize it doesn't own the
133 * "struct pt_regs"
134 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100135 struct pt_regs *regs = (struct pt_regs *)&bx;
136 const stack_t __user *uss = (const stack_t __user *)bx;
137 stack_t __user *uoss = (stack_t __user *)regs->cx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100139 return do_sigaltstack(uss, uoss, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
Hiroshi Shimamoto666ac7b2008-11-21 17:38:25 -0800141#else /* !CONFIG_X86_32 */
142asmlinkage long
143sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
144 struct pt_regs *regs)
145{
146 return do_sigaltstack(uss, uoss, regs->sp);
147}
148#endif /* CONFIG_X86_32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Hiroshi Shimamotoa2e8d3d2008-10-02 22:09:20 -0700150#define COPY(x) { \
151 err |= __get_user(regs->x, &sc->x); \
152}
153
154#define COPY_SEG(seg) { \
155 unsigned short tmp; \
156 err |= __get_user(tmp, &sc->seg); \
157 regs->seg = tmp; \
158}
159
Hiroshi Shimamoto3ddd9722008-11-20 18:32:17 -0800160#define COPY_SEG_CPL3(seg) { \
Hiroshi Shimamotoa2e8d3d2008-10-02 22:09:20 -0700161 unsigned short tmp; \
162 err |= __get_user(tmp, &sc->seg); \
163 regs->seg = tmp | 3; \
164}
165
166#define GET_SEG(seg) { \
167 unsigned short tmp; \
168 err |= __get_user(tmp, &sc->seg); \
169 loadsegment(seg, tmp); \
170}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172/*
173 * Do a signal return; undo the signal stack.
174 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175static int
Harvey Harrison866bc132008-02-08 12:10:02 -0800176restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
177 unsigned long *pax)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Hiroshi Shimamoto69e13ad2008-10-02 22:18:47 -0700179 void __user *buf;
180 unsigned int tmpflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 unsigned int err = 0;
182
183 /* Always make any pending restarted system calls return -EINTR */
184 current_thread_info()->restart_block.fn = do_no_restart_syscall;
185
Hiroshi Shimamoto709110b2008-10-23 17:14:25 -0700186#ifdef CONFIG_X86_32
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100187 GET_SEG(gs);
188 COPY_SEG(fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 COPY_SEG(es);
190 COPY_SEG(ds);
Hiroshi Shimamoto709110b2008-10-23 17:14:25 -0700191#endif /* CONFIG_X86_32 */
192
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800193 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
194 COPY(dx); COPY(cx); COPY(ip);
Hiroshi Shimamoto709110b2008-10-23 17:14:25 -0700195
196#ifdef CONFIG_X86_64
197 COPY(r8);
198 COPY(r9);
199 COPY(r10);
200 COPY(r11);
201 COPY(r12);
202 COPY(r13);
203 COPY(r14);
204 COPY(r15);
205#endif /* CONFIG_X86_64 */
206
207#ifdef CONFIG_X86_32
Hiroshi Shimamoto3ddd9722008-11-20 18:32:17 -0800208 COPY_SEG_CPL3(cs);
209 COPY_SEG_CPL3(ss);
Hiroshi Shimamoto709110b2008-10-23 17:14:25 -0700210#else /* !CONFIG_X86_32 */
211 /* Kernel saves and restores only the CS segment register on signals,
212 * which is the bare minimum needed to allow mixed 32/64-bit code.
213 * App's signal handler can save/restore other segments if needed. */
Hiroshi Shimamoto3ddd9722008-11-20 18:32:17 -0800214 COPY_SEG_CPL3(cs);
Hiroshi Shimamoto709110b2008-10-23 17:14:25 -0700215#endif /* CONFIG_X86_32 */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100216
Hiroshi Shimamoto69e13ad2008-10-02 22:18:47 -0700217 err |= __get_user(tmpflags, &sc->flags);
218 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
219 regs->orig_ax = -1; /* disable syscall checks */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100220
Hiroshi Shimamoto69e13ad2008-10-02 22:18:47 -0700221 err |= __get_user(buf, &sc->fpstate);
222 err |= restore_i387_xstate(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Harvey Harrison866bc132008-02-08 12:10:02 -0800224 err |= __get_user(*pax, &sc->ax);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Harvey Harrison866bc132008-02-08 12:10:02 -0800228asmlinkage unsigned long sys_sigreturn(unsigned long __unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Ingo Molnar7e907f42008-03-06 10:33:08 +0100230 struct sigframe __user *frame;
231 struct pt_regs *regs;
Harvey Harrison866bc132008-02-08 12:10:02 -0800232 unsigned long ax;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100233 sigset_t set;
234
235 regs = (struct pt_regs *) &__unused;
236 frame = (struct sigframe __user *)(regs->sp - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
239 goto badframe;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100240 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 && __copy_from_user(&set.sig[1], &frame->extramask,
242 sizeof(frame->extramask))))
243 goto badframe;
244
245 sigdelsetmask(&set, ~_BLOCKABLE);
246 spin_lock_irq(&current->sighand->siglock);
247 current->blocked = set;
248 recalc_sigpending();
249 spin_unlock_irq(&current->sighand->siglock);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100250
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100251 if (restore_sigcontext(regs, &frame->sc, &ax))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 goto badframe;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100253 return ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255badframe:
Andi Kleen03252912008-01-30 13:33:18 +0100256 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamoto1181f8b2008-07-03 13:12:13 -0700257 printk("%s%s[%d] bad frame in sigreturn frame:"
Ingo Molnar97b44ae2008-03-06 10:43:17 +0100258 "%p ip:%lx sp:%lx oeax:%lx",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700259 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100260 current->comm, task_pid_nr(current), frame, regs->ip,
261 regs->sp, regs->orig_ax);
Andi Kleen03252912008-01-30 13:33:18 +0100262 print_vma_addr(" in ", regs->ip);
Ingo Molnar97b44ae2008-03-06 10:43:17 +0100263 printk(KERN_CONT "\n");
Andi Kleen03252912008-01-30 13:33:18 +0100264 }
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 force_sig(SIGSEGV, current);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return 0;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100269}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Hiroshi Shimamotoe6babb62008-09-12 17:03:31 -0700271static long do_rt_sigreturn(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Harvey Harrison2d19c452008-02-08 12:10:00 -0800273 struct rt_sigframe __user *frame;
Harvey Harrison866bc132008-02-08 12:10:02 -0800274 unsigned long ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Harvey Harrison2d19c452008-02-08 12:10:00 -0800277 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
279 goto badframe;
280 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
281 goto badframe;
282
283 sigdelsetmask(&set, ~_BLOCKABLE);
284 spin_lock_irq(&current->sighand->siglock);
285 current->blocked = set;
286 recalc_sigpending();
287 spin_unlock_irq(&current->sighand->siglock);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100288
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100289 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 goto badframe;
291
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100292 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 goto badframe;
294
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100295 return ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297badframe:
Hiroshi Shimamotoe6babb62008-09-12 17:03:31 -0700298 signal_fault(regs, frame, "rt_sigreturn");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return 0;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100300}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Hiroshi Shimamoto2456d732008-11-21 17:38:57 -0800302#ifdef CONFIG_X86_32
Hiroshi Shimamotoe6babb62008-09-12 17:03:31 -0700303asmlinkage int sys_rt_sigreturn(unsigned long __unused)
304{
305 struct pt_regs *regs = (struct pt_regs *)&__unused;
306
307 return do_rt_sigreturn(regs);
308}
Hiroshi Shimamoto2456d732008-11-21 17:38:57 -0800309#else /* !CONFIG_X86_32 */
310asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
311{
312 return do_rt_sigreturn(regs);
313}
314#endif /* CONFIG_X86_32 */
Hiroshi Shimamotoe6babb62008-09-12 17:03:31 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/*
317 * Set up a signal frame.
318 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319static int
Suresh Siddhaab513702008-07-29 10:29:22 -0700320setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 struct pt_regs *regs, unsigned long mask)
322{
Hiroshi Shimamoto15002fa2008-11-07 19:25:36 -0800323 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Hiroshi Shimamoto15002fa2008-11-07 19:25:36 -0800325#ifdef CONFIG_X86_32
326 {
327 unsigned int tmp;
328
329 savesegment(gs, tmp);
330 err |= __put_user(tmp, (unsigned int __user *)&sc->gs);
331 }
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100332 err |= __put_user(regs->fs, (unsigned int __user *)&sc->fs);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100333 err |= __put_user(regs->es, (unsigned int __user *)&sc->es);
334 err |= __put_user(regs->ds, (unsigned int __user *)&sc->ds);
Hiroshi Shimamoto15002fa2008-11-07 19:25:36 -0800335#endif /* CONFIG_X86_32 */
336
H. Peter Anvin742fa542008-01-30 13:30:56 +0100337 err |= __put_user(regs->di, &sc->di);
338 err |= __put_user(regs->si, &sc->si);
339 err |= __put_user(regs->bp, &sc->bp);
340 err |= __put_user(regs->sp, &sc->sp);
341 err |= __put_user(regs->bx, &sc->bx);
342 err |= __put_user(regs->dx, &sc->dx);
343 err |= __put_user(regs->cx, &sc->cx);
344 err |= __put_user(regs->ax, &sc->ax);
Hiroshi Shimamoto15002fa2008-11-07 19:25:36 -0800345#ifdef CONFIG_X86_64
346 err |= __put_user(regs->r8, &sc->r8);
347 err |= __put_user(regs->r9, &sc->r9);
348 err |= __put_user(regs->r10, &sc->r10);
349 err |= __put_user(regs->r11, &sc->r11);
350 err |= __put_user(regs->r12, &sc->r12);
351 err |= __put_user(regs->r13, &sc->r13);
352 err |= __put_user(regs->r14, &sc->r14);
353 err |= __put_user(regs->r15, &sc->r15);
354#endif /* CONFIG_X86_64 */
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 err |= __put_user(current->thread.trap_no, &sc->trapno);
357 err |= __put_user(current->thread.error_code, &sc->err);
H. Peter Anvin742fa542008-01-30 13:30:56 +0100358 err |= __put_user(regs->ip, &sc->ip);
Hiroshi Shimamoto15002fa2008-11-07 19:25:36 -0800359#ifdef CONFIG_X86_32
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100360 err |= __put_user(regs->cs, (unsigned int __user *)&sc->cs);
H. Peter Anvin742fa542008-01-30 13:30:56 +0100361 err |= __put_user(regs->flags, &sc->flags);
362 err |= __put_user(regs->sp, &sc->sp_at_signal);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100363 err |= __put_user(regs->ss, (unsigned int __user *)&sc->ss);
Hiroshi Shimamoto15002fa2008-11-07 19:25:36 -0800364#else /* !CONFIG_X86_32 */
365 err |= __put_user(regs->flags, &sc->flags);
366 err |= __put_user(regs->cs, &sc->cs);
367 err |= __put_user(0, &sc->gs);
368 err |= __put_user(0, &sc->fs);
369#endif /* CONFIG_X86_32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Hiroshi Shimamoto4b336692008-11-05 18:30:25 -0800371 err |= __put_user(fpstate, &sc->fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 /* non-iBCS2 extensions.. */
374 err |= __put_user(mask, &sc->oldmask);
375 err |= __put_user(current->thread.cr2, &sc->cr2);
376
377 return err;
378}
379
380/*
381 * Determine which stack to use..
382 */
383static inline void __user *
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700384get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
Suresh Siddhaab513702008-07-29 10:29:22 -0700385 void **fpstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100387 unsigned long sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 /* Default to using normal stack */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100390 sp = regs->sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Roland McGrath83bd0102008-01-30 13:30:06 +0100392 /*
393 * If we are on the alternate signal stack and would overflow it, don't.
394 * Return an always-bogus address instead so we will die with SIGSEGV.
395 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100396 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
Roland McGrath83bd0102008-01-30 13:30:06 +0100397 return (void __user *) -1L;
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /* This is the X/Open sanctioned signal stack switching. */
400 if (ka->sa.sa_flags & SA_ONSTACK) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100401 if (sas_ss_flags(sp) == 0)
402 sp = current->sas_ss_sp + current->sas_ss_size;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100403 } else {
404 /* This is the legacy signal stack switching. */
405 if ((regs->ss & 0xffff) != __USER_DS &&
406 !(ka->sa.sa_flags & SA_RESTORER) &&
407 ka->sa.sa_restorer)
408 sp = (unsigned long) ka->sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700411 if (used_math()) {
412 sp = sp - sig_xstate_size;
413 *fpstate = (struct _fpstate *) sp;
Hiroshi Shimamoto4b336692008-11-05 18:30:25 -0800414 if (save_i387_xstate(*fpstate) < 0)
415 return (void __user *)-1L;
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700416 }
417
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100418 sp -= frame_size;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100419 /*
420 * Align the stack pointer according to the i386 ABI,
421 * i.e. so that on function entry ((sp + 4) & 15) == 0.
422 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100423 sp = ((sp + 4) & -16ul) - 4;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100424
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100425 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Ingo Molnar7e907f42008-03-06 10:33:08 +0100428static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700429__setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
430 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100433 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700435 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700437 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700440 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700442 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700443 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700445 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700446 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700449 if (__copy_to_user(&frame->extramask, &set->sig[1],
450 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700451 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700454 if (current->mm->context.vdso)
Roland McGrath6c3652e2008-01-30 13:30:42 +0100455 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100456 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100457 restorer = &frame->retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (ka->sa.sa_flags & SA_RESTORER)
459 restorer = ka->sa.sa_restorer;
460
461 /* Set up to return from userspace. */
462 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100465 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 *
467 * WE DO NOT USE IT ANY MORE! It's only left here for historical
468 * reasons and because gdb uses it as a signature to notice
469 * signal handler stack frames.
470 */
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -0800471 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700474 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100477 regs->sp = (unsigned long)frame;
478 regs->ip = (unsigned long)ka->sa.sa_handler;
479 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800480 regs->dx = 0;
481 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100483 regs->ds = __USER_DS;
484 regs->es = __USER_DS;
485 regs->ss = __USER_DS;
486 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
David Howells283828f2006-01-18 17:44:00 -0800488 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700491static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
492 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100495 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700497 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700499 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700502 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700504 err |= __put_user(sig, &frame->sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 err |= __put_user(&frame->info, &frame->pinfo);
506 err |= __put_user(&frame->uc, &frame->puc);
507 err |= copy_siginfo_to_user(&frame->info, info);
508 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700509 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /* Create the ucontext. */
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700512 if (cpu_has_xsave)
513 err |= __put_user(UC_FP_XSTATE, &frame->uc.uc_flags);
514 else
515 err |= __put_user(0, &frame->uc.uc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 err |= __put_user(0, &frame->uc.uc_link);
517 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100518 err |= __put_user(sas_ss_flags(regs->sp),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 &frame->uc.uc_stack.ss_flags);
520 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700521 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
Ingo Molnar7e907f42008-03-06 10:33:08 +0100522 regs, set->sig[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
524 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700525 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 /* Set up to return from userspace. */
Roland McGrath6c3652e2008-01-30 13:30:42 +0100528 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (ka->sa.sa_flags & SA_RESTORER)
530 restorer = ka->sa.sa_restorer;
531 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100534 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 *
536 * WE DO NOT USE IT ANY MORE! It's only left here for historical
537 * reasons and because gdb uses it as a signature to notice
538 * signal handler stack frames.
539 */
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -0800540 err |= __put_user(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700543 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100546 regs->sp = (unsigned long)frame;
547 regs->ip = (unsigned long)ka->sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700548 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100549 regs->dx = (unsigned long)&frame->info;
550 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100552 regs->ds = __USER_DS;
553 regs->es = __USER_DS;
554 regs->ss = __USER_DS;
555 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
David Howells283828f2006-01-18 17:44:00 -0800557 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
560/*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100561 * OK, we're invoking a handler:
562 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700563static int signr_convert(int sig)
564{
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700565#ifdef CONFIG_X86_32
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700566 struct thread_info *info = current_thread_info();
567
568 if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
569 return info->exec_domain->signal_invmap[sig];
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700570#endif /* CONFIG_X86_32 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700571 return sig;
572}
573
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700574#ifdef CONFIG_X86_32
575
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700576#define is_ia32 1
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700577#define ia32_setup_frame __setup_frame
578#define ia32_setup_rt_frame __setup_rt_frame
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700579
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700580#else /* !CONFIG_X86_32 */
581
582#ifdef CONFIG_IA32_EMULATION
583#define is_ia32 test_thread_flag(TIF_IA32)
584#else /* !CONFIG_IA32_EMULATION */
585#define is_ia32 0
586#endif /* CONFIG_IA32_EMULATION */
587
588#endif /* CONFIG_X86_32 */
589
Roland McGrath7c1def12005-06-23 00:08:21 -0700590static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700591setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
592 sigset_t *set, struct pt_regs *regs)
593{
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700594 int usig = signr_convert(sig);
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700595 int ret;
596
597 /* Set up the stack frame */
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700598 if (is_ia32) {
599 if (ka->sa.sa_flags & SA_SIGINFO)
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700600 ret = ia32_setup_rt_frame(usig, ka, info, set, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700601 else
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700602 ret = ia32_setup_frame(usig, ka, set, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700603 } else
604 ret = __setup_rt_frame(sig, ka, info, set, regs);
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700605
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700606 if (ret) {
607 force_sigsegv(sig, current);
608 return -EFAULT;
609 }
610
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700611 return ret;
612}
613
614static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800616 sigset_t *oldset, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Roland McGrath7c1def12005-06-23 00:08:21 -0700618 int ret;
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700621 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700623 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800624 case -ERESTART_RESTARTBLOCK:
625 case -ERESTARTNOHAND:
626 regs->ax = -EINTR;
627 break;
628
629 case -ERESTARTSYS:
630 if (!(ka->sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100631 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800633 }
634 /* fallthrough */
635 case -ERESTARTNOINTR:
636 regs->ax = regs->orig_ax;
637 regs->ip -= 2;
638 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
640 }
641
642 /*
Roland McGrathe1f28772008-01-30 13:30:50 +0100643 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
644 * flag so that register information in the sigcontext is correct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100646 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
Roland McGrathe1f28772008-01-30 13:30:50 +0100647 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100648 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700650 ret = setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Ingo Molnar7e907f42008-03-06 10:33:08 +0100652 if (ret)
653 return ret;
Roland McGrath7c1def12005-06-23 00:08:21 -0700654
Hiroshi Shimamoto86d32372008-09-23 17:22:32 -0700655#ifdef CONFIG_X86_64
656 /*
657 * This has nothing to do with segment registers,
658 * despite the name. This magic affects uaccess.h
659 * macros' behavior. Reset it to the normal setting.
660 */
661 set_fs(USER_DS);
662#endif
663
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700664 /*
665 * Clear the direction flag as per the ABI for function entry.
666 */
667 regs->flags &= ~X86_EFLAGS_DF;
668
669 /*
670 * Clear TF when entering the signal handler, but
671 * notify any tracer that was single-stepping it.
672 * The tracer may want to single-step inside the
673 * handler too.
674 */
675 regs->flags &= ~X86_EFLAGS_TF;
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700676
Ingo Molnar7e907f42008-03-06 10:33:08 +0100677 spin_lock_irq(&current->sighand->siglock);
678 sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
679 if (!(ka->sa.sa_flags & SA_NODEFER))
680 sigaddset(&current->blocked, sig);
681 recalc_sigpending();
682 spin_unlock_irq(&current->sighand->siglock);
683
Roland McGrath36a03302008-03-14 17:46:38 -0700684 tracehook_signal_handler(sig, info, ka, regs,
685 test_thread_flag(TIF_SINGLESTEP));
686
Ingo Molnar7e907f42008-03-06 10:33:08 +0100687 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700690#ifdef CONFIG_X86_32
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700691#define NR_restart_syscall __NR_restart_syscall
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700692#else /* !CONFIG_X86_32 */
693#define NR_restart_syscall \
694 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
695#endif /* CONFIG_X86_32 */
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697/*
698 * Note that 'init' is a special process: it doesn't get signals it doesn't
699 * want to handle. Thus you cannot kill init even with a SIGKILL even by
700 * mistake.
701 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100702static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800704 struct k_sigaction ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 siginfo_t info;
706 int signr;
David Howells283828f2006-01-18 17:44:00 -0800707 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 /*
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800710 * We want the common case to go fast, which is why we may in certain
711 * cases get here from kernel mode. Just return without doing anything
712 * if so.
713 * X86_32: vm86 regs switched out by assembly code before reaching
714 * here, so testing against kernel CS suffices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 */
Vincent Hanquez717b5942005-06-23 00:08:45 -0700716 if (!user_mode(regs))
David Howells283828f2006-01-18 17:44:00 -0800717 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700719 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
David Howells283828f2006-01-18 17:44:00 -0800720 oldset = &current->saved_sigmask;
721 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 oldset = &current->blocked;
723
724 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
725 if (signr > 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100726 /*
727 * Re-enable any watchpoints before delivering the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 * signal to user space. The processor register will
729 * have been cleared if the watchpoint triggered
730 * inside the kernel.
731 */
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800732 if (current->thread.debugreg7)
Roland McGrath0f534092008-01-30 13:30:59 +0100733 set_debugreg(current->thread.debugreg7, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Ingo Molnar7e907f42008-03-06 10:33:08 +0100735 /* Whee! Actually deliver the signal. */
David Howells283828f2006-01-18 17:44:00 -0800736 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100737 /*
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700738 * A signal was successfully delivered; the saved
David Howells283828f2006-01-18 17:44:00 -0800739 * sigmask will have been stored in the signal frame,
740 * and will be restored by sigreturn, so we can simply
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700741 * clear the TS_RESTORE_SIGMASK flag.
Ingo Molnar7e907f42008-03-06 10:33:08 +0100742 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700743 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
David Howells283828f2006-01-18 17:44:00 -0800744 }
David Howells283828f2006-01-18 17:44:00 -0800745 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700749 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700751 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800752 case -ERESTARTNOHAND:
753 case -ERESTARTSYS:
754 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100755 regs->ax = regs->orig_ax;
756 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800757 break;
758
759 case -ERESTART_RESTARTBLOCK:
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700760 regs->ax = NR_restart_syscall;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100761 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800762 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764 }
David Howells283828f2006-01-18 17:44:00 -0800765
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800766 /*
767 * If there's no signal to deliver, we just put the saved sigmask
768 * back.
769 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700770 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
771 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
David Howells283828f2006-01-18 17:44:00 -0800772 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
776/*
777 * notification of userspace execution resumption
David Howells283828f2006-01-18 17:44:00 -0800778 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100780void
781do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700783#if defined(CONFIG_X86_64) && defined(CONFIG_X86_MCE)
784 /* notify userspace of pending MCEs */
785 if (thread_info_flags & _TIF_MCE_NOTIFY)
786 mce_notify_user();
787#endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 /* deal with pending signal delivery */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700790 if (thread_info_flags & _TIF_SIGPENDING)
David Howells283828f2006-01-18 17:44:00 -0800791 do_signal(regs);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100792
Roland McGrath59e52132008-04-19 19:10:57 -0700793 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
794 clear_thread_flag(TIF_NOTIFY_RESUME);
795 tracehook_notify_resume(regs);
796 }
797
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700798#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 clear_thread_flag(TIF_IRET);
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700800#endif /* CONFIG_X86_32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700802
803void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
804{
805 struct task_struct *me = current;
806
807 if (show_unhandled_signals && printk_ratelimit()) {
808 printk(KERN_INFO
809 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
810 me->comm, me->pid, where, frame,
811 regs->ip, regs->sp, regs->orig_ax);
812 print_vma_addr(" in ", regs->ip);
813 printk(KERN_CONT "\n");
814 }
815
816 force_sig(SIGSEGV, me);
817}