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 <asm/system.h>
28 #include <asm/ucontext.h>
29 #include <asm/uaccess.h>
30 #include <asm/pgtable.h>
31 #include <asm/cacheflush.h>
34 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
36 struct fdpic_func_descriptor {
42 * Atomically swap in the new signal mask, and wait for a signal.
45 sys_sigsuspend(old_sigset_t mask,
46 unsigned long r5, unsigned long r6, unsigned long r7,
47 struct pt_regs __regs)
50 spin_lock_irq(¤t->sighand->siglock);
51 current->saved_sigmask = current->blocked;
52 siginitset(¤t->blocked, mask);
54 spin_unlock_irq(¤t->sighand->siglock);
56 current->state = TASK_INTERRUPTIBLE;
58 set_thread_flag(TIF_RESTORE_SIGMASK);
59 return -ERESTARTNOHAND;
63 sys_sigaction(int sig, const struct old_sigaction __user *act,
64 struct old_sigaction __user *oact)
66 struct k_sigaction new_ka, old_ka;
71 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
72 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
73 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
75 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
76 __get_user(mask, &act->sa_mask);
77 siginitset(&new_ka.sa.sa_mask, mask);
80 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
83 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
84 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
85 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
87 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
88 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
95 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
96 unsigned long r6, unsigned long r7,
97 struct pt_regs __regs)
99 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
101 return do_sigaltstack(uss, uoss, regs->regs[15]);
106 * Do a signal return; undo the signal stack.
109 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
110 #if defined(CONFIG_CPU_SH2)
111 #define TRAP_NOARG 0xc320 /* Syscall w/no args (NR in R3) */
113 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) */
115 #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
119 struct sigcontext sc;
120 unsigned long extramask[_NSIG_WORDS-1];
132 static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
134 struct task_struct *tsk = current;
136 if (!(current_cpu_data.flags & CPU_HAS_FPU))
140 return __copy_from_user(&tsk->thread.fpu.hard, &sc->sc_fpregs[0],
141 sizeof(long)*(16*2+2));
144 static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
145 struct pt_regs *regs)
147 struct task_struct *tsk = current;
149 if (!(current_cpu_data.flags & CPU_HAS_FPU))
153 __put_user(0, &sc->sc_ownedfp);
157 __put_user(1, &sc->sc_ownedfp);
159 /* This will cause a "finit" to be triggered by the next
160 attempted FPU operation by the 'current' process.
164 unlazy_fpu(tsk, regs);
165 return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.fpu.hard,
166 sizeof(long)*(16*2+2));
168 #endif /* CONFIG_SH_FPU */
171 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
173 unsigned int err = 0;
175 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
177 COPY(regs[2]); COPY(regs[3]);
178 COPY(regs[4]); COPY(regs[5]);
179 COPY(regs[6]); COPY(regs[7]);
180 COPY(regs[8]); COPY(regs[9]);
181 COPY(regs[10]); COPY(regs[11]);
182 COPY(regs[12]); COPY(regs[13]);
183 COPY(regs[14]); COPY(regs[15]);
184 COPY(gbr); COPY(mach);
185 COPY(macl); COPY(pr);
190 if (current_cpu_data.flags & CPU_HAS_FPU) {
192 struct task_struct *tsk = current;
194 regs->sr |= SR_FD; /* Release FPU */
195 clear_fpu(tsk, regs);
197 __get_user (owned_fp, &sc->sc_ownedfp);
199 err |= restore_sigcontext_fpu(sc);
203 regs->tra = -1; /* disable syscall checks */
204 err |= __get_user(*r0_p, &sc->sc_regs[0]);
208 asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
209 unsigned long r6, unsigned long r7,
210 struct pt_regs __regs)
212 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
213 struct sigframe __user *frame = (struct sigframe __user *)regs->regs[15];
217 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
220 if (__get_user(set.sig[0], &frame->sc.oldmask)
222 && __copy_from_user(&set.sig[1], &frame->extramask,
223 sizeof(frame->extramask))))
226 sigdelsetmask(&set, ~_BLOCKABLE);
228 spin_lock_irq(¤t->sighand->siglock);
229 current->blocked = set;
231 spin_unlock_irq(¤t->sighand->siglock);
233 if (restore_sigcontext(regs, &frame->sc, &r0))
238 force_sig(SIGSEGV, current);
242 asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
243 unsigned long r6, unsigned long r7,
244 struct pt_regs __regs)
246 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
247 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15];
252 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
255 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
258 sigdelsetmask(&set, ~_BLOCKABLE);
259 spin_lock_irq(¤t->sighand->siglock);
260 current->blocked = set;
262 spin_unlock_irq(¤t->sighand->siglock);
264 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
267 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
269 /* It is more difficult to avoid calling this function than to
270 call it and ignore errors. */
271 do_sigaltstack((const stack_t __user *)&st, NULL, (unsigned long)frame);
276 force_sig(SIGSEGV, current);
281 * Set up a signal frame.
285 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
290 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
291 COPY(regs[0]); COPY(regs[1]);
292 COPY(regs[2]); COPY(regs[3]);
293 COPY(regs[4]); COPY(regs[5]);
294 COPY(regs[6]); COPY(regs[7]);
295 COPY(regs[8]); COPY(regs[9]);
296 COPY(regs[10]); COPY(regs[11]);
297 COPY(regs[12]); COPY(regs[13]);
298 COPY(regs[14]); COPY(regs[15]);
299 COPY(gbr); COPY(mach);
300 COPY(macl); COPY(pr);
305 err |= save_sigcontext_fpu(sc, regs);
308 /* non-iBCS2 extensions.. */
309 err |= __put_user(mask, &sc->oldmask);
315 * Determine which stack to use..
317 static inline void __user *
318 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
320 if (ka->sa.sa_flags & SA_ONSTACK) {
321 if (sas_ss_flags(sp) == 0)
322 sp = current->sas_ss_sp + current->sas_ss_size;
325 return (void __user *)((sp - frame_size) & -8ul);
328 /* These symbols are defined with the addresses in the vsyscall page.
329 See vsyscall-trapa.S. */
330 extern void __user __kernel_sigreturn;
331 extern void __user __kernel_rt_sigreturn;
333 static int setup_frame(int sig, struct k_sigaction *ka,
334 sigset_t *set, struct pt_regs *regs)
336 struct sigframe __user *frame;
340 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
342 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
345 signal = current_thread_info()->exec_domain
346 && current_thread_info()->exec_domain->signal_invmap
348 ? current_thread_info()->exec_domain->signal_invmap[sig]
351 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
354 err |= __copy_to_user(frame->extramask, &set->sig[1],
355 sizeof(frame->extramask));
357 /* Set up to return from userspace. If provided, use a stub
358 already in userspace. */
359 if (ka->sa.sa_flags & SA_RESTORER) {
360 regs->pr = (unsigned long) ka->sa.sa_restorer;
361 #ifdef CONFIG_VSYSCALL
362 } else if (likely(current->mm->context.vdso)) {
363 regs->pr = VDSO_SYM(&__kernel_sigreturn);
366 /* Generate return code (system call to sigreturn) */
367 err |= __put_user(MOVW(7), &frame->retcode[0]);
368 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
369 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
370 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
371 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
372 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
373 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
374 err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
375 regs->pr = (unsigned long) frame->retcode;
376 flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
382 /* Set up registers for signal handler */
383 regs->regs[15] = (unsigned long) frame;
384 regs->regs[4] = signal; /* Arg for signal handler */
386 regs->regs[6] = (unsigned long) &frame->sc;
388 if (current->personality & FDPIC_FUNCPTRS) {
389 struct fdpic_func_descriptor __user *funcptr =
390 (struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
392 __get_user(regs->pc, &funcptr->text);
393 __get_user(regs->regs[12], &funcptr->GOT);
395 regs->pc = (unsigned long)ka->sa.sa_handler;
399 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
400 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
405 force_sigsegv(sig, current);
409 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
410 sigset_t *set, struct pt_regs *regs)
412 struct rt_sigframe __user *frame;
416 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
418 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
421 signal = current_thread_info()->exec_domain
422 && current_thread_info()->exec_domain->signal_invmap
424 ? current_thread_info()->exec_domain->signal_invmap[sig]
427 err |= copy_siginfo_to_user(&frame->info, info);
429 /* Create the ucontext. */
430 err |= __put_user(0, &frame->uc.uc_flags);
431 err |= __put_user(0, &frame->uc.uc_link);
432 err |= __put_user((void *)current->sas_ss_sp,
433 &frame->uc.uc_stack.ss_sp);
434 err |= __put_user(sas_ss_flags(regs->regs[15]),
435 &frame->uc.uc_stack.ss_flags);
436 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
437 err |= setup_sigcontext(&frame->uc.uc_mcontext,
439 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
441 /* Set up to return from userspace. If provided, use a stub
442 already in userspace. */
443 if (ka->sa.sa_flags & SA_RESTORER) {
444 regs->pr = (unsigned long) ka->sa.sa_restorer;
445 #ifdef CONFIG_VSYSCALL
446 } else if (likely(current->mm->context.vdso)) {
447 regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
450 /* Generate return code (system call to rt_sigreturn) */
451 err |= __put_user(MOVW(7), &frame->retcode[0]);
452 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
453 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
454 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
455 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
456 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
457 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
458 err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
459 regs->pr = (unsigned long) frame->retcode;
465 /* Set up registers for signal handler */
466 regs->regs[15] = (unsigned long) frame;
467 regs->regs[4] = signal; /* Arg for signal handler */
468 regs->regs[5] = (unsigned long) &frame->info;
469 regs->regs[6] = (unsigned long) &frame->uc;
471 if (current->personality & FDPIC_FUNCPTRS) {
472 struct fdpic_func_descriptor __user *funcptr =
473 (struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
475 __get_user(regs->pc, &funcptr->text);
476 __get_user(regs->regs[12], &funcptr->GOT);
478 regs->pc = (unsigned long)ka->sa.sa_handler;
482 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
483 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
485 flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
490 force_sigsegv(sig, current);
495 * OK, we're invoking a handler
499 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
500 sigset_t *oldset, struct pt_regs *regs, unsigned int save_r0)
504 /* Are we from a system call? */
505 if (regs->tra >= 0) {
506 /* If so, check system call restarting.. */
507 switch (regs->regs[0]) {
508 case -ERESTART_RESTARTBLOCK:
509 case -ERESTARTNOHAND:
510 regs->regs[0] = -EINTR;
514 if (!(ka->sa.sa_flags & SA_RESTART)) {
515 regs->regs[0] = -EINTR;
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);
598 /* Did we come from a system call? */
599 if (regs->tra >= 0) {
600 /* Restart the system call - no handlers present */
601 if (regs->regs[0] == -ERESTARTNOHAND ||
602 regs->regs[0] == -ERESTARTSYS ||
603 regs->regs[0] == -ERESTARTNOINTR) {
604 regs->regs[0] = save_r0;
605 regs->pc -= instruction_size(ctrl_inw(regs->pc - 4));
606 } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
607 regs->pc -= instruction_size(ctrl_inw(regs->pc - 4));
608 regs->regs[3] = __NR_restart_syscall;
612 /* if there's no signal to deliver, we just put the saved sigmask
614 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
615 clear_thread_flag(TIF_RESTORE_SIGMASK);
616 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
620 asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
621 __u32 thread_info_flags)
623 /* deal with pending signal delivery */
624 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
625 do_signal(regs, save_r0);