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 #ifdef CONFIG_CPU_HAS_SMARTMIPS
93 err |= __put_user(regs->acx, &sc->sc_acx);
95 err |= __put_user(regs->hi, &sc->sc_mdhi);
96 err |= __put_user(regs->lo, &sc->sc_mdlo);
98 err |= __put_user(mfhi1(), &sc->sc_hi1);
99 err |= __put_user(mflo1(), &sc->sc_lo1);
100 err |= __put_user(mfhi2(), &sc->sc_hi2);
101 err |= __put_user(mflo2(), &sc->sc_lo2);
102 err |= __put_user(mfhi3(), &sc->sc_hi3);
103 err |= __put_user(mflo3(), &sc->sc_lo3);
104 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
107 err |= __put_user(!!used_math(), &sc->sc_used_math);
111 * Save FPU state to signal context. Signal handler
112 * will "inherit" current FPU state.
116 if (!is_fpu_owner()) {
120 err |= save_fp_context(sc);
127 int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
129 unsigned int used_math;
134 /* Always make any pending restarted system calls return -EINTR */
135 current_thread_info()->restart_block.fn = do_no_restart_syscall;
137 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
139 #ifdef CONFIG_CPU_HAS_SMARTMIPS
140 err |= __get_user(regs->acx, &sc->sc_acx);
142 err |= __get_user(regs->hi, &sc->sc_mdhi);
143 err |= __get_user(regs->lo, &sc->sc_mdlo);
145 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
146 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
147 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
148 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
149 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
150 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
151 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
154 for (i = 1; i < 32; i++)
155 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
157 err |= __get_user(used_math, &sc->sc_used_math);
158 conditional_used_math(used_math);
163 /* restore fpu context if we have used it before */
165 err |= restore_fp_context(sc);
167 /* signal handler may have used FPU. Give it up. */
176 void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
181 /* Default to using normal stack */
185 * FPU emulator may have it's own trampoline active just
186 * above the user stack, 16-bytes before the next lowest
187 * 16 byte boundary. Try to avoid trashing it.
191 /* This is the X/Open sanctioned signal stack switching. */
192 if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
193 sp = current->sas_ss_sp + current->sas_ss_size;
195 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
198 int install_sigtramp(unsigned int __user *tramp, unsigned int syscall)
203 * Set up the return code ...
205 * li v0, __NR__foo_sigreturn
209 err = __put_user(0x24020000 + syscall, tramp + 0);
210 err |= __put_user(0x0000000c , tramp + 1);
211 if (ICACHE_REFILLS_WORKAROUND_WAR) {
212 err |= __put_user(0, tramp + 2);
213 err |= __put_user(0, tramp + 3);
214 err |= __put_user(0, tramp + 4);
215 err |= __put_user(0, tramp + 5);
216 err |= __put_user(0, tramp + 6);
217 err |= __put_user(0, tramp + 7);
219 flush_cache_sigtramp((unsigned long) tramp);
225 * Atomically swap in the new signal mask, and wait for a signal.
228 #ifdef CONFIG_TRAD_SIGNALS
229 asmlinkage int sys_sigsuspend(nabi_no_regargs struct pt_regs regs)
232 sigset_t __user *uset;
234 uset = (sigset_t __user *) regs.regs[4];
235 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
237 sigdelsetmask(&newset, ~_BLOCKABLE);
239 spin_lock_irq(¤t->sighand->siglock);
240 current->saved_sigmask = current->blocked;
241 current->blocked = newset;
243 spin_unlock_irq(¤t->sighand->siglock);
245 current->state = TASK_INTERRUPTIBLE;
247 set_thread_flag(TIF_RESTORE_SIGMASK);
248 return -ERESTARTNOHAND;
252 asmlinkage int sys_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
255 sigset_t __user *unewset;
258 /* XXX Don't preclude handling different sized sigset_t's. */
259 sigsetsize = regs.regs[5];
260 if (sigsetsize != sizeof(sigset_t))
263 unewset = (sigset_t __user *) regs.regs[4];
264 if (copy_from_user(&newset, unewset, sizeof(newset)))
266 sigdelsetmask(&newset, ~_BLOCKABLE);
268 spin_lock_irq(¤t->sighand->siglock);
269 current->saved_sigmask = current->blocked;
270 current->blocked = newset;
272 spin_unlock_irq(¤t->sighand->siglock);
274 current->state = TASK_INTERRUPTIBLE;
276 set_thread_flag(TIF_RESTORE_SIGMASK);
277 return -ERESTARTNOHAND;
280 #ifdef CONFIG_TRAD_SIGNALS
281 asmlinkage int sys_sigaction(int sig, const struct sigaction __user *act,
282 struct sigaction __user *oact)
284 struct k_sigaction new_ka, old_ka;
291 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
293 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
294 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
295 err |= __get_user(mask, &act->sa_mask.sig[0]);
299 siginitset(&new_ka.sa.sa_mask, mask);
302 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
305 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
307 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
308 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
309 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
310 err |= __put_user(0, &oact->sa_mask.sig[1]);
311 err |= __put_user(0, &oact->sa_mask.sig[2]);
312 err |= __put_user(0, &oact->sa_mask.sig[3]);
321 asmlinkage int sys_sigaltstack(nabi_no_regargs struct pt_regs regs)
323 const stack_t __user *uss = (const stack_t __user *) regs.regs[4];
324 stack_t __user *uoss = (stack_t __user *) regs.regs[5];
325 unsigned long usp = regs.regs[29];
327 return do_sigaltstack(uss, uoss, usp);
330 #ifdef CONFIG_TRAD_SIGNALS
331 asmlinkage void sys_sigreturn(nabi_no_regargs struct pt_regs regs)
333 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 spin_lock_irq(¤t->sighand->siglock);
344 current->blocked = blocked;
346 spin_unlock_irq(¤t->sighand->siglock);
348 if (restore_sigcontext(®s, &frame->sf_sc))
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 spin_lock_irq(¤t->sighand->siglock);
380 current->blocked = set;
382 spin_unlock_irq(¤t->sighand->siglock);
384 if (restore_sigcontext(®s, &frame->rs_uc.uc_mcontext))
387 if (__copy_from_user(&st, &frame->rs_uc.uc_stack, sizeof(st)))
389 /* It is more difficult to avoid calling this function than to
390 call it and ignore errors. */
391 do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]);
394 * Don't let your children do this ...
396 __asm__ __volatile__(
404 force_sig(SIGSEGV, current);
407 #ifdef CONFIG_TRAD_SIGNALS
408 static int setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
409 int signr, sigset_t *set)
411 struct sigframe __user *frame;
414 frame = get_sigframe(ka, regs, sizeof(*frame));
415 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
418 err |= install_sigtramp(frame->sf_code, __NR_sigreturn);
420 err |= setup_sigcontext(regs, &frame->sf_sc);
421 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
426 * Arguments to signal handler:
429 * a1 = 0 (should be cause)
430 * a2 = pointer to struct sigcontext
432 * $25 and c0_epc point to the signal handler, $29 points to the
435 regs->regs[ 4] = signr;
437 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
438 regs->regs[29] = (unsigned long) frame;
439 regs->regs[31] = (unsigned long) frame->sf_code;
440 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
442 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
443 current->comm, current->pid,
444 frame, regs->cp0_epc, regs->regs[31]);
448 force_sigsegv(signr, current);
453 static int setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
454 int signr, sigset_t *set, siginfo_t *info)
456 struct rt_sigframe __user *frame;
459 frame = get_sigframe(ka, regs, sizeof(*frame));
460 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
463 err |= install_sigtramp(frame->rs_code, __NR_rt_sigreturn);
465 /* Create siginfo. */
466 err |= copy_siginfo_to_user(&frame->rs_info, info);
468 /* Create the ucontext. */
469 err |= __put_user(0, &frame->rs_uc.uc_flags);
470 err |= __put_user(NULL, &frame->rs_uc.uc_link);
471 err |= __put_user((void __user *)current->sas_ss_sp,
472 &frame->rs_uc.uc_stack.ss_sp);
473 err |= __put_user(sas_ss_flags(regs->regs[29]),
474 &frame->rs_uc.uc_stack.ss_flags);
475 err |= __put_user(current->sas_ss_size,
476 &frame->rs_uc.uc_stack.ss_size);
477 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
478 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
484 * Arguments to signal handler:
487 * a1 = 0 (should be cause)
488 * a2 = pointer to ucontext
490 * $25 and c0_epc point to the signal handler, $29 points to
491 * the struct rt_sigframe.
493 regs->regs[ 4] = signr;
494 regs->regs[ 5] = (unsigned long) &frame->rs_info;
495 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
496 regs->regs[29] = (unsigned long) frame;
497 regs->regs[31] = (unsigned long) frame->rs_code;
498 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
500 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
501 current->comm, current->pid,
502 frame, regs->cp0_epc, regs->regs[31]);
507 force_sigsegv(signr, current);
511 struct mips_abi mips_abi = {
512 #ifdef CONFIG_TRAD_SIGNALS
513 .setup_frame = setup_frame,
515 .setup_rt_frame = setup_rt_frame,
516 .restart = __NR_restart_syscall
519 static int handle_signal(unsigned long sig, siginfo_t *info,
520 struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
524 switch(regs->regs[0]) {
525 case ERESTART_RESTARTBLOCK:
527 regs->regs[2] = EINTR;
530 if (!(ka->sa.sa_flags & SA_RESTART)) {
531 regs->regs[2] = EINTR;
535 case ERESTARTNOINTR: /* Userland will reload $v0. */
536 regs->regs[7] = regs->regs[26];
540 regs->regs[0] = 0; /* Don't deal with this again. */
542 if (sig_uses_siginfo(ka))
543 ret = current->thread.abi->setup_rt_frame(ka, regs, sig, oldset, info);
545 ret = current->thread.abi->setup_frame(ka, regs, sig, oldset);
547 spin_lock_irq(¤t->sighand->siglock);
548 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
549 if (!(ka->sa.sa_flags & SA_NODEFER))
550 sigaddset(¤t->blocked,sig);
552 spin_unlock_irq(¤t->sighand->siglock);
557 static void do_signal(struct pt_regs *regs)
559 struct k_sigaction ka;
565 * We want the common case to go fast, which is why we may in certain
566 * cases get here from kernel mode. Just return without doing anything
569 if (!user_mode(regs))
572 if (test_thread_flag(TIF_RESTORE_SIGMASK))
573 oldset = ¤t->saved_sigmask;
575 oldset = ¤t->blocked;
577 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
579 /* Whee! Actually deliver the signal. */
580 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
582 * A signal was successfully delivered; the saved
583 * sigmask will have been stored in the signal frame,
584 * and will be restored by sigreturn, so we can simply
585 * clear the TIF_RESTORE_SIGMASK flag.
587 if (test_thread_flag(TIF_RESTORE_SIGMASK))
588 clear_thread_flag(TIF_RESTORE_SIGMASK);
595 * Who's code doesn't conform to the restartable syscall convention
596 * dies here!!! The li instruction, a single machine instruction,
597 * must directly be followed by the syscall instruction.
600 if (regs->regs[2] == ERESTARTNOHAND ||
601 regs->regs[2] == ERESTARTSYS ||
602 regs->regs[2] == ERESTARTNOINTR) {
603 regs->regs[7] = regs->regs[26];
606 if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
607 regs->regs[2] = current->thread.abi->restart;
608 regs->regs[7] = regs->regs[26];
611 regs->regs[0] = 0; /* Don't deal with this again. */
615 * If there's no signal to deliver, we just put the saved sigmask
618 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
619 clear_thread_flag(TIF_RESTORE_SIGMASK);
620 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
625 * notification of userspace execution resumption
626 * - triggered by the TIF_WORK_MASK flags
628 asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
629 __u32 thread_info_flags)
631 /* deal with pending signal delivery */
632 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))