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;
381 /* Set up registers for signal handler */
382 regs->regs[15] = (unsigned long) frame;
383 regs->regs[4] = signal; /* Arg for signal handler */
385 regs->regs[6] = (unsigned long) &frame->sc;
387 if (current->personality & FDPIC_FUNCPTRS) {
388 struct fdpic_func_descriptor __user *funcptr =
389 (struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
391 __get_user(regs->pc, &funcptr->text);
392 __get_user(regs->regs[12], &funcptr->GOT);
394 regs->pc = (unsigned long)ka->sa.sa_handler;
398 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
399 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
401 flush_cache_sigtramp(regs->pr);
403 if ((-regs->pr & (L1_CACHE_BYTES-1)) < sizeof(frame->retcode))
404 flush_cache_sigtramp(regs->pr + L1_CACHE_BYTES);
409 force_sigsegv(sig, current);
413 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
414 sigset_t *set, struct pt_regs *regs)
416 struct rt_sigframe __user *frame;
420 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
422 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
425 signal = current_thread_info()->exec_domain
426 && current_thread_info()->exec_domain->signal_invmap
428 ? current_thread_info()->exec_domain->signal_invmap[sig]
431 err |= copy_siginfo_to_user(&frame->info, info);
433 /* Create the ucontext. */
434 err |= __put_user(0, &frame->uc.uc_flags);
435 err |= __put_user(0, &frame->uc.uc_link);
436 err |= __put_user((void *)current->sas_ss_sp,
437 &frame->uc.uc_stack.ss_sp);
438 err |= __put_user(sas_ss_flags(regs->regs[15]),
439 &frame->uc.uc_stack.ss_flags);
440 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
441 err |= setup_sigcontext(&frame->uc.uc_mcontext,
443 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
445 /* Set up to return from userspace. If provided, use a stub
446 already in userspace. */
447 if (ka->sa.sa_flags & SA_RESTORER) {
448 regs->pr = (unsigned long) ka->sa.sa_restorer;
449 #ifdef CONFIG_VSYSCALL
450 } else if (likely(current->mm->context.vdso)) {
451 regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
454 /* Generate return code (system call to rt_sigreturn) */
455 err |= __put_user(MOVW(7), &frame->retcode[0]);
456 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
457 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
458 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
459 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
460 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
461 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
462 err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
463 regs->pr = (unsigned long) frame->retcode;
469 /* Set up registers for signal handler */
470 regs->regs[15] = (unsigned long) frame;
471 regs->regs[4] = signal; /* Arg for signal handler */
472 regs->regs[5] = (unsigned long) &frame->info;
473 regs->regs[6] = (unsigned long) &frame->uc;
475 if (current->personality & FDPIC_FUNCPTRS) {
476 struct fdpic_func_descriptor __user *funcptr =
477 (struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
479 __get_user(regs->pc, &funcptr->text);
480 __get_user(regs->regs[12], &funcptr->GOT);
482 regs->pc = (unsigned long)ka->sa.sa_handler;
486 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
487 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
489 flush_cache_sigtramp(regs->pr);
491 if ((-regs->pr & (L1_CACHE_BYTES-1)) < sizeof(frame->retcode))
492 flush_cache_sigtramp(regs->pr + L1_CACHE_BYTES);
497 force_sigsegv(sig, current);
502 * OK, we're invoking a handler
506 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
507 sigset_t *oldset, struct pt_regs *regs, unsigned int save_r0)
511 /* Are we from a system call? */
512 if (regs->tra >= 0) {
513 /* If so, check system call restarting.. */
514 switch (regs->regs[0]) {
515 case -ERESTART_RESTARTBLOCK:
516 case -ERESTARTNOHAND:
517 regs->regs[0] = -EINTR;
521 if (!(ka->sa.sa_flags & SA_RESTART)) {
522 regs->regs[0] = -EINTR;
526 case -ERESTARTNOINTR:
527 regs->regs[0] = save_r0;
528 regs->pc -= instruction_size(
529 ctrl_inw(regs->pc - 4));
534 /* Set up the stack frame */
535 if (ka->sa.sa_flags & SA_SIGINFO)
536 ret = setup_rt_frame(sig, ka, info, oldset, regs);
538 ret = setup_frame(sig, ka, oldset, regs);
540 if (ka->sa.sa_flags & SA_ONESHOT)
541 ka->sa.sa_handler = SIG_DFL;
544 spin_lock_irq(¤t->sighand->siglock);
545 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
546 if (!(ka->sa.sa_flags & SA_NODEFER))
547 sigaddset(¤t->blocked,sig);
549 spin_unlock_irq(¤t->sighand->siglock);
556 * Note that 'init' is a special process: it doesn't get signals it doesn't
557 * want to handle. Thus you cannot kill init even with a SIGKILL even by
560 * Note that we go through the signals twice: once to check the signals that
561 * the kernel can handle, and then we build all the user-level signal handling
562 * stack-frames in one go after that.
564 static void do_signal(struct pt_regs *regs, unsigned int save_r0)
568 struct k_sigaction ka;
572 * We want the common case to go fast, which
573 * is why we may in certain cases get here from
574 * kernel mode. Just return without doing anything
577 if (!user_mode(regs))
583 if (test_thread_flag(TIF_RESTORE_SIGMASK))
584 oldset = ¤t->saved_sigmask;
586 oldset = ¤t->blocked;
588 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
590 /* Whee! Actually deliver the signal. */
591 if (handle_signal(signr, &ka, &info, oldset,
592 regs, save_r0) == 0) {
593 /* a signal was successfully delivered; the saved
594 * sigmask will have been stored in the signal frame,
595 * and will be restored by sigreturn, so we can simply
596 * clear the TIF_RESTORE_SIGMASK flag */
597 if (test_thread_flag(TIF_RESTORE_SIGMASK))
598 clear_thread_flag(TIF_RESTORE_SIGMASK);
605 /* Did we come from a system call? */
606 if (regs->tra >= 0) {
607 /* Restart the system call - no handlers present */
608 if (regs->regs[0] == -ERESTARTNOHAND ||
609 regs->regs[0] == -ERESTARTSYS ||
610 regs->regs[0] == -ERESTARTNOINTR) {
611 regs->regs[0] = save_r0;
612 regs->pc -= instruction_size(ctrl_inw(regs->pc - 4));
613 } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
614 regs->pc -= instruction_size(ctrl_inw(regs->pc - 4));
615 regs->regs[3] = __NR_restart_syscall;
619 /* if there's no signal to deliver, we just put the saved sigmask
621 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
622 clear_thread_flag(TIF_RESTORE_SIGMASK);
623 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
627 asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
628 __u32 thread_info_flags)
630 /* deal with pending signal delivery */
631 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
632 do_signal(regs, save_r0);