• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* signal.c: FRV specific bits of signal handling
2  *
3  * Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  * - Derived from arch/m68k/kernel/signal.c
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version
10  * 2 of the License, or (at your option) any later version.
11  */
12 
13 #include <linux/sched.h>
14 #include <linux/mm.h>
15 #include <linux/smp.h>
16 #include <linux/kernel.h>
17 #include <linux/signal.h>
18 #include <linux/errno.h>
19 #include <linux/wait.h>
20 #include <linux/ptrace.h>
21 #include <linux/unistd.h>
22 #include <linux/personality.h>
23 #include <linux/freezer.h>
24 #include <asm/ucontext.h>
25 #include <asm/uaccess.h>
26 #include <asm/cacheflush.h>
27 
28 #define DEBUG_SIG 0
29 
30 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
31 
32 struct fdpic_func_descriptor {
33 	unsigned long	text;
34 	unsigned long	GOT;
35 };
36 
37 /*
38  * Atomically swap in the new signal mask, and wait for a signal.
39  */
sys_sigsuspend(int history0,int history1,old_sigset_t mask)40 asmlinkage int sys_sigsuspend(int history0, int history1, old_sigset_t mask)
41 {
42 	mask &= _BLOCKABLE;
43 	spin_lock_irq(&current->sighand->siglock);
44 	current->saved_sigmask = current->blocked;
45 	siginitset(&current->blocked, mask);
46 	recalc_sigpending();
47 	spin_unlock_irq(&current->sighand->siglock);
48 
49 	current->state = TASK_INTERRUPTIBLE;
50 	schedule();
51 	set_thread_flag(TIF_RESTORE_SIGMASK);
52 	return -ERESTARTNOHAND;
53 }
54 
sys_sigaction(int sig,const struct old_sigaction __user * act,struct old_sigaction __user * oact)55 asmlinkage int sys_sigaction(int sig,
56 			     const struct old_sigaction __user *act,
57 			     struct old_sigaction __user *oact)
58 {
59 	struct k_sigaction new_ka, old_ka;
60 	int ret;
61 
62 	if (act) {
63 		old_sigset_t mask;
64 		if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
65 		    __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
66 		    __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
67 			return -EFAULT;
68 		__get_user(new_ka.sa.sa_flags, &act->sa_flags);
69 		__get_user(mask, &act->sa_mask);
70 		siginitset(&new_ka.sa.sa_mask, mask);
71 	}
72 
73 	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
74 
75 	if (!ret && oact) {
76 		if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
77 		    __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
78 		    __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
79 			return -EFAULT;
80 		__put_user(old_ka.sa.sa_flags, &oact->sa_flags);
81 		__put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
82 	}
83 
84 	return ret;
85 }
86 
87 asmlinkage
sys_sigaltstack(const stack_t __user * uss,stack_t __user * uoss)88 int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
89 {
90 	return do_sigaltstack(uss, uoss, __frame->sp);
91 }
92 
93 
94 /*
95  * Do a signal return; undo the signal stack.
96  */
97 
98 struct sigframe
99 {
100 	__sigrestore_t pretcode;
101 	int sig;
102 	struct sigcontext sc;
103 	unsigned long extramask[_NSIG_WORDS-1];
104 	uint32_t retcode[2];
105 };
106 
107 struct rt_sigframe
108 {
109 	__sigrestore_t pretcode;
110 	int sig;
111 	struct siginfo __user *pinfo;
112 	void __user *puc;
113 	struct siginfo info;
114 	struct ucontext uc;
115 	uint32_t retcode[2];
116 };
117 
restore_sigcontext(struct sigcontext __user * sc,int * _gr8)118 static int restore_sigcontext(struct sigcontext __user *sc, int *_gr8)
119 {
120 	struct user_context *user = current->thread.user;
121 	unsigned long tbr, psr;
122 
123 	tbr = user->i.tbr;
124 	psr = user->i.psr;
125 	if (copy_from_user(user, &sc->sc_context, sizeof(sc->sc_context)))
126 		goto badframe;
127 	user->i.tbr = tbr;
128 	user->i.psr = psr;
129 
130 	restore_user_regs(user);
131 
132 	user->i.syscallno = -1;		/* disable syscall checks */
133 
134 	*_gr8 = user->i.gr[8];
135 	return 0;
136 
137  badframe:
138 	return 1;
139 }
140 
sys_sigreturn(void)141 asmlinkage int sys_sigreturn(void)
142 {
143 	struct sigframe __user *frame = (struct sigframe __user *) __frame->sp;
144 	sigset_t set;
145 	int gr8;
146 
147 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
148 		goto badframe;
149 	if (__get_user(set.sig[0], &frame->sc.sc_oldmask))
150 		goto badframe;
151 
152 	if (_NSIG_WORDS > 1 &&
153 	    __copy_from_user(&set.sig[1], &frame->extramask, sizeof(frame->extramask)))
154 		goto badframe;
155 
156 	sigdelsetmask(&set, ~_BLOCKABLE);
157 	spin_lock_irq(&current->sighand->siglock);
158 	current->blocked = set;
159 	recalc_sigpending();
160 	spin_unlock_irq(&current->sighand->siglock);
161 
162 	if (restore_sigcontext(&frame->sc, &gr8))
163 		goto badframe;
164 	return gr8;
165 
166  badframe:
167 	force_sig(SIGSEGV, current);
168 	return 0;
169 }
170 
sys_rt_sigreturn(void)171 asmlinkage int sys_rt_sigreturn(void)
172 {
173 	struct rt_sigframe __user *frame = (struct rt_sigframe __user *) __frame->sp;
174 	sigset_t set;
175 	int gr8;
176 
177 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
178 		goto badframe;
179 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
180 		goto badframe;
181 
182 	sigdelsetmask(&set, ~_BLOCKABLE);
183 	spin_lock_irq(&current->sighand->siglock);
184 	current->blocked = set;
185 	recalc_sigpending();
186 	spin_unlock_irq(&current->sighand->siglock);
187 
188 	if (restore_sigcontext(&frame->uc.uc_mcontext, &gr8))
189 		goto badframe;
190 
191 	if (do_sigaltstack(&frame->uc.uc_stack, NULL, __frame->sp) == -EFAULT)
192 		goto badframe;
193 
194 	return gr8;
195 
196 badframe:
197 	force_sig(SIGSEGV, current);
198 	return 0;
199 }
200 
201 /*
202  * Set up a signal frame
203  */
setup_sigcontext(struct sigcontext __user * sc,unsigned long mask)204 static int setup_sigcontext(struct sigcontext __user *sc, unsigned long mask)
205 {
206 	save_user_regs(current->thread.user);
207 
208 	if (copy_to_user(&sc->sc_context, current->thread.user, sizeof(sc->sc_context)) != 0)
209 		goto badframe;
210 
211 	/* non-iBCS2 extensions.. */
212 	if (__put_user(mask, &sc->sc_oldmask) < 0)
213 		goto badframe;
214 
215 	return 0;
216 
217  badframe:
218 	return 1;
219 }
220 
221 /*****************************************************************************/
222 /*
223  * Determine which stack to use..
224  */
get_sigframe(struct k_sigaction * ka,size_t frame_size)225 static inline void __user *get_sigframe(struct k_sigaction *ka,
226 					size_t frame_size)
227 {
228 	unsigned long sp;
229 
230 	/* Default to using normal stack */
231 	sp = __frame->sp;
232 
233 	/* This is the X/Open sanctioned signal stack switching.  */
234 	if (ka->sa.sa_flags & SA_ONSTACK) {
235 		if (! sas_ss_flags(sp))
236 			sp = current->sas_ss_sp + current->sas_ss_size;
237 	}
238 
239 	return (void __user *) ((sp - frame_size) & ~7UL);
240 
241 } /* end get_sigframe() */
242 
243 /*****************************************************************************/
244 /*
245  *
246  */
setup_frame(int sig,struct k_sigaction * ka,sigset_t * set)247 static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set)
248 {
249 	struct sigframe __user *frame;
250 	int rsig;
251 
252 	frame = get_sigframe(ka, sizeof(*frame));
253 
254 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
255 		goto give_sigsegv;
256 
257 	rsig = sig;
258 	if (sig < 32 &&
259 	    __current_thread_info->exec_domain &&
260 	    __current_thread_info->exec_domain->signal_invmap)
261 		rsig = __current_thread_info->exec_domain->signal_invmap[sig];
262 
263 	if (__put_user(rsig, &frame->sig) < 0)
264 		goto give_sigsegv;
265 
266 	if (setup_sigcontext(&frame->sc, set->sig[0]))
267 		goto give_sigsegv;
268 
269 	if (_NSIG_WORDS > 1) {
270 		if (__copy_to_user(frame->extramask, &set->sig[1],
271 				   sizeof(frame->extramask)))
272 			goto give_sigsegv;
273 	}
274 
275 	/* Set up to return from userspace.  If provided, use a stub
276 	 * already in userspace.  */
277 	if (ka->sa.sa_flags & SA_RESTORER) {
278 		if (__put_user(ka->sa.sa_restorer, &frame->pretcode) < 0)
279 			goto give_sigsegv;
280 	}
281 	else {
282 		/* Set up the following code on the stack:
283 		 *	setlos	#__NR_sigreturn,gr7
284 		 *	tira	gr0,0
285 		 */
286 		if (__put_user((__sigrestore_t)frame->retcode, &frame->pretcode) ||
287 		    __put_user(0x8efc0000|__NR_sigreturn, &frame->retcode[0]) ||
288 		    __put_user(0xc0700000, &frame->retcode[1]))
289 			goto give_sigsegv;
290 
291 		flush_icache_range((unsigned long) frame->retcode,
292 				   (unsigned long) (frame->retcode + 2));
293 	}
294 
295 	/* set up registers for signal handler */
296 	__frame->sp   = (unsigned long) frame;
297 	__frame->lr   = (unsigned long) &frame->retcode;
298 	__frame->gr8  = sig;
299 
300 	if (current->personality & FDPIC_FUNCPTRS) {
301 		struct fdpic_func_descriptor __user *funcptr =
302 			(struct fdpic_func_descriptor __user *) ka->sa.sa_handler;
303 		__get_user(__frame->pc, &funcptr->text);
304 		__get_user(__frame->gr15, &funcptr->GOT);
305 	} else {
306 		__frame->pc   = (unsigned long) ka->sa.sa_handler;
307 		__frame->gr15 = 0;
308 	}
309 
310 	set_fs(USER_DS);
311 
312 	/* the tracer may want to single-step inside the handler */
313 	if (test_thread_flag(TIF_SINGLESTEP))
314 		ptrace_notify(SIGTRAP);
315 
316 #if DEBUG_SIG
317 	printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
318 	       sig, current->comm, current->pid, frame, __frame->pc,
319 	       frame->pretcode);
320 #endif
321 
322 	return 0;
323 
324 give_sigsegv:
325 	force_sig(SIGSEGV, current);
326 	return -EFAULT;
327 
328 } /* end setup_frame() */
329 
330 /*****************************************************************************/
331 /*
332  *
333  */
setup_rt_frame(int sig,struct k_sigaction * ka,siginfo_t * info,sigset_t * set)334 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
335 			  sigset_t *set)
336 {
337 	struct rt_sigframe __user *frame;
338 	int rsig;
339 
340 	frame = get_sigframe(ka, sizeof(*frame));
341 
342 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
343 		goto give_sigsegv;
344 
345 	rsig = sig;
346 	if (sig < 32 &&
347 	    __current_thread_info->exec_domain &&
348 	    __current_thread_info->exec_domain->signal_invmap)
349 		rsig = __current_thread_info->exec_domain->signal_invmap[sig];
350 
351 	if (__put_user(rsig,		&frame->sig) ||
352 	    __put_user(&frame->info,	&frame->pinfo) ||
353 	    __put_user(&frame->uc,	&frame->puc))
354 		goto give_sigsegv;
355 
356 	if (copy_siginfo_to_user(&frame->info, info))
357 		goto give_sigsegv;
358 
359 	/* Create the ucontext.  */
360 	if (__put_user(0, &frame->uc.uc_flags) ||
361 	    __put_user(NULL, &frame->uc.uc_link) ||
362 	    __put_user((void __user *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp) ||
363 	    __put_user(sas_ss_flags(__frame->sp), &frame->uc.uc_stack.ss_flags) ||
364 	    __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size))
365 		goto give_sigsegv;
366 
367 	if (setup_sigcontext(&frame->uc.uc_mcontext, set->sig[0]))
368 		goto give_sigsegv;
369 
370 	if (__copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
371 		goto give_sigsegv;
372 
373 	/* Set up to return from userspace.  If provided, use a stub
374 	 * already in userspace.  */
375 	if (ka->sa.sa_flags & SA_RESTORER) {
376 		if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
377 			goto give_sigsegv;
378 	}
379 	else {
380 		/* Set up the following code on the stack:
381 		 *	setlos	#__NR_sigreturn,gr7
382 		 *	tira	gr0,0
383 		 */
384 		if (__put_user((__sigrestore_t)frame->retcode, &frame->pretcode) ||
385 		    __put_user(0x8efc0000|__NR_rt_sigreturn, &frame->retcode[0]) ||
386 		    __put_user(0xc0700000, &frame->retcode[1]))
387 			goto give_sigsegv;
388 
389 		flush_icache_range((unsigned long) frame->retcode,
390 				   (unsigned long) (frame->retcode + 2));
391 	}
392 
393 	/* Set up registers for signal handler */
394 	__frame->sp  = (unsigned long) frame;
395 	__frame->lr   = (unsigned long) &frame->retcode;
396 	__frame->gr8 = sig;
397 	__frame->gr9 = (unsigned long) &frame->info;
398 
399 	if (current->personality & FDPIC_FUNCPTRS) {
400 		struct fdpic_func_descriptor __user *funcptr =
401 			(struct fdpic_func_descriptor __user *) ka->sa.sa_handler;
402 		__get_user(__frame->pc, &funcptr->text);
403 		__get_user(__frame->gr15, &funcptr->GOT);
404 	} else {
405 		__frame->pc   = (unsigned long) ka->sa.sa_handler;
406 		__frame->gr15 = 0;
407 	}
408 
409 	set_fs(USER_DS);
410 
411 	/* the tracer may want to single-step inside the handler */
412 	if (test_thread_flag(TIF_SINGLESTEP))
413 		ptrace_notify(SIGTRAP);
414 
415 #if DEBUG_SIG
416 	printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
417 	       sig, current->comm, current->pid, frame, __frame->pc,
418 	       frame->pretcode);
419 #endif
420 
421 	return 0;
422 
423 give_sigsegv:
424 	force_sig(SIGSEGV, current);
425 	return -EFAULT;
426 
427 } /* end setup_rt_frame() */
428 
429 /*****************************************************************************/
430 /*
431  * OK, we're invoking a handler
432  */
handle_signal(unsigned long sig,siginfo_t * info,struct k_sigaction * ka,sigset_t * oldset)433 static int handle_signal(unsigned long sig, siginfo_t *info,
434 			 struct k_sigaction *ka, sigset_t *oldset)
435 {
436 	int ret;
437 
438 	/* Are we from a system call? */
439 	if (in_syscall(__frame)) {
440 		/* If so, check system call restarting.. */
441 		switch (__frame->gr8) {
442 		case -ERESTART_RESTARTBLOCK:
443 		case -ERESTARTNOHAND:
444 			__frame->gr8 = -EINTR;
445 			break;
446 
447 		case -ERESTARTSYS:
448 			if (!(ka->sa.sa_flags & SA_RESTART)) {
449 				__frame->gr8 = -EINTR;
450 				break;
451 			}
452 
453 			/* fallthrough */
454 		case -ERESTARTNOINTR:
455 			__frame->gr8 = __frame->orig_gr8;
456 			__frame->pc -= 4;
457 		}
458 	}
459 
460 	/* Set up the stack frame */
461 	if (ka->sa.sa_flags & SA_SIGINFO)
462 		ret = setup_rt_frame(sig, ka, info, oldset);
463 	else
464 		ret = setup_frame(sig, ka, oldset);
465 
466 	if (ret == 0) {
467 		spin_lock_irq(&current->sighand->siglock);
468 		sigorsets(&current->blocked, &current->blocked,
469 			  &ka->sa.sa_mask);
470 		if (!(ka->sa.sa_flags & SA_NODEFER))
471 			sigaddset(&current->blocked, sig);
472 		recalc_sigpending();
473 		spin_unlock_irq(&current->sighand->siglock);
474 	}
475 
476 	return ret;
477 
478 } /* end handle_signal() */
479 
480 /*****************************************************************************/
481 /*
482  * Note that 'init' is a special process: it doesn't get signals it doesn't
483  * want to handle. Thus you cannot kill init even with a SIGKILL even by
484  * mistake.
485  */
do_signal(void)486 static void do_signal(void)
487 {
488 	struct k_sigaction ka;
489 	siginfo_t info;
490 	sigset_t *oldset;
491 	int signr;
492 
493 	/*
494 	 * We want the common case to go fast, which
495 	 * is why we may in certain cases get here from
496 	 * kernel mode. Just return without doing anything
497 	 * if so.
498 	 */
499 	if (!user_mode(__frame))
500 		return;
501 
502 	if (try_to_freeze())
503 		goto no_signal;
504 
505 	if (test_thread_flag(TIF_RESTORE_SIGMASK))
506 		oldset = &current->saved_sigmask;
507 	else
508 		oldset = &current->blocked;
509 
510 	signr = get_signal_to_deliver(&info, &ka, __frame, NULL);
511 	if (signr > 0) {
512 		if (handle_signal(signr, &info, &ka, oldset) == 0) {
513 			/* a signal was successfully delivered; the saved
514 			 * sigmask will have been stored in the signal frame,
515 			 * and will be restored by sigreturn, so we can simply
516 			 * clear the TIF_RESTORE_SIGMASK flag */
517 			if (test_thread_flag(TIF_RESTORE_SIGMASK))
518 				clear_thread_flag(TIF_RESTORE_SIGMASK);
519 		}
520 
521 		return;
522 	}
523 
524 no_signal:
525 	/* Did we come from a system call? */
526 	if (__frame->syscallno >= 0) {
527 		/* Restart the system call - no handlers present */
528 		switch (__frame->gr8) {
529 		case -ERESTARTNOHAND:
530 		case -ERESTARTSYS:
531 		case -ERESTARTNOINTR:
532 			__frame->gr8 = __frame->orig_gr8;
533 			__frame->pc -= 4;
534 			break;
535 
536 		case -ERESTART_RESTARTBLOCK:
537 			__frame->gr8 = __NR_restart_syscall;
538 			__frame->pc -= 4;
539 			break;
540 		}
541 	}
542 
543 	/* if there's no signal to deliver, we just put the saved sigmask
544 	 * back */
545 	if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
546 		clear_thread_flag(TIF_RESTORE_SIGMASK);
547 		sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
548 	}
549 
550 } /* end do_signal() */
551 
552 /*****************************************************************************/
553 /*
554  * notification of userspace execution resumption
555  * - triggered by the TIF_WORK_MASK flags
556  */
do_notify_resume(__u32 thread_info_flags)557 asmlinkage void do_notify_resume(__u32 thread_info_flags)
558 {
559 	/* pending single-step? */
560 	if (thread_info_flags & _TIF_SINGLESTEP)
561 		clear_thread_flag(TIF_SINGLESTEP);
562 
563 	/* deal with pending signal delivery */
564 	if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
565 		do_signal();
566 
567 } /* end do_notify_resume() */
568