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 */
51 /* Matches struct ucontext from its uc_mcontext field onwards */
52 struct sigcontext sf_sc;
54 unsigned long long sf_extcontext[0];
58 u32 rs_ass[4]; /* argument save space for o32 */
59 u32 rs_pad[2]; /* Was: signal trampoline */
60 struct siginfo rs_info;
61 struct ucontext rs_uc;
65 * Thread saved context copy to/from a signal context presumed to be on the
66 * user stack, and therefore accessed with appropriate macros from uaccess.h.
68 static int copy_fp_to_sigcontext(void __user *sc)
70 struct mips_abi *abi = current->thread.abi;
71 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
72 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
75 int inc = test_thread_flag(TIF_32BIT_FPREGS) ? 2 : 1;
77 for (i = 0; i < NUM_FPU_REGS; i += inc) {
79 __put_user(get_fpr64(¤t->thread.fpu.fpr[i], 0),
82 err |= __put_user(current->thread.fpu.fcr31, csr);
87 static int copy_fp_from_sigcontext(void __user *sc)
89 struct mips_abi *abi = current->thread.abi;
90 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
91 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
94 int inc = test_thread_flag(TIF_32BIT_FPREGS) ? 2 : 1;
97 for (i = 0; i < NUM_FPU_REGS; i += inc) {
98 err |= __get_user(fpr_val, &fpregs[i]);
99 set_fpr64(¤t->thread.fpu.fpr[i], 0, fpr_val);
101 err |= __get_user(current->thread.fpu.fcr31, csr);
107 * Wrappers for the assembly _{save,restore}_fp_context functions.
109 static int save_hw_fp_context(void __user *sc)
111 struct mips_abi *abi = current->thread.abi;
112 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
113 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
115 return _save_fp_context(fpregs, csr);
118 static int restore_hw_fp_context(void __user *sc)
120 struct mips_abi *abi = current->thread.abi;
121 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
122 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
124 return _restore_fp_context(fpregs, csr);
130 int protected_save_fp_context(void __user *sc)
132 struct mips_abi *abi = current->thread.abi;
133 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
134 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
135 uint32_t __user *used_math = sc + abi->off_sc_used_math;
139 used = used_math() ? USED_FP : 0;
141 if (!test_thread_flag(TIF_32BIT_FPREGS))
143 if (test_thread_flag(TIF_HYBRID_FPREGS))
144 used |= USED_HYBRID_FPRS;
147 err = __put_user(used, used_math);
148 if (err || !(used & USED_FP))
152 * EVA does not have userland equivalents of ldc1 or sdc1, so
153 * save to the kernel FP context & copy that to userland below.
155 if (config_enabled(CONFIG_EVA))
160 if (is_fpu_owner()) {
161 err = save_fp_context(sc);
165 err = copy_fp_to_sigcontext(sc);
169 /* touch the sigcontext and try again */
170 err = __put_user(0, &fpregs[0]) |
171 __put_user(0, &fpregs[31]) |
174 break; /* really bad sigcontext */
180 int protected_restore_fp_context(void __user *sc)
182 struct mips_abi *abi = current->thread.abi;
183 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
184 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
185 uint32_t __user *used_math = sc + abi->off_sc_used_math;
187 int err, sig, tmp __maybe_unused;
189 err = __get_user(used, used_math);
190 conditional_used_math(used & USED_FP);
193 * The signal handler may have used FPU; give it up if the program
194 * doesn't want it following sigreturn.
196 if (err || !(used & USED_FP)) {
201 err = sig = fpcsr_pending(csr);
206 * EVA does not have userland equivalents of ldc1 or sdc1, so we
207 * disable the FPU here such that the code below simply copies to
208 * the kernel FP context.
210 if (config_enabled(CONFIG_EVA))
215 if (is_fpu_owner()) {
216 err = restore_fp_context(sc);
220 err = copy_fp_from_sigcontext(sc);
224 /* touch the sigcontext and try again */
225 err = __get_user(tmp, &fpregs[0]) |
226 __get_user(tmp, &fpregs[31]) |
227 __get_user(tmp, csr);
229 break; /* really bad sigcontext */
235 int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
240 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
242 err |= __put_user(0, &sc->sc_regs[0]);
243 for (i = 1; i < 32; i++)
244 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
246 #ifdef CONFIG_CPU_HAS_SMARTMIPS
247 err |= __put_user(regs->acx, &sc->sc_acx);
249 err |= __put_user(regs->hi, &sc->sc_mdhi);
250 err |= __put_user(regs->lo, &sc->sc_mdlo);
252 err |= __put_user(mfhi1(), &sc->sc_hi1);
253 err |= __put_user(mflo1(), &sc->sc_lo1);
254 err |= __put_user(mfhi2(), &sc->sc_hi2);
255 err |= __put_user(mflo2(), &sc->sc_lo2);
256 err |= __put_user(mfhi3(), &sc->sc_hi3);
257 err |= __put_user(mflo3(), &sc->sc_lo3);
258 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
263 * Save FPU state to signal context. Signal handler
264 * will "inherit" current FPU state.
266 err |= protected_save_fp_context(sc);
271 int fpcsr_pending(unsigned int __user *fpcsr)
274 unsigned int csr, enabled;
276 err = __get_user(csr, fpcsr);
277 enabled = FPU_CSR_UNI_X | ((csr & FPU_CSR_ALL_E) << 5);
279 * If the signal handler set some FPU exceptions, clear it and
284 err |= __put_user(csr, fpcsr);
290 int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
296 /* Always make any pending restarted system calls return -EINTR */
297 current->restart_block.fn = do_no_restart_syscall;
299 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
301 #ifdef CONFIG_CPU_HAS_SMARTMIPS
302 err |= __get_user(regs->acx, &sc->sc_acx);
304 err |= __get_user(regs->hi, &sc->sc_mdhi);
305 err |= __get_user(regs->lo, &sc->sc_mdlo);
307 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
308 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
309 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
310 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
311 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
312 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
313 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
316 for (i = 1; i < 32; i++)
317 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
319 return err ?: protected_restore_fp_context(sc);
322 void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
327 /* Default to using normal stack */
331 * FPU emulator may have it's own trampoline active just
332 * above the user stack, 16-bytes before the next lowest
333 * 16 byte boundary. Try to avoid trashing it.
337 sp = sigsp(sp, ksig);
339 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
343 * Atomically swap in the new signal mask, and wait for a signal.
346 #ifdef CONFIG_TRAD_SIGNALS
347 SYSCALL_DEFINE1(sigsuspend, sigset_t __user *, uset)
349 return sys_rt_sigsuspend(uset, sizeof(sigset_t));
353 #ifdef CONFIG_TRAD_SIGNALS
354 SYSCALL_DEFINE3(sigaction, int, sig, const struct sigaction __user *, act,
355 struct sigaction __user *, oact)
357 struct k_sigaction new_ka, old_ka;
364 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
366 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
367 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
368 err |= __get_user(mask, &act->sa_mask.sig[0]);
372 siginitset(&new_ka.sa.sa_mask, mask);
375 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
378 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
380 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
381 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
382 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
383 err |= __put_user(0, &oact->sa_mask.sig[1]);
384 err |= __put_user(0, &oact->sa_mask.sig[2]);
385 err |= __put_user(0, &oact->sa_mask.sig[3]);
394 #ifdef CONFIG_TRAD_SIGNALS
395 asmlinkage void sys_sigreturn(nabi_no_regargs struct pt_regs regs)
397 struct sigframe __user *frame;
401 frame = (struct sigframe __user *) regs.regs[29];
402 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
404 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
407 set_current_blocked(&blocked);
409 sig = restore_sigcontext(®s, &frame->sf_sc);
413 force_sig(sig, current);
416 * Don't let your children do this ...
418 __asm__ __volatile__(
426 force_sig(SIGSEGV, current);
428 #endif /* CONFIG_TRAD_SIGNALS */
430 asmlinkage void sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
432 struct rt_sigframe __user *frame;
436 frame = (struct rt_sigframe __user *) regs.regs[29];
437 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
439 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
442 set_current_blocked(&set);
444 sig = restore_sigcontext(®s, &frame->rs_uc.uc_mcontext);
448 force_sig(sig, current);
450 if (restore_altstack(&frame->rs_uc.uc_stack))
454 * Don't let your children do this ...
456 __asm__ __volatile__(
464 force_sig(SIGSEGV, current);
467 #ifdef CONFIG_TRAD_SIGNALS
468 static int setup_frame(void *sig_return, struct ksignal *ksig,
469 struct pt_regs *regs, sigset_t *set)
471 struct sigframe __user *frame;
474 frame = get_sigframe(ksig, regs, sizeof(*frame));
475 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
478 err |= setup_sigcontext(regs, &frame->sf_sc);
479 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
484 * Arguments to signal handler:
487 * a1 = 0 (should be cause)
488 * a2 = pointer to struct sigcontext
490 * $25 and c0_epc point to the signal handler, $29 points to the
493 regs->regs[ 4] = ksig->sig;
495 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
496 regs->regs[29] = (unsigned long) frame;
497 regs->regs[31] = (unsigned long) sig_return;
498 regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->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 static int setup_rt_frame(void *sig_return, struct ksignal *ksig,
508 struct pt_regs *regs, sigset_t *set)
510 struct rt_sigframe __user *frame;
513 frame = get_sigframe(ksig, regs, sizeof(*frame));
514 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
517 /* Create siginfo. */
518 err |= copy_siginfo_to_user(&frame->rs_info, &ksig->info);
520 /* Create the ucontext. */
521 err |= __put_user(0, &frame->rs_uc.uc_flags);
522 err |= __put_user(NULL, &frame->rs_uc.uc_link);
523 err |= __save_altstack(&frame->rs_uc.uc_stack, regs->regs[29]);
524 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
525 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
531 * Arguments to signal handler:
534 * a1 = 0 (should be cause)
535 * a2 = pointer to ucontext
537 * $25 and c0_epc point to the signal handler, $29 points to
538 * the struct rt_sigframe.
540 regs->regs[ 4] = ksig->sig;
541 regs->regs[ 5] = (unsigned long) &frame->rs_info;
542 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
543 regs->regs[29] = (unsigned long) frame;
544 regs->regs[31] = (unsigned long) sig_return;
545 regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
547 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
548 current->comm, current->pid,
549 frame, regs->cp0_epc, regs->regs[31]);
554 struct mips_abi mips_abi = {
555 #ifdef CONFIG_TRAD_SIGNALS
556 .setup_frame = setup_frame,
557 .signal_return_offset = offsetof(struct mips_vdso, signal_trampoline),
559 .setup_rt_frame = setup_rt_frame,
560 .rt_signal_return_offset =
561 offsetof(struct mips_vdso, rt_signal_trampoline),
562 .restart = __NR_restart_syscall,
564 .off_sc_fpregs = offsetof(struct sigcontext, sc_fpregs),
565 .off_sc_fpc_csr = offsetof(struct sigcontext, sc_fpc_csr),
566 .off_sc_used_math = offsetof(struct sigcontext, sc_used_math),
569 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
571 sigset_t *oldset = sigmask_to_save();
573 struct mips_abi *abi = current->thread.abi;
574 #ifdef CONFIG_CPU_MICROMIPS
576 unsigned long tmp = (unsigned long)current->mm->context.vdso;
581 void *vdso = current->mm->context.vdso;
585 switch(regs->regs[2]) {
586 case ERESTART_RESTARTBLOCK:
588 regs->regs[2] = EINTR;
591 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
592 regs->regs[2] = EINTR;
597 regs->regs[7] = regs->regs[26];
598 regs->regs[2] = regs->regs[0];
602 regs->regs[0] = 0; /* Don't deal with this again. */
605 if (sig_uses_siginfo(&ksig->ka))
606 ret = abi->setup_rt_frame(vdso + abi->rt_signal_return_offset,
609 ret = abi->setup_frame(vdso + abi->signal_return_offset, ksig,
612 signal_setup_done(ret, ksig, 0);
615 static void do_signal(struct pt_regs *regs)
619 if (get_signal(&ksig)) {
620 /* Whee! Actually deliver the signal. */
621 handle_signal(&ksig, regs);
626 switch (regs->regs[2]) {
630 regs->regs[2] = regs->regs[0];
631 regs->regs[7] = regs->regs[26];
635 case ERESTART_RESTARTBLOCK:
636 regs->regs[2] = current->thread.abi->restart;
637 regs->regs[7] = regs->regs[26];
641 regs->regs[0] = 0; /* Don't deal with this again. */
645 * If there's no signal to deliver, we just put the saved sigmask
648 restore_saved_sigmask();
652 * notification of userspace execution resumption
653 * - triggered by the TIF_WORK_MASK flags
655 asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
656 __u32 thread_info_flags)
662 /* deal with pending signal delivery */
663 if (thread_info_flags & _TIF_SIGPENDING)
666 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
667 clear_thread_flag(TIF_NOTIFY_RESUME);
668 tracehook_notify_resume(regs);
675 static int smp_save_fp_context(void __user *sc)
677 return raw_cpu_has_fpu
678 ? save_hw_fp_context(sc)
679 : copy_fp_to_sigcontext(sc);
682 static int smp_restore_fp_context(void __user *sc)
684 return raw_cpu_has_fpu
685 ? restore_hw_fp_context(sc)
686 : copy_fp_from_sigcontext(sc);
690 static int signal_setup(void)
693 * The offset from sigcontext to extended context should be the same
694 * regardless of the type of signal, such that userland can always know
695 * where to look if it wishes to find the extended context structures.
697 BUILD_BUG_ON((offsetof(struct sigframe, sf_extcontext) -
698 offsetof(struct sigframe, sf_sc)) !=
699 (offsetof(struct rt_sigframe, rs_uc.uc_extcontext) -
700 offsetof(struct rt_sigframe, rs_uc.uc_mcontext)));
703 /* For now just do the cpu_has_fpu check when the functions are invoked */
704 save_fp_context = smp_save_fp_context;
705 restore_fp_context = smp_restore_fp_context;
708 save_fp_context = save_hw_fp_context;
709 restore_fp_context = restore_hw_fp_context;
711 save_fp_context = copy_fp_to_sigcontext;
712 restore_fp_context = copy_fp_from_sigcontext;
714 #endif /* CONFIG_SMP */
719 arch_initcall(signal_setup);