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