added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / arch / frv / kernel / signal.c
blob3bdb368292a8c8a53f9c00b3aa39dc169420b095
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 <asm/ucontext.h>
25 #include <asm/uaccess.h>
26 #include <asm/cacheflush.h>
28 #define DEBUG_SIG 0
30 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
32 struct fdpic_func_descriptor {
33 unsigned long text;
34 unsigned long GOT;
38 * Atomically swap in the new signal mask, and wait for a signal.
40 asmlinkage int sys_sigsuspend(int history0, int history1, old_sigset_t mask)
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);
49 current->state = TASK_INTERRUPTIBLE;
50 schedule();
51 set_thread_flag(TIF_RESTORE_SIGMASK);
52 return -ERESTARTNOHAND;
55 asmlinkage int sys_sigaction(int sig,
56 const struct old_sigaction __user *act,
57 struct old_sigaction __user *oact)
59 struct k_sigaction new_ka, old_ka;
60 int ret;
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);
73 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
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);
84 return ret;
87 asmlinkage
88 int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
90 return do_sigaltstack(uss, uoss, __frame->sp);
95 * Do a signal return; undo the signal stack.
98 struct sigframe
100 __sigrestore_t pretcode;
101 int sig;
102 struct sigcontext sc;
103 unsigned long extramask[_NSIG_WORDS-1];
104 uint32_t retcode[2];
107 struct rt_sigframe
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];
118 static int restore_sigcontext(struct sigcontext __user *sc, int *_gr8)
120 struct user_context *user = current->thread.user;
121 unsigned long tbr, psr;
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;
130 restore_user_regs(user);
132 user->i.syscallno = -1; /* disable syscall checks */
134 *_gr8 = user->i.gr[8];
135 return 0;
137 badframe:
138 return 1;
141 asmlinkage int sys_sigreturn(void)
143 struct sigframe __user *frame = (struct sigframe __user *) __frame->sp;
144 sigset_t set;
145 int gr8;
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;
152 if (_NSIG_WORDS > 1 &&
153 __copy_from_user(&set.sig[1], &frame->extramask, sizeof(frame->extramask)))
154 goto badframe;
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);
162 if (restore_sigcontext(&frame->sc, &gr8))
163 goto badframe;
164 return gr8;
166 badframe:
167 force_sig(SIGSEGV, current);
168 return 0;
171 asmlinkage int sys_rt_sigreturn(void)
173 struct rt_sigframe __user *frame = (struct rt_sigframe __user *) __frame->sp;
174 sigset_t set;
175 int gr8;
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;
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);
188 if (restore_sigcontext(&frame->uc.uc_mcontext, &gr8))
189 goto badframe;
191 if (do_sigaltstack(&frame->uc.uc_stack, NULL, __frame->sp) == -EFAULT)
192 goto badframe;
194 return gr8;
196 badframe:
197 force_sig(SIGSEGV, current);
198 return 0;
202 * Set up a signal frame
204 static int setup_sigcontext(struct sigcontext __user *sc, unsigned long mask)
206 save_user_regs(current->thread.user);
208 if (copy_to_user(&sc->sc_context, current->thread.user, sizeof(sc->sc_context)) != 0)
209 goto badframe;
211 /* non-iBCS2 extensions.. */
212 if (__put_user(mask, &sc->sc_oldmask) < 0)
213 goto badframe;
215 return 0;
217 badframe:
218 return 1;
221 /*****************************************************************************/
223 * Determine which stack to use..
225 static inline void __user *get_sigframe(struct k_sigaction *ka,
226 size_t frame_size)
228 unsigned long sp;
230 /* Default to using normal stack */
231 sp = __frame->sp;
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;
239 return (void __user *) ((sp - frame_size) & ~7UL);
241 } /* end get_sigframe() */
243 /*****************************************************************************/
247 static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set)
249 struct sigframe __user *frame;
250 int rsig;
252 frame = get_sigframe(ka, sizeof(*frame));
254 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
255 goto give_sigsegv;
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];
263 if (__put_user(rsig, &frame->sig) < 0)
264 goto give_sigsegv;
266 if (setup_sigcontext(&frame->sc, set->sig[0]))
267 goto give_sigsegv;
269 if (_NSIG_WORDS > 1) {
270 if (__copy_to_user(frame->extramask, &set->sig[1],
271 sizeof(frame->extramask)))
272 goto give_sigsegv;
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;
281 else {
282 /* Set up the following code on the stack:
283 * setlos #__NR_sigreturn,gr7
284 * tira gr0,0
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;
291 flush_icache_range((unsigned long) frame->retcode,
292 (unsigned long) (frame->retcode + 2));
295 /* set up registers for signal handler */
296 __frame->sp = (unsigned long) frame;
297 __frame->lr = (unsigned long) &frame->retcode;
298 __frame->gr8 = sig;
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;
310 set_fs(USER_DS);
312 /* the tracer may want to single-step inside the handler */
313 if (test_thread_flag(TIF_SINGLESTEP))
314 ptrace_notify(SIGTRAP);
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
322 return 0;
324 give_sigsegv:
325 force_sig(SIGSEGV, current);
326 return -EFAULT;
328 } /* end setup_frame() */
330 /*****************************************************************************/
334 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
335 sigset_t *set)
337 struct rt_sigframe __user *frame;
338 int rsig;
340 frame = get_sigframe(ka, sizeof(*frame));
342 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
343 goto give_sigsegv;
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];
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;
356 if (copy_siginfo_to_user(&frame->info, info))
357 goto give_sigsegv;
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;
367 if (setup_sigcontext(&frame->uc.uc_mcontext, set->sig[0]))
368 goto give_sigsegv;
370 if (__copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
371 goto give_sigsegv;
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;
379 else {
380 /* Set up the following code on the stack:
381 * setlos #__NR_sigreturn,gr7
382 * tira gr0,0
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;
389 flush_icache_range((unsigned long) frame->retcode,
390 (unsigned long) (frame->retcode + 2));
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;
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;
409 set_fs(USER_DS);
411 /* the tracer may want to single-step inside the handler */
412 if (test_thread_flag(TIF_SINGLESTEP))
413 ptrace_notify(SIGTRAP);
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
421 return 0;
423 give_sigsegv:
424 force_sig(SIGSEGV, current);
425 return -EFAULT;
427 } /* end setup_rt_frame() */
429 /*****************************************************************************/
431 * OK, we're invoking a handler
433 static int handle_signal(unsigned long sig, siginfo_t *info,
434 struct k_sigaction *ka, sigset_t *oldset)
436 int ret;
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;
447 case -ERESTARTSYS:
448 if (!(ka->sa.sa_flags & SA_RESTART)) {
449 __frame->gr8 = -EINTR;
450 break;
453 /* fallthrough */
454 case -ERESTARTNOINTR:
455 __frame->gr8 = __frame->orig_gr8;
456 __frame->pc -= 4;
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);
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);
476 return ret;
478 } /* end handle_signal() */
480 /*****************************************************************************/
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.
486 static void do_signal(void)
488 struct k_sigaction ka;
489 siginfo_t info;
490 sigset_t *oldset;
491 int signr;
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.
499 if (!user_mode(__frame))
500 return;
502 if (try_to_freeze())
503 goto no_signal;
505 if (test_thread_flag(TIF_RESTORE_SIGMASK))
506 oldset = &current->saved_sigmask;
507 else
508 oldset = &current->blocked;
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);
521 return;
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;
536 case -ERESTART_RESTARTBLOCK:
537 __frame->gr8 = __NR_restart_syscall;
538 __frame->pc -= 4;
539 break;
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);
550 } /* end do_signal() */
552 /*****************************************************************************/
554 * notification of userspace execution resumption
555 * - triggered by the TIF_WORK_MASK flags
557 asmlinkage void do_notify_resume(__u32 thread_info_flags)
559 /* pending single-step? */
560 if (thread_info_flags & _TIF_SINGLESTEP)
561 clear_thread_flag(TIF_SINGLESTEP);
563 /* deal with pending signal delivery */
564 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
565 do_signal();
567 } /* end do_notify_resume() */