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 <asm/cacheflush.h>
27 #include <asm/ucontext.h>
28 #include <asm/uaccess.h>
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)
42 spin_lock_irq(¤t->sighand->siglock);
43 current->saved_sigmask = current->blocked;
44 siginitset(¤t->blocked, mask);
46 spin_unlock_irq(¤t->sighand->siglock);
48 current->state = TASK_INTERRUPTIBLE;
50 set_thread_flag(TIF_RESTORE_SIGMASK);
51 return -ERESTARTNOHAND;
55 * set signal action syscall
57 asmlinkage long 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;
66 if (verify_area(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))
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);
78 if (verify_area(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))
82 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
83 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
90 * set alternate signal stack syscall
92 asmlinkage long sys_sigaltstack(const stack_t __user *uss, stack_t *uoss)
94 return do_sigaltstack(uss, uoss, __frame->sp);
98 * do a signal return; undo the signal stack.
100 static int restore_sigcontext(struct pt_regs *regs,
101 struct sigcontext __user *sc, long *_d0)
103 unsigned int err = 0;
105 if (is_using_fpu(current))
106 fpu_kill_state(current);
108 #define COPY(x) err |= __get_user(regs->x, &sc->x)
109 COPY(d1); COPY(d2); COPY(d3);
110 COPY(a0); COPY(a1); COPY(a2); COPY(a3);
111 COPY(e0); COPY(e1); COPY(e2); COPY(e3);
112 COPY(e4); COPY(e5); COPY(e6); COPY(e7);
113 COPY(lar); COPY(lir);
114 COPY(mdr); COPY(mdrq);
115 COPY(mcvf); COPY(mcrl); COPY(mcrh);
120 unsigned int tmpflags;
121 #ifndef CONFIG_MN10300_USING_JTAG
122 #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
125 #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
128 err |= __get_user(tmpflags, &sc->epsw);
129 regs->epsw = (regs->epsw & ~USER_EPSW) |
130 (tmpflags & USER_EPSW);
131 regs->orig_d0 = -1; /* disable syscall checks */
135 struct fpucontext *buf;
136 err |= __get_user(buf, &sc->fpucontext);
138 if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
140 err |= fpu_restore_sigcontext(buf);
144 err |= __get_user(*_d0, &sc->d0);
152 * standard signal return syscall
154 asmlinkage long sys_sigreturn(void)
156 struct sigframe __user *frame = (struct sigframe __user *) __frame->sp;
160 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
162 if (__get_user(set.sig[0], &frame->sc.oldmask))
165 if (_NSIG_WORDS > 1 &&
166 __copy_from_user(&set.sig[1], &frame->extramask,
167 sizeof(frame->extramask)))
170 sigdelsetmask(&set, ~_BLOCKABLE);
171 spin_lock_irq(¤t->sighand->siglock);
172 current->blocked = set;
174 spin_unlock_irq(¤t->sighand->siglock);
176 if (restore_sigcontext(__frame, &frame->sc, &d0))
182 force_sig(SIGSEGV, current);
187 * realtime signal return syscall
189 asmlinkage long sys_rt_sigreturn(void)
191 struct rt_sigframe __user *frame =
192 (struct rt_sigframe __user *) __frame->sp;
196 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
198 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
201 sigdelsetmask(&set, ~_BLOCKABLE);
202 spin_lock_irq(¤t->sighand->siglock);
203 current->blocked = set;
205 spin_unlock_irq(¤t->sighand->siglock);
207 if (restore_sigcontext(__frame, &frame->uc.uc_mcontext, &d0))
210 if (do_sigaltstack(&frame->uc.uc_stack, NULL, __frame->sp) == -EFAULT)
216 force_sig(SIGSEGV, current);
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,
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);
241 tmp = fpu_setup_sigcontext(fpuctx);
245 err |= __put_user(tmp ? fpuctx : NULL, &sc->fpucontext);
247 /* non-iBCS2 extensions.. */
248 err |= __put_user(mask, &sc->oldmask);
254 * determine which stack to use..
256 static inline void __user *get_sigframe(struct k_sigaction *ka,
257 struct pt_regs *regs,
262 /* default to using normal stack */
265 /* this is the X/Open sanctioned signal stack switching. */
266 if (ka->sa.sa_flags & SA_ONSTACK) {
267 if (!on_sig_stack(sp))
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;
283 frame = get_sigframe(ka, regs, sizeof(*frame));
285 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
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)
298 if (setup_sigcontext(&frame->sc, &frame->fpuctx, regs, set->sig[0]))
301 if (_NSIG_WORDS > 1) {
302 if (__copy_to_user(frame->extramask, &set->sig[1],
303 sizeof(frame->extramask)))
307 /* set up to return from userspace. If provided, use a stub already in
309 if (ka->sa.sa_flags & SA_RESTORER) {
310 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
313 if (__put_user((void (*)(void))frame->retcode,
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)))
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;
331 regs->d1 = (unsigned long) &frame->sc;
335 /* the tracer may want to single-step inside the handler */
336 if (test_thread_flag(TIF_SINGLESTEP))
337 ptrace_notify(SIGTRAP);
340 printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
341 sig, current->comm, current->pid, frame, regs->pc,
348 force_sig(SIGSEGV, current);
353 * set up a realtime signal frame
355 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
356 sigset_t *set, struct pt_regs *regs)
358 struct rt_sigframe __user *frame;
361 frame = get_sigframe(ka, regs, sizeof(*frame));
363 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
368 current_thread_info()->exec_domain &&
369 current_thread_info()->exec_domain->signal_invmap)
370 rsig = current_thread_info()->exec_domain->signal_invmap[sig];
372 if (__put_user(rsig, &frame->sig) ||
373 __put_user(&frame->info, &frame->pinfo) ||
374 __put_user(&frame->uc, &frame->puc) ||
375 copy_siginfo_to_user(&frame->info, info))
378 /* create the ucontext. */
379 if (__put_user(0, &frame->uc.uc_flags) ||
380 __put_user(0, &frame->uc.uc_link) ||
381 __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp) ||
382 __put_user(sas_ss_flags(regs->sp), &frame->uc.uc_stack.ss_flags) ||
383 __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size) ||
384 setup_sigcontext(&frame->uc.uc_mcontext,
385 &frame->fpuctx, regs, set->sig[0]) ||
386 __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
389 /* set up to return from userspace. If provided, use a stub already in
391 if (ka->sa.sa_flags & SA_RESTORER) {
392 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
395 if (__put_user((void(*)(void))frame->retcode,
397 /* This is mov $,d0; syscall 0 */
398 __put_user(0x2c, (char *)(frame->retcode + 0)) ||
399 __put_user(__NR_rt_sigreturn,
400 (char *)(frame->retcode + 1)) ||
401 __put_user(0x00, (char *)(frame->retcode + 2)) ||
402 __put_user(0xf0, (char *)(frame->retcode + 3)) ||
403 __put_user(0xe0, (char *)(frame->retcode + 4)))
406 flush_icache_range((u_long) frame->retcode,
407 (u_long) frame->retcode + 5);
410 /* Set up registers for signal handler */
411 regs->sp = (unsigned long) frame;
412 regs->pc = (unsigned long) ka->sa.sa_handler;
414 regs->d1 = (long) &frame->info;
418 /* the tracer may want to single-step inside the handler */
419 if (test_thread_flag(TIF_SINGLESTEP))
420 ptrace_notify(SIGTRAP);
423 printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
424 sig, current->comm, current->pid, frame, regs->pc,
431 force_sig(SIGSEGV, current);
436 * handle the actual delivery of a signal to userspace
438 static int handle_signal(int sig,
439 siginfo_t *info, struct k_sigaction *ka,
440 sigset_t *oldset, struct pt_regs *regs)
444 /* Are we from a system call? */
445 if (regs->orig_d0 >= 0) {
446 /* If so, check system call restarting.. */
448 case -ERESTART_RESTARTBLOCK:
449 case -ERESTARTNOHAND:
454 if (!(ka->sa.sa_flags & SA_RESTART)) {
460 case -ERESTARTNOINTR:
461 regs->d0 = regs->orig_d0;
466 /* Set up the stack frame */
467 if (ka->sa.sa_flags & SA_SIGINFO)
468 ret = setup_rt_frame(sig, ka, info, oldset, regs);
470 ret = setup_frame(sig, ka, oldset, regs);
473 spin_lock_irq(¤t->sighand->siglock);
474 sigorsets(¤t->blocked, ¤t->blocked,
476 if (!(ka->sa.sa_flags & SA_NODEFER))
477 sigaddset(¤t->blocked, sig);
479 spin_unlock_irq(¤t->sighand->siglock);
486 * handle a potential signal
488 static void do_signal(struct pt_regs *regs)
490 struct k_sigaction ka;
495 /* we want the common case to go fast, which is why we may in certain
496 * cases get here from kernel mode */
497 if (!user_mode(regs))
500 if (test_thread_flag(TIF_RESTORE_SIGMASK))
501 oldset = ¤t->saved_sigmask;
503 oldset = ¤t->blocked;
505 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
507 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
508 /* a signal was successfully delivered; the saved
509 * sigmask will have been stored in the signal frame,
510 * and will be restored by sigreturn, so we can simply
511 * clear the TIF_RESTORE_SIGMASK flag */
512 if (test_thread_flag(TIF_RESTORE_SIGMASK))
513 clear_thread_flag(TIF_RESTORE_SIGMASK);
519 /* did we come from a system call? */
520 if (regs->orig_d0 >= 0) {
521 /* restart the system call - no handlers present */
523 case -ERESTARTNOHAND:
525 case -ERESTARTNOINTR:
526 regs->d0 = regs->orig_d0;
530 case -ERESTART_RESTARTBLOCK:
531 regs->d0 = __NR_restart_syscall;
537 /* if there's no signal to deliver, we just put the saved sigmask
539 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
540 clear_thread_flag(TIF_RESTORE_SIGMASK);
541 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
546 * notification of userspace execution resumption
547 * - triggered by current->work.notify_resume
549 asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags)
551 /* Pending single-step? */
552 if (thread_info_flags & _TIF_SINGLESTEP) {
553 #ifndef CONFIG_MN10300_USING_JTAG
554 regs->epsw |= EPSW_T;
555 clear_thread_flag(TIF_SINGLESTEP);
557 BUG(); /* no h/w single-step if using JTAG unit */
561 /* deal with pending signal delivery */
562 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))