blob: 2a2435d3037d9ac9dd8fab949c3c57c775e426e4 [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>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053030#include <asm/syscalls.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010031
Harvey Harrison123a6342008-02-08 12:10:00 -080032#include "sigframe.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
35
Harvey Harrison1a176802008-02-08 12:09:59 -080036#define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
37 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
38 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
39 X86_EFLAGS_CF)
40
41#ifdef CONFIG_X86_32
42# define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
43#else
44# define FIX_EFLAGS __FIX_EFLAGS
45#endif
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
48 * Atomically swap in the new signal mask, and wait for a signal.
49 */
50asmlinkage int
51sys_sigsuspend(int history0, int history1, old_sigset_t mask)
52{
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 mask &= _BLOCKABLE;
54 spin_lock_irq(&current->sighand->siglock);
David Howells283828f2006-01-18 17:44:00 -080055 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 siginitset(&current->blocked, mask);
57 recalc_sigpending();
58 spin_unlock_irq(&current->sighand->siglock);
59
David Howells283828f2006-01-18 17:44:00 -080060 current->state = TASK_INTERRUPTIBLE;
61 schedule();
Roland McGrath5a8da0e2008-04-30 00:53:10 -070062 set_restore_sigmask();
Ingo Molnar7e907f42008-03-06 10:33:08 +010063
David Howells283828f2006-01-18 17:44:00 -080064 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
Ingo Molnar7e907f42008-03-06 10:33:08 +010067asmlinkage int
Linus Torvalds1da177e2005-04-16 15:20:36 -070068sys_sigaction(int sig, const struct old_sigaction __user *act,
69 struct old_sigaction __user *oact)
70{
71 struct k_sigaction new_ka, old_ka;
72 int ret;
73
74 if (act) {
75 old_sigset_t mask;
Ingo Molnar7e907f42008-03-06 10:33:08 +010076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
78 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
79 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
80 return -EFAULT;
Ingo Molnar7e907f42008-03-06 10:33:08 +010081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
83 __get_user(mask, &act->sa_mask);
84 siginitset(&new_ka.sa.sa_mask, mask);
85 }
86
87 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
88
89 if (!ret && oact) {
90 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
91 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
92 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
93 return -EFAULT;
Ingo Molnar7e907f42008-03-06 10:33:08 +010094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
96 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
97 }
98
99 return ret;
100}
101
Ingo Molnar7e907f42008-03-06 10:33:08 +0100102asmlinkage int sys_sigaltstack(unsigned long bx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Ingo Molnar7e907f42008-03-06 10:33:08 +0100104 /*
105 * This is needed to make gcc realize it doesn't own the
106 * "struct pt_regs"
107 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100108 struct pt_regs *regs = (struct pt_regs *)&bx;
109 const stack_t __user *uss = (const stack_t __user *)bx;
110 stack_t __user *uoss = (stack_t __user *)regs->cx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100112 return do_sigaltstack(uss, uoss, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
115
116/*
117 * Do a signal return; undo the signal stack.
118 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static int
Harvey Harrison866bc132008-02-08 12:10:02 -0800120restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
121 unsigned long *pax)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 unsigned int err = 0;
124
125 /* Always make any pending restarted system calls return -EINTR */
126 current_thread_info()->restart_block.fn = do_no_restart_syscall;
127
H. Peter Anvin742fa542008-01-30 13:30:56 +0100128#define COPY(x) err |= __get_user(regs->x, &sc->x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130#define COPY_SEG(seg) \
131 { unsigned short tmp; \
132 err |= __get_user(tmp, &sc->seg); \
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100133 regs->seg = tmp; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135#define COPY_SEG_STRICT(seg) \
136 { unsigned short tmp; \
137 err |= __get_user(tmp, &sc->seg); \
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100138 regs->seg = tmp|3; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140#define GET_SEG(seg) \
141 { unsigned short tmp; \
142 err |= __get_user(tmp, &sc->seg); \
Ingo Molnar7e907f42008-03-06 10:33:08 +0100143 loadsegment(seg, tmp); }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100145 GET_SEG(gs);
146 COPY_SEG(fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 COPY_SEG(es);
148 COPY_SEG(ds);
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800149 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
150 COPY(dx); COPY(cx); COPY(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 COPY_SEG_STRICT(cs);
152 COPY_SEG_STRICT(ss);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 {
155 unsigned int tmpflags;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100156
H. Peter Anvin742fa542008-01-30 13:30:56 +0100157 err |= __get_user(tmpflags, &sc->flags);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100158 regs->flags = (regs->flags & ~FIX_EFLAGS) |
159 (tmpflags & FIX_EFLAGS);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100160 regs->orig_ax = -1; /* disable syscall checks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162
163 {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100164 struct _fpstate __user *buf;
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 err |= __get_user(buf, &sc->fpstate);
167 if (buf) {
168 if (!access_ok(VERIFY_READ, buf, sizeof(*buf)))
169 goto badframe;
170 err |= restore_i387(buf);
171 } else {
172 struct task_struct *me = current;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (used_math()) {
175 clear_fpu(me);
176 clear_used_math();
177 }
178 }
179 }
180
Harvey Harrison866bc132008-02-08 12:10:02 -0800181 err |= __get_user(*pax, &sc->ax);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return err;
183
184badframe:
185 return 1;
186}
187
Harvey Harrison866bc132008-02-08 12:10:02 -0800188asmlinkage unsigned long sys_sigreturn(unsigned long __unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Ingo Molnar7e907f42008-03-06 10:33:08 +0100190 struct sigframe __user *frame;
191 struct pt_regs *regs;
Harvey Harrison866bc132008-02-08 12:10:02 -0800192 unsigned long ax;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100193 sigset_t set;
194
195 regs = (struct pt_regs *) &__unused;
196 frame = (struct sigframe __user *)(regs->sp - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
199 goto badframe;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100200 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 && __copy_from_user(&set.sig[1], &frame->extramask,
202 sizeof(frame->extramask))))
203 goto badframe;
204
205 sigdelsetmask(&set, ~_BLOCKABLE);
206 spin_lock_irq(&current->sighand->siglock);
207 current->blocked = set;
208 recalc_sigpending();
209 spin_unlock_irq(&current->sighand->siglock);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100210
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100211 if (restore_sigcontext(regs, &frame->sc, &ax))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 goto badframe;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100213 return ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215badframe:
Andi Kleen03252912008-01-30 13:33:18 +0100216 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamoto1181f8b2008-07-03 13:12:13 -0700217 printk("%s%s[%d] bad frame in sigreturn frame:"
Ingo Molnar97b44ae2008-03-06 10:43:17 +0100218 "%p ip:%lx sp:%lx oeax:%lx",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700219 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100220 current->comm, task_pid_nr(current), frame, regs->ip,
221 regs->sp, regs->orig_ax);
Andi Kleen03252912008-01-30 13:33:18 +0100222 print_vma_addr(" in ", regs->ip);
Ingo Molnar97b44ae2008-03-06 10:43:17 +0100223 printk(KERN_CONT "\n");
Andi Kleen03252912008-01-30 13:33:18 +0100224 }
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 force_sig(SIGSEGV, current);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return 0;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100229}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231asmlinkage int sys_rt_sigreturn(unsigned long __unused)
232{
Harvey Harrison2d19c452008-02-08 12:10:00 -0800233 struct pt_regs *regs = (struct pt_regs *)&__unused;
234 struct rt_sigframe __user *frame;
Harvey Harrison866bc132008-02-08 12:10:02 -0800235 unsigned long ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Harvey Harrison2d19c452008-02-08 12:10:00 -0800238 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
240 goto badframe;
241 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
242 goto badframe;
243
244 sigdelsetmask(&set, ~_BLOCKABLE);
245 spin_lock_irq(&current->sighand->siglock);
246 current->blocked = set;
247 recalc_sigpending();
248 spin_unlock_irq(&current->sighand->siglock);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100249
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100250 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 goto badframe;
252
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100253 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 goto badframe;
255
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100256 return ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258badframe:
259 force_sig(SIGSEGV, current);
260 return 0;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100261}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263/*
264 * Set up a signal frame.
265 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266static int
267setup_sigcontext(struct sigcontext __user *sc, struct _fpstate __user *fpstate,
268 struct pt_regs *regs, unsigned long mask)
269{
270 int tmp, err = 0;
271
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100272 err |= __put_user(regs->fs, (unsigned int __user *)&sc->fs);
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100273 savesegment(gs, tmp);
274 err |= __put_user(tmp, (unsigned int __user *)&sc->gs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100276 err |= __put_user(regs->es, (unsigned int __user *)&sc->es);
277 err |= __put_user(regs->ds, (unsigned int __user *)&sc->ds);
H. Peter Anvin742fa542008-01-30 13:30:56 +0100278 err |= __put_user(regs->di, &sc->di);
279 err |= __put_user(regs->si, &sc->si);
280 err |= __put_user(regs->bp, &sc->bp);
281 err |= __put_user(regs->sp, &sc->sp);
282 err |= __put_user(regs->bx, &sc->bx);
283 err |= __put_user(regs->dx, &sc->dx);
284 err |= __put_user(regs->cx, &sc->cx);
285 err |= __put_user(regs->ax, &sc->ax);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 err |= __put_user(current->thread.trap_no, &sc->trapno);
287 err |= __put_user(current->thread.error_code, &sc->err);
H. Peter Anvin742fa542008-01-30 13:30:56 +0100288 err |= __put_user(regs->ip, &sc->ip);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100289 err |= __put_user(regs->cs, (unsigned int __user *)&sc->cs);
H. Peter Anvin742fa542008-01-30 13:30:56 +0100290 err |= __put_user(regs->flags, &sc->flags);
291 err |= __put_user(regs->sp, &sc->sp_at_signal);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100292 err |= __put_user(regs->ss, (unsigned int __user *)&sc->ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 tmp = save_i387(fpstate);
295 if (tmp < 0)
Ingo Molnar7e907f42008-03-06 10:33:08 +0100296 err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 else
Ingo Molnar7e907f42008-03-06 10:33:08 +0100298 err |= __put_user(tmp ? fpstate : NULL, &sc->fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 /* non-iBCS2 extensions.. */
301 err |= __put_user(mask, &sc->oldmask);
302 err |= __put_user(current->thread.cr2, &sc->cr2);
303
304 return err;
305}
306
307/*
308 * Determine which stack to use..
309 */
310static inline void __user *
Ingo Molnar7e907f42008-03-06 10:33:08 +0100311get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100313 unsigned long sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 /* Default to using normal stack */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100316 sp = regs->sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Roland McGrath83bd0102008-01-30 13:30:06 +0100318 /*
319 * If we are on the alternate signal stack and would overflow it, don't.
320 * Return an always-bogus address instead so we will die with SIGSEGV.
321 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100322 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
Roland McGrath83bd0102008-01-30 13:30:06 +0100323 return (void __user *) -1L;
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /* This is the X/Open sanctioned signal stack switching. */
326 if (ka->sa.sa_flags & SA_ONSTACK) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100327 if (sas_ss_flags(sp) == 0)
328 sp = current->sas_ss_sp + current->sas_ss_size;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100329 } else {
330 /* This is the legacy signal stack switching. */
331 if ((regs->ss & 0xffff) != __USER_DS &&
332 !(ka->sa.sa_flags & SA_RESTORER) &&
333 ka->sa.sa_restorer)
334 sp = (unsigned long) ka->sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100337 sp -= frame_size;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100338 /*
339 * Align the stack pointer according to the i386 ABI,
340 * i.e. so that on function entry ((sp + 4) & 15) == 0.
341 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100342 sp = ((sp + 4) & -16ul) - 4;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100343
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100344 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Ingo Molnar7e907f42008-03-06 10:33:08 +0100347static int
348setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
349 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100352 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 int err = 0;
354 int usig;
355
356 frame = get_sigframe(ka, regs, sizeof(*frame));
357
358 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
359 goto give_sigsegv;
360
361 usig = current_thread_info()->exec_domain
362 && current_thread_info()->exec_domain->signal_invmap
363 && sig < 32
364 ? current_thread_info()->exec_domain->signal_invmap[sig]
365 : sig;
366
367 err = __put_user(usig, &frame->sig);
368 if (err)
369 goto give_sigsegv;
370
371 err = setup_sigcontext(&frame->sc, &frame->fpstate, regs, set->sig[0]);
372 if (err)
373 goto give_sigsegv;
374
375 if (_NSIG_WORDS > 1) {
376 err = __copy_to_user(&frame->extramask, &set->sig[1],
377 sizeof(frame->extramask));
378 if (err)
379 goto give_sigsegv;
380 }
381
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700382 if (current->mm->context.vdso)
Roland McGrath6c3652e2008-01-30 13:30:42 +0100383 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100384 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100385 restorer = &frame->retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (ka->sa.sa_flags & SA_RESTORER)
387 restorer = ka->sa.sa_restorer;
388
389 /* Set up to return from userspace. */
390 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100393 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 *
395 * WE DO NOT USE IT ANY MORE! It's only left here for historical
396 * reasons and because gdb uses it as a signature to notice
397 * signal handler stack frames.
398 */
399 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
400 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
401 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
402
403 if (err)
404 goto give_sigsegv;
405
406 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100407 regs->sp = (unsigned long)frame;
408 regs->ip = (unsigned long)ka->sa.sa_handler;
409 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800410 regs->dx = 0;
411 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100413 regs->ds = __USER_DS;
414 regs->es = __USER_DS;
415 regs->ss = __USER_DS;
416 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
David Howells283828f2006-01-18 17:44:00 -0800418 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420give_sigsegv:
421 force_sigsegv(sig, current);
David Howells283828f2006-01-18 17:44:00 -0800422 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Roland McGrath7c1def12005-06-23 00:08:21 -0700425static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Ingo Molnar7e907f42008-03-06 10:33:08 +0100426 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100429 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 int err = 0;
431 int usig;
432
433 frame = get_sigframe(ka, regs, sizeof(*frame));
434
435 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
436 goto give_sigsegv;
437
438 usig = current_thread_info()->exec_domain
439 && current_thread_info()->exec_domain->signal_invmap
440 && sig < 32
441 ? current_thread_info()->exec_domain->signal_invmap[sig]
442 : sig;
443
444 err |= __put_user(usig, &frame->sig);
445 err |= __put_user(&frame->info, &frame->pinfo);
446 err |= __put_user(&frame->uc, &frame->puc);
447 err |= copy_siginfo_to_user(&frame->info, info);
448 if (err)
449 goto give_sigsegv;
450
451 /* Create the ucontext. */
452 err |= __put_user(0, &frame->uc.uc_flags);
453 err |= __put_user(0, &frame->uc.uc_link);
454 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100455 err |= __put_user(sas_ss_flags(regs->sp),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 &frame->uc.uc_stack.ss_flags);
457 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
458 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
Ingo Molnar7e907f42008-03-06 10:33:08 +0100459 regs, set->sig[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
461 if (err)
462 goto give_sigsegv;
463
464 /* Set up to return from userspace. */
Roland McGrath6c3652e2008-01-30 13:30:42 +0100465 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (ka->sa.sa_flags & SA_RESTORER)
467 restorer = ka->sa.sa_restorer;
468 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100471 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 *
473 * WE DO NOT USE IT ANY MORE! It's only left here for historical
474 * reasons and because gdb uses it as a signature to notice
475 * signal handler stack frames.
476 */
477 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
478 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
479 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
480
481 if (err)
482 goto give_sigsegv;
483
484 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100485 regs->sp = (unsigned long)frame;
486 regs->ip = (unsigned long)ka->sa.sa_handler;
487 regs->ax = (unsigned long)usig;
488 regs->dx = (unsigned long)&frame->info;
489 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100491 regs->ds = __USER_DS;
492 regs->es = __USER_DS;
493 regs->ss = __USER_DS;
494 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
David Howells283828f2006-01-18 17:44:00 -0800496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498give_sigsegv:
499 force_sigsegv(sig, current);
David Howells283828f2006-01-18 17:44:00 -0800500 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
503/*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100504 * OK, we're invoking a handler:
505 */
Roland McGrath7c1def12005-06-23 00:08:21 -0700506static 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? */
Harvey Harrison9902a702008-02-08 12:09:57 -0800513 if ((long)regs->orig_ax >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /* If so, check system call restarting.. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100515 switch (regs->ax) {
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
542 /* Set up the stack frame */
543 if (ka->sa.sa_flags & SA_SIGINFO)
Roland McGrath7c1def12005-06-23 00:08:21 -0700544 ret = setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 else
Roland McGrath7c1def12005-06-23 00:08:21 -0700546 ret = setup_frame(sig, ka, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Ingo Molnar7e907f42008-03-06 10:33:08 +0100548 if (ret)
549 return ret;
Roland McGrath7c1def12005-06-23 00:08:21 -0700550
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700551 /*
552 * Clear the direction flag as per the ABI for function entry.
553 */
554 regs->flags &= ~X86_EFLAGS_DF;
555
556 /*
557 * Clear TF when entering the signal handler, but
558 * notify any tracer that was single-stepping it.
559 * The tracer may want to single-step inside the
560 * handler too.
561 */
562 regs->flags &= ~X86_EFLAGS_TF;
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700563
Ingo Molnar7e907f42008-03-06 10:33:08 +0100564 spin_lock_irq(&current->sighand->siglock);
565 sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
566 if (!(ka->sa.sa_flags & SA_NODEFER))
567 sigaddset(&current->blocked, sig);
568 recalc_sigpending();
569 spin_unlock_irq(&current->sighand->siglock);
570
Roland McGrath36a03302008-03-14 17:46:38 -0700571 tracehook_signal_handler(sig, info, ka, regs,
572 test_thread_flag(TIF_SINGLESTEP));
573
Ingo Molnar7e907f42008-03-06 10:33:08 +0100574 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
577/*
578 * Note that 'init' is a special process: it doesn't get signals it doesn't
579 * want to handle. Thus you cannot kill init even with a SIGKILL even by
580 * mistake.
581 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100582static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800584 struct k_sigaction ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 siginfo_t info;
586 int signr;
David Howells283828f2006-01-18 17:44:00 -0800587 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 /*
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800590 * We want the common case to go fast, which is why we may in certain
591 * cases get here from kernel mode. Just return without doing anything
592 * if so.
593 * X86_32: vm86 regs switched out by assembly code before reaching
594 * here, so testing against kernel CS suffices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 */
Vincent Hanquez717b5942005-06-23 00:08:45 -0700596 if (!user_mode(regs))
David Howells283828f2006-01-18 17:44:00 -0800597 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700599 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
David Howells283828f2006-01-18 17:44:00 -0800600 oldset = &current->saved_sigmask;
601 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 oldset = &current->blocked;
603
604 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
605 if (signr > 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100606 /*
607 * Re-enable any watchpoints before delivering the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 * signal to user space. The processor register will
609 * have been cleared if the watchpoint triggered
610 * inside the kernel.
611 */
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800612 if (current->thread.debugreg7)
Roland McGrath0f534092008-01-30 13:30:59 +0100613 set_debugreg(current->thread.debugreg7, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Ingo Molnar7e907f42008-03-06 10:33:08 +0100615 /* Whee! Actually deliver the signal. */
David Howells283828f2006-01-18 17:44:00 -0800616 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100617 /*
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700618 * A signal was successfully delivered; the saved
David Howells283828f2006-01-18 17:44:00 -0800619 * sigmask will have been stored in the signal frame,
620 * and will be restored by sigreturn, so we can simply
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700621 * clear the TS_RESTORE_SIGMASK flag.
Ingo Molnar7e907f42008-03-06 10:33:08 +0100622 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700623 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
David Howells283828f2006-01-18 17:44:00 -0800624 }
David Howells283828f2006-01-18 17:44:00 -0800625 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /* Did we come from a system call? */
Harvey Harrison9902a702008-02-08 12:09:57 -0800629 if ((long)regs->orig_ax >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /* Restart the system call - no handlers present */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100631 switch (regs->ax) {
David Howells283828f2006-01-18 17:44:00 -0800632 case -ERESTARTNOHAND:
633 case -ERESTARTSYS:
634 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100635 regs->ax = regs->orig_ax;
636 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800637 break;
638
639 case -ERESTART_RESTARTBLOCK:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100640 regs->ax = __NR_restart_syscall;
641 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800642 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644 }
David Howells283828f2006-01-18 17:44:00 -0800645
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800646 /*
647 * If there's no signal to deliver, we just put the saved sigmask
648 * back.
649 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700650 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
651 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
David Howells283828f2006-01-18 17:44:00 -0800652 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656/*
657 * notification of userspace execution resumption
David Howells283828f2006-01-18 17:44:00 -0800658 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100660void
661do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /* deal with pending signal delivery */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700664 if (thread_info_flags & _TIF_SIGPENDING)
David Howells283828f2006-01-18 17:44:00 -0800665 do_signal(regs);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100666
Roland McGrath59e52132008-04-19 19:10:57 -0700667 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
668 clear_thread_flag(TIF_NOTIFY_RESUME);
669 tracehook_notify_resume(regs);
670 }
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 clear_thread_flag(TIF_IRET);
673}