blob: d6dd057d0f2210784023ee1c591ed4f660e837e2 [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
Hiroshi Shimamotoa2e8d3d2008-10-02 22:09:20 -0700116#define COPY(x) { \
117 err |= __get_user(regs->x, &sc->x); \
118}
119
120#define COPY_SEG(seg) { \
121 unsigned short tmp; \
122 err |= __get_user(tmp, &sc->seg); \
123 regs->seg = tmp; \
124}
125
126#define COPY_SEG_STRICT(seg) { \
127 unsigned short tmp; \
128 err |= __get_user(tmp, &sc->seg); \
129 regs->seg = tmp | 3; \
130}
131
132#define GET_SEG(seg) { \
133 unsigned short tmp; \
134 err |= __get_user(tmp, &sc->seg); \
135 loadsegment(seg, tmp); \
136}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/*
139 * Do a signal return; undo the signal stack.
140 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static int
Harvey Harrison866bc132008-02-08 12:10:02 -0800142restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
143 unsigned long *pax)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Hiroshi Shimamoto69e13ad2008-10-02 22:18:47 -0700145 void __user *buf;
146 unsigned int tmpflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 unsigned int err = 0;
148
149 /* Always make any pending restarted system calls return -EINTR */
150 current_thread_info()->restart_block.fn = do_no_restart_syscall;
151
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100152 GET_SEG(gs);
153 COPY_SEG(fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 COPY_SEG(es);
155 COPY_SEG(ds);
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800156 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
157 COPY(dx); COPY(cx); COPY(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 COPY_SEG_STRICT(cs);
159 COPY_SEG_STRICT(ss);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100160
Hiroshi Shimamoto69e13ad2008-10-02 22:18:47 -0700161 err |= __get_user(tmpflags, &sc->flags);
162 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
163 regs->orig_ax = -1; /* disable syscall checks */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100164
Hiroshi Shimamoto69e13ad2008-10-02 22:18:47 -0700165 err |= __get_user(buf, &sc->fpstate);
166 err |= restore_i387_xstate(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Harvey Harrison866bc132008-02-08 12:10:02 -0800168 err |= __get_user(*pax, &sc->ax);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Harvey Harrison866bc132008-02-08 12:10:02 -0800172asmlinkage unsigned long sys_sigreturn(unsigned long __unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Ingo Molnar7e907f42008-03-06 10:33:08 +0100174 struct sigframe __user *frame;
175 struct pt_regs *regs;
Harvey Harrison866bc132008-02-08 12:10:02 -0800176 unsigned long ax;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100177 sigset_t set;
178
179 regs = (struct pt_regs *) &__unused;
180 frame = (struct sigframe __user *)(regs->sp - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
183 goto badframe;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100184 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 && __copy_from_user(&set.sig[1], &frame->extramask,
186 sizeof(frame->extramask))))
187 goto badframe;
188
189 sigdelsetmask(&set, ~_BLOCKABLE);
190 spin_lock_irq(&current->sighand->siglock);
191 current->blocked = set;
192 recalc_sigpending();
193 spin_unlock_irq(&current->sighand->siglock);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100194
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100195 if (restore_sigcontext(regs, &frame->sc, &ax))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 goto badframe;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100197 return ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199badframe:
Andi Kleen03252912008-01-30 13:33:18 +0100200 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamoto1181f8b2008-07-03 13:12:13 -0700201 printk("%s%s[%d] bad frame in sigreturn frame:"
Ingo Molnar97b44ae2008-03-06 10:43:17 +0100202 "%p ip:%lx sp:%lx oeax:%lx",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700203 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100204 current->comm, task_pid_nr(current), frame, regs->ip,
205 regs->sp, regs->orig_ax);
Andi Kleen03252912008-01-30 13:33:18 +0100206 print_vma_addr(" in ", regs->ip);
Ingo Molnar97b44ae2008-03-06 10:43:17 +0100207 printk(KERN_CONT "\n");
Andi Kleen03252912008-01-30 13:33:18 +0100208 }
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 force_sig(SIGSEGV, current);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return 0;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100213}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Hiroshi Shimamotoe6babb62008-09-12 17:03:31 -0700215static long do_rt_sigreturn(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Harvey Harrison2d19c452008-02-08 12:10:00 -0800217 struct rt_sigframe __user *frame;
Harvey Harrison866bc132008-02-08 12:10:02 -0800218 unsigned long ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Harvey Harrison2d19c452008-02-08 12:10:00 -0800221 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
223 goto badframe;
224 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
225 goto badframe;
226
227 sigdelsetmask(&set, ~_BLOCKABLE);
228 spin_lock_irq(&current->sighand->siglock);
229 current->blocked = set;
230 recalc_sigpending();
231 spin_unlock_irq(&current->sighand->siglock);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100232
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100233 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 goto badframe;
235
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100236 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 goto badframe;
238
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100239 return ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241badframe:
Hiroshi Shimamotoe6babb62008-09-12 17:03:31 -0700242 signal_fault(regs, frame, "rt_sigreturn");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return 0;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100244}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Hiroshi Shimamotoe6babb62008-09-12 17:03:31 -0700246asmlinkage int sys_rt_sigreturn(unsigned long __unused)
247{
248 struct pt_regs *regs = (struct pt_regs *)&__unused;
249
250 return do_rt_sigreturn(regs);
251}
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253/*
254 * Set up a signal frame.
255 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256static int
Suresh Siddhaab513702008-07-29 10:29:22 -0700257setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 struct pt_regs *regs, unsigned long mask)
259{
260 int tmp, err = 0;
261
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100262 err |= __put_user(regs->fs, (unsigned int __user *)&sc->fs);
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100263 savesegment(gs, tmp);
264 err |= __put_user(tmp, (unsigned int __user *)&sc->gs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100266 err |= __put_user(regs->es, (unsigned int __user *)&sc->es);
267 err |= __put_user(regs->ds, (unsigned int __user *)&sc->ds);
H. Peter Anvin742fa542008-01-30 13:30:56 +0100268 err |= __put_user(regs->di, &sc->di);
269 err |= __put_user(regs->si, &sc->si);
270 err |= __put_user(regs->bp, &sc->bp);
271 err |= __put_user(regs->sp, &sc->sp);
272 err |= __put_user(regs->bx, &sc->bx);
273 err |= __put_user(regs->dx, &sc->dx);
274 err |= __put_user(regs->cx, &sc->cx);
275 err |= __put_user(regs->ax, &sc->ax);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 err |= __put_user(current->thread.trap_no, &sc->trapno);
277 err |= __put_user(current->thread.error_code, &sc->err);
H. Peter Anvin742fa542008-01-30 13:30:56 +0100278 err |= __put_user(regs->ip, &sc->ip);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100279 err |= __put_user(regs->cs, (unsigned int __user *)&sc->cs);
H. Peter Anvin742fa542008-01-30 13:30:56 +0100280 err |= __put_user(regs->flags, &sc->flags);
281 err |= __put_user(regs->sp, &sc->sp_at_signal);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100282 err |= __put_user(regs->ss, (unsigned int __user *)&sc->ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Suresh Siddhaab513702008-07-29 10:29:22 -0700284 tmp = save_i387_xstate(fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (tmp < 0)
Ingo Molnar7e907f42008-03-06 10:33:08 +0100286 err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 else
Ingo Molnar7e907f42008-03-06 10:33:08 +0100288 err |= __put_user(tmp ? fpstate : NULL, &sc->fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 /* non-iBCS2 extensions.. */
291 err |= __put_user(mask, &sc->oldmask);
292 err |= __put_user(current->thread.cr2, &sc->cr2);
293
294 return err;
295}
296
297/*
298 * Determine which stack to use..
299 */
300static inline void __user *
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700301get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
Suresh Siddhaab513702008-07-29 10:29:22 -0700302 void **fpstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100304 unsigned long sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /* Default to using normal stack */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100307 sp = regs->sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Roland McGrath83bd0102008-01-30 13:30:06 +0100309 /*
310 * If we are on the alternate signal stack and would overflow it, don't.
311 * Return an always-bogus address instead so we will die with SIGSEGV.
312 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100313 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
Roland McGrath83bd0102008-01-30 13:30:06 +0100314 return (void __user *) -1L;
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /* This is the X/Open sanctioned signal stack switching. */
317 if (ka->sa.sa_flags & SA_ONSTACK) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100318 if (sas_ss_flags(sp) == 0)
319 sp = current->sas_ss_sp + current->sas_ss_size;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100320 } else {
321 /* This is the legacy signal stack switching. */
322 if ((regs->ss & 0xffff) != __USER_DS &&
323 !(ka->sa.sa_flags & SA_RESTORER) &&
324 ka->sa.sa_restorer)
325 sp = (unsigned long) ka->sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700328 if (used_math()) {
329 sp = sp - sig_xstate_size;
330 *fpstate = (struct _fpstate *) sp;
331 }
332
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100333 sp -= frame_size;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100334 /*
335 * Align the stack pointer according to the i386 ABI,
336 * i.e. so that on function entry ((sp + 4) & 15) == 0.
337 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100338 sp = ((sp + 4) & -16ul) - 4;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100339
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100340 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Ingo Molnar7e907f42008-03-06 10:33:08 +0100343static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700344__setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
345 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100348 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700350 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700352 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
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 (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700358 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700360 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700361 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700364 if (__copy_to_user(&frame->extramask, &set->sig[1],
365 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700366 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700369 if (current->mm->context.vdso)
Roland McGrath6c3652e2008-01-30 13:30:42 +0100370 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100371 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100372 restorer = &frame->retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (ka->sa.sa_flags & SA_RESTORER)
374 restorer = ka->sa.sa_restorer;
375
376 /* Set up to return from userspace. */
377 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100380 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 *
382 * WE DO NOT USE IT ANY MORE! It's only left here for historical
383 * reasons and because gdb uses it as a signature to notice
384 * signal handler stack frames.
385 */
386 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
387 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
388 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
389
390 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700391 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100394 regs->sp = (unsigned long)frame;
395 regs->ip = (unsigned long)ka->sa.sa_handler;
396 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800397 regs->dx = 0;
398 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100400 regs->ds = __USER_DS;
401 regs->es = __USER_DS;
402 regs->ss = __USER_DS;
403 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
David Howells283828f2006-01-18 17:44:00 -0800405 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700408static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
409 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100412 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700414 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700416 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700419 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700421 err |= __put_user(sig, &frame->sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 err |= __put_user(&frame->info, &frame->pinfo);
423 err |= __put_user(&frame->uc, &frame->puc);
424 err |= copy_siginfo_to_user(&frame->info, info);
425 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700426 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 /* Create the ucontext. */
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700429 if (cpu_has_xsave)
430 err |= __put_user(UC_FP_XSTATE, &frame->uc.uc_flags);
431 else
432 err |= __put_user(0, &frame->uc.uc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 err |= __put_user(0, &frame->uc.uc_link);
434 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100435 err |= __put_user(sas_ss_flags(regs->sp),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 &frame->uc.uc_stack.ss_flags);
437 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700438 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
Ingo Molnar7e907f42008-03-06 10:33:08 +0100439 regs, set->sig[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
441 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700442 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 /* Set up to return from userspace. */
Roland McGrath6c3652e2008-01-30 13:30:42 +0100445 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (ka->sa.sa_flags & SA_RESTORER)
447 restorer = ka->sa.sa_restorer;
448 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100451 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 *
453 * WE DO NOT USE IT ANY MORE! It's only left here for historical
454 * reasons and because gdb uses it as a signature to notice
455 * signal handler stack frames.
456 */
457 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
458 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
459 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
460
461 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700462 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100465 regs->sp = (unsigned long)frame;
466 regs->ip = (unsigned long)ka->sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700467 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100468 regs->dx = (unsigned long)&frame->info;
469 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100471 regs->ds = __USER_DS;
472 regs->es = __USER_DS;
473 regs->ss = __USER_DS;
474 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
David Howells283828f2006-01-18 17:44:00 -0800476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
479/*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100480 * OK, we're invoking a handler:
481 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700482static int signr_convert(int sig)
483{
484 struct thread_info *info = current_thread_info();
485
486 if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
487 return info->exec_domain->signal_invmap[sig];
488 return sig;
489}
490
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700491#define is_ia32 1
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700492#define ia32_setup_frame __setup_frame
493#define ia32_setup_rt_frame __setup_rt_frame
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700494
Roland McGrath7c1def12005-06-23 00:08:21 -0700495static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700496setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
497 sigset_t *set, struct pt_regs *regs)
498{
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700499 int usig = signr_convert(sig);
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700500 int ret;
501
502 /* Set up the stack frame */
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700503 if (is_ia32) {
504 if (ka->sa.sa_flags & SA_SIGINFO)
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700505 ret = ia32_setup_rt_frame(usig, ka, info, set, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700506 else
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700507 ret = ia32_setup_frame(usig, ka, set, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700508 } else
509 ret = __setup_rt_frame(sig, ka, info, set, regs);
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700510
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700511 if (ret) {
512 force_sigsegv(sig, current);
513 return -EFAULT;
514 }
515
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700516 return ret;
517}
518
519static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800521 sigset_t *oldset, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Roland McGrath7c1def12005-06-23 00:08:21 -0700523 int ret;
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700526 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700528 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800529 case -ERESTART_RESTARTBLOCK:
530 case -ERESTARTNOHAND:
531 regs->ax = -EINTR;
532 break;
533
534 case -ERESTARTSYS:
535 if (!(ka->sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100536 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800538 }
539 /* fallthrough */
540 case -ERESTARTNOINTR:
541 regs->ax = regs->orig_ax;
542 regs->ip -= 2;
543 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545 }
546
547 /*
Roland McGrathe1f28772008-01-30 13:30:50 +0100548 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
549 * flag so that register information in the sigcontext is correct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100551 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
Roland McGrathe1f28772008-01-30 13:30:50 +0100552 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100553 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700555 ret = setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Ingo Molnar7e907f42008-03-06 10:33:08 +0100557 if (ret)
558 return ret;
Roland McGrath7c1def12005-06-23 00:08:21 -0700559
Hiroshi Shimamoto86d32372008-09-23 17:22:32 -0700560#ifdef CONFIG_X86_64
561 /*
562 * This has nothing to do with segment registers,
563 * despite the name. This magic affects uaccess.h
564 * macros' behavior. Reset it to the normal setting.
565 */
566 set_fs(USER_DS);
567#endif
568
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700569 /*
570 * Clear the direction flag as per the ABI for function entry.
571 */
572 regs->flags &= ~X86_EFLAGS_DF;
573
574 /*
575 * Clear TF when entering the signal handler, but
576 * notify any tracer that was single-stepping it.
577 * The tracer may want to single-step inside the
578 * handler too.
579 */
580 regs->flags &= ~X86_EFLAGS_TF;
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700581
Ingo Molnar7e907f42008-03-06 10:33:08 +0100582 spin_lock_irq(&current->sighand->siglock);
583 sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
584 if (!(ka->sa.sa_flags & SA_NODEFER))
585 sigaddset(&current->blocked, sig);
586 recalc_sigpending();
587 spin_unlock_irq(&current->sighand->siglock);
588
Roland McGrath36a03302008-03-14 17:46:38 -0700589 tracehook_signal_handler(sig, info, ka, regs,
590 test_thread_flag(TIF_SINGLESTEP));
591
Ingo Molnar7e907f42008-03-06 10:33:08 +0100592 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700595#define NR_restart_syscall __NR_restart_syscall
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596/*
597 * Note that 'init' is a special process: it doesn't get signals it doesn't
598 * want to handle. Thus you cannot kill init even with a SIGKILL even by
599 * mistake.
600 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100601static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800603 struct k_sigaction ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 siginfo_t info;
605 int signr;
David Howells283828f2006-01-18 17:44:00 -0800606 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 /*
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800609 * We want the common case to go fast, which is why we may in certain
610 * cases get here from kernel mode. Just return without doing anything
611 * if so.
612 * X86_32: vm86 regs switched out by assembly code before reaching
613 * here, so testing against kernel CS suffices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 */
Vincent Hanquez717b5942005-06-23 00:08:45 -0700615 if (!user_mode(regs))
David Howells283828f2006-01-18 17:44:00 -0800616 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700618 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
David Howells283828f2006-01-18 17:44:00 -0800619 oldset = &current->saved_sigmask;
620 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 oldset = &current->blocked;
622
623 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
624 if (signr > 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100625 /*
626 * Re-enable any watchpoints before delivering the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 * signal to user space. The processor register will
628 * have been cleared if the watchpoint triggered
629 * inside the kernel.
630 */
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800631 if (current->thread.debugreg7)
Roland McGrath0f534092008-01-30 13:30:59 +0100632 set_debugreg(current->thread.debugreg7, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Ingo Molnar7e907f42008-03-06 10:33:08 +0100634 /* Whee! Actually deliver the signal. */
David Howells283828f2006-01-18 17:44:00 -0800635 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100636 /*
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700637 * A signal was successfully delivered; the saved
David Howells283828f2006-01-18 17:44:00 -0800638 * sigmask will have been stored in the signal frame,
639 * and will be restored by sigreturn, so we can simply
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700640 * clear the TS_RESTORE_SIGMASK flag.
Ingo Molnar7e907f42008-03-06 10:33:08 +0100641 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700642 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
David Howells283828f2006-01-18 17:44:00 -0800643 }
David Howells283828f2006-01-18 17:44:00 -0800644 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700648 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700650 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800651 case -ERESTARTNOHAND:
652 case -ERESTARTSYS:
653 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100654 regs->ax = regs->orig_ax;
655 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800656 break;
657
658 case -ERESTART_RESTARTBLOCK:
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700659 regs->ax = NR_restart_syscall;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100660 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800661 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663 }
David Howells283828f2006-01-18 17:44:00 -0800664
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800665 /*
666 * If there's no signal to deliver, we just put the saved sigmask
667 * back.
668 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700669 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
670 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
David Howells283828f2006-01-18 17:44:00 -0800671 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
675/*
676 * notification of userspace execution resumption
David Howells283828f2006-01-18 17:44:00 -0800677 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100679void
680do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700682#if defined(CONFIG_X86_64) && defined(CONFIG_X86_MCE)
683 /* notify userspace of pending MCEs */
684 if (thread_info_flags & _TIF_MCE_NOTIFY)
685 mce_notify_user();
686#endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /* deal with pending signal delivery */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700689 if (thread_info_flags & _TIF_SIGPENDING)
David Howells283828f2006-01-18 17:44:00 -0800690 do_signal(regs);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100691
Roland McGrath59e52132008-04-19 19:10:57 -0700692 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
693 clear_thread_flag(TIF_NOTIFY_RESUME);
694 tracehook_notify_resume(regs);
695 }
696
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700697#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 clear_thread_flag(TIF_IRET);
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700699#endif /* CONFIG_X86_32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700701
702void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
703{
704 struct task_struct *me = current;
705
706 if (show_unhandled_signals && printk_ratelimit()) {
707 printk(KERN_INFO
708 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
709 me->comm, me->pid, where, frame,
710 regs->ip, regs->sp, regs->orig_ax);
711 print_vma_addr(" in ", regs->ip);
712 printk(KERN_CONT "\n");
713 }
714
715 force_sig(SIGSEGV, me);
716}