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>
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,
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,
92 return do_sigaltstack(uss, uoss, regs.regs[15]);
97 * Do a signal return; undo the signal stack.
100 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
101 #define TRAP16 0xc310 /* Syscall w/no args (NR in R3) */
102 #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
106 struct sigcontext sc;
107 unsigned long extramask[_NSIG_WORDS-1];
119 static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
121 struct task_struct *tsk = current;
123 if (!(cpu_data->flags & CPU_HAS_FPU))
127 return __copy_from_user(&tsk->thread.fpu.hard, &sc->sc_fpregs[0],
128 sizeof(long)*(16*2+2));
131 static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
132 struct pt_regs *regs)
134 struct task_struct *tsk = current;
136 if (!(cpu_data->flags & CPU_HAS_FPU))
140 __put_user(0, &sc->sc_ownedfp);
144 __put_user(1, &sc->sc_ownedfp);
146 /* This will cause a "finit" to be triggered by the next
147 attempted FPU operation by the 'current' process.
151 unlazy_fpu(tsk, regs);
152 return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.fpu.hard,
153 sizeof(long)*(16*2+2));
155 #endif /* CONFIG_SH_FPU */
158 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
160 unsigned int err = 0;
162 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
164 COPY(regs[2]); COPY(regs[3]);
165 COPY(regs[4]); COPY(regs[5]);
166 COPY(regs[6]); COPY(regs[7]);
167 COPY(regs[8]); COPY(regs[9]);
168 COPY(regs[10]); COPY(regs[11]);
169 COPY(regs[12]); COPY(regs[13]);
170 COPY(regs[14]); COPY(regs[15]);
171 COPY(gbr); COPY(mach);
172 COPY(macl); COPY(pr);
177 if (cpu_data->flags & CPU_HAS_FPU) {
179 struct task_struct *tsk = current;
181 regs->sr |= SR_FD; /* Release FPU */
182 clear_fpu(tsk, regs);
184 __get_user (owned_fp, &sc->sc_ownedfp);
186 err |= restore_sigcontext_fpu(sc);
190 regs->tra = -1; /* disable syscall checks */
191 err |= __get_user(*r0_p, &sc->sc_regs[0]);
195 asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
196 unsigned long r6, unsigned long r7,
199 struct sigframe __user *frame = (struct sigframe __user *)regs.regs[15];
203 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
206 if (__get_user(set.sig[0], &frame->sc.oldmask)
208 && __copy_from_user(&set.sig[1], &frame->extramask,
209 sizeof(frame->extramask))))
212 sigdelsetmask(&set, ~_BLOCKABLE);
214 spin_lock_irq(¤t->sighand->siglock);
215 current->blocked = set;
217 spin_unlock_irq(¤t->sighand->siglock);
219 if (restore_sigcontext(®s, &frame->sc, &r0))
224 force_sig(SIGSEGV, current);
228 asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
229 unsigned long r6, unsigned long r7,
232 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs.regs[15];
237 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
240 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
243 sigdelsetmask(&set, ~_BLOCKABLE);
244 spin_lock_irq(¤t->sighand->siglock);
245 current->blocked = set;
247 spin_unlock_irq(¤t->sighand->siglock);
249 if (restore_sigcontext(®s, &frame->uc.uc_mcontext, &r0))
252 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
254 /* It is more difficult to avoid calling this function than to
255 call it and ignore errors. */
256 do_sigaltstack(&st, NULL, regs.regs[15]);
261 force_sig(SIGSEGV, current);
266 * Set up a signal frame.
270 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
275 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
276 COPY(regs[0]); COPY(regs[1]);
277 COPY(regs[2]); COPY(regs[3]);
278 COPY(regs[4]); COPY(regs[5]);
279 COPY(regs[6]); COPY(regs[7]);
280 COPY(regs[8]); COPY(regs[9]);
281 COPY(regs[10]); COPY(regs[11]);
282 COPY(regs[12]); COPY(regs[13]);
283 COPY(regs[14]); COPY(regs[15]);
284 COPY(gbr); COPY(mach);
285 COPY(macl); COPY(pr);
290 err |= save_sigcontext_fpu(sc, regs);
293 /* non-iBCS2 extensions.. */
294 err |= __put_user(mask, &sc->oldmask);
300 * Determine which stack to use..
302 static inline void __user *
303 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
305 if (ka->sa.sa_flags & SA_ONSTACK) {
306 if (sas_ss_flags(sp) == 0)
307 sp = current->sas_ss_sp + current->sas_ss_size;
310 return (void __user *)((sp - frame_size) & -8ul);
313 /* These symbols are defined with the addresses in the vsyscall page.
314 See vsyscall-trapa.S. */
315 extern void __user __kernel_sigreturn;
316 extern void __user __kernel_rt_sigreturn;
318 static int setup_frame(int sig, struct k_sigaction *ka,
319 sigset_t *set, struct pt_regs *regs)
321 struct sigframe __user *frame;
325 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
327 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
330 signal = current_thread_info()->exec_domain
331 && current_thread_info()->exec_domain->signal_invmap
333 ? current_thread_info()->exec_domain->signal_invmap[sig]
336 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
339 err |= __copy_to_user(frame->extramask, &set->sig[1],
340 sizeof(frame->extramask));
342 /* Set up to return from userspace. If provided, use a stub
343 already in userspace. */
344 if (ka->sa.sa_flags & SA_RESTORER) {
345 regs->pr = (unsigned long) ka->sa.sa_restorer;
346 #ifdef CONFIG_VSYSCALL
347 } else if (likely(current->mm->context.vdso)) {
348 regs->pr = VDSO_SYM(&__kernel_sigreturn);
351 /* Generate return code (system call to sigreturn) */
352 err |= __put_user(MOVW(7), &frame->retcode[0]);
353 err |= __put_user(TRAP16, &frame->retcode[1]);
354 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
355 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
356 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
357 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
358 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
359 err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
360 regs->pr = (unsigned long) frame->retcode;
366 /* Set up registers for signal handler */
367 regs->regs[15] = (unsigned long) frame;
368 regs->regs[4] = signal; /* Arg for signal handler */
370 regs->regs[6] = (unsigned long) &frame->sc;
371 regs->pc = (unsigned long) ka->sa.sa_handler;
375 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
376 current->comm, current->pid, frame, regs->pc, regs->pr);
378 flush_cache_sigtramp(regs->pr);
380 if ((-regs->pr & (L1_CACHE_BYTES-1)) < sizeof(frame->retcode))
381 flush_cache_sigtramp(regs->pr + L1_CACHE_BYTES);
386 force_sigsegv(sig, current);
390 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
391 sigset_t *set, struct pt_regs *regs)
393 struct rt_sigframe __user *frame;
397 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
399 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
402 signal = current_thread_info()->exec_domain
403 && current_thread_info()->exec_domain->signal_invmap
405 ? current_thread_info()->exec_domain->signal_invmap[sig]
408 err |= copy_siginfo_to_user(&frame->info, info);
410 /* Create the ucontext. */
411 err |= __put_user(0, &frame->uc.uc_flags);
412 err |= __put_user(0, &frame->uc.uc_link);
413 err |= __put_user((void *)current->sas_ss_sp,
414 &frame->uc.uc_stack.ss_sp);
415 err |= __put_user(sas_ss_flags(regs->regs[15]),
416 &frame->uc.uc_stack.ss_flags);
417 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
418 err |= setup_sigcontext(&frame->uc.uc_mcontext,
420 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
422 /* Set up to return from userspace. If provided, use a stub
423 already in userspace. */
424 if (ka->sa.sa_flags & SA_RESTORER) {
425 regs->pr = (unsigned long) ka->sa.sa_restorer;
426 #ifdef CONFIG_VSYSCALL
427 } else if (likely(current->mm->context.vdso)) {
428 regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
431 /* Generate return code (system call to rt_sigreturn) */
432 err |= __put_user(MOVW(7), &frame->retcode[0]);
433 err |= __put_user(TRAP16, &frame->retcode[1]);
434 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
435 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
436 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
437 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
438 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
439 err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
440 regs->pr = (unsigned long) frame->retcode;
446 /* Set up registers for signal handler */
447 regs->regs[15] = (unsigned long) frame;
448 regs->regs[4] = signal; /* Arg for signal handler */
449 regs->regs[5] = (unsigned long) &frame->info;
450 regs->regs[6] = (unsigned long) &frame->uc;
451 regs->pc = (unsigned long) ka->sa.sa_handler;
455 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
456 current->comm, current->pid, frame, regs->pc, regs->pr);
458 flush_cache_sigtramp(regs->pr);
460 if ((-regs->pr & (L1_CACHE_BYTES-1)) < sizeof(frame->retcode))
461 flush_cache_sigtramp(regs->pr + L1_CACHE_BYTES);
466 force_sigsegv(sig, current);
471 * OK, we're invoking a handler
475 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
476 sigset_t *oldset, struct pt_regs *regs)
480 /* Are we from a system call? */
481 if (regs->tra >= 0) {
482 /* If so, check system call restarting.. */
483 switch (regs->regs[0]) {
484 case -ERESTARTNOHAND:
485 regs->regs[0] = -EINTR;
489 if (!(ka->sa.sa_flags & SA_RESTART)) {
490 regs->regs[0] = -EINTR;
494 case -ERESTARTNOINTR:
499 #ifdef CONFIG_PREEMPT
502 local_irq_save(flags);
504 if (regs->regs[15] >= 0xc0000000) {
505 int offset = (int)regs->regs[15];
507 /* Reset stack pointer: clear critical region mark */
508 regs->regs[15] = regs->regs[1];
509 if (regs->pc < regs->regs[0])
510 /* Go to rewind point #1 */
511 regs->pc = regs->regs[0] + offset - 2;
513 #ifdef CONFIG_PREEMPT
514 local_irq_restore(flags);
518 /* Set up the stack frame */
519 if (ka->sa.sa_flags & SA_SIGINFO)
520 ret = setup_rt_frame(sig, ka, info, oldset, regs);
522 ret = setup_frame(sig, ka, oldset, regs);
524 if (ka->sa.sa_flags & SA_ONESHOT)
525 ka->sa.sa_handler = SIG_DFL;
528 spin_lock_irq(¤t->sighand->siglock);
529 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
530 if (!(ka->sa.sa_flags & SA_NODEFER))
531 sigaddset(¤t->blocked,sig);
533 spin_unlock_irq(¤t->sighand->siglock);
540 * Note that 'init' is a special process: it doesn't get signals it doesn't
541 * want to handle. Thus you cannot kill init even with a SIGKILL even by
544 * Note that we go through the signals twice: once to check the signals that
545 * the kernel can handle, and then we build all the user-level signal handling
546 * stack-frames in one go after that.
548 static void do_signal(struct pt_regs *regs, unsigned int save_r0)
552 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))
567 if (test_thread_flag(TIF_RESTORE_SIGMASK))
568 oldset = ¤t->saved_sigmask;
570 oldset = ¤t->blocked;
572 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
574 /* Whee! Actually deliver the signal. */
575 if (handle_signal(signr, &ka, &info, oldset, regs) == 0) {
576 /* a signal was successfully delivered; the saved
577 * sigmask will have been stored in the signal frame,
578 * and will be restored by sigreturn, so we can simply
579 * clear the TIF_RESTORE_SIGMASK flag */
580 if (test_thread_flag(TIF_RESTORE_SIGMASK))
581 clear_thread_flag(TIF_RESTORE_SIGMASK);
586 /* Did we come from a system call? */
587 if (regs->tra >= 0) {
588 /* Restart the system call - no handlers present */
589 if (regs->regs[0] == -ERESTARTNOHAND ||
590 regs->regs[0] == -ERESTARTSYS ||
591 regs->regs[0] == -ERESTARTNOINTR) {
592 regs->regs[0] = save_r0;
594 } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
596 regs->regs[3] = __NR_restart_syscall;
600 /* if there's no signal to deliver, we just put the saved sigmask
602 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
603 clear_thread_flag(TIF_RESTORE_SIGMASK);
604 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
608 asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
609 __u32 thread_info_flags)
611 /* deal with pending signal delivery */
612 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
613 do_signal(regs, save_r0);