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)))
36 asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset);
39 * Atomically swap in the new signal mask, and wait for a signal.
42 sys_sigsuspend(old_sigset_t mask,
43 unsigned long r5, unsigned long r6, unsigned long r7,
49 spin_lock_irq(¤t->sighand->siglock);
50 saveset = current->blocked;
51 siginitset(¤t->blocked, mask);
53 spin_unlock_irq(¤t->sighand->siglock);
55 regs.regs[0] = -EINTR;
57 current->state = TASK_INTERRUPTIBLE;
59 if (do_signal(®s, &saveset))
65 sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize,
66 unsigned long r6, unsigned long r7,
69 sigset_t saveset, newset;
71 /* XXX: Don't preclude handling different sized sigset_t's. */
72 if (sigsetsize != sizeof(sigset_t))
75 if (copy_from_user(&newset, unewset, sizeof(newset)))
77 sigdelsetmask(&newset, ~_BLOCKABLE);
78 spin_lock_irq(¤t->sighand->siglock);
79 saveset = current->blocked;
80 current->blocked = newset;
82 spin_unlock_irq(¤t->sighand->siglock);
84 regs.regs[0] = -EINTR;
86 current->state = TASK_INTERRUPTIBLE;
88 if (do_signal(®s, &saveset))
94 sys_sigaction(int sig, const struct old_sigaction __user *act,
95 struct old_sigaction __user *oact)
97 struct k_sigaction new_ka, old_ka;
102 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
103 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
104 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
106 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
107 __get_user(mask, &act->sa_mask);
108 siginitset(&new_ka.sa.sa_mask, mask);
111 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
114 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
115 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
116 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
118 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
119 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
126 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
127 unsigned long r6, unsigned long r7,
130 return do_sigaltstack(uss, uoss, regs.regs[15]);
135 * Do a signal return; undo the signal stack.
138 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
139 #define TRAP16 0xc310 /* Syscall w/no args (NR in R3) */
140 #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
144 struct sigcontext sc;
145 unsigned long extramask[_NSIG_WORDS-1];
157 static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
159 struct task_struct *tsk = current;
161 if (!(cpu_data->flags & CPU_HAS_FPU))
165 return __copy_from_user(&tsk->thread.fpu.hard, &sc->sc_fpregs[0],
166 sizeof(long)*(16*2+2));
169 static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
170 struct pt_regs *regs)
172 struct task_struct *tsk = current;
174 if (!(cpu_data->flags & CPU_HAS_FPU))
178 __put_user(0, &sc->sc_ownedfp);
182 __put_user(1, &sc->sc_ownedfp);
184 /* This will cause a "finit" to be triggered by the next
185 attempted FPU operation by the 'current' process.
189 unlazy_fpu(tsk, regs);
190 return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.fpu.hard,
191 sizeof(long)*(16*2+2));
193 #endif /* CONFIG_SH_FPU */
196 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
198 unsigned int err = 0;
200 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
202 COPY(regs[2]); COPY(regs[3]);
203 COPY(regs[4]); COPY(regs[5]);
204 COPY(regs[6]); COPY(regs[7]);
205 COPY(regs[8]); COPY(regs[9]);
206 COPY(regs[10]); COPY(regs[11]);
207 COPY(regs[12]); COPY(regs[13]);
208 COPY(regs[14]); COPY(regs[15]);
209 COPY(gbr); COPY(mach);
210 COPY(macl); COPY(pr);
215 if (cpu_data->flags & CPU_HAS_FPU) {
217 struct task_struct *tsk = current;
219 regs->sr |= SR_FD; /* Release FPU */
220 clear_fpu(tsk, regs);
222 __get_user (owned_fp, &sc->sc_ownedfp);
224 err |= restore_sigcontext_fpu(sc);
228 regs->tra = -1; /* disable syscall checks */
229 err |= __get_user(*r0_p, &sc->sc_regs[0]);
233 asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
234 unsigned long r6, unsigned long r7,
237 struct sigframe __user *frame = (struct sigframe __user *)regs.regs[15];
241 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
244 if (__get_user(set.sig[0], &frame->sc.oldmask)
246 && __copy_from_user(&set.sig[1], &frame->extramask,
247 sizeof(frame->extramask))))
250 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(®s, &frame->sc, &r0))
262 force_sig(SIGSEGV, current);
266 asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
267 unsigned long r6, unsigned long r7,
270 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs.regs[15];
275 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
278 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
281 sigdelsetmask(&set, ~_BLOCKABLE);
282 spin_lock_irq(¤t->sighand->siglock);
283 current->blocked = set;
285 spin_unlock_irq(¤t->sighand->siglock);
287 if (restore_sigcontext(®s, &frame->uc.uc_mcontext, &r0))
290 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
292 /* It is more difficult to avoid calling this function than to
293 call it and ignore errors. */
294 do_sigaltstack(&st, NULL, regs.regs[15]);
299 force_sig(SIGSEGV, current);
304 * Set up a signal frame.
308 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
313 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
314 COPY(regs[0]); COPY(regs[1]);
315 COPY(regs[2]); COPY(regs[3]);
316 COPY(regs[4]); COPY(regs[5]);
317 COPY(regs[6]); COPY(regs[7]);
318 COPY(regs[8]); COPY(regs[9]);
319 COPY(regs[10]); COPY(regs[11]);
320 COPY(regs[12]); COPY(regs[13]);
321 COPY(regs[14]); COPY(regs[15]);
322 COPY(gbr); COPY(mach);
323 COPY(macl); COPY(pr);
328 err |= save_sigcontext_fpu(sc, regs);
331 /* non-iBCS2 extensions.. */
332 err |= __put_user(mask, &sc->oldmask);
338 * Determine which stack to use..
340 static inline void __user *
341 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
343 if (ka->sa.sa_flags & SA_ONSTACK) {
344 if (sas_ss_flags(sp) == 0)
345 sp = current->sas_ss_sp + current->sas_ss_size;
348 return (void __user *)((sp - frame_size) & -8ul);
351 static void setup_frame(int sig, struct k_sigaction *ka,
352 sigset_t *set, struct pt_regs *regs)
354 struct sigframe __user *frame;
358 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
360 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
363 signal = current_thread_info()->exec_domain
364 && current_thread_info()->exec_domain->signal_invmap
366 ? current_thread_info()->exec_domain->signal_invmap[sig]
369 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
371 if (_NSIG_WORDS > 1) {
372 err |= __copy_to_user(frame->extramask, &set->sig[1],
373 sizeof(frame->extramask));
376 /* Set up to return from userspace. If provided, use a stub
377 already in userspace. */
378 if (ka->sa.sa_flags & SA_RESTORER) {
379 regs->pr = (unsigned long) ka->sa.sa_restorer;
381 /* Generate return code (system call to sigreturn) */
382 err |= __put_user(MOVW(7), &frame->retcode[0]);
383 err |= __put_user(TRAP16, &frame->retcode[1]);
384 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
385 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
386 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
387 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
388 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
389 err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
390 regs->pr = (unsigned long) frame->retcode;
396 /* Set up registers for signal handler */
397 regs->regs[15] = (unsigned long) frame;
398 regs->regs[4] = signal; /* Arg for signal handler */
400 regs->regs[6] = (unsigned long) &frame->sc;
401 regs->pc = (unsigned long) ka->sa.sa_handler;
406 printk("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
407 current->comm, current->pid, frame, regs->pc, regs->pr);
410 flush_cache_sigtramp(regs->pr);
411 if ((-regs->pr & (L1_CACHE_BYTES-1)) < sizeof(frame->retcode))
412 flush_cache_sigtramp(regs->pr + L1_CACHE_BYTES);
416 force_sigsegv(sig, current);
419 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
420 sigset_t *set, struct pt_regs *regs)
422 struct rt_sigframe __user *frame;
426 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
428 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
431 signal = current_thread_info()->exec_domain
432 && current_thread_info()->exec_domain->signal_invmap
434 ? current_thread_info()->exec_domain->signal_invmap[sig]
437 err |= copy_siginfo_to_user(&frame->info, info);
439 /* Create the ucontext. */
440 err |= __put_user(0, &frame->uc.uc_flags);
441 err |= __put_user(0, &frame->uc.uc_link);
442 err |= __put_user((void *)current->sas_ss_sp,
443 &frame->uc.uc_stack.ss_sp);
444 err |= __put_user(sas_ss_flags(regs->regs[15]),
445 &frame->uc.uc_stack.ss_flags);
446 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
447 err |= setup_sigcontext(&frame->uc.uc_mcontext,
449 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
451 /* Set up to return from userspace. If provided, use a stub
452 already in userspace. */
453 if (ka->sa.sa_flags & SA_RESTORER) {
454 regs->pr = (unsigned long) ka->sa.sa_restorer;
456 /* Generate return code (system call to rt_sigreturn) */
457 err |= __put_user(MOVW(7), &frame->retcode[0]);
458 err |= __put_user(TRAP16, &frame->retcode[1]);
459 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
460 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
461 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
462 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
463 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
464 err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
465 regs->pr = (unsigned long) frame->retcode;
471 /* Set up registers for signal handler */
472 regs->regs[15] = (unsigned long) frame;
473 regs->regs[4] = signal; /* Arg for signal handler */
474 regs->regs[5] = (unsigned long) &frame->info;
475 regs->regs[6] = (unsigned long) &frame->uc;
476 regs->pc = (unsigned long) ka->sa.sa_handler;
481 printk("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
482 current->comm, current->pid, frame, regs->pc, regs->pr);
485 flush_cache_sigtramp(regs->pr);
486 if ((-regs->pr & (L1_CACHE_BYTES-1)) < sizeof(frame->retcode))
487 flush_cache_sigtramp(regs->pr + L1_CACHE_BYTES);
491 force_sigsegv(sig, current);
495 * OK, we're invoking a handler
499 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
500 sigset_t *oldset, struct pt_regs *regs)
502 /* Are we from a system call? */
503 if (regs->tra >= 0) {
504 /* If so, check system call restarting.. */
505 switch (regs->regs[0]) {
506 case -ERESTARTNOHAND:
507 regs->regs[0] = -EINTR;
511 if (!(ka->sa.sa_flags & SA_RESTART)) {
512 regs->regs[0] = -EINTR;
516 case -ERESTARTNOINTR:
521 #ifdef CONFIG_PREEMPT
524 local_irq_save(flags);
526 if (regs->regs[15] >= 0xc0000000) {
527 int offset = (int)regs->regs[15];
529 /* Reset stack pointer: clear critical region mark */
530 regs->regs[15] = regs->regs[1];
531 if (regs->pc < regs->regs[0])
532 /* Go to rewind point #1 */
533 regs->pc = regs->regs[0] + offset - 2;
535 #ifdef CONFIG_PREEMPT
536 local_irq_restore(flags);
540 /* Set up the stack frame */
541 if (ka->sa.sa_flags & SA_SIGINFO)
542 setup_rt_frame(sig, ka, info, oldset, regs);
544 setup_frame(sig, ka, oldset, regs);
546 if (ka->sa.sa_flags & SA_ONESHOT)
547 ka->sa.sa_handler = SIG_DFL;
549 spin_lock_irq(¤t->sighand->siglock);
550 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
551 if (!(ka->sa.sa_flags & SA_NODEFER))
552 sigaddset(¤t->blocked,sig);
554 spin_unlock_irq(¤t->sighand->siglock);
558 * Note that 'init' is a special process: it doesn't get signals it doesn't
559 * want to handle. Thus you cannot kill init even with a SIGKILL even by
562 * Note that we go through the signals twice: once to check the signals that
563 * the kernel can handle, and then we build all the user-level signal handling
564 * stack-frames in one go after that.
566 int do_signal(struct pt_regs *regs, sigset_t *oldset)
570 struct k_sigaction ka;
573 * We want the common case to go fast, which
574 * is why we may in certain cases get here from
575 * kernel mode. Just return without doing anything
578 if (!user_mode(regs))
585 oldset = ¤t->blocked;
587 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
589 /* Whee! Actually deliver the signal. */
590 handle_signal(signr, &ka, &info, oldset, regs);
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] == -ERESTART_RESTARTBLOCK) {