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/smp_lock.h>
15 #include <linux/kernel.h>
16 #include <linux/signal.h>
17 #include <linux/errno.h>
18 #include <linux/wait.h>
19 #include <linux/ptrace.h>
20 #include <linux/unistd.h>
21 #include <linux/stddef.h>
22 #include <linux/tty.h>
23 #include <linux/elf.h>
24 #include <linux/personality.h>
25 #include <linux/binfmts.h>
26 #include <linux/freezer.h>
28 #include <asm/ucontext.h>
29 #include <asm/uaccess.h>
30 #include <asm/pgtable.h>
31 #include <asm/cacheflush.h>
33 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
36 * Atomically swap in the new signal mask, and wait for a signal.
39 sys_sigsuspend(old_sigset_t mask,
40 unsigned long r5, unsigned long r6, unsigned long r7,
41 struct pt_regs __regs)
44 spin_lock_irq(¤t->sighand->siglock);
45 current->saved_sigmask = current->blocked;
46 siginitset(¤t->blocked, mask);
48 spin_unlock_irq(¤t->sighand->siglock);
50 current->state = TASK_INTERRUPTIBLE;
52 set_thread_flag(TIF_RESTORE_SIGMASK);
53 return -ERESTARTNOHAND;
57 sys_sigaction(int sig, const struct old_sigaction __user *act,
58 struct old_sigaction __user *oact)
60 struct k_sigaction new_ka, old_ka;
65 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
66 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
67 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
69 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
70 __get_user(mask, &act->sa_mask);
71 siginitset(&new_ka.sa.sa_mask, mask);
74 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
77 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
78 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
79 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
81 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
82 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
89 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
90 unsigned long r6, unsigned long r7,
91 struct pt_regs __regs)
93 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
95 return do_sigaltstack(uss, uoss, regs->regs[15]);
100 * Do a signal return; undo the signal stack.
103 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
104 #if defined(CONFIG_CPU_SH2)
105 #define TRAP_NOARG 0xc320 /* Syscall w/no args (NR in R3) */
107 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) */
109 #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
113 struct sigcontext sc;
114 unsigned long extramask[_NSIG_WORDS-1];
126 static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
128 struct task_struct *tsk = current;
130 if (!(cpu_data->flags & CPU_HAS_FPU))
134 return __copy_from_user(&tsk->thread.fpu.hard, &sc->sc_fpregs[0],
135 sizeof(long)*(16*2+2));
138 static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
139 struct pt_regs *regs)
141 struct task_struct *tsk = current;
143 if (!(cpu_data->flags & CPU_HAS_FPU))
147 __put_user(0, &sc->sc_ownedfp);
151 __put_user(1, &sc->sc_ownedfp);
153 /* This will cause a "finit" to be triggered by the next
154 attempted FPU operation by the 'current' process.
158 unlazy_fpu(tsk, regs);
159 return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.fpu.hard,
160 sizeof(long)*(16*2+2));
162 #endif /* CONFIG_SH_FPU */
165 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
167 unsigned int err = 0;
169 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
171 COPY(regs[2]); COPY(regs[3]);
172 COPY(regs[4]); COPY(regs[5]);
173 COPY(regs[6]); COPY(regs[7]);
174 COPY(regs[8]); COPY(regs[9]);
175 COPY(regs[10]); COPY(regs[11]);
176 COPY(regs[12]); COPY(regs[13]);
177 COPY(regs[14]); COPY(regs[15]);
178 COPY(gbr); COPY(mach);
179 COPY(macl); COPY(pr);
184 if (cpu_data->flags & CPU_HAS_FPU) {
186 struct task_struct *tsk = current;
188 regs->sr |= SR_FD; /* Release FPU */
189 clear_fpu(tsk, regs);
191 __get_user (owned_fp, &sc->sc_ownedfp);
193 err |= restore_sigcontext_fpu(sc);
197 regs->tra = -1; /* disable syscall checks */
198 err |= __get_user(*r0_p, &sc->sc_regs[0]);
202 asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
203 unsigned long r6, unsigned long r7,
204 struct pt_regs __regs)
206 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
207 struct sigframe __user *frame = (struct sigframe __user *)regs->regs[15];
211 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
214 if (__get_user(set.sig[0], &frame->sc.oldmask)
216 && __copy_from_user(&set.sig[1], &frame->extramask,
217 sizeof(frame->extramask))))
220 sigdelsetmask(&set, ~_BLOCKABLE);
222 spin_lock_irq(¤t->sighand->siglock);
223 current->blocked = set;
225 spin_unlock_irq(¤t->sighand->siglock);
227 if (restore_sigcontext(regs, &frame->sc, &r0))
232 force_sig(SIGSEGV, current);
236 asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
237 unsigned long r6, unsigned long r7,
238 struct pt_regs __regs)
240 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
241 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15];
246 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
249 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
252 sigdelsetmask(&set, ~_BLOCKABLE);
253 spin_lock_irq(¤t->sighand->siglock);
254 current->blocked = set;
256 spin_unlock_irq(¤t->sighand->siglock);
258 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
261 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
263 /* It is more difficult to avoid calling this function than to
264 call it and ignore errors. */
265 do_sigaltstack(&st, NULL, regs->regs[15]);
270 force_sig(SIGSEGV, current);
275 * Set up a signal frame.
279 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
284 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
285 COPY(regs[0]); COPY(regs[1]);
286 COPY(regs[2]); COPY(regs[3]);
287 COPY(regs[4]); COPY(regs[5]);
288 COPY(regs[6]); COPY(regs[7]);
289 COPY(regs[8]); COPY(regs[9]);
290 COPY(regs[10]); COPY(regs[11]);
291 COPY(regs[12]); COPY(regs[13]);
292 COPY(regs[14]); COPY(regs[15]);
293 COPY(gbr); COPY(mach);
294 COPY(macl); COPY(pr);
299 err |= save_sigcontext_fpu(sc, regs);
302 /* non-iBCS2 extensions.. */
303 err |= __put_user(mask, &sc->oldmask);
309 * Determine which stack to use..
311 static inline void __user *
312 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
314 if (ka->sa.sa_flags & SA_ONSTACK) {
315 if (sas_ss_flags(sp) == 0)
316 sp = current->sas_ss_sp + current->sas_ss_size;
319 return (void __user *)((sp - frame_size) & -8ul);
322 /* These symbols are defined with the addresses in the vsyscall page.
323 See vsyscall-trapa.S. */
324 extern void __user __kernel_sigreturn;
325 extern void __user __kernel_rt_sigreturn;
327 static int setup_frame(int sig, struct k_sigaction *ka,
328 sigset_t *set, struct pt_regs *regs)
330 struct sigframe __user *frame;
334 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
336 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
339 signal = current_thread_info()->exec_domain
340 && current_thread_info()->exec_domain->signal_invmap
342 ? current_thread_info()->exec_domain->signal_invmap[sig]
345 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
348 err |= __copy_to_user(frame->extramask, &set->sig[1],
349 sizeof(frame->extramask));
351 /* Set up to return from userspace. If provided, use a stub
352 already in userspace. */
353 if (ka->sa.sa_flags & SA_RESTORER) {
354 regs->pr = (unsigned long) ka->sa.sa_restorer;
355 #ifdef CONFIG_VSYSCALL
356 } else if (likely(current->mm->context.vdso)) {
357 regs->pr = VDSO_SYM(&__kernel_sigreturn);
360 /* Generate return code (system call to sigreturn) */
361 err |= __put_user(MOVW(7), &frame->retcode[0]);
362 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
363 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
364 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
365 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
366 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
367 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
368 err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
369 regs->pr = (unsigned long) frame->retcode;
375 /* Set up registers for signal handler */
376 regs->regs[15] = (unsigned long) frame;
377 regs->regs[4] = signal; /* Arg for signal handler */
379 regs->regs[6] = (unsigned long) &frame->sc;
380 regs->pc = (unsigned long) ka->sa.sa_handler;
384 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
385 current->comm, current->pid, frame, regs->pc, regs->pr);
387 flush_cache_sigtramp(regs->pr);
389 if ((-regs->pr & (L1_CACHE_BYTES-1)) < sizeof(frame->retcode))
390 flush_cache_sigtramp(regs->pr + L1_CACHE_BYTES);
395 force_sigsegv(sig, current);
399 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
400 sigset_t *set, struct pt_regs *regs)
402 struct rt_sigframe __user *frame;
406 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
408 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
411 signal = current_thread_info()->exec_domain
412 && current_thread_info()->exec_domain->signal_invmap
414 ? current_thread_info()->exec_domain->signal_invmap[sig]
417 err |= copy_siginfo_to_user(&frame->info, info);
419 /* Create the ucontext. */
420 err |= __put_user(0, &frame->uc.uc_flags);
421 err |= __put_user(0, &frame->uc.uc_link);
422 err |= __put_user((void *)current->sas_ss_sp,
423 &frame->uc.uc_stack.ss_sp);
424 err |= __put_user(sas_ss_flags(regs->regs[15]),
425 &frame->uc.uc_stack.ss_flags);
426 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
427 err |= setup_sigcontext(&frame->uc.uc_mcontext,
429 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
431 /* Set up to return from userspace. If provided, use a stub
432 already in userspace. */
433 if (ka->sa.sa_flags & SA_RESTORER) {
434 regs->pr = (unsigned long) ka->sa.sa_restorer;
435 #ifdef CONFIG_VSYSCALL
436 } else if (likely(current->mm->context.vdso)) {
437 regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
440 /* Generate return code (system call to rt_sigreturn) */
441 err |= __put_user(MOVW(7), &frame->retcode[0]);
442 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
443 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
444 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
445 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
446 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
447 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
448 err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
449 regs->pr = (unsigned long) frame->retcode;
455 /* Set up registers for signal handler */
456 regs->regs[15] = (unsigned long) frame;
457 regs->regs[4] = signal; /* Arg for signal handler */
458 regs->regs[5] = (unsigned long) &frame->info;
459 regs->regs[6] = (unsigned long) &frame->uc;
460 regs->pc = (unsigned long) ka->sa.sa_handler;
464 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
465 current->comm, current->pid, frame, regs->pc, regs->pr);
467 flush_cache_sigtramp(regs->pr);
469 if ((-regs->pr & (L1_CACHE_BYTES-1)) < sizeof(frame->retcode))
470 flush_cache_sigtramp(regs->pr + L1_CACHE_BYTES);
475 force_sigsegv(sig, current);
480 * OK, we're invoking a handler
484 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
485 sigset_t *oldset, struct pt_regs *regs)
489 /* Are we from a system call? */
490 if (regs->tra >= 0) {
491 /* If so, check system call restarting.. */
492 switch (regs->regs[0]) {
493 case -ERESTARTNOHAND:
494 regs->regs[0] = -EINTR;
498 if (!(ka->sa.sa_flags & SA_RESTART)) {
499 regs->regs[0] = -EINTR;
503 case -ERESTARTNOINTR:
508 #ifdef CONFIG_PREEMPT
511 local_irq_save(flags);
513 if (regs->regs[15] >= 0xc0000000) {
514 int offset = (int)regs->regs[15];
516 /* Reset stack pointer: clear critical region mark */
517 regs->regs[15] = regs->regs[1];
518 if (regs->pc < regs->regs[0])
519 /* Go to rewind point #1 */
520 regs->pc = regs->regs[0] + offset - 2;
522 #ifdef CONFIG_PREEMPT
523 local_irq_restore(flags);
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, regs) == 0) {
585 /* a signal was successfully delivered; the saved
586 * sigmask will have been stored in the signal frame,
587 * and will be restored by sigreturn, so we can simply
588 * clear the TIF_RESTORE_SIGMASK flag */
589 if (test_thread_flag(TIF_RESTORE_SIGMASK))
590 clear_thread_flag(TIF_RESTORE_SIGMASK);
595 /* Did we come from a system call? */
596 if (regs->tra >= 0) {
597 /* Restart the system call - no handlers present */
598 if (regs->regs[0] == -ERESTARTNOHAND ||
599 regs->regs[0] == -ERESTARTSYS ||
600 regs->regs[0] == -ERESTARTNOINTR) {
601 regs->regs[0] = save_r0;
603 } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
605 regs->regs[3] = __NR_restart_syscall;
609 /* if there's no signal to deliver, we just put the saved sigmask
611 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
612 clear_thread_flag(TIF_RESTORE_SIGMASK);
613 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
617 asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
618 __u32 thread_info_flags)
620 /* deal with pending signal delivery */
621 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
622 do_signal(regs, save_r0);