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"
39 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
44 int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
49 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
51 err |= __put_user(0, &sc->sc_regs[0]);
52 for (i = 1; i < 32; i++)
53 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
55 err |= __put_user(regs->hi, &sc->sc_mdhi);
56 err |= __put_user(regs->lo, &sc->sc_mdlo);
58 err |= __put_user(mfhi1(), &sc->sc_hi1);
59 err |= __put_user(mflo1(), &sc->sc_lo1);
60 err |= __put_user(mfhi2(), &sc->sc_hi2);
61 err |= __put_user(mflo2(), &sc->sc_lo2);
62 err |= __put_user(mfhi3(), &sc->sc_hi3);
63 err |= __put_user(mflo3(), &sc->sc_lo3);
64 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
67 err |= __put_user(!!used_math(), &sc->sc_used_math);
71 * Save FPU state to signal context. Signal handler
72 * will "inherit" current FPU state.
76 if (!is_fpu_owner()) {
80 err |= save_fp_context(sc);
87 int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
89 unsigned int used_math;
94 /* Always make any pending restarted system calls return -EINTR */
95 current_thread_info()->restart_block.fn = do_no_restart_syscall;
97 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
98 err |= __get_user(regs->hi, &sc->sc_mdhi);
99 err |= __get_user(regs->lo, &sc->sc_mdlo);
101 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
102 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
103 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
104 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
105 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
106 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
107 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
110 for (i = 1; i < 32; i++)
111 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
113 err |= __get_user(used_math, &sc->sc_used_math);
114 conditional_used_math(used_math);
119 /* restore fpu context if we have used it before */
121 err |= restore_fp_context(sc);
123 /* signal handler may have used FPU. Give it up. */
132 void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
137 /* Default to using normal stack */
141 * FPU emulator may have it's own trampoline active just
142 * above the user stack, 16-bytes before the next lowest
143 * 16 byte boundary. Try to avoid trashing it.
147 /* This is the X/Open sanctioned signal stack switching. */
148 if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
149 sp = current->sas_ss_sp + current->sas_ss_size;
151 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
154 int install_sigtramp(unsigned int __user *tramp, unsigned int syscall)
159 * Set up the return code ...
161 * li v0, __NR__foo_sigreturn
165 err = __put_user(0x24020000 + syscall, tramp + 0);
166 err |= __put_user(0x0000000c , tramp + 1);
167 if (ICACHE_REFILLS_WORKAROUND_WAR) {
168 err |= __put_user(0, tramp + 2);
169 err |= __put_user(0, tramp + 3);
170 err |= __put_user(0, tramp + 4);
171 err |= __put_user(0, tramp + 5);
172 err |= __put_user(0, tramp + 6);
173 err |= __put_user(0, tramp + 7);
175 flush_cache_sigtramp((unsigned long) tramp);
181 * Atomically swap in the new signal mask, and wait for a signal.
184 #ifdef CONFIG_TRAD_SIGNALS
185 save_static_function(sys_sigsuspend);
186 __attribute_used__ noinline static int
187 _sys_sigsuspend(nabi_no_regargs struct pt_regs regs)
190 sigset_t __user *uset;
192 uset = (sigset_t __user *) regs.regs[4];
193 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
195 sigdelsetmask(&newset, ~_BLOCKABLE);
197 spin_lock_irq(¤t->sighand->siglock);
198 current->saved_sigmask = current->blocked;
199 current->blocked = newset;
201 spin_unlock_irq(¤t->sighand->siglock);
203 current->state = TASK_INTERRUPTIBLE;
205 set_thread_flag(TIF_RESTORE_SIGMASK);
206 return -ERESTARTNOHAND;
210 save_static_function(sys_rt_sigsuspend);
211 __attribute_used__ noinline static int
212 _sys_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
215 sigset_t __user *unewset;
218 /* XXX Don't preclude handling different sized sigset_t's. */
219 sigsetsize = regs.regs[5];
220 if (sigsetsize != sizeof(sigset_t))
223 unewset = (sigset_t __user *) regs.regs[4];
224 if (copy_from_user(&newset, unewset, sizeof(newset)))
226 sigdelsetmask(&newset, ~_BLOCKABLE);
228 spin_lock_irq(¤t->sighand->siglock);
229 current->saved_sigmask = current->blocked;
230 current->blocked = newset;
232 spin_unlock_irq(¤t->sighand->siglock);
234 current->state = TASK_INTERRUPTIBLE;
236 set_thread_flag(TIF_RESTORE_SIGMASK);
237 return -ERESTARTNOHAND;
240 #ifdef CONFIG_TRAD_SIGNALS
241 asmlinkage int sys_sigaction(int sig, const struct sigaction __user *act,
242 struct sigaction __user *oact)
244 struct k_sigaction new_ka, old_ka;
251 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
253 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
254 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
255 err |= __get_user(mask, &act->sa_mask.sig[0]);
259 siginitset(&new_ka.sa.sa_mask, mask);
262 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
265 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
267 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
268 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
269 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
270 err |= __put_user(0, &oact->sa_mask.sig[1]);
271 err |= __put_user(0, &oact->sa_mask.sig[2]);
272 err |= __put_user(0, &oact->sa_mask.sig[3]);
281 asmlinkage int sys_sigaltstack(nabi_no_regargs struct pt_regs regs)
283 const stack_t __user *uss = (const stack_t __user *) regs.regs[4];
284 stack_t __user *uoss = (stack_t __user *) regs.regs[5];
285 unsigned long usp = regs.regs[29];
287 return do_sigaltstack(uss, uoss, usp);
291 * Horribly complicated - with the bloody RM9000 workarounds enabled
292 * the signal trampolines is moving to the end of the structure so we can
293 * increase the alignment without breaking software compatibility.
295 #ifdef CONFIG_TRAD_SIGNALS
297 u32 sf_ass[4]; /* argument save space for o32 */
298 #if ICACHE_REFILLS_WORKAROUND_WAR
301 u32 sf_code[2]; /* signal trampoline */
303 struct sigcontext sf_sc;
305 #if ICACHE_REFILLS_WORKAROUND_WAR
306 u32 sf_code[8] ____cacheline_aligned; /* signal trampoline */
312 u32 rs_ass[4]; /* argument save space for o32 */
313 #if ICACHE_REFILLS_WORKAROUND_WAR
316 u32 rs_code[2]; /* signal trampoline */
318 struct siginfo rs_info;
319 struct ucontext rs_uc;
320 #if ICACHE_REFILLS_WORKAROUND_WAR
321 u32 rs_code[8] ____cacheline_aligned; /* signal trampoline */
325 #ifdef CONFIG_TRAD_SIGNALS
326 save_static_function(sys_sigreturn);
327 __attribute_used__ noinline static void
328 _sys_sigreturn(nabi_no_regargs struct pt_regs regs)
330 struct sigframe __user *frame;
333 frame = (struct sigframe __user *) regs.regs[29];
334 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
336 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
339 sigdelsetmask(&blocked, ~_BLOCKABLE);
340 spin_lock_irq(¤t->sighand->siglock);
341 current->blocked = blocked;
343 spin_unlock_irq(¤t->sighand->siglock);
345 if (restore_sigcontext(®s, &frame->sf_sc))
349 * Don't let your children do this ...
351 __asm__ __volatile__(
359 force_sig(SIGSEGV, current);
361 #endif /* CONFIG_TRAD_SIGNALS */
363 save_static_function(sys_rt_sigreturn);
364 __attribute_used__ noinline static void
365 _sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
367 struct rt_sigframe __user *frame;
371 frame = (struct rt_sigframe __user *) regs.regs[29];
372 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
374 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
377 sigdelsetmask(&set, ~_BLOCKABLE);
378 spin_lock_irq(¤t->sighand->siglock);
379 current->blocked = set;
381 spin_unlock_irq(¤t->sighand->siglock);
383 if (restore_sigcontext(®s, &frame->rs_uc.uc_mcontext))
386 if (__copy_from_user(&st, &frame->rs_uc.uc_stack, sizeof(st)))
388 /* It is more difficult to avoid calling this function than to
389 call it and ignore errors. */
390 do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]);
393 * Don't let your children do this ...
395 __asm__ __volatile__(
403 force_sig(SIGSEGV, current);
406 #ifdef CONFIG_TRAD_SIGNALS
407 int setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
408 int signr, sigset_t *set)
410 struct sigframe __user *frame;
413 frame = get_sigframe(ka, regs, sizeof(*frame));
414 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
417 install_sigtramp(frame->sf_code, __NR_sigreturn);
419 err |= setup_sigcontext(regs, &frame->sf_sc);
420 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
425 * Arguments to signal handler:
428 * a1 = 0 (should be cause)
429 * a2 = pointer to struct sigcontext
431 * $25 and c0_epc point to the signal handler, $29 points to the
434 regs->regs[ 4] = signr;
436 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
437 regs->regs[29] = (unsigned long) frame;
438 regs->regs[31] = (unsigned long) frame->sf_code;
439 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
442 printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n",
443 current->comm, current->pid,
444 frame, regs->cp0_epc, frame->regs[31]);
449 force_sigsegv(signr, current);
454 int setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
455 int signr, sigset_t *set, siginfo_t *info)
457 struct rt_sigframe __user *frame;
460 frame = get_sigframe(ka, regs, sizeof(*frame));
461 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
464 install_sigtramp(frame->rs_code, __NR_rt_sigreturn);
466 /* Create siginfo. */
467 err |= copy_siginfo_to_user(&frame->rs_info, info);
469 /* Create the ucontext. */
470 err |= __put_user(0, &frame->rs_uc.uc_flags);
471 err |= __put_user(NULL, &frame->rs_uc.uc_link);
472 err |= __put_user((void __user *)current->sas_ss_sp,
473 &frame->rs_uc.uc_stack.ss_sp);
474 err |= __put_user(sas_ss_flags(regs->regs[29]),
475 &frame->rs_uc.uc_stack.ss_flags);
476 err |= __put_user(current->sas_ss_size,
477 &frame->rs_uc.uc_stack.ss_size);
478 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
479 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
485 * Arguments to signal handler:
488 * a1 = 0 (should be cause)
489 * a2 = pointer to ucontext
491 * $25 and c0_epc point to the signal handler, $29 points to
492 * the struct rt_sigframe.
494 regs->regs[ 4] = signr;
495 regs->regs[ 5] = (unsigned long) &frame->rs_info;
496 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
497 regs->regs[29] = (unsigned long) frame;
498 regs->regs[31] = (unsigned long) frame->rs_code;
499 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
502 printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n",
503 current->comm, current->pid,
504 frame, regs->cp0_epc, regs->regs[31]);
509 force_sigsegv(signr, current);
513 static inline int handle_signal(unsigned long sig, siginfo_t *info,
514 struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
518 switch(regs->regs[0]) {
519 case ERESTART_RESTARTBLOCK:
521 regs->regs[2] = EINTR;
524 if (!(ka->sa.sa_flags & SA_RESTART)) {
525 regs->regs[2] = EINTR;
529 case ERESTARTNOINTR: /* Userland will reload $v0. */
530 regs->regs[7] = regs->regs[26];
534 regs->regs[0] = 0; /* Don't deal with this again. */
536 if (sig_uses_siginfo(ka))
537 ret = current->thread.abi->setup_rt_frame(ka, regs, sig, oldset, info);
539 ret = current->thread.abi->setup_frame(ka, regs, sig, oldset);
541 spin_lock_irq(¤t->sighand->siglock);
542 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
543 if (!(ka->sa.sa_flags & SA_NODEFER))
544 sigaddset(¤t->blocked,sig);
546 spin_unlock_irq(¤t->sighand->siglock);
551 void do_signal(struct pt_regs *regs)
553 struct k_sigaction ka;
559 * We want the common case to go fast, which is why we may in certain
560 * cases get here from kernel mode. Just return without doing anything
563 if (!user_mode(regs))
566 if (test_thread_flag(TIF_RESTORE_SIGMASK))
567 oldset = ¤t->saved_sigmask;
569 oldset = ¤t->blocked;
571 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
573 /* Whee! Actually deliver the signal. */
574 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
576 * A signal was successfully delivered; the saved
577 * sigmask will have been stored in the signal frame,
578 * and will be restored by sigreturn, so we can simply
579 * clear the TIF_RESTORE_SIGMASK flag.
581 if (test_thread_flag(TIF_RESTORE_SIGMASK))
582 clear_thread_flag(TIF_RESTORE_SIGMASK);
589 * Who's code doesn't conform to the restartable syscall convention
590 * dies here!!! The li instruction, a single machine instruction,
591 * must directly be followed by the syscall instruction.
594 if (regs->regs[2] == ERESTARTNOHAND ||
595 regs->regs[2] == ERESTARTSYS ||
596 regs->regs[2] == ERESTARTNOINTR) {
597 regs->regs[7] = regs->regs[26];
600 if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
601 regs->regs[2] = __NR_restart_syscall;
602 regs->regs[7] = regs->regs[26];
605 regs->regs[0] = 0; /* Don't deal with this again. */
609 * If there's no signal to deliver, we just put the saved sigmask
612 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
613 clear_thread_flag(TIF_RESTORE_SIGMASK);
614 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
619 * notification of userspace execution resumption
620 * - triggered by the TIF_WORK_MASK flags
622 asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
623 __u32 thread_info_flags)
625 /* deal with pending signal delivery */
626 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
627 current->thread.abi->do_signal(regs);