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.
9 * Copyright (C) 2014, Imagination Technologies Ltd.
11 #include <linux/cache.h>
12 #include <linux/context_tracking.h>
13 #include <linux/irqflags.h>
14 #include <linux/sched.h>
16 #include <linux/personality.h>
17 #include <linux/smp.h>
18 #include <linux/kernel.h>
19 #include <linux/signal.h>
20 #include <linux/errno.h>
21 #include <linux/wait.h>
22 #include <linux/ptrace.h>
23 #include <linux/unistd.h>
24 #include <linux/compiler.h>
25 #include <linux/syscalls.h>
26 #include <linux/uaccess.h>
27 #include <linux/tracehook.h>
31 #include <linux/bitops.h>
32 #include <asm/cacheflush.h>
35 #include <asm/ucontext.h>
36 #include <asm/cpu-features.h>
42 #include "signal-common.h"
44 static int (*save_fp_context)(void __user *sc);
45 static int (*restore_fp_context)(void __user *sc);
48 u32 sf_ass[4]; /* argument save space for o32 */
49 u32 sf_pad[2]; /* Was: signal trampoline */
50 struct sigcontext sf_sc;
55 u32 rs_ass[4]; /* argument save space for o32 */
56 u32 rs_pad[2]; /* Was: signal trampoline */
57 struct siginfo rs_info;
58 struct ucontext rs_uc;
62 * Thread saved context copy to/from a signal context presumed to be on the
63 * user stack, and therefore accessed with appropriate macros from uaccess.h.
65 static int copy_fp_to_sigcontext(void __user *sc)
67 struct mips_abi *abi = current->thread.abi;
68 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
69 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
72 int inc = test_thread_flag(TIF_32BIT_FPREGS) ? 2 : 1;
74 for (i = 0; i < NUM_FPU_REGS; i += inc) {
76 __put_user(get_fpr64(¤t->thread.fpu.fpr[i], 0),
79 err |= __put_user(current->thread.fpu.fcr31, csr);
84 static int copy_fp_from_sigcontext(void __user *sc)
86 struct mips_abi *abi = current->thread.abi;
87 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
88 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
91 int inc = test_thread_flag(TIF_32BIT_FPREGS) ? 2 : 1;
94 for (i = 0; i < NUM_FPU_REGS; i += inc) {
95 err |= __get_user(fpr_val, &fpregs[i]);
96 set_fpr64(¤t->thread.fpu.fpr[i], 0, fpr_val);
98 err |= __get_user(current->thread.fpu.fcr31, csr);
104 * Wrappers for the assembly _{save,restore}_fp_context functions.
106 static int save_hw_fp_context(void __user *sc)
108 struct mips_abi *abi = current->thread.abi;
109 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
110 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
112 return _save_fp_context(fpregs, csr);
115 static int restore_hw_fp_context(void __user *sc)
117 struct mips_abi *abi = current->thread.abi;
118 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
119 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
121 return _restore_fp_context(fpregs, csr);
127 int protected_save_fp_context(void __user *sc)
129 struct mips_abi *abi = current->thread.abi;
130 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
131 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
132 uint32_t __user *used_math = sc + abi->off_sc_used_math;
136 used = used_math() ? USED_FP : 0;
138 if (!test_thread_flag(TIF_32BIT_FPREGS))
140 if (test_thread_flag(TIF_HYBRID_FPREGS))
141 used |= USED_HYBRID_FPRS;
144 err = __put_user(used, used_math);
145 if (err || !(used & USED_FP))
149 * EVA does not have userland equivalents of ldc1 or sdc1, so
150 * save to the kernel FP context & copy that to userland below.
152 if (config_enabled(CONFIG_EVA))
157 if (is_fpu_owner()) {
158 err = save_fp_context(sc);
162 err = copy_fp_to_sigcontext(sc);
166 /* touch the sigcontext and try again */
167 err = __put_user(0, &fpregs[0]) |
168 __put_user(0, &fpregs[31]) |
171 break; /* really bad sigcontext */
177 int protected_restore_fp_context(void __user *sc)
179 struct mips_abi *abi = current->thread.abi;
180 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
181 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
182 uint32_t __user *used_math = sc + abi->off_sc_used_math;
184 int err, sig, tmp __maybe_unused;
186 err = __get_user(used, used_math);
187 conditional_used_math(used & USED_FP);
190 * The signal handler may have used FPU; give it up if the program
191 * doesn't want it following sigreturn.
193 if (err || !(used & USED_FP)) {
198 err = sig = fpcsr_pending(csr);
203 * EVA does not have userland equivalents of ldc1 or sdc1, so we
204 * disable the FPU here such that the code below simply copies to
205 * the kernel FP context.
207 if (config_enabled(CONFIG_EVA))
212 if (is_fpu_owner()) {
213 err = restore_fp_context(sc);
217 err = copy_fp_from_sigcontext(sc);
221 /* touch the sigcontext and try again */
222 err = __get_user(tmp, &fpregs[0]) |
223 __get_user(tmp, &fpregs[31]) |
224 __get_user(tmp, csr);
226 break; /* really bad sigcontext */
232 int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
237 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
239 err |= __put_user(0, &sc->sc_regs[0]);
240 for (i = 1; i < 32; i++)
241 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
243 #ifdef CONFIG_CPU_HAS_SMARTMIPS
244 err |= __put_user(regs->acx, &sc->sc_acx);
246 err |= __put_user(regs->hi, &sc->sc_mdhi);
247 err |= __put_user(regs->lo, &sc->sc_mdlo);
249 err |= __put_user(mfhi1(), &sc->sc_hi1);
250 err |= __put_user(mflo1(), &sc->sc_lo1);
251 err |= __put_user(mfhi2(), &sc->sc_hi2);
252 err |= __put_user(mflo2(), &sc->sc_lo2);
253 err |= __put_user(mfhi3(), &sc->sc_hi3);
254 err |= __put_user(mflo3(), &sc->sc_lo3);
255 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
260 * Save FPU state to signal context. Signal handler
261 * will "inherit" current FPU state.
263 err |= protected_save_fp_context(sc);
268 int fpcsr_pending(unsigned int __user *fpcsr)
271 unsigned int csr, enabled;
273 err = __get_user(csr, fpcsr);
274 enabled = FPU_CSR_UNI_X | ((csr & FPU_CSR_ALL_E) << 5);
276 * If the signal handler set some FPU exceptions, clear it and
281 err |= __put_user(csr, fpcsr);
287 int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
293 /* Always make any pending restarted system calls return -EINTR */
294 current->restart_block.fn = do_no_restart_syscall;
296 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
298 #ifdef CONFIG_CPU_HAS_SMARTMIPS
299 err |= __get_user(regs->acx, &sc->sc_acx);
301 err |= __get_user(regs->hi, &sc->sc_mdhi);
302 err |= __get_user(regs->lo, &sc->sc_mdlo);
304 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
305 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
306 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
307 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
308 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
309 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
310 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
313 for (i = 1; i < 32; i++)
314 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
316 return err ?: protected_restore_fp_context(sc);
319 void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
324 /* Default to using normal stack */
328 * FPU emulator may have it's own trampoline active just
329 * above the user stack, 16-bytes before the next lowest
330 * 16 byte boundary. Try to avoid trashing it.
334 sp = sigsp(sp, ksig);
336 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
340 * Atomically swap in the new signal mask, and wait for a signal.
343 #ifdef CONFIG_TRAD_SIGNALS
344 SYSCALL_DEFINE1(sigsuspend, sigset_t __user *, uset)
346 return sys_rt_sigsuspend(uset, sizeof(sigset_t));
350 #ifdef CONFIG_TRAD_SIGNALS
351 SYSCALL_DEFINE3(sigaction, int, sig, const struct sigaction __user *, act,
352 struct sigaction __user *, oact)
354 struct k_sigaction new_ka, old_ka;
361 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
363 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
364 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
365 err |= __get_user(mask, &act->sa_mask.sig[0]);
369 siginitset(&new_ka.sa.sa_mask, mask);
372 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
375 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
377 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
378 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
379 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
380 err |= __put_user(0, &oact->sa_mask.sig[1]);
381 err |= __put_user(0, &oact->sa_mask.sig[2]);
382 err |= __put_user(0, &oact->sa_mask.sig[3]);
391 #ifdef CONFIG_TRAD_SIGNALS
392 asmlinkage void sys_sigreturn(nabi_no_regargs struct pt_regs regs)
394 struct sigframe __user *frame;
398 frame = (struct sigframe __user *) regs.regs[29];
399 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
401 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
404 set_current_blocked(&blocked);
406 sig = restore_sigcontext(®s, &frame->sf_sc);
410 force_sig(sig, current);
413 * Don't let your children do this ...
415 __asm__ __volatile__(
423 force_sig(SIGSEGV, current);
425 #endif /* CONFIG_TRAD_SIGNALS */
427 asmlinkage void sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
429 struct rt_sigframe __user *frame;
433 frame = (struct rt_sigframe __user *) regs.regs[29];
434 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
436 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
439 set_current_blocked(&set);
441 sig = restore_sigcontext(®s, &frame->rs_uc.uc_mcontext);
445 force_sig(sig, current);
447 if (restore_altstack(&frame->rs_uc.uc_stack))
451 * Don't let your children do this ...
453 __asm__ __volatile__(
461 force_sig(SIGSEGV, current);
464 #ifdef CONFIG_TRAD_SIGNALS
465 static int setup_frame(void *sig_return, struct ksignal *ksig,
466 struct pt_regs *regs, sigset_t *set)
468 struct sigframe __user *frame;
471 frame = get_sigframe(ksig, regs, sizeof(*frame));
472 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
475 err |= setup_sigcontext(regs, &frame->sf_sc);
476 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
481 * Arguments to signal handler:
484 * a1 = 0 (should be cause)
485 * a2 = pointer to struct sigcontext
487 * $25 and c0_epc point to the signal handler, $29 points to the
490 regs->regs[ 4] = ksig->sig;
492 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
493 regs->regs[29] = (unsigned long) frame;
494 regs->regs[31] = (unsigned long) sig_return;
495 regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
497 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
498 current->comm, current->pid,
499 frame, regs->cp0_epc, regs->regs[31]);
504 static int setup_rt_frame(void *sig_return, struct ksignal *ksig,
505 struct pt_regs *regs, sigset_t *set)
507 struct rt_sigframe __user *frame;
510 frame = get_sigframe(ksig, regs, sizeof(*frame));
511 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
514 /* Create siginfo. */
515 err |= copy_siginfo_to_user(&frame->rs_info, &ksig->info);
517 /* Create the ucontext. */
518 err |= __put_user(0, &frame->rs_uc.uc_flags);
519 err |= __put_user(NULL, &frame->rs_uc.uc_link);
520 err |= __save_altstack(&frame->rs_uc.uc_stack, regs->regs[29]);
521 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
522 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
528 * Arguments to signal handler:
531 * a1 = 0 (should be cause)
532 * a2 = pointer to ucontext
534 * $25 and c0_epc point to the signal handler, $29 points to
535 * the struct rt_sigframe.
537 regs->regs[ 4] = ksig->sig;
538 regs->regs[ 5] = (unsigned long) &frame->rs_info;
539 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
540 regs->regs[29] = (unsigned long) frame;
541 regs->regs[31] = (unsigned long) sig_return;
542 regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
544 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
545 current->comm, current->pid,
546 frame, regs->cp0_epc, regs->regs[31]);
551 struct mips_abi mips_abi = {
552 #ifdef CONFIG_TRAD_SIGNALS
553 .setup_frame = setup_frame,
554 .signal_return_offset = offsetof(struct mips_vdso, signal_trampoline),
556 .setup_rt_frame = setup_rt_frame,
557 .rt_signal_return_offset =
558 offsetof(struct mips_vdso, rt_signal_trampoline),
559 .restart = __NR_restart_syscall,
561 .off_sc_fpregs = offsetof(struct sigcontext, sc_fpregs),
562 .off_sc_fpc_csr = offsetof(struct sigcontext, sc_fpc_csr),
563 .off_sc_used_math = offsetof(struct sigcontext, sc_used_math),
566 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
568 sigset_t *oldset = sigmask_to_save();
570 struct mips_abi *abi = current->thread.abi;
571 #ifdef CONFIG_CPU_MICROMIPS
573 unsigned long tmp = (unsigned long)current->mm->context.vdso;
578 void *vdso = current->mm->context.vdso;
582 switch(regs->regs[2]) {
583 case ERESTART_RESTARTBLOCK:
585 regs->regs[2] = EINTR;
588 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
589 regs->regs[2] = EINTR;
594 regs->regs[7] = regs->regs[26];
595 regs->regs[2] = regs->regs[0];
599 regs->regs[0] = 0; /* Don't deal with this again. */
602 if (sig_uses_siginfo(&ksig->ka))
603 ret = abi->setup_rt_frame(vdso + abi->rt_signal_return_offset,
606 ret = abi->setup_frame(vdso + abi->signal_return_offset, ksig,
609 signal_setup_done(ret, ksig, 0);
612 static void do_signal(struct pt_regs *regs)
616 if (get_signal(&ksig)) {
617 /* Whee! Actually deliver the signal. */
618 handle_signal(&ksig, regs);
623 switch (regs->regs[2]) {
627 regs->regs[2] = regs->regs[0];
628 regs->regs[7] = regs->regs[26];
632 case ERESTART_RESTARTBLOCK:
633 regs->regs[2] = current->thread.abi->restart;
634 regs->regs[7] = regs->regs[26];
638 regs->regs[0] = 0; /* Don't deal with this again. */
642 * If there's no signal to deliver, we just put the saved sigmask
645 restore_saved_sigmask();
649 * notification of userspace execution resumption
650 * - triggered by the TIF_WORK_MASK flags
652 asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
653 __u32 thread_info_flags)
659 /* deal with pending signal delivery */
660 if (thread_info_flags & _TIF_SIGPENDING)
663 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
664 clear_thread_flag(TIF_NOTIFY_RESUME);
665 tracehook_notify_resume(regs);
672 static int smp_save_fp_context(void __user *sc)
674 return raw_cpu_has_fpu
675 ? save_hw_fp_context(sc)
676 : copy_fp_to_sigcontext(sc);
679 static int smp_restore_fp_context(void __user *sc)
681 return raw_cpu_has_fpu
682 ? restore_hw_fp_context(sc)
683 : copy_fp_from_sigcontext(sc);
687 static int signal_setup(void)
690 /* For now just do the cpu_has_fpu check when the functions are invoked */
691 save_fp_context = smp_save_fp_context;
692 restore_fp_context = smp_restore_fp_context;
695 save_fp_context = save_hw_fp_context;
696 restore_fp_context = restore_hw_fp_context;
698 save_fp_context = copy_fp_to_sigcontext;
699 restore_fp_context = copy_fp_from_sigcontext;
701 #endif /* CONFIG_SMP */
706 arch_initcall(signal_setup);