2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 * Copyright (C) 1994 - 2000 Ralf Baechle
8 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
10 #include <linux/cache.h>
11 #include <linux/irqflags.h>
12 #include <linux/sched.h>
14 #include <linux/personality.h>
15 #include <linux/smp.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/compiler.h>
23 #include <linux/syscalls.h>
24 #include <linux/uaccess.h>
25 #include <linux/tracehook.h>
29 #include <linux/bitops.h>
30 #include <asm/cacheflush.h>
33 #include <asm/ucontext.h>
34 #include <asm/cpu-features.h>
39 #include "signal-common.h"
41 static int (*save_fp_context)(struct sigcontext __user *sc);
42 static int (*restore_fp_context)(struct sigcontext __user *sc);
44 extern asmlinkage int _save_fp_context(struct sigcontext __user *sc);
45 extern asmlinkage int _restore_fp_context(struct sigcontext __user *sc);
47 extern asmlinkage int fpu_emulator_save_context(struct sigcontext __user *sc);
48 extern asmlinkage int fpu_emulator_restore_context(struct sigcontext __user *sc);
51 u32 sf_ass[4]; /* argument save space for o32 */
52 u32 sf_pad[2]; /* Was: signal trampoline */
53 struct sigcontext sf_sc;
58 u32 rs_ass[4]; /* argument save space for o32 */
59 u32 rs_pad[2]; /* Was: signal trampoline */
60 struct siginfo rs_info;
61 struct ucontext rs_uc;
67 static int protected_save_fp_context(struct sigcontext __user *sc)
73 err = save_fp_context(sc); /* this might fail */
77 /* touch the sigcontext and try again */
78 err = __put_user(0, &sc->sc_fpregs[0]) |
79 __put_user(0, &sc->sc_fpregs[31]) |
80 __put_user(0, &sc->sc_fpc_csr);
82 break; /* really bad sigcontext */
87 static int protected_restore_fp_context(struct sigcontext __user *sc)
89 int err, tmp __maybe_unused;
93 err = restore_fp_context(sc); /* this might fail */
97 /* touch the sigcontext and try again */
98 err = __get_user(tmp, &sc->sc_fpregs[0]) |
99 __get_user(tmp, &sc->sc_fpregs[31]) |
100 __get_user(tmp, &sc->sc_fpc_csr);
102 break; /* really bad sigcontext */
107 int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
111 unsigned int used_math;
113 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
115 err |= __put_user(0, &sc->sc_regs[0]);
116 for (i = 1; i < 32; i++)
117 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
119 #ifdef CONFIG_CPU_HAS_SMARTMIPS
120 err |= __put_user(regs->acx, &sc->sc_acx);
122 err |= __put_user(regs->hi, &sc->sc_mdhi);
123 err |= __put_user(regs->lo, &sc->sc_mdlo);
125 err |= __put_user(mfhi1(), &sc->sc_hi1);
126 err |= __put_user(mflo1(), &sc->sc_lo1);
127 err |= __put_user(mfhi2(), &sc->sc_hi2);
128 err |= __put_user(mflo2(), &sc->sc_lo2);
129 err |= __put_user(mfhi3(), &sc->sc_hi3);
130 err |= __put_user(mflo3(), &sc->sc_lo3);
131 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
134 used_math = !!used_math();
135 err |= __put_user(used_math, &sc->sc_used_math);
139 * Save FPU state to signal context. Signal handler
140 * will "inherit" current FPU state.
142 err |= protected_save_fp_context(sc);
147 int fpcsr_pending(unsigned int __user *fpcsr)
150 unsigned int csr, enabled;
152 err = __get_user(csr, fpcsr);
153 enabled = FPU_CSR_UNI_X | ((csr & FPU_CSR_ALL_E) << 5);
155 * If the signal handler set some FPU exceptions, clear it and
160 err |= __put_user(csr, fpcsr);
167 check_and_restore_fp_context(struct sigcontext __user *sc)
171 err = sig = fpcsr_pending(&sc->sc_fpc_csr);
174 err |= protected_restore_fp_context(sc);
178 int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
180 unsigned int used_math;
185 /* Always make any pending restarted system calls return -EINTR */
186 current_thread_info()->restart_block.fn = do_no_restart_syscall;
188 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
190 #ifdef CONFIG_CPU_HAS_SMARTMIPS
191 err |= __get_user(regs->acx, &sc->sc_acx);
193 err |= __get_user(regs->hi, &sc->sc_mdhi);
194 err |= __get_user(regs->lo, &sc->sc_mdlo);
196 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
197 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
198 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
199 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
200 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
201 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
202 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
205 for (i = 1; i < 32; i++)
206 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
208 err |= __get_user(used_math, &sc->sc_used_math);
209 conditional_used_math(used_math);
212 /* restore fpu context if we have used it before */
214 err = check_and_restore_fp_context(sc);
216 /* signal handler may have used FPU. Give it up. */
223 void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
228 /* Default to using normal stack */
232 * FPU emulator may have it's own trampoline active just
233 * above the user stack, 16-bytes before the next lowest
234 * 16 byte boundary. Try to avoid trashing it.
238 /* This is the X/Open sanctioned signal stack switching. */
239 if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
240 sp = current->sas_ss_sp + current->sas_ss_size;
242 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
246 * Atomically swap in the new signal mask, and wait for a signal.
249 #ifdef CONFIG_TRAD_SIGNALS
250 asmlinkage int sys_sigsuspend(nabi_no_regargs struct pt_regs regs)
253 sigset_t __user *uset;
255 uset = (sigset_t __user *) regs.regs[4];
256 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
258 return sigsuspend(&newset);
262 asmlinkage int sys_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
265 sigset_t __user *unewset;
268 /* XXX Don't preclude handling different sized sigset_t's. */
269 sigsetsize = regs.regs[5];
270 if (sigsetsize != sizeof(sigset_t))
273 unewset = (sigset_t __user *) regs.regs[4];
274 if (copy_from_user(&newset, unewset, sizeof(newset)))
276 return sigsuspend(&newset);
279 #ifdef CONFIG_TRAD_SIGNALS
280 SYSCALL_DEFINE3(sigaction, int, sig, const struct sigaction __user *, act,
281 struct sigaction __user *, oact)
283 struct k_sigaction new_ka, old_ka;
290 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
292 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
293 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
294 err |= __get_user(mask, &act->sa_mask.sig[0]);
298 siginitset(&new_ka.sa.sa_mask, mask);
301 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
304 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
306 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
307 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
308 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
309 err |= __put_user(0, &oact->sa_mask.sig[1]);
310 err |= __put_user(0, &oact->sa_mask.sig[2]);
311 err |= __put_user(0, &oact->sa_mask.sig[3]);
320 asmlinkage int sys_sigaltstack(nabi_no_regargs struct pt_regs regs)
322 const stack_t __user *uss = (const stack_t __user *) regs.regs[4];
323 stack_t __user *uoss = (stack_t __user *) regs.regs[5];
324 unsigned long usp = regs.regs[29];
326 return do_sigaltstack(uss, uoss, usp);
329 #ifdef CONFIG_TRAD_SIGNALS
330 asmlinkage void sys_sigreturn(nabi_no_regargs struct pt_regs regs)
332 struct sigframe __user *frame;
336 frame = (struct sigframe __user *) regs.regs[29];
337 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
339 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
342 sigdelsetmask(&blocked, ~_BLOCKABLE);
343 set_current_blocked(&blocked);
345 sig = restore_sigcontext(®s, &frame->sf_sc);
349 force_sig(sig, current);
352 * Don't let your children do this ...
354 __asm__ __volatile__(
362 force_sig(SIGSEGV, current);
364 #endif /* CONFIG_TRAD_SIGNALS */
366 asmlinkage void sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
368 struct rt_sigframe __user *frame;
372 frame = (struct rt_sigframe __user *) regs.regs[29];
373 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
375 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
378 sigdelsetmask(&set, ~_BLOCKABLE);
379 set_current_blocked(&set);
381 sig = restore_sigcontext(®s, &frame->rs_uc.uc_mcontext);
385 force_sig(sig, current);
387 /* It is more difficult to avoid calling this function than to
388 call it and ignore errors. */
389 do_sigaltstack(&frame->rs_uc.uc_stack, NULL, regs.regs[29]);
392 * Don't let your children do this ...
394 __asm__ __volatile__(
402 force_sig(SIGSEGV, current);
405 #ifdef CONFIG_TRAD_SIGNALS
406 static int setup_frame(void *sig_return, struct k_sigaction *ka,
407 struct pt_regs *regs, int signr, sigset_t *set)
409 struct sigframe __user *frame;
412 frame = get_sigframe(ka, regs, sizeof(*frame));
413 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
416 err |= setup_sigcontext(regs, &frame->sf_sc);
417 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
422 * Arguments to signal handler:
425 * a1 = 0 (should be cause)
426 * a2 = pointer to struct sigcontext
428 * $25 and c0_epc point to the signal handler, $29 points to the
431 regs->regs[ 4] = signr;
433 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
434 regs->regs[29] = (unsigned long) frame;
435 regs->regs[31] = (unsigned long) sig_return;
436 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
438 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
439 current->comm, current->pid,
440 frame, regs->cp0_epc, regs->regs[31]);
444 force_sigsegv(signr, current);
449 static int setup_rt_frame(void *sig_return, struct k_sigaction *ka,
450 struct pt_regs *regs, int signr, sigset_t *set,
453 struct rt_sigframe __user *frame;
456 frame = get_sigframe(ka, regs, sizeof(*frame));
457 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
460 /* Create siginfo. */
461 err |= copy_siginfo_to_user(&frame->rs_info, info);
463 /* Create the ucontext. */
464 err |= __put_user(0, &frame->rs_uc.uc_flags);
465 err |= __put_user(NULL, &frame->rs_uc.uc_link);
466 err |= __put_user((void __user *)current->sas_ss_sp,
467 &frame->rs_uc.uc_stack.ss_sp);
468 err |= __put_user(sas_ss_flags(regs->regs[29]),
469 &frame->rs_uc.uc_stack.ss_flags);
470 err |= __put_user(current->sas_ss_size,
471 &frame->rs_uc.uc_stack.ss_size);
472 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
473 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
479 * Arguments to signal handler:
482 * a1 = 0 (should be cause)
483 * a2 = pointer to ucontext
485 * $25 and c0_epc point to the signal handler, $29 points to
486 * the struct rt_sigframe.
488 regs->regs[ 4] = signr;
489 regs->regs[ 5] = (unsigned long) &frame->rs_info;
490 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
491 regs->regs[29] = (unsigned long) frame;
492 regs->regs[31] = (unsigned long) sig_return;
493 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
495 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
496 current->comm, current->pid,
497 frame, regs->cp0_epc, regs->regs[31]);
502 force_sigsegv(signr, current);
506 struct mips_abi mips_abi = {
507 #ifdef CONFIG_TRAD_SIGNALS
508 .setup_frame = setup_frame,
509 .signal_return_offset = offsetof(struct mips_vdso, signal_trampoline),
511 .setup_rt_frame = setup_rt_frame,
512 .rt_signal_return_offset =
513 offsetof(struct mips_vdso, rt_signal_trampoline),
514 .restart = __NR_restart_syscall
517 static int handle_signal(unsigned long sig, siginfo_t *info,
518 struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
521 struct mips_abi *abi = current->thread.abi;
522 void *vdso = current->mm->context.vdso;
525 switch(regs->regs[2]) {
526 case ERESTART_RESTARTBLOCK:
528 regs->regs[2] = EINTR;
531 if (!(ka->sa.sa_flags & SA_RESTART)) {
532 regs->regs[2] = EINTR;
537 regs->regs[7] = regs->regs[26];
538 regs->regs[2] = regs->regs[0];
542 regs->regs[0] = 0; /* Don't deal with this again. */
545 if (sig_uses_siginfo(ka))
546 ret = abi->setup_rt_frame(vdso + abi->rt_signal_return_offset,
547 ka, regs, sig, oldset, info);
549 ret = abi->setup_frame(vdso + abi->signal_return_offset,
550 ka, regs, sig, oldset);
555 block_sigmask(ka, sig);
560 static void do_signal(struct pt_regs *regs)
562 struct k_sigaction ka;
568 * We want the common case to go fast, which is why we may in certain
569 * cases get here from kernel mode. Just return without doing anything
572 if (!user_mode(regs))
575 if (test_thread_flag(TIF_RESTORE_SIGMASK))
576 oldset = ¤t->saved_sigmask;
578 oldset = ¤t->blocked;
580 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
582 /* Whee! Actually deliver the signal. */
583 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
585 * A signal was successfully delivered; the saved
586 * sigmask will have been stored in the signal frame,
587 * and will be restored by sigreturn, so we can simply
588 * clear the TIF_RESTORE_SIGMASK flag.
590 if (test_thread_flag(TIF_RESTORE_SIGMASK))
591 clear_thread_flag(TIF_RESTORE_SIGMASK);
598 if (regs->regs[2] == ERESTARTNOHAND ||
599 regs->regs[2] == ERESTARTSYS ||
600 regs->regs[2] == ERESTARTNOINTR) {
601 regs->regs[2] = regs->regs[0];
602 regs->regs[7] = regs->regs[26];
605 if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
606 regs->regs[2] = current->thread.abi->restart;
607 regs->regs[7] = regs->regs[26];
610 regs->regs[0] = 0; /* Don't deal with this again. */
614 * If there's no signal to deliver, we just put the saved sigmask
617 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
618 clear_thread_flag(TIF_RESTORE_SIGMASK);
619 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
624 * notification of userspace execution resumption
625 * - triggered by the TIF_WORK_MASK flags
627 asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
628 __u32 thread_info_flags)
632 /* deal with pending signal delivery */
633 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
636 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
637 clear_thread_flag(TIF_NOTIFY_RESUME);
638 tracehook_notify_resume(regs);
643 static int smp_save_fp_context(struct sigcontext __user *sc)
645 return raw_cpu_has_fpu
646 ? _save_fp_context(sc)
647 : fpu_emulator_save_context(sc);
650 static int smp_restore_fp_context(struct sigcontext __user *sc)
652 return raw_cpu_has_fpu
653 ? _restore_fp_context(sc)
654 : fpu_emulator_restore_context(sc);
658 static int signal_setup(void)
661 /* For now just do the cpu_has_fpu check when the functions are invoked */
662 save_fp_context = smp_save_fp_context;
663 restore_fp_context = smp_restore_fp_context;
666 save_fp_context = _save_fp_context;
667 restore_fp_context = _restore_fp_context;
669 save_fp_context = fpu_emulator_save_context;
670 restore_fp_context = fpu_emulator_restore_context;
677 arch_initcall(signal_setup);