blob: d433861a659918a277b920bfde10c8d632f290a0 [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 */
Ingo Molnar7e907f42008-03-06 10:33:08 +01007#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/personality.h>
Andi Kleen9fbbd4d2007-02-13 13:26:26 +010010#include <linux/binfmts.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010011#include <linux/suspend.h>
12#include <linux/kernel.h>
13#include <linux/ptrace.h>
14#include <linux/signal.h>
15#include <linux/stddef.h>
16#include <linux/unistd.h>
17#include <linux/errno.h>
18#include <linux/sched.h>
19#include <linux/wait.h>
Roland McGrath36a03302008-03-14 17:46:38 -070020#include <linux/tracehook.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010021#include <linux/elf.h>
22#include <linux/smp.h>
23#include <linux/mm.h>
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/processor.h>
26#include <asm/ucontext.h>
27#include <asm/uaccess.h>
28#include <asm/i387.h>
Roland McGrath6c3652e2008-01-30 13:30:42 +010029#include <asm/vdso.h>
Hiroshi Shimamotobb579252008-09-05 16:26:55 -070030#include <asm/syscall.h>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053031#include <asm/syscalls.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010032
Harvey Harrison123a6342008-02-08 12:10:00 -080033#include "sigframe.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
36
Harvey Harrison1a176802008-02-08 12:09:59 -080037#define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
38 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
39 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
40 X86_EFLAGS_CF)
41
42#ifdef CONFIG_X86_32
43# define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
44#else
45# define FIX_EFLAGS __FIX_EFLAGS
46#endif
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*
49 * Atomically swap in the new signal mask, and wait for a signal.
50 */
51asmlinkage int
52sys_sigsuspend(int history0, int history1, old_sigset_t mask)
53{
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 mask &= _BLOCKABLE;
55 spin_lock_irq(&current->sighand->siglock);
David Howells283828f2006-01-18 17:44:00 -080056 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 siginitset(&current->blocked, mask);
58 recalc_sigpending();
59 spin_unlock_irq(&current->sighand->siglock);
60
David Howells283828f2006-01-18 17:44:00 -080061 current->state = TASK_INTERRUPTIBLE;
62 schedule();
Roland McGrath5a8da0e2008-04-30 00:53:10 -070063 set_restore_sigmask();
Ingo Molnar7e907f42008-03-06 10:33:08 +010064
David Howells283828f2006-01-18 17:44:00 -080065 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
Ingo Molnar7e907f42008-03-06 10:33:08 +010068asmlinkage int
Linus Torvalds1da177e2005-04-16 15:20:36 -070069sys_sigaction(int sig, const struct old_sigaction __user *act,
70 struct old_sigaction __user *oact)
71{
72 struct k_sigaction new_ka, old_ka;
73 int ret;
74
75 if (act) {
76 old_sigset_t mask;
Ingo Molnar7e907f42008-03-06 10:33:08 +010077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
79 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
80 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
81 return -EFAULT;
Ingo Molnar7e907f42008-03-06 10:33:08 +010082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
84 __get_user(mask, &act->sa_mask);
85 siginitset(&new_ka.sa.sa_mask, mask);
86 }
87
88 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
89
90 if (!ret && oact) {
91 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
92 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
93 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
94 return -EFAULT;
Ingo Molnar7e907f42008-03-06 10:33:08 +010095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
97 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
98 }
99
100 return ret;
101}
102
Ingo Molnar7e907f42008-03-06 10:33:08 +0100103asmlinkage int sys_sigaltstack(unsigned long bx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Ingo Molnar7e907f42008-03-06 10:33:08 +0100105 /*
106 * This is needed to make gcc realize it doesn't own the
107 * "struct pt_regs"
108 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100109 struct pt_regs *regs = (struct pt_regs *)&bx;
110 const stack_t __user *uss = (const stack_t __user *)bx;
111 stack_t __user *uoss = (stack_t __user *)regs->cx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100113 return do_sigaltstack(uss, uoss, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116
117/*
118 * Do a signal return; undo the signal stack.
119 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120static int
Harvey Harrison866bc132008-02-08 12:10:02 -0800121restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
122 unsigned long *pax)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 unsigned int err = 0;
125
126 /* Always make any pending restarted system calls return -EINTR */
127 current_thread_info()->restart_block.fn = do_no_restart_syscall;
128
H. Peter Anvin742fa542008-01-30 13:30:56 +0100129#define COPY(x) err |= __get_user(regs->x, &sc->x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131#define COPY_SEG(seg) \
132 { unsigned short tmp; \
133 err |= __get_user(tmp, &sc->seg); \
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100134 regs->seg = tmp; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136#define COPY_SEG_STRICT(seg) \
137 { unsigned short tmp; \
138 err |= __get_user(tmp, &sc->seg); \
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100139 regs->seg = tmp|3; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141#define GET_SEG(seg) \
142 { unsigned short tmp; \
143 err |= __get_user(tmp, &sc->seg); \
Ingo Molnar7e907f42008-03-06 10:33:08 +0100144 loadsegment(seg, tmp); }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100146 GET_SEG(gs);
147 COPY_SEG(fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 COPY_SEG(es);
149 COPY_SEG(ds);
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800150 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
151 COPY(dx); COPY(cx); COPY(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 COPY_SEG_STRICT(cs);
153 COPY_SEG_STRICT(ss);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 {
156 unsigned int tmpflags;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100157
H. Peter Anvin742fa542008-01-30 13:30:56 +0100158 err |= __get_user(tmpflags, &sc->flags);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100159 regs->flags = (regs->flags & ~FIX_EFLAGS) |
160 (tmpflags & FIX_EFLAGS);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100161 regs->orig_ax = -1; /* disable syscall checks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
163
164 {
Suresh Siddhaab513702008-07-29 10:29:22 -0700165 void __user *buf;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 err |= __get_user(buf, &sc->fpstate);
Suresh Siddhaab513702008-07-29 10:29:22 -0700168 err |= restore_i387_xstate(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170
Harvey Harrison866bc132008-02-08 12:10:02 -0800171 err |= __get_user(*pax, &sc->ax);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Harvey Harrison866bc132008-02-08 12:10:02 -0800175asmlinkage unsigned long sys_sigreturn(unsigned long __unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Ingo Molnar7e907f42008-03-06 10:33:08 +0100177 struct sigframe __user *frame;
178 struct pt_regs *regs;
Harvey Harrison866bc132008-02-08 12:10:02 -0800179 unsigned long ax;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100180 sigset_t set;
181
182 regs = (struct pt_regs *) &__unused;
183 frame = (struct sigframe __user *)(regs->sp - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
186 goto badframe;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100187 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 && __copy_from_user(&set.sig[1], &frame->extramask,
189 sizeof(frame->extramask))))
190 goto badframe;
191
192 sigdelsetmask(&set, ~_BLOCKABLE);
193 spin_lock_irq(&current->sighand->siglock);
194 current->blocked = set;
195 recalc_sigpending();
196 spin_unlock_irq(&current->sighand->siglock);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100197
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100198 if (restore_sigcontext(regs, &frame->sc, &ax))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 goto badframe;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100200 return ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202badframe:
Andi Kleen03252912008-01-30 13:33:18 +0100203 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamoto1181f8b2008-07-03 13:12:13 -0700204 printk("%s%s[%d] bad frame in sigreturn frame:"
Ingo Molnar97b44ae2008-03-06 10:43:17 +0100205 "%p ip:%lx sp:%lx oeax:%lx",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700206 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100207 current->comm, task_pid_nr(current), frame, regs->ip,
208 regs->sp, regs->orig_ax);
Andi Kleen03252912008-01-30 13:33:18 +0100209 print_vma_addr(" in ", regs->ip);
Ingo Molnar97b44ae2008-03-06 10:43:17 +0100210 printk(KERN_CONT "\n");
Andi Kleen03252912008-01-30 13:33:18 +0100211 }
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 force_sig(SIGSEGV, current);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return 0;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100216}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218asmlinkage int sys_rt_sigreturn(unsigned long __unused)
219{
Harvey Harrison2d19c452008-02-08 12:10:00 -0800220 struct pt_regs *regs = (struct pt_regs *)&__unused;
221 struct rt_sigframe __user *frame;
Harvey Harrison866bc132008-02-08 12:10:02 -0800222 unsigned long ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Harvey Harrison2d19c452008-02-08 12:10:00 -0800225 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
227 goto badframe;
228 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
229 goto badframe;
230
231 sigdelsetmask(&set, ~_BLOCKABLE);
232 spin_lock_irq(&current->sighand->siglock);
233 current->blocked = set;
234 recalc_sigpending();
235 spin_unlock_irq(&current->sighand->siglock);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100236
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100237 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 goto badframe;
239
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100240 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goto badframe;
242
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100243 return ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245badframe:
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700246 signal_fault(regs, frame, "rt sigreturn");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return 0;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100248}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250/*
251 * Set up a signal frame.
252 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static int
Suresh Siddhaab513702008-07-29 10:29:22 -0700254setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 struct pt_regs *regs, unsigned long mask)
256{
257 int tmp, err = 0;
258
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100259 err |= __put_user(regs->fs, (unsigned int __user *)&sc->fs);
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100260 savesegment(gs, tmp);
261 err |= __put_user(tmp, (unsigned int __user *)&sc->gs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100263 err |= __put_user(regs->es, (unsigned int __user *)&sc->es);
264 err |= __put_user(regs->ds, (unsigned int __user *)&sc->ds);
H. Peter Anvin742fa542008-01-30 13:30:56 +0100265 err |= __put_user(regs->di, &sc->di);
266 err |= __put_user(regs->si, &sc->si);
267 err |= __put_user(regs->bp, &sc->bp);
268 err |= __put_user(regs->sp, &sc->sp);
269 err |= __put_user(regs->bx, &sc->bx);
270 err |= __put_user(regs->dx, &sc->dx);
271 err |= __put_user(regs->cx, &sc->cx);
272 err |= __put_user(regs->ax, &sc->ax);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 err |= __put_user(current->thread.trap_no, &sc->trapno);
274 err |= __put_user(current->thread.error_code, &sc->err);
H. Peter Anvin742fa542008-01-30 13:30:56 +0100275 err |= __put_user(regs->ip, &sc->ip);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100276 err |= __put_user(regs->cs, (unsigned int __user *)&sc->cs);
H. Peter Anvin742fa542008-01-30 13:30:56 +0100277 err |= __put_user(regs->flags, &sc->flags);
278 err |= __put_user(regs->sp, &sc->sp_at_signal);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100279 err |= __put_user(regs->ss, (unsigned int __user *)&sc->ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Suresh Siddhaab513702008-07-29 10:29:22 -0700281 tmp = save_i387_xstate(fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (tmp < 0)
Ingo Molnar7e907f42008-03-06 10:33:08 +0100283 err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 else
Ingo Molnar7e907f42008-03-06 10:33:08 +0100285 err |= __put_user(tmp ? fpstate : NULL, &sc->fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 /* non-iBCS2 extensions.. */
288 err |= __put_user(mask, &sc->oldmask);
289 err |= __put_user(current->thread.cr2, &sc->cr2);
290
291 return err;
292}
293
294/*
295 * Determine which stack to use..
296 */
297static inline void __user *
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700298get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
Suresh Siddhaab513702008-07-29 10:29:22 -0700299 void **fpstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100301 unsigned long sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 /* Default to using normal stack */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100304 sp = regs->sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Roland McGrath83bd0102008-01-30 13:30:06 +0100306 /*
307 * If we are on the alternate signal stack and would overflow it, don't.
308 * Return an always-bogus address instead so we will die with SIGSEGV.
309 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100310 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
Roland McGrath83bd0102008-01-30 13:30:06 +0100311 return (void __user *) -1L;
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /* This is the X/Open sanctioned signal stack switching. */
314 if (ka->sa.sa_flags & SA_ONSTACK) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100315 if (sas_ss_flags(sp) == 0)
316 sp = current->sas_ss_sp + current->sas_ss_size;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100317 } else {
318 /* This is the legacy signal stack switching. */
319 if ((regs->ss & 0xffff) != __USER_DS &&
320 !(ka->sa.sa_flags & SA_RESTORER) &&
321 ka->sa.sa_restorer)
322 sp = (unsigned long) ka->sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700325 if (used_math()) {
326 sp = sp - sig_xstate_size;
327 *fpstate = (struct _fpstate *) sp;
328 }
329
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100330 sp -= frame_size;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100331 /*
332 * Align the stack pointer according to the i386 ABI,
333 * i.e. so that on function entry ((sp + 4) & 15) == 0.
334 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100335 sp = ((sp + 4) & -16ul) - 4;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100336
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100337 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Ingo Molnar7e907f42008-03-06 10:33:08 +0100340static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700341__setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
342 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100345 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700347 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700349 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700352 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700354 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700355 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700357 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700358 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700361 if (__copy_to_user(&frame->extramask, &set->sig[1],
362 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700363 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700366 if (current->mm->context.vdso)
Roland McGrath6c3652e2008-01-30 13:30:42 +0100367 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100368 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100369 restorer = &frame->retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (ka->sa.sa_flags & SA_RESTORER)
371 restorer = ka->sa.sa_restorer;
372
373 /* Set up to return from userspace. */
374 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100377 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 *
379 * WE DO NOT USE IT ANY MORE! It's only left here for historical
380 * reasons and because gdb uses it as a signature to notice
381 * signal handler stack frames.
382 */
383 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
384 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
385 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
386
387 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700388 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100391 regs->sp = (unsigned long)frame;
392 regs->ip = (unsigned long)ka->sa.sa_handler;
393 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800394 regs->dx = 0;
395 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100397 regs->ds = __USER_DS;
398 regs->es = __USER_DS;
399 regs->ss = __USER_DS;
400 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
David Howells283828f2006-01-18 17:44:00 -0800402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700405static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
406 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100409 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700411 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700413 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700416 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700418 err |= __put_user(sig, &frame->sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 err |= __put_user(&frame->info, &frame->pinfo);
420 err |= __put_user(&frame->uc, &frame->puc);
421 err |= copy_siginfo_to_user(&frame->info, info);
422 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700423 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 /* Create the ucontext. */
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700426 if (cpu_has_xsave)
427 err |= __put_user(UC_FP_XSTATE, &frame->uc.uc_flags);
428 else
429 err |= __put_user(0, &frame->uc.uc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 err |= __put_user(0, &frame->uc.uc_link);
431 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100432 err |= __put_user(sas_ss_flags(regs->sp),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 &frame->uc.uc_stack.ss_flags);
434 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700435 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
Ingo Molnar7e907f42008-03-06 10:33:08 +0100436 regs, set->sig[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
438 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700439 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 /* Set up to return from userspace. */
Roland McGrath6c3652e2008-01-30 13:30:42 +0100442 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (ka->sa.sa_flags & SA_RESTORER)
444 restorer = ka->sa.sa_restorer;
445 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100448 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 *
450 * WE DO NOT USE IT ANY MORE! It's only left here for historical
451 * reasons and because gdb uses it as a signature to notice
452 * signal handler stack frames.
453 */
454 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
455 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
456 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
457
458 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700459 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100462 regs->sp = (unsigned long)frame;
463 regs->ip = (unsigned long)ka->sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700464 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100465 regs->dx = (unsigned long)&frame->info;
466 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100468 regs->ds = __USER_DS;
469 regs->es = __USER_DS;
470 regs->ss = __USER_DS;
471 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
David Howells283828f2006-01-18 17:44:00 -0800473 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476/*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100477 * OK, we're invoking a handler:
478 */
Roland McGrath7c1def12005-06-23 00:08:21 -0700479static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700480setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
481 sigset_t *set, struct pt_regs *regs)
482{
483 int ret;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700484 int usig;
485
486 usig = current_thread_info()->exec_domain
487 && current_thread_info()->exec_domain->signal_invmap
488 && sig < 32
489 ? current_thread_info()->exec_domain->signal_invmap[sig]
490 : sig;
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700491
492 /* Set up the stack frame */
493 if (ka->sa.sa_flags & SA_SIGINFO)
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700494 ret = __setup_rt_frame(usig, ka, info, set, regs);
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700495 else
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700496 ret = __setup_frame(usig, ka, set, regs);
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700497
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700498 if (ret) {
499 force_sigsegv(sig, current);
500 return -EFAULT;
501 }
502
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700503 return ret;
504}
505
506static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800508 sigset_t *oldset, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Roland McGrath7c1def12005-06-23 00:08:21 -0700510 int ret;
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700513 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700515 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800516 case -ERESTART_RESTARTBLOCK:
517 case -ERESTARTNOHAND:
518 regs->ax = -EINTR;
519 break;
520
521 case -ERESTARTSYS:
522 if (!(ka->sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100523 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800525 }
526 /* fallthrough */
527 case -ERESTARTNOINTR:
528 regs->ax = regs->orig_ax;
529 regs->ip -= 2;
530 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532 }
533
534 /*
Roland McGrathe1f28772008-01-30 13:30:50 +0100535 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
536 * flag so that register information in the sigcontext is correct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100538 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
Roland McGrathe1f28772008-01-30 13:30:50 +0100539 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100540 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700542 ret = setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Ingo Molnar7e907f42008-03-06 10:33:08 +0100544 if (ret)
545 return ret;
Roland McGrath7c1def12005-06-23 00:08:21 -0700546
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700547 /*
548 * Clear the direction flag as per the ABI for function entry.
549 */
550 regs->flags &= ~X86_EFLAGS_DF;
551
552 /*
553 * Clear TF when entering the signal handler, but
554 * notify any tracer that was single-stepping it.
555 * The tracer may want to single-step inside the
556 * handler too.
557 */
558 regs->flags &= ~X86_EFLAGS_TF;
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700559
Ingo Molnar7e907f42008-03-06 10:33:08 +0100560 spin_lock_irq(&current->sighand->siglock);
561 sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
562 if (!(ka->sa.sa_flags & SA_NODEFER))
563 sigaddset(&current->blocked, sig);
564 recalc_sigpending();
565 spin_unlock_irq(&current->sighand->siglock);
566
Roland McGrath36a03302008-03-14 17:46:38 -0700567 tracehook_signal_handler(sig, info, ka, regs,
568 test_thread_flag(TIF_SINGLESTEP));
569
Ingo Molnar7e907f42008-03-06 10:33:08 +0100570 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700573#define NR_restart_syscall __NR_restart_syscall
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574/*
575 * Note that 'init' is a special process: it doesn't get signals it doesn't
576 * want to handle. Thus you cannot kill init even with a SIGKILL even by
577 * mistake.
578 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100579static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800581 struct k_sigaction ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 siginfo_t info;
583 int signr;
David Howells283828f2006-01-18 17:44:00 -0800584 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 /*
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800587 * We want the common case to go fast, which is why we may in certain
588 * cases get here from kernel mode. Just return without doing anything
589 * if so.
590 * X86_32: vm86 regs switched out by assembly code before reaching
591 * here, so testing against kernel CS suffices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 */
Vincent Hanquez717b5942005-06-23 00:08:45 -0700593 if (!user_mode(regs))
David Howells283828f2006-01-18 17:44:00 -0800594 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700596 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
David Howells283828f2006-01-18 17:44:00 -0800597 oldset = &current->saved_sigmask;
598 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 oldset = &current->blocked;
600
601 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
602 if (signr > 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100603 /*
604 * Re-enable any watchpoints before delivering the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 * signal to user space. The processor register will
606 * have been cleared if the watchpoint triggered
607 * inside the kernel.
608 */
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800609 if (current->thread.debugreg7)
Roland McGrath0f534092008-01-30 13:30:59 +0100610 set_debugreg(current->thread.debugreg7, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Ingo Molnar7e907f42008-03-06 10:33:08 +0100612 /* Whee! Actually deliver the signal. */
David Howells283828f2006-01-18 17:44:00 -0800613 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100614 /*
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700615 * A signal was successfully delivered; the saved
David Howells283828f2006-01-18 17:44:00 -0800616 * sigmask will have been stored in the signal frame,
617 * and will be restored by sigreturn, so we can simply
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700618 * clear the TS_RESTORE_SIGMASK flag.
Ingo Molnar7e907f42008-03-06 10:33:08 +0100619 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700620 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
David Howells283828f2006-01-18 17:44:00 -0800621 }
David Howells283828f2006-01-18 17:44:00 -0800622 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700626 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700628 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800629 case -ERESTARTNOHAND:
630 case -ERESTARTSYS:
631 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100632 regs->ax = regs->orig_ax;
633 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800634 break;
635
636 case -ERESTART_RESTARTBLOCK:
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700637 regs->ax = NR_restart_syscall;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100638 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800639 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641 }
David Howells283828f2006-01-18 17:44:00 -0800642
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800643 /*
644 * If there's no signal to deliver, we just put the saved sigmask
645 * back.
646 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700647 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
648 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
David Howells283828f2006-01-18 17:44:00 -0800649 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
653/*
654 * notification of userspace execution resumption
David Howells283828f2006-01-18 17:44:00 -0800655 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100657void
658do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /* deal with pending signal delivery */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700661 if (thread_info_flags & _TIF_SIGPENDING)
David Howells283828f2006-01-18 17:44:00 -0800662 do_signal(regs);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100663
Roland McGrath59e52132008-04-19 19:10:57 -0700664 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
665 clear_thread_flag(TIF_NOTIFY_RESUME);
666 tracehook_notify_resume(regs);
667 }
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 clear_thread_flag(TIF_IRET);
670}
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700671
672void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
673{
674 struct task_struct *me = current;
675
676 if (show_unhandled_signals && printk_ratelimit()) {
677 printk(KERN_INFO
678 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
679 me->comm, me->pid, where, frame,
680 regs->ip, regs->sp, regs->orig_ax);
681 print_vma_addr(" in ", regs->ip);
682 printk(KERN_CONT "\n");
683 }
684
685 force_sig(SIGSEGV, me);
686}