blob: 19a7a5669b5b328085438b6b081459be09fe414f [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>
20#include <linux/elf.h>
21#include <linux/smp.h>
22#include <linux/mm.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/processor.h>
25#include <asm/ucontext.h>
26#include <asm/uaccess.h>
27#include <asm/i387.h>
Roland McGrath6c3652e2008-01-30 13:30:42 +010028#include <asm/vdso.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010029
Harvey Harrison123a6342008-02-08 12:10:00 -080030#include "sigframe.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
33
Harvey Harrison1a176802008-02-08 12:09:59 -080034#define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
35 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
36 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
37 X86_EFLAGS_CF)
38
39#ifdef CONFIG_X86_32
40# define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
41#else
42# define FIX_EFLAGS __FIX_EFLAGS
43#endif
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
46 * Atomically swap in the new signal mask, and wait for a signal.
47 */
48asmlinkage int
49sys_sigsuspend(int history0, int history1, old_sigset_t mask)
50{
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 mask &= _BLOCKABLE;
52 spin_lock_irq(&current->sighand->siglock);
David Howells283828f2006-01-18 17:44:00 -080053 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 siginitset(&current->blocked, mask);
55 recalc_sigpending();
56 spin_unlock_irq(&current->sighand->siglock);
57
David Howells283828f2006-01-18 17:44:00 -080058 current->state = TASK_INTERRUPTIBLE;
59 schedule();
Roland McGrath5a8da0e2008-04-30 00:53:10 -070060 set_restore_sigmask();
Ingo Molnar7e907f42008-03-06 10:33:08 +010061
David Howells283828f2006-01-18 17:44:00 -080062 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
Ingo Molnar7e907f42008-03-06 10:33:08 +010065asmlinkage int
Linus Torvalds1da177e2005-04-16 15:20:36 -070066sys_sigaction(int sig, const struct old_sigaction __user *act,
67 struct old_sigaction __user *oact)
68{
69 struct k_sigaction new_ka, old_ka;
70 int ret;
71
72 if (act) {
73 old_sigset_t mask;
Ingo Molnar7e907f42008-03-06 10:33:08 +010074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
76 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
77 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
78 return -EFAULT;
Ingo Molnar7e907f42008-03-06 10:33:08 +010079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
81 __get_user(mask, &act->sa_mask);
82 siginitset(&new_ka.sa.sa_mask, mask);
83 }
84
85 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
86
87 if (!ret && oact) {
88 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
89 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
90 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
91 return -EFAULT;
Ingo Molnar7e907f42008-03-06 10:33:08 +010092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
94 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
95 }
96
97 return ret;
98}
99
Ingo Molnar7e907f42008-03-06 10:33:08 +0100100asmlinkage int sys_sigaltstack(unsigned long bx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Ingo Molnar7e907f42008-03-06 10:33:08 +0100102 /*
103 * This is needed to make gcc realize it doesn't own the
104 * "struct pt_regs"
105 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100106 struct pt_regs *regs = (struct pt_regs *)&bx;
107 const stack_t __user *uss = (const stack_t __user *)bx;
108 stack_t __user *uoss = (stack_t __user *)regs->cx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100110 return do_sigaltstack(uss, uoss, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113
114/*
115 * Do a signal return; undo the signal stack.
116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static int
Harvey Harrison866bc132008-02-08 12:10:02 -0800118restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
119 unsigned long *pax)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 unsigned int err = 0;
122
123 /* Always make any pending restarted system calls return -EINTR */
124 current_thread_info()->restart_block.fn = do_no_restart_syscall;
125
H. Peter Anvin742fa542008-01-30 13:30:56 +0100126#define COPY(x) err |= __get_user(regs->x, &sc->x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128#define COPY_SEG(seg) \
129 { unsigned short tmp; \
130 err |= __get_user(tmp, &sc->seg); \
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100131 regs->seg = tmp; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133#define COPY_SEG_STRICT(seg) \
134 { unsigned short tmp; \
135 err |= __get_user(tmp, &sc->seg); \
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100136 regs->seg = tmp|3; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138#define GET_SEG(seg) \
139 { unsigned short tmp; \
140 err |= __get_user(tmp, &sc->seg); \
Ingo Molnar7e907f42008-03-06 10:33:08 +0100141 loadsegment(seg, tmp); }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100143 GET_SEG(gs);
144 COPY_SEG(fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 COPY_SEG(es);
146 COPY_SEG(ds);
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800147 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
148 COPY(dx); COPY(cx); COPY(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 COPY_SEG_STRICT(cs);
150 COPY_SEG_STRICT(ss);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 {
153 unsigned int tmpflags;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100154
H. Peter Anvin742fa542008-01-30 13:30:56 +0100155 err |= __get_user(tmpflags, &sc->flags);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100156 regs->flags = (regs->flags & ~FIX_EFLAGS) |
157 (tmpflags & FIX_EFLAGS);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100158 regs->orig_ax = -1; /* disable syscall checks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160
161 {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100162 struct _fpstate __user *buf;
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 err |= __get_user(buf, &sc->fpstate);
165 if (buf) {
166 if (!access_ok(VERIFY_READ, buf, sizeof(*buf)))
167 goto badframe;
168 err |= restore_i387(buf);
169 } else {
170 struct task_struct *me = current;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (used_math()) {
173 clear_fpu(me);
174 clear_used_math();
175 }
176 }
177 }
178
Harvey Harrison866bc132008-02-08 12:10:02 -0800179 err |= __get_user(*pax, &sc->ax);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return err;
181
182badframe:
183 return 1;
184}
185
Harvey Harrison866bc132008-02-08 12:10:02 -0800186asmlinkage unsigned long sys_sigreturn(unsigned long __unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Ingo Molnar7e907f42008-03-06 10:33:08 +0100188 struct sigframe __user *frame;
189 struct pt_regs *regs;
Harvey Harrison866bc132008-02-08 12:10:02 -0800190 unsigned long ax;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100191 sigset_t set;
192
193 regs = (struct pt_regs *) &__unused;
194 frame = (struct sigframe __user *)(regs->sp - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
197 goto badframe;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100198 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 && __copy_from_user(&set.sig[1], &frame->extramask,
200 sizeof(frame->extramask))))
201 goto badframe;
202
203 sigdelsetmask(&set, ~_BLOCKABLE);
204 spin_lock_irq(&current->sighand->siglock);
205 current->blocked = set;
206 recalc_sigpending();
207 spin_unlock_irq(&current->sighand->siglock);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100208
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100209 if (restore_sigcontext(regs, &frame->sc, &ax))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 goto badframe;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100211 return ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213badframe:
Andi Kleen03252912008-01-30 13:33:18 +0100214 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamoto1181f8b2008-07-03 13:12:13 -0700215 printk("%s%s[%d] bad frame in sigreturn frame:"
Ingo Molnar97b44ae2008-03-06 10:43:17 +0100216 "%p ip:%lx sp:%lx oeax:%lx",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700217 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100218 current->comm, task_pid_nr(current), frame, regs->ip,
219 regs->sp, regs->orig_ax);
Andi Kleen03252912008-01-30 13:33:18 +0100220 print_vma_addr(" in ", regs->ip);
Ingo Molnar97b44ae2008-03-06 10:43:17 +0100221 printk(KERN_CONT "\n");
Andi Kleen03252912008-01-30 13:33:18 +0100222 }
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 force_sig(SIGSEGV, current);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return 0;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100227}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229asmlinkage int sys_rt_sigreturn(unsigned long __unused)
230{
Harvey Harrison2d19c452008-02-08 12:10:00 -0800231 struct pt_regs *regs = (struct pt_regs *)&__unused;
232 struct rt_sigframe __user *frame;
Harvey Harrison866bc132008-02-08 12:10:02 -0800233 unsigned long ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Harvey Harrison2d19c452008-02-08 12:10:00 -0800236 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
238 goto badframe;
239 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
240 goto badframe;
241
242 sigdelsetmask(&set, ~_BLOCKABLE);
243 spin_lock_irq(&current->sighand->siglock);
244 current->blocked = set;
245 recalc_sigpending();
246 spin_unlock_irq(&current->sighand->siglock);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100247
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100248 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 goto badframe;
250
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100251 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 goto badframe;
253
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100254 return ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256badframe:
257 force_sig(SIGSEGV, current);
258 return 0;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100259}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261/*
262 * Set up a signal frame.
263 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264static int
265setup_sigcontext(struct sigcontext __user *sc, struct _fpstate __user *fpstate,
266 struct pt_regs *regs, unsigned long mask)
267{
268 int tmp, err = 0;
269
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100270 err |= __put_user(regs->fs, (unsigned int __user *)&sc->fs);
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100271 savesegment(gs, tmp);
272 err |= __put_user(tmp, (unsigned int __user *)&sc->gs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100274 err |= __put_user(regs->es, (unsigned int __user *)&sc->es);
275 err |= __put_user(regs->ds, (unsigned int __user *)&sc->ds);
H. Peter Anvin742fa542008-01-30 13:30:56 +0100276 err |= __put_user(regs->di, &sc->di);
277 err |= __put_user(regs->si, &sc->si);
278 err |= __put_user(regs->bp, &sc->bp);
279 err |= __put_user(regs->sp, &sc->sp);
280 err |= __put_user(regs->bx, &sc->bx);
281 err |= __put_user(regs->dx, &sc->dx);
282 err |= __put_user(regs->cx, &sc->cx);
283 err |= __put_user(regs->ax, &sc->ax);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 err |= __put_user(current->thread.trap_no, &sc->trapno);
285 err |= __put_user(current->thread.error_code, &sc->err);
H. Peter Anvin742fa542008-01-30 13:30:56 +0100286 err |= __put_user(regs->ip, &sc->ip);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100287 err |= __put_user(regs->cs, (unsigned int __user *)&sc->cs);
H. Peter Anvin742fa542008-01-30 13:30:56 +0100288 err |= __put_user(regs->flags, &sc->flags);
289 err |= __put_user(regs->sp, &sc->sp_at_signal);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100290 err |= __put_user(regs->ss, (unsigned int __user *)&sc->ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 tmp = save_i387(fpstate);
293 if (tmp < 0)
Ingo Molnar7e907f42008-03-06 10:33:08 +0100294 err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 else
Ingo Molnar7e907f42008-03-06 10:33:08 +0100296 err |= __put_user(tmp ? fpstate : NULL, &sc->fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 /* non-iBCS2 extensions.. */
299 err |= __put_user(mask, &sc->oldmask);
300 err |= __put_user(current->thread.cr2, &sc->cr2);
301
302 return err;
303}
304
305/*
306 * Determine which stack to use..
307 */
308static inline void __user *
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700309get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
310 struct _fpstate **fpstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100312 unsigned long sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /* Default to using normal stack */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100315 sp = regs->sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Roland McGrath83bd0102008-01-30 13:30:06 +0100317 /*
318 * If we are on the alternate signal stack and would overflow it, don't.
319 * Return an always-bogus address instead so we will die with SIGSEGV.
320 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100321 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
Roland McGrath83bd0102008-01-30 13:30:06 +0100322 return (void __user *) -1L;
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /* This is the X/Open sanctioned signal stack switching. */
325 if (ka->sa.sa_flags & SA_ONSTACK) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100326 if (sas_ss_flags(sp) == 0)
327 sp = current->sas_ss_sp + current->sas_ss_size;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100328 } else {
329 /* This is the legacy signal stack switching. */
330 if ((regs->ss & 0xffff) != __USER_DS &&
331 !(ka->sa.sa_flags & SA_RESTORER) &&
332 ka->sa.sa_restorer)
333 sp = (unsigned long) ka->sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700336 if (used_math()) {
337 sp = sp - sig_xstate_size;
338 *fpstate = (struct _fpstate *) sp;
339 }
340
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100341 sp -= frame_size;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100342 /*
343 * Align the stack pointer according to the i386 ABI,
344 * i.e. so that on function entry ((sp + 4) & 15) == 0.
345 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100346 sp = ((sp + 4) & -16ul) - 4;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100347
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100348 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Ingo Molnar7e907f42008-03-06 10:33:08 +0100351static int
352setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
353 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100356 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 int err = 0;
358 int usig;
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700359 struct _fpstate __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700361 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
364 goto give_sigsegv;
365
366 usig = current_thread_info()->exec_domain
367 && current_thread_info()->exec_domain->signal_invmap
368 && sig < 32
369 ? current_thread_info()->exec_domain->signal_invmap[sig]
370 : sig;
371
372 err = __put_user(usig, &frame->sig);
373 if (err)
374 goto give_sigsegv;
375
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700376 err = setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (err)
378 goto give_sigsegv;
379
380 if (_NSIG_WORDS > 1) {
381 err = __copy_to_user(&frame->extramask, &set->sig[1],
382 sizeof(frame->extramask));
383 if (err)
384 goto give_sigsegv;
385 }
386
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700387 if (current->mm->context.vdso)
Roland McGrath6c3652e2008-01-30 13:30:42 +0100388 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100389 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100390 restorer = &frame->retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (ka->sa.sa_flags & SA_RESTORER)
392 restorer = ka->sa.sa_restorer;
393
394 /* Set up to return from userspace. */
395 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100398 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 *
400 * WE DO NOT USE IT ANY MORE! It's only left here for historical
401 * reasons and because gdb uses it as a signature to notice
402 * signal handler stack frames.
403 */
404 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
405 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
406 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
407
408 if (err)
409 goto give_sigsegv;
410
411 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100412 regs->sp = (unsigned long)frame;
413 regs->ip = (unsigned long)ka->sa.sa_handler;
414 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800415 regs->dx = 0;
416 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100418 regs->ds = __USER_DS;
419 regs->es = __USER_DS;
420 regs->ss = __USER_DS;
421 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
David Howells283828f2006-01-18 17:44:00 -0800423 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425give_sigsegv:
426 force_sigsegv(sig, current);
David Howells283828f2006-01-18 17:44:00 -0800427 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Roland McGrath7c1def12005-06-23 00:08:21 -0700430static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Ingo Molnar7e907f42008-03-06 10:33:08 +0100431 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100434 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 int err = 0;
436 int usig;
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700437 struct _fpstate __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700439 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
442 goto give_sigsegv;
443
444 usig = current_thread_info()->exec_domain
445 && current_thread_info()->exec_domain->signal_invmap
446 && sig < 32
447 ? current_thread_info()->exec_domain->signal_invmap[sig]
448 : sig;
449
450 err |= __put_user(usig, &frame->sig);
451 err |= __put_user(&frame->info, &frame->pinfo);
452 err |= __put_user(&frame->uc, &frame->puc);
453 err |= copy_siginfo_to_user(&frame->info, info);
454 if (err)
455 goto give_sigsegv;
456
457 /* Create the ucontext. */
458 err |= __put_user(0, &frame->uc.uc_flags);
459 err |= __put_user(0, &frame->uc.uc_link);
460 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100461 err |= __put_user(sas_ss_flags(regs->sp),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 &frame->uc.uc_stack.ss_flags);
463 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700464 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
Ingo Molnar7e907f42008-03-06 10:33:08 +0100465 regs, set->sig[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
467 if (err)
468 goto give_sigsegv;
469
470 /* Set up to return from userspace. */
Roland McGrath6c3652e2008-01-30 13:30:42 +0100471 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (ka->sa.sa_flags & SA_RESTORER)
473 restorer = ka->sa.sa_restorer;
474 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100477 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 *
479 * WE DO NOT USE IT ANY MORE! It's only left here for historical
480 * reasons and because gdb uses it as a signature to notice
481 * signal handler stack frames.
482 */
483 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
484 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
485 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
486
487 if (err)
488 goto give_sigsegv;
489
490 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100491 regs->sp = (unsigned long)frame;
492 regs->ip = (unsigned long)ka->sa.sa_handler;
493 regs->ax = (unsigned long)usig;
494 regs->dx = (unsigned long)&frame->info;
495 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100497 regs->ds = __USER_DS;
498 regs->es = __USER_DS;
499 regs->ss = __USER_DS;
500 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
David Howells283828f2006-01-18 17:44:00 -0800502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504give_sigsegv:
505 force_sigsegv(sig, current);
David Howells283828f2006-01-18 17:44:00 -0800506 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
509/*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100510 * OK, we're invoking a handler:
511 */
Roland McGrath7c1def12005-06-23 00:08:21 -0700512static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800514 sigset_t *oldset, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Roland McGrath7c1def12005-06-23 00:08:21 -0700516 int ret;
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /* Are we from a system call? */
Harvey Harrison9902a702008-02-08 12:09:57 -0800519 if ((long)regs->orig_ax >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* If so, check system call restarting.. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100521 switch (regs->ax) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800522 case -ERESTART_RESTARTBLOCK:
523 case -ERESTARTNOHAND:
524 regs->ax = -EINTR;
525 break;
526
527 case -ERESTARTSYS:
528 if (!(ka->sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100529 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800531 }
532 /* fallthrough */
533 case -ERESTARTNOINTR:
534 regs->ax = regs->orig_ax;
535 regs->ip -= 2;
536 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538 }
539
540 /*
Roland McGrathe1f28772008-01-30 13:30:50 +0100541 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
542 * flag so that register information in the sigcontext is correct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100544 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
Roland McGrathe1f28772008-01-30 13:30:50 +0100545 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100546 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 /* Set up the stack frame */
549 if (ka->sa.sa_flags & SA_SIGINFO)
Roland McGrath7c1def12005-06-23 00:08:21 -0700550 ret = setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 else
Roland McGrath7c1def12005-06-23 00:08:21 -0700552 ret = setup_frame(sig, ka, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Ingo Molnar7e907f42008-03-06 10:33:08 +0100554 if (ret)
555 return ret;
Roland McGrath7c1def12005-06-23 00:08:21 -0700556
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700557 /*
558 * Clear the direction flag as per the ABI for function entry.
559 */
560 regs->flags &= ~X86_EFLAGS_DF;
561
562 /*
563 * Clear TF when entering the signal handler, but
564 * notify any tracer that was single-stepping it.
565 * The tracer may want to single-step inside the
566 * handler too.
567 */
568 regs->flags &= ~X86_EFLAGS_TF;
569 if (test_thread_flag(TIF_SINGLESTEP))
570 ptrace_notify(SIGTRAP);
571
Ingo Molnar7e907f42008-03-06 10:33:08 +0100572 spin_lock_irq(&current->sighand->siglock);
573 sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
574 if (!(ka->sa.sa_flags & SA_NODEFER))
575 sigaddset(&current->blocked, sig);
576 recalc_sigpending();
577 spin_unlock_irq(&current->sighand->siglock);
578
579 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
582/*
583 * Note that 'init' is a special process: it doesn't get signals it doesn't
584 * want to handle. Thus you cannot kill init even with a SIGKILL even by
585 * mistake.
586 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100587static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800589 struct k_sigaction ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 siginfo_t info;
591 int signr;
David Howells283828f2006-01-18 17:44:00 -0800592 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /*
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800595 * We want the common case to go fast, which is why we may in certain
596 * cases get here from kernel mode. Just return without doing anything
597 * if so.
598 * X86_32: vm86 regs switched out by assembly code before reaching
599 * here, so testing against kernel CS suffices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
Vincent Hanquez717b5942005-06-23 00:08:45 -0700601 if (!user_mode(regs))
David Howells283828f2006-01-18 17:44:00 -0800602 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700604 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
David Howells283828f2006-01-18 17:44:00 -0800605 oldset = &current->saved_sigmask;
606 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 oldset = &current->blocked;
608
609 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
610 if (signr > 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100611 /*
612 * Re-enable any watchpoints before delivering the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 * signal to user space. The processor register will
614 * have been cleared if the watchpoint triggered
615 * inside the kernel.
616 */
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800617 if (current->thread.debugreg7)
Roland McGrath0f534092008-01-30 13:30:59 +0100618 set_debugreg(current->thread.debugreg7, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Ingo Molnar7e907f42008-03-06 10:33:08 +0100620 /* Whee! Actually deliver the signal. */
David Howells283828f2006-01-18 17:44:00 -0800621 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100622 /*
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700623 * A signal was successfully delivered; the saved
David Howells283828f2006-01-18 17:44:00 -0800624 * sigmask will have been stored in the signal frame,
625 * and will be restored by sigreturn, so we can simply
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700626 * clear the TS_RESTORE_SIGMASK flag.
Ingo Molnar7e907f42008-03-06 10:33:08 +0100627 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700628 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
David Howells283828f2006-01-18 17:44:00 -0800629 }
David Howells283828f2006-01-18 17:44:00 -0800630 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /* Did we come from a system call? */
Harvey Harrison9902a702008-02-08 12:09:57 -0800634 if ((long)regs->orig_ax >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /* Restart the system call - no handlers present */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100636 switch (regs->ax) {
David Howells283828f2006-01-18 17:44:00 -0800637 case -ERESTARTNOHAND:
638 case -ERESTARTSYS:
639 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100640 regs->ax = regs->orig_ax;
641 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800642 break;
643
644 case -ERESTART_RESTARTBLOCK:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100645 regs->ax = __NR_restart_syscall;
646 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800647 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649 }
David Howells283828f2006-01-18 17:44:00 -0800650
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800651 /*
652 * If there's no signal to deliver, we just put the saved sigmask
653 * back.
654 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700655 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
656 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
David Howells283828f2006-01-18 17:44:00 -0800657 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
661/*
662 * notification of userspace execution resumption
David Howells283828f2006-01-18 17:44:00 -0800663 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100665void
666do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 /* deal with pending signal delivery */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700669 if (thread_info_flags & _TIF_SIGPENDING)
David Howells283828f2006-01-18 17:44:00 -0800670 do_signal(regs);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 clear_thread_flag(TIF_IRET);
673}