Add linux-next specific files for 20110831
[linux-2.6/next.git] / arch / mn10300 / kernel / signal.c
blobbd77c6d23a45a07b2be2893d0c5bf7ae5cc6f8e8
1 /* MN10300 Signal handling
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
12 #include <linux/sched.h>
13 #include <linux/mm.h>
14 #include <linux/smp.h>
15 #include <linux/kernel.h>
16 #include <linux/signal.h>
17 #include <linux/errno.h>
18 #include <linux/wait.h>
19 #include <linux/ptrace.h>
20 #include <linux/unistd.h>
21 #include <linux/stddef.h>
22 #include <linux/tty.h>
23 #include <linux/personality.h>
24 #include <linux/suspend.h>
25 #include <linux/tracehook.h>
26 #include <asm/cacheflush.h>
27 #include <asm/ucontext.h>
28 #include <asm/uaccess.h>
29 #include <asm/fpu.h>
30 #include "sigframe.h"
32 #define DEBUG_SIG 0
34 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
37 * atomically swap in the new signal mask, and wait for a signal.
39 asmlinkage long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
41 sigset_t blocked;
43 current->saved_sigmask = current->blocked;
45 mask &= _BLOCKABLE;
46 siginitset(&blocked, mask);
47 set_current_blocked(&blocked);
49 current->state = TASK_INTERRUPTIBLE;
50 schedule();
51 set_thread_flag(TIF_RESTORE_SIGMASK);
52 return -ERESTARTNOHAND;
56 * set signal action syscall
58 asmlinkage long sys_sigaction(int sig,
59 const struct old_sigaction __user *act,
60 struct old_sigaction __user *oact)
62 struct k_sigaction new_ka, old_ka;
63 int ret;
65 if (act) {
66 old_sigset_t mask;
67 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
68 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
69 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
70 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
71 __get_user(mask, &act->sa_mask))
72 return -EFAULT;
73 siginitset(&new_ka.sa.sa_mask, mask);
76 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
78 if (!ret && oact) {
79 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
80 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
81 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
82 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
83 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
84 return -EFAULT;
87 return ret;
91 * set alternate signal stack syscall
93 asmlinkage long sys_sigaltstack(const stack_t __user *uss, stack_t *uoss)
95 return do_sigaltstack(uss, uoss, current_frame()->sp);
99 * do a signal return; undo the signal stack.
101 static int restore_sigcontext(struct pt_regs *regs,
102 struct sigcontext __user *sc, long *_d0)
104 unsigned int err = 0;
106 /* Always make any pending restarted system calls return -EINTR */
107 current_thread_info()->restart_block.fn = do_no_restart_syscall;
109 if (is_using_fpu(current))
110 fpu_kill_state(current);
112 #define COPY(x) err |= __get_user(regs->x, &sc->x)
113 COPY(d1); COPY(d2); COPY(d3);
114 COPY(a0); COPY(a1); COPY(a2); COPY(a3);
115 COPY(e0); COPY(e1); COPY(e2); COPY(e3);
116 COPY(e4); COPY(e5); COPY(e6); COPY(e7);
117 COPY(lar); COPY(lir);
118 COPY(mdr); COPY(mdrq);
119 COPY(mcvf); COPY(mcrl); COPY(mcrh);
120 COPY(sp); COPY(pc);
121 #undef COPY
124 unsigned int tmpflags;
125 #ifndef CONFIG_MN10300_USING_JTAG
126 #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
127 EPSW_T | EPSW_nAR)
128 #else
129 #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
130 EPSW_nAR)
131 #endif
132 err |= __get_user(tmpflags, &sc->epsw);
133 regs->epsw = (regs->epsw & ~USER_EPSW) |
134 (tmpflags & USER_EPSW);
135 regs->orig_d0 = -1; /* disable syscall checks */
139 struct fpucontext *buf;
140 err |= __get_user(buf, &sc->fpucontext);
141 if (buf) {
142 if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
143 goto badframe;
144 err |= fpu_restore_sigcontext(buf);
148 err |= __get_user(*_d0, &sc->d0);
149 return err;
151 badframe:
152 return 1;
156 * standard signal return syscall
158 asmlinkage long sys_sigreturn(void)
160 struct sigframe __user *frame;
161 sigset_t set;
162 long d0;
164 frame = (struct sigframe __user *) current_frame()->sp;
165 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
166 goto badframe;
167 if (__get_user(set.sig[0], &frame->sc.oldmask))
168 goto badframe;
170 if (_NSIG_WORDS > 1 &&
171 __copy_from_user(&set.sig[1], &frame->extramask,
172 sizeof(frame->extramask)))
173 goto badframe;
175 sigdelsetmask(&set, ~_BLOCKABLE);
176 set_current_blocked(&set);
178 if (restore_sigcontext(current_frame(), &frame->sc, &d0))
179 goto badframe;
181 return d0;
183 badframe:
184 force_sig(SIGSEGV, current);
185 return 0;
189 * realtime signal return syscall
191 asmlinkage long sys_rt_sigreturn(void)
193 struct rt_sigframe __user *frame;
194 sigset_t set;
195 long d0;
197 frame = (struct rt_sigframe __user *) current_frame()->sp;
198 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
199 goto badframe;
200 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
201 goto badframe;
203 sigdelsetmask(&set, ~_BLOCKABLE);
204 set_current_blocked(&set);
206 if (restore_sigcontext(current_frame(), &frame->uc.uc_mcontext, &d0))
207 goto badframe;
209 if (do_sigaltstack(&frame->uc.uc_stack, NULL, current_frame()->sp) ==
210 -EFAULT)
211 goto badframe;
213 return d0;
215 badframe:
216 force_sig(SIGSEGV, current);
217 return 0;
221 * store the userspace context into a signal frame
223 static int setup_sigcontext(struct sigcontext __user *sc,
224 struct fpucontext *fpuctx,
225 struct pt_regs *regs,
226 unsigned long mask)
228 int tmp, err = 0;
230 #define COPY(x) err |= __put_user(regs->x, &sc->x)
231 COPY(d0); COPY(d1); COPY(d2); COPY(d3);
232 COPY(a0); COPY(a1); COPY(a2); COPY(a3);
233 COPY(e0); COPY(e1); COPY(e2); COPY(e3);
234 COPY(e4); COPY(e5); COPY(e6); COPY(e7);
235 COPY(lar); COPY(lir);
236 COPY(mdr); COPY(mdrq);
237 COPY(mcvf); COPY(mcrl); COPY(mcrh);
238 COPY(sp); COPY(epsw); COPY(pc);
239 #undef COPY
241 tmp = fpu_setup_sigcontext(fpuctx);
242 if (tmp < 0)
243 err = 1;
244 else
245 err |= __put_user(tmp ? fpuctx : NULL, &sc->fpucontext);
247 /* non-iBCS2 extensions.. */
248 err |= __put_user(mask, &sc->oldmask);
250 return err;
254 * determine which stack to use..
256 static inline void __user *get_sigframe(struct k_sigaction *ka,
257 struct pt_regs *regs,
258 size_t frame_size)
260 unsigned long sp;
262 /* default to using normal stack */
263 sp = regs->sp;
265 /* this is the X/Open sanctioned signal stack switching. */
266 if (ka->sa.sa_flags & SA_ONSTACK) {
267 if (sas_ss_flags(sp) == 0)
268 sp = current->sas_ss_sp + current->sas_ss_size;
271 return (void __user *) ((sp - frame_size) & ~7UL);
275 * set up a normal signal frame
277 static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
278 struct pt_regs *regs)
280 struct sigframe __user *frame;
281 int rsig;
283 frame = get_sigframe(ka, regs, sizeof(*frame));
285 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
286 goto give_sigsegv;
288 rsig = sig;
289 if (sig < 32 &&
290 current_thread_info()->exec_domain &&
291 current_thread_info()->exec_domain->signal_invmap)
292 rsig = current_thread_info()->exec_domain->signal_invmap[sig];
294 if (__put_user(rsig, &frame->sig) < 0 ||
295 __put_user(&frame->sc, &frame->psc) < 0)
296 goto give_sigsegv;
298 if (setup_sigcontext(&frame->sc, &frame->fpuctx, regs, set->sig[0]))
299 goto give_sigsegv;
301 if (_NSIG_WORDS > 1) {
302 if (__copy_to_user(frame->extramask, &set->sig[1],
303 sizeof(frame->extramask)))
304 goto give_sigsegv;
307 /* set up to return from userspace. If provided, use a stub already in
308 * userspace */
309 if (ka->sa.sa_flags & SA_RESTORER) {
310 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
311 goto give_sigsegv;
312 } else {
313 if (__put_user((void (*)(void))frame->retcode,
314 &frame->pretcode))
315 goto give_sigsegv;
316 /* this is mov $,d0; syscall 0 */
317 if (__put_user(0x2c, (char *)(frame->retcode + 0)) ||
318 __put_user(__NR_sigreturn, (char *)(frame->retcode + 1)) ||
319 __put_user(0x00, (char *)(frame->retcode + 2)) ||
320 __put_user(0xf0, (char *)(frame->retcode + 3)) ||
321 __put_user(0xe0, (char *)(frame->retcode + 4)))
322 goto give_sigsegv;
323 flush_icache_range((unsigned long) frame->retcode,
324 (unsigned long) frame->retcode + 5);
327 /* set up registers for signal handler */
328 regs->sp = (unsigned long) frame;
329 regs->pc = (unsigned long) ka->sa.sa_handler;
330 regs->d0 = sig;
331 regs->d1 = (unsigned long) &frame->sc;
333 /* the tracer may want to single-step inside the handler */
334 if (test_thread_flag(TIF_SINGLESTEP))
335 ptrace_notify(SIGTRAP);
337 #if DEBUG_SIG
338 printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
339 sig, current->comm, current->pid, frame, regs->pc,
340 frame->pretcode);
341 #endif
343 return 0;
345 give_sigsegv:
346 force_sigsegv(sig, current);
347 return -EFAULT;
351 * set up a realtime signal frame
353 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
354 sigset_t *set, struct pt_regs *regs)
356 struct rt_sigframe __user *frame;
357 int rsig;
359 frame = get_sigframe(ka, regs, sizeof(*frame));
361 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
362 goto give_sigsegv;
364 rsig = sig;
365 if (sig < 32 &&
366 current_thread_info()->exec_domain &&
367 current_thread_info()->exec_domain->signal_invmap)
368 rsig = current_thread_info()->exec_domain->signal_invmap[sig];
370 if (__put_user(rsig, &frame->sig) ||
371 __put_user(&frame->info, &frame->pinfo) ||
372 __put_user(&frame->uc, &frame->puc) ||
373 copy_siginfo_to_user(&frame->info, info))
374 goto give_sigsegv;
376 /* create the ucontext. */
377 if (__put_user(0, &frame->uc.uc_flags) ||
378 __put_user(0, &frame->uc.uc_link) ||
379 __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp) ||
380 __put_user(sas_ss_flags(regs->sp), &frame->uc.uc_stack.ss_flags) ||
381 __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size) ||
382 setup_sigcontext(&frame->uc.uc_mcontext,
383 &frame->fpuctx, regs, set->sig[0]) ||
384 __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
385 goto give_sigsegv;
387 /* set up to return from userspace. If provided, use a stub already in
388 * userspace */
389 if (ka->sa.sa_flags & SA_RESTORER) {
390 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
391 goto give_sigsegv;
392 } else {
393 if (__put_user((void(*)(void))frame->retcode,
394 &frame->pretcode) ||
395 /* This is mov $,d0; syscall 0 */
396 __put_user(0x2c, (char *)(frame->retcode + 0)) ||
397 __put_user(__NR_rt_sigreturn,
398 (char *)(frame->retcode + 1)) ||
399 __put_user(0x00, (char *)(frame->retcode + 2)) ||
400 __put_user(0xf0, (char *)(frame->retcode + 3)) ||
401 __put_user(0xe0, (char *)(frame->retcode + 4)))
402 goto give_sigsegv;
404 flush_icache_range((u_long) frame->retcode,
405 (u_long) frame->retcode + 5);
408 /* Set up registers for signal handler */
409 regs->sp = (unsigned long) frame;
410 regs->pc = (unsigned long) ka->sa.sa_handler;
411 regs->d0 = sig;
412 regs->d1 = (long) &frame->info;
414 /* the tracer may want to single-step inside the handler */
415 if (test_thread_flag(TIF_SINGLESTEP))
416 ptrace_notify(SIGTRAP);
418 #if DEBUG_SIG
419 printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
420 sig, current->comm, current->pid, frame, regs->pc,
421 frame->pretcode);
422 #endif
424 return 0;
426 give_sigsegv:
427 force_sigsegv(sig, current);
428 return -EFAULT;
431 static inline void stepback(struct pt_regs *regs)
433 regs->pc -= 2;
434 regs->orig_d0 = -1;
438 * handle the actual delivery of a signal to userspace
440 static int handle_signal(int sig,
441 siginfo_t *info, struct k_sigaction *ka,
442 sigset_t *oldset, struct pt_regs *regs)
444 int ret;
446 /* Are we from a system call? */
447 if (regs->orig_d0 >= 0) {
448 /* If so, check system call restarting.. */
449 switch (regs->d0) {
450 case -ERESTART_RESTARTBLOCK:
451 case -ERESTARTNOHAND:
452 regs->d0 = -EINTR;
453 break;
455 case -ERESTARTSYS:
456 if (!(ka->sa.sa_flags & SA_RESTART)) {
457 regs->d0 = -EINTR;
458 break;
461 /* fallthrough */
462 case -ERESTARTNOINTR:
463 regs->d0 = regs->orig_d0;
464 stepback(regs);
468 /* Set up the stack frame */
469 if (ka->sa.sa_flags & SA_SIGINFO)
470 ret = setup_rt_frame(sig, ka, info, oldset, regs);
471 else
472 ret = setup_frame(sig, ka, oldset, regs);
474 if (ret == 0)
475 block_sigmask(ka, sig);
477 return ret;
481 * handle a potential signal
483 static void do_signal(struct pt_regs *regs)
485 struct k_sigaction ka;
486 siginfo_t info;
487 sigset_t *oldset;
488 int signr;
490 /* we want the common case to go fast, which is why we may in certain
491 * cases get here from kernel mode */
492 if (!user_mode(regs))
493 return;
495 if (test_thread_flag(TIF_RESTORE_SIGMASK))
496 oldset = &current->saved_sigmask;
497 else
498 oldset = &current->blocked;
500 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
501 if (signr > 0) {
502 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
503 /* a signal was successfully delivered; the saved
504 * sigmask will have been stored in the signal frame,
505 * and will be restored by sigreturn, so we can simply
506 * clear the TIF_RESTORE_SIGMASK flag */
507 if (test_thread_flag(TIF_RESTORE_SIGMASK))
508 clear_thread_flag(TIF_RESTORE_SIGMASK);
510 tracehook_signal_handler(signr, &info, &ka, regs,
511 test_thread_flag(TIF_SINGLESTEP));
514 return;
517 /* did we come from a system call? */
518 if (regs->orig_d0 >= 0) {
519 /* restart the system call - no handlers present */
520 switch (regs->d0) {
521 case -ERESTARTNOHAND:
522 case -ERESTARTSYS:
523 case -ERESTARTNOINTR:
524 regs->d0 = regs->orig_d0;
525 stepback(regs);
526 break;
528 case -ERESTART_RESTARTBLOCK:
529 regs->d0 = __NR_restart_syscall;
530 stepback(regs);
531 break;
535 /* if there's no signal to deliver, we just put the saved sigmask
536 * back */
537 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
538 clear_thread_flag(TIF_RESTORE_SIGMASK);
539 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
544 * notification of userspace execution resumption
545 * - triggered by current->work.notify_resume
547 asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags)
549 /* Pending single-step? */
550 if (thread_info_flags & _TIF_SINGLESTEP) {
551 #ifndef CONFIG_MN10300_USING_JTAG
552 regs->epsw |= EPSW_T;
553 clear_thread_flag(TIF_SINGLESTEP);
554 #else
555 BUG(); /* no h/w single-step if using JTAG unit */
556 #endif
559 /* deal with pending signal delivery */
560 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
561 do_signal(regs);
563 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
564 clear_thread_flag(TIF_NOTIFY_RESUME);
565 tracehook_notify_resume(current_frame());
566 if (current->replacement_session_keyring)
567 key_replace_session_keyring();