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/sched.h>
13 #include <linux/personality.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/compiler.h>
26 #include <linux/bitops.h>
27 #include <asm/cacheflush.h>
30 #include <asm/uaccess.h>
31 #include <asm/ucontext.h>
32 #include <asm/cpu-features.h>
35 #include "signal-common.h"
38 * Horribly complicated - with the bloody RM9000 workarounds enabled
39 * the signal trampolines is moving to the end of the structure so we can
40 * increase the alignment without breaking software compatibility.
42 #if ICACHE_REFILLS_WORKAROUND_WAR == 0
45 u32 sf_ass[4]; /* argument save space for o32 */
46 u32 sf_code[2]; /* signal trampoline */
47 struct sigcontext sf_sc;
52 u32 rs_ass[4]; /* argument save space for o32 */
53 u32 rs_code[2]; /* signal trampoline */
54 struct siginfo rs_info;
55 struct ucontext rs_uc;
61 u32 sf_ass[4]; /* argument save space for o32 */
63 struct sigcontext sf_sc; /* hw context */
65 u32 sf_code[8] ____cacheline_aligned; /* signal trampoline */
69 u32 rs_ass[4]; /* argument save space for o32 */
71 struct siginfo rs_info;
72 struct ucontext rs_uc;
73 u32 rs_code[8] ____cacheline_aligned; /* signal trampoline */
81 int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
85 unsigned int used_math;
87 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
89 err |= __put_user(0, &sc->sc_regs[0]);
90 for (i = 1; i < 32; i++)
91 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
93 #ifdef CONFIG_CPU_HAS_SMARTMIPS
94 err |= __put_user(regs->acx, &sc->sc_acx);
96 err |= __put_user(regs->hi, &sc->sc_mdhi);
97 err |= __put_user(regs->lo, &sc->sc_mdlo);
99 err |= __put_user(mfhi1(), &sc->sc_hi1);
100 err |= __put_user(mflo1(), &sc->sc_lo1);
101 err |= __put_user(mfhi2(), &sc->sc_hi2);
102 err |= __put_user(mflo2(), &sc->sc_lo2);
103 err |= __put_user(mfhi3(), &sc->sc_hi3);
104 err |= __put_user(mflo3(), &sc->sc_lo3);
105 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
108 used_math = !!used_math();
109 err |= __put_user(used_math, &sc->sc_used_math);
113 * Save FPU state to signal context. Signal handler
114 * will "inherit" current FPU state.
117 enable_fp_in_kernel();
118 err |= save_fp_context(sc);
119 disable_fp_in_kernel();
124 int fpcsr_pending(unsigned int __user *fpcsr)
127 unsigned int csr, enabled;
129 err = __get_user(csr, fpcsr);
130 enabled = FPU_CSR_UNI_X | ((csr & FPU_CSR_ALL_E) << 5);
132 * If the signal handler set some FPU exceptions, clear it and
137 err |= __put_user(csr, fpcsr);
144 check_and_restore_fp_context(struct sigcontext __user *sc)
148 err = sig = fpcsr_pending(&sc->sc_fpc_csr);
151 err |= restore_fp_context(sc);
155 int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
157 unsigned int used_math;
162 /* Always make any pending restarted system calls return -EINTR */
163 current_thread_info()->restart_block.fn = do_no_restart_syscall;
165 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
167 #ifdef CONFIG_CPU_HAS_SMARTMIPS
168 err |= __get_user(regs->acx, &sc->sc_acx);
170 err |= __get_user(regs->hi, &sc->sc_mdhi);
171 err |= __get_user(regs->lo, &sc->sc_mdlo);
173 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
174 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
175 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
176 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
177 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
178 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
179 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
182 for (i = 1; i < 32; i++)
183 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
185 err |= __get_user(used_math, &sc->sc_used_math);
186 conditional_used_math(used_math);
189 /* restore fpu context if we have used it before */
191 enable_fp_in_kernel();
193 err = check_and_restore_fp_context(sc);
194 disable_fp_in_kernel();
196 /* signal handler may have used FPU. Give it up. */
203 void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
208 /* Default to using normal stack */
212 * FPU emulator may have it's own trampoline active just
213 * above the user stack, 16-bytes before the next lowest
214 * 16 byte boundary. Try to avoid trashing it.
218 /* This is the X/Open sanctioned signal stack switching. */
219 if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
220 sp = current->sas_ss_sp + current->sas_ss_size;
222 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
225 int install_sigtramp(unsigned int __user *tramp, unsigned int syscall)
230 * Set up the return code ...
232 * li v0, __NR__foo_sigreturn
236 err = __put_user(0x24020000 + syscall, tramp + 0);
237 err |= __put_user(0x0000000c , tramp + 1);
238 if (ICACHE_REFILLS_WORKAROUND_WAR) {
239 err |= __put_user(0, tramp + 2);
240 err |= __put_user(0, tramp + 3);
241 err |= __put_user(0, tramp + 4);
242 err |= __put_user(0, tramp + 5);
243 err |= __put_user(0, tramp + 6);
244 err |= __put_user(0, tramp + 7);
246 flush_cache_sigtramp((unsigned long) tramp);
252 * Atomically swap in the new signal mask, and wait for a signal.
255 #ifdef CONFIG_TRAD_SIGNALS
256 asmlinkage int sys_sigsuspend(nabi_no_regargs struct pt_regs regs)
259 sigset_t __user *uset;
261 uset = (sigset_t __user *) regs.regs[4];
262 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
264 sigdelsetmask(&newset, ~_BLOCKABLE);
266 spin_lock_irq(¤t->sighand->siglock);
267 current->saved_sigmask = current->blocked;
268 current->blocked = newset;
270 spin_unlock_irq(¤t->sighand->siglock);
272 current->state = TASK_INTERRUPTIBLE;
274 set_thread_flag(TIF_RESTORE_SIGMASK);
275 return -ERESTARTNOHAND;
279 asmlinkage int sys_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
282 sigset_t __user *unewset;
285 /* XXX Don't preclude handling different sized sigset_t's. */
286 sigsetsize = regs.regs[5];
287 if (sigsetsize != sizeof(sigset_t))
290 unewset = (sigset_t __user *) regs.regs[4];
291 if (copy_from_user(&newset, unewset, sizeof(newset)))
293 sigdelsetmask(&newset, ~_BLOCKABLE);
295 spin_lock_irq(¤t->sighand->siglock);
296 current->saved_sigmask = current->blocked;
297 current->blocked = newset;
299 spin_unlock_irq(¤t->sighand->siglock);
301 current->state = TASK_INTERRUPTIBLE;
303 set_thread_flag(TIF_RESTORE_SIGMASK);
304 return -ERESTARTNOHAND;
307 #ifdef CONFIG_TRAD_SIGNALS
308 asmlinkage int sys_sigaction(int sig, const struct sigaction __user *act,
309 struct sigaction __user *oact)
311 struct k_sigaction new_ka, old_ka;
318 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
320 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
321 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
322 err |= __get_user(mask, &act->sa_mask.sig[0]);
326 siginitset(&new_ka.sa.sa_mask, mask);
329 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
332 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
334 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
335 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
336 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
337 err |= __put_user(0, &oact->sa_mask.sig[1]);
338 err |= __put_user(0, &oact->sa_mask.sig[2]);
339 err |= __put_user(0, &oact->sa_mask.sig[3]);
348 asmlinkage int sys_sigaltstack(nabi_no_regargs struct pt_regs regs)
350 const stack_t __user *uss = (const stack_t __user *) regs.regs[4];
351 stack_t __user *uoss = (stack_t __user *) regs.regs[5];
352 unsigned long usp = regs.regs[29];
354 return do_sigaltstack(uss, uoss, usp);
357 #ifdef CONFIG_TRAD_SIGNALS
358 asmlinkage void sys_sigreturn(nabi_no_regargs struct pt_regs regs)
360 struct sigframe __user *frame;
364 frame = (struct sigframe __user *) regs.regs[29];
365 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
367 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
370 sigdelsetmask(&blocked, ~_BLOCKABLE);
371 spin_lock_irq(¤t->sighand->siglock);
372 current->blocked = blocked;
374 spin_unlock_irq(¤t->sighand->siglock);
376 sig = restore_sigcontext(®s, &frame->sf_sc);
380 force_sig(sig, current);
383 * Don't let your children do this ...
385 __asm__ __volatile__(
393 force_sig(SIGSEGV, current);
395 #endif /* CONFIG_TRAD_SIGNALS */
397 asmlinkage void sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
399 struct rt_sigframe __user *frame;
404 frame = (struct rt_sigframe __user *) regs.regs[29];
405 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
407 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
410 sigdelsetmask(&set, ~_BLOCKABLE);
411 spin_lock_irq(¤t->sighand->siglock);
412 current->blocked = set;
414 spin_unlock_irq(¤t->sighand->siglock);
416 sig = restore_sigcontext(®s, &frame->rs_uc.uc_mcontext);
420 force_sig(sig, current);
422 if (__copy_from_user(&st, &frame->rs_uc.uc_stack, sizeof(st)))
424 /* It is more difficult to avoid calling this function than to
425 call it and ignore errors. */
426 do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]);
429 * Don't let your children do this ...
431 __asm__ __volatile__(
439 force_sig(SIGSEGV, current);
442 #ifdef CONFIG_TRAD_SIGNALS
443 static int setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
444 int signr, sigset_t *set)
446 struct sigframe __user *frame;
449 frame = get_sigframe(ka, regs, sizeof(*frame));
450 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
453 err |= install_sigtramp(frame->sf_code, __NR_sigreturn);
455 err |= setup_sigcontext(regs, &frame->sf_sc);
456 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
461 * Arguments to signal handler:
464 * a1 = 0 (should be cause)
465 * a2 = pointer to struct sigcontext
467 * $25 and c0_epc point to the signal handler, $29 points to the
470 regs->regs[ 4] = signr;
472 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
473 regs->regs[29] = (unsigned long) frame;
474 regs->regs[31] = (unsigned long) frame->sf_code;
475 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
477 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
478 current->comm, current->pid,
479 frame, regs->cp0_epc, regs->regs[31]);
483 force_sigsegv(signr, current);
488 static int setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
489 int signr, sigset_t *set, siginfo_t *info)
491 struct rt_sigframe __user *frame;
494 frame = get_sigframe(ka, regs, sizeof(*frame));
495 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
498 err |= install_sigtramp(frame->rs_code, __NR_rt_sigreturn);
500 /* Create siginfo. */
501 err |= copy_siginfo_to_user(&frame->rs_info, info);
503 /* Create the ucontext. */
504 err |= __put_user(0, &frame->rs_uc.uc_flags);
505 err |= __put_user(NULL, &frame->rs_uc.uc_link);
506 err |= __put_user((void __user *)current->sas_ss_sp,
507 &frame->rs_uc.uc_stack.ss_sp);
508 err |= __put_user(sas_ss_flags(regs->regs[29]),
509 &frame->rs_uc.uc_stack.ss_flags);
510 err |= __put_user(current->sas_ss_size,
511 &frame->rs_uc.uc_stack.ss_size);
512 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
513 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
519 * Arguments to signal handler:
522 * a1 = 0 (should be cause)
523 * a2 = pointer to ucontext
525 * $25 and c0_epc point to the signal handler, $29 points to
526 * the struct rt_sigframe.
528 regs->regs[ 4] = signr;
529 regs->regs[ 5] = (unsigned long) &frame->rs_info;
530 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
531 regs->regs[29] = (unsigned long) frame;
532 regs->regs[31] = (unsigned long) frame->rs_code;
533 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
535 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
536 current->comm, current->pid,
537 frame, regs->cp0_epc, regs->regs[31]);
542 force_sigsegv(signr, current);
546 struct mips_abi mips_abi = {
547 #ifdef CONFIG_TRAD_SIGNALS
548 .setup_frame = setup_frame,
550 .setup_rt_frame = setup_rt_frame,
551 .restart = __NR_restart_syscall
554 static int handle_signal(unsigned long sig, siginfo_t *info,
555 struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
559 switch(regs->regs[0]) {
560 case ERESTART_RESTARTBLOCK:
562 regs->regs[2] = EINTR;
565 if (!(ka->sa.sa_flags & SA_RESTART)) {
566 regs->regs[2] = EINTR;
570 case ERESTARTNOINTR: /* Userland will reload $v0. */
571 regs->regs[7] = regs->regs[26];
575 regs->regs[0] = 0; /* Don't deal with this again. */
577 if (sig_uses_siginfo(ka))
578 ret = current->thread.abi->setup_rt_frame(ka, regs, sig, oldset, info);
580 ret = current->thread.abi->setup_frame(ka, regs, sig, oldset);
582 spin_lock_irq(¤t->sighand->siglock);
583 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
584 if (!(ka->sa.sa_flags & SA_NODEFER))
585 sigaddset(¤t->blocked,sig);
587 spin_unlock_irq(¤t->sighand->siglock);
592 static void do_signal(struct pt_regs *regs)
594 struct k_sigaction ka;
600 * We want the common case to go fast, which is why we may in certain
601 * cases get here from kernel mode. Just return without doing anything
604 if (!user_mode(regs))
607 if (test_thread_flag(TIF_RESTORE_SIGMASK))
608 oldset = ¤t->saved_sigmask;
610 oldset = ¤t->blocked;
612 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
614 /* Whee! Actually deliver the signal. */
615 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
617 * A signal was successfully delivered; the saved
618 * sigmask will have been stored in the signal frame,
619 * and will be restored by sigreturn, so we can simply
620 * clear the TIF_RESTORE_SIGMASK flag.
622 if (test_thread_flag(TIF_RESTORE_SIGMASK))
623 clear_thread_flag(TIF_RESTORE_SIGMASK);
630 * Who's code doesn't conform to the restartable syscall convention
631 * dies here!!! The li instruction, a single machine instruction,
632 * must directly be followed by the syscall instruction.
635 if (regs->regs[2] == ERESTARTNOHAND ||
636 regs->regs[2] == ERESTARTSYS ||
637 regs->regs[2] == ERESTARTNOINTR) {
638 regs->regs[7] = regs->regs[26];
641 if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
642 regs->regs[2] = current->thread.abi->restart;
643 regs->regs[7] = regs->regs[26];
646 regs->regs[0] = 0; /* Don't deal with this again. */
650 * If there's no signal to deliver, we just put the saved sigmask
653 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
654 clear_thread_flag(TIF_RESTORE_SIGMASK);
655 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
660 * notification of userspace execution resumption
661 * - triggered by the TIF_WORK_MASK flags
663 asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
664 __u32 thread_info_flags)
666 /* deal with pending signal delivery */
667 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))