Merge branch 'upstream' of git://git.kernel.org/pub/scm/linux/kernel/git/jgarzik...
[deliverable/linux.git] / arch / x86 / kernel / signal_32.c
1 /*
2 * linux/arch/i386/kernel/signal.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
7 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
8 */
9
10 #include <linux/sched.h>
11 #include <linux/mm.h>
12 #include <linux/smp.h>
13 #include <linux/kernel.h>
14 #include <linux/signal.h>
15 #include <linux/errno.h>
16 #include <linux/wait.h>
17 #include <linux/unistd.h>
18 #include <linux/stddef.h>
19 #include <linux/personality.h>
20 #include <linux/suspend.h>
21 #include <linux/ptrace.h>
22 #include <linux/elf.h>
23 #include <linux/binfmts.h>
24 #include <asm/processor.h>
25 #include <asm/ucontext.h>
26 #include <asm/uaccess.h>
27 #include <asm/i387.h>
28 #include "sigframe_32.h"
29
30 #define DEBUG_SIG 0
31
32 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
33
34 /*
35 * Atomically swap in the new signal mask, and wait for a signal.
36 */
37 asmlinkage int
38 sys_sigsuspend(int history0, int history1, old_sigset_t mask)
39 {
40 mask &= _BLOCKABLE;
41 spin_lock_irq(&current->sighand->siglock);
42 current->saved_sigmask = current->blocked;
43 siginitset(&current->blocked, mask);
44 recalc_sigpending();
45 spin_unlock_irq(&current->sighand->siglock);
46
47 current->state = TASK_INTERRUPTIBLE;
48 schedule();
49 set_thread_flag(TIF_RESTORE_SIGMASK);
50 return -ERESTARTNOHAND;
51 }
52
53 asmlinkage int
54 sys_sigaction(int sig, const struct old_sigaction __user *act,
55 struct old_sigaction __user *oact)
56 {
57 struct k_sigaction new_ka, old_ka;
58 int ret;
59
60 if (act) {
61 old_sigset_t mask;
62 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
63 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
64 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
65 return -EFAULT;
66 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
67 __get_user(mask, &act->sa_mask);
68 siginitset(&new_ka.sa.sa_mask, mask);
69 }
70
71 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
72
73 if (!ret && oact) {
74 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
75 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
76 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
77 return -EFAULT;
78 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
79 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
80 }
81
82 return ret;
83 }
84
85 asmlinkage int
86 sys_sigaltstack(unsigned long ebx)
87 {
88 /* This is needed to make gcc realize it doesn't own the "struct pt_regs" */
89 struct pt_regs *regs = (struct pt_regs *)&ebx;
90 const stack_t __user *uss = (const stack_t __user *)ebx;
91 stack_t __user *uoss = (stack_t __user *)regs->ecx;
92
93 return do_sigaltstack(uss, uoss, regs->esp);
94 }
95
96
97 /*
98 * Do a signal return; undo the signal stack.
99 */
100
101 static int
102 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *peax)
103 {
104 unsigned int err = 0;
105
106 /* Always make any pending restarted system calls return -EINTR */
107 current_thread_info()->restart_block.fn = do_no_restart_syscall;
108
109 #define COPY(x) err |= __get_user(regs->x, &sc->x)
110
111 #define COPY_SEG(seg) \
112 { unsigned short tmp; \
113 err |= __get_user(tmp, &sc->seg); \
114 regs->x##seg = tmp; }
115
116 #define COPY_SEG_STRICT(seg) \
117 { unsigned short tmp; \
118 err |= __get_user(tmp, &sc->seg); \
119 regs->x##seg = tmp|3; }
120
121 #define GET_SEG(seg) \
122 { unsigned short tmp; \
123 err |= __get_user(tmp, &sc->seg); \
124 loadsegment(seg,tmp); }
125
126 #define FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_RF | \
127 X86_EFLAGS_OF | X86_EFLAGS_DF | \
128 X86_EFLAGS_TF | X86_EFLAGS_SF | X86_EFLAGS_ZF | \
129 X86_EFLAGS_AF | X86_EFLAGS_PF | X86_EFLAGS_CF)
130
131 GET_SEG(gs);
132 COPY_SEG(fs);
133 COPY_SEG(es);
134 COPY_SEG(ds);
135 COPY(edi);
136 COPY(esi);
137 COPY(ebp);
138 COPY(esp);
139 COPY(ebx);
140 COPY(edx);
141 COPY(ecx);
142 COPY(eip);
143 COPY_SEG_STRICT(cs);
144 COPY_SEG_STRICT(ss);
145
146 {
147 unsigned int tmpflags;
148 err |= __get_user(tmpflags, &sc->eflags);
149 regs->eflags = (regs->eflags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
150 regs->orig_eax = -1; /* disable syscall checks */
151 }
152
153 {
154 struct _fpstate __user * buf;
155 err |= __get_user(buf, &sc->fpstate);
156 if (buf) {
157 if (!access_ok(VERIFY_READ, buf, sizeof(*buf)))
158 goto badframe;
159 err |= restore_i387(buf);
160 } else {
161 struct task_struct *me = current;
162 if (used_math()) {
163 clear_fpu(me);
164 clear_used_math();
165 }
166 }
167 }
168
169 err |= __get_user(*peax, &sc->eax);
170 return err;
171
172 badframe:
173 return 1;
174 }
175
176 asmlinkage int sys_sigreturn(unsigned long __unused)
177 {
178 struct pt_regs *regs = (struct pt_regs *) &__unused;
179 struct sigframe __user *frame = (struct sigframe __user *)(regs->esp - 8);
180 sigset_t set;
181 int eax;
182
183 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
184 goto badframe;
185 if (__get_user(set.sig[0], &frame->sc.oldmask)
186 || (_NSIG_WORDS > 1
187 && __copy_from_user(&set.sig[1], &frame->extramask,
188 sizeof(frame->extramask))))
189 goto badframe;
190
191 sigdelsetmask(&set, ~_BLOCKABLE);
192 spin_lock_irq(&current->sighand->siglock);
193 current->blocked = set;
194 recalc_sigpending();
195 spin_unlock_irq(&current->sighand->siglock);
196
197 if (restore_sigcontext(regs, &frame->sc, &eax))
198 goto badframe;
199 return eax;
200
201 badframe:
202 if (show_unhandled_signals && printk_ratelimit())
203 printk("%s%s[%d] bad frame in sigreturn frame:%p eip:%lx"
204 " esp:%lx oeax:%lx\n",
205 current->pid > 1 ? KERN_INFO : KERN_EMERG,
206 current->comm, current->pid, frame, regs->eip,
207 regs->esp, regs->orig_eax);
208
209 force_sig(SIGSEGV, current);
210 return 0;
211 }
212
213 asmlinkage int sys_rt_sigreturn(unsigned long __unused)
214 {
215 struct pt_regs *regs = (struct pt_regs *) &__unused;
216 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)(regs->esp - 4);
217 sigset_t set;
218 int eax;
219
220 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
221 goto badframe;
222 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
223 goto badframe;
224
225 sigdelsetmask(&set, ~_BLOCKABLE);
226 spin_lock_irq(&current->sighand->siglock);
227 current->blocked = set;
228 recalc_sigpending();
229 spin_unlock_irq(&current->sighand->siglock);
230
231 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &eax))
232 goto badframe;
233
234 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->esp) == -EFAULT)
235 goto badframe;
236
237 return eax;
238
239 badframe:
240 force_sig(SIGSEGV, current);
241 return 0;
242 }
243
244 /*
245 * Set up a signal frame.
246 */
247
248 static int
249 setup_sigcontext(struct sigcontext __user *sc, struct _fpstate __user *fpstate,
250 struct pt_regs *regs, unsigned long mask)
251 {
252 int tmp, err = 0;
253
254 err |= __put_user(regs->xfs, (unsigned int __user *)&sc->fs);
255 savesegment(gs, tmp);
256 err |= __put_user(tmp, (unsigned int __user *)&sc->gs);
257
258 err |= __put_user(regs->xes, (unsigned int __user *)&sc->es);
259 err |= __put_user(regs->xds, (unsigned int __user *)&sc->ds);
260 err |= __put_user(regs->edi, &sc->edi);
261 err |= __put_user(regs->esi, &sc->esi);
262 err |= __put_user(regs->ebp, &sc->ebp);
263 err |= __put_user(regs->esp, &sc->esp);
264 err |= __put_user(regs->ebx, &sc->ebx);
265 err |= __put_user(regs->edx, &sc->edx);
266 err |= __put_user(regs->ecx, &sc->ecx);
267 err |= __put_user(regs->eax, &sc->eax);
268 err |= __put_user(current->thread.trap_no, &sc->trapno);
269 err |= __put_user(current->thread.error_code, &sc->err);
270 err |= __put_user(regs->eip, &sc->eip);
271 err |= __put_user(regs->xcs, (unsigned int __user *)&sc->cs);
272 err |= __put_user(regs->eflags, &sc->eflags);
273 err |= __put_user(regs->esp, &sc->esp_at_signal);
274 err |= __put_user(regs->xss, (unsigned int __user *)&sc->ss);
275
276 tmp = save_i387(fpstate);
277 if (tmp < 0)
278 err = 1;
279 else
280 err |= __put_user(tmp ? fpstate : NULL, &sc->fpstate);
281
282 /* non-iBCS2 extensions.. */
283 err |= __put_user(mask, &sc->oldmask);
284 err |= __put_user(current->thread.cr2, &sc->cr2);
285
286 return err;
287 }
288
289 /*
290 * Determine which stack to use..
291 */
292 static inline void __user *
293 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
294 {
295 unsigned long esp;
296
297 /* Default to using normal stack */
298 esp = regs->esp;
299
300 /* This is the X/Open sanctioned signal stack switching. */
301 if (ka->sa.sa_flags & SA_ONSTACK) {
302 if (sas_ss_flags(esp) == 0)
303 esp = current->sas_ss_sp + current->sas_ss_size;
304 }
305
306 /* This is the legacy signal stack switching. */
307 else if ((regs->xss & 0xffff) != __USER_DS &&
308 !(ka->sa.sa_flags & SA_RESTORER) &&
309 ka->sa.sa_restorer) {
310 esp = (unsigned long) ka->sa.sa_restorer;
311 }
312
313 esp -= frame_size;
314 /* Align the stack pointer according to the i386 ABI,
315 * i.e. so that on function entry ((sp + 4) & 15) == 0. */
316 esp = ((esp + 4) & -16ul) - 4;
317 return (void __user *) esp;
318 }
319
320 /* These symbols are defined with the addresses in the vsyscall page.
321 See vsyscall-sigreturn.S. */
322 extern void __user __kernel_sigreturn;
323 extern void __user __kernel_rt_sigreturn;
324
325 static int setup_frame(int sig, struct k_sigaction *ka,
326 sigset_t *set, struct pt_regs * regs)
327 {
328 void __user *restorer;
329 struct sigframe __user *frame;
330 int err = 0;
331 int usig;
332
333 frame = get_sigframe(ka, regs, sizeof(*frame));
334
335 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
336 goto give_sigsegv;
337
338 usig = current_thread_info()->exec_domain
339 && current_thread_info()->exec_domain->signal_invmap
340 && sig < 32
341 ? current_thread_info()->exec_domain->signal_invmap[sig]
342 : sig;
343
344 err = __put_user(usig, &frame->sig);
345 if (err)
346 goto give_sigsegv;
347
348 err = setup_sigcontext(&frame->sc, &frame->fpstate, regs, set->sig[0]);
349 if (err)
350 goto give_sigsegv;
351
352 if (_NSIG_WORDS > 1) {
353 err = __copy_to_user(&frame->extramask, &set->sig[1],
354 sizeof(frame->extramask));
355 if (err)
356 goto give_sigsegv;
357 }
358
359 if (current->binfmt->hasvdso)
360 restorer = (void *)VDSO_SYM(&__kernel_sigreturn);
361 else
362 restorer = (void *)&frame->retcode;
363 if (ka->sa.sa_flags & SA_RESTORER)
364 restorer = ka->sa.sa_restorer;
365
366 /* Set up to return from userspace. */
367 err |= __put_user(restorer, &frame->pretcode);
368
369 /*
370 * This is popl %eax ; movl $,%eax ; int $0x80
371 *
372 * WE DO NOT USE IT ANY MORE! It's only left here for historical
373 * reasons and because gdb uses it as a signature to notice
374 * signal handler stack frames.
375 */
376 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
377 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
378 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
379
380 if (err)
381 goto give_sigsegv;
382
383 /* Set up registers for signal handler */
384 regs->esp = (unsigned long) frame;
385 regs->eip = (unsigned long) ka->sa.sa_handler;
386 regs->eax = (unsigned long) sig;
387 regs->edx = (unsigned long) 0;
388 regs->ecx = (unsigned long) 0;
389
390 set_fs(USER_DS);
391 regs->xds = __USER_DS;
392 regs->xes = __USER_DS;
393 regs->xss = __USER_DS;
394 regs->xcs = __USER_CS;
395
396 /*
397 * Clear TF when entering the signal handler, but
398 * notify any tracer that was single-stepping it.
399 * The tracer may want to single-step inside the
400 * handler too.
401 */
402 regs->eflags &= ~TF_MASK;
403 if (test_thread_flag(TIF_SINGLESTEP))
404 ptrace_notify(SIGTRAP);
405
406 #if DEBUG_SIG
407 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
408 current->comm, current->pid, frame, regs->eip, frame->pretcode);
409 #endif
410
411 return 0;
412
413 give_sigsegv:
414 force_sigsegv(sig, current);
415 return -EFAULT;
416 }
417
418 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
419 sigset_t *set, struct pt_regs * regs)
420 {
421 void __user *restorer;
422 struct rt_sigframe __user *frame;
423 int err = 0;
424 int usig;
425
426 frame = get_sigframe(ka, regs, sizeof(*frame));
427
428 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
429 goto give_sigsegv;
430
431 usig = current_thread_info()->exec_domain
432 && current_thread_info()->exec_domain->signal_invmap
433 && sig < 32
434 ? current_thread_info()->exec_domain->signal_invmap[sig]
435 : sig;
436
437 err |= __put_user(usig, &frame->sig);
438 err |= __put_user(&frame->info, &frame->pinfo);
439 err |= __put_user(&frame->uc, &frame->puc);
440 err |= copy_siginfo_to_user(&frame->info, info);
441 if (err)
442 goto give_sigsegv;
443
444 /* Create the ucontext. */
445 err |= __put_user(0, &frame->uc.uc_flags);
446 err |= __put_user(0, &frame->uc.uc_link);
447 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
448 err |= __put_user(sas_ss_flags(regs->esp),
449 &frame->uc.uc_stack.ss_flags);
450 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
451 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
452 regs, set->sig[0]);
453 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
454 if (err)
455 goto give_sigsegv;
456
457 /* Set up to return from userspace. */
458 restorer = (void *)VDSO_SYM(&__kernel_rt_sigreturn);
459 if (ka->sa.sa_flags & SA_RESTORER)
460 restorer = ka->sa.sa_restorer;
461 err |= __put_user(restorer, &frame->pretcode);
462
463 /*
464 * This is movl $,%eax ; int $0x80
465 *
466 * WE DO NOT USE IT ANY MORE! It's only left here for historical
467 * reasons and because gdb uses it as a signature to notice
468 * signal handler stack frames.
469 */
470 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
471 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
472 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
473
474 if (err)
475 goto give_sigsegv;
476
477 /* Set up registers for signal handler */
478 regs->esp = (unsigned long) frame;
479 regs->eip = (unsigned long) ka->sa.sa_handler;
480 regs->eax = (unsigned long) usig;
481 regs->edx = (unsigned long) &frame->info;
482 regs->ecx = (unsigned long) &frame->uc;
483
484 set_fs(USER_DS);
485 regs->xds = __USER_DS;
486 regs->xes = __USER_DS;
487 regs->xss = __USER_DS;
488 regs->xcs = __USER_CS;
489
490 /*
491 * Clear TF when entering the signal handler, but
492 * notify any tracer that was single-stepping it.
493 * The tracer may want to single-step inside the
494 * handler too.
495 */
496 regs->eflags &= ~TF_MASK;
497 if (test_thread_flag(TIF_SINGLESTEP))
498 ptrace_notify(SIGTRAP);
499
500 #if DEBUG_SIG
501 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
502 current->comm, current->pid, frame, regs->eip, frame->pretcode);
503 #endif
504
505 return 0;
506
507 give_sigsegv:
508 force_sigsegv(sig, current);
509 return -EFAULT;
510 }
511
512 /*
513 * OK, we're invoking a handler
514 */
515
516 static int
517 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
518 sigset_t *oldset, struct pt_regs * regs)
519 {
520 int ret;
521
522 /* Are we from a system call? */
523 if (regs->orig_eax >= 0) {
524 /* If so, check system call restarting.. */
525 switch (regs->eax) {
526 case -ERESTART_RESTARTBLOCK:
527 case -ERESTARTNOHAND:
528 regs->eax = -EINTR;
529 break;
530
531 case -ERESTARTSYS:
532 if (!(ka->sa.sa_flags & SA_RESTART)) {
533 regs->eax = -EINTR;
534 break;
535 }
536 /* fallthrough */
537 case -ERESTARTNOINTR:
538 regs->eax = regs->orig_eax;
539 regs->eip -= 2;
540 }
541 }
542
543 /*
544 * If TF is set due to a debugger (PT_DTRACE), clear the TF flag so
545 * that register information in the sigcontext is correct.
546 */
547 if (unlikely(regs->eflags & TF_MASK)
548 && likely(current->ptrace & PT_DTRACE)) {
549 current->ptrace &= ~PT_DTRACE;
550 regs->eflags &= ~TF_MASK;
551 }
552
553 /* Set up the stack frame */
554 if (ka->sa.sa_flags & SA_SIGINFO)
555 ret = setup_rt_frame(sig, ka, info, oldset, regs);
556 else
557 ret = setup_frame(sig, ka, oldset, regs);
558
559 if (ret == 0) {
560 spin_lock_irq(&current->sighand->siglock);
561 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
562 if (!(ka->sa.sa_flags & SA_NODEFER))
563 sigaddset(&current->blocked,sig);
564 recalc_sigpending();
565 spin_unlock_irq(&current->sighand->siglock);
566 }
567
568 return ret;
569 }
570
571 /*
572 * Note that 'init' is a special process: it doesn't get signals it doesn't
573 * want to handle. Thus you cannot kill init even with a SIGKILL even by
574 * mistake.
575 */
576 static void fastcall do_signal(struct pt_regs *regs)
577 {
578 siginfo_t info;
579 int signr;
580 struct k_sigaction ka;
581 sigset_t *oldset;
582
583 /*
584 * We want the common case to go fast, which
585 * is why we may in certain cases get here from
586 * kernel mode. Just return without doing anything
587 * if so. vm86 regs switched out by assembly code
588 * before reaching here, so testing against kernel
589 * CS suffices.
590 */
591 if (!user_mode(regs))
592 return;
593
594 if (test_thread_flag(TIF_RESTORE_SIGMASK))
595 oldset = &current->saved_sigmask;
596 else
597 oldset = &current->blocked;
598
599 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
600 if (signr > 0) {
601 /* Reenable any watchpoints before delivering the
602 * signal to user space. The processor register will
603 * have been cleared if the watchpoint triggered
604 * inside the kernel.
605 */
606 if (unlikely(current->thread.debugreg[7]))
607 set_debugreg(current->thread.debugreg[7], 7);
608
609 /* Whee! Actually deliver the signal. */
610 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
611 /* a signal was successfully delivered; the saved
612 * sigmask will have been stored in the signal frame,
613 * and will be restored by sigreturn, so we can simply
614 * clear the TIF_RESTORE_SIGMASK flag */
615 if (test_thread_flag(TIF_RESTORE_SIGMASK))
616 clear_thread_flag(TIF_RESTORE_SIGMASK);
617 }
618
619 return;
620 }
621
622 /* Did we come from a system call? */
623 if (regs->orig_eax >= 0) {
624 /* Restart the system call - no handlers present */
625 switch (regs->eax) {
626 case -ERESTARTNOHAND:
627 case -ERESTARTSYS:
628 case -ERESTARTNOINTR:
629 regs->eax = regs->orig_eax;
630 regs->eip -= 2;
631 break;
632
633 case -ERESTART_RESTARTBLOCK:
634 regs->eax = __NR_restart_syscall;
635 regs->eip -= 2;
636 break;
637 }
638 }
639
640 /* if there's no signal to deliver, we just put the saved sigmask
641 * back */
642 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
643 clear_thread_flag(TIF_RESTORE_SIGMASK);
644 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
645 }
646 }
647
648 /*
649 * notification of userspace execution resumption
650 * - triggered by the TIF_WORK_MASK flags
651 */
652 __attribute__((regparm(3)))
653 void do_notify_resume(struct pt_regs *regs, void *_unused,
654 __u32 thread_info_flags)
655 {
656 /* Pending single-step? */
657 if (thread_info_flags & _TIF_SINGLESTEP) {
658 regs->eflags |= TF_MASK;
659 clear_thread_flag(TIF_SINGLESTEP);
660 }
661
662 /* deal with pending signal delivery */
663 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
664 do_signal(regs);
665
666 clear_thread_flag(TIF_IRET);
667 }
This page took 0.055157 seconds and 5 git commands to generate.