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 struct fdpic_func_descriptor {
41 * The following define adds a 64 byte gap between the signal
42 * stack frame and previous contents of the stack. This allows
43 * frame unwinding in a function epilogue but only if a frame
44 * pointer is used in the function. This is necessary because
45 * current gcc compilers (<4.3) do not generate unwind info on
46 * SH for function epilogues.
48 #define UNWINDGUARD 64
51 * Atomically swap in the new signal mask, and wait for a signal.
54 sys_sigsuspend(old_sigset_t mask)
57 siginitset(&blocked, mask);
58 return sigsuspend(&blocked);
62 sys_sigaction(int sig, const struct old_sigaction __user *act,
63 struct old_sigaction __user *oact)
65 struct k_sigaction new_ka, old_ka;
70 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
71 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
72 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
73 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
74 __get_user(mask, &act->sa_mask))
76 siginitset(&new_ka.sa.sa_mask, mask);
79 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
82 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
83 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
84 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
85 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
86 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
94 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
95 unsigned long r6, unsigned long r7,
96 struct pt_regs __regs)
98 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
100 return do_sigaltstack(uss, uoss, regs->regs[15]);
105 * Do a signal return; undo the signal stack.
108 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
109 #if defined(CONFIG_CPU_SH2)
110 #define TRAP_NOARG 0xc320 /* Syscall w/no args (NR in R3) */
112 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) */
114 #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
118 struct sigcontext sc;
119 unsigned long extramask[_NSIG_WORDS-1];
131 static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
133 struct task_struct *tsk = current;
135 if (!(boot_cpu_data.flags & CPU_HAS_FPU))
139 return __copy_from_user(&tsk->thread.xstate->hardfpu, &sc->sc_fpregs[0],
140 sizeof(long)*(16*2+2));
143 static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
144 struct pt_regs *regs)
146 struct task_struct *tsk = current;
148 if (!(boot_cpu_data.flags & CPU_HAS_FPU))
152 return __put_user(0, &sc->sc_ownedfp);
154 if (__put_user(1, &sc->sc_ownedfp))
157 /* This will cause a "finit" to be triggered by the next
158 attempted FPU operation by the 'current' process.
162 unlazy_fpu(tsk, regs);
163 return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.xstate->hardfpu,
164 sizeof(long)*(16*2+2));
166 #endif /* CONFIG_SH_FPU */
169 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
171 unsigned int err = 0;
173 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
175 COPY(regs[2]); COPY(regs[3]);
176 COPY(regs[4]); COPY(regs[5]);
177 COPY(regs[6]); COPY(regs[7]);
178 COPY(regs[8]); COPY(regs[9]);
179 COPY(regs[10]); COPY(regs[11]);
180 COPY(regs[12]); COPY(regs[13]);
181 COPY(regs[14]); COPY(regs[15]);
182 COPY(gbr); COPY(mach);
183 COPY(macl); COPY(pr);
188 if (boot_cpu_data.flags & CPU_HAS_FPU) {
190 struct task_struct *tsk = current;
192 regs->sr |= SR_FD; /* Release FPU */
193 clear_fpu(tsk, regs);
195 err |= __get_user (owned_fp, &sc->sc_ownedfp);
197 err |= restore_sigcontext_fpu(sc);
201 regs->tra = -1; /* disable syscall checks */
202 err |= __get_user(*r0_p, &sc->sc_regs[0]);
206 asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
207 unsigned long r6, unsigned long r7,
208 struct pt_regs __regs)
210 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
211 struct sigframe __user *frame = (struct sigframe __user *)regs->regs[15];
215 /* Always make any pending restarted system calls return -EINTR */
216 current_thread_info()->restart_block.fn = do_no_restart_syscall;
218 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
221 if (__get_user(set.sig[0], &frame->sc.oldmask)
223 && __copy_from_user(&set.sig[1], &frame->extramask,
224 sizeof(frame->extramask))))
227 set_current_blocked(&set);
229 if (restore_sigcontext(regs, &frame->sc, &r0))
234 force_sig(SIGSEGV, current);
238 asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
239 unsigned long r6, unsigned long r7,
240 struct pt_regs __regs)
242 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
243 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15];
247 /* Always make any pending restarted system calls return -EINTR */
248 current_thread_info()->restart_block.fn = do_no_restart_syscall;
250 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
253 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
256 set_current_blocked(&set);
258 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
261 if (do_sigaltstack(&frame->uc.uc_stack, NULL,
262 regs->regs[15]) == -EFAULT)
268 force_sig(SIGSEGV, current);
273 * Set up a signal frame.
277 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
282 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
283 COPY(regs[0]); COPY(regs[1]);
284 COPY(regs[2]); COPY(regs[3]);
285 COPY(regs[4]); COPY(regs[5]);
286 COPY(regs[6]); COPY(regs[7]);
287 COPY(regs[8]); COPY(regs[9]);
288 COPY(regs[10]); COPY(regs[11]);
289 COPY(regs[12]); COPY(regs[13]);
290 COPY(regs[14]); COPY(regs[15]);
291 COPY(gbr); COPY(mach);
292 COPY(macl); COPY(pr);
297 err |= save_sigcontext_fpu(sc, regs);
300 /* non-iBCS2 extensions.. */
301 err |= __put_user(mask, &sc->oldmask);
307 * Determine which stack to use..
309 static inline void __user *
310 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
312 if (ka->sa.sa_flags & SA_ONSTACK) {
313 if (sas_ss_flags(sp) == 0)
314 sp = current->sas_ss_sp + current->sas_ss_size;
317 return (void __user *)((sp - (frame_size+UNWINDGUARD)) & -8ul);
320 /* These symbols are defined with the addresses in the vsyscall page.
321 See vsyscall-trapa.S. */
322 extern void __kernel_sigreturn(void);
323 extern void __kernel_rt_sigreturn(void);
325 static int setup_frame(int sig, struct k_sigaction *ka,
326 sigset_t *set, struct pt_regs *regs)
328 struct sigframe __user *frame;
332 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
334 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
337 signal = current_thread_info()->exec_domain
338 && current_thread_info()->exec_domain->signal_invmap
340 ? current_thread_info()->exec_domain->signal_invmap[sig]
343 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
346 err |= __copy_to_user(frame->extramask, &set->sig[1],
347 sizeof(frame->extramask));
349 /* Set up to return from userspace. If provided, use a stub
350 already in userspace. */
351 if (ka->sa.sa_flags & SA_RESTORER) {
352 regs->pr = (unsigned long) ka->sa.sa_restorer;
353 #ifdef CONFIG_VSYSCALL
354 } else if (likely(current->mm->context.vdso)) {
355 regs->pr = VDSO_SYM(&__kernel_sigreturn);
358 /* Generate return code (system call to sigreturn) */
359 err |= __put_user(MOVW(7), &frame->retcode[0]);
360 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
361 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
362 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
363 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
364 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
365 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
366 err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
367 regs->pr = (unsigned long) frame->retcode;
368 flush_icache_range(regs->pr, regs->pr + sizeof(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;
380 if (current->personality & FDPIC_FUNCPTRS) {
381 struct fdpic_func_descriptor __user *funcptr =
382 (struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
384 err |= __get_user(regs->pc, &funcptr->text);
385 err |= __get_user(regs->regs[12], &funcptr->GOT);
387 regs->pc = (unsigned long)ka->sa.sa_handler;
394 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
395 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
400 force_sigsegv(sig, current);
404 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
405 sigset_t *set, struct pt_regs *regs)
407 struct rt_sigframe __user *frame;
411 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
413 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
416 signal = current_thread_info()->exec_domain
417 && current_thread_info()->exec_domain->signal_invmap
419 ? current_thread_info()->exec_domain->signal_invmap[sig]
422 err |= copy_siginfo_to_user(&frame->info, info);
424 /* Create the ucontext. */
425 err |= __put_user(0, &frame->uc.uc_flags);
426 err |= __put_user(NULL, &frame->uc.uc_link);
427 err |= __put_user((void *)current->sas_ss_sp,
428 &frame->uc.uc_stack.ss_sp);
429 err |= __put_user(sas_ss_flags(regs->regs[15]),
430 &frame->uc.uc_stack.ss_flags);
431 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
432 err |= setup_sigcontext(&frame->uc.uc_mcontext,
434 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
436 /* Set up to return from userspace. If provided, use a stub
437 already in userspace. */
438 if (ka->sa.sa_flags & SA_RESTORER) {
439 regs->pr = (unsigned long) ka->sa.sa_restorer;
440 #ifdef CONFIG_VSYSCALL
441 } else if (likely(current->mm->context.vdso)) {
442 regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
445 /* Generate return code (system call to rt_sigreturn) */
446 err |= __put_user(MOVW(7), &frame->retcode[0]);
447 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
448 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
449 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
450 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
451 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
452 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
453 err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
454 regs->pr = (unsigned long) frame->retcode;
455 flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
461 /* Set up registers for signal handler */
462 regs->regs[15] = (unsigned long) frame;
463 regs->regs[4] = signal; /* Arg for signal handler */
464 regs->regs[5] = (unsigned long) &frame->info;
465 regs->regs[6] = (unsigned long) &frame->uc;
467 if (current->personality & FDPIC_FUNCPTRS) {
468 struct fdpic_func_descriptor __user *funcptr =
469 (struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
471 err |= __get_user(regs->pc, &funcptr->text);
472 err |= __get_user(regs->regs[12], &funcptr->GOT);
474 regs->pc = (unsigned long)ka->sa.sa_handler;
481 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
482 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
487 force_sigsegv(sig, current);
492 handle_syscall_restart(unsigned long save_r0, struct pt_regs *regs,
493 struct sigaction *sa)
495 /* If we're not from a syscall, bail out */
499 /* check for system call restart.. */
500 switch (regs->regs[0]) {
501 case -ERESTART_RESTARTBLOCK:
502 case -ERESTARTNOHAND:
503 no_system_call_restart:
504 regs->regs[0] = -EINTR;
508 if (!(sa->sa_flags & SA_RESTART))
509 goto no_system_call_restart;
511 case -ERESTARTNOINTR:
512 regs->regs[0] = save_r0;
513 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
519 * OK, we're invoking a handler
522 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
523 struct pt_regs *regs, unsigned int save_r0)
525 sigset_t *oldset = sigmask_to_save();
528 /* Set up the stack frame */
529 if (ka->sa.sa_flags & SA_SIGINFO)
530 ret = setup_rt_frame(sig, ka, info, oldset, regs);
532 ret = setup_frame(sig, ka, oldset, regs);
536 signal_delivered(sig, info, ka, regs,
537 test_thread_flag(TIF_SINGLESTEP));
541 * Note that 'init' is a special process: it doesn't get signals it doesn't
542 * want to handle. Thus you cannot kill init even with a SIGKILL even by
545 * Note that we go through the signals twice: once to check the signals that
546 * the kernel can handle, and then we build all the user-level signal handling
547 * stack-frames in one go after that.
549 static void do_signal(struct pt_regs *regs, unsigned int save_r0)
553 struct k_sigaction ka;
556 * We want the common case to go fast, which
557 * is why we may in certain cases get here from
558 * kernel mode. Just return without doing anything
561 if (!user_mode(regs))
564 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
566 handle_syscall_restart(save_r0, regs, &ka.sa);
568 /* Whee! Actually deliver the signal. */
569 handle_signal(signr, &ka, &info, regs, save_r0);
573 /* Did we come from a system call? */
574 if (regs->tra >= 0) {
575 /* Restart the system call - no handlers present */
576 if (regs->regs[0] == -ERESTARTNOHAND ||
577 regs->regs[0] == -ERESTARTSYS ||
578 regs->regs[0] == -ERESTARTNOINTR) {
579 regs->regs[0] = save_r0;
580 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
581 } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
582 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
583 regs->regs[3] = __NR_restart_syscall;
588 * If there's no signal to deliver, we just put the saved sigmask
591 restore_saved_sigmask();
594 asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
595 unsigned long thread_info_flags)
597 /* deal with pending signal delivery */
598 if (thread_info_flags & _TIF_SIGPENDING)
599 do_signal(regs, save_r0);
601 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
602 clear_thread_flag(TIF_NOTIFY_RESUME);
603 tracehook_notify_resume(regs);