xref: /openbmc/linux/arch/mips/kernel/signal.c (revision afc98d90)
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  */
10 #include <linux/cache.h>
11 #include <linux/context_tracking.h>
12 #include <linux/irqflags.h>
13 #include <linux/sched.h>
14 #include <linux/mm.h>
15 #include <linux/personality.h>
16 #include <linux/smp.h>
17 #include <linux/kernel.h>
18 #include <linux/signal.h>
19 #include <linux/errno.h>
20 #include <linux/wait.h>
21 #include <linux/ptrace.h>
22 #include <linux/unistd.h>
23 #include <linux/compiler.h>
24 #include <linux/syscalls.h>
25 #include <linux/uaccess.h>
26 #include <linux/tracehook.h>
27 
28 #include <asm/abi.h>
29 #include <asm/asm.h>
30 #include <linux/bitops.h>
31 #include <asm/cacheflush.h>
32 #include <asm/fpu.h>
33 #include <asm/sim.h>
34 #include <asm/ucontext.h>
35 #include <asm/cpu-features.h>
36 #include <asm/war.h>
37 #include <asm/vdso.h>
38 #include <asm/dsp.h>
39 #include <asm/inst.h>
40 
41 #include "signal-common.h"
42 
43 static int (*save_fp_context)(struct sigcontext __user *sc);
44 static int (*restore_fp_context)(struct sigcontext __user *sc);
45 
46 extern asmlinkage int _save_fp_context(struct sigcontext __user *sc);
47 extern asmlinkage int _restore_fp_context(struct sigcontext __user *sc);
48 
49 extern asmlinkage int fpu_emulator_save_context(struct sigcontext __user *sc);
50 extern asmlinkage int fpu_emulator_restore_context(struct sigcontext __user *sc);
51 
52 struct sigframe {
53 	u32 sf_ass[4];		/* argument save space for o32 */
54 	u32 sf_pad[2];		/* Was: signal trampoline */
55 	struct sigcontext sf_sc;
56 	sigset_t sf_mask;
57 };
58 
59 struct rt_sigframe {
60 	u32 rs_ass[4];		/* argument save space for o32 */
61 	u32 rs_pad[2];		/* Was: signal trampoline */
62 	struct siginfo rs_info;
63 	struct ucontext rs_uc;
64 };
65 
66 /*
67  * Helper routines
68  */
69 static int protected_save_fp_context(struct sigcontext __user *sc)
70 {
71 	int err;
72 	while (1) {
73 		lock_fpu_owner();
74 		err = own_fpu_inatomic(1);
75 		if (!err)
76 			err = save_fp_context(sc); /* this might fail */
77 		unlock_fpu_owner();
78 		if (likely(!err))
79 			break;
80 		/* touch the sigcontext and try again */
81 		err = __put_user(0, &sc->sc_fpregs[0]) |
82 			__put_user(0, &sc->sc_fpregs[31]) |
83 			__put_user(0, &sc->sc_fpc_csr);
84 		if (err)
85 			break;	/* really bad sigcontext */
86 	}
87 	return err;
88 }
89 
90 static int protected_restore_fp_context(struct sigcontext __user *sc)
91 {
92 	int err, tmp __maybe_unused;
93 	while (1) {
94 		lock_fpu_owner();
95 		err = own_fpu_inatomic(0);
96 		if (!err)
97 			err = restore_fp_context(sc); /* this might fail */
98 		unlock_fpu_owner();
99 		if (likely(!err))
100 			break;
101 		/* touch the sigcontext and try again */
102 		err = __get_user(tmp, &sc->sc_fpregs[0]) |
103 			__get_user(tmp, &sc->sc_fpregs[31]) |
104 			__get_user(tmp, &sc->sc_fpc_csr);
105 		if (err)
106 			break;	/* really bad sigcontext */
107 	}
108 	return err;
109 }
110 
111 int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
112 {
113 	int err = 0;
114 	int i;
115 	unsigned int used_math;
116 
117 	err |= __put_user(regs->cp0_epc, &sc->sc_pc);
118 
119 	err |= __put_user(0, &sc->sc_regs[0]);
120 	for (i = 1; i < 32; i++)
121 		err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
122 
123 #ifdef CONFIG_CPU_HAS_SMARTMIPS
124 	err |= __put_user(regs->acx, &sc->sc_acx);
125 #endif
126 	err |= __put_user(regs->hi, &sc->sc_mdhi);
127 	err |= __put_user(regs->lo, &sc->sc_mdlo);
128 	if (cpu_has_dsp) {
129 		err |= __put_user(mfhi1(), &sc->sc_hi1);
130 		err |= __put_user(mflo1(), &sc->sc_lo1);
131 		err |= __put_user(mfhi2(), &sc->sc_hi2);
132 		err |= __put_user(mflo2(), &sc->sc_lo2);
133 		err |= __put_user(mfhi3(), &sc->sc_hi3);
134 		err |= __put_user(mflo3(), &sc->sc_lo3);
135 		err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
136 	}
137 
138 	used_math = !!used_math();
139 	err |= __put_user(used_math, &sc->sc_used_math);
140 
141 	if (used_math) {
142 		/*
143 		 * Save FPU state to signal context. Signal handler
144 		 * will "inherit" current FPU state.
145 		 */
146 		err |= protected_save_fp_context(sc);
147 	}
148 	return err;
149 }
150 
151 int fpcsr_pending(unsigned int __user *fpcsr)
152 {
153 	int err, sig = 0;
154 	unsigned int csr, enabled;
155 
156 	err = __get_user(csr, fpcsr);
157 	enabled = FPU_CSR_UNI_X | ((csr & FPU_CSR_ALL_E) << 5);
158 	/*
159 	 * If the signal handler set some FPU exceptions, clear it and
160 	 * send SIGFPE.
161 	 */
162 	if (csr & enabled) {
163 		csr &= ~enabled;
164 		err |= __put_user(csr, fpcsr);
165 		sig = SIGFPE;
166 	}
167 	return err ?: sig;
168 }
169 
170 static int
171 check_and_restore_fp_context(struct sigcontext __user *sc)
172 {
173 	int err, sig;
174 
175 	err = sig = fpcsr_pending(&sc->sc_fpc_csr);
176 	if (err > 0)
177 		err = 0;
178 	err |= protected_restore_fp_context(sc);
179 	return err ?: sig;
180 }
181 
182 int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
183 {
184 	unsigned int used_math;
185 	unsigned long treg;
186 	int err = 0;
187 	int i;
188 
189 	/* Always make any pending restarted system calls return -EINTR */
190 	current_thread_info()->restart_block.fn = do_no_restart_syscall;
191 
192 	err |= __get_user(regs->cp0_epc, &sc->sc_pc);
193 
194 #ifdef CONFIG_CPU_HAS_SMARTMIPS
195 	err |= __get_user(regs->acx, &sc->sc_acx);
196 #endif
197 	err |= __get_user(regs->hi, &sc->sc_mdhi);
198 	err |= __get_user(regs->lo, &sc->sc_mdlo);
199 	if (cpu_has_dsp) {
200 		err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
201 		err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
202 		err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
203 		err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
204 		err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
205 		err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
206 		err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
207 	}
208 
209 	for (i = 1; i < 32; i++)
210 		err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
211 
212 	err |= __get_user(used_math, &sc->sc_used_math);
213 	conditional_used_math(used_math);
214 
215 	if (used_math) {
216 		/* restore fpu context if we have used it before */
217 		if (!err)
218 			err = check_and_restore_fp_context(sc);
219 	} else {
220 		/* signal handler may have used FPU.  Give it up. */
221 		lose_fpu(0);
222 	}
223 
224 	return err;
225 }
226 
227 void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
228 			  size_t frame_size)
229 {
230 	unsigned long sp;
231 
232 	/* Default to using normal stack */
233 	sp = regs->regs[29];
234 
235 	/*
236 	 * FPU emulator may have it's own trampoline active just
237 	 * above the user stack, 16-bytes before the next lowest
238 	 * 16 byte boundary.  Try to avoid trashing it.
239 	 */
240 	sp -= 32;
241 
242 	/* This is the X/Open sanctioned signal stack switching.  */
243 	if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
244 		sp = current->sas_ss_sp + current->sas_ss_size;
245 
246 	return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
247 }
248 
249 /*
250  * Atomically swap in the new signal mask, and wait for a signal.
251  */
252 
253 #ifdef CONFIG_TRAD_SIGNALS
254 SYSCALL_DEFINE1(sigsuspend, sigset_t __user *, uset)
255 {
256 	return sys_rt_sigsuspend(uset, sizeof(sigset_t));
257 }
258 #endif
259 
260 #ifdef CONFIG_TRAD_SIGNALS
261 SYSCALL_DEFINE3(sigaction, int, sig, const struct sigaction __user *, act,
262 	struct sigaction __user *, oact)
263 {
264 	struct k_sigaction new_ka, old_ka;
265 	int ret;
266 	int err = 0;
267 
268 	if (act) {
269 		old_sigset_t mask;
270 
271 		if (!access_ok(VERIFY_READ, act, sizeof(*act)))
272 			return -EFAULT;
273 		err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
274 		err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
275 		err |= __get_user(mask, &act->sa_mask.sig[0]);
276 		if (err)
277 			return -EFAULT;
278 
279 		siginitset(&new_ka.sa.sa_mask, mask);
280 	}
281 
282 	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
283 
284 	if (!ret && oact) {
285 		if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
286 			return -EFAULT;
287 		err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
288 		err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
289 		err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
290 		err |= __put_user(0, &oact->sa_mask.sig[1]);
291 		err |= __put_user(0, &oact->sa_mask.sig[2]);
292 		err |= __put_user(0, &oact->sa_mask.sig[3]);
293 		if (err)
294 			return -EFAULT;
295 	}
296 
297 	return ret;
298 }
299 #endif
300 
301 #ifdef CONFIG_TRAD_SIGNALS
302 asmlinkage void sys_sigreturn(nabi_no_regargs struct pt_regs regs)
303 {
304 	struct sigframe __user *frame;
305 	sigset_t blocked;
306 	int sig;
307 
308 	frame = (struct sigframe __user *) regs.regs[29];
309 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
310 		goto badframe;
311 	if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
312 		goto badframe;
313 
314 	set_current_blocked(&blocked);
315 
316 	sig = restore_sigcontext(&regs, &frame->sf_sc);
317 	if (sig < 0)
318 		goto badframe;
319 	else if (sig)
320 		force_sig(sig, current);
321 
322 	/*
323 	 * Don't let your children do this ...
324 	 */
325 	__asm__ __volatile__(
326 		"move\t$29, %0\n\t"
327 		"j\tsyscall_exit"
328 		:/* no outputs */
329 		:"r" (&regs));
330 	/* Unreached */
331 
332 badframe:
333 	force_sig(SIGSEGV, current);
334 }
335 #endif /* CONFIG_TRAD_SIGNALS */
336 
337 asmlinkage void sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
338 {
339 	struct rt_sigframe __user *frame;
340 	sigset_t set;
341 	int sig;
342 
343 	frame = (struct rt_sigframe __user *) regs.regs[29];
344 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
345 		goto badframe;
346 	if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
347 		goto badframe;
348 
349 	set_current_blocked(&set);
350 
351 	sig = restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext);
352 	if (sig < 0)
353 		goto badframe;
354 	else if (sig)
355 		force_sig(sig, current);
356 
357 	if (restore_altstack(&frame->rs_uc.uc_stack))
358 		goto badframe;
359 
360 	/*
361 	 * Don't let your children do this ...
362 	 */
363 	__asm__ __volatile__(
364 		"move\t$29, %0\n\t"
365 		"j\tsyscall_exit"
366 		:/* no outputs */
367 		:"r" (&regs));
368 	/* Unreached */
369 
370 badframe:
371 	force_sig(SIGSEGV, current);
372 }
373 
374 #ifdef CONFIG_TRAD_SIGNALS
375 static int setup_frame(void *sig_return, struct k_sigaction *ka,
376 		       struct pt_regs *regs, int signr, sigset_t *set)
377 {
378 	struct sigframe __user *frame;
379 	int err = 0;
380 
381 	frame = get_sigframe(ka, regs, sizeof(*frame));
382 	if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
383 		goto give_sigsegv;
384 
385 	err |= setup_sigcontext(regs, &frame->sf_sc);
386 	err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
387 	if (err)
388 		goto give_sigsegv;
389 
390 	/*
391 	 * Arguments to signal handler:
392 	 *
393 	 *   a0 = signal number
394 	 *   a1 = 0 (should be cause)
395 	 *   a2 = pointer to struct sigcontext
396 	 *
397 	 * $25 and c0_epc point to the signal handler, $29 points to the
398 	 * struct sigframe.
399 	 */
400 	regs->regs[ 4] = signr;
401 	regs->regs[ 5] = 0;
402 	regs->regs[ 6] = (unsigned long) &frame->sf_sc;
403 	regs->regs[29] = (unsigned long) frame;
404 	regs->regs[31] = (unsigned long) sig_return;
405 	regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
406 
407 	DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
408 	       current->comm, current->pid,
409 	       frame, regs->cp0_epc, regs->regs[31]);
410 	return 0;
411 
412 give_sigsegv:
413 	force_sigsegv(signr, current);
414 	return -EFAULT;
415 }
416 #endif
417 
418 static int setup_rt_frame(void *sig_return, struct k_sigaction *ka,
419 			  struct pt_regs *regs, int signr, sigset_t *set,
420 			  siginfo_t *info)
421 {
422 	struct rt_sigframe __user *frame;
423 	int err = 0;
424 
425 	frame = get_sigframe(ka, regs, sizeof(*frame));
426 	if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
427 		goto give_sigsegv;
428 
429 	/* Create siginfo.  */
430 	err |= copy_siginfo_to_user(&frame->rs_info, info);
431 
432 	/* Create the ucontext.	 */
433 	err |= __put_user(0, &frame->rs_uc.uc_flags);
434 	err |= __put_user(NULL, &frame->rs_uc.uc_link);
435 	err |= __save_altstack(&frame->rs_uc.uc_stack, regs->regs[29]);
436 	err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
437 	err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
438 
439 	if (err)
440 		goto give_sigsegv;
441 
442 	/*
443 	 * Arguments to signal handler:
444 	 *
445 	 *   a0 = signal number
446 	 *   a1 = 0 (should be cause)
447 	 *   a2 = pointer to ucontext
448 	 *
449 	 * $25 and c0_epc point to the signal handler, $29 points to
450 	 * the struct rt_sigframe.
451 	 */
452 	regs->regs[ 4] = signr;
453 	regs->regs[ 5] = (unsigned long) &frame->rs_info;
454 	regs->regs[ 6] = (unsigned long) &frame->rs_uc;
455 	regs->regs[29] = (unsigned long) frame;
456 	regs->regs[31] = (unsigned long) sig_return;
457 	regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
458 
459 	DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
460 	       current->comm, current->pid,
461 	       frame, regs->cp0_epc, regs->regs[31]);
462 
463 	return 0;
464 
465 give_sigsegv:
466 	force_sigsegv(signr, current);
467 	return -EFAULT;
468 }
469 
470 struct mips_abi mips_abi = {
471 #ifdef CONFIG_TRAD_SIGNALS
472 	.setup_frame	= setup_frame,
473 	.signal_return_offset = offsetof(struct mips_vdso, signal_trampoline),
474 #endif
475 	.setup_rt_frame = setup_rt_frame,
476 	.rt_signal_return_offset =
477 		offsetof(struct mips_vdso, rt_signal_trampoline),
478 	.restart	= __NR_restart_syscall
479 };
480 
481 static void handle_signal(unsigned long sig, siginfo_t *info,
482 	struct k_sigaction *ka, struct pt_regs *regs)
483 {
484 	sigset_t *oldset = sigmask_to_save();
485 	int ret;
486 	struct mips_abi *abi = current->thread.abi;
487 #ifdef CONFIG_CPU_MICROMIPS
488 	void *vdso;
489 	unsigned int tmp = (unsigned int)current->mm->context.vdso;
490 
491 	set_isa16_mode(tmp);
492 	vdso = (void *)tmp;
493 #else
494 	void *vdso = current->mm->context.vdso;
495 #endif
496 
497 	if (regs->regs[0]) {
498 		switch(regs->regs[2]) {
499 		case ERESTART_RESTARTBLOCK:
500 		case ERESTARTNOHAND:
501 			regs->regs[2] = EINTR;
502 			break;
503 		case ERESTARTSYS:
504 			if (!(ka->sa.sa_flags & SA_RESTART)) {
505 				regs->regs[2] = EINTR;
506 				break;
507 			}
508 		/* fallthrough */
509 		case ERESTARTNOINTR:
510 			regs->regs[7] = regs->regs[26];
511 			regs->regs[2] = regs->regs[0];
512 			regs->cp0_epc -= 4;
513 		}
514 
515 		regs->regs[0] = 0;		/* Don't deal with this again.	*/
516 	}
517 
518 	if (sig_uses_siginfo(ka))
519 		ret = abi->setup_rt_frame(vdso + abi->rt_signal_return_offset,
520 					  ka, regs, sig, oldset, info);
521 	else
522 		ret = abi->setup_frame(vdso + abi->signal_return_offset,
523 				       ka, regs, sig, oldset);
524 
525 	if (ret)
526 		return;
527 
528 	signal_delivered(sig, info, ka, regs, 0);
529 }
530 
531 static void do_signal(struct pt_regs *regs)
532 {
533 	struct k_sigaction ka;
534 	siginfo_t info;
535 	int signr;
536 
537 	signr = get_signal_to_deliver(&info, &ka, regs, NULL);
538 	if (signr > 0) {
539 		/* Whee!  Actually deliver the signal.	*/
540 		handle_signal(signr, &info, &ka, regs);
541 		return;
542 	}
543 
544 	if (regs->regs[0]) {
545 		switch (regs->regs[2]) {
546 		case ERESTARTNOHAND:
547 		case ERESTARTSYS:
548 		case ERESTARTNOINTR:
549 			regs->regs[2] = regs->regs[0];
550 			regs->regs[7] = regs->regs[26];
551 			regs->cp0_epc -= 4;
552 			break;
553 
554 		case ERESTART_RESTARTBLOCK:
555 			regs->regs[2] = current->thread.abi->restart;
556 			regs->regs[7] = regs->regs[26];
557 			regs->cp0_epc -= 4;
558 			break;
559 		}
560 		regs->regs[0] = 0;	/* Don't deal with this again.	*/
561 	}
562 
563 	/*
564 	 * If there's no signal to deliver, we just put the saved sigmask
565 	 * back
566 	 */
567 	restore_saved_sigmask();
568 }
569 
570 /*
571  * notification of userspace execution resumption
572  * - triggered by the TIF_WORK_MASK flags
573  */
574 asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
575 	__u32 thread_info_flags)
576 {
577 	local_irq_enable();
578 
579 	user_exit();
580 
581 	/* deal with pending signal delivery */
582 	if (thread_info_flags & _TIF_SIGPENDING)
583 		do_signal(regs);
584 
585 	if (thread_info_flags & _TIF_NOTIFY_RESUME) {
586 		clear_thread_flag(TIF_NOTIFY_RESUME);
587 		tracehook_notify_resume(regs);
588 	}
589 
590 	user_enter();
591 }
592 
593 #ifdef CONFIG_SMP
594 static int smp_save_fp_context(struct sigcontext __user *sc)
595 {
596 	return raw_cpu_has_fpu
597 	       ? _save_fp_context(sc)
598 	       : fpu_emulator_save_context(sc);
599 }
600 
601 static int smp_restore_fp_context(struct sigcontext __user *sc)
602 {
603 	return raw_cpu_has_fpu
604 	       ? _restore_fp_context(sc)
605 	       : fpu_emulator_restore_context(sc);
606 }
607 #endif
608 
609 static int signal_setup(void)
610 {
611 #ifdef CONFIG_SMP
612 	/* For now just do the cpu_has_fpu check when the functions are invoked */
613 	save_fp_context = smp_save_fp_context;
614 	restore_fp_context = smp_restore_fp_context;
615 #else
616 	if (cpu_has_fpu) {
617 		save_fp_context = _save_fp_context;
618 		restore_fp_context = _restore_fp_context;
619 	} else {
620 		save_fp_context = fpu_emulator_save_context;
621 		restore_fp_context = fpu_emulator_restore_context;
622 	}
623 #endif
624 
625 	return 0;
626 }
627 
628 arch_initcall(signal_setup);
629