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)))
37 * Atomically swap in the new signal mask, and wait for a signal.
40 sys_sigsuspend(old_sigset_t mask,
41 unsigned long r5, unsigned long r6, unsigned long r7,
42 struct pt_regs __regs)
45 spin_lock_irq(¤t->sighand->siglock);
46 current->saved_sigmask = current->blocked;
47 siginitset(¤t->blocked, mask);
49 spin_unlock_irq(¤t->sighand->siglock);
51 current->state = TASK_INTERRUPTIBLE;
53 set_thread_flag(TIF_RESTORE_SIGMASK);
54 return -ERESTARTNOHAND;
58 sys_sigaction(int sig, const struct old_sigaction __user *act,
59 struct old_sigaction __user *oact)
61 struct k_sigaction new_ka, old_ka;
66 if (!access_ok(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 (!access_ok(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 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
91 unsigned long r6, unsigned long r7,
92 struct pt_regs __regs)
94 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
96 return do_sigaltstack(uss, uoss, regs->regs[15]);
101 * Do a signal return; undo the signal stack.
104 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
105 #if defined(CONFIG_CPU_SH2)
106 #define TRAP_NOARG 0xc320 /* Syscall w/no args (NR in R3) */
108 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) */
110 #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
114 struct sigcontext sc;
115 unsigned long extramask[_NSIG_WORDS-1];
127 static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
129 struct task_struct *tsk = current;
131 if (!(current_cpu_data.flags & CPU_HAS_FPU))
135 return __copy_from_user(&tsk->thread.fpu.hard, &sc->sc_fpregs[0],
136 sizeof(long)*(16*2+2));
139 static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
140 struct pt_regs *regs)
142 struct task_struct *tsk = current;
144 if (!(current_cpu_data.flags & CPU_HAS_FPU))
148 __put_user(0, &sc->sc_ownedfp);
152 __put_user(1, &sc->sc_ownedfp);
154 /* This will cause a "finit" to be triggered by the next
155 attempted FPU operation by the 'current' process.
159 unlazy_fpu(tsk, regs);
160 return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.fpu.hard,
161 sizeof(long)*(16*2+2));
163 #endif /* CONFIG_SH_FPU */
166 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
168 unsigned int err = 0;
170 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
172 COPY(regs[2]); COPY(regs[3]);
173 COPY(regs[4]); COPY(regs[5]);
174 COPY(regs[6]); COPY(regs[7]);
175 COPY(regs[8]); COPY(regs[9]);
176 COPY(regs[10]); COPY(regs[11]);
177 COPY(regs[12]); COPY(regs[13]);
178 COPY(regs[14]); COPY(regs[15]);
179 COPY(gbr); COPY(mach);
180 COPY(macl); COPY(pr);
185 if (current_cpu_data.flags & CPU_HAS_FPU) {
187 struct task_struct *tsk = current;
189 regs->sr |= SR_FD; /* Release FPU */
190 clear_fpu(tsk, regs);
192 __get_user (owned_fp, &sc->sc_ownedfp);
194 err |= restore_sigcontext_fpu(sc);
198 regs->tra = -1; /* disable syscall checks */
199 err |= __get_user(*r0_p, &sc->sc_regs[0]);
203 asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
204 unsigned long r6, unsigned long r7,
205 struct pt_regs __regs)
207 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
208 struct sigframe __user *frame = (struct sigframe __user *)regs->regs[15];
212 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
215 if (__get_user(set.sig[0], &frame->sc.oldmask)
217 && __copy_from_user(&set.sig[1], &frame->extramask,
218 sizeof(frame->extramask))))
221 sigdelsetmask(&set, ~_BLOCKABLE);
223 spin_lock_irq(¤t->sighand->siglock);
224 current->blocked = set;
226 spin_unlock_irq(¤t->sighand->siglock);
228 if (restore_sigcontext(regs, &frame->sc, &r0))
233 force_sig(SIGSEGV, current);
237 asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
238 unsigned long r6, unsigned long r7,
239 struct pt_regs __regs)
241 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
242 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15];
247 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
250 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
253 sigdelsetmask(&set, ~_BLOCKABLE);
254 spin_lock_irq(¤t->sighand->siglock);
255 current->blocked = set;
257 spin_unlock_irq(¤t->sighand->siglock);
259 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
262 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
264 /* It is more difficult to avoid calling this function than to
265 call it and ignore errors. */
266 do_sigaltstack((const stack_t __user *)&st, NULL, (unsigned long)frame);
271 force_sig(SIGSEGV, current);
276 * Set up a signal frame.
280 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
285 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
286 COPY(regs[0]); COPY(regs[1]);
287 COPY(regs[2]); COPY(regs[3]);
288 COPY(regs[4]); COPY(regs[5]);
289 COPY(regs[6]); COPY(regs[7]);
290 COPY(regs[8]); COPY(regs[9]);
291 COPY(regs[10]); COPY(regs[11]);
292 COPY(regs[12]); COPY(regs[13]);
293 COPY(regs[14]); COPY(regs[15]);
294 COPY(gbr); COPY(mach);
295 COPY(macl); COPY(pr);
300 err |= save_sigcontext_fpu(sc, regs);
303 /* non-iBCS2 extensions.. */
304 err |= __put_user(mask, &sc->oldmask);
310 * Determine which stack to use..
312 static inline void __user *
313 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
315 if (ka->sa.sa_flags & SA_ONSTACK) {
316 if (sas_ss_flags(sp) == 0)
317 sp = current->sas_ss_sp + current->sas_ss_size;
320 return (void __user *)((sp - frame_size) & -8ul);
323 /* These symbols are defined with the addresses in the vsyscall page.
324 See vsyscall-trapa.S. */
325 extern void __user __kernel_sigreturn;
326 extern void __user __kernel_rt_sigreturn;
328 static int setup_frame(int sig, struct k_sigaction *ka,
329 sigset_t *set, struct pt_regs *regs)
331 struct sigframe __user *frame;
335 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
337 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
340 signal = current_thread_info()->exec_domain
341 && current_thread_info()->exec_domain->signal_invmap
343 ? current_thread_info()->exec_domain->signal_invmap[sig]
346 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
349 err |= __copy_to_user(frame->extramask, &set->sig[1],
350 sizeof(frame->extramask));
352 /* Set up to return from userspace. If provided, use a stub
353 already in userspace. */
354 if (ka->sa.sa_flags & SA_RESTORER) {
355 regs->pr = (unsigned long) ka->sa.sa_restorer;
356 #ifdef CONFIG_VSYSCALL
357 } else if (likely(current->mm->context.vdso)) {
358 regs->pr = VDSO_SYM(&__kernel_sigreturn);
361 /* Generate return code (system call to sigreturn) */
362 err |= __put_user(MOVW(7), &frame->retcode[0]);
363 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
364 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
365 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
366 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
367 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
368 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
369 err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
370 regs->pr = (unsigned long) frame->retcode;
376 /* Set up registers for signal handler */
377 regs->regs[15] = (unsigned long) frame;
378 regs->regs[4] = signal; /* Arg for signal handler */
380 regs->regs[6] = (unsigned long) &frame->sc;
381 regs->pc = (unsigned long) ka->sa.sa_handler;
385 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
386 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
388 flush_cache_sigtramp(regs->pr);
390 if ((-regs->pr & (L1_CACHE_BYTES-1)) < sizeof(frame->retcode))
391 flush_cache_sigtramp(regs->pr + L1_CACHE_BYTES);
396 force_sigsegv(sig, current);
400 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
401 sigset_t *set, struct pt_regs *regs)
403 struct rt_sigframe __user *frame;
407 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
409 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
412 signal = current_thread_info()->exec_domain
413 && current_thread_info()->exec_domain->signal_invmap
415 ? current_thread_info()->exec_domain->signal_invmap[sig]
418 err |= copy_siginfo_to_user(&frame->info, info);
420 /* Create the ucontext. */
421 err |= __put_user(0, &frame->uc.uc_flags);
422 err |= __put_user(0, &frame->uc.uc_link);
423 err |= __put_user((void *)current->sas_ss_sp,
424 &frame->uc.uc_stack.ss_sp);
425 err |= __put_user(sas_ss_flags(regs->regs[15]),
426 &frame->uc.uc_stack.ss_flags);
427 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
428 err |= setup_sigcontext(&frame->uc.uc_mcontext,
430 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
432 /* Set up to return from userspace. If provided, use a stub
433 already in userspace. */
434 if (ka->sa.sa_flags & SA_RESTORER) {
435 regs->pr = (unsigned long) ka->sa.sa_restorer;
436 #ifdef CONFIG_VSYSCALL
437 } else if (likely(current->mm->context.vdso)) {
438 regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
441 /* Generate return code (system call to rt_sigreturn) */
442 err |= __put_user(MOVW(7), &frame->retcode[0]);
443 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
444 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
445 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
446 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
447 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
448 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
449 err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
450 regs->pr = (unsigned long) frame->retcode;
456 /* Set up registers for signal handler */
457 regs->regs[15] = (unsigned long) frame;
458 regs->regs[4] = signal; /* Arg for signal handler */
459 regs->regs[5] = (unsigned long) &frame->info;
460 regs->regs[6] = (unsigned long) &frame->uc;
461 regs->pc = (unsigned long) ka->sa.sa_handler;
465 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
466 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
468 flush_cache_sigtramp(regs->pr);
470 if ((-regs->pr & (L1_CACHE_BYTES-1)) < sizeof(frame->retcode))
471 flush_cache_sigtramp(regs->pr + L1_CACHE_BYTES);
476 force_sigsegv(sig, current);
481 * OK, we're invoking a handler
485 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
486 sigset_t *oldset, struct pt_regs *regs, unsigned int save_r0)
490 /* Are we from a system call? */
491 if (regs->tra >= 0) {
492 /* If so, check system call restarting.. */
493 switch (regs->regs[0]) {
494 case -ERESTART_RESTARTBLOCK:
495 case -ERESTARTNOHAND:
496 regs->regs[0] = -EINTR;
500 if (!(ka->sa.sa_flags & SA_RESTART)) {
501 regs->regs[0] = -EINTR;
505 case -ERESTARTNOINTR:
506 regs->regs[0] = save_r0;
507 regs->pc -= instruction_size(
508 ctrl_inw(regs->pc - 4));
513 /* Set up the stack frame */
514 if (ka->sa.sa_flags & SA_SIGINFO)
515 ret = setup_rt_frame(sig, ka, info, oldset, regs);
517 ret = setup_frame(sig, ka, oldset, regs);
519 if (ka->sa.sa_flags & SA_ONESHOT)
520 ka->sa.sa_handler = SIG_DFL;
523 spin_lock_irq(¤t->sighand->siglock);
524 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
525 if (!(ka->sa.sa_flags & SA_NODEFER))
526 sigaddset(¤t->blocked,sig);
528 spin_unlock_irq(¤t->sighand->siglock);
535 * Note that 'init' is a special process: it doesn't get signals it doesn't
536 * want to handle. Thus you cannot kill init even with a SIGKILL even by
539 * Note that we go through the signals twice: once to check the signals that
540 * the kernel can handle, and then we build all the user-level signal handling
541 * stack-frames in one go after that.
543 static void do_signal(struct pt_regs *regs, unsigned int save_r0)
547 struct k_sigaction ka;
551 * We want the common case to go fast, which
552 * is why we may in certain cases get here from
553 * kernel mode. Just return without doing anything
556 if (!user_mode(regs))
562 if (test_thread_flag(TIF_RESTORE_SIGMASK))
563 oldset = ¤t->saved_sigmask;
565 oldset = ¤t->blocked;
567 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
569 /* Whee! Actually deliver the signal. */
570 if (handle_signal(signr, &ka, &info, oldset,
571 regs, save_r0) == 0) {
572 /* a signal was successfully delivered; the saved
573 * sigmask will have been stored in the signal frame,
574 * and will be restored by sigreturn, so we can simply
575 * clear the TIF_RESTORE_SIGMASK flag */
576 if (test_thread_flag(TIF_RESTORE_SIGMASK))
577 clear_thread_flag(TIF_RESTORE_SIGMASK);
584 /* Did we come from a system call? */
585 if (regs->tra >= 0) {
586 /* Restart the system call - no handlers present */
587 if (regs->regs[0] == -ERESTARTNOHAND ||
588 regs->regs[0] == -ERESTARTSYS ||
589 regs->regs[0] == -ERESTARTNOINTR) {
590 regs->regs[0] = save_r0;
591 regs->pc -= instruction_size(ctrl_inw(regs->pc - 4));
592 } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
593 regs->pc -= instruction_size(ctrl_inw(regs->pc - 4));
594 regs->regs[3] = __NR_restart_syscall;
598 /* if there's no signal to deliver, we just put the saved sigmask
600 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
601 clear_thread_flag(TIF_RESTORE_SIGMASK);
602 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
606 asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
607 __u32 thread_info_flags)
609 /* deal with pending signal delivery */
610 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
611 do_signal(regs, save_r0);