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 <linux/tracehook.h>
28 #include <asm/ucontext.h>
29 #include <asm/uaccess.h>
30 #include <asm/pgtable.h>
31 #include <asm/cacheflush.h>
32 #include <asm/syscalls.h>
35 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
37 struct fdpic_func_descriptor {
43 * The following define adds a 64 byte gap between the signal
44 * stack frame and previous contents of the stack. This allows
45 * frame unwinding in a function epilogue but only if a frame
46 * pointer is used in the function. This is necessary because
47 * current gcc compilers (<4.3) do not generate unwind info on
48 * SH for function epilogues.
50 #define UNWINDGUARD 64
53 * Atomically swap in the new signal mask, and wait for a signal.
56 sys_sigsuspend(old_sigset_t mask)
59 siginitset(&blocked, mask);
60 return sigsuspend(&blocked);
64 sys_sigaction(int sig, const struct old_sigaction __user *act,
65 struct old_sigaction __user *oact)
67 struct k_sigaction new_ka, old_ka;
72 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
73 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
74 __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))
78 siginitset(&new_ka.sa.sa_mask, mask);
81 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
84 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
85 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
86 __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))
96 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
97 unsigned long r6, unsigned long r7,
98 struct pt_regs __regs)
100 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
102 return do_sigaltstack(uss, uoss, regs->regs[15]);
107 * Do a signal return; undo the signal stack.
110 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
111 #if defined(CONFIG_CPU_SH2)
112 #define TRAP_NOARG 0xc320 /* Syscall w/no args (NR in R3) */
114 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) */
116 #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
120 struct sigcontext sc;
121 unsigned long extramask[_NSIG_WORDS-1];
133 static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
135 struct task_struct *tsk = current;
137 if (!(boot_cpu_data.flags & CPU_HAS_FPU))
141 return __copy_from_user(&tsk->thread.xstate->hardfpu, &sc->sc_fpregs[0],
142 sizeof(long)*(16*2+2));
145 static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
146 struct pt_regs *regs)
148 struct task_struct *tsk = current;
150 if (!(boot_cpu_data.flags & CPU_HAS_FPU))
154 return __put_user(0, &sc->sc_ownedfp);
156 if (__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.xstate->hardfpu,
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 (boot_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 err |= __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 /* Always make any pending restarted system calls return -EINTR */
218 current_thread_info()->restart_block.fn = do_no_restart_syscall;
220 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
223 if (__get_user(set.sig[0], &frame->sc.oldmask)
225 && __copy_from_user(&set.sig[1], &frame->extramask,
226 sizeof(frame->extramask))))
229 sigdelsetmask(&set, ~_BLOCKABLE);
230 set_current_blocked(&set);
232 if (restore_sigcontext(regs, &frame->sc, &r0))
237 force_sig(SIGSEGV, current);
241 asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
242 unsigned long r6, unsigned long r7,
243 struct pt_regs __regs)
245 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
246 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15];
250 /* Always make any pending restarted system calls return -EINTR */
251 current_thread_info()->restart_block.fn = do_no_restart_syscall;
253 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
256 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
259 sigdelsetmask(&set, ~_BLOCKABLE);
260 set_current_blocked(&set);
262 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
265 if (do_sigaltstack(&frame->uc.uc_stack, NULL,
266 regs->regs[15]) == -EFAULT)
272 force_sig(SIGSEGV, current);
277 * Set up a signal frame.
281 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
286 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
287 COPY(regs[0]); COPY(regs[1]);
288 COPY(regs[2]); COPY(regs[3]);
289 COPY(regs[4]); COPY(regs[5]);
290 COPY(regs[6]); COPY(regs[7]);
291 COPY(regs[8]); COPY(regs[9]);
292 COPY(regs[10]); COPY(regs[11]);
293 COPY(regs[12]); COPY(regs[13]);
294 COPY(regs[14]); COPY(regs[15]);
295 COPY(gbr); COPY(mach);
296 COPY(macl); COPY(pr);
301 err |= save_sigcontext_fpu(sc, regs);
304 /* non-iBCS2 extensions.. */
305 err |= __put_user(mask, &sc->oldmask);
311 * Determine which stack to use..
313 static inline void __user *
314 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
316 if (ka->sa.sa_flags & SA_ONSTACK) {
317 if (sas_ss_flags(sp) == 0)
318 sp = current->sas_ss_sp + current->sas_ss_size;
321 return (void __user *)((sp - (frame_size+UNWINDGUARD)) & -8ul);
324 /* These symbols are defined with the addresses in the vsyscall page.
325 See vsyscall-trapa.S. */
326 extern void __kernel_sigreturn(void);
327 extern void __kernel_rt_sigreturn(void);
329 static int setup_frame(int sig, struct k_sigaction *ka,
330 sigset_t *set, struct pt_regs *regs)
332 struct sigframe __user *frame;
336 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
338 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
341 signal = current_thread_info()->exec_domain
342 && current_thread_info()->exec_domain->signal_invmap
344 ? current_thread_info()->exec_domain->signal_invmap[sig]
347 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
350 err |= __copy_to_user(frame->extramask, &set->sig[1],
351 sizeof(frame->extramask));
353 /* Set up to return from userspace. If provided, use a stub
354 already in userspace. */
355 if (ka->sa.sa_flags & SA_RESTORER) {
356 regs->pr = (unsigned long) ka->sa.sa_restorer;
357 #ifdef CONFIG_VSYSCALL
358 } else if (likely(current->mm->context.vdso)) {
359 regs->pr = VDSO_SYM(&__kernel_sigreturn);
362 /* Generate return code (system call to sigreturn) */
363 err |= __put_user(MOVW(7), &frame->retcode[0]);
364 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
365 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
366 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
367 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
368 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
369 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
370 err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
371 regs->pr = (unsigned long) frame->retcode;
372 flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
378 /* Set up registers for signal handler */
379 regs->regs[15] = (unsigned long) frame;
380 regs->regs[4] = signal; /* Arg for signal handler */
382 regs->regs[6] = (unsigned long) &frame->sc;
384 if (current->personality & FDPIC_FUNCPTRS) {
385 struct fdpic_func_descriptor __user *funcptr =
386 (struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
388 err |= __get_user(regs->pc, &funcptr->text);
389 err |= __get_user(regs->regs[12], &funcptr->GOT);
391 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);
404 force_sigsegv(sig, current);
408 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
409 sigset_t *set, struct pt_regs *regs)
411 struct rt_sigframe __user *frame;
415 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
417 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
420 signal = current_thread_info()->exec_domain
421 && current_thread_info()->exec_domain->signal_invmap
423 ? current_thread_info()->exec_domain->signal_invmap[sig]
426 err |= copy_siginfo_to_user(&frame->info, info);
428 /* Create the ucontext. */
429 err |= __put_user(0, &frame->uc.uc_flags);
430 err |= __put_user(NULL, &frame->uc.uc_link);
431 err |= __put_user((void *)current->sas_ss_sp,
432 &frame->uc.uc_stack.ss_sp);
433 err |= __put_user(sas_ss_flags(regs->regs[15]),
434 &frame->uc.uc_stack.ss_flags);
435 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
436 err |= setup_sigcontext(&frame->uc.uc_mcontext,
438 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
440 /* Set up to return from userspace. If provided, use a stub
441 already in userspace. */
442 if (ka->sa.sa_flags & SA_RESTORER) {
443 regs->pr = (unsigned long) ka->sa.sa_restorer;
444 #ifdef CONFIG_VSYSCALL
445 } else if (likely(current->mm->context.vdso)) {
446 regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
449 /* Generate return code (system call to rt_sigreturn) */
450 err |= __put_user(MOVW(7), &frame->retcode[0]);
451 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
452 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
453 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
454 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
455 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
456 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
457 err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
458 regs->pr = (unsigned long) frame->retcode;
459 flush_icache_range(regs->pr, regs->pr + sizeof(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 err |= __get_user(regs->pc, &funcptr->text);
476 err |= __get_user(regs->regs[12], &funcptr->GOT);
478 regs->pc = (unsigned long)ka->sa.sa_handler;
485 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
486 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
491 force_sigsegv(sig, current);
496 handle_syscall_restart(unsigned long save_r0, struct pt_regs *regs,
497 struct sigaction *sa)
499 /* If we're not from a syscall, bail out */
503 /* check for system call restart.. */
504 switch (regs->regs[0]) {
505 case -ERESTART_RESTARTBLOCK:
506 case -ERESTARTNOHAND:
507 no_system_call_restart:
508 regs->regs[0] = -EINTR;
512 if (!(sa->sa_flags & SA_RESTART))
513 goto no_system_call_restart;
515 case -ERESTARTNOINTR:
516 regs->regs[0] = save_r0;
517 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
523 * OK, we're invoking a handler
526 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
527 sigset_t *oldset, struct pt_regs *regs, unsigned int save_r0)
531 /* Set up the stack frame */
532 if (ka->sa.sa_flags & SA_SIGINFO)
533 ret = setup_rt_frame(sig, ka, info, oldset, regs);
535 ret = setup_frame(sig, ka, oldset, regs);
538 block_sigmask(ka, sig);
544 * Note that 'init' is a special process: it doesn't get signals it doesn't
545 * want to handle. Thus you cannot kill init even with a SIGKILL even by
548 * Note that we go through the signals twice: once to check the signals that
549 * the kernel can handle, and then we build all the user-level signal handling
550 * stack-frames in one go after that.
552 static void do_signal(struct pt_regs *regs, unsigned int save_r0)
556 struct k_sigaction ka;
560 * We want the common case to go fast, which
561 * is why we may in certain cases get here from
562 * kernel mode. Just return without doing anything
565 if (!user_mode(regs))
568 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
569 oldset = ¤t->saved_sigmask;
571 oldset = ¤t->blocked;
573 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
575 handle_syscall_restart(save_r0, regs, &ka.sa);
577 /* Whee! Actually deliver the signal. */
578 if (handle_signal(signr, &ka, &info, oldset,
579 regs, save_r0) == 0) {
581 * A signal was successfully delivered; the saved
582 * sigmask will have been stored in the signal frame,
583 * and will be restored by sigreturn, so we can simply
584 * clear the TS_RESTORE_SIGMASK flag
586 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
588 tracehook_signal_handler(signr, &info, &ka, regs,
589 test_thread_flag(TIF_SINGLESTEP));
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;
602 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
603 } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
604 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
605 regs->regs[3] = __NR_restart_syscall;
610 * If there's no signal to deliver, we just put the saved sigmask
613 restore_saved_sigmask();
616 asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
617 unsigned long thread_info_flags)
619 /* deal with pending signal delivery */
620 if (thread_info_flags & _TIF_SIGPENDING)
621 do_signal(regs, save_r0);
623 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
624 clear_thread_flag(TIF_NOTIFY_RESUME);
625 tracehook_notify_resume(regs);