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>
35 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
37 struct fdpic_func_descriptor {
43 * Atomically swap in the new signal mask, and wait for a signal.
46 sys_sigsuspend(old_sigset_t mask,
47 unsigned long r5, unsigned long r6, unsigned long r7,
48 struct pt_regs __regs)
51 spin_lock_irq(¤t->sighand->siglock);
52 current->saved_sigmask = current->blocked;
53 siginitset(¤t->blocked, mask);
55 spin_unlock_irq(¤t->sighand->siglock);
57 current->state = TASK_INTERRUPTIBLE;
59 set_thread_flag(TIF_RESTORE_SIGMASK);
60 return -ERESTARTNOHAND;
64 sys_sigaction(int sig, const struct old_sigaction __user *act,
65 struct old_sigaction __user *oact)
67 struct k_sigaction new_ka, old_ka;
72 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
73 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
74 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
76 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
77 __get_user(mask, &act->sa_mask);
78 siginitset(&new_ka.sa.sa_mask, mask);
81 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
84 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
85 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
86 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
88 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
89 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
96 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
97 unsigned long r6, unsigned long r7,
98 struct pt_regs __regs)
100 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
102 return do_sigaltstack(uss, uoss, regs->regs[15]);
107 * Do a signal return; undo the signal stack.
110 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
111 #if defined(CONFIG_CPU_SH2)
112 #define TRAP_NOARG 0xc320 /* Syscall w/no args (NR in R3) */
114 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) */
116 #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
120 struct sigcontext sc;
121 unsigned long extramask[_NSIG_WORDS-1];
133 static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
135 struct task_struct *tsk = current;
137 if (!(current_cpu_data.flags & CPU_HAS_FPU))
141 return __copy_from_user(&tsk->thread.fpu.hard, &sc->sc_fpregs[0],
142 sizeof(long)*(16*2+2));
145 static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
146 struct pt_regs *regs)
148 struct task_struct *tsk = current;
150 if (!(current_cpu_data.flags & CPU_HAS_FPU))
154 __put_user(0, &sc->sc_ownedfp);
158 __put_user(1, &sc->sc_ownedfp);
160 /* This will cause a "finit" to be triggered by the next
161 attempted FPU operation by the 'current' process.
165 unlazy_fpu(tsk, regs);
166 return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.fpu.hard,
167 sizeof(long)*(16*2+2));
169 #endif /* CONFIG_SH_FPU */
172 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
174 unsigned int err = 0;
176 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
178 COPY(regs[2]); COPY(regs[3]);
179 COPY(regs[4]); COPY(regs[5]);
180 COPY(regs[6]); COPY(regs[7]);
181 COPY(regs[8]); COPY(regs[9]);
182 COPY(regs[10]); COPY(regs[11]);
183 COPY(regs[12]); COPY(regs[13]);
184 COPY(regs[14]); COPY(regs[15]);
185 COPY(gbr); COPY(mach);
186 COPY(macl); COPY(pr);
191 if (current_cpu_data.flags & CPU_HAS_FPU) {
193 struct task_struct *tsk = current;
195 regs->sr |= SR_FD; /* Release FPU */
196 clear_fpu(tsk, regs);
198 __get_user (owned_fp, &sc->sc_ownedfp);
200 err |= restore_sigcontext_fpu(sc);
204 regs->tra = -1; /* disable syscall checks */
205 err |= __get_user(*r0_p, &sc->sc_regs[0]);
209 asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
210 unsigned long r6, unsigned long r7,
211 struct pt_regs __regs)
213 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
214 struct sigframe __user *frame = (struct sigframe __user *)regs->regs[15];
218 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
221 if (__get_user(set.sig[0], &frame->sc.oldmask)
223 && __copy_from_user(&set.sig[1], &frame->extramask,
224 sizeof(frame->extramask))))
227 sigdelsetmask(&set, ~_BLOCKABLE);
229 spin_lock_irq(¤t->sighand->siglock);
230 current->blocked = set;
232 spin_unlock_irq(¤t->sighand->siglock);
234 if (restore_sigcontext(regs, &frame->sc, &r0))
239 force_sig(SIGSEGV, current);
243 asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
244 unsigned long r6, unsigned long r7,
245 struct pt_regs __regs)
247 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
248 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15];
253 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
256 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
259 sigdelsetmask(&set, ~_BLOCKABLE);
260 spin_lock_irq(¤t->sighand->siglock);
261 current->blocked = set;
263 spin_unlock_irq(¤t->sighand->siglock);
265 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
268 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
270 /* It is more difficult to avoid calling this function than to
271 call it and ignore errors. */
272 do_sigaltstack((const stack_t __user *)&st, NULL, (unsigned long)frame);
277 force_sig(SIGSEGV, current);
282 * Set up a signal frame.
286 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
291 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
292 COPY(regs[0]); COPY(regs[1]);
293 COPY(regs[2]); COPY(regs[3]);
294 COPY(regs[4]); COPY(regs[5]);
295 COPY(regs[6]); COPY(regs[7]);
296 COPY(regs[8]); COPY(regs[9]);
297 COPY(regs[10]); COPY(regs[11]);
298 COPY(regs[12]); COPY(regs[13]);
299 COPY(regs[14]); COPY(regs[15]);
300 COPY(gbr); COPY(mach);
301 COPY(macl); COPY(pr);
306 err |= save_sigcontext_fpu(sc, regs);
309 /* non-iBCS2 extensions.. */
310 err |= __put_user(mask, &sc->oldmask);
316 * Determine which stack to use..
318 static inline void __user *
319 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
321 if (ka->sa.sa_flags & SA_ONSTACK) {
322 if (sas_ss_flags(sp) == 0)
323 sp = current->sas_ss_sp + current->sas_ss_size;
326 return (void __user *)((sp - frame_size) & -8ul);
329 /* These symbols are defined with the addresses in the vsyscall page.
330 See vsyscall-trapa.S. */
331 extern void __user __kernel_sigreturn;
332 extern void __user __kernel_rt_sigreturn;
334 static int setup_frame(int sig, struct k_sigaction *ka,
335 sigset_t *set, struct pt_regs *regs)
337 struct sigframe __user *frame;
341 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
343 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
346 signal = current_thread_info()->exec_domain
347 && current_thread_info()->exec_domain->signal_invmap
349 ? current_thread_info()->exec_domain->signal_invmap[sig]
352 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
355 err |= __copy_to_user(frame->extramask, &set->sig[1],
356 sizeof(frame->extramask));
358 /* Set up to return from userspace. If provided, use a stub
359 already in userspace. */
360 if (ka->sa.sa_flags & SA_RESTORER) {
361 regs->pr = (unsigned long) ka->sa.sa_restorer;
362 #ifdef CONFIG_VSYSCALL
363 } else if (likely(current->mm->context.vdso)) {
364 regs->pr = VDSO_SYM(&__kernel_sigreturn);
367 /* Generate return code (system call to sigreturn) */
368 err |= __put_user(MOVW(7), &frame->retcode[0]);
369 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
370 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
371 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
372 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
373 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
374 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
375 err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
376 regs->pr = (unsigned long) frame->retcode;
377 flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
383 /* Set up registers for signal handler */
384 regs->regs[15] = (unsigned long) frame;
385 regs->regs[4] = signal; /* Arg for signal handler */
387 regs->regs[6] = (unsigned long) &frame->sc;
389 if (current->personality & FDPIC_FUNCPTRS) {
390 struct fdpic_func_descriptor __user *funcptr =
391 (struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
393 __get_user(regs->pc, &funcptr->text);
394 __get_user(regs->regs[12], &funcptr->GOT);
396 regs->pc = (unsigned long)ka->sa.sa_handler;
400 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
401 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
406 force_sigsegv(sig, current);
410 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
411 sigset_t *set, struct pt_regs *regs)
413 struct rt_sigframe __user *frame;
417 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
419 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
422 signal = current_thread_info()->exec_domain
423 && current_thread_info()->exec_domain->signal_invmap
425 ? current_thread_info()->exec_domain->signal_invmap[sig]
428 err |= copy_siginfo_to_user(&frame->info, info);
430 /* Create the ucontext. */
431 err |= __put_user(0, &frame->uc.uc_flags);
432 err |= __put_user(0, &frame->uc.uc_link);
433 err |= __put_user((void *)current->sas_ss_sp,
434 &frame->uc.uc_stack.ss_sp);
435 err |= __put_user(sas_ss_flags(regs->regs[15]),
436 &frame->uc.uc_stack.ss_flags);
437 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
438 err |= setup_sigcontext(&frame->uc.uc_mcontext,
440 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
442 /* Set up to return from userspace. If provided, use a stub
443 already in userspace. */
444 if (ka->sa.sa_flags & SA_RESTORER) {
445 regs->pr = (unsigned long) ka->sa.sa_restorer;
446 #ifdef CONFIG_VSYSCALL
447 } else if (likely(current->mm->context.vdso)) {
448 regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
451 /* Generate return code (system call to rt_sigreturn) */
452 err |= __put_user(MOVW(7), &frame->retcode[0]);
453 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
454 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
455 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
456 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
457 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
458 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
459 err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
460 regs->pr = (unsigned long) frame->retcode;
466 /* Set up registers for signal handler */
467 regs->regs[15] = (unsigned long) frame;
468 regs->regs[4] = signal; /* Arg for signal handler */
469 regs->regs[5] = (unsigned long) &frame->info;
470 regs->regs[6] = (unsigned long) &frame->uc;
472 if (current->personality & FDPIC_FUNCPTRS) {
473 struct fdpic_func_descriptor __user *funcptr =
474 (struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
476 __get_user(regs->pc, &funcptr->text);
477 __get_user(regs->regs[12], &funcptr->GOT);
479 regs->pc = (unsigned long)ka->sa.sa_handler;
483 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
484 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
486 flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
491 force_sigsegv(sig, current);
496 * OK, we're invoking a handler
500 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
501 sigset_t *oldset, struct pt_regs *regs, unsigned int save_r0)
505 /* Are we from a system call? */
506 if (regs->tra >= 0) {
507 /* If so, check system call restarting.. */
508 switch (regs->regs[0]) {
509 case -ERESTART_RESTARTBLOCK:
510 case -ERESTARTNOHAND:
511 no_system_call_restart:
512 regs->regs[0] = -EINTR;
516 if (!(ka->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(
522 ctrl_inw(regs->pc - 4));
527 /* Set up the stack frame */
528 if (ka->sa.sa_flags & SA_SIGINFO)
529 ret = setup_rt_frame(sig, ka, info, oldset, regs);
531 ret = setup_frame(sig, ka, oldset, regs);
533 if (ka->sa.sa_flags & SA_ONESHOT)
534 ka->sa.sa_handler = SIG_DFL;
537 spin_lock_irq(¤t->sighand->siglock);
538 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
539 if (!(ka->sa.sa_flags & SA_NODEFER))
540 sigaddset(¤t->blocked,sig);
542 spin_unlock_irq(¤t->sighand->siglock);
549 * Note that 'init' is a special process: it doesn't get signals it doesn't
550 * want to handle. Thus you cannot kill init even with a SIGKILL even by
553 * Note that we go through the signals twice: once to check the signals that
554 * the kernel can handle, and then we build all the user-level signal handling
555 * stack-frames in one go after that.
557 static void do_signal(struct pt_regs *regs, unsigned int save_r0)
561 struct k_sigaction ka;
565 * We want the common case to go fast, which
566 * is why we may in certain cases get here from
567 * kernel mode. Just return without doing anything
570 if (!user_mode(regs))
576 if (test_thread_flag(TIF_RESTORE_SIGMASK))
577 oldset = ¤t->saved_sigmask;
579 oldset = ¤t->blocked;
581 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
583 /* Whee! Actually deliver the signal. */
584 if (handle_signal(signr, &ka, &info, oldset,
585 regs, save_r0) == 0) {
586 /* a signal was successfully delivered; the saved
587 * sigmask will have been stored in the signal frame,
588 * and will be restored by sigreturn, so we can simply
589 * clear the TIF_RESTORE_SIGMASK flag */
590 if (test_thread_flag(TIF_RESTORE_SIGMASK))
591 clear_thread_flag(TIF_RESTORE_SIGMASK);
593 tracehook_signal_handler(signr, &info, &ka, regs,
594 test_thread_flag(TIF_SINGLESTEP));
601 /* Did we come from a system call? */
602 if (regs->tra >= 0) {
603 /* Restart the system call - no handlers present */
604 if (regs->regs[0] == -ERESTARTNOHAND ||
605 regs->regs[0] == -ERESTARTSYS ||
606 regs->regs[0] == -ERESTARTNOINTR) {
607 regs->regs[0] = save_r0;
608 regs->pc -= instruction_size(ctrl_inw(regs->pc - 4));
609 } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
610 regs->pc -= instruction_size(ctrl_inw(regs->pc - 4));
611 regs->regs[3] = __NR_restart_syscall;
615 /* if there's no signal to deliver, we just put the saved sigmask
617 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
618 clear_thread_flag(TIF_RESTORE_SIGMASK);
619 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
623 asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
624 unsigned long thread_info_flags)
626 /* deal with pending signal delivery */
627 if (thread_info_flags & _TIF_SIGPENDING)
628 do_signal(regs, save_r0);
630 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
631 clear_thread_flag(TIF_NOTIFY_RESUME);
632 tracehook_notify_resume(regs);