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
12 #include <linux/sched.h>
14 #include <linux/smp.h>
15 #include <linux/smp_lock.h>
16 #include <linux/kernel.h>
17 #include <linux/signal.h>
18 #include <linux/errno.h>
19 #include <linux/wait.h>
20 #include <linux/ptrace.h>
21 #include <linux/unistd.h>
22 #include <linux/stddef.h>
23 #include <linux/tty.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>
34 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
37 * Atomically swap in the new signal mask, and wait for a signal.
40 sys_sigsuspend(old_sigset_t mask,
41 unsigned long r5, unsigned long r6, unsigned long r7,
45 spin_lock_irq(¤t->sighand->siglock);
46 current->saved_sigmask = current->blocked;
47 siginitset(¤t->blocked, mask);
49 spin_unlock_irq(¤t->sighand->siglock);
51 current->state = TASK_INTERRUPTIBLE;
53 set_thread_flag(TIF_RESTORE_SIGMASK);
54 return -ERESTARTNOHAND;
58 sys_sigaction(int sig, const struct old_sigaction __user *act,
59 struct old_sigaction __user *oact)
61 struct k_sigaction new_ka, old_ka;
66 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
67 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
68 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
70 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
71 __get_user(mask, &act->sa_mask);
72 siginitset(&new_ka.sa.sa_mask, mask);
75 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
78 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
79 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
80 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
82 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
83 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
90 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
91 unsigned long r6, unsigned long r7,
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 #define TRAP16 0xc310 /* Syscall w/no args (NR in R3) */
104 #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
108 struct sigcontext sc;
109 unsigned long extramask[_NSIG_WORDS-1];
121 static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
123 struct task_struct *tsk = current;
125 if (!(cpu_data->flags & CPU_HAS_FPU))
129 return __copy_from_user(&tsk->thread.fpu.hard, &sc->sc_fpregs[0],
130 sizeof(long)*(16*2+2));
133 static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
134 struct pt_regs *regs)
136 struct task_struct *tsk = current;
138 if (!(cpu_data->flags & CPU_HAS_FPU))
142 __put_user(0, &sc->sc_ownedfp);
146 __put_user(1, &sc->sc_ownedfp);
148 /* This will cause a "finit" to be triggered by the next
149 attempted FPU operation by the 'current' process.
153 unlazy_fpu(tsk, regs);
154 return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.fpu.hard,
155 sizeof(long)*(16*2+2));
157 #endif /* CONFIG_SH_FPU */
160 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
162 unsigned int err = 0;
164 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
166 COPY(regs[2]); COPY(regs[3]);
167 COPY(regs[4]); COPY(regs[5]);
168 COPY(regs[6]); COPY(regs[7]);
169 COPY(regs[8]); COPY(regs[9]);
170 COPY(regs[10]); COPY(regs[11]);
171 COPY(regs[12]); COPY(regs[13]);
172 COPY(regs[14]); COPY(regs[15]);
173 COPY(gbr); COPY(mach);
174 COPY(macl); COPY(pr);
179 if (cpu_data->flags & CPU_HAS_FPU) {
181 struct task_struct *tsk = current;
183 regs->sr |= SR_FD; /* Release FPU */
184 clear_fpu(tsk, regs);
186 __get_user (owned_fp, &sc->sc_ownedfp);
188 err |= restore_sigcontext_fpu(sc);
192 regs->tra = -1; /* disable syscall checks */
193 err |= __get_user(*r0_p, &sc->sc_regs[0]);
197 asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
198 unsigned long r6, unsigned long r7,
201 struct sigframe __user *frame = (struct sigframe __user *)regs.regs[15];
205 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
208 if (__get_user(set.sig[0], &frame->sc.oldmask)
210 && __copy_from_user(&set.sig[1], &frame->extramask,
211 sizeof(frame->extramask))))
214 sigdelsetmask(&set, ~_BLOCKABLE);
216 spin_lock_irq(¤t->sighand->siglock);
217 current->blocked = set;
219 spin_unlock_irq(¤t->sighand->siglock);
221 if (restore_sigcontext(®s, &frame->sc, &r0))
226 force_sig(SIGSEGV, current);
230 asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
231 unsigned long r6, unsigned long r7,
234 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs.regs[15];
239 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
242 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
245 sigdelsetmask(&set, ~_BLOCKABLE);
246 spin_lock_irq(¤t->sighand->siglock);
247 current->blocked = set;
249 spin_unlock_irq(¤t->sighand->siglock);
251 if (restore_sigcontext(®s, &frame->uc.uc_mcontext, &r0))
254 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
256 /* It is more difficult to avoid calling this function than to
257 call it and ignore errors. */
258 do_sigaltstack(&st, NULL, regs.regs[15]);
263 force_sig(SIGSEGV, current);
268 * Set up a signal frame.
272 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
277 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
278 COPY(regs[0]); COPY(regs[1]);
279 COPY(regs[2]); COPY(regs[3]);
280 COPY(regs[4]); COPY(regs[5]);
281 COPY(regs[6]); COPY(regs[7]);
282 COPY(regs[8]); COPY(regs[9]);
283 COPY(regs[10]); COPY(regs[11]);
284 COPY(regs[12]); COPY(regs[13]);
285 COPY(regs[14]); COPY(regs[15]);
286 COPY(gbr); COPY(mach);
287 COPY(macl); COPY(pr);
292 err |= save_sigcontext_fpu(sc, regs);
295 /* non-iBCS2 extensions.. */
296 err |= __put_user(mask, &sc->oldmask);
302 * Determine which stack to use..
304 static inline void __user *
305 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
307 if (ka->sa.sa_flags & SA_ONSTACK) {
308 if (sas_ss_flags(sp) == 0)
309 sp = current->sas_ss_sp + current->sas_ss_size;
312 return (void __user *)((sp - frame_size) & -8ul);
315 static int setup_frame(int sig, struct k_sigaction *ka,
316 sigset_t *set, struct pt_regs *regs)
318 struct sigframe __user *frame;
322 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
324 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
327 signal = current_thread_info()->exec_domain
328 && current_thread_info()->exec_domain->signal_invmap
330 ? current_thread_info()->exec_domain->signal_invmap[sig]
333 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
336 err |= __copy_to_user(frame->extramask, &set->sig[1],
337 sizeof(frame->extramask));
339 /* Set up to return from userspace. If provided, use a stub
340 already in userspace. */
341 if (ka->sa.sa_flags & SA_RESTORER) {
342 regs->pr = (unsigned long) ka->sa.sa_restorer;
344 /* Generate return code (system call to sigreturn) */
345 err |= __put_user(MOVW(7), &frame->retcode[0]);
346 err |= __put_user(TRAP16, &frame->retcode[1]);
347 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
348 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
349 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
350 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
351 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
352 err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
353 regs->pr = (unsigned long) frame->retcode;
359 /* Set up registers for signal handler */
360 regs->regs[15] = (unsigned long) frame;
361 regs->regs[4] = signal; /* Arg for signal handler */
363 regs->regs[6] = (unsigned long) &frame->sc;
364 regs->pc = (unsigned long) ka->sa.sa_handler;
368 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
369 current->comm, current->pid, frame, regs->pc, regs->pr);
371 flush_cache_sigtramp(regs->pr);
373 if ((-regs->pr & (L1_CACHE_BYTES-1)) < sizeof(frame->retcode))
374 flush_cache_sigtramp(regs->pr + L1_CACHE_BYTES);
379 force_sigsegv(sig, current);
383 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
384 sigset_t *set, struct pt_regs *regs)
386 struct rt_sigframe __user *frame;
390 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
392 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
395 signal = current_thread_info()->exec_domain
396 && current_thread_info()->exec_domain->signal_invmap
398 ? current_thread_info()->exec_domain->signal_invmap[sig]
401 err |= copy_siginfo_to_user(&frame->info, info);
403 /* Create the ucontext. */
404 err |= __put_user(0, &frame->uc.uc_flags);
405 err |= __put_user(0, &frame->uc.uc_link);
406 err |= __put_user((void *)current->sas_ss_sp,
407 &frame->uc.uc_stack.ss_sp);
408 err |= __put_user(sas_ss_flags(regs->regs[15]),
409 &frame->uc.uc_stack.ss_flags);
410 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
411 err |= setup_sigcontext(&frame->uc.uc_mcontext,
413 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
415 /* Set up to return from userspace. If provided, use a stub
416 already in userspace. */
417 if (ka->sa.sa_flags & SA_RESTORER) {
418 regs->pr = (unsigned long) ka->sa.sa_restorer;
420 /* Generate return code (system call to rt_sigreturn) */
421 err |= __put_user(MOVW(7), &frame->retcode[0]);
422 err |= __put_user(TRAP16, &frame->retcode[1]);
423 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
424 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
425 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
426 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
427 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
428 err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
429 regs->pr = (unsigned long) frame->retcode;
435 /* Set up registers for signal handler */
436 regs->regs[15] = (unsigned long) frame;
437 regs->regs[4] = signal; /* Arg for signal handler */
438 regs->regs[5] = (unsigned long) &frame->info;
439 regs->regs[6] = (unsigned long) &frame->uc;
440 regs->pc = (unsigned long) ka->sa.sa_handler;
444 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
445 current->comm, current->pid, frame, regs->pc, regs->pr);
447 flush_cache_sigtramp(regs->pr);
449 if ((-regs->pr & (L1_CACHE_BYTES-1)) < sizeof(frame->retcode))
450 flush_cache_sigtramp(regs->pr + L1_CACHE_BYTES);
455 force_sigsegv(sig, current);
460 * OK, we're invoking a handler
464 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
465 sigset_t *oldset, struct pt_regs *regs)
469 /* Are we from a system call? */
470 if (regs->tra >= 0) {
471 /* If so, check system call restarting.. */
472 switch (regs->regs[0]) {
473 case -ERESTARTNOHAND:
474 regs->regs[0] = -EINTR;
478 if (!(ka->sa.sa_flags & SA_RESTART)) {
479 regs->regs[0] = -EINTR;
483 case -ERESTARTNOINTR:
488 #ifdef CONFIG_PREEMPT
491 local_irq_save(flags);
493 if (regs->regs[15] >= 0xc0000000) {
494 int offset = (int)regs->regs[15];
496 /* Reset stack pointer: clear critical region mark */
497 regs->regs[15] = regs->regs[1];
498 if (regs->pc < regs->regs[0])
499 /* Go to rewind point #1 */
500 regs->pc = regs->regs[0] + offset - 2;
502 #ifdef CONFIG_PREEMPT
503 local_irq_restore(flags);
507 /* Set up the stack frame */
508 if (ka->sa.sa_flags & SA_SIGINFO)
509 ret = setup_rt_frame(sig, ka, info, oldset, regs);
511 ret = setup_frame(sig, ka, oldset, regs);
513 if (ka->sa.sa_flags & SA_ONESHOT)
514 ka->sa.sa_handler = SIG_DFL;
517 spin_lock_irq(¤t->sighand->siglock);
518 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
519 if (!(ka->sa.sa_flags & SA_NODEFER))
520 sigaddset(¤t->blocked,sig);
522 spin_unlock_irq(¤t->sighand->siglock);
529 * Note that 'init' is a special process: it doesn't get signals it doesn't
530 * want to handle. Thus you cannot kill init even with a SIGKILL even by
533 * Note that we go through the signals twice: once to check the signals that
534 * the kernel can handle, and then we build all the user-level signal handling
535 * stack-frames in one go after that.
537 static void do_signal(struct pt_regs *regs, unsigned int save_r0)
541 struct k_sigaction ka;
545 * We want the common case to go fast, which
546 * is why we may in certain cases get here from
547 * kernel mode. Just return without doing anything
550 if (!user_mode(regs))
556 if (test_thread_flag(TIF_RESTORE_SIGMASK))
557 oldset = ¤t->saved_sigmask;
559 oldset = ¤t->blocked;
561 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
563 /* Whee! Actually deliver the signal. */
564 if (handle_signal(signr, &ka, &info, oldset, regs) == 0) {
565 /* a signal was successfully delivered; the saved
566 * sigmask will have been stored in the signal frame,
567 * and will be restored by sigreturn, so we can simply
568 * clear the TIF_RESTORE_SIGMASK flag */
569 if (test_thread_flag(TIF_RESTORE_SIGMASK))
570 clear_thread_flag(TIF_RESTORE_SIGMASK);
575 /* Did we come from a system call? */
576 if (regs->tra >= 0) {
577 /* Restart the system call - no handlers present */
578 if (regs->regs[0] == -ERESTARTNOHAND ||
579 regs->regs[0] == -ERESTARTSYS ||
580 regs->regs[0] == -ERESTARTNOINTR) {
581 regs->regs[0] = save_r0;
583 } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
585 regs->regs[3] = __NR_restart_syscall;
589 /* if there's no signal to deliver, we just put the saved sigmask
591 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
592 clear_thread_flag(TIF_RESTORE_SIGMASK);
593 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
597 asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
598 __u32 thread_info_flags)
600 /* deal with pending signal delivery */
601 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
602 do_signal(regs, save_r0);