xref: /linux/arch/mips/kernel/signal.c (revision d8327c784b51b57dac2c26cfad87dce0d68dfd98)
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/config.h>
11 #include <linux/cache.h>
12 #include <linux/sched.h>
13 #include <linux/mm.h>
14 #include <linux/personality.h>
15 #include <linux/smp.h>
16 #include <linux/smp_lock.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 
25 #include <asm/abi.h>
26 #include <asm/asm.h>
27 #include <linux/bitops.h>
28 #include <asm/cacheflush.h>
29 #include <asm/fpu.h>
30 #include <asm/sim.h>
31 #include <asm/uaccess.h>
32 #include <asm/ucontext.h>
33 #include <asm/cpu-features.h>
34 #include <asm/war.h>
35 
36 #include "signal-common.h"
37 
38 #define DEBUG_SIG 0
39 
40 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
41 
42 /*
43  * Atomically swap in the new signal mask, and wait for a signal.
44  */
45 
46 #ifdef CONFIG_TRAD_SIGNALS
47 save_static_function(sys_sigsuspend);
48 __attribute_used__ noinline static int
49 _sys_sigsuspend(nabi_no_regargs struct pt_regs regs)
50 {
51 	sigset_t newset;
52 	sigset_t __user *uset;
53 
54 	uset = (sigset_t __user *) regs.regs[4];
55 	if (copy_from_user(&newset, uset, sizeof(sigset_t)))
56 		return -EFAULT;
57 	sigdelsetmask(&newset, ~_BLOCKABLE);
58 
59 	spin_lock_irq(&current->sighand->siglock);
60 	current->saved_sigmask = current->blocked;
61 	current->blocked = newset;
62 	recalc_sigpending();
63 	spin_unlock_irq(&current->sighand->siglock);
64 
65 	current->state = TASK_INTERRUPTIBLE;
66 	schedule();
67 	set_thread_flag(TIF_RESTORE_SIGMASK);
68 	return -ERESTARTNOHAND;
69 }
70 #endif
71 
72 save_static_function(sys_rt_sigsuspend);
73 __attribute_used__ noinline static int
74 _sys_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
75 {
76 	sigset_t newset;
77 	sigset_t __user *unewset;
78 	size_t sigsetsize;
79 
80 	/* XXX Don't preclude handling different sized sigset_t's.  */
81 	sigsetsize = regs.regs[5];
82 	if (sigsetsize != sizeof(sigset_t))
83 		return -EINVAL;
84 
85 	unewset = (sigset_t __user *) regs.regs[4];
86 	if (copy_from_user(&newset, unewset, sizeof(newset)))
87 		return -EFAULT;
88 	sigdelsetmask(&newset, ~_BLOCKABLE);
89 
90 	spin_lock_irq(&current->sighand->siglock);
91 	current->saved_sigmask = current->blocked;
92 	current->blocked = newset;
93         recalc_sigpending();
94 	spin_unlock_irq(&current->sighand->siglock);
95 
96 	current->state = TASK_INTERRUPTIBLE;
97 	schedule();
98 	set_thread_flag(TIF_RESTORE_SIGMASK);
99 	return -ERESTARTNOHAND;
100 }
101 
102 #ifdef CONFIG_TRAD_SIGNALS
103 asmlinkage int sys_sigaction(int sig, const struct sigaction *act,
104 	struct sigaction *oact)
105 {
106 	struct k_sigaction new_ka, old_ka;
107 	int ret;
108 	int err = 0;
109 
110 	if (act) {
111 		old_sigset_t mask;
112 
113 		if (!access_ok(VERIFY_READ, act, sizeof(*act)))
114 			return -EFAULT;
115 		err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
116 		err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
117 		err |= __get_user(mask, &act->sa_mask.sig[0]);
118 		if (err)
119 			return -EFAULT;
120 
121 		siginitset(&new_ka.sa.sa_mask, mask);
122 	}
123 
124 	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
125 
126 	if (!ret && oact) {
127 		if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
128                         return -EFAULT;
129 		err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
130 		err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
131 		err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
132 		err |= __put_user(0, &oact->sa_mask.sig[1]);
133 		err |= __put_user(0, &oact->sa_mask.sig[2]);
134 		err |= __put_user(0, &oact->sa_mask.sig[3]);
135 		if (err)
136 			return -EFAULT;
137 	}
138 
139 	return ret;
140 }
141 #endif
142 
143 asmlinkage int sys_sigaltstack(nabi_no_regargs struct pt_regs regs)
144 {
145 	const stack_t __user *uss = (const stack_t __user *) regs.regs[4];
146 	stack_t __user *uoss = (stack_t __user *) regs.regs[5];
147 	unsigned long usp = regs.regs[29];
148 
149 	return do_sigaltstack(uss, uoss, usp);
150 }
151 
152 /*
153  * Horribly complicated - with the bloody RM9000 workarounds enabled
154  * the signal trampolines is moving to the end of the structure so we can
155  * increase the alignment without breaking software compatibility.
156  */
157 #ifdef CONFIG_TRAD_SIGNALS
158 struct sigframe {
159 	u32 sf_ass[4];			/* argument save space for o32 */
160 #if ICACHE_REFILLS_WORKAROUND_WAR
161 	u32 sf_pad[2];
162 #else
163 	u32 sf_code[2];			/* signal trampoline */
164 #endif
165 	struct sigcontext sf_sc;
166 	sigset_t sf_mask;
167 #if ICACHE_REFILLS_WORKAROUND_WAR
168 	u32 sf_code[8] ____cacheline_aligned;	/* signal trampoline */
169 #endif
170 };
171 #endif
172 
173 struct rt_sigframe {
174 	u32 rs_ass[4];			/* argument save space for o32 */
175 #if ICACHE_REFILLS_WORKAROUND_WAR
176 	u32 rs_pad[2];
177 #else
178 	u32 rs_code[2];			/* signal trampoline */
179 #endif
180 	struct siginfo rs_info;
181 	struct ucontext rs_uc;
182 #if ICACHE_REFILLS_WORKAROUND_WAR
183 	u32 rs_code[8] ____cacheline_aligned;	/* signal trampoline */
184 #endif
185 };
186 
187 #ifdef CONFIG_TRAD_SIGNALS
188 save_static_function(sys_sigreturn);
189 __attribute_used__ noinline static void
190 _sys_sigreturn(nabi_no_regargs struct pt_regs regs)
191 {
192 	struct sigframe __user *frame;
193 	sigset_t blocked;
194 
195 	frame = (struct sigframe __user *) regs.regs[29];
196 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
197 		goto badframe;
198 	if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
199 		goto badframe;
200 
201 	sigdelsetmask(&blocked, ~_BLOCKABLE);
202 	spin_lock_irq(&current->sighand->siglock);
203 	current->blocked = blocked;
204 	recalc_sigpending();
205 	spin_unlock_irq(&current->sighand->siglock);
206 
207 	if (restore_sigcontext(&regs, &frame->sf_sc))
208 		goto badframe;
209 
210 	/*
211 	 * Don't let your children do this ...
212 	 */
213 	__asm__ __volatile__(
214 		"move\t$29, %0\n\t"
215 		"j\tsyscall_exit"
216 		:/* no outputs */
217 		:"r" (&regs));
218 	/* Unreached */
219 
220 badframe:
221 	force_sig(SIGSEGV, current);
222 }
223 #endif /* CONFIG_TRAD_SIGNALS */
224 
225 save_static_function(sys_rt_sigreturn);
226 __attribute_used__ noinline static void
227 _sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
228 {
229 	struct rt_sigframe __user *frame;
230 	sigset_t set;
231 	stack_t st;
232 
233 	frame = (struct rt_sigframe __user *) regs.regs[29];
234 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
235 		goto badframe;
236 	if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
237 		goto badframe;
238 
239 	sigdelsetmask(&set, ~_BLOCKABLE);
240 	spin_lock_irq(&current->sighand->siglock);
241 	current->blocked = set;
242 	recalc_sigpending();
243 	spin_unlock_irq(&current->sighand->siglock);
244 
245 	if (restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext))
246 		goto badframe;
247 
248 	if (__copy_from_user(&st, &frame->rs_uc.uc_stack, sizeof(st)))
249 		goto badframe;
250 	/* It is more difficult to avoid calling this function than to
251 	   call it and ignore errors.  */
252 	do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]);
253 
254 	/*
255 	 * Don't let your children do this ...
256 	 */
257 	__asm__ __volatile__(
258 		"move\t$29, %0\n\t"
259 		"j\tsyscall_exit"
260 		:/* no outputs */
261 		:"r" (&regs));
262 	/* Unreached */
263 
264 badframe:
265 	force_sig(SIGSEGV, current);
266 }
267 
268 #ifdef CONFIG_TRAD_SIGNALS
269 int setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
270 	int signr, sigset_t *set)
271 {
272 	struct sigframe __user *frame;
273 	int err = 0;
274 
275 	frame = get_sigframe(ka, regs, sizeof(*frame));
276 	if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
277 		goto give_sigsegv;
278 
279 	install_sigtramp(frame->sf_code, __NR_sigreturn);
280 
281 	err |= setup_sigcontext(regs, &frame->sf_sc);
282 	err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
283 	if (err)
284 		goto give_sigsegv;
285 
286 	/*
287 	 * Arguments to signal handler:
288 	 *
289 	 *   a0 = signal number
290 	 *   a1 = 0 (should be cause)
291 	 *   a2 = pointer to struct sigcontext
292 	 *
293 	 * $25 and c0_epc point to the signal handler, $29 points to the
294 	 * struct sigframe.
295 	 */
296 	regs->regs[ 4] = signr;
297 	regs->regs[ 5] = 0;
298 	regs->regs[ 6] = (unsigned long) &frame->sf_sc;
299 	regs->regs[29] = (unsigned long) frame;
300 	regs->regs[31] = (unsigned long) frame->sf_code;
301 	regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
302 
303 #if DEBUG_SIG
304 	printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n",
305 	       current->comm, current->pid,
306 	       frame, regs->cp0_epc, frame->regs[31]);
307 #endif
308         return 0;
309 
310 give_sigsegv:
311 	force_sigsegv(signr, current);
312 	return -EFAULT;
313 }
314 #endif
315 
316 int setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
317 	int signr, sigset_t *set, siginfo_t *info)
318 {
319 	struct rt_sigframe __user *frame;
320 	int err = 0;
321 
322 	frame = get_sigframe(ka, regs, sizeof(*frame));
323 	if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
324 		goto give_sigsegv;
325 
326 	install_sigtramp(frame->rs_code, __NR_rt_sigreturn);
327 
328 	/* Create siginfo.  */
329 	err |= copy_siginfo_to_user(&frame->rs_info, info);
330 
331 	/* Create the ucontext.  */
332 	err |= __put_user(0, &frame->rs_uc.uc_flags);
333 	err |= __put_user(NULL, &frame->rs_uc.uc_link);
334 	err |= __put_user((void *)current->sas_ss_sp,
335 	                  &frame->rs_uc.uc_stack.ss_sp);
336 	err |= __put_user(sas_ss_flags(regs->regs[29]),
337 	                  &frame->rs_uc.uc_stack.ss_flags);
338 	err |= __put_user(current->sas_ss_size,
339 	                  &frame->rs_uc.uc_stack.ss_size);
340 	err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
341 	err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
342 
343 	if (err)
344 		goto give_sigsegv;
345 
346 	/*
347 	 * Arguments to signal handler:
348 	 *
349 	 *   a0 = signal number
350 	 *   a1 = 0 (should be cause)
351 	 *   a2 = pointer to ucontext
352 	 *
353 	 * $25 and c0_epc point to the signal handler, $29 points to
354 	 * the struct rt_sigframe.
355 	 */
356 	regs->regs[ 4] = signr;
357 	regs->regs[ 5] = (unsigned long) &frame->rs_info;
358 	regs->regs[ 6] = (unsigned long) &frame->rs_uc;
359 	regs->regs[29] = (unsigned long) frame;
360 	regs->regs[31] = (unsigned long) frame->rs_code;
361 	regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
362 
363 #if DEBUG_SIG
364 	printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n",
365 	       current->comm, current->pid,
366 	       frame, regs->cp0_epc, regs->regs[31]);
367 #endif
368 	return 0;
369 
370 give_sigsegv:
371 	force_sigsegv(signr, current);
372 	return -EFAULT;
373 }
374 
375 static inline int handle_signal(unsigned long sig, siginfo_t *info,
376 	struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
377 {
378 	int ret;
379 
380 	switch(regs->regs[0]) {
381 	case ERESTART_RESTARTBLOCK:
382 	case ERESTARTNOHAND:
383 		regs->regs[2] = EINTR;
384 		break;
385 	case ERESTARTSYS:
386 		if (!(ka->sa.sa_flags & SA_RESTART)) {
387 			regs->regs[2] = EINTR;
388 			break;
389 		}
390 	/* fallthrough */
391 	case ERESTARTNOINTR:		/* Userland will reload $v0.  */
392 		regs->regs[7] = regs->regs[26];
393 		regs->cp0_epc -= 8;
394 	}
395 
396 	regs->regs[0] = 0;		/* Don't deal with this again.  */
397 
398 	if (sig_uses_siginfo(ka))
399 		ret = current->thread.abi->setup_rt_frame(ka, regs, sig, oldset, info);
400 	else
401 		ret = current->thread.abi->setup_frame(ka, regs, sig, oldset);
402 
403 	spin_lock_irq(&current->sighand->siglock);
404 	sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
405 	if (!(ka->sa.sa_flags & SA_NODEFER))
406 		sigaddset(&current->blocked,sig);
407 	recalc_sigpending();
408 	spin_unlock_irq(&current->sighand->siglock);
409 
410 	return ret;
411 }
412 
413 void do_signal(struct pt_regs *regs)
414 {
415 	struct k_sigaction ka;
416 	sigset_t *oldset;
417 	siginfo_t info;
418 	int signr;
419 
420 	/*
421 	 * We want the common case to go fast, which is why we may in certain
422 	 * cases get here from kernel mode. Just return without doing anything
423 	 * if so.
424 	 */
425 	if (!user_mode(regs))
426 		return;
427 
428 	if (try_to_freeze())
429 		goto no_signal;
430 
431 	if (test_thread_flag(TIF_RESTORE_SIGMASK))
432 		oldset = &current->saved_sigmask;
433 	else
434 		oldset = &current->blocked;
435 
436 
437 	signr = get_signal_to_deliver(&info, &ka, regs, NULL);
438 	if (signr > 0) {
439 		/* Whee!  Actually deliver the signal.  */
440 		if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
441 			/*
442 			 * A signal was successfully delivered; the saved
443 			 * sigmask will have been stored in the signal frame,
444 			 * and will be restored by sigreturn, so we can simply
445 			 * clear the TIF_RESTORE_SIGMASK flag.
446 			 */
447 			if (test_thread_flag(TIF_RESTORE_SIGMASK))
448 				clear_thread_flag(TIF_RESTORE_SIGMASK);
449 		}
450 	}
451 
452 no_signal:
453 	/*
454 	 * Who's code doesn't conform to the restartable syscall convention
455 	 * dies here!!!  The li instruction, a single machine instruction,
456 	 * must directly be followed by the syscall instruction.
457 	 */
458 	if (regs->regs[0]) {
459 		if (regs->regs[2] == ERESTARTNOHAND ||
460 		    regs->regs[2] == ERESTARTSYS ||
461 		    regs->regs[2] == ERESTARTNOINTR) {
462 			regs->regs[7] = regs->regs[26];
463 			regs->cp0_epc -= 8;
464 		}
465 		if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
466 			regs->regs[2] = __NR_restart_syscall;
467 			regs->regs[7] = regs->regs[26];
468 			regs->cp0_epc -= 4;
469 		}
470 	}
471 
472 	/*
473 	 * If there's no signal to deliver, we just put the saved sigmask
474 	 * back
475 	 */
476 	if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
477 		clear_thread_flag(TIF_RESTORE_SIGMASK);
478 		sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
479 	}
480 }
481 
482 /*
483  * notification of userspace execution resumption
484  * - triggered by the TIF_WORK_MASK flags
485  */
486 asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
487 	__u32 thread_info_flags)
488 {
489 	/* deal with pending signal delivery */
490 	if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
491 		current->thread.abi->do_signal(regs);
492 }
493