blob: 4445d26efd478d42aae4857f349dfd44dcd964c1 [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>
Ingo Molnar7e907f42008-03-06 10:33:08 +010030
Harvey Harrison123a6342008-02-08 12:10:00 -080031#include "sigframe.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
34
Harvey Harrison1a176802008-02-08 12:09:59 -080035#define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
36 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
37 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
38 X86_EFLAGS_CF)
39
40#ifdef CONFIG_X86_32
41# define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
42#else
43# define FIX_EFLAGS __FIX_EFLAGS
44#endif
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
47 * Atomically swap in the new signal mask, and wait for a signal.
48 */
49asmlinkage int
50sys_sigsuspend(int history0, int history1, old_sigset_t mask)
51{
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 mask &= _BLOCKABLE;
53 spin_lock_irq(&current->sighand->siglock);
David Howells283828f2006-01-18 17:44:00 -080054 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 siginitset(&current->blocked, mask);
56 recalc_sigpending();
57 spin_unlock_irq(&current->sighand->siglock);
58
David Howells283828f2006-01-18 17:44:00 -080059 current->state = TASK_INTERRUPTIBLE;
60 schedule();
Roland McGrath5a8da0e2008-04-30 00:53:10 -070061 set_restore_sigmask();
Ingo Molnar7e907f42008-03-06 10:33:08 +010062
David Howells283828f2006-01-18 17:44:00 -080063 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
Ingo Molnar7e907f42008-03-06 10:33:08 +010066asmlinkage int
Linus Torvalds1da177e2005-04-16 15:20:36 -070067sys_sigaction(int sig, const struct old_sigaction __user *act,
68 struct old_sigaction __user *oact)
69{
70 struct k_sigaction new_ka, old_ka;
71 int ret;
72
73 if (act) {
74 old_sigset_t mask;
Ingo Molnar7e907f42008-03-06 10:33:08 +010075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
77 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
78 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
79 return -EFAULT;
Ingo Molnar7e907f42008-03-06 10:33:08 +010080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
82 __get_user(mask, &act->sa_mask);
83 siginitset(&new_ka.sa.sa_mask, mask);
84 }
85
86 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
87
88 if (!ret && oact) {
89 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
90 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
91 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
92 return -EFAULT;
Ingo Molnar7e907f42008-03-06 10:33:08 +010093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
95 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
96 }
97
98 return ret;
99}
100
Ingo Molnar7e907f42008-03-06 10:33:08 +0100101asmlinkage int sys_sigaltstack(unsigned long bx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Ingo Molnar7e907f42008-03-06 10:33:08 +0100103 /*
104 * This is needed to make gcc realize it doesn't own the
105 * "struct pt_regs"
106 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100107 struct pt_regs *regs = (struct pt_regs *)&bx;
108 const stack_t __user *uss = (const stack_t __user *)bx;
109 stack_t __user *uoss = (stack_t __user *)regs->cx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100111 return do_sigaltstack(uss, uoss, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114
115/*
116 * Do a signal return; undo the signal stack.
117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static int
Harvey Harrison866bc132008-02-08 12:10:02 -0800119restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
120 unsigned long *pax)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 unsigned int err = 0;
123
124 /* Always make any pending restarted system calls return -EINTR */
125 current_thread_info()->restart_block.fn = do_no_restart_syscall;
126
H. Peter Anvin742fa542008-01-30 13:30:56 +0100127#define COPY(x) err |= __get_user(regs->x, &sc->x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129#define COPY_SEG(seg) \
130 { unsigned short tmp; \
131 err |= __get_user(tmp, &sc->seg); \
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100132 regs->seg = tmp; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134#define COPY_SEG_STRICT(seg) \
135 { unsigned short tmp; \
136 err |= __get_user(tmp, &sc->seg); \
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100137 regs->seg = tmp|3; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139#define GET_SEG(seg) \
140 { unsigned short tmp; \
141 err |= __get_user(tmp, &sc->seg); \
Ingo Molnar7e907f42008-03-06 10:33:08 +0100142 loadsegment(seg, tmp); }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100144 GET_SEG(gs);
145 COPY_SEG(fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 COPY_SEG(es);
147 COPY_SEG(ds);
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800148 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
149 COPY(dx); COPY(cx); COPY(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 COPY_SEG_STRICT(cs);
151 COPY_SEG_STRICT(ss);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 {
154 unsigned int tmpflags;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100155
H. Peter Anvin742fa542008-01-30 13:30:56 +0100156 err |= __get_user(tmpflags, &sc->flags);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100157 regs->flags = (regs->flags & ~FIX_EFLAGS) |
158 (tmpflags & FIX_EFLAGS);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100159 regs->orig_ax = -1; /* disable syscall checks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161
162 {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100163 struct _fpstate __user *buf;
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 err |= __get_user(buf, &sc->fpstate);
166 if (buf) {
167 if (!access_ok(VERIFY_READ, buf, sizeof(*buf)))
168 goto badframe;
169 err |= restore_i387(buf);
170 } else {
171 struct task_struct *me = current;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (used_math()) {
174 clear_fpu(me);
175 clear_used_math();
176 }
177 }
178 }
179
Harvey Harrison866bc132008-02-08 12:10:02 -0800180 err |= __get_user(*pax, &sc->ax);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return err;
182
183badframe:
184 return 1;
185}
186
Harvey Harrison866bc132008-02-08 12:10:02 -0800187asmlinkage unsigned long sys_sigreturn(unsigned long __unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Ingo Molnar7e907f42008-03-06 10:33:08 +0100189 struct sigframe __user *frame;
190 struct pt_regs *regs;
Harvey Harrison866bc132008-02-08 12:10:02 -0800191 unsigned long ax;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100192 sigset_t set;
193
194 regs = (struct pt_regs *) &__unused;
195 frame = (struct sigframe __user *)(regs->sp - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
198 goto badframe;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100199 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 && __copy_from_user(&set.sig[1], &frame->extramask,
201 sizeof(frame->extramask))))
202 goto badframe;
203
204 sigdelsetmask(&set, ~_BLOCKABLE);
205 spin_lock_irq(&current->sighand->siglock);
206 current->blocked = set;
207 recalc_sigpending();
208 spin_unlock_irq(&current->sighand->siglock);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100209
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100210 if (restore_sigcontext(regs, &frame->sc, &ax))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 goto badframe;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100212 return ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214badframe:
Andi Kleen03252912008-01-30 13:33:18 +0100215 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamoto1181f8b2008-07-03 13:12:13 -0700216 printk("%s%s[%d] bad frame in sigreturn frame:"
Ingo Molnar97b44ae2008-03-06 10:43:17 +0100217 "%p ip:%lx sp:%lx oeax:%lx",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700218 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100219 current->comm, task_pid_nr(current), frame, regs->ip,
220 regs->sp, regs->orig_ax);
Andi Kleen03252912008-01-30 13:33:18 +0100221 print_vma_addr(" in ", regs->ip);
Ingo Molnar97b44ae2008-03-06 10:43:17 +0100222 printk(KERN_CONT "\n");
Andi Kleen03252912008-01-30 13:33:18 +0100223 }
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 force_sig(SIGSEGV, current);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return 0;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100228}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230asmlinkage int sys_rt_sigreturn(unsigned long __unused)
231{
Harvey Harrison2d19c452008-02-08 12:10:00 -0800232 struct pt_regs *regs = (struct pt_regs *)&__unused;
233 struct rt_sigframe __user *frame;
Harvey Harrison866bc132008-02-08 12:10:02 -0800234 unsigned long ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Harvey Harrison2d19c452008-02-08 12:10:00 -0800237 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
239 goto badframe;
240 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
241 goto badframe;
242
243 sigdelsetmask(&set, ~_BLOCKABLE);
244 spin_lock_irq(&current->sighand->siglock);
245 current->blocked = set;
246 recalc_sigpending();
247 spin_unlock_irq(&current->sighand->siglock);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100248
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100249 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 goto badframe;
251
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100252 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 goto badframe;
254
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100255 return ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257badframe:
258 force_sig(SIGSEGV, current);
259 return 0;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100260}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262/*
263 * Set up a signal frame.
264 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265static int
266setup_sigcontext(struct sigcontext __user *sc, struct _fpstate __user *fpstate,
267 struct pt_regs *regs, unsigned long mask)
268{
269 int tmp, err = 0;
270
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100271 err |= __put_user(regs->fs, (unsigned int __user *)&sc->fs);
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100272 savesegment(gs, tmp);
273 err |= __put_user(tmp, (unsigned int __user *)&sc->gs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100275 err |= __put_user(regs->es, (unsigned int __user *)&sc->es);
276 err |= __put_user(regs->ds, (unsigned int __user *)&sc->ds);
H. Peter Anvin742fa542008-01-30 13:30:56 +0100277 err |= __put_user(regs->di, &sc->di);
278 err |= __put_user(regs->si, &sc->si);
279 err |= __put_user(regs->bp, &sc->bp);
280 err |= __put_user(regs->sp, &sc->sp);
281 err |= __put_user(regs->bx, &sc->bx);
282 err |= __put_user(regs->dx, &sc->dx);
283 err |= __put_user(regs->cx, &sc->cx);
284 err |= __put_user(regs->ax, &sc->ax);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 err |= __put_user(current->thread.trap_no, &sc->trapno);
286 err |= __put_user(current->thread.error_code, &sc->err);
H. Peter Anvin742fa542008-01-30 13:30:56 +0100287 err |= __put_user(regs->ip, &sc->ip);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100288 err |= __put_user(regs->cs, (unsigned int __user *)&sc->cs);
H. Peter Anvin742fa542008-01-30 13:30:56 +0100289 err |= __put_user(regs->flags, &sc->flags);
290 err |= __put_user(regs->sp, &sc->sp_at_signal);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100291 err |= __put_user(regs->ss, (unsigned int __user *)&sc->ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 tmp = save_i387(fpstate);
294 if (tmp < 0)
Ingo Molnar7e907f42008-03-06 10:33:08 +0100295 err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 else
Ingo Molnar7e907f42008-03-06 10:33:08 +0100297 err |= __put_user(tmp ? fpstate : NULL, &sc->fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 /* non-iBCS2 extensions.. */
300 err |= __put_user(mask, &sc->oldmask);
301 err |= __put_user(current->thread.cr2, &sc->cr2);
302
303 return err;
304}
305
306/*
307 * Determine which stack to use..
308 */
309static inline void __user *
Ingo Molnar7e907f42008-03-06 10:33:08 +0100310get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
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
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100336 sp -= frame_size;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100337 /*
338 * Align the stack pointer according to the i386 ABI,
339 * i.e. so that on function entry ((sp + 4) & 15) == 0.
340 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100341 sp = ((sp + 4) & -16ul) - 4;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100342
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100343 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Ingo Molnar7e907f42008-03-06 10:33:08 +0100346static int
347setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
348 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100351 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 int err = 0;
353 int usig;
354
355 frame = get_sigframe(ka, regs, sizeof(*frame));
356
357 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
358 goto give_sigsegv;
359
360 usig = current_thread_info()->exec_domain
361 && current_thread_info()->exec_domain->signal_invmap
362 && sig < 32
363 ? current_thread_info()->exec_domain->signal_invmap[sig]
364 : sig;
365
366 err = __put_user(usig, &frame->sig);
367 if (err)
368 goto give_sigsegv;
369
370 err = setup_sigcontext(&frame->sc, &frame->fpstate, regs, set->sig[0]);
371 if (err)
372 goto give_sigsegv;
373
374 if (_NSIG_WORDS > 1) {
375 err = __copy_to_user(&frame->extramask, &set->sig[1],
376 sizeof(frame->extramask));
377 if (err)
378 goto give_sigsegv;
379 }
380
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700381 if (current->mm->context.vdso)
Roland McGrath6c3652e2008-01-30 13:30:42 +0100382 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100383 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100384 restorer = &frame->retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (ka->sa.sa_flags & SA_RESTORER)
386 restorer = ka->sa.sa_restorer;
387
388 /* Set up to return from userspace. */
389 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100392 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 *
394 * WE DO NOT USE IT ANY MORE! It's only left here for historical
395 * reasons and because gdb uses it as a signature to notice
396 * signal handler stack frames.
397 */
398 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
399 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
400 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
401
402 if (err)
403 goto give_sigsegv;
404
405 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100406 regs->sp = (unsigned long)frame;
407 regs->ip = (unsigned long)ka->sa.sa_handler;
408 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800409 regs->dx = 0;
410 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100412 regs->ds = __USER_DS;
413 regs->es = __USER_DS;
414 regs->ss = __USER_DS;
415 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
David Howells283828f2006-01-18 17:44:00 -0800417 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419give_sigsegv:
420 force_sigsegv(sig, current);
David Howells283828f2006-01-18 17:44:00 -0800421 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Roland McGrath7c1def12005-06-23 00:08:21 -0700424static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Ingo Molnar7e907f42008-03-06 10:33:08 +0100425 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100428 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 int err = 0;
430 int usig;
431
432 frame = get_sigframe(ka, regs, sizeof(*frame));
433
434 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
435 goto give_sigsegv;
436
437 usig = current_thread_info()->exec_domain
438 && current_thread_info()->exec_domain->signal_invmap
439 && sig < 32
440 ? current_thread_info()->exec_domain->signal_invmap[sig]
441 : sig;
442
443 err |= __put_user(usig, &frame->sig);
444 err |= __put_user(&frame->info, &frame->pinfo);
445 err |= __put_user(&frame->uc, &frame->puc);
446 err |= copy_siginfo_to_user(&frame->info, info);
447 if (err)
448 goto give_sigsegv;
449
450 /* Create the ucontext. */
451 err |= __put_user(0, &frame->uc.uc_flags);
452 err |= __put_user(0, &frame->uc.uc_link);
453 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100454 err |= __put_user(sas_ss_flags(regs->sp),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 &frame->uc.uc_stack.ss_flags);
456 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
457 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
Ingo Molnar7e907f42008-03-06 10:33:08 +0100458 regs, set->sig[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
460 if (err)
461 goto give_sigsegv;
462
463 /* Set up to return from userspace. */
Roland McGrath6c3652e2008-01-30 13:30:42 +0100464 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (ka->sa.sa_flags & SA_RESTORER)
466 restorer = ka->sa.sa_restorer;
467 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100470 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 *
472 * WE DO NOT USE IT ANY MORE! It's only left here for historical
473 * reasons and because gdb uses it as a signature to notice
474 * signal handler stack frames.
475 */
476 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
477 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
478 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
479
480 if (err)
481 goto give_sigsegv;
482
483 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100484 regs->sp = (unsigned long)frame;
485 regs->ip = (unsigned long)ka->sa.sa_handler;
486 regs->ax = (unsigned long)usig;
487 regs->dx = (unsigned long)&frame->info;
488 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100490 regs->ds = __USER_DS;
491 regs->es = __USER_DS;
492 regs->ss = __USER_DS;
493 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
David Howells283828f2006-01-18 17:44:00 -0800495 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497give_sigsegv:
498 force_sigsegv(sig, current);
David Howells283828f2006-01-18 17:44:00 -0800499 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
502/*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100503 * OK, we're invoking a handler:
504 */
Roland McGrath7c1def12005-06-23 00:08:21 -0700505static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800507 sigset_t *oldset, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Roland McGrath7c1def12005-06-23 00:08:21 -0700509 int ret;
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /* Are we from a system call? */
Harvey Harrison9902a702008-02-08 12:09:57 -0800512 if ((long)regs->orig_ax >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /* If so, check system call restarting.. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100514 switch (regs->ax) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800515 case -ERESTART_RESTARTBLOCK:
516 case -ERESTARTNOHAND:
517 regs->ax = -EINTR;
518 break;
519
520 case -ERESTARTSYS:
521 if (!(ka->sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100522 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800524 }
525 /* fallthrough */
526 case -ERESTARTNOINTR:
527 regs->ax = regs->orig_ax;
528 regs->ip -= 2;
529 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531 }
532
533 /*
Roland McGrathe1f28772008-01-30 13:30:50 +0100534 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
535 * flag so that register information in the sigcontext is correct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100537 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
Roland McGrathe1f28772008-01-30 13:30:50 +0100538 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100539 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 /* Set up the stack frame */
542 if (ka->sa.sa_flags & SA_SIGINFO)
Roland McGrath7c1def12005-06-23 00:08:21 -0700543 ret = setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 else
Roland McGrath7c1def12005-06-23 00:08:21 -0700545 ret = setup_frame(sig, ka, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Ingo Molnar7e907f42008-03-06 10:33:08 +0100547 if (ret)
548 return ret;
Roland McGrath7c1def12005-06-23 00:08:21 -0700549
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700550 /*
551 * Clear the direction flag as per the ABI for function entry.
552 */
553 regs->flags &= ~X86_EFLAGS_DF;
554
555 /*
556 * Clear TF when entering the signal handler, but
557 * notify any tracer that was single-stepping it.
558 * The tracer may want to single-step inside the
559 * handler too.
560 */
561 regs->flags &= ~X86_EFLAGS_TF;
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700562
Ingo Molnar7e907f42008-03-06 10:33:08 +0100563 spin_lock_irq(&current->sighand->siglock);
564 sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
565 if (!(ka->sa.sa_flags & SA_NODEFER))
566 sigaddset(&current->blocked, sig);
567 recalc_sigpending();
568 spin_unlock_irq(&current->sighand->siglock);
569
Roland McGrath36a03302008-03-14 17:46:38 -0700570 tracehook_signal_handler(sig, info, ka, regs,
571 test_thread_flag(TIF_SINGLESTEP));
572
Ingo Molnar7e907f42008-03-06 10:33:08 +0100573 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576/*
577 * Note that 'init' is a special process: it doesn't get signals it doesn't
578 * want to handle. Thus you cannot kill init even with a SIGKILL even by
579 * mistake.
580 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100581static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800583 struct k_sigaction ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 siginfo_t info;
585 int signr;
David Howells283828f2006-01-18 17:44:00 -0800586 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 /*
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800589 * We want the common case to go fast, which is why we may in certain
590 * cases get here from kernel mode. Just return without doing anything
591 * if so.
592 * X86_32: vm86 regs switched out by assembly code before reaching
593 * here, so testing against kernel CS suffices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 */
Vincent Hanquez717b5942005-06-23 00:08:45 -0700595 if (!user_mode(regs))
David Howells283828f2006-01-18 17:44:00 -0800596 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700598 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
David Howells283828f2006-01-18 17:44:00 -0800599 oldset = &current->saved_sigmask;
600 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 oldset = &current->blocked;
602
603 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
604 if (signr > 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100605 /*
606 * Re-enable any watchpoints before delivering the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * signal to user space. The processor register will
608 * have been cleared if the watchpoint triggered
609 * inside the kernel.
610 */
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800611 if (current->thread.debugreg7)
Roland McGrath0f534092008-01-30 13:30:59 +0100612 set_debugreg(current->thread.debugreg7, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Ingo Molnar7e907f42008-03-06 10:33:08 +0100614 /* Whee! Actually deliver the signal. */
David Howells283828f2006-01-18 17:44:00 -0800615 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100616 /*
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700617 * A signal was successfully delivered; the saved
David Howells283828f2006-01-18 17:44:00 -0800618 * sigmask will have been stored in the signal frame,
619 * and will be restored by sigreturn, so we can simply
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700620 * clear the TS_RESTORE_SIGMASK flag.
Ingo Molnar7e907f42008-03-06 10:33:08 +0100621 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700622 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
David Howells283828f2006-01-18 17:44:00 -0800623 }
David Howells283828f2006-01-18 17:44:00 -0800624 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /* Did we come from a system call? */
Harvey Harrison9902a702008-02-08 12:09:57 -0800628 if ((long)regs->orig_ax >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* Restart the system call - no handlers present */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100630 switch (regs->ax) {
David Howells283828f2006-01-18 17:44:00 -0800631 case -ERESTARTNOHAND:
632 case -ERESTARTSYS:
633 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100634 regs->ax = regs->orig_ax;
635 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800636 break;
637
638 case -ERESTART_RESTARTBLOCK:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100639 regs->ax = __NR_restart_syscall;
640 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800641 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643 }
David Howells283828f2006-01-18 17:44:00 -0800644
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800645 /*
646 * If there's no signal to deliver, we just put the saved sigmask
647 * back.
648 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700649 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
650 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
David Howells283828f2006-01-18 17:44:00 -0800651 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
655/*
656 * notification of userspace execution resumption
David Howells283828f2006-01-18 17:44:00 -0800657 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100659void
660do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 /* deal with pending signal delivery */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700663 if (thread_info_flags & _TIF_SIGPENDING)
David Howells283828f2006-01-18 17:44:00 -0800664 do_signal(regs);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100665
Roland McGrath59e52132008-04-19 19:10:57 -0700666 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
667 clear_thread_flag(TIF_NOTIFY_RESUME);
668 tracehook_notify_resume(regs);
669 }
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 clear_thread_flag(TIF_IRET);
672}