Add linux-next specific files for 20110831
[linux-2.6/next.git] / arch / frv / kernel / signal.c
blobdfa460e5f8d21661c53d614fb729adada529d978
1 /* signal.c: FRV specific bits of signal handling
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
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.
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 <linux/tracehook.h>
25 #include <asm/ucontext.h>
26 #include <asm/uaccess.h>
27 #include <asm/cacheflush.h>
29 #define DEBUG_SIG 0
31 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
33 struct fdpic_func_descriptor {
34 unsigned long text;
35 unsigned long GOT;
39 * Atomically swap in the new signal mask, and wait for a signal.
41 asmlinkage int sys_sigsuspend(int history0, int history1, old_sigset_t mask)
43 sigset_t blocked;
45 current->saved_sigmask = current->blocked;
47 mask &= _BLOCKABLE;
48 siginitset(&blocked, mask);
49 set_current_blocked(&blocked);
51 current->state = TASK_INTERRUPTIBLE;
52 schedule();
53 set_thread_flag(TIF_RESTORE_SIGMASK);
54 return -ERESTARTNOHAND;
57 asmlinkage int sys_sigaction(int sig,
58 const struct old_sigaction __user *act,
59 struct old_sigaction __user *oact)
61 struct k_sigaction new_ka, old_ka;
62 int ret;
64 if (act) {
65 old_sigset_t mask;
66 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
67 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
68 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
69 return -EFAULT;
70 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
71 __get_user(mask, &act->sa_mask);
72 siginitset(&new_ka.sa.sa_mask, mask);
75 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
77 if (!ret && oact) {
78 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
79 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
80 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
81 return -EFAULT;
82 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
83 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
86 return ret;
89 asmlinkage
90 int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
92 return do_sigaltstack(uss, uoss, __frame->sp);
97 * Do a signal return; undo the signal stack.
100 struct sigframe
102 __sigrestore_t pretcode;
103 int sig;
104 struct sigcontext sc;
105 unsigned long extramask[_NSIG_WORDS-1];
106 uint32_t retcode[2];
109 struct rt_sigframe
111 __sigrestore_t pretcode;
112 int sig;
113 struct siginfo __user *pinfo;
114 void __user *puc;
115 struct siginfo info;
116 struct ucontext uc;
117 uint32_t retcode[2];
120 static int restore_sigcontext(struct sigcontext __user *sc, int *_gr8)
122 struct user_context *user = current->thread.user;
123 unsigned long tbr, psr;
125 /* Always make any pending restarted system calls return -EINTR */
126 current_thread_info()->restart_block.fn = do_no_restart_syscall;
128 tbr = user->i.tbr;
129 psr = user->i.psr;
130 if (copy_from_user(user, &sc->sc_context, sizeof(sc->sc_context)))
131 goto badframe;
132 user->i.tbr = tbr;
133 user->i.psr = psr;
135 restore_user_regs(user);
137 user->i.syscallno = -1; /* disable syscall checks */
139 *_gr8 = user->i.gr[8];
140 return 0;
142 badframe:
143 return 1;
146 asmlinkage int sys_sigreturn(void)
148 struct sigframe __user *frame = (struct sigframe __user *) __frame->sp;
149 sigset_t set;
150 int gr8;
152 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
153 goto badframe;
154 if (__get_user(set.sig[0], &frame->sc.sc_oldmask))
155 goto badframe;
157 if (_NSIG_WORDS > 1 &&
158 __copy_from_user(&set.sig[1], &frame->extramask, sizeof(frame->extramask)))
159 goto badframe;
161 sigdelsetmask(&set, ~_BLOCKABLE);
162 set_current_blocked(&set);
164 if (restore_sigcontext(&frame->sc, &gr8))
165 goto badframe;
166 return gr8;
168 badframe:
169 force_sig(SIGSEGV, current);
170 return 0;
173 asmlinkage int sys_rt_sigreturn(void)
175 struct rt_sigframe __user *frame = (struct rt_sigframe __user *) __frame->sp;
176 sigset_t set;
177 int gr8;
179 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
180 goto badframe;
181 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
182 goto badframe;
184 sigdelsetmask(&set, ~_BLOCKABLE);
185 set_current_blocked(&set);
187 if (restore_sigcontext(&frame->uc.uc_mcontext, &gr8))
188 goto badframe;
190 if (do_sigaltstack(&frame->uc.uc_stack, NULL, __frame->sp) == -EFAULT)
191 goto badframe;
193 return gr8;
195 badframe:
196 force_sig(SIGSEGV, current);
197 return 0;
201 * Set up a signal frame
203 static int setup_sigcontext(struct sigcontext __user *sc, unsigned long mask)
205 save_user_regs(current->thread.user);
207 if (copy_to_user(&sc->sc_context, current->thread.user, sizeof(sc->sc_context)) != 0)
208 goto badframe;
210 /* non-iBCS2 extensions.. */
211 if (__put_user(mask, &sc->sc_oldmask) < 0)
212 goto badframe;
214 return 0;
216 badframe:
217 return 1;
220 /*****************************************************************************/
222 * Determine which stack to use..
224 static inline void __user *get_sigframe(struct k_sigaction *ka,
225 size_t frame_size)
227 unsigned long sp;
229 /* Default to using normal stack */
230 sp = __frame->sp;
232 /* This is the X/Open sanctioned signal stack switching. */
233 if (ka->sa.sa_flags & SA_ONSTACK) {
234 if (! sas_ss_flags(sp))
235 sp = current->sas_ss_sp + current->sas_ss_size;
238 return (void __user *) ((sp - frame_size) & ~7UL);
240 } /* end get_sigframe() */
242 /*****************************************************************************/
246 static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set)
248 struct sigframe __user *frame;
249 int rsig;
251 set_fs(USER_DS);
253 frame = get_sigframe(ka, sizeof(*frame));
255 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
256 goto give_sigsegv;
258 rsig = sig;
259 if (sig < 32 &&
260 __current_thread_info->exec_domain &&
261 __current_thread_info->exec_domain->signal_invmap)
262 rsig = __current_thread_info->exec_domain->signal_invmap[sig];
264 if (__put_user(rsig, &frame->sig) < 0)
265 goto give_sigsegv;
267 if (setup_sigcontext(&frame->sc, set->sig[0]))
268 goto give_sigsegv;
270 if (_NSIG_WORDS > 1) {
271 if (__copy_to_user(frame->extramask, &set->sig[1],
272 sizeof(frame->extramask)))
273 goto give_sigsegv;
276 /* Set up to return from userspace. If provided, use a stub
277 * already in userspace. */
278 if (ka->sa.sa_flags & SA_RESTORER) {
279 if (__put_user(ka->sa.sa_restorer, &frame->pretcode) < 0)
280 goto give_sigsegv;
282 else {
283 /* Set up the following code on the stack:
284 * setlos #__NR_sigreturn,gr7
285 * tira gr0,0
287 if (__put_user((__sigrestore_t)frame->retcode, &frame->pretcode) ||
288 __put_user(0x8efc0000|__NR_sigreturn, &frame->retcode[0]) ||
289 __put_user(0xc0700000, &frame->retcode[1]))
290 goto give_sigsegv;
292 flush_icache_range((unsigned long) frame->retcode,
293 (unsigned long) (frame->retcode + 2));
296 /* Set up registers for the signal handler */
297 if (current->personality & FDPIC_FUNCPTRS) {
298 struct fdpic_func_descriptor __user *funcptr =
299 (struct fdpic_func_descriptor __user *) ka->sa.sa_handler;
300 struct fdpic_func_descriptor desc;
301 if (copy_from_user(&desc, funcptr, sizeof(desc)))
302 goto give_sigsegv;
303 __frame->pc = desc.text;
304 __frame->gr15 = desc.GOT;
305 } else {
306 __frame->pc = (unsigned long) ka->sa.sa_handler;
307 __frame->gr15 = 0;
310 __frame->sp = (unsigned long) frame;
311 __frame->lr = (unsigned long) &frame->retcode;
312 __frame->gr8 = sig;
314 /* the tracer may want to single-step inside the handler */
315 if (test_thread_flag(TIF_SINGLESTEP))
316 ptrace_notify(SIGTRAP);
318 #if DEBUG_SIG
319 printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
320 sig, current->comm, current->pid, frame, __frame->pc,
321 frame->pretcode);
322 #endif
324 return 0;
326 give_sigsegv:
327 force_sigsegv(sig, current);
328 return -EFAULT;
330 } /* end setup_frame() */
332 /*****************************************************************************/
336 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
337 sigset_t *set)
339 struct rt_sigframe __user *frame;
340 int rsig;
342 set_fs(USER_DS);
344 frame = get_sigframe(ka, sizeof(*frame));
346 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
347 goto give_sigsegv;
349 rsig = sig;
350 if (sig < 32 &&
351 __current_thread_info->exec_domain &&
352 __current_thread_info->exec_domain->signal_invmap)
353 rsig = __current_thread_info->exec_domain->signal_invmap[sig];
355 if (__put_user(rsig, &frame->sig) ||
356 __put_user(&frame->info, &frame->pinfo) ||
357 __put_user(&frame->uc, &frame->puc))
358 goto give_sigsegv;
360 if (copy_siginfo_to_user(&frame->info, info))
361 goto give_sigsegv;
363 /* Create the ucontext. */
364 if (__put_user(0, &frame->uc.uc_flags) ||
365 __put_user(NULL, &frame->uc.uc_link) ||
366 __put_user((void __user *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp) ||
367 __put_user(sas_ss_flags(__frame->sp), &frame->uc.uc_stack.ss_flags) ||
368 __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size))
369 goto give_sigsegv;
371 if (setup_sigcontext(&frame->uc.uc_mcontext, set->sig[0]))
372 goto give_sigsegv;
374 if (__copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
375 goto give_sigsegv;
377 /* Set up to return from userspace. If provided, use a stub
378 * already in userspace. */
379 if (ka->sa.sa_flags & SA_RESTORER) {
380 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
381 goto give_sigsegv;
383 else {
384 /* Set up the following code on the stack:
385 * setlos #__NR_sigreturn,gr7
386 * tira gr0,0
388 if (__put_user((__sigrestore_t)frame->retcode, &frame->pretcode) ||
389 __put_user(0x8efc0000|__NR_rt_sigreturn, &frame->retcode[0]) ||
390 __put_user(0xc0700000, &frame->retcode[1]))
391 goto give_sigsegv;
393 flush_icache_range((unsigned long) frame->retcode,
394 (unsigned long) (frame->retcode + 2));
397 /* Set up registers for signal handler */
398 if (current->personality & FDPIC_FUNCPTRS) {
399 struct fdpic_func_descriptor __user *funcptr =
400 (struct fdpic_func_descriptor __user *) ka->sa.sa_handler;
401 struct fdpic_func_descriptor desc;
402 if (copy_from_user(&desc, funcptr, sizeof(desc)))
403 goto give_sigsegv;
404 __frame->pc = desc.text;
405 __frame->gr15 = desc.GOT;
406 } else {
407 __frame->pc = (unsigned long) ka->sa.sa_handler;
408 __frame->gr15 = 0;
411 __frame->sp = (unsigned long) frame;
412 __frame->lr = (unsigned long) &frame->retcode;
413 __frame->gr8 = sig;
414 __frame->gr9 = (unsigned long) &frame->info;
416 /* the tracer may want to single-step inside the handler */
417 if (test_thread_flag(TIF_SINGLESTEP))
418 ptrace_notify(SIGTRAP);
420 #if DEBUG_SIG
421 printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
422 sig, current->comm, current->pid, frame, __frame->pc,
423 frame->pretcode);
424 #endif
426 return 0;
428 give_sigsegv:
429 force_sigsegv(sig, current);
430 return -EFAULT;
432 } /* end setup_rt_frame() */
434 /*****************************************************************************/
436 * OK, we're invoking a handler
438 static int handle_signal(unsigned long sig, siginfo_t *info,
439 struct k_sigaction *ka, sigset_t *oldset)
441 int ret;
443 /* Are we from a system call? */
444 if (__frame->syscallno != -1) {
445 /* If so, check system call restarting.. */
446 switch (__frame->gr8) {
447 case -ERESTART_RESTARTBLOCK:
448 case -ERESTARTNOHAND:
449 __frame->gr8 = -EINTR;
450 break;
452 case -ERESTARTSYS:
453 if (!(ka->sa.sa_flags & SA_RESTART)) {
454 __frame->gr8 = -EINTR;
455 break;
458 /* fallthrough */
459 case -ERESTARTNOINTR:
460 __frame->gr8 = __frame->orig_gr8;
461 __frame->pc -= 4;
463 __frame->syscallno = -1;
466 /* Set up the stack frame */
467 if (ka->sa.sa_flags & SA_SIGINFO)
468 ret = setup_rt_frame(sig, ka, info, oldset);
469 else
470 ret = setup_frame(sig, ka, oldset);
472 if (ret == 0)
473 block_sigmask(ka, sig);
475 return ret;
477 } /* end handle_signal() */
479 /*****************************************************************************/
481 * Note that 'init' is a special process: it doesn't get signals it doesn't
482 * want to handle. Thus you cannot kill init even with a SIGKILL even by
483 * mistake.
485 static void do_signal(void)
487 struct k_sigaction ka;
488 siginfo_t info;
489 sigset_t *oldset;
490 int signr;
493 * We want the common case to go fast, which
494 * is why we may in certain cases get here from
495 * kernel mode. Just return without doing anything
496 * if so.
498 if (!user_mode(__frame))
499 return;
501 if (try_to_freeze())
502 goto no_signal;
504 if (test_thread_flag(TIF_RESTORE_SIGMASK))
505 oldset = &current->saved_sigmask;
506 else
507 oldset = &current->blocked;
509 signr = get_signal_to_deliver(&info, &ka, __frame, NULL);
510 if (signr > 0) {
511 if (handle_signal(signr, &info, &ka, oldset) == 0) {
512 /* a signal was successfully delivered; the saved
513 * sigmask will have been stored in the signal frame,
514 * and will be restored by sigreturn, so we can simply
515 * clear the TIF_RESTORE_SIGMASK flag */
516 if (test_thread_flag(TIF_RESTORE_SIGMASK))
517 clear_thread_flag(TIF_RESTORE_SIGMASK);
519 tracehook_signal_handler(signr, &info, &ka, __frame,
520 test_thread_flag(TIF_SINGLESTEP));
523 return;
526 no_signal:
527 /* Did we come from a system call? */
528 if (__frame->syscallno != -1) {
529 /* Restart the system call - no handlers present */
530 switch (__frame->gr8) {
531 case -ERESTARTNOHAND:
532 case -ERESTARTSYS:
533 case -ERESTARTNOINTR:
534 __frame->gr8 = __frame->orig_gr8;
535 __frame->pc -= 4;
536 break;
538 case -ERESTART_RESTARTBLOCK:
539 __frame->gr7 = __NR_restart_syscall;
540 __frame->pc -= 4;
541 break;
543 __frame->syscallno = -1;
546 /* if there's no signal to deliver, we just put the saved sigmask
547 * back */
548 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
549 clear_thread_flag(TIF_RESTORE_SIGMASK);
550 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
553 } /* end do_signal() */
555 /*****************************************************************************/
557 * notification of userspace execution resumption
558 * - triggered by the TIF_WORK_MASK flags
560 asmlinkage void do_notify_resume(__u32 thread_info_flags)
562 /* pending single-step? */
563 if (thread_info_flags & _TIF_SINGLESTEP)
564 clear_thread_flag(TIF_SINGLESTEP);
566 /* deal with pending signal delivery */
567 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
568 do_signal();
570 /* deal with notification on about to resume userspace execution */
571 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
572 clear_thread_flag(TIF_NOTIFY_RESUME);
573 tracehook_notify_resume(__frame);
574 if (current->replacement_session_keyring)
575 key_replace_session_keyring();
578 } /* end do_notify_resume() */