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)
86 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
88 err |= __put_user(0, &sc->sc_regs[0]);
89 for (i = 1; i < 32; i++)
90 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
92 err |= __put_user(regs->hi, &sc->sc_mdhi);
93 err |= __put_user(regs->lo, &sc->sc_mdlo);
95 err |= __put_user(mfhi1(), &sc->sc_hi1);
96 err |= __put_user(mflo1(), &sc->sc_lo1);
97 err |= __put_user(mfhi2(), &sc->sc_hi2);
98 err |= __put_user(mflo2(), &sc->sc_lo2);
99 err |= __put_user(mfhi3(), &sc->sc_hi3);
100 err |= __put_user(mflo3(), &sc->sc_lo3);
101 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
104 err |= __put_user(!!used_math(), &sc->sc_used_math);
108 * Save FPU state to signal context. Signal handler
109 * will "inherit" current FPU state.
113 if (!is_fpu_owner()) {
117 err |= save_fp_context(sc);
124 int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
126 unsigned int used_math;
131 /* Always make any pending restarted system calls return -EINTR */
132 current_thread_info()->restart_block.fn = do_no_restart_syscall;
134 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
135 err |= __get_user(regs->hi, &sc->sc_mdhi);
136 err |= __get_user(regs->lo, &sc->sc_mdlo);
138 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
139 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
140 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
141 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
142 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
143 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
144 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
147 for (i = 1; i < 32; i++)
148 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
150 err |= __get_user(used_math, &sc->sc_used_math);
151 conditional_used_math(used_math);
156 /* restore fpu context if we have used it before */
158 err |= restore_fp_context(sc);
160 /* signal handler may have used FPU. Give it up. */
169 void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
174 /* Default to using normal stack */
178 * FPU emulator may have it's own trampoline active just
179 * above the user stack, 16-bytes before the next lowest
180 * 16 byte boundary. Try to avoid trashing it.
184 /* This is the X/Open sanctioned signal stack switching. */
185 if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
186 sp = current->sas_ss_sp + current->sas_ss_size;
188 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
191 int install_sigtramp(unsigned int __user *tramp, unsigned int syscall)
196 * Set up the return code ...
198 * li v0, __NR__foo_sigreturn
202 err = __put_user(0x24020000 + syscall, tramp + 0);
203 err |= __put_user(0x0000000c , tramp + 1);
204 if (ICACHE_REFILLS_WORKAROUND_WAR) {
205 err |= __put_user(0, tramp + 2);
206 err |= __put_user(0, tramp + 3);
207 err |= __put_user(0, tramp + 4);
208 err |= __put_user(0, tramp + 5);
209 err |= __put_user(0, tramp + 6);
210 err |= __put_user(0, tramp + 7);
212 flush_cache_sigtramp((unsigned long) tramp);
218 * Atomically swap in the new signal mask, and wait for a signal.
221 #ifdef CONFIG_TRAD_SIGNALS
222 asmlinkage int sys_sigsuspend(nabi_no_regargs struct pt_regs regs)
225 sigset_t __user *uset;
227 uset = (sigset_t __user *) regs.regs[4];
228 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
230 sigdelsetmask(&newset, ~_BLOCKABLE);
232 spin_lock_irq(¤t->sighand->siglock);
233 current->saved_sigmask = current->blocked;
234 current->blocked = newset;
236 spin_unlock_irq(¤t->sighand->siglock);
238 current->state = TASK_INTERRUPTIBLE;
240 set_thread_flag(TIF_RESTORE_SIGMASK);
241 return -ERESTARTNOHAND;
245 asmlinkage int sys_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
248 sigset_t __user *unewset;
251 /* XXX Don't preclude handling different sized sigset_t's. */
252 sigsetsize = regs.regs[5];
253 if (sigsetsize != sizeof(sigset_t))
256 unewset = (sigset_t __user *) regs.regs[4];
257 if (copy_from_user(&newset, unewset, sizeof(newset)))
259 sigdelsetmask(&newset, ~_BLOCKABLE);
261 spin_lock_irq(¤t->sighand->siglock);
262 current->saved_sigmask = current->blocked;
263 current->blocked = newset;
265 spin_unlock_irq(¤t->sighand->siglock);
267 current->state = TASK_INTERRUPTIBLE;
269 set_thread_flag(TIF_RESTORE_SIGMASK);
270 return -ERESTARTNOHAND;
273 #ifdef CONFIG_TRAD_SIGNALS
274 asmlinkage int sys_sigaction(int sig, const struct sigaction __user *act,
275 struct sigaction __user *oact)
277 struct k_sigaction new_ka, old_ka;
284 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
286 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
287 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
288 err |= __get_user(mask, &act->sa_mask.sig[0]);
292 siginitset(&new_ka.sa.sa_mask, mask);
295 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
298 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
300 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
301 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
302 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
303 err |= __put_user(0, &oact->sa_mask.sig[1]);
304 err |= __put_user(0, &oact->sa_mask.sig[2]);
305 err |= __put_user(0, &oact->sa_mask.sig[3]);
314 asmlinkage int sys_sigaltstack(nabi_no_regargs struct pt_regs regs)
316 const stack_t __user *uss = (const stack_t __user *) regs.regs[4];
317 stack_t __user *uoss = (stack_t __user *) regs.regs[5];
318 unsigned long usp = regs.regs[29];
320 return do_sigaltstack(uss, uoss, usp);
323 #ifdef CONFIG_TRAD_SIGNALS
324 asmlinkage void sys_sigreturn(nabi_no_regargs struct pt_regs regs)
326 struct sigframe __user *frame;
329 frame = (struct sigframe __user *) regs.regs[29];
330 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
332 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
335 sigdelsetmask(&blocked, ~_BLOCKABLE);
336 spin_lock_irq(¤t->sighand->siglock);
337 current->blocked = blocked;
339 spin_unlock_irq(¤t->sighand->siglock);
341 if (restore_sigcontext(®s, &frame->sf_sc))
345 * Don't let your children do this ...
347 __asm__ __volatile__(
355 force_sig(SIGSEGV, current);
357 #endif /* CONFIG_TRAD_SIGNALS */
359 asmlinkage void sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
361 struct rt_sigframe __user *frame;
365 frame = (struct rt_sigframe __user *) regs.regs[29];
366 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
368 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
371 sigdelsetmask(&set, ~_BLOCKABLE);
372 spin_lock_irq(¤t->sighand->siglock);
373 current->blocked = set;
375 spin_unlock_irq(¤t->sighand->siglock);
377 if (restore_sigcontext(®s, &frame->rs_uc.uc_mcontext))
380 if (__copy_from_user(&st, &frame->rs_uc.uc_stack, sizeof(st)))
382 /* It is more difficult to avoid calling this function than to
383 call it and ignore errors. */
384 do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]);
387 * Don't let your children do this ...
389 __asm__ __volatile__(
397 force_sig(SIGSEGV, current);
400 #ifdef CONFIG_TRAD_SIGNALS
401 int setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
402 int signr, sigset_t *set)
404 struct sigframe __user *frame;
407 frame = get_sigframe(ka, regs, sizeof(*frame));
408 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
411 err |= install_sigtramp(frame->sf_code, __NR_sigreturn);
413 err |= setup_sigcontext(regs, &frame->sf_sc);
414 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
419 * Arguments to signal handler:
422 * a1 = 0 (should be cause)
423 * a2 = pointer to struct sigcontext
425 * $25 and c0_epc point to the signal handler, $29 points to the
428 regs->regs[ 4] = signr;
430 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
431 regs->regs[29] = (unsigned long) frame;
432 regs->regs[31] = (unsigned long) frame->sf_code;
433 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
435 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
436 current->comm, current->pid,
437 frame, regs->cp0_epc, regs->regs[31]);
441 force_sigsegv(signr, current);
446 int setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
447 int signr, sigset_t *set, siginfo_t *info)
449 struct rt_sigframe __user *frame;
452 frame = get_sigframe(ka, regs, sizeof(*frame));
453 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
456 err |= install_sigtramp(frame->rs_code, __NR_rt_sigreturn);
458 /* Create siginfo. */
459 err |= copy_siginfo_to_user(&frame->rs_info, info);
461 /* Create the ucontext. */
462 err |= __put_user(0, &frame->rs_uc.uc_flags);
463 err |= __put_user(NULL, &frame->rs_uc.uc_link);
464 err |= __put_user((void __user *)current->sas_ss_sp,
465 &frame->rs_uc.uc_stack.ss_sp);
466 err |= __put_user(sas_ss_flags(regs->regs[29]),
467 &frame->rs_uc.uc_stack.ss_flags);
468 err |= __put_user(current->sas_ss_size,
469 &frame->rs_uc.uc_stack.ss_size);
470 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
471 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
477 * Arguments to signal handler:
480 * a1 = 0 (should be cause)
481 * a2 = pointer to ucontext
483 * $25 and c0_epc point to the signal handler, $29 points to
484 * the struct rt_sigframe.
486 regs->regs[ 4] = signr;
487 regs->regs[ 5] = (unsigned long) &frame->rs_info;
488 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
489 regs->regs[29] = (unsigned long) frame;
490 regs->regs[31] = (unsigned long) frame->rs_code;
491 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
493 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
494 current->comm, current->pid,
495 frame, regs->cp0_epc, regs->regs[31]);
500 force_sigsegv(signr, current);
504 static int handle_signal(unsigned long sig, siginfo_t *info,
505 struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
509 switch(regs->regs[0]) {
510 case ERESTART_RESTARTBLOCK:
512 regs->regs[2] = EINTR;
515 if (!(ka->sa.sa_flags & SA_RESTART)) {
516 regs->regs[2] = EINTR;
520 case ERESTARTNOINTR: /* Userland will reload $v0. */
521 regs->regs[7] = regs->regs[26];
525 regs->regs[0] = 0; /* Don't deal with this again. */
527 if (sig_uses_siginfo(ka))
528 ret = current->thread.abi->setup_rt_frame(ka, regs, sig, oldset, info);
530 ret = current->thread.abi->setup_frame(ka, regs, sig, oldset);
532 spin_lock_irq(¤t->sighand->siglock);
533 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
534 if (!(ka->sa.sa_flags & SA_NODEFER))
535 sigaddset(¤t->blocked,sig);
537 spin_unlock_irq(¤t->sighand->siglock);
542 void do_signal(struct pt_regs *regs)
544 struct k_sigaction ka;
550 * We want the common case to go fast, which is why we may in certain
551 * cases get here from kernel mode. Just return without doing anything
554 if (!user_mode(regs))
557 if (test_thread_flag(TIF_RESTORE_SIGMASK))
558 oldset = ¤t->saved_sigmask;
560 oldset = ¤t->blocked;
562 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
564 /* Whee! Actually deliver the signal. */
565 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
567 * A signal was successfully delivered; the saved
568 * sigmask will have been stored in the signal frame,
569 * and will be restored by sigreturn, so we can simply
570 * clear the TIF_RESTORE_SIGMASK flag.
572 if (test_thread_flag(TIF_RESTORE_SIGMASK))
573 clear_thread_flag(TIF_RESTORE_SIGMASK);
580 * Who's code doesn't conform to the restartable syscall convention
581 * dies here!!! The li instruction, a single machine instruction,
582 * must directly be followed by the syscall instruction.
585 if (regs->regs[2] == ERESTARTNOHAND ||
586 regs->regs[2] == ERESTARTSYS ||
587 regs->regs[2] == ERESTARTNOINTR) {
588 regs->regs[7] = regs->regs[26];
591 if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
592 regs->regs[2] = __NR_restart_syscall;
593 regs->regs[7] = regs->regs[26];
596 regs->regs[0] = 0; /* Don't deal with this again. */
600 * If there's no signal to deliver, we just put the saved sigmask
603 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
604 clear_thread_flag(TIF_RESTORE_SIGMASK);
605 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
610 * notification of userspace execution resumption
611 * - triggered by the TIF_WORK_MASK flags
613 asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
614 __u32 thread_info_flags)
616 /* deal with pending signal delivery */
617 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
618 current->thread.abi->do_signal(regs);