[MIPS] Retry {save,restore}_fp_context if failed in atomic context.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / mips / kernel / signal.c
blob07d67309451a62cc4bc4ac89c9ec520e30f45639
1 /*
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
4 * for more details.
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 * Copyright (C) 1994 - 2000 Ralf Baechle
8 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
9 */
10 #include <linux/cache.h>
11 #include <linux/sched.h>
12 #include <linux/mm.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>
23 #include <linux/uaccess.h>
25 #include <asm/abi.h>
26 #include <asm/asm.h>
27 #include <linux/bitops.h>
28 #include <asm/cacheflush.h>
29 #include <asm/fpu.h>
30 #include <asm/sim.h>
31 #include <asm/ucontext.h>
32 #include <asm/cpu-features.h>
33 #include <asm/war.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
44 struct sigframe {
45 u32 sf_ass[4]; /* argument save space for o32 */
46 u32 sf_code[2]; /* signal trampoline */
47 struct sigcontext sf_sc;
48 sigset_t sf_mask;
51 struct rt_sigframe {
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;
58 #else
60 struct sigframe {
61 u32 sf_ass[4]; /* argument save space for o32 */
62 u32 sf_pad[2];
63 struct sigcontext sf_sc; /* hw context */
64 sigset_t sf_mask;
65 u32 sf_code[8] ____cacheline_aligned; /* signal trampoline */
68 struct rt_sigframe {
69 u32 rs_ass[4]; /* argument save space for o32 */
70 u32 rs_pad[2];
71 struct siginfo rs_info;
72 struct ucontext rs_uc;
73 u32 rs_code[8] ____cacheline_aligned; /* signal trampoline */
76 #endif
79 * Helper routines
81 static int protected_save_fp_context(struct sigcontext __user *sc)
83 int err;
84 while (1) {
85 lock_fpu_owner();
86 own_fpu_inatomic(1);
87 err = save_fp_context(sc); /* this might fail */
88 unlock_fpu_owner();
89 if (likely(!err))
90 break;
91 /* touch the sigcontext and try again */
92 err = __put_user(0, &sc->sc_fpregs[0]) |
93 __put_user(0, &sc->sc_fpregs[31]) |
94 __put_user(0, &sc->sc_fpc_csr);
95 if (err)
96 break; /* really bad sigcontext */
98 return err;
101 static int protected_restore_fp_context(struct sigcontext __user *sc)
103 int err, tmp;
104 while (1) {
105 lock_fpu_owner();
106 own_fpu_inatomic(0);
107 err = restore_fp_context(sc); /* this might fail */
108 unlock_fpu_owner();
109 if (likely(!err))
110 break;
111 /* touch the sigcontext and try again */
112 err = __get_user(tmp, &sc->sc_fpregs[0]) |
113 __get_user(tmp, &sc->sc_fpregs[31]) |
114 __get_user(tmp, &sc->sc_fpc_csr);
115 if (err)
116 break; /* really bad sigcontext */
118 return err;
121 int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
123 int err = 0;
124 int i;
125 unsigned int used_math;
127 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
129 err |= __put_user(0, &sc->sc_regs[0]);
130 for (i = 1; i < 32; i++)
131 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
133 #ifdef CONFIG_CPU_HAS_SMARTMIPS
134 err |= __put_user(regs->acx, &sc->sc_acx);
135 #endif
136 err |= __put_user(regs->hi, &sc->sc_mdhi);
137 err |= __put_user(regs->lo, &sc->sc_mdlo);
138 if (cpu_has_dsp) {
139 err |= __put_user(mfhi1(), &sc->sc_hi1);
140 err |= __put_user(mflo1(), &sc->sc_lo1);
141 err |= __put_user(mfhi2(), &sc->sc_hi2);
142 err |= __put_user(mflo2(), &sc->sc_lo2);
143 err |= __put_user(mfhi3(), &sc->sc_hi3);
144 err |= __put_user(mflo3(), &sc->sc_lo3);
145 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
148 used_math = !!used_math();
149 err |= __put_user(used_math, &sc->sc_used_math);
151 if (used_math) {
153 * Save FPU state to signal context. Signal handler
154 * will "inherit" current FPU state.
156 err |= protected_save_fp_context(sc);
158 return err;
161 int fpcsr_pending(unsigned int __user *fpcsr)
163 int err, sig = 0;
164 unsigned int csr, enabled;
166 err = __get_user(csr, fpcsr);
167 enabled = FPU_CSR_UNI_X | ((csr & FPU_CSR_ALL_E) << 5);
169 * If the signal handler set some FPU exceptions, clear it and
170 * send SIGFPE.
172 if (csr & enabled) {
173 csr &= ~enabled;
174 err |= __put_user(csr, fpcsr);
175 sig = SIGFPE;
177 return err ?: sig;
180 static int
181 check_and_restore_fp_context(struct sigcontext __user *sc)
183 int err, sig;
185 err = sig = fpcsr_pending(&sc->sc_fpc_csr);
186 if (err > 0)
187 err = 0;
188 err |= protected_restore_fp_context(sc);
189 return err ?: sig;
192 int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
194 unsigned int used_math;
195 unsigned long treg;
196 int err = 0;
197 int i;
199 /* Always make any pending restarted system calls return -EINTR */
200 current_thread_info()->restart_block.fn = do_no_restart_syscall;
202 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
204 #ifdef CONFIG_CPU_HAS_SMARTMIPS
205 err |= __get_user(regs->acx, &sc->sc_acx);
206 #endif
207 err |= __get_user(regs->hi, &sc->sc_mdhi);
208 err |= __get_user(regs->lo, &sc->sc_mdlo);
209 if (cpu_has_dsp) {
210 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
211 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
212 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
213 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
214 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
215 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
216 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
219 for (i = 1; i < 32; i++)
220 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
222 err |= __get_user(used_math, &sc->sc_used_math);
223 conditional_used_math(used_math);
225 if (used_math) {
226 /* restore fpu context if we have used it before */
227 if (!err)
228 err = check_and_restore_fp_context(sc);
229 } else {
230 /* signal handler may have used FPU. Give it up. */
231 lose_fpu(0);
234 return err;
237 void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
238 size_t frame_size)
240 unsigned long sp;
242 /* Default to using normal stack */
243 sp = regs->regs[29];
246 * FPU emulator may have it's own trampoline active just
247 * above the user stack, 16-bytes before the next lowest
248 * 16 byte boundary. Try to avoid trashing it.
250 sp -= 32;
252 /* This is the X/Open sanctioned signal stack switching. */
253 if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
254 sp = current->sas_ss_sp + current->sas_ss_size;
256 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
259 int install_sigtramp(unsigned int __user *tramp, unsigned int syscall)
261 int err;
264 * Set up the return code ...
266 * li v0, __NR__foo_sigreturn
267 * syscall
270 err = __put_user(0x24020000 + syscall, tramp + 0);
271 err |= __put_user(0x0000000c , tramp + 1);
272 if (ICACHE_REFILLS_WORKAROUND_WAR) {
273 err |= __put_user(0, tramp + 2);
274 err |= __put_user(0, tramp + 3);
275 err |= __put_user(0, tramp + 4);
276 err |= __put_user(0, tramp + 5);
277 err |= __put_user(0, tramp + 6);
278 err |= __put_user(0, tramp + 7);
280 flush_cache_sigtramp((unsigned long) tramp);
282 return err;
286 * Atomically swap in the new signal mask, and wait for a signal.
289 #ifdef CONFIG_TRAD_SIGNALS
290 asmlinkage int sys_sigsuspend(nabi_no_regargs struct pt_regs regs)
292 sigset_t newset;
293 sigset_t __user *uset;
295 uset = (sigset_t __user *) regs.regs[4];
296 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
297 return -EFAULT;
298 sigdelsetmask(&newset, ~_BLOCKABLE);
300 spin_lock_irq(&current->sighand->siglock);
301 current->saved_sigmask = current->blocked;
302 current->blocked = newset;
303 recalc_sigpending();
304 spin_unlock_irq(&current->sighand->siglock);
306 current->state = TASK_INTERRUPTIBLE;
307 schedule();
308 set_thread_flag(TIF_RESTORE_SIGMASK);
309 return -ERESTARTNOHAND;
311 #endif
313 asmlinkage int sys_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
315 sigset_t newset;
316 sigset_t __user *unewset;
317 size_t sigsetsize;
319 /* XXX Don't preclude handling different sized sigset_t's. */
320 sigsetsize = regs.regs[5];
321 if (sigsetsize != sizeof(sigset_t))
322 return -EINVAL;
324 unewset = (sigset_t __user *) regs.regs[4];
325 if (copy_from_user(&newset, unewset, sizeof(newset)))
326 return -EFAULT;
327 sigdelsetmask(&newset, ~_BLOCKABLE);
329 spin_lock_irq(&current->sighand->siglock);
330 current->saved_sigmask = current->blocked;
331 current->blocked = newset;
332 recalc_sigpending();
333 spin_unlock_irq(&current->sighand->siglock);
335 current->state = TASK_INTERRUPTIBLE;
336 schedule();
337 set_thread_flag(TIF_RESTORE_SIGMASK);
338 return -ERESTARTNOHAND;
341 #ifdef CONFIG_TRAD_SIGNALS
342 asmlinkage int sys_sigaction(int sig, const struct sigaction __user *act,
343 struct sigaction __user *oact)
345 struct k_sigaction new_ka, old_ka;
346 int ret;
347 int err = 0;
349 if (act) {
350 old_sigset_t mask;
352 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
353 return -EFAULT;
354 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
355 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
356 err |= __get_user(mask, &act->sa_mask.sig[0]);
357 if (err)
358 return -EFAULT;
360 siginitset(&new_ka.sa.sa_mask, mask);
363 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
365 if (!ret && oact) {
366 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
367 return -EFAULT;
368 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
369 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
370 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
371 err |= __put_user(0, &oact->sa_mask.sig[1]);
372 err |= __put_user(0, &oact->sa_mask.sig[2]);
373 err |= __put_user(0, &oact->sa_mask.sig[3]);
374 if (err)
375 return -EFAULT;
378 return ret;
380 #endif
382 asmlinkage int sys_sigaltstack(nabi_no_regargs struct pt_regs regs)
384 const stack_t __user *uss = (const stack_t __user *) regs.regs[4];
385 stack_t __user *uoss = (stack_t __user *) regs.regs[5];
386 unsigned long usp = regs.regs[29];
388 return do_sigaltstack(uss, uoss, usp);
391 #ifdef CONFIG_TRAD_SIGNALS
392 asmlinkage void sys_sigreturn(nabi_no_regargs struct pt_regs regs)
394 struct sigframe __user *frame;
395 sigset_t blocked;
396 int sig;
398 frame = (struct sigframe __user *) regs.regs[29];
399 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
400 goto badframe;
401 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
402 goto badframe;
404 sigdelsetmask(&blocked, ~_BLOCKABLE);
405 spin_lock_irq(&current->sighand->siglock);
406 current->blocked = blocked;
407 recalc_sigpending();
408 spin_unlock_irq(&current->sighand->siglock);
410 sig = restore_sigcontext(&regs, &frame->sf_sc);
411 if (sig < 0)
412 goto badframe;
413 else if (sig)
414 force_sig(sig, current);
417 * Don't let your children do this ...
419 __asm__ __volatile__(
420 "move\t$29, %0\n\t"
421 "j\tsyscall_exit"
422 :/* no outputs */
423 :"r" (&regs));
424 /* Unreached */
426 badframe:
427 force_sig(SIGSEGV, current);
429 #endif /* CONFIG_TRAD_SIGNALS */
431 asmlinkage void sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
433 struct rt_sigframe __user *frame;
434 sigset_t set;
435 stack_t st;
436 int sig;
438 frame = (struct rt_sigframe __user *) regs.regs[29];
439 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
440 goto badframe;
441 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
442 goto badframe;
444 sigdelsetmask(&set, ~_BLOCKABLE);
445 spin_lock_irq(&current->sighand->siglock);
446 current->blocked = set;
447 recalc_sigpending();
448 spin_unlock_irq(&current->sighand->siglock);
450 sig = restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext);
451 if (sig < 0)
452 goto badframe;
453 else if (sig)
454 force_sig(sig, current);
456 if (__copy_from_user(&st, &frame->rs_uc.uc_stack, sizeof(st)))
457 goto badframe;
458 /* It is more difficult to avoid calling this function than to
459 call it and ignore errors. */
460 do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]);
463 * Don't let your children do this ...
465 __asm__ __volatile__(
466 "move\t$29, %0\n\t"
467 "j\tsyscall_exit"
468 :/* no outputs */
469 :"r" (&regs));
470 /* Unreached */
472 badframe:
473 force_sig(SIGSEGV, current);
476 #ifdef CONFIG_TRAD_SIGNALS
477 static int setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
478 int signr, sigset_t *set)
480 struct sigframe __user *frame;
481 int err = 0;
483 frame = get_sigframe(ka, regs, sizeof(*frame));
484 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
485 goto give_sigsegv;
487 err |= install_sigtramp(frame->sf_code, __NR_sigreturn);
489 err |= setup_sigcontext(regs, &frame->sf_sc);
490 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
491 if (err)
492 goto give_sigsegv;
495 * Arguments to signal handler:
497 * a0 = signal number
498 * a1 = 0 (should be cause)
499 * a2 = pointer to struct sigcontext
501 * $25 and c0_epc point to the signal handler, $29 points to the
502 * struct sigframe.
504 regs->regs[ 4] = signr;
505 regs->regs[ 5] = 0;
506 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
507 regs->regs[29] = (unsigned long) frame;
508 regs->regs[31] = (unsigned long) frame->sf_code;
509 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
511 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
512 current->comm, current->pid,
513 frame, regs->cp0_epc, regs->regs[31]);
514 return 0;
516 give_sigsegv:
517 force_sigsegv(signr, current);
518 return -EFAULT;
520 #endif
522 static int setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
523 int signr, sigset_t *set, siginfo_t *info)
525 struct rt_sigframe __user *frame;
526 int err = 0;
528 frame = get_sigframe(ka, regs, sizeof(*frame));
529 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
530 goto give_sigsegv;
532 err |= install_sigtramp(frame->rs_code, __NR_rt_sigreturn);
534 /* Create siginfo. */
535 err |= copy_siginfo_to_user(&frame->rs_info, info);
537 /* Create the ucontext. */
538 err |= __put_user(0, &frame->rs_uc.uc_flags);
539 err |= __put_user(NULL, &frame->rs_uc.uc_link);
540 err |= __put_user((void __user *)current->sas_ss_sp,
541 &frame->rs_uc.uc_stack.ss_sp);
542 err |= __put_user(sas_ss_flags(regs->regs[29]),
543 &frame->rs_uc.uc_stack.ss_flags);
544 err |= __put_user(current->sas_ss_size,
545 &frame->rs_uc.uc_stack.ss_size);
546 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
547 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
549 if (err)
550 goto give_sigsegv;
553 * Arguments to signal handler:
555 * a0 = signal number
556 * a1 = 0 (should be cause)
557 * a2 = pointer to ucontext
559 * $25 and c0_epc point to the signal handler, $29 points to
560 * the struct rt_sigframe.
562 regs->regs[ 4] = signr;
563 regs->regs[ 5] = (unsigned long) &frame->rs_info;
564 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
565 regs->regs[29] = (unsigned long) frame;
566 regs->regs[31] = (unsigned long) frame->rs_code;
567 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
569 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
570 current->comm, current->pid,
571 frame, regs->cp0_epc, regs->regs[31]);
573 return 0;
575 give_sigsegv:
576 force_sigsegv(signr, current);
577 return -EFAULT;
580 struct mips_abi mips_abi = {
581 #ifdef CONFIG_TRAD_SIGNALS
582 .setup_frame = setup_frame,
583 #endif
584 .setup_rt_frame = setup_rt_frame,
585 .restart = __NR_restart_syscall
588 static int handle_signal(unsigned long sig, siginfo_t *info,
589 struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
591 int ret;
593 switch(regs->regs[0]) {
594 case ERESTART_RESTARTBLOCK:
595 case ERESTARTNOHAND:
596 regs->regs[2] = EINTR;
597 break;
598 case ERESTARTSYS:
599 if (!(ka->sa.sa_flags & SA_RESTART)) {
600 regs->regs[2] = EINTR;
601 break;
603 /* fallthrough */
604 case ERESTARTNOINTR: /* Userland will reload $v0. */
605 regs->regs[7] = regs->regs[26];
606 regs->cp0_epc -= 8;
609 regs->regs[0] = 0; /* Don't deal with this again. */
611 if (sig_uses_siginfo(ka))
612 ret = current->thread.abi->setup_rt_frame(ka, regs, sig, oldset, info);
613 else
614 ret = current->thread.abi->setup_frame(ka, regs, sig, oldset);
616 spin_lock_irq(&current->sighand->siglock);
617 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
618 if (!(ka->sa.sa_flags & SA_NODEFER))
619 sigaddset(&current->blocked,sig);
620 recalc_sigpending();
621 spin_unlock_irq(&current->sighand->siglock);
623 return ret;
626 static void do_signal(struct pt_regs *regs)
628 struct k_sigaction ka;
629 sigset_t *oldset;
630 siginfo_t info;
631 int signr;
634 * We want the common case to go fast, which is why we may in certain
635 * cases get here from kernel mode. Just return without doing anything
636 * if so.
638 if (!user_mode(regs))
639 return;
641 if (test_thread_flag(TIF_RESTORE_SIGMASK))
642 oldset = &current->saved_sigmask;
643 else
644 oldset = &current->blocked;
646 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
647 if (signr > 0) {
648 /* Whee! Actually deliver the signal. */
649 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
651 * A signal was successfully delivered; the saved
652 * sigmask will have been stored in the signal frame,
653 * and will be restored by sigreturn, so we can simply
654 * clear the TIF_RESTORE_SIGMASK flag.
656 if (test_thread_flag(TIF_RESTORE_SIGMASK))
657 clear_thread_flag(TIF_RESTORE_SIGMASK);
660 return;
664 * Who's code doesn't conform to the restartable syscall convention
665 * dies here!!! The li instruction, a single machine instruction,
666 * must directly be followed by the syscall instruction.
668 if (regs->regs[0]) {
669 if (regs->regs[2] == ERESTARTNOHAND ||
670 regs->regs[2] == ERESTARTSYS ||
671 regs->regs[2] == ERESTARTNOINTR) {
672 regs->regs[7] = regs->regs[26];
673 regs->cp0_epc -= 8;
675 if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
676 regs->regs[2] = current->thread.abi->restart;
677 regs->regs[7] = regs->regs[26];
678 regs->cp0_epc -= 4;
680 regs->regs[0] = 0; /* Don't deal with this again. */
684 * If there's no signal to deliver, we just put the saved sigmask
685 * back
687 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
688 clear_thread_flag(TIF_RESTORE_SIGMASK);
689 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
694 * notification of userspace execution resumption
695 * - triggered by the TIF_WORK_MASK flags
697 asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
698 __u32 thread_info_flags)
700 /* deal with pending signal delivery */
701 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
702 do_signal(regs);