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"
37 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
39 #if ICACHE_REFILLS_WORKAROUND_WAR == 0
42 u32 rs_ass[4]; /* argument save space for o32 */
43 u32 rs_code[2]; /* signal trampoline */
44 struct siginfo rs_info;
45 struct ucontext rs_uc;
51 u32 rs_ass[4]; /* argument save space for o32 */
53 struct siginfo rs_info;
54 struct ucontext rs_uc;
55 u32 rs_code[8] ____cacheline_aligned; /* signal trampoline */
63 int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
68 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
70 err |= __put_user(0, &sc->sc_regs[0]);
71 for (i = 1; i < 32; i++)
72 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
74 err |= __put_user(regs->hi, &sc->sc_mdhi);
75 err |= __put_user(regs->lo, &sc->sc_mdlo);
77 err |= __put_user(mfhi1(), &sc->sc_hi1);
78 err |= __put_user(mflo1(), &sc->sc_lo1);
79 err |= __put_user(mfhi2(), &sc->sc_hi2);
80 err |= __put_user(mflo2(), &sc->sc_lo2);
81 err |= __put_user(mfhi3(), &sc->sc_hi3);
82 err |= __put_user(mflo3(), &sc->sc_lo3);
83 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
86 err |= __put_user(!!used_math(), &sc->sc_used_math);
90 * Save FPU state to signal context. Signal handler
91 * will "inherit" current FPU state.
95 if (!is_fpu_owner()) {
99 err |= save_fp_context(sc);
106 int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
108 unsigned int used_math;
113 /* Always make any pending restarted system calls return -EINTR */
114 current_thread_info()->restart_block.fn = do_no_restart_syscall;
116 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
117 err |= __get_user(regs->hi, &sc->sc_mdhi);
118 err |= __get_user(regs->lo, &sc->sc_mdlo);
120 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
121 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
122 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
123 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
124 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
125 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
126 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
129 for (i = 1; i < 32; i++)
130 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
132 err |= __get_user(used_math, &sc->sc_used_math);
133 conditional_used_math(used_math);
138 /* restore fpu context if we have used it before */
140 err |= restore_fp_context(sc);
142 /* signal handler may have used FPU. Give it up. */
151 void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
156 /* Default to using normal stack */
160 * FPU emulator may have it's own trampoline active just
161 * above the user stack, 16-bytes before the next lowest
162 * 16 byte boundary. Try to avoid trashing it.
166 /* This is the X/Open sanctioned signal stack switching. */
167 if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
168 sp = current->sas_ss_sp + current->sas_ss_size;
170 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
173 int install_sigtramp(unsigned int __user *tramp, unsigned int syscall)
178 * Set up the return code ...
180 * li v0, __NR__foo_sigreturn
184 err = __put_user(0x24020000 + syscall, tramp + 0);
185 err |= __put_user(0x0000000c , tramp + 1);
186 if (ICACHE_REFILLS_WORKAROUND_WAR) {
187 err |= __put_user(0, tramp + 2);
188 err |= __put_user(0, tramp + 3);
189 err |= __put_user(0, tramp + 4);
190 err |= __put_user(0, tramp + 5);
191 err |= __put_user(0, tramp + 6);
192 err |= __put_user(0, tramp + 7);
194 flush_cache_sigtramp((unsigned long) tramp);
200 * Atomically swap in the new signal mask, and wait for a signal.
203 #ifdef CONFIG_TRAD_SIGNALS
204 asmlinkage int sys_sigsuspend(nabi_no_regargs struct pt_regs regs)
207 sigset_t __user *uset;
209 uset = (sigset_t __user *) regs.regs[4];
210 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
212 sigdelsetmask(&newset, ~_BLOCKABLE);
214 spin_lock_irq(¤t->sighand->siglock);
215 current->saved_sigmask = current->blocked;
216 current->blocked = newset;
218 spin_unlock_irq(¤t->sighand->siglock);
220 current->state = TASK_INTERRUPTIBLE;
222 set_thread_flag(TIF_RESTORE_SIGMASK);
223 return -ERESTARTNOHAND;
227 asmlinkage int sys_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
230 sigset_t __user *unewset;
233 /* XXX Don't preclude handling different sized sigset_t's. */
234 sigsetsize = regs.regs[5];
235 if (sigsetsize != sizeof(sigset_t))
238 unewset = (sigset_t __user *) regs.regs[4];
239 if (copy_from_user(&newset, unewset, sizeof(newset)))
241 sigdelsetmask(&newset, ~_BLOCKABLE);
243 spin_lock_irq(¤t->sighand->siglock);
244 current->saved_sigmask = current->blocked;
245 current->blocked = newset;
247 spin_unlock_irq(¤t->sighand->siglock);
249 current->state = TASK_INTERRUPTIBLE;
251 set_thread_flag(TIF_RESTORE_SIGMASK);
252 return -ERESTARTNOHAND;
255 #ifdef CONFIG_TRAD_SIGNALS
256 asmlinkage int sys_sigaction(int sig, const struct sigaction __user *act,
257 struct sigaction __user *oact)
259 struct k_sigaction new_ka, old_ka;
266 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
268 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
269 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
270 err |= __get_user(mask, &act->sa_mask.sig[0]);
274 siginitset(&new_ka.sa.sa_mask, mask);
277 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
280 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
282 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
283 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
284 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
285 err |= __put_user(0, &oact->sa_mask.sig[1]);
286 err |= __put_user(0, &oact->sa_mask.sig[2]);
287 err |= __put_user(0, &oact->sa_mask.sig[3]);
296 asmlinkage int sys_sigaltstack(nabi_no_regargs struct pt_regs regs)
298 const stack_t __user *uss = (const stack_t __user *) regs.regs[4];
299 stack_t __user *uoss = (stack_t __user *) regs.regs[5];
300 unsigned long usp = regs.regs[29];
302 return do_sigaltstack(uss, uoss, usp);
305 #ifdef CONFIG_TRAD_SIGNALS
306 asmlinkage void sys_sigreturn(nabi_no_regargs struct pt_regs regs)
308 struct sigframe __user *frame;
311 frame = (struct sigframe __user *) regs.regs[29];
312 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
314 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
317 sigdelsetmask(&blocked, ~_BLOCKABLE);
318 spin_lock_irq(¤t->sighand->siglock);
319 current->blocked = blocked;
321 spin_unlock_irq(¤t->sighand->siglock);
323 if (restore_sigcontext(®s, &frame->sf_sc))
327 * Don't let your children do this ...
329 __asm__ __volatile__(
337 force_sig(SIGSEGV, current);
339 #endif /* CONFIG_TRAD_SIGNALS */
341 asmlinkage void sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
343 struct rt_sigframe __user *frame;
347 frame = (struct rt_sigframe __user *) regs.regs[29];
348 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
350 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
353 sigdelsetmask(&set, ~_BLOCKABLE);
354 spin_lock_irq(¤t->sighand->siglock);
355 current->blocked = set;
357 spin_unlock_irq(¤t->sighand->siglock);
359 if (restore_sigcontext(®s, &frame->rs_uc.uc_mcontext))
362 if (__copy_from_user(&st, &frame->rs_uc.uc_stack, sizeof(st)))
364 /* It is more difficult to avoid calling this function than to
365 call it and ignore errors. */
366 do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]);
369 * Don't let your children do this ...
371 __asm__ __volatile__(
379 force_sig(SIGSEGV, current);
382 #ifdef CONFIG_TRAD_SIGNALS
383 int setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
384 int signr, sigset_t *set)
386 struct sigframe __user *frame;
389 frame = get_sigframe(ka, regs, sizeof(*frame));
390 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
393 err |= install_sigtramp(frame->sf_code, __NR_sigreturn);
395 err |= setup_sigcontext(regs, &frame->sf_sc);
396 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
401 * Arguments to signal handler:
404 * a1 = 0 (should be cause)
405 * a2 = pointer to struct sigcontext
407 * $25 and c0_epc point to the signal handler, $29 points to the
410 regs->regs[ 4] = signr;
412 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
413 regs->regs[29] = (unsigned long) frame;
414 regs->regs[31] = (unsigned long) frame->sf_code;
415 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
417 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
418 current->comm, current->pid,
419 frame, regs->cp0_epc, regs->regs[31]);
423 force_sigsegv(signr, current);
428 int setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
429 int signr, sigset_t *set, siginfo_t *info)
431 struct rt_sigframe __user *frame;
434 frame = get_sigframe(ka, regs, sizeof(*frame));
435 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
438 err |= install_sigtramp(frame->rs_code, __NR_rt_sigreturn);
440 /* Create siginfo. */
441 err |= copy_siginfo_to_user(&frame->rs_info, info);
443 /* Create the ucontext. */
444 err |= __put_user(0, &frame->rs_uc.uc_flags);
445 err |= __put_user(NULL, &frame->rs_uc.uc_link);
446 err |= __put_user((void __user *)current->sas_ss_sp,
447 &frame->rs_uc.uc_stack.ss_sp);
448 err |= __put_user(sas_ss_flags(regs->regs[29]),
449 &frame->rs_uc.uc_stack.ss_flags);
450 err |= __put_user(current->sas_ss_size,
451 &frame->rs_uc.uc_stack.ss_size);
452 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
453 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
459 * Arguments to signal handler:
462 * a1 = 0 (should be cause)
463 * a2 = pointer to ucontext
465 * $25 and c0_epc point to the signal handler, $29 points to
466 * the struct rt_sigframe.
468 regs->regs[ 4] = signr;
469 regs->regs[ 5] = (unsigned long) &frame->rs_info;
470 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
471 regs->regs[29] = (unsigned long) frame;
472 regs->regs[31] = (unsigned long) frame->rs_code;
473 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
475 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
476 current->comm, current->pid,
477 frame, regs->cp0_epc, regs->regs[31]);
482 force_sigsegv(signr, current);
486 static int handle_signal(unsigned long sig, siginfo_t *info,
487 struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
491 switch(regs->regs[0]) {
492 case ERESTART_RESTARTBLOCK:
494 regs->regs[2] = EINTR;
497 if (!(ka->sa.sa_flags & SA_RESTART)) {
498 regs->regs[2] = EINTR;
502 case ERESTARTNOINTR: /* Userland will reload $v0. */
503 regs->regs[7] = regs->regs[26];
507 regs->regs[0] = 0; /* Don't deal with this again. */
509 if (sig_uses_siginfo(ka))
510 ret = current->thread.abi->setup_rt_frame(ka, regs, sig, oldset, info);
512 ret = current->thread.abi->setup_frame(ka, regs, sig, oldset);
514 spin_lock_irq(¤t->sighand->siglock);
515 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
516 if (!(ka->sa.sa_flags & SA_NODEFER))
517 sigaddset(¤t->blocked,sig);
519 spin_unlock_irq(¤t->sighand->siglock);
524 void do_signal(struct pt_regs *regs)
526 struct k_sigaction ka;
532 * We want the common case to go fast, which is why we may in certain
533 * cases get here from kernel mode. Just return without doing anything
536 if (!user_mode(regs))
539 if (test_thread_flag(TIF_RESTORE_SIGMASK))
540 oldset = ¤t->saved_sigmask;
542 oldset = ¤t->blocked;
544 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
546 /* Whee! Actually deliver the signal. */
547 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
549 * A signal was successfully delivered; the saved
550 * sigmask will have been stored in the signal frame,
551 * and will be restored by sigreturn, so we can simply
552 * clear the TIF_RESTORE_SIGMASK flag.
554 if (test_thread_flag(TIF_RESTORE_SIGMASK))
555 clear_thread_flag(TIF_RESTORE_SIGMASK);
562 * Who's code doesn't conform to the restartable syscall convention
563 * dies here!!! The li instruction, a single machine instruction,
564 * must directly be followed by the syscall instruction.
567 if (regs->regs[2] == ERESTARTNOHAND ||
568 regs->regs[2] == ERESTARTSYS ||
569 regs->regs[2] == ERESTARTNOINTR) {
570 regs->regs[7] = regs->regs[26];
573 if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
574 regs->regs[2] = __NR_restart_syscall;
575 regs->regs[7] = regs->regs[26];
578 regs->regs[0] = 0; /* Don't deal with this again. */
582 * If there's no signal to deliver, we just put the saved sigmask
585 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
586 clear_thread_flag(TIF_RESTORE_SIGMASK);
587 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
592 * notification of userspace execution resumption
593 * - triggered by the TIF_WORK_MASK flags
595 asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
596 __u32 thread_info_flags)
598 /* deal with pending signal delivery */
599 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
600 current->thread.abi->do_signal(regs);