2 * linux/arch/sh/kernel/signal.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
8 * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
11 #include <linux/sched.h>
13 #include <linux/smp.h>
14 #include <linux/kernel.h>
15 #include <linux/signal.h>
16 #include <linux/errno.h>
17 #include <linux/wait.h>
18 #include <linux/ptrace.h>
19 #include <linux/unistd.h>
20 #include <linux/stddef.h>
21 #include <linux/tty.h>
22 #include <linux/elf.h>
23 #include <linux/personality.h>
24 #include <linux/binfmts.h>
25 #include <linux/freezer.h>
27 #include <linux/tracehook.h>
28 #include <asm/system.h>
29 #include <asm/ucontext.h>
30 #include <asm/uaccess.h>
31 #include <asm/pgtable.h>
32 #include <asm/cacheflush.h>
33 #include <asm/syscalls.h>
36 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
38 struct fdpic_func_descriptor {
44 * Atomically swap in the new signal mask, and wait for a signal.
47 sys_sigsuspend(old_sigset_t mask,
48 unsigned long r5, unsigned long r6, unsigned long r7,
49 struct pt_regs __regs)
52 spin_lock_irq(¤t->sighand->siglock);
53 current->saved_sigmask = current->blocked;
54 siginitset(¤t->blocked, mask);
56 spin_unlock_irq(¤t->sighand->siglock);
58 current->state = TASK_INTERRUPTIBLE;
60 set_thread_flag(TIF_RESTORE_SIGMASK);
61 return -ERESTARTNOHAND;
65 sys_sigaction(int sig, const struct old_sigaction __user *act,
66 struct old_sigaction __user *oact)
68 struct k_sigaction new_ka, old_ka;
73 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
74 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
75 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
77 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
78 __get_user(mask, &act->sa_mask);
79 siginitset(&new_ka.sa.sa_mask, mask);
82 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
85 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
86 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
87 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
89 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
90 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
97 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
98 unsigned long r6, unsigned long r7,
99 struct pt_regs __regs)
101 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
103 return do_sigaltstack(uss, uoss, regs->regs[15]);
108 * Do a signal return; undo the signal stack.
111 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
112 #if defined(CONFIG_CPU_SH2)
113 #define TRAP_NOARG 0xc320 /* Syscall w/no args (NR in R3) */
115 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) */
117 #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
121 struct sigcontext sc;
122 unsigned long extramask[_NSIG_WORDS-1];
134 static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
136 struct task_struct *tsk = current;
138 if (!(current_cpu_data.flags & CPU_HAS_FPU))
142 return __copy_from_user(&tsk->thread.fpu.hard, &sc->sc_fpregs[0],
143 sizeof(long)*(16*2+2));
146 static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
147 struct pt_regs *regs)
149 struct task_struct *tsk = current;
151 if (!(current_cpu_data.flags & CPU_HAS_FPU))
155 __put_user(0, &sc->sc_ownedfp);
159 __put_user(1, &sc->sc_ownedfp);
161 /* This will cause a "finit" to be triggered by the next
162 attempted FPU operation by the 'current' process.
166 unlazy_fpu(tsk, regs);
167 return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.fpu.hard,
168 sizeof(long)*(16*2+2));
170 #endif /* CONFIG_SH_FPU */
173 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
175 unsigned int err = 0;
177 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
179 COPY(regs[2]); COPY(regs[3]);
180 COPY(regs[4]); COPY(regs[5]);
181 COPY(regs[6]); COPY(regs[7]);
182 COPY(regs[8]); COPY(regs[9]);
183 COPY(regs[10]); COPY(regs[11]);
184 COPY(regs[12]); COPY(regs[13]);
185 COPY(regs[14]); COPY(regs[15]);
186 COPY(gbr); COPY(mach);
187 COPY(macl); COPY(pr);
192 if (current_cpu_data.flags & CPU_HAS_FPU) {
194 struct task_struct *tsk = current;
196 regs->sr |= SR_FD; /* Release FPU */
197 clear_fpu(tsk, regs);
199 __get_user (owned_fp, &sc->sc_ownedfp);
201 err |= restore_sigcontext_fpu(sc);
205 regs->tra = -1; /* disable syscall checks */
206 err |= __get_user(*r0_p, &sc->sc_regs[0]);
210 asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
211 unsigned long r6, unsigned long r7,
212 struct pt_regs __regs)
214 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
215 struct sigframe __user *frame = (struct sigframe __user *)regs->regs[15];
219 /* Always make any pending restarted system calls return -EINTR */
220 current_thread_info()->restart_block.fn = do_no_restart_syscall;
222 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
225 if (__get_user(set.sig[0], &frame->sc.oldmask)
227 && __copy_from_user(&set.sig[1], &frame->extramask,
228 sizeof(frame->extramask))))
231 sigdelsetmask(&set, ~_BLOCKABLE);
233 spin_lock_irq(¤t->sighand->siglock);
234 current->blocked = set;
236 spin_unlock_irq(¤t->sighand->siglock);
238 if (restore_sigcontext(regs, &frame->sc, &r0))
243 force_sig(SIGSEGV, current);
247 asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
248 unsigned long r6, unsigned long r7,
249 struct pt_regs __regs)
251 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
252 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15];
256 /* Always make any pending restarted system calls return -EINTR */
257 current_thread_info()->restart_block.fn = do_no_restart_syscall;
259 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
262 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
265 sigdelsetmask(&set, ~_BLOCKABLE);
266 spin_lock_irq(¤t->sighand->siglock);
267 current->blocked = set;
269 spin_unlock_irq(¤t->sighand->siglock);
271 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
274 if (do_sigaltstack(&frame->uc.uc_stack, NULL,
275 regs->regs[15]) == -EFAULT)
281 force_sig(SIGSEGV, current);
286 * Set up a signal frame.
290 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
295 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
296 COPY(regs[0]); COPY(regs[1]);
297 COPY(regs[2]); COPY(regs[3]);
298 COPY(regs[4]); COPY(regs[5]);
299 COPY(regs[6]); COPY(regs[7]);
300 COPY(regs[8]); COPY(regs[9]);
301 COPY(regs[10]); COPY(regs[11]);
302 COPY(regs[12]); COPY(regs[13]);
303 COPY(regs[14]); COPY(regs[15]);
304 COPY(gbr); COPY(mach);
305 COPY(macl); COPY(pr);
310 err |= save_sigcontext_fpu(sc, regs);
313 /* non-iBCS2 extensions.. */
314 err |= __put_user(mask, &sc->oldmask);
320 * Determine which stack to use..
322 static inline void __user *
323 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
325 if (ka->sa.sa_flags & SA_ONSTACK) {
326 if (sas_ss_flags(sp) == 0)
327 sp = current->sas_ss_sp + current->sas_ss_size;
330 return (void __user *)((sp - frame_size) & -8ul);
333 /* These symbols are defined with the addresses in the vsyscall page.
334 See vsyscall-trapa.S. */
335 extern void __user __kernel_sigreturn;
336 extern void __user __kernel_rt_sigreturn;
338 static int setup_frame(int sig, struct k_sigaction *ka,
339 sigset_t *set, struct pt_regs *regs)
341 struct sigframe __user *frame;
345 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
347 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
350 signal = current_thread_info()->exec_domain
351 && current_thread_info()->exec_domain->signal_invmap
353 ? current_thread_info()->exec_domain->signal_invmap[sig]
356 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
359 err |= __copy_to_user(frame->extramask, &set->sig[1],
360 sizeof(frame->extramask));
362 /* Set up to return from userspace. If provided, use a stub
363 already in userspace. */
364 if (ka->sa.sa_flags & SA_RESTORER) {
365 regs->pr = (unsigned long) ka->sa.sa_restorer;
366 #ifdef CONFIG_VSYSCALL
367 } else if (likely(current->mm->context.vdso)) {
368 regs->pr = VDSO_SYM(&__kernel_sigreturn);
371 /* Generate return code (system call to sigreturn) */
372 err |= __put_user(MOVW(7), &frame->retcode[0]);
373 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
374 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
375 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
376 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
377 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
378 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
379 err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
380 regs->pr = (unsigned long) frame->retcode;
381 flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
387 /* Set up registers for signal handler */
388 regs->regs[15] = (unsigned long) frame;
389 regs->regs[4] = signal; /* Arg for signal handler */
391 regs->regs[6] = (unsigned long) &frame->sc;
393 if (current->personality & FDPIC_FUNCPTRS) {
394 struct fdpic_func_descriptor __user *funcptr =
395 (struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
397 __get_user(regs->pc, &funcptr->text);
398 __get_user(regs->regs[12], &funcptr->GOT);
400 regs->pc = (unsigned long)ka->sa.sa_handler;
404 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
405 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
410 force_sigsegv(sig, current);
414 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
415 sigset_t *set, struct pt_regs *regs)
417 struct rt_sigframe __user *frame;
421 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
423 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
426 signal = current_thread_info()->exec_domain
427 && current_thread_info()->exec_domain->signal_invmap
429 ? current_thread_info()->exec_domain->signal_invmap[sig]
432 err |= copy_siginfo_to_user(&frame->info, info);
434 /* Create the ucontext. */
435 err |= __put_user(0, &frame->uc.uc_flags);
436 err |= __put_user(NULL, &frame->uc.uc_link);
437 err |= __put_user((void *)current->sas_ss_sp,
438 &frame->uc.uc_stack.ss_sp);
439 err |= __put_user(sas_ss_flags(regs->regs[15]),
440 &frame->uc.uc_stack.ss_flags);
441 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
442 err |= setup_sigcontext(&frame->uc.uc_mcontext,
444 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
446 /* Set up to return from userspace. If provided, use a stub
447 already in userspace. */
448 if (ka->sa.sa_flags & SA_RESTORER) {
449 regs->pr = (unsigned long) ka->sa.sa_restorer;
450 #ifdef CONFIG_VSYSCALL
451 } else if (likely(current->mm->context.vdso)) {
452 regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
455 /* Generate return code (system call to rt_sigreturn) */
456 err |= __put_user(MOVW(7), &frame->retcode[0]);
457 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
458 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
459 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
460 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
461 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
462 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
463 err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
464 regs->pr = (unsigned long) frame->retcode;
470 /* Set up registers for signal handler */
471 regs->regs[15] = (unsigned long) frame;
472 regs->regs[4] = signal; /* Arg for signal handler */
473 regs->regs[5] = (unsigned long) &frame->info;
474 regs->regs[6] = (unsigned long) &frame->uc;
476 if (current->personality & FDPIC_FUNCPTRS) {
477 struct fdpic_func_descriptor __user *funcptr =
478 (struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
480 __get_user(regs->pc, &funcptr->text);
481 __get_user(regs->regs[12], &funcptr->GOT);
483 regs->pc = (unsigned long)ka->sa.sa_handler;
487 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
488 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
490 flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
495 force_sigsegv(sig, current);
500 handle_syscall_restart(unsigned long save_r0, struct pt_regs *regs,
501 struct sigaction *sa)
503 /* If we're not from a syscall, bail out */
507 /* check for system call restart.. */
508 switch (regs->regs[0]) {
509 case -ERESTART_RESTARTBLOCK:
510 case -ERESTARTNOHAND:
511 no_system_call_restart:
512 regs->regs[0] = -EINTR;
516 if (!(sa->sa_flags & SA_RESTART))
517 goto no_system_call_restart;
519 case -ERESTARTNOINTR:
520 regs->regs[0] = save_r0;
521 regs->pc -= instruction_size(ctrl_inw(regs->pc - 4));
527 * OK, we're invoking a handler
530 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
531 sigset_t *oldset, struct pt_regs *regs, unsigned int save_r0)
535 /* Set up the stack frame */
536 if (ka->sa.sa_flags & SA_SIGINFO)
537 ret = setup_rt_frame(sig, ka, info, oldset, regs);
539 ret = setup_frame(sig, ka, oldset, regs);
541 if (ka->sa.sa_flags & SA_ONESHOT)
542 ka->sa.sa_handler = SIG_DFL;
545 spin_lock_irq(¤t->sighand->siglock);
546 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
547 if (!(ka->sa.sa_flags & SA_NODEFER))
548 sigaddset(¤t->blocked,sig);
550 spin_unlock_irq(¤t->sighand->siglock);
557 * Note that 'init' is a special process: it doesn't get signals it doesn't
558 * want to handle. Thus you cannot kill init even with a SIGKILL even by
561 * Note that we go through the signals twice: once to check the signals that
562 * the kernel can handle, and then we build all the user-level signal handling
563 * stack-frames in one go after that.
565 static void do_signal(struct pt_regs *regs, unsigned int save_r0)
569 struct k_sigaction ka;
573 * We want the common case to go fast, which
574 * is why we may in certain cases get here from
575 * kernel mode. Just return without doing anything
578 if (!user_mode(regs))
584 if (test_thread_flag(TIF_RESTORE_SIGMASK))
585 oldset = ¤t->saved_sigmask;
587 oldset = ¤t->blocked;
589 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
591 handle_syscall_restart(save_r0, regs, &ka.sa);
593 /* Whee! Actually deliver the signal. */
594 if (handle_signal(signr, &ka, &info, oldset,
595 regs, save_r0) == 0) {
596 /* a signal was successfully delivered; the saved
597 * sigmask will have been stored in the signal frame,
598 * and will be restored by sigreturn, so we can simply
599 * clear the TIF_RESTORE_SIGMASK flag */
600 if (test_thread_flag(TIF_RESTORE_SIGMASK))
601 clear_thread_flag(TIF_RESTORE_SIGMASK);
603 tracehook_signal_handler(signr, &info, &ka, regs,
604 test_thread_flag(TIF_SINGLESTEP));
611 /* Did we come from a system call? */
612 if (regs->tra >= 0) {
613 /* Restart the system call - no handlers present */
614 if (regs->regs[0] == -ERESTARTNOHAND ||
615 regs->regs[0] == -ERESTARTSYS ||
616 regs->regs[0] == -ERESTARTNOINTR) {
617 regs->regs[0] = save_r0;
618 regs->pc -= instruction_size(ctrl_inw(regs->pc - 4));
619 } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
620 regs->pc -= instruction_size(ctrl_inw(regs->pc - 4));
621 regs->regs[3] = __NR_restart_syscall;
625 /* if there's no signal to deliver, we just put the saved sigmask
627 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
628 clear_thread_flag(TIF_RESTORE_SIGMASK);
629 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
633 asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
634 unsigned long thread_info_flags)
636 /* deal with pending signal delivery */
637 if (thread_info_flags & _TIF_SIGPENDING)
638 do_signal(regs, save_r0);
640 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
641 clear_thread_flag(TIF_NOTIFY_RESUME);
642 tracehook_notify_resume(regs);