[CRYPTO] api: Remove deprecated interface
[pv_ops_mirror.git] / arch / i386 / kernel / signal.c
blob65d7620eaa093756e083ed5d0f9786ccfc1bd917
1 /*
2 * linux/arch/i386/kernel/signal.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
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 */
10 #include <linux/sched.h>
11 #include <linux/mm.h>
12 #include <linux/smp.h>
13 #include <linux/smp_lock.h>
14 #include <linux/kernel.h>
15 #include <linux/signal.h>
16 #include <linux/errno.h>
17 #include <linux/wait.h>
18 #include <linux/unistd.h>
19 #include <linux/stddef.h>
20 #include <linux/personality.h>
21 #include <linux/suspend.h>
22 #include <linux/ptrace.h>
23 #include <linux/elf.h>
24 #include <asm/processor.h>
25 #include <asm/ucontext.h>
26 #include <asm/uaccess.h>
27 #include <asm/i387.h>
28 #include "sigframe.h"
30 #define DEBUG_SIG 0
32 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
35 * Atomically swap in the new signal mask, and wait for a signal.
37 asmlinkage int
38 sys_sigsuspend(int history0, int history1, old_sigset_t mask)
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);
47 current->state = TASK_INTERRUPTIBLE;
48 schedule();
49 set_thread_flag(TIF_RESTORE_SIGMASK);
50 return -ERESTARTNOHAND;
53 asmlinkage int
54 sys_sigaction(int sig, const struct old_sigaction __user *act,
55 struct old_sigaction __user *oact)
57 struct k_sigaction new_ka, old_ka;
58 int ret;
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);
71 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
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);
82 return ret;
85 asmlinkage int
86 sys_sigaltstack(unsigned long ebx)
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;
93 return do_sigaltstack(uss, uoss, regs->esp);
98 * Do a signal return; undo the signal stack.
101 static int
102 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *peax)
104 unsigned int err = 0;
106 /* Always make any pending restarted system calls return -EINTR */
107 current_thread_info()->restart_block.fn = do_no_restart_syscall;
109 #define COPY(x) err |= __get_user(regs->x, &sc->x)
111 #define COPY_SEG(seg) \
112 { unsigned short tmp; \
113 err |= __get_user(tmp, &sc->seg); \
114 regs->x##seg = tmp; }
116 #define COPY_SEG_STRICT(seg) \
117 { unsigned short tmp; \
118 err |= __get_user(tmp, &sc->seg); \
119 regs->x##seg = tmp|3; }
121 #define GET_SEG(seg) \
122 { unsigned short tmp; \
123 err |= __get_user(tmp, &sc->seg); \
124 loadsegment(seg,tmp); }
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)
131 COPY_SEG(gs);
132 GET_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);
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 */
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();
169 err |= __get_user(*peax, &sc->eax);
170 return err;
172 badframe:
173 return 1;
176 asmlinkage int sys_sigreturn(unsigned long __unused)
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;
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;
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);
197 if (restore_sigcontext(regs, &frame->sc, &eax))
198 goto badframe;
199 return eax;
201 badframe:
202 force_sig(SIGSEGV, current);
203 return 0;
206 asmlinkage int sys_rt_sigreturn(unsigned long __unused)
208 struct pt_regs *regs = (struct pt_regs *) &__unused;
209 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)(regs->esp - 4);
210 sigset_t set;
211 int eax;
213 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
214 goto badframe;
215 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
216 goto badframe;
218 sigdelsetmask(&set, ~_BLOCKABLE);
219 spin_lock_irq(&current->sighand->siglock);
220 current->blocked = set;
221 recalc_sigpending();
222 spin_unlock_irq(&current->sighand->siglock);
224 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &eax))
225 goto badframe;
227 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->esp) == -EFAULT)
228 goto badframe;
230 return eax;
232 badframe:
233 force_sig(SIGSEGV, current);
234 return 0;
238 * Set up a signal frame.
241 static int
242 setup_sigcontext(struct sigcontext __user *sc, struct _fpstate __user *fpstate,
243 struct pt_regs *regs, unsigned long mask)
245 int tmp, err = 0;
247 err |= __put_user(regs->xgs, (unsigned int __user *)&sc->gs);
248 savesegment(fs, tmp);
249 err |= __put_user(tmp, (unsigned int __user *)&sc->fs);
251 err |= __put_user(regs->xes, (unsigned int __user *)&sc->es);
252 err |= __put_user(regs->xds, (unsigned int __user *)&sc->ds);
253 err |= __put_user(regs->edi, &sc->edi);
254 err |= __put_user(regs->esi, &sc->esi);
255 err |= __put_user(regs->ebp, &sc->ebp);
256 err |= __put_user(regs->esp, &sc->esp);
257 err |= __put_user(regs->ebx, &sc->ebx);
258 err |= __put_user(regs->edx, &sc->edx);
259 err |= __put_user(regs->ecx, &sc->ecx);
260 err |= __put_user(regs->eax, &sc->eax);
261 err |= __put_user(current->thread.trap_no, &sc->trapno);
262 err |= __put_user(current->thread.error_code, &sc->err);
263 err |= __put_user(regs->eip, &sc->eip);
264 err |= __put_user(regs->xcs, (unsigned int __user *)&sc->cs);
265 err |= __put_user(regs->eflags, &sc->eflags);
266 err |= __put_user(regs->esp, &sc->esp_at_signal);
267 err |= __put_user(regs->xss, (unsigned int __user *)&sc->ss);
269 tmp = save_i387(fpstate);
270 if (tmp < 0)
271 err = 1;
272 else
273 err |= __put_user(tmp ? fpstate : NULL, &sc->fpstate);
275 /* non-iBCS2 extensions.. */
276 err |= __put_user(mask, &sc->oldmask);
277 err |= __put_user(current->thread.cr2, &sc->cr2);
279 return err;
283 * Determine which stack to use..
285 static inline void __user *
286 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
288 unsigned long esp;
290 /* Default to using normal stack */
291 esp = regs->esp;
293 /* This is the X/Open sanctioned signal stack switching. */
294 if (ka->sa.sa_flags & SA_ONSTACK) {
295 if (sas_ss_flags(esp) == 0)
296 esp = current->sas_ss_sp + current->sas_ss_size;
299 /* This is the legacy signal stack switching. */
300 else if ((regs->xss & 0xffff) != __USER_DS &&
301 !(ka->sa.sa_flags & SA_RESTORER) &&
302 ka->sa.sa_restorer) {
303 esp = (unsigned long) ka->sa.sa_restorer;
306 esp -= frame_size;
307 /* Align the stack pointer according to the i386 ABI,
308 * i.e. so that on function entry ((sp + 4) & 15) == 0. */
309 esp = ((esp + 4) & -16ul) - 4;
310 return (void __user *) esp;
313 /* These symbols are defined with the addresses in the vsyscall page.
314 See vsyscall-sigreturn.S. */
315 extern void __user __kernel_sigreturn;
316 extern void __user __kernel_rt_sigreturn;
318 static int setup_frame(int sig, struct k_sigaction *ka,
319 sigset_t *set, struct pt_regs * regs)
321 void __user *restorer;
322 struct sigframe __user *frame;
323 int err = 0;
324 int usig;
326 frame = get_sigframe(ka, regs, sizeof(*frame));
328 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
329 goto give_sigsegv;
331 usig = current_thread_info()->exec_domain
332 && current_thread_info()->exec_domain->signal_invmap
333 && sig < 32
334 ? current_thread_info()->exec_domain->signal_invmap[sig]
335 : sig;
337 err = __put_user(usig, &frame->sig);
338 if (err)
339 goto give_sigsegv;
341 err = setup_sigcontext(&frame->sc, &frame->fpstate, regs, set->sig[0]);
342 if (err)
343 goto give_sigsegv;
345 if (_NSIG_WORDS > 1) {
346 err = __copy_to_user(&frame->extramask, &set->sig[1],
347 sizeof(frame->extramask));
348 if (err)
349 goto give_sigsegv;
352 restorer = (void *)VDSO_SYM(&__kernel_sigreturn);
353 if (ka->sa.sa_flags & SA_RESTORER)
354 restorer = ka->sa.sa_restorer;
356 /* Set up to return from userspace. */
357 err |= __put_user(restorer, &frame->pretcode);
360 * This is popl %eax ; movl $,%eax ; int $0x80
362 * WE DO NOT USE IT ANY MORE! It's only left here for historical
363 * reasons and because gdb uses it as a signature to notice
364 * signal handler stack frames.
366 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
367 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
368 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
370 if (err)
371 goto give_sigsegv;
373 /* Set up registers for signal handler */
374 regs->esp = (unsigned long) frame;
375 regs->eip = (unsigned long) ka->sa.sa_handler;
376 regs->eax = (unsigned long) sig;
377 regs->edx = (unsigned long) 0;
378 regs->ecx = (unsigned long) 0;
380 set_fs(USER_DS);
381 regs->xds = __USER_DS;
382 regs->xes = __USER_DS;
383 regs->xss = __USER_DS;
384 regs->xcs = __USER_CS;
387 * Clear TF when entering the signal handler, but
388 * notify any tracer that was single-stepping it.
389 * The tracer may want to single-step inside the
390 * handler too.
392 regs->eflags &= ~TF_MASK;
393 if (test_thread_flag(TIF_SINGLESTEP))
394 ptrace_notify(SIGTRAP);
396 #if DEBUG_SIG
397 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
398 current->comm, current->pid, frame, regs->eip, frame->pretcode);
399 #endif
401 return 0;
403 give_sigsegv:
404 force_sigsegv(sig, current);
405 return -EFAULT;
408 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
409 sigset_t *set, struct pt_regs * regs)
411 void __user *restorer;
412 struct rt_sigframe __user *frame;
413 int err = 0;
414 int usig;
416 frame = get_sigframe(ka, regs, sizeof(*frame));
418 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
419 goto give_sigsegv;
421 usig = current_thread_info()->exec_domain
422 && current_thread_info()->exec_domain->signal_invmap
423 && sig < 32
424 ? current_thread_info()->exec_domain->signal_invmap[sig]
425 : sig;
427 err |= __put_user(usig, &frame->sig);
428 err |= __put_user(&frame->info, &frame->pinfo);
429 err |= __put_user(&frame->uc, &frame->puc);
430 err |= copy_siginfo_to_user(&frame->info, info);
431 if (err)
432 goto give_sigsegv;
434 /* Create the ucontext. */
435 err |= __put_user(0, &frame->uc.uc_flags);
436 err |= __put_user(0, &frame->uc.uc_link);
437 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
438 err |= __put_user(sas_ss_flags(regs->esp),
439 &frame->uc.uc_stack.ss_flags);
440 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
441 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
442 regs, set->sig[0]);
443 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
444 if (err)
445 goto give_sigsegv;
447 /* Set up to return from userspace. */
448 restorer = (void *)VDSO_SYM(&__kernel_rt_sigreturn);
449 if (ka->sa.sa_flags & SA_RESTORER)
450 restorer = ka->sa.sa_restorer;
451 err |= __put_user(restorer, &frame->pretcode);
454 * This is movl $,%eax ; int $0x80
456 * WE DO NOT USE IT ANY MORE! It's only left here for historical
457 * reasons and because gdb uses it as a signature to notice
458 * signal handler stack frames.
460 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
461 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
462 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
464 if (err)
465 goto give_sigsegv;
467 /* Set up registers for signal handler */
468 regs->esp = (unsigned long) frame;
469 regs->eip = (unsigned long) ka->sa.sa_handler;
470 regs->eax = (unsigned long) usig;
471 regs->edx = (unsigned long) &frame->info;
472 regs->ecx = (unsigned long) &frame->uc;
474 set_fs(USER_DS);
475 regs->xds = __USER_DS;
476 regs->xes = __USER_DS;
477 regs->xss = __USER_DS;
478 regs->xcs = __USER_CS;
481 * Clear TF when entering the signal handler, but
482 * notify any tracer that was single-stepping it.
483 * The tracer may want to single-step inside the
484 * handler too.
486 regs->eflags &= ~TF_MASK;
487 if (test_thread_flag(TIF_SINGLESTEP))
488 ptrace_notify(SIGTRAP);
490 #if DEBUG_SIG
491 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
492 current->comm, current->pid, frame, regs->eip, frame->pretcode);
493 #endif
495 return 0;
497 give_sigsegv:
498 force_sigsegv(sig, current);
499 return -EFAULT;
503 * OK, we're invoking a handler
506 static int
507 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
508 sigset_t *oldset, struct pt_regs * regs)
510 int ret;
512 /* Are we from a system call? */
513 if (regs->orig_eax >= 0) {
514 /* If so, check system call restarting.. */
515 switch (regs->eax) {
516 case -ERESTART_RESTARTBLOCK:
517 case -ERESTARTNOHAND:
518 regs->eax = -EINTR;
519 break;
521 case -ERESTARTSYS:
522 if (!(ka->sa.sa_flags & SA_RESTART)) {
523 regs->eax = -EINTR;
524 break;
526 /* fallthrough */
527 case -ERESTARTNOINTR:
528 regs->eax = regs->orig_eax;
529 regs->eip -= 2;
534 * If TF is set due to a debugger (PT_DTRACE), clear the TF flag so
535 * that register information in the sigcontext is correct.
537 if (unlikely(regs->eflags & TF_MASK)
538 && likely(current->ptrace & PT_DTRACE)) {
539 current->ptrace &= ~PT_DTRACE;
540 regs->eflags &= ~TF_MASK;
543 /* Set up the stack frame */
544 if (ka->sa.sa_flags & SA_SIGINFO)
545 ret = setup_rt_frame(sig, ka, info, oldset, regs);
546 else
547 ret = setup_frame(sig, ka, oldset, regs);
549 if (ret == 0) {
550 spin_lock_irq(&current->sighand->siglock);
551 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
552 if (!(ka->sa.sa_flags & SA_NODEFER))
553 sigaddset(&current->blocked,sig);
554 recalc_sigpending();
555 spin_unlock_irq(&current->sighand->siglock);
558 return ret;
562 * Note that 'init' is a special process: it doesn't get signals it doesn't
563 * want to handle. Thus you cannot kill init even with a SIGKILL even by
564 * mistake.
566 static void fastcall do_signal(struct pt_regs *regs)
568 siginfo_t info;
569 int signr;
570 struct k_sigaction ka;
571 sigset_t *oldset;
574 * We want the common case to go fast, which
575 * is why we may in certain cases get here from
576 * kernel mode. Just return without doing anything
577 * if so. vm86 regs switched out by assembly code
578 * before reaching here, so testing against kernel
579 * CS suffices.
581 if (!user_mode(regs))
582 return;
584 if (test_thread_flag(TIF_RESTORE_SIGMASK))
585 oldset = &current->saved_sigmask;
586 else
587 oldset = &current->blocked;
589 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
590 if (signr > 0) {
591 /* Reenable any watchpoints before delivering the
592 * signal to user space. The processor register will
593 * have been cleared if the watchpoint triggered
594 * inside the kernel.
596 if (unlikely(current->thread.debugreg[7]))
597 set_debugreg(current->thread.debugreg[7], 7);
599 /* Whee! Actually deliver the signal. */
600 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
601 /* a signal was successfully delivered; the saved
602 * sigmask will have been stored in the signal frame,
603 * and will be restored by sigreturn, so we can simply
604 * clear the TIF_RESTORE_SIGMASK flag */
605 if (test_thread_flag(TIF_RESTORE_SIGMASK))
606 clear_thread_flag(TIF_RESTORE_SIGMASK);
609 return;
612 /* Did we come from a system call? */
613 if (regs->orig_eax >= 0) {
614 /* Restart the system call - no handlers present */
615 switch (regs->eax) {
616 case -ERESTARTNOHAND:
617 case -ERESTARTSYS:
618 case -ERESTARTNOINTR:
619 regs->eax = regs->orig_eax;
620 regs->eip -= 2;
621 break;
623 case -ERESTART_RESTARTBLOCK:
624 regs->eax = __NR_restart_syscall;
625 regs->eip -= 2;
626 break;
630 /* if there's no signal to deliver, we just put the saved sigmask
631 * back */
632 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
633 clear_thread_flag(TIF_RESTORE_SIGMASK);
634 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
639 * notification of userspace execution resumption
640 * - triggered by the TIF_WORK_MASK flags
642 __attribute__((regparm(3)))
643 void do_notify_resume(struct pt_regs *regs, void *_unused,
644 __u32 thread_info_flags)
646 /* Pending single-step? */
647 if (thread_info_flags & _TIF_SINGLESTEP) {
648 regs->eflags |= TF_MASK;
649 clear_thread_flag(TIF_SINGLESTEP);
652 /* deal with pending signal delivery */
653 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
654 do_signal(regs);
656 clear_thread_flag(TIF_IRET);