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>
14 #include <linux/smp.h>
15 #include <linux/smp_lock.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/stddef.h>
23 #include <linux/tty.h>
24 #include <linux/personality.h>
25 #include <linux/suspend.h>
26 #include <linux/tracehook.h>
27 #include <asm/cacheflush.h>
28 #include <asm/ucontext.h>
29 #include <asm/uaccess.h>
35 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
38 * atomically swap in the new signal mask, and wait for a signal.
40 asmlinkage long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
43 spin_lock_irq(¤t->sighand->siglock);
44 current->saved_sigmask = current->blocked;
45 siginitset(¤t->blocked, mask);
47 spin_unlock_irq(¤t->sighand->siglock);
49 current->state = TASK_INTERRUPTIBLE;
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;
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))
71 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
72 __get_user(mask, &act->sa_mask);
73 siginitset(&new_ka.sa.sa_mask, mask);
76 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
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))
83 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
84 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
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, __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 if (is_using_fpu(current))
107 fpu_kill_state(current);
109 #define COPY(x) err |= __get_user(regs->x, &sc->x)
110 COPY(d1); COPY(d2); COPY(d3);
111 COPY(a0); COPY(a1); COPY(a2); COPY(a3);
112 COPY(e0); COPY(e1); COPY(e2); COPY(e3);
113 COPY(e4); COPY(e5); COPY(e6); COPY(e7);
114 COPY(lar); COPY(lir);
115 COPY(mdr); COPY(mdrq);
116 COPY(mcvf); COPY(mcrl); COPY(mcrh);
121 unsigned int tmpflags;
122 #ifndef CONFIG_MN10300_USING_JTAG
123 #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
126 #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
129 err |= __get_user(tmpflags, &sc->epsw);
130 regs->epsw = (regs->epsw & ~USER_EPSW) |
131 (tmpflags & USER_EPSW);
132 regs->orig_d0 = -1; /* disable syscall checks */
136 struct fpucontext *buf;
137 err |= __get_user(buf, &sc->fpucontext);
139 if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
141 err |= fpu_restore_sigcontext(buf);
145 err |= __get_user(*_d0, &sc->d0);
153 * standard signal return syscall
155 asmlinkage long sys_sigreturn(void)
157 struct sigframe __user *frame = (struct sigframe __user *) __frame->sp;
161 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
163 if (__get_user(set.sig[0], &frame->sc.oldmask))
166 if (_NSIG_WORDS > 1 &&
167 __copy_from_user(&set.sig[1], &frame->extramask,
168 sizeof(frame->extramask)))
171 sigdelsetmask(&set, ~_BLOCKABLE);
172 spin_lock_irq(¤t->sighand->siglock);
173 current->blocked = set;
175 spin_unlock_irq(¤t->sighand->siglock);
177 if (restore_sigcontext(__frame, &frame->sc, &d0))
183 force_sig(SIGSEGV, current);
188 * realtime signal return syscall
190 asmlinkage long sys_rt_sigreturn(void)
192 struct rt_sigframe __user *frame =
193 (struct rt_sigframe __user *) __frame->sp;
197 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
199 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
202 sigdelsetmask(&set, ~_BLOCKABLE);
203 spin_lock_irq(¤t->sighand->siglock);
204 current->blocked = set;
206 spin_unlock_irq(¤t->sighand->siglock);
208 if (restore_sigcontext(__frame, &frame->uc.uc_mcontext, &d0))
211 if (do_sigaltstack(&frame->uc.uc_stack, NULL, __frame->sp) == -EFAULT)
217 force_sig(SIGSEGV, current);
222 * store the userspace context into a signal frame
224 static int setup_sigcontext(struct sigcontext __user *sc,
225 struct fpucontext *fpuctx,
226 struct pt_regs *regs,
231 #define COPY(x) err |= __put_user(regs->x, &sc->x)
232 COPY(d0); COPY(d1); COPY(d2); COPY(d3);
233 COPY(a0); COPY(a1); COPY(a2); COPY(a3);
234 COPY(e0); COPY(e1); COPY(e2); COPY(e3);
235 COPY(e4); COPY(e5); COPY(e6); COPY(e7);
236 COPY(lar); COPY(lir);
237 COPY(mdr); COPY(mdrq);
238 COPY(mcvf); COPY(mcrl); COPY(mcrh);
239 COPY(sp); COPY(epsw); COPY(pc);
242 tmp = fpu_setup_sigcontext(fpuctx);
246 err |= __put_user(tmp ? fpuctx : NULL, &sc->fpucontext);
248 /* non-iBCS2 extensions.. */
249 err |= __put_user(mask, &sc->oldmask);
255 * determine which stack to use..
257 static inline void __user *get_sigframe(struct k_sigaction *ka,
258 struct pt_regs *regs,
263 /* default to using normal stack */
266 /* this is the X/Open sanctioned signal stack switching. */
267 if (ka->sa.sa_flags & SA_ONSTACK) {
268 if (!on_sig_stack(sp))
269 sp = current->sas_ss_sp + current->sas_ss_size;
272 return (void __user *) ((sp - frame_size) & ~7UL);
276 * set up a normal signal frame
278 static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
279 struct pt_regs *regs)
281 struct sigframe __user *frame;
284 frame = get_sigframe(ka, regs, sizeof(*frame));
286 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
291 current_thread_info()->exec_domain &&
292 current_thread_info()->exec_domain->signal_invmap)
293 rsig = current_thread_info()->exec_domain->signal_invmap[sig];
295 if (__put_user(rsig, &frame->sig) < 0 ||
296 __put_user(&frame->sc, &frame->psc) < 0)
299 if (setup_sigcontext(&frame->sc, &frame->fpuctx, regs, set->sig[0]))
302 if (_NSIG_WORDS > 1) {
303 if (__copy_to_user(frame->extramask, &set->sig[1],
304 sizeof(frame->extramask)))
308 /* set up to return from userspace. If provided, use a stub already in
310 if (ka->sa.sa_flags & SA_RESTORER) {
311 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
314 if (__put_user((void (*)(void))frame->retcode,
317 /* this is mov $,d0; syscall 0 */
318 if (__put_user(0x2c, (char *)(frame->retcode + 0)) ||
319 __put_user(__NR_sigreturn, (char *)(frame->retcode + 1)) ||
320 __put_user(0x00, (char *)(frame->retcode + 2)) ||
321 __put_user(0xf0, (char *)(frame->retcode + 3)) ||
322 __put_user(0xe0, (char *)(frame->retcode + 4)))
324 flush_icache_range((unsigned long) frame->retcode,
325 (unsigned long) frame->retcode + 5);
328 /* set up registers for signal handler */
329 regs->sp = (unsigned long) frame;
330 regs->pc = (unsigned long) ka->sa.sa_handler;
332 regs->d1 = (unsigned long) &frame->sc;
336 /* the tracer may want to single-step inside the handler */
337 if (test_thread_flag(TIF_SINGLESTEP))
338 ptrace_notify(SIGTRAP);
341 printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
342 sig, current->comm, current->pid, frame, regs->pc,
349 force_sig(SIGSEGV, current);
354 * set up a realtime signal frame
356 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
357 sigset_t *set, struct pt_regs *regs)
359 struct rt_sigframe __user *frame;
362 frame = get_sigframe(ka, regs, sizeof(*frame));
364 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
369 current_thread_info()->exec_domain &&
370 current_thread_info()->exec_domain->signal_invmap)
371 rsig = current_thread_info()->exec_domain->signal_invmap[sig];
373 if (__put_user(rsig, &frame->sig) ||
374 __put_user(&frame->info, &frame->pinfo) ||
375 __put_user(&frame->uc, &frame->puc) ||
376 copy_siginfo_to_user(&frame->info, info))
379 /* create the ucontext. */
380 if (__put_user(0, &frame->uc.uc_flags) ||
381 __put_user(0, &frame->uc.uc_link) ||
382 __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp) ||
383 __put_user(sas_ss_flags(regs->sp), &frame->uc.uc_stack.ss_flags) ||
384 __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size) ||
385 setup_sigcontext(&frame->uc.uc_mcontext,
386 &frame->fpuctx, regs, set->sig[0]) ||
387 __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
390 /* set up to return from userspace. If provided, use a stub already in
392 if (ka->sa.sa_flags & SA_RESTORER) {
393 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
396 if (__put_user((void(*)(void))frame->retcode,
398 /* This is mov $,d0; syscall 0 */
399 __put_user(0x2c, (char *)(frame->retcode + 0)) ||
400 __put_user(__NR_rt_sigreturn,
401 (char *)(frame->retcode + 1)) ||
402 __put_user(0x00, (char *)(frame->retcode + 2)) ||
403 __put_user(0xf0, (char *)(frame->retcode + 3)) ||
404 __put_user(0xe0, (char *)(frame->retcode + 4)))
407 flush_icache_range((u_long) frame->retcode,
408 (u_long) frame->retcode + 5);
411 /* Set up registers for signal handler */
412 regs->sp = (unsigned long) frame;
413 regs->pc = (unsigned long) ka->sa.sa_handler;
415 regs->d1 = (long) &frame->info;
419 /* the tracer may want to single-step inside the handler */
420 if (test_thread_flag(TIF_SINGLESTEP))
421 ptrace_notify(SIGTRAP);
424 printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
425 sig, current->comm, current->pid, frame, regs->pc,
432 force_sig(SIGSEGV, current);
437 * handle the actual delivery of a signal to userspace
439 static int handle_signal(int sig,
440 siginfo_t *info, struct k_sigaction *ka,
441 sigset_t *oldset, struct pt_regs *regs)
445 /* Are we from a system call? */
446 if (regs->orig_d0 >= 0) {
447 /* If so, check system call restarting.. */
449 case -ERESTART_RESTARTBLOCK:
450 case -ERESTARTNOHAND:
455 if (!(ka->sa.sa_flags & SA_RESTART)) {
461 case -ERESTARTNOINTR:
462 regs->d0 = regs->orig_d0;
467 /* Set up the stack frame */
468 if (ka->sa.sa_flags & SA_SIGINFO)
469 ret = setup_rt_frame(sig, ka, info, oldset, regs);
471 ret = setup_frame(sig, ka, oldset, regs);
474 spin_lock_irq(¤t->sighand->siglock);
475 sigorsets(¤t->blocked, ¤t->blocked,
477 if (!(ka->sa.sa_flags & SA_NODEFER))
478 sigaddset(¤t->blocked, sig);
480 spin_unlock_irq(¤t->sighand->siglock);
487 * handle a potential signal
489 static void do_signal(struct pt_regs *regs)
491 struct k_sigaction ka;
496 /* we want the common case to go fast, which is why we may in certain
497 * cases get here from kernel mode */
498 if (!user_mode(regs))
501 if (test_thread_flag(TIF_RESTORE_SIGMASK))
502 oldset = ¤t->saved_sigmask;
504 oldset = ¤t->blocked;
506 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
508 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
509 /* a signal was successfully delivered; the saved
510 * sigmask will have been stored in the signal frame,
511 * and will be restored by sigreturn, so we can simply
512 * clear the TIF_RESTORE_SIGMASK flag */
513 if (test_thread_flag(TIF_RESTORE_SIGMASK))
514 clear_thread_flag(TIF_RESTORE_SIGMASK);
516 tracehook_signal_handler(signr, &info, &ka, regs,
517 test_thread_flag(TIF_SINGLESTEP));
523 /* did we come from a system call? */
524 if (regs->orig_d0 >= 0) {
525 /* restart the system call - no handlers present */
527 case -ERESTARTNOHAND:
529 case -ERESTARTNOINTR:
530 regs->d0 = regs->orig_d0;
534 case -ERESTART_RESTARTBLOCK:
535 regs->d0 = __NR_restart_syscall;
541 /* if there's no signal to deliver, we just put the saved sigmask
543 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
544 clear_thread_flag(TIF_RESTORE_SIGMASK);
545 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
550 * notification of userspace execution resumption
551 * - triggered by current->work.notify_resume
553 asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags)
555 /* Pending single-step? */
556 if (thread_info_flags & _TIF_SINGLESTEP) {
557 #ifndef CONFIG_MN10300_USING_JTAG
558 regs->epsw |= EPSW_T;
559 clear_thread_flag(TIF_SINGLESTEP);
561 BUG(); /* no h/w single-step if using JTAG unit */
565 /* deal with pending signal delivery */
566 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
569 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
570 clear_thread_flag(TIF_NOTIFY_RESUME);
571 tracehook_notify_resume(__frame);