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>
26 #include <asm/system.h>
27 #include <asm/ucontext.h>
28 #include <asm/uaccess.h>
29 #include <asm/pgtable.h>
30 #include <asm/cacheflush.h>
32 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
35 * Atomically swap in the new signal mask, and wait for a signal.
38 sys_sigsuspend(old_sigset_t mask,
39 unsigned long r5, unsigned long r6, unsigned long r7,
40 struct pt_regs __regs)
43 spin_lock_irq(¤t->sighand->siglock);
44 current->saved_sigmask = current->blocked;
45 siginitset(¤t->blocked, mask);
47 spin_unlock_irq(¤t->sighand->siglock);
49 current->state = TASK_INTERRUPTIBLE;
51 set_thread_flag(TIF_RESTORE_SIGMASK);
52 return -ERESTARTNOHAND;
56 sys_sigaction(int sig, const struct old_sigaction __user *act,
57 struct old_sigaction __user *oact)
59 struct k_sigaction new_ka, old_ka;
64 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
65 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
66 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
68 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
69 __get_user(mask, &act->sa_mask);
70 siginitset(&new_ka.sa.sa_mask, mask);
73 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
76 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
77 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
78 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
80 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
81 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
88 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
89 unsigned long r6, unsigned long r7,
90 struct pt_regs __regs)
92 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
94 return do_sigaltstack(uss, uoss, regs->regs[15]);
99 * Do a signal return; undo the signal stack.
102 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
103 #if defined(CONFIG_CPU_SH2)
104 #define TRAP_NOARG 0xc320 /* Syscall w/no args (NR in R3) */
106 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) */
108 #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
112 struct sigcontext sc;
113 unsigned long extramask[_NSIG_WORDS-1];
125 static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
127 struct task_struct *tsk = current;
129 if (!(current_cpu_data.flags & CPU_HAS_FPU))
133 return __copy_from_user(&tsk->thread.fpu.hard, &sc->sc_fpregs[0],
134 sizeof(long)*(16*2+2));
137 static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
138 struct pt_regs *regs)
140 struct task_struct *tsk = current;
142 if (!(current_cpu_data.flags & CPU_HAS_FPU))
146 __put_user(0, &sc->sc_ownedfp);
150 __put_user(1, &sc->sc_ownedfp);
152 /* This will cause a "finit" to be triggered by the next
153 attempted FPU operation by the 'current' process.
157 unlazy_fpu(tsk, regs);
158 return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.fpu.hard,
159 sizeof(long)*(16*2+2));
161 #endif /* CONFIG_SH_FPU */
164 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
166 unsigned int err = 0;
168 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
170 COPY(regs[2]); COPY(regs[3]);
171 COPY(regs[4]); COPY(regs[5]);
172 COPY(regs[6]); COPY(regs[7]);
173 COPY(regs[8]); COPY(regs[9]);
174 COPY(regs[10]); COPY(regs[11]);
175 COPY(regs[12]); COPY(regs[13]);
176 COPY(regs[14]); COPY(regs[15]);
177 COPY(gbr); COPY(mach);
178 COPY(macl); COPY(pr);
183 if (current_cpu_data.flags & CPU_HAS_FPU) {
185 struct task_struct *tsk = current;
187 regs->sr |= SR_FD; /* Release FPU */
188 clear_fpu(tsk, regs);
190 __get_user (owned_fp, &sc->sc_ownedfp);
192 err |= restore_sigcontext_fpu(sc);
196 regs->tra = -1; /* disable syscall checks */
197 err |= __get_user(*r0_p, &sc->sc_regs[0]);
201 asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
202 unsigned long r6, unsigned long r7,
203 struct pt_regs __regs)
205 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
206 struct sigframe __user *frame = (struct sigframe __user *)regs->regs[15];
210 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
213 if (__get_user(set.sig[0], &frame->sc.oldmask)
215 && __copy_from_user(&set.sig[1], &frame->extramask,
216 sizeof(frame->extramask))))
219 sigdelsetmask(&set, ~_BLOCKABLE);
221 spin_lock_irq(¤t->sighand->siglock);
222 current->blocked = set;
224 spin_unlock_irq(¤t->sighand->siglock);
226 if (restore_sigcontext(regs, &frame->sc, &r0))
231 force_sig(SIGSEGV, current);
235 asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
236 unsigned long r6, unsigned long r7,
237 struct pt_regs __regs)
239 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
240 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15];
245 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
248 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
251 sigdelsetmask(&set, ~_BLOCKABLE);
252 spin_lock_irq(¤t->sighand->siglock);
253 current->blocked = set;
255 spin_unlock_irq(¤t->sighand->siglock);
257 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
260 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
262 /* It is more difficult to avoid calling this function than to
263 call it and ignore errors. */
264 do_sigaltstack(&st, NULL, regs->regs[15]);
269 force_sig(SIGSEGV, current);
274 * Set up a signal frame.
278 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
283 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
284 COPY(regs[0]); COPY(regs[1]);
285 COPY(regs[2]); COPY(regs[3]);
286 COPY(regs[4]); COPY(regs[5]);
287 COPY(regs[6]); COPY(regs[7]);
288 COPY(regs[8]); COPY(regs[9]);
289 COPY(regs[10]); COPY(regs[11]);
290 COPY(regs[12]); COPY(regs[13]);
291 COPY(regs[14]); COPY(regs[15]);
292 COPY(gbr); COPY(mach);
293 COPY(macl); COPY(pr);
298 err |= save_sigcontext_fpu(sc, regs);
301 /* non-iBCS2 extensions.. */
302 err |= __put_user(mask, &sc->oldmask);
308 * Determine which stack to use..
310 static inline void __user *
311 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
313 if (ka->sa.sa_flags & SA_ONSTACK) {
314 if (sas_ss_flags(sp) == 0)
315 sp = current->sas_ss_sp + current->sas_ss_size;
318 return (void __user *)((sp - frame_size) & -8ul);
321 /* These symbols are defined with the addresses in the vsyscall page.
322 See vsyscall-trapa.S. */
323 extern void __user __kernel_sigreturn;
324 extern void __user __kernel_rt_sigreturn;
326 static int setup_frame(int sig, struct k_sigaction *ka,
327 sigset_t *set, struct pt_regs *regs)
329 struct sigframe __user *frame;
333 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
335 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
338 signal = current_thread_info()->exec_domain
339 && current_thread_info()->exec_domain->signal_invmap
341 ? current_thread_info()->exec_domain->signal_invmap[sig]
344 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
347 err |= __copy_to_user(frame->extramask, &set->sig[1],
348 sizeof(frame->extramask));
350 /* Set up to return from userspace. If provided, use a stub
351 already in userspace. */
352 if (ka->sa.sa_flags & SA_RESTORER) {
353 regs->pr = (unsigned long) ka->sa.sa_restorer;
354 #ifdef CONFIG_VSYSCALL
355 } else if (likely(current->mm->context.vdso)) {
356 regs->pr = VDSO_SYM(&__kernel_sigreturn);
359 /* Generate return code (system call to sigreturn) */
360 err |= __put_user(MOVW(7), &frame->retcode[0]);
361 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
362 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
363 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
364 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
365 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
366 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
367 err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
368 regs->pr = (unsigned long) frame->retcode;
374 /* Set up registers for signal handler */
375 regs->regs[15] = (unsigned long) frame;
376 regs->regs[4] = signal; /* Arg for signal handler */
378 regs->regs[6] = (unsigned long) &frame->sc;
379 regs->pc = (unsigned long) ka->sa.sa_handler;
383 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
384 current->comm, current->pid, frame, regs->pc, regs->pr);
386 flush_cache_sigtramp(regs->pr);
388 if ((-regs->pr & (L1_CACHE_BYTES-1)) < sizeof(frame->retcode))
389 flush_cache_sigtramp(regs->pr + L1_CACHE_BYTES);
394 force_sigsegv(sig, current);
398 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
399 sigset_t *set, struct pt_regs *regs)
401 struct rt_sigframe __user *frame;
405 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
407 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
410 signal = current_thread_info()->exec_domain
411 && current_thread_info()->exec_domain->signal_invmap
413 ? current_thread_info()->exec_domain->signal_invmap[sig]
416 err |= copy_siginfo_to_user(&frame->info, info);
418 /* Create the ucontext. */
419 err |= __put_user(0, &frame->uc.uc_flags);
420 err |= __put_user(0, &frame->uc.uc_link);
421 err |= __put_user((void *)current->sas_ss_sp,
422 &frame->uc.uc_stack.ss_sp);
423 err |= __put_user(sas_ss_flags(regs->regs[15]),
424 &frame->uc.uc_stack.ss_flags);
425 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
426 err |= setup_sigcontext(&frame->uc.uc_mcontext,
428 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
430 /* Set up to return from userspace. If provided, use a stub
431 already in userspace. */
432 if (ka->sa.sa_flags & SA_RESTORER) {
433 regs->pr = (unsigned long) ka->sa.sa_restorer;
434 #ifdef CONFIG_VSYSCALL
435 } else if (likely(current->mm->context.vdso)) {
436 regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
439 /* Generate return code (system call to rt_sigreturn) */
440 err |= __put_user(MOVW(7), &frame->retcode[0]);
441 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
442 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
443 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
444 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
445 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
446 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
447 err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
448 regs->pr = (unsigned long) frame->retcode;
454 /* Set up registers for signal handler */
455 regs->regs[15] = (unsigned long) frame;
456 regs->regs[4] = signal; /* Arg for signal handler */
457 regs->regs[5] = (unsigned long) &frame->info;
458 regs->regs[6] = (unsigned long) &frame->uc;
459 regs->pc = (unsigned long) ka->sa.sa_handler;
463 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
464 current->comm, current->pid, frame, regs->pc, regs->pr);
466 flush_cache_sigtramp(regs->pr);
468 if ((-regs->pr & (L1_CACHE_BYTES-1)) < sizeof(frame->retcode))
469 flush_cache_sigtramp(regs->pr + L1_CACHE_BYTES);
474 force_sigsegv(sig, current);
479 * OK, we're invoking a handler
483 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
484 sigset_t *oldset, struct pt_regs *regs)
488 /* Are we from a system call? */
489 if (regs->tra >= 0) {
490 /* If so, check system call restarting.. */
491 switch (regs->regs[0]) {
492 case -ERESTARTNOHAND:
493 regs->regs[0] = -EINTR;
497 if (!(ka->sa.sa_flags & SA_RESTART)) {
498 regs->regs[0] = -EINTR;
502 case -ERESTARTNOINTR:
503 regs->pc -= instruction_size(
504 ctrl_inw(regs->pc - 4));
509 #ifdef CONFIG_PREEMPT
512 local_irq_save(flags);
514 if (regs->regs[15] >= 0xc0000000) {
515 int offset = (int)regs->regs[15];
517 /* Reset stack pointer: clear critical region mark */
518 regs->regs[15] = regs->regs[1];
519 if (regs->pc < regs->regs[0])
520 /* Go to rewind point #1 */
521 regs->pc = regs->regs[0] + offset -
522 instruction_size(ctrl_inw(regs->pc-4));
524 #ifdef CONFIG_PREEMPT
525 local_irq_restore(flags);
529 /* Set up the stack frame */
530 if (ka->sa.sa_flags & SA_SIGINFO)
531 ret = setup_rt_frame(sig, ka, info, oldset, regs);
533 ret = setup_frame(sig, ka, oldset, regs);
535 if (ka->sa.sa_flags & SA_ONESHOT)
536 ka->sa.sa_handler = SIG_DFL;
539 spin_lock_irq(¤t->sighand->siglock);
540 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
541 if (!(ka->sa.sa_flags & SA_NODEFER))
542 sigaddset(¤t->blocked,sig);
544 spin_unlock_irq(¤t->sighand->siglock);
551 * Note that 'init' is a special process: it doesn't get signals it doesn't
552 * want to handle. Thus you cannot kill init even with a SIGKILL even by
555 * Note that we go through the signals twice: once to check the signals that
556 * the kernel can handle, and then we build all the user-level signal handling
557 * stack-frames in one go after that.
559 static void do_signal(struct pt_regs *regs, unsigned int save_r0)
563 struct k_sigaction ka;
567 * We want the common case to go fast, which
568 * is why we may in certain cases get here from
569 * kernel mode. Just return without doing anything
572 if (!user_mode(regs))
578 if (test_thread_flag(TIF_RESTORE_SIGMASK))
579 oldset = ¤t->saved_sigmask;
581 oldset = ¤t->blocked;
583 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
585 /* Whee! Actually deliver the signal. */
586 if (handle_signal(signr, &ka, &info, oldset, regs) == 0) {
587 /* a signal was successfully delivered; the saved
588 * sigmask will have been stored in the signal frame,
589 * and will be restored by sigreturn, so we can simply
590 * clear the TIF_RESTORE_SIGMASK flag */
591 if (test_thread_flag(TIF_RESTORE_SIGMASK))
592 clear_thread_flag(TIF_RESTORE_SIGMASK);
599 /* Did we come from a system call? */
600 if (regs->tra >= 0) {
601 /* Restart the system call - no handlers present */
602 if (regs->regs[0] == -ERESTARTNOHAND ||
603 regs->regs[0] == -ERESTARTSYS ||
604 regs->regs[0] == -ERESTARTNOINTR) {
605 regs->regs[0] = save_r0;
606 regs->pc -= instruction_size(ctrl_inw(regs->pc - 4));
607 } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
608 regs->pc -= instruction_size(ctrl_inw(regs->pc - 4));
609 regs->regs[3] = __NR_restart_syscall;
613 /* if there's no signal to deliver, we just put the saved sigmask
615 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
616 clear_thread_flag(TIF_RESTORE_SIGMASK);
617 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
621 asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
622 __u32 thread_info_flags)
624 /* deal with pending signal delivery */
625 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
626 do_signal(regs, save_r0);