Loading...
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.
5 *
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.
10 */
11#include <linux/cache.h>
12#include <linux/context_tracking.h>
13#include <linux/irqflags.h>
14#include <linux/sched.h>
15#include <linux/mm.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/uprobes.h>
25#include <linux/compiler.h>
26#include <linux/syscalls.h>
27#include <linux/uaccess.h>
28#include <linux/tracehook.h>
29
30#include <asm/abi.h>
31#include <asm/asm.h>
32#include <linux/bitops.h>
33#include <asm/cacheflush.h>
34#include <asm/fpu.h>
35#include <asm/sim.h>
36#include <asm/ucontext.h>
37#include <asm/cpu-features.h>
38#include <asm/war.h>
39#include <asm/dsp.h>
40#include <asm/inst.h>
41#include <asm/msa.h>
42
43#include "signal-common.h"
44
45static int (*save_fp_context)(void __user *sc);
46static int (*restore_fp_context)(void __user *sc);
47
48struct sigframe {
49 u32 sf_ass[4]; /* argument save space for o32 */
50 u32 sf_pad[2]; /* Was: signal trampoline */
51
52 /* Matches struct ucontext from its uc_mcontext field onwards */
53 struct sigcontext sf_sc;
54 sigset_t sf_mask;
55 unsigned long long sf_extcontext[0];
56};
57
58struct rt_sigframe {
59 u32 rs_ass[4]; /* argument save space for o32 */
60 u32 rs_pad[2]; /* Was: signal trampoline */
61 struct siginfo rs_info;
62 struct ucontext rs_uc;
63};
64
65#ifdef CONFIG_MIPS_FP_SUPPORT
66
67/*
68 * Thread saved context copy to/from a signal context presumed to be on the
69 * user stack, and therefore accessed with appropriate macros from uaccess.h.
70 */
71static int copy_fp_to_sigcontext(void __user *sc)
72{
73 struct mips_abi *abi = current->thread.abi;
74 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
75 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
76 int i;
77 int err = 0;
78 int inc = test_thread_flag(TIF_32BIT_FPREGS) ? 2 : 1;
79
80 for (i = 0; i < NUM_FPU_REGS; i += inc) {
81 err |=
82 __put_user(get_fpr64(¤t->thread.fpu.fpr[i], 0),
83 &fpregs[i]);
84 }
85 err |= __put_user(current->thread.fpu.fcr31, csr);
86
87 return err;
88}
89
90static int copy_fp_from_sigcontext(void __user *sc)
91{
92 struct mips_abi *abi = current->thread.abi;
93 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
94 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
95 int i;
96 int err = 0;
97 int inc = test_thread_flag(TIF_32BIT_FPREGS) ? 2 : 1;
98 u64 fpr_val;
99
100 for (i = 0; i < NUM_FPU_REGS; i += inc) {
101 err |= __get_user(fpr_val, &fpregs[i]);
102 set_fpr64(¤t->thread.fpu.fpr[i], 0, fpr_val);
103 }
104 err |= __get_user(current->thread.fpu.fcr31, csr);
105
106 return err;
107}
108
109#else /* !CONFIG_MIPS_FP_SUPPORT */
110
111static int copy_fp_to_sigcontext(void __user *sc)
112{
113 return 0;
114}
115
116static int copy_fp_from_sigcontext(void __user *sc)
117{
118 return 0;
119}
120
121#endif /* !CONFIG_MIPS_FP_SUPPORT */
122
123/*
124 * Wrappers for the assembly _{save,restore}_fp_context functions.
125 */
126static int save_hw_fp_context(void __user *sc)
127{
128 struct mips_abi *abi = current->thread.abi;
129 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
130 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
131
132 return _save_fp_context(fpregs, csr);
133}
134
135static int restore_hw_fp_context(void __user *sc)
136{
137 struct mips_abi *abi = current->thread.abi;
138 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
139 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
140
141 return _restore_fp_context(fpregs, csr);
142}
143
144/*
145 * Extended context handling.
146 */
147
148static inline void __user *sc_to_extcontext(void __user *sc)
149{
150 struct ucontext __user *uc;
151
152 /*
153 * We can just pretend the sigcontext is always embedded in a struct
154 * ucontext here, because the offset from sigcontext to extended
155 * context is the same in the struct sigframe case.
156 */
157 uc = container_of(sc, struct ucontext, uc_mcontext);
158 return &uc->uc_extcontext;
159}
160
161#ifdef CONFIG_CPU_HAS_MSA
162
163static int save_msa_extcontext(void __user *buf)
164{
165 struct msa_extcontext __user *msa = buf;
166 uint64_t val;
167 int i, err;
168
169 if (!thread_msa_context_live())
170 return 0;
171
172 /*
173 * Ensure that we can't lose the live MSA context between checking
174 * for it & writing it to memory.
175 */
176 preempt_disable();
177
178 if (is_msa_enabled()) {
179 /*
180 * There are no EVA versions of the vector register load/store
181 * instructions, so MSA context has to be saved to kernel memory
182 * and then copied to user memory. The save to kernel memory
183 * should already have been done when handling scalar FP
184 * context.
185 */
186 BUG_ON(IS_ENABLED(CONFIG_EVA));
187
188 err = __put_user(read_msa_csr(), &msa->csr);
189 err |= _save_msa_all_upper(&msa->wr);
190
191 preempt_enable();
192 } else {
193 preempt_enable();
194
195 err = __put_user(current->thread.fpu.msacsr, &msa->csr);
196
197 for (i = 0; i < NUM_FPU_REGS; i++) {
198 val = get_fpr64(¤t->thread.fpu.fpr[i], 1);
199 err |= __put_user(val, &msa->wr[i]);
200 }
201 }
202
203 err |= __put_user(MSA_EXTCONTEXT_MAGIC, &msa->ext.magic);
204 err |= __put_user(sizeof(*msa), &msa->ext.size);
205
206 return err ? -EFAULT : sizeof(*msa);
207}
208
209static int restore_msa_extcontext(void __user *buf, unsigned int size)
210{
211 struct msa_extcontext __user *msa = buf;
212 unsigned long long val;
213 unsigned int csr;
214 int i, err;
215
216 if (size != sizeof(*msa))
217 return -EINVAL;
218
219 err = get_user(csr, &msa->csr);
220 if (err)
221 return err;
222
223 preempt_disable();
224
225 if (is_msa_enabled()) {
226 /*
227 * There are no EVA versions of the vector register load/store
228 * instructions, so MSA context has to be copied to kernel
229 * memory and later loaded to registers. The same is true of
230 * scalar FP context, so FPU & MSA should have already been
231 * disabled whilst handling scalar FP context.
232 */
233 BUG_ON(IS_ENABLED(CONFIG_EVA));
234
235 write_msa_csr(csr);
236 err |= _restore_msa_all_upper(&msa->wr);
237 preempt_enable();
238 } else {
239 preempt_enable();
240
241 current->thread.fpu.msacsr = csr;
242
243 for (i = 0; i < NUM_FPU_REGS; i++) {
244 err |= __get_user(val, &msa->wr[i]);
245 set_fpr64(¤t->thread.fpu.fpr[i], 1, val);
246 }
247 }
248
249 return err;
250}
251
252#else /* !CONFIG_CPU_HAS_MSA */
253
254static int save_msa_extcontext(void __user *buf)
255{
256 return 0;
257}
258
259static int restore_msa_extcontext(void __user *buf, unsigned int size)
260{
261 return SIGSYS;
262}
263
264#endif /* !CONFIG_CPU_HAS_MSA */
265
266static int save_extcontext(void __user *buf)
267{
268 int sz;
269
270 sz = save_msa_extcontext(buf);
271 if (sz < 0)
272 return sz;
273 buf += sz;
274
275 /* If no context was saved then trivially return */
276 if (!sz)
277 return 0;
278
279 /* Write the end marker */
280 if (__put_user(END_EXTCONTEXT_MAGIC, (u32 *)buf))
281 return -EFAULT;
282
283 sz += sizeof(((struct extcontext *)NULL)->magic);
284 return sz;
285}
286
287static int restore_extcontext(void __user *buf)
288{
289 struct extcontext ext;
290 int err;
291
292 while (1) {
293 err = __get_user(ext.magic, (unsigned int *)buf);
294 if (err)
295 return err;
296
297 if (ext.magic == END_EXTCONTEXT_MAGIC)
298 return 0;
299
300 err = __get_user(ext.size, (unsigned int *)(buf
301 + offsetof(struct extcontext, size)));
302 if (err)
303 return err;
304
305 switch (ext.magic) {
306 case MSA_EXTCONTEXT_MAGIC:
307 err = restore_msa_extcontext(buf, ext.size);
308 break;
309
310 default:
311 err = -EINVAL;
312 break;
313 }
314
315 if (err)
316 return err;
317
318 buf += ext.size;
319 }
320}
321
322/*
323 * Helper routines
324 */
325int protected_save_fp_context(void __user *sc)
326{
327 struct mips_abi *abi = current->thread.abi;
328 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
329 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
330 uint32_t __user *used_math = sc + abi->off_sc_used_math;
331 unsigned int used, ext_sz;
332 int err;
333
334 used = used_math() ? USED_FP : 0;
335 if (!used)
336 goto fp_done;
337
338 if (!test_thread_flag(TIF_32BIT_FPREGS))
339 used |= USED_FR1;
340 if (test_thread_flag(TIF_HYBRID_FPREGS))
341 used |= USED_HYBRID_FPRS;
342
343 /*
344 * EVA does not have userland equivalents of ldc1 or sdc1, so
345 * save to the kernel FP context & copy that to userland below.
346 */
347 if (IS_ENABLED(CONFIG_EVA))
348 lose_fpu(1);
349
350 while (1) {
351 lock_fpu_owner();
352 if (is_fpu_owner()) {
353 err = save_fp_context(sc);
354 unlock_fpu_owner();
355 } else {
356 unlock_fpu_owner();
357 err = copy_fp_to_sigcontext(sc);
358 }
359 if (likely(!err))
360 break;
361 /* touch the sigcontext and try again */
362 err = __put_user(0, &fpregs[0]) |
363 __put_user(0, &fpregs[31]) |
364 __put_user(0, csr);
365 if (err)
366 return err; /* really bad sigcontext */
367 }
368
369fp_done:
370 ext_sz = err = save_extcontext(sc_to_extcontext(sc));
371 if (err < 0)
372 return err;
373 used |= ext_sz ? USED_EXTCONTEXT : 0;
374
375 return __put_user(used, used_math);
376}
377
378int protected_restore_fp_context(void __user *sc)
379{
380 struct mips_abi *abi = current->thread.abi;
381 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
382 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
383 uint32_t __user *used_math = sc + abi->off_sc_used_math;
384 unsigned int used;
385 int err, sig = 0, tmp __maybe_unused;
386
387 err = __get_user(used, used_math);
388 conditional_used_math(used & USED_FP);
389
390 /*
391 * The signal handler may have used FPU; give it up if the program
392 * doesn't want it following sigreturn.
393 */
394 if (err || !(used & USED_FP))
395 lose_fpu(0);
396 if (err)
397 return err;
398 if (!(used & USED_FP))
399 goto fp_done;
400
401 err = sig = fpcsr_pending(csr);
402 if (err < 0)
403 return err;
404
405 /*
406 * EVA does not have userland equivalents of ldc1 or sdc1, so we
407 * disable the FPU here such that the code below simply copies to
408 * the kernel FP context.
409 */
410 if (IS_ENABLED(CONFIG_EVA))
411 lose_fpu(0);
412
413 while (1) {
414 lock_fpu_owner();
415 if (is_fpu_owner()) {
416 err = restore_fp_context(sc);
417 unlock_fpu_owner();
418 } else {
419 unlock_fpu_owner();
420 err = copy_fp_from_sigcontext(sc);
421 }
422 if (likely(!err))
423 break;
424 /* touch the sigcontext and try again */
425 err = __get_user(tmp, &fpregs[0]) |
426 __get_user(tmp, &fpregs[31]) |
427 __get_user(tmp, csr);
428 if (err)
429 break; /* really bad sigcontext */
430 }
431
432fp_done:
433 if (!err && (used & USED_EXTCONTEXT))
434 err = restore_extcontext(sc_to_extcontext(sc));
435
436 return err ?: sig;
437}
438
439int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
440{
441 int err = 0;
442 int i;
443
444 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
445
446 err |= __put_user(0, &sc->sc_regs[0]);
447 for (i = 1; i < 32; i++)
448 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
449
450#ifdef CONFIG_CPU_HAS_SMARTMIPS
451 err |= __put_user(regs->acx, &sc->sc_acx);
452#endif
453 err |= __put_user(regs->hi, &sc->sc_mdhi);
454 err |= __put_user(regs->lo, &sc->sc_mdlo);
455 if (cpu_has_dsp) {
456 err |= __put_user(mfhi1(), &sc->sc_hi1);
457 err |= __put_user(mflo1(), &sc->sc_lo1);
458 err |= __put_user(mfhi2(), &sc->sc_hi2);
459 err |= __put_user(mflo2(), &sc->sc_lo2);
460 err |= __put_user(mfhi3(), &sc->sc_hi3);
461 err |= __put_user(mflo3(), &sc->sc_lo3);
462 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
463 }
464
465
466 /*
467 * Save FPU state to signal context. Signal handler
468 * will "inherit" current FPU state.
469 */
470 err |= protected_save_fp_context(sc);
471
472 return err;
473}
474
475static size_t extcontext_max_size(void)
476{
477 size_t sz = 0;
478
479 /*
480 * The assumption here is that between this point & the point at which
481 * the extended context is saved the size of the context should only
482 * ever be able to shrink (if the task is preempted), but never grow.
483 * That is, what this function returns is an upper bound on the size of
484 * the extended context for the current task at the current time.
485 */
486
487 if (thread_msa_context_live())
488 sz += sizeof(struct msa_extcontext);
489
490 /* If any context is saved then we'll append the end marker */
491 if (sz)
492 sz += sizeof(((struct extcontext *)NULL)->magic);
493
494 return sz;
495}
496
497int fpcsr_pending(unsigned int __user *fpcsr)
498{
499 int err, sig = 0;
500 unsigned int csr, enabled;
501
502 err = __get_user(csr, fpcsr);
503 enabled = FPU_CSR_UNI_X | ((csr & FPU_CSR_ALL_E) << 5);
504 /*
505 * If the signal handler set some FPU exceptions, clear it and
506 * send SIGFPE.
507 */
508 if (csr & enabled) {
509 csr &= ~enabled;
510 err |= __put_user(csr, fpcsr);
511 sig = SIGFPE;
512 }
513 return err ?: sig;
514}
515
516int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
517{
518 unsigned long treg;
519 int err = 0;
520 int i;
521
522 /* Always make any pending restarted system calls return -EINTR */
523 current->restart_block.fn = do_no_restart_syscall;
524
525 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
526
527#ifdef CONFIG_CPU_HAS_SMARTMIPS
528 err |= __get_user(regs->acx, &sc->sc_acx);
529#endif
530 err |= __get_user(regs->hi, &sc->sc_mdhi);
531 err |= __get_user(regs->lo, &sc->sc_mdlo);
532 if (cpu_has_dsp) {
533 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
534 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
535 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
536 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
537 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
538 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
539 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
540 }
541
542 for (i = 1; i < 32; i++)
543 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
544
545 return err ?: protected_restore_fp_context(sc);
546}
547
548void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
549 size_t frame_size)
550{
551 unsigned long sp;
552
553 /* Leave space for potential extended context */
554 frame_size += extcontext_max_size();
555
556 /* Default to using normal stack */
557 sp = regs->regs[29];
558
559 /*
560 * FPU emulator may have it's own trampoline active just
561 * above the user stack, 16-bytes before the next lowest
562 * 16 byte boundary. Try to avoid trashing it.
563 */
564 sp -= 32;
565
566 sp = sigsp(sp, ksig);
567
568 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
569}
570
571/*
572 * Atomically swap in the new signal mask, and wait for a signal.
573 */
574
575#ifdef CONFIG_TRAD_SIGNALS
576SYSCALL_DEFINE1(sigsuspend, sigset_t __user *, uset)
577{
578 return sys_rt_sigsuspend(uset, sizeof(sigset_t));
579}
580#endif
581
582#ifdef CONFIG_TRAD_SIGNALS
583SYSCALL_DEFINE3(sigaction, int, sig, const struct sigaction __user *, act,
584 struct sigaction __user *, oact)
585{
586 struct k_sigaction new_ka, old_ka;
587 int ret;
588 int err = 0;
589
590 if (act) {
591 old_sigset_t mask;
592
593 if (!access_ok(act, sizeof(*act)))
594 return -EFAULT;
595 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
596 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
597 err |= __get_user(mask, &act->sa_mask.sig[0]);
598 if (err)
599 return -EFAULT;
600
601 siginitset(&new_ka.sa.sa_mask, mask);
602 }
603
604 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
605
606 if (!ret && oact) {
607 if (!access_ok(oact, sizeof(*oact)))
608 return -EFAULT;
609 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
610 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
611 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
612 err |= __put_user(0, &oact->sa_mask.sig[1]);
613 err |= __put_user(0, &oact->sa_mask.sig[2]);
614 err |= __put_user(0, &oact->sa_mask.sig[3]);
615 if (err)
616 return -EFAULT;
617 }
618
619 return ret;
620}
621#endif
622
623#ifdef CONFIG_TRAD_SIGNALS
624asmlinkage void sys_sigreturn(void)
625{
626 struct sigframe __user *frame;
627 struct pt_regs *regs;
628 sigset_t blocked;
629 int sig;
630
631 regs = current_pt_regs();
632 frame = (struct sigframe __user *)regs->regs[29];
633 if (!access_ok(frame, sizeof(*frame)))
634 goto badframe;
635 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
636 goto badframe;
637
638 set_current_blocked(&blocked);
639
640 sig = restore_sigcontext(regs, &frame->sf_sc);
641 if (sig < 0)
642 goto badframe;
643 else if (sig)
644 force_sig(sig);
645
646 /*
647 * Don't let your children do this ...
648 */
649 __asm__ __volatile__(
650 "move\t$29, %0\n\t"
651 "j\tsyscall_exit"
652 : /* no outputs */
653 : "r" (regs));
654 /* Unreached */
655
656badframe:
657 force_sig(SIGSEGV);
658}
659#endif /* CONFIG_TRAD_SIGNALS */
660
661asmlinkage void sys_rt_sigreturn(void)
662{
663 struct rt_sigframe __user *frame;
664 struct pt_regs *regs;
665 sigset_t set;
666 int sig;
667
668 regs = current_pt_regs();
669 frame = (struct rt_sigframe __user *)regs->regs[29];
670 if (!access_ok(frame, sizeof(*frame)))
671 goto badframe;
672 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
673 goto badframe;
674
675 set_current_blocked(&set);
676
677 sig = restore_sigcontext(regs, &frame->rs_uc.uc_mcontext);
678 if (sig < 0)
679 goto badframe;
680 else if (sig)
681 force_sig(sig);
682
683 if (restore_altstack(&frame->rs_uc.uc_stack))
684 goto badframe;
685
686 /*
687 * Don't let your children do this ...
688 */
689 __asm__ __volatile__(
690 "move\t$29, %0\n\t"
691 "j\tsyscall_exit"
692 : /* no outputs */
693 : "r" (regs));
694 /* Unreached */
695
696badframe:
697 force_sig(SIGSEGV);
698}
699
700#ifdef CONFIG_TRAD_SIGNALS
701static int setup_frame(void *sig_return, struct ksignal *ksig,
702 struct pt_regs *regs, sigset_t *set)
703{
704 struct sigframe __user *frame;
705 int err = 0;
706
707 frame = get_sigframe(ksig, regs, sizeof(*frame));
708 if (!access_ok(frame, sizeof (*frame)))
709 return -EFAULT;
710
711 err |= setup_sigcontext(regs, &frame->sf_sc);
712 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
713 if (err)
714 return -EFAULT;
715
716 /*
717 * Arguments to signal handler:
718 *
719 * a0 = signal number
720 * a1 = 0 (should be cause)
721 * a2 = pointer to struct sigcontext
722 *
723 * $25 and c0_epc point to the signal handler, $29 points to the
724 * struct sigframe.
725 */
726 regs->regs[ 4] = ksig->sig;
727 regs->regs[ 5] = 0;
728 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
729 regs->regs[29] = (unsigned long) frame;
730 regs->regs[31] = (unsigned long) sig_return;
731 regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
732
733 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
734 current->comm, current->pid,
735 frame, regs->cp0_epc, regs->regs[31]);
736 return 0;
737}
738#endif
739
740static int setup_rt_frame(void *sig_return, struct ksignal *ksig,
741 struct pt_regs *regs, sigset_t *set)
742{
743 struct rt_sigframe __user *frame;
744 int err = 0;
745
746 frame = get_sigframe(ksig, regs, sizeof(*frame));
747 if (!access_ok(frame, sizeof (*frame)))
748 return -EFAULT;
749
750 /* Create siginfo. */
751 err |= copy_siginfo_to_user(&frame->rs_info, &ksig->info);
752
753 /* Create the ucontext. */
754 err |= __put_user(0, &frame->rs_uc.uc_flags);
755 err |= __put_user(NULL, &frame->rs_uc.uc_link);
756 err |= __save_altstack(&frame->rs_uc.uc_stack, regs->regs[29]);
757 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
758 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
759
760 if (err)
761 return -EFAULT;
762
763 /*
764 * Arguments to signal handler:
765 *
766 * a0 = signal number
767 * a1 = 0 (should be cause)
768 * a2 = pointer to ucontext
769 *
770 * $25 and c0_epc point to the signal handler, $29 points to
771 * the struct rt_sigframe.
772 */
773 regs->regs[ 4] = ksig->sig;
774 regs->regs[ 5] = (unsigned long) &frame->rs_info;
775 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
776 regs->regs[29] = (unsigned long) frame;
777 regs->regs[31] = (unsigned long) sig_return;
778 regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
779
780 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
781 current->comm, current->pid,
782 frame, regs->cp0_epc, regs->regs[31]);
783
784 return 0;
785}
786
787struct mips_abi mips_abi = {
788#ifdef CONFIG_TRAD_SIGNALS
789 .setup_frame = setup_frame,
790#endif
791 .setup_rt_frame = setup_rt_frame,
792 .restart = __NR_restart_syscall,
793
794 .off_sc_fpregs = offsetof(struct sigcontext, sc_fpregs),
795 .off_sc_fpc_csr = offsetof(struct sigcontext, sc_fpc_csr),
796 .off_sc_used_math = offsetof(struct sigcontext, sc_used_math),
797
798 .vdso = &vdso_image,
799};
800
801static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
802{
803 sigset_t *oldset = sigmask_to_save();
804 int ret;
805 struct mips_abi *abi = current->thread.abi;
806 void *vdso = current->mm->context.vdso;
807
808 /*
809 * If we were emulating a delay slot instruction, exit that frame such
810 * that addresses in the sigframe are as expected for userland and we
811 * don't have a problem if we reuse the thread's frame for an
812 * instruction within the signal handler.
813 */
814 dsemul_thread_rollback(regs);
815
816 if (regs->regs[0]) {
817 switch(regs->regs[2]) {
818 case ERESTART_RESTARTBLOCK:
819 case ERESTARTNOHAND:
820 regs->regs[2] = EINTR;
821 break;
822 case ERESTARTSYS:
823 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
824 regs->regs[2] = EINTR;
825 break;
826 }
827 /* fallthrough */
828 case ERESTARTNOINTR:
829 regs->regs[7] = regs->regs[26];
830 regs->regs[2] = regs->regs[0];
831 regs->cp0_epc -= 4;
832 }
833
834 regs->regs[0] = 0; /* Don't deal with this again. */
835 }
836
837 rseq_signal_deliver(ksig, regs);
838
839 if (sig_uses_siginfo(&ksig->ka, abi))
840 ret = abi->setup_rt_frame(vdso + abi->vdso->off_rt_sigreturn,
841 ksig, regs, oldset);
842 else
843 ret = abi->setup_frame(vdso + abi->vdso->off_sigreturn,
844 ksig, regs, oldset);
845
846 signal_setup_done(ret, ksig, 0);
847}
848
849static void do_signal(struct pt_regs *regs)
850{
851 struct ksignal ksig;
852
853 if (get_signal(&ksig)) {
854 /* Whee! Actually deliver the signal. */
855 handle_signal(&ksig, regs);
856 return;
857 }
858
859 if (regs->regs[0]) {
860 switch (regs->regs[2]) {
861 case ERESTARTNOHAND:
862 case ERESTARTSYS:
863 case ERESTARTNOINTR:
864 regs->regs[2] = regs->regs[0];
865 regs->regs[7] = regs->regs[26];
866 regs->cp0_epc -= 4;
867 break;
868
869 case ERESTART_RESTARTBLOCK:
870 regs->regs[2] = current->thread.abi->restart;
871 regs->regs[7] = regs->regs[26];
872 regs->cp0_epc -= 4;
873 break;
874 }
875 regs->regs[0] = 0; /* Don't deal with this again. */
876 }
877
878 /*
879 * If there's no signal to deliver, we just put the saved sigmask
880 * back
881 */
882 restore_saved_sigmask();
883}
884
885/*
886 * notification of userspace execution resumption
887 * - triggered by the TIF_WORK_MASK flags
888 */
889asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
890 __u32 thread_info_flags)
891{
892 local_irq_enable();
893
894 user_exit();
895
896 if (thread_info_flags & _TIF_UPROBE)
897 uprobe_notify_resume(regs);
898
899 /* deal with pending signal delivery */
900 if (thread_info_flags & _TIF_SIGPENDING)
901 do_signal(regs);
902
903 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
904 clear_thread_flag(TIF_NOTIFY_RESUME);
905 tracehook_notify_resume(regs);
906 rseq_handle_notify_resume(NULL, regs);
907 }
908
909 user_enter();
910}
911
912#if defined(CONFIG_SMP) && defined(CONFIG_MIPS_FP_SUPPORT)
913static int smp_save_fp_context(void __user *sc)
914{
915 return raw_cpu_has_fpu
916 ? save_hw_fp_context(sc)
917 : copy_fp_to_sigcontext(sc);
918}
919
920static int smp_restore_fp_context(void __user *sc)
921{
922 return raw_cpu_has_fpu
923 ? restore_hw_fp_context(sc)
924 : copy_fp_from_sigcontext(sc);
925}
926#endif
927
928static int signal_setup(void)
929{
930 /*
931 * The offset from sigcontext to extended context should be the same
932 * regardless of the type of signal, such that userland can always know
933 * where to look if it wishes to find the extended context structures.
934 */
935 BUILD_BUG_ON((offsetof(struct sigframe, sf_extcontext) -
936 offsetof(struct sigframe, sf_sc)) !=
937 (offsetof(struct rt_sigframe, rs_uc.uc_extcontext) -
938 offsetof(struct rt_sigframe, rs_uc.uc_mcontext)));
939
940#if defined(CONFIG_SMP) && defined(CONFIG_MIPS_FP_SUPPORT)
941 /* For now just do the cpu_has_fpu check when the functions are invoked */
942 save_fp_context = smp_save_fp_context;
943 restore_fp_context = smp_restore_fp_context;
944#else
945 if (cpu_has_fpu) {
946 save_fp_context = save_hw_fp_context;
947 restore_fp_context = restore_hw_fp_context;
948 } else {
949 save_fp_context = copy_fp_to_sigcontext;
950 restore_fp_context = copy_fp_from_sigcontext;
951 }
952#endif /* CONFIG_SMP */
953
954 return 0;
955}
956
957arch_initcall(signal_setup);
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.
5 *
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.
10 */
11#include <linux/cache.h>
12#include <linux/context_tracking.h>
13#include <linux/irqflags.h>
14#include <linux/sched.h>
15#include <linux/mm.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/uprobes.h>
25#include <linux/compiler.h>
26#include <linux/syscalls.h>
27#include <linux/uaccess.h>
28#include <linux/tracehook.h>
29
30#include <asm/abi.h>
31#include <asm/asm.h>
32#include <linux/bitops.h>
33#include <asm/cacheflush.h>
34#include <asm/fpu.h>
35#include <asm/sim.h>
36#include <asm/ucontext.h>
37#include <asm/cpu-features.h>
38#include <asm/war.h>
39#include <asm/dsp.h>
40#include <asm/inst.h>
41#include <asm/msa.h>
42
43#include "signal-common.h"
44
45static int (*save_fp_context)(void __user *sc);
46static int (*restore_fp_context)(void __user *sc);
47
48struct sigframe {
49 u32 sf_ass[4]; /* argument save space for o32 */
50 u32 sf_pad[2]; /* Was: signal trampoline */
51
52 /* Matches struct ucontext from its uc_mcontext field onwards */
53 struct sigcontext sf_sc;
54 sigset_t sf_mask;
55 unsigned long long sf_extcontext[0];
56};
57
58struct rt_sigframe {
59 u32 rs_ass[4]; /* argument save space for o32 */
60 u32 rs_pad[2]; /* Was: signal trampoline */
61 struct siginfo rs_info;
62 struct ucontext rs_uc;
63};
64
65/*
66 * Thread saved context copy to/from a signal context presumed to be on the
67 * user stack, and therefore accessed with appropriate macros from uaccess.h.
68 */
69static int copy_fp_to_sigcontext(void __user *sc)
70{
71 struct mips_abi *abi = current->thread.abi;
72 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
73 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
74 int i;
75 int err = 0;
76 int inc = test_thread_flag(TIF_32BIT_FPREGS) ? 2 : 1;
77
78 for (i = 0; i < NUM_FPU_REGS; i += inc) {
79 err |=
80 __put_user(get_fpr64(¤t->thread.fpu.fpr[i], 0),
81 &fpregs[i]);
82 }
83 err |= __put_user(current->thread.fpu.fcr31, csr);
84
85 return err;
86}
87
88static int copy_fp_from_sigcontext(void __user *sc)
89{
90 struct mips_abi *abi = current->thread.abi;
91 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
92 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
93 int i;
94 int err = 0;
95 int inc = test_thread_flag(TIF_32BIT_FPREGS) ? 2 : 1;
96 u64 fpr_val;
97
98 for (i = 0; i < NUM_FPU_REGS; i += inc) {
99 err |= __get_user(fpr_val, &fpregs[i]);
100 set_fpr64(¤t->thread.fpu.fpr[i], 0, fpr_val);
101 }
102 err |= __get_user(current->thread.fpu.fcr31, csr);
103
104 return err;
105}
106
107/*
108 * Wrappers for the assembly _{save,restore}_fp_context functions.
109 */
110static int save_hw_fp_context(void __user *sc)
111{
112 struct mips_abi *abi = current->thread.abi;
113 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
114 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
115
116 return _save_fp_context(fpregs, csr);
117}
118
119static int restore_hw_fp_context(void __user *sc)
120{
121 struct mips_abi *abi = current->thread.abi;
122 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
123 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
124
125 return _restore_fp_context(fpregs, csr);
126}
127
128/*
129 * Extended context handling.
130 */
131
132static inline void __user *sc_to_extcontext(void __user *sc)
133{
134 struct ucontext __user *uc;
135
136 /*
137 * We can just pretend the sigcontext is always embedded in a struct
138 * ucontext here, because the offset from sigcontext to extended
139 * context is the same in the struct sigframe case.
140 */
141 uc = container_of(sc, struct ucontext, uc_mcontext);
142 return &uc->uc_extcontext;
143}
144
145static int save_msa_extcontext(void __user *buf)
146{
147 struct msa_extcontext __user *msa = buf;
148 uint64_t val;
149 int i, err;
150
151 if (!thread_msa_context_live())
152 return 0;
153
154 /*
155 * Ensure that we can't lose the live MSA context between checking
156 * for it & writing it to memory.
157 */
158 preempt_disable();
159
160 if (is_msa_enabled()) {
161 /*
162 * There are no EVA versions of the vector register load/store
163 * instructions, so MSA context has to be saved to kernel memory
164 * and then copied to user memory. The save to kernel memory
165 * should already have been done when handling scalar FP
166 * context.
167 */
168 BUG_ON(IS_ENABLED(CONFIG_EVA));
169
170 err = __put_user(read_msa_csr(), &msa->csr);
171 err |= _save_msa_all_upper(&msa->wr);
172
173 preempt_enable();
174 } else {
175 preempt_enable();
176
177 err = __put_user(current->thread.fpu.msacsr, &msa->csr);
178
179 for (i = 0; i < NUM_FPU_REGS; i++) {
180 val = get_fpr64(¤t->thread.fpu.fpr[i], 1);
181 err |= __put_user(val, &msa->wr[i]);
182 }
183 }
184
185 err |= __put_user(MSA_EXTCONTEXT_MAGIC, &msa->ext.magic);
186 err |= __put_user(sizeof(*msa), &msa->ext.size);
187
188 return err ? -EFAULT : sizeof(*msa);
189}
190
191static int restore_msa_extcontext(void __user *buf, unsigned int size)
192{
193 struct msa_extcontext __user *msa = buf;
194 unsigned long long val;
195 unsigned int csr;
196 int i, err;
197
198 if (!IS_ENABLED(CONFIG_CPU_HAS_MSA))
199 return SIGSYS;
200
201 if (size != sizeof(*msa))
202 return -EINVAL;
203
204 err = get_user(csr, &msa->csr);
205 if (err)
206 return err;
207
208 preempt_disable();
209
210 if (is_msa_enabled()) {
211 /*
212 * There are no EVA versions of the vector register load/store
213 * instructions, so MSA context has to be copied to kernel
214 * memory and later loaded to registers. The same is true of
215 * scalar FP context, so FPU & MSA should have already been
216 * disabled whilst handling scalar FP context.
217 */
218 BUG_ON(IS_ENABLED(CONFIG_EVA));
219
220 write_msa_csr(csr);
221 err |= _restore_msa_all_upper(&msa->wr);
222 preempt_enable();
223 } else {
224 preempt_enable();
225
226 current->thread.fpu.msacsr = csr;
227
228 for (i = 0; i < NUM_FPU_REGS; i++) {
229 err |= __get_user(val, &msa->wr[i]);
230 set_fpr64(¤t->thread.fpu.fpr[i], 1, val);
231 }
232 }
233
234 return err;
235}
236
237static int save_extcontext(void __user *buf)
238{
239 int sz;
240
241 sz = save_msa_extcontext(buf);
242 if (sz < 0)
243 return sz;
244 buf += sz;
245
246 /* If no context was saved then trivially return */
247 if (!sz)
248 return 0;
249
250 /* Write the end marker */
251 if (__put_user(END_EXTCONTEXT_MAGIC, (u32 *)buf))
252 return -EFAULT;
253
254 sz += sizeof(((struct extcontext *)NULL)->magic);
255 return sz;
256}
257
258static int restore_extcontext(void __user *buf)
259{
260 struct extcontext ext;
261 int err;
262
263 while (1) {
264 err = __get_user(ext.magic, (unsigned int *)buf);
265 if (err)
266 return err;
267
268 if (ext.magic == END_EXTCONTEXT_MAGIC)
269 return 0;
270
271 err = __get_user(ext.size, (unsigned int *)(buf
272 + offsetof(struct extcontext, size)));
273 if (err)
274 return err;
275
276 switch (ext.magic) {
277 case MSA_EXTCONTEXT_MAGIC:
278 err = restore_msa_extcontext(buf, ext.size);
279 break;
280
281 default:
282 err = -EINVAL;
283 break;
284 }
285
286 if (err)
287 return err;
288
289 buf += ext.size;
290 }
291}
292
293/*
294 * Helper routines
295 */
296int protected_save_fp_context(void __user *sc)
297{
298 struct mips_abi *abi = current->thread.abi;
299 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
300 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
301 uint32_t __user *used_math = sc + abi->off_sc_used_math;
302 unsigned int used, ext_sz;
303 int err;
304
305 used = used_math() ? USED_FP : 0;
306 if (!used)
307 goto fp_done;
308
309 if (!test_thread_flag(TIF_32BIT_FPREGS))
310 used |= USED_FR1;
311 if (test_thread_flag(TIF_HYBRID_FPREGS))
312 used |= USED_HYBRID_FPRS;
313
314 /*
315 * EVA does not have userland equivalents of ldc1 or sdc1, so
316 * save to the kernel FP context & copy that to userland below.
317 */
318 if (IS_ENABLED(CONFIG_EVA))
319 lose_fpu(1);
320
321 while (1) {
322 lock_fpu_owner();
323 if (is_fpu_owner()) {
324 err = save_fp_context(sc);
325 unlock_fpu_owner();
326 } else {
327 unlock_fpu_owner();
328 err = copy_fp_to_sigcontext(sc);
329 }
330 if (likely(!err))
331 break;
332 /* touch the sigcontext and try again */
333 err = __put_user(0, &fpregs[0]) |
334 __put_user(0, &fpregs[31]) |
335 __put_user(0, csr);
336 if (err)
337 return err; /* really bad sigcontext */
338 }
339
340fp_done:
341 ext_sz = err = save_extcontext(sc_to_extcontext(sc));
342 if (err < 0)
343 return err;
344 used |= ext_sz ? USED_EXTCONTEXT : 0;
345
346 return __put_user(used, used_math);
347}
348
349int protected_restore_fp_context(void __user *sc)
350{
351 struct mips_abi *abi = current->thread.abi;
352 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
353 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
354 uint32_t __user *used_math = sc + abi->off_sc_used_math;
355 unsigned int used;
356 int err, sig = 0, tmp __maybe_unused;
357
358 err = __get_user(used, used_math);
359 conditional_used_math(used & USED_FP);
360
361 /*
362 * The signal handler may have used FPU; give it up if the program
363 * doesn't want it following sigreturn.
364 */
365 if (err || !(used & USED_FP))
366 lose_fpu(0);
367 if (err)
368 return err;
369 if (!(used & USED_FP))
370 goto fp_done;
371
372 err = sig = fpcsr_pending(csr);
373 if (err < 0)
374 return err;
375
376 /*
377 * EVA does not have userland equivalents of ldc1 or sdc1, so we
378 * disable the FPU here such that the code below simply copies to
379 * the kernel FP context.
380 */
381 if (IS_ENABLED(CONFIG_EVA))
382 lose_fpu(0);
383
384 while (1) {
385 lock_fpu_owner();
386 if (is_fpu_owner()) {
387 err = restore_fp_context(sc);
388 unlock_fpu_owner();
389 } else {
390 unlock_fpu_owner();
391 err = copy_fp_from_sigcontext(sc);
392 }
393 if (likely(!err))
394 break;
395 /* touch the sigcontext and try again */
396 err = __get_user(tmp, &fpregs[0]) |
397 __get_user(tmp, &fpregs[31]) |
398 __get_user(tmp, csr);
399 if (err)
400 break; /* really bad sigcontext */
401 }
402
403fp_done:
404 if (!err && (used & USED_EXTCONTEXT))
405 err = restore_extcontext(sc_to_extcontext(sc));
406
407 return err ?: sig;
408}
409
410int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
411{
412 int err = 0;
413 int i;
414
415 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
416
417 err |= __put_user(0, &sc->sc_regs[0]);
418 for (i = 1; i < 32; i++)
419 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
420
421#ifdef CONFIG_CPU_HAS_SMARTMIPS
422 err |= __put_user(regs->acx, &sc->sc_acx);
423#endif
424 err |= __put_user(regs->hi, &sc->sc_mdhi);
425 err |= __put_user(regs->lo, &sc->sc_mdlo);
426 if (cpu_has_dsp) {
427 err |= __put_user(mfhi1(), &sc->sc_hi1);
428 err |= __put_user(mflo1(), &sc->sc_lo1);
429 err |= __put_user(mfhi2(), &sc->sc_hi2);
430 err |= __put_user(mflo2(), &sc->sc_lo2);
431 err |= __put_user(mfhi3(), &sc->sc_hi3);
432 err |= __put_user(mflo3(), &sc->sc_lo3);
433 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
434 }
435
436
437 /*
438 * Save FPU state to signal context. Signal handler
439 * will "inherit" current FPU state.
440 */
441 err |= protected_save_fp_context(sc);
442
443 return err;
444}
445
446static size_t extcontext_max_size(void)
447{
448 size_t sz = 0;
449
450 /*
451 * The assumption here is that between this point & the point at which
452 * the extended context is saved the size of the context should only
453 * ever be able to shrink (if the task is preempted), but never grow.
454 * That is, what this function returns is an upper bound on the size of
455 * the extended context for the current task at the current time.
456 */
457
458 if (thread_msa_context_live())
459 sz += sizeof(struct msa_extcontext);
460
461 /* If any context is saved then we'll append the end marker */
462 if (sz)
463 sz += sizeof(((struct extcontext *)NULL)->magic);
464
465 return sz;
466}
467
468int fpcsr_pending(unsigned int __user *fpcsr)
469{
470 int err, sig = 0;
471 unsigned int csr, enabled;
472
473 err = __get_user(csr, fpcsr);
474 enabled = FPU_CSR_UNI_X | ((csr & FPU_CSR_ALL_E) << 5);
475 /*
476 * If the signal handler set some FPU exceptions, clear it and
477 * send SIGFPE.
478 */
479 if (csr & enabled) {
480 csr &= ~enabled;
481 err |= __put_user(csr, fpcsr);
482 sig = SIGFPE;
483 }
484 return err ?: sig;
485}
486
487int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
488{
489 unsigned long treg;
490 int err = 0;
491 int i;
492
493 /* Always make any pending restarted system calls return -EINTR */
494 current->restart_block.fn = do_no_restart_syscall;
495
496 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
497
498#ifdef CONFIG_CPU_HAS_SMARTMIPS
499 err |= __get_user(regs->acx, &sc->sc_acx);
500#endif
501 err |= __get_user(regs->hi, &sc->sc_mdhi);
502 err |= __get_user(regs->lo, &sc->sc_mdlo);
503 if (cpu_has_dsp) {
504 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
505 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
506 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
507 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
508 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
509 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
510 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
511 }
512
513 for (i = 1; i < 32; i++)
514 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
515
516 return err ?: protected_restore_fp_context(sc);
517}
518
519void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
520 size_t frame_size)
521{
522 unsigned long sp;
523
524 /* Leave space for potential extended context */
525 frame_size += extcontext_max_size();
526
527 /* Default to using normal stack */
528 sp = regs->regs[29];
529
530 /*
531 * FPU emulator may have it's own trampoline active just
532 * above the user stack, 16-bytes before the next lowest
533 * 16 byte boundary. Try to avoid trashing it.
534 */
535 sp -= 32;
536
537 sp = sigsp(sp, ksig);
538
539 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
540}
541
542/*
543 * Atomically swap in the new signal mask, and wait for a signal.
544 */
545
546#ifdef CONFIG_TRAD_SIGNALS
547SYSCALL_DEFINE1(sigsuspend, sigset_t __user *, uset)
548{
549 return sys_rt_sigsuspend(uset, sizeof(sigset_t));
550}
551#endif
552
553#ifdef CONFIG_TRAD_SIGNALS
554SYSCALL_DEFINE3(sigaction, int, sig, const struct sigaction __user *, act,
555 struct sigaction __user *, oact)
556{
557 struct k_sigaction new_ka, old_ka;
558 int ret;
559 int err = 0;
560
561 if (act) {
562 old_sigset_t mask;
563
564 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
565 return -EFAULT;
566 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
567 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
568 err |= __get_user(mask, &act->sa_mask.sig[0]);
569 if (err)
570 return -EFAULT;
571
572 siginitset(&new_ka.sa.sa_mask, mask);
573 }
574
575 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
576
577 if (!ret && oact) {
578 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
579 return -EFAULT;
580 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
581 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
582 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
583 err |= __put_user(0, &oact->sa_mask.sig[1]);
584 err |= __put_user(0, &oact->sa_mask.sig[2]);
585 err |= __put_user(0, &oact->sa_mask.sig[3]);
586 if (err)
587 return -EFAULT;
588 }
589
590 return ret;
591}
592#endif
593
594#ifdef CONFIG_TRAD_SIGNALS
595asmlinkage void sys_sigreturn(nabi_no_regargs struct pt_regs regs)
596{
597 struct sigframe __user *frame;
598 sigset_t blocked;
599 int sig;
600
601 frame = (struct sigframe __user *) regs.regs[29];
602 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
603 goto badframe;
604 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
605 goto badframe;
606
607 set_current_blocked(&blocked);
608
609 sig = restore_sigcontext(®s, &frame->sf_sc);
610 if (sig < 0)
611 goto badframe;
612 else if (sig)
613 force_sig(sig, current);
614
615 /*
616 * Don't let your children do this ...
617 */
618 __asm__ __volatile__(
619 "move\t$29, %0\n\t"
620 "j\tsyscall_exit"
621 :/* no outputs */
622 :"r" (®s));
623 /* Unreached */
624
625badframe:
626 force_sig(SIGSEGV, current);
627}
628#endif /* CONFIG_TRAD_SIGNALS */
629
630asmlinkage void sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
631{
632 struct rt_sigframe __user *frame;
633 sigset_t set;
634 int sig;
635
636 frame = (struct rt_sigframe __user *) regs.regs[29];
637 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
638 goto badframe;
639 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
640 goto badframe;
641
642 set_current_blocked(&set);
643
644 sig = restore_sigcontext(®s, &frame->rs_uc.uc_mcontext);
645 if (sig < 0)
646 goto badframe;
647 else if (sig)
648 force_sig(sig, current);
649
650 if (restore_altstack(&frame->rs_uc.uc_stack))
651 goto badframe;
652
653 /*
654 * Don't let your children do this ...
655 */
656 __asm__ __volatile__(
657 "move\t$29, %0\n\t"
658 "j\tsyscall_exit"
659 :/* no outputs */
660 :"r" (®s));
661 /* Unreached */
662
663badframe:
664 force_sig(SIGSEGV, current);
665}
666
667#ifdef CONFIG_TRAD_SIGNALS
668static int setup_frame(void *sig_return, struct ksignal *ksig,
669 struct pt_regs *regs, sigset_t *set)
670{
671 struct sigframe __user *frame;
672 int err = 0;
673
674 frame = get_sigframe(ksig, regs, sizeof(*frame));
675 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
676 return -EFAULT;
677
678 err |= setup_sigcontext(regs, &frame->sf_sc);
679 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
680 if (err)
681 return -EFAULT;
682
683 /*
684 * Arguments to signal handler:
685 *
686 * a0 = signal number
687 * a1 = 0 (should be cause)
688 * a2 = pointer to struct sigcontext
689 *
690 * $25 and c0_epc point to the signal handler, $29 points to the
691 * struct sigframe.
692 */
693 regs->regs[ 4] = ksig->sig;
694 regs->regs[ 5] = 0;
695 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
696 regs->regs[29] = (unsigned long) frame;
697 regs->regs[31] = (unsigned long) sig_return;
698 regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
699
700 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
701 current->comm, current->pid,
702 frame, regs->cp0_epc, regs->regs[31]);
703 return 0;
704}
705#endif
706
707static int setup_rt_frame(void *sig_return, struct ksignal *ksig,
708 struct pt_regs *regs, sigset_t *set)
709{
710 struct rt_sigframe __user *frame;
711 int err = 0;
712
713 frame = get_sigframe(ksig, regs, sizeof(*frame));
714 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
715 return -EFAULT;
716
717 /* Create siginfo. */
718 err |= copy_siginfo_to_user(&frame->rs_info, &ksig->info);
719
720 /* Create the ucontext. */
721 err |= __put_user(0, &frame->rs_uc.uc_flags);
722 err |= __put_user(NULL, &frame->rs_uc.uc_link);
723 err |= __save_altstack(&frame->rs_uc.uc_stack, regs->regs[29]);
724 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
725 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
726
727 if (err)
728 return -EFAULT;
729
730 /*
731 * Arguments to signal handler:
732 *
733 * a0 = signal number
734 * a1 = 0 (should be cause)
735 * a2 = pointer to ucontext
736 *
737 * $25 and c0_epc point to the signal handler, $29 points to
738 * the struct rt_sigframe.
739 */
740 regs->regs[ 4] = ksig->sig;
741 regs->regs[ 5] = (unsigned long) &frame->rs_info;
742 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
743 regs->regs[29] = (unsigned long) frame;
744 regs->regs[31] = (unsigned long) sig_return;
745 regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
746
747 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
748 current->comm, current->pid,
749 frame, regs->cp0_epc, regs->regs[31]);
750
751 return 0;
752}
753
754struct mips_abi mips_abi = {
755#ifdef CONFIG_TRAD_SIGNALS
756 .setup_frame = setup_frame,
757#endif
758 .setup_rt_frame = setup_rt_frame,
759 .restart = __NR_restart_syscall,
760
761 .off_sc_fpregs = offsetof(struct sigcontext, sc_fpregs),
762 .off_sc_fpc_csr = offsetof(struct sigcontext, sc_fpc_csr),
763 .off_sc_used_math = offsetof(struct sigcontext, sc_used_math),
764
765 .vdso = &vdso_image,
766};
767
768static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
769{
770 sigset_t *oldset = sigmask_to_save();
771 int ret;
772 struct mips_abi *abi = current->thread.abi;
773 void *vdso = current->mm->context.vdso;
774
775 /*
776 * If we were emulating a delay slot instruction, exit that frame such
777 * that addresses in the sigframe are as expected for userland and we
778 * don't have a problem if we reuse the thread's frame for an
779 * instruction within the signal handler.
780 */
781 dsemul_thread_rollback(regs);
782
783 if (regs->regs[0]) {
784 switch(regs->regs[2]) {
785 case ERESTART_RESTARTBLOCK:
786 case ERESTARTNOHAND:
787 regs->regs[2] = EINTR;
788 break;
789 case ERESTARTSYS:
790 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
791 regs->regs[2] = EINTR;
792 break;
793 }
794 /* fallthrough */
795 case ERESTARTNOINTR:
796 regs->regs[7] = regs->regs[26];
797 regs->regs[2] = regs->regs[0];
798 regs->cp0_epc -= 4;
799 }
800
801 regs->regs[0] = 0; /* Don't deal with this again. */
802 }
803
804 if (sig_uses_siginfo(&ksig->ka, abi))
805 ret = abi->setup_rt_frame(vdso + abi->vdso->off_rt_sigreturn,
806 ksig, regs, oldset);
807 else
808 ret = abi->setup_frame(vdso + abi->vdso->off_sigreturn,
809 ksig, regs, oldset);
810
811 signal_setup_done(ret, ksig, 0);
812}
813
814static void do_signal(struct pt_regs *regs)
815{
816 struct ksignal ksig;
817
818 if (get_signal(&ksig)) {
819 /* Whee! Actually deliver the signal. */
820 handle_signal(&ksig, regs);
821 return;
822 }
823
824 if (regs->regs[0]) {
825 switch (regs->regs[2]) {
826 case ERESTARTNOHAND:
827 case ERESTARTSYS:
828 case ERESTARTNOINTR:
829 regs->regs[2] = regs->regs[0];
830 regs->regs[7] = regs->regs[26];
831 regs->cp0_epc -= 4;
832 break;
833
834 case ERESTART_RESTARTBLOCK:
835 regs->regs[2] = current->thread.abi->restart;
836 regs->regs[7] = regs->regs[26];
837 regs->cp0_epc -= 4;
838 break;
839 }
840 regs->regs[0] = 0; /* Don't deal with this again. */
841 }
842
843 /*
844 * If there's no signal to deliver, we just put the saved sigmask
845 * back
846 */
847 restore_saved_sigmask();
848}
849
850/*
851 * notification of userspace execution resumption
852 * - triggered by the TIF_WORK_MASK flags
853 */
854asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
855 __u32 thread_info_flags)
856{
857 local_irq_enable();
858
859 user_exit();
860
861 if (thread_info_flags & _TIF_UPROBE)
862 uprobe_notify_resume(regs);
863
864 /* deal with pending signal delivery */
865 if (thread_info_flags & _TIF_SIGPENDING)
866 do_signal(regs);
867
868 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
869 clear_thread_flag(TIF_NOTIFY_RESUME);
870 tracehook_notify_resume(regs);
871 }
872
873 user_enter();
874}
875
876#ifdef CONFIG_SMP
877static int smp_save_fp_context(void __user *sc)
878{
879 return raw_cpu_has_fpu
880 ? save_hw_fp_context(sc)
881 : copy_fp_to_sigcontext(sc);
882}
883
884static int smp_restore_fp_context(void __user *sc)
885{
886 return raw_cpu_has_fpu
887 ? restore_hw_fp_context(sc)
888 : copy_fp_from_sigcontext(sc);
889}
890#endif
891
892static int signal_setup(void)
893{
894 /*
895 * The offset from sigcontext to extended context should be the same
896 * regardless of the type of signal, such that userland can always know
897 * where to look if it wishes to find the extended context structures.
898 */
899 BUILD_BUG_ON((offsetof(struct sigframe, sf_extcontext) -
900 offsetof(struct sigframe, sf_sc)) !=
901 (offsetof(struct rt_sigframe, rs_uc.uc_extcontext) -
902 offsetof(struct rt_sigframe, rs_uc.uc_mcontext)));
903
904#ifdef CONFIG_SMP
905 /* For now just do the cpu_has_fpu check when the functions are invoked */
906 save_fp_context = smp_save_fp_context;
907 restore_fp_context = smp_restore_fp_context;
908#else
909 if (cpu_has_fpu) {
910 save_fp_context = save_hw_fp_context;
911 restore_fp_context = restore_hw_fp_context;
912 } else {
913 save_fp_context = copy_fp_to_sigcontext;
914 restore_fp_context = copy_fp_from_sigcontext;
915 }
916#endif /* CONFIG_SMP */
917
918 return 0;
919}
920
921arch_initcall(signal_setup);