blob: 28161170fb0aa36dcb16d91f730e83fee9d4409c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/x86_64/kernel/signal.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
6 *
7 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
8 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
9 * 2000-2002 x86-64 support by Andi Kleen
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#include <linux/sched.h>
13#include <linux/mm.h>
14#include <linux/smp.h>
15#include <linux/smp_lock.h>
16#include <linux/kernel.h>
17#include <linux/signal.h>
18#include <linux/errno.h>
19#include <linux/wait.h>
20#include <linux/ptrace.h>
21#include <linux/unistd.h>
22#include <linux/stddef.h>
23#include <linux/personality.h>
24#include <linux/compiler.h>
25#include <asm/ucontext.h>
26#include <asm/uaccess.h>
27#include <asm/i387.h>
28#include <asm/proto.h>
Oliver Korpillac5924b72005-05-27 12:52:55 -070029#include <asm/ia32_unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/* #define DEBUG_SIG 1 */
32
33#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
34
Roland McGrath0928d6e2005-06-23 00:08:37 -070035int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 sigset_t *set, struct pt_regs * regs);
Roland McGrath0928d6e2005-06-23 00:08:37 -070037int ia32_setup_frame(int sig, struct k_sigaction *ka,
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 sigset_t *set, struct pt_regs * regs);
39
40asmlinkage long
41sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize, struct pt_regs *regs)
42{
43 sigset_t saveset, newset;
44
45 /* XXX: Don't preclude handling different sized sigset_t's. */
46 if (sigsetsize != sizeof(sigset_t))
47 return -EINVAL;
48
49 if (copy_from_user(&newset, unewset, sizeof(newset)))
50 return -EFAULT;
51 sigdelsetmask(&newset, ~_BLOCKABLE);
52
53 spin_lock_irq(&current->sighand->siglock);
54 saveset = current->blocked;
55 current->blocked = newset;
56 recalc_sigpending();
57 spin_unlock_irq(&current->sighand->siglock);
58#ifdef DEBUG_SIG
59 printk("rt_sigsuspend savset(%lx) newset(%lx) regs(%p) rip(%lx)\n",
60 saveset, newset, regs, regs->rip);
61#endif
62 regs->rax = -EINTR;
63 while (1) {
64 current->state = TASK_INTERRUPTIBLE;
65 schedule();
66 if (do_signal(regs, &saveset))
67 return -EINTR;
68 }
69}
70
71asmlinkage long
72sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
73 struct pt_regs *regs)
74{
75 return do_sigaltstack(uss, uoss, regs->rsp);
76}
77
78
79/*
80 * Do a signal return; undo the signal stack.
81 */
82
83struct rt_sigframe
84{
Al Viro993fb382005-04-26 07:43:42 -070085 char __user *pretcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 struct ucontext uc;
87 struct siginfo info;
88};
89
90static int
91restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, unsigned long *prax)
92{
93 unsigned int err = 0;
94
95 /* Always make any pending restarted system calls return -EINTR */
96 current_thread_info()->restart_block.fn = do_no_restart_syscall;
97
98#define COPY(x) err |= __get_user(regs->x, &sc->x)
99
100 COPY(rdi); COPY(rsi); COPY(rbp); COPY(rsp); COPY(rbx);
101 COPY(rdx); COPY(rcx); COPY(rip);
102 COPY(r8);
103 COPY(r9);
104 COPY(r10);
105 COPY(r11);
106 COPY(r12);
107 COPY(r13);
108 COPY(r14);
109 COPY(r15);
110
Bryan Forde4e5d322005-11-05 17:25:54 +0100111 /* Kernel saves and restores only the CS segment register on signals,
112 * which is the bare minimum needed to allow mixed 32/64-bit code.
113 * App's signal handler can save/restore other segments if needed. */
114 {
115 unsigned cs;
116 err |= __get_user(cs, &sc->cs);
117 regs->cs = cs | 3; /* Force into user mode */
118 }
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 {
121 unsigned int tmpflags;
122 err |= __get_user(tmpflags, &sc->eflags);
123 regs->eflags = (regs->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
124 regs->orig_rax = -1; /* disable syscall checks */
125 }
126
127 {
128 struct _fpstate __user * buf;
129 err |= __get_user(buf, &sc->fpstate);
130
131 if (buf) {
132 if (!access_ok(VERIFY_READ, buf, sizeof(*buf)))
133 goto badframe;
134 err |= restore_i387(buf);
135 } else {
136 struct task_struct *me = current;
137 if (used_math()) {
138 clear_fpu(me);
139 clear_used_math();
140 }
141 }
142 }
143
144 err |= __get_user(*prax, &sc->rax);
145 return err;
146
147badframe:
148 return 1;
149}
150
151asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
152{
153 struct rt_sigframe __user *frame;
154 sigset_t set;
155 unsigned long eax;
156
157 frame = (struct rt_sigframe __user *)(regs->rsp - 8);
158 if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) {
159 goto badframe;
160 }
161 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) {
162 goto badframe;
163 }
164
165 sigdelsetmask(&set, ~_BLOCKABLE);
166 spin_lock_irq(&current->sighand->siglock);
167 current->blocked = set;
168 recalc_sigpending();
169 spin_unlock_irq(&current->sighand->siglock);
170
171 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &eax))
172 goto badframe;
173
174#ifdef DEBUG_SIG
175 printk("%d sigreturn rip:%lx rsp:%lx frame:%p rax:%lx\n",current->pid,regs.rip,regs.rsp,frame,eax);
176#endif
177
178 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->rsp) == -EFAULT)
179 goto badframe;
180
181 return eax;
182
183badframe:
184 signal_fault(regs,frame,"sigreturn");
185 return 0;
186}
187
188/*
189 * Set up a signal frame.
190 */
191
192static inline int
193setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, unsigned long mask, struct task_struct *me)
194{
195 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Bryan Forde4e5d322005-11-05 17:25:54 +0100197 err |= __put_user(regs->cs, &sc->cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 err |= __put_user(0, &sc->gs);
199 err |= __put_user(0, &sc->fs);
200
201 err |= __put_user(regs->rdi, &sc->rdi);
202 err |= __put_user(regs->rsi, &sc->rsi);
203 err |= __put_user(regs->rbp, &sc->rbp);
204 err |= __put_user(regs->rsp, &sc->rsp);
205 err |= __put_user(regs->rbx, &sc->rbx);
206 err |= __put_user(regs->rdx, &sc->rdx);
207 err |= __put_user(regs->rcx, &sc->rcx);
208 err |= __put_user(regs->rax, &sc->rax);
209 err |= __put_user(regs->r8, &sc->r8);
210 err |= __put_user(regs->r9, &sc->r9);
211 err |= __put_user(regs->r10, &sc->r10);
212 err |= __put_user(regs->r11, &sc->r11);
213 err |= __put_user(regs->r12, &sc->r12);
214 err |= __put_user(regs->r13, &sc->r13);
215 err |= __put_user(regs->r14, &sc->r14);
216 err |= __put_user(regs->r15, &sc->r15);
217 err |= __put_user(me->thread.trap_no, &sc->trapno);
218 err |= __put_user(me->thread.error_code, &sc->err);
219 err |= __put_user(regs->rip, &sc->rip);
Andi Kleenbe61bff2005-04-16 15:24:57 -0700220 err |= __put_user(regs->eflags, &sc->eflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 err |= __put_user(mask, &sc->oldmask);
222 err |= __put_user(me->thread.cr2, &sc->cr2);
223
224 return err;
225}
226
227/*
228 * Determine which stack to use..
229 */
230
231static void __user *
232get_stack(struct k_sigaction *ka, struct pt_regs *regs, unsigned long size)
233{
234 unsigned long rsp;
235
236 /* Default to using normal stack - redzone*/
237 rsp = regs->rsp - 128;
238
239 /* This is the X/Open sanctioned signal stack switching. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (ka->sa.sa_flags & SA_ONSTACK) {
241 if (sas_ss_flags(rsp) == 0)
242 rsp = current->sas_ss_sp + current->sas_ss_size;
243 }
244
245 return (void __user *)round_down(rsp - size, 16);
246}
247
Roland McGrath0928d6e2005-06-23 00:08:37 -0700248static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 sigset_t *set, struct pt_regs * regs)
250{
251 struct rt_sigframe __user *frame;
252 struct _fpstate __user *fp = NULL;
253 int err = 0;
254 struct task_struct *me = current;
255
256 if (used_math()) {
257 fp = get_stack(ka, regs, sizeof(struct _fpstate));
258 frame = (void __user *)round_down(
259 (unsigned long)fp - sizeof(struct rt_sigframe), 16) - 8;
260
261 if (!access_ok(VERIFY_WRITE, fp, sizeof(struct _fpstate)))
262 goto give_sigsegv;
263
264 if (save_i387(fp) < 0)
265 err |= -1;
266 } else
267 frame = get_stack(ka, regs, sizeof(struct rt_sigframe)) - 8;
268
269 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
270 goto give_sigsegv;
271
272 if (ka->sa.sa_flags & SA_SIGINFO) {
273 err |= copy_siginfo_to_user(&frame->info, info);
274 if (err)
275 goto give_sigsegv;
276 }
277
278 /* Create the ucontext. */
279 err |= __put_user(0, &frame->uc.uc_flags);
280 err |= __put_user(0, &frame->uc.uc_link);
281 err |= __put_user(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
282 err |= __put_user(sas_ss_flags(regs->rsp),
283 &frame->uc.uc_stack.ss_flags);
284 err |= __put_user(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
285 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0], me);
286 err |= __put_user(fp, &frame->uc.uc_mcontext.fpstate);
287 if (sizeof(*set) == 16) {
288 __put_user(set->sig[0], &frame->uc.uc_sigmask.sig[0]);
289 __put_user(set->sig[1], &frame->uc.uc_sigmask.sig[1]);
290 } else
291 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
292
293 /* Set up to return from userspace. If provided, use a stub
294 already in userspace. */
295 /* x86-64 should always use SA_RESTORER. */
296 if (ka->sa.sa_flags & SA_RESTORER) {
297 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
298 } else {
299 /* could use a vstub here */
300 goto give_sigsegv;
301 }
302
303 if (err)
304 goto give_sigsegv;
305
306#ifdef DEBUG_SIG
307 printk("%d old rip %lx old rsp %lx old rax %lx\n", current->pid,regs->rip,regs->rsp,regs->rax);
308#endif
309
310 /* Set up registers for signal handler */
311 {
312 struct exec_domain *ed = current_thread_info()->exec_domain;
313 if (unlikely(ed && ed->signal_invmap && sig < 32))
314 sig = ed->signal_invmap[sig];
315 }
316 regs->rdi = sig;
317 /* In case the signal handler was declared without prototypes */
318 regs->rax = 0;
319
320 /* This also works for non SA_SIGINFO handlers because they expect the
321 next argument after the signal number on the stack. */
322 regs->rsi = (unsigned long)&frame->info;
323 regs->rdx = (unsigned long)&frame->uc;
324 regs->rip = (unsigned long) ka->sa.sa_handler;
325
326 regs->rsp = (unsigned long)frame;
327
Bryan Forde4e5d322005-11-05 17:25:54 +0100328 /* Set up the CS register to run signal handlers in 64-bit mode,
329 even if the handler happens to be interrupting 32-bit code. */
330 regs->cs = __USER_CS;
331
332 /* This, by contrast, has nothing to do with segment registers -
333 see include/asm-x86_64/uaccess.h for details. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 set_fs(USER_DS);
Bryan Forde4e5d322005-11-05 17:25:54 +0100335
Andi Kleenbe61bff2005-04-16 15:24:57 -0700336 regs->eflags &= ~TF_MASK;
337 if (test_thread_flag(TIF_SINGLESTEP))
338 ptrace_notify(SIGTRAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339#ifdef DEBUG_SIG
340 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
341 current->comm, current->pid, frame, regs->rip, frame->pretcode);
342#endif
343
Roland McGrath0928d6e2005-06-23 00:08:37 -0700344 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346give_sigsegv:
347 force_sigsegv(sig, current);
Roland McGrath0928d6e2005-06-23 00:08:37 -0700348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
351/*
352 * OK, we're invoking a handler
353 */
354
Roland McGrath0928d6e2005-06-23 00:08:37 -0700355static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
357 sigset_t *oldset, struct pt_regs *regs)
358{
Roland McGrath0928d6e2005-06-23 00:08:37 -0700359 int ret;
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361#ifdef DEBUG_SIG
362 printk("handle_signal pid:%d sig:%lu rip:%lx rsp:%lx regs=%p\n",
363 current->pid, sig,
364 regs->rip, regs->rsp, regs);
365#endif
366
367 /* Are we from a system call? */
368 if ((long)regs->orig_rax >= 0) {
369 /* If so, check system call restarting.. */
370 switch (regs->rax) {
371 case -ERESTART_RESTARTBLOCK:
372 case -ERESTARTNOHAND:
373 regs->rax = -EINTR;
374 break;
375
376 case -ERESTARTSYS:
377 if (!(ka->sa.sa_flags & SA_RESTART)) {
378 regs->rax = -EINTR;
379 break;
380 }
381 /* fallthrough */
382 case -ERESTARTNOINTR:
383 regs->rax = regs->orig_rax;
384 regs->rip -= 2;
385 break;
386 }
387 }
388
Andi Kleend61915d2005-04-16 15:25:00 -0700389 /*
390 * If TF is set due to a debugger (PT_DTRACE), clear the TF
391 * flag so that register information in the sigcontext is
392 * correct.
393 */
394 if (unlikely(regs->eflags & TF_MASK)) {
395 if (likely(current->ptrace & PT_DTRACE)) {
396 current->ptrace &= ~PT_DTRACE;
397 regs->eflags &= ~TF_MASK;
398 }
399 }
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401#ifdef CONFIG_IA32_EMULATION
402 if (test_thread_flag(TIF_IA32)) {
403 if (ka->sa.sa_flags & SA_SIGINFO)
Roland McGrath0928d6e2005-06-23 00:08:37 -0700404 ret = ia32_setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 else
Roland McGrath0928d6e2005-06-23 00:08:37 -0700406 ret = ia32_setup_frame(sig, ka, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 } else
408#endif
Roland McGrath0928d6e2005-06-23 00:08:37 -0700409 ret = setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Steven Rostedt69be8f12005-08-29 11:44:09 -0400411 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 spin_lock_irq(&current->sighand->siglock);
413 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
Steven Rostedt69be8f12005-08-29 11:44:09 -0400414 if (!(ka->sa.sa_flags & SA_NODEFER))
415 sigaddset(&current->blocked,sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 recalc_sigpending();
417 spin_unlock_irq(&current->sighand->siglock);
418 }
Roland McGrath0928d6e2005-06-23 00:08:37 -0700419
420 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
423/*
424 * Note that 'init' is a special process: it doesn't get signals it doesn't
425 * want to handle. Thus you cannot kill init even with a SIGKILL even by
426 * mistake.
427 */
428int do_signal(struct pt_regs *regs, sigset_t *oldset)
429{
430 struct k_sigaction ka;
431 siginfo_t info;
432 int signr;
433
434 /*
435 * We want the common case to go fast, which
436 * is why we may in certain cases get here from
437 * kernel mode. Just return without doing anything
438 * if so.
439 */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700440 if (!user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return 1;
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (!oldset)
444 oldset = &current->blocked;
445
446 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
447 if (signr > 0) {
448 /* Reenable any watchpoints before delivering the
449 * signal to user space. The processor register will
450 * have been cleared if the watchpoint triggered
451 * inside the kernel.
452 */
453 if (current->thread.debugreg7)
Vincent Hanqueze9129e52005-06-23 00:08:46 -0700454 set_debugreg(current->thread.debugreg7, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 /* Whee! Actually deliver the signal. */
Roland McGrath0928d6e2005-06-23 00:08:37 -0700457 return handle_signal(signr, &info, &ka, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 /* Did we come from a system call? */
461 if ((long)regs->orig_rax >= 0) {
462 /* Restart the system call - no handlers present */
463 long res = regs->rax;
464 if (res == -ERESTARTNOHAND ||
465 res == -ERESTARTSYS ||
466 res == -ERESTARTNOINTR) {
467 regs->rax = regs->orig_rax;
468 regs->rip -= 2;
469 }
470 if (regs->rax == (unsigned long)-ERESTART_RESTARTBLOCK) {
Andi Kleen607a1682005-05-20 14:27:58 -0700471 regs->rax = test_thread_flag(TIF_IA32) ?
472 __NR_ia32_restart_syscall :
473 __NR_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 regs->rip -= 2;
475 }
476 }
477 return 0;
478}
479
480void do_notify_resume(struct pt_regs *regs, sigset_t *oldset, __u32 thread_info_flags)
481{
482#ifdef DEBUG_SIG
483 printk("do_notify_resume flags:%x rip:%lx rsp:%lx caller:%lx pending:%lx\n",
484 thread_info_flags, regs->rip, regs->rsp, __builtin_return_address(0),signal_pending(current));
485#endif
486
487 /* Pending single-step? */
488 if (thread_info_flags & _TIF_SINGLESTEP) {
489 regs->eflags |= TF_MASK;
490 clear_thread_flag(TIF_SINGLESTEP);
491 }
492
493 /* deal with pending signal delivery */
494 if (thread_info_flags & _TIF_SIGPENDING)
495 do_signal(regs,oldset);
496}
497
498void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
499{
500 struct task_struct *me = current;
501 if (exception_trace)
502 printk("%s[%d] bad frame in %s frame:%p rip:%lx rsp:%lx orax:%lx\n",
503 me->comm,me->pid,where,frame,regs->rip,regs->rsp,regs->orig_rax);
504
505 force_sig(SIGSEGV, me);
506}