2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * arch/sh64/kernel/signal.c
8 * Copyright (C) 2000, 2001 Paolo Alberelli
9 * Copyright (C) 2003 Paul Mundt
10 * Copyright (C) 2004 Richard Curnow
12 * Started from sh version.
15 #include <linux/rwsem.h>
16 #include <linux/sched.h>
18 #include <linux/smp.h>
19 #include <linux/smp_lock.h>
20 #include <linux/kernel.h>
21 #include <linux/signal.h>
22 #include <linux/errno.h>
23 #include <linux/wait.h>
24 #include <linux/personality.h>
25 #include <linux/suspend.h>
26 #include <linux/ptrace.h>
27 #include <linux/unistd.h>
28 #include <linux/stddef.h>
29 #include <linux/personality.h>
30 #include <asm/ucontext.h>
31 #include <asm/uaccess.h>
32 #include <asm/pgtable.h>
41 #define REF_REG_RET regs->regs[REG_RET]
42 #define REF_REG_SP regs->regs[REG_SP]
43 #define DEREF_REG_PR regs->regs[REG_PR]
47 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
49 asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset);
52 * Atomically swap in the new signal mask, and wait for a signal.
56 sys_sigsuspend(old_sigset_t mask,
57 unsigned long r3, unsigned long r4, unsigned long r5,
58 unsigned long r6, unsigned long r7,
59 struct pt_regs * regs)
64 spin_lock_irq(¤t->sighand->siglock);
65 saveset = current->blocked;
66 siginitset(¤t->blocked, mask);
68 spin_unlock_irq(¤t->sighand->siglock);
72 current->state = TASK_INTERRUPTIBLE;
74 regs->pc += 4; /* because sys_sigreturn decrements the pc */
75 if (do_signal(regs, &saveset)) {
76 /* pc now points at signal handler. Need to decrement
77 it because entry.S will increment it. */
85 sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize,
86 unsigned long r4, unsigned long r5, unsigned long r6,
88 struct pt_regs * regs)
90 sigset_t saveset, newset;
92 /* XXX: Don't preclude handling different sized sigset_t's. */
93 if (sigsetsize != sizeof(sigset_t))
96 if (copy_from_user(&newset, unewset, sizeof(newset)))
98 sigdelsetmask(&newset, ~_BLOCKABLE);
99 spin_lock_irq(¤t->sighand->siglock);
100 saveset = current->blocked;
101 current->blocked = newset;
103 spin_unlock_irq(¤t->sighand->siglock);
105 REF_REG_RET = -EINTR;
107 current->state = TASK_INTERRUPTIBLE;
109 regs->pc += 4; /* because sys_sigreturn decrements the pc */
110 if (do_signal(regs, &saveset)) {
111 /* pc now points at signal handler. Need to decrement
112 it because entry.S will increment it. */
120 sys_sigaction(int sig, const struct old_sigaction __user *act,
121 struct old_sigaction __user *oact)
123 struct k_sigaction new_ka, old_ka;
128 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
129 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
130 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
132 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
133 __get_user(mask, &act->sa_mask);
134 siginitset(&new_ka.sa.sa_mask, mask);
137 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
140 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
141 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
142 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
144 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
145 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
152 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
153 unsigned long r4, unsigned long r5, unsigned long r6,
155 struct pt_regs * regs)
157 return do_sigaltstack(uss, uoss, REF_REG_SP);
162 * Do a signal return; undo the signal stack.
167 struct sigcontext sc;
168 unsigned long extramask[_NSIG_WORDS-1];
169 long long retcode[2];
174 struct siginfo __user *pinfo;
178 long long retcode[2];
181 #ifndef CONFIG_NOFPU_SUPPORT
183 restore_sigcontext_fpu(struct pt_regs *regs, struct sigcontext __user *sc)
188 err |= __get_user (fpvalid, &sc->sc_fpvalid);
189 current->used_math = fpvalid;
193 if (current == last_task_used_math) {
194 last_task_used_math = NULL;
198 err |= __copy_from_user(¤t->thread.fpu.hard, &sc->sc_fpregs[0],
199 (sizeof(long long) * 32) + (sizeof(int) * 1));
205 setup_sigcontext_fpu(struct pt_regs *regs, struct sigcontext __user *sc)
210 fpvalid = current->used_math;
211 err |= __put_user(fpvalid, &sc->sc_fpvalid);
215 if (current == last_task_used_math) {
217 fpsave(¤t->thread.fpu.hard);
219 last_task_used_math = NULL;
223 err |= __copy_to_user(&sc->sc_fpregs[0], ¤t->thread.fpu.hard,
224 (sizeof(long long) * 32) + (sizeof(int) * 1));
225 current->used_math = 0;
231 restore_sigcontext_fpu(struct pt_regs *regs, struct sigcontext __user *sc)
234 setup_sigcontext_fpu(struct pt_regs *regs, struct sigcontext __user *sc)
239 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, long long *r2_p)
241 unsigned int err = 0;
242 unsigned long long current_sr, new_sr;
243 #define SR_MASK 0xffff8cfd
245 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
247 COPY(regs[0]); COPY(regs[1]); COPY(regs[2]); COPY(regs[3]);
248 COPY(regs[4]); COPY(regs[5]); COPY(regs[6]); COPY(regs[7]);
249 COPY(regs[8]); COPY(regs[9]); COPY(regs[10]); COPY(regs[11]);
250 COPY(regs[12]); COPY(regs[13]); COPY(regs[14]); COPY(regs[15]);
251 COPY(regs[16]); COPY(regs[17]); COPY(regs[18]); COPY(regs[19]);
252 COPY(regs[20]); COPY(regs[21]); COPY(regs[22]); COPY(regs[23]);
253 COPY(regs[24]); COPY(regs[25]); COPY(regs[26]); COPY(regs[27]);
254 COPY(regs[28]); COPY(regs[29]); COPY(regs[30]); COPY(regs[31]);
255 COPY(regs[32]); COPY(regs[33]); COPY(regs[34]); COPY(regs[35]);
256 COPY(regs[36]); COPY(regs[37]); COPY(regs[38]); COPY(regs[39]);
257 COPY(regs[40]); COPY(regs[41]); COPY(regs[42]); COPY(regs[43]);
258 COPY(regs[44]); COPY(regs[45]); COPY(regs[46]); COPY(regs[47]);
259 COPY(regs[48]); COPY(regs[49]); COPY(regs[50]); COPY(regs[51]);
260 COPY(regs[52]); COPY(regs[53]); COPY(regs[54]); COPY(regs[55]);
261 COPY(regs[56]); COPY(regs[57]); COPY(regs[58]); COPY(regs[59]);
262 COPY(regs[60]); COPY(regs[61]); COPY(regs[62]);
263 COPY(tregs[0]); COPY(tregs[1]); COPY(tregs[2]); COPY(tregs[3]);
264 COPY(tregs[4]); COPY(tregs[5]); COPY(tregs[6]); COPY(tregs[7]);
266 /* Prevent the signal handler manipulating SR in a way that can
267 crash the kernel. i.e. only allow S, Q, M, PR, SZ, FR to be
269 current_sr = regs->sr;
270 err |= __get_user(new_sr, &sc->sc_sr);
272 regs->sr |= (new_sr & ~SR_MASK);
278 /* Must do this last in case it sets regs->sr.fd (i.e. after rest of sr
279 * has been restored above.) */
280 err |= restore_sigcontext_fpu(regs, sc);
282 regs->syscall_nr = -1; /* disable syscall checks */
283 err |= __get_user(*r2_p, &sc->sc_regs[REG_RET]);
287 asmlinkage int sys_sigreturn(unsigned long r2, unsigned long r3,
288 unsigned long r4, unsigned long r5,
289 unsigned long r6, unsigned long r7,
290 struct pt_regs * regs)
292 struct sigframe __user *frame = (struct sigframe __user *) (long) REF_REG_SP;
296 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
299 if (__get_user(set.sig[0], &frame->sc.oldmask)
301 && __copy_from_user(&set.sig[1], &frame->extramask,
302 sizeof(frame->extramask))))
305 sigdelsetmask(&set, ~_BLOCKABLE);
307 spin_lock_irq(¤t->sighand->siglock);
308 current->blocked = set;
310 spin_unlock_irq(¤t->sighand->siglock);
312 if (restore_sigcontext(regs, &frame->sc, &ret))
319 force_sig(SIGSEGV, current);
323 asmlinkage int sys_rt_sigreturn(unsigned long r2, unsigned long r3,
324 unsigned long r4, unsigned long r5,
325 unsigned long r6, unsigned long r7,
326 struct pt_regs * regs)
328 struct rt_sigframe __user *frame = (struct rt_sigframe __user *) (long) REF_REG_SP;
333 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
336 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
339 sigdelsetmask(&set, ~_BLOCKABLE);
340 spin_lock_irq(¤t->sighand->siglock);
341 current->blocked = set;
343 spin_unlock_irq(¤t->sighand->siglock);
345 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ret))
349 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
351 /* It is more difficult to avoid calling this function than to
352 call it and ignore errors. */
353 do_sigaltstack(&st, NULL, REF_REG_SP);
358 force_sig(SIGSEGV, current);
363 * Set up a signal frame.
367 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
372 /* Do this first, otherwise is this sets sr->fd, that value isn't preserved. */
373 err |= setup_sigcontext_fpu(regs, sc);
375 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
377 COPY(regs[0]); COPY(regs[1]); COPY(regs[2]); COPY(regs[3]);
378 COPY(regs[4]); COPY(regs[5]); COPY(regs[6]); COPY(regs[7]);
379 COPY(regs[8]); COPY(regs[9]); COPY(regs[10]); COPY(regs[11]);
380 COPY(regs[12]); COPY(regs[13]); COPY(regs[14]); COPY(regs[15]);
381 COPY(regs[16]); COPY(regs[17]); COPY(regs[18]); COPY(regs[19]);
382 COPY(regs[20]); COPY(regs[21]); COPY(regs[22]); COPY(regs[23]);
383 COPY(regs[24]); COPY(regs[25]); COPY(regs[26]); COPY(regs[27]);
384 COPY(regs[28]); COPY(regs[29]); COPY(regs[30]); COPY(regs[31]);
385 COPY(regs[32]); COPY(regs[33]); COPY(regs[34]); COPY(regs[35]);
386 COPY(regs[36]); COPY(regs[37]); COPY(regs[38]); COPY(regs[39]);
387 COPY(regs[40]); COPY(regs[41]); COPY(regs[42]); COPY(regs[43]);
388 COPY(regs[44]); COPY(regs[45]); COPY(regs[46]); COPY(regs[47]);
389 COPY(regs[48]); COPY(regs[49]); COPY(regs[50]); COPY(regs[51]);
390 COPY(regs[52]); COPY(regs[53]); COPY(regs[54]); COPY(regs[55]);
391 COPY(regs[56]); COPY(regs[57]); COPY(regs[58]); COPY(regs[59]);
392 COPY(regs[60]); COPY(regs[61]); COPY(regs[62]);
393 COPY(tregs[0]); COPY(tregs[1]); COPY(tregs[2]); COPY(tregs[3]);
394 COPY(tregs[4]); COPY(tregs[5]); COPY(tregs[6]); COPY(tregs[7]);
399 err |= __put_user(mask, &sc->oldmask);
405 * Determine which stack to use..
407 static inline void __user *
408 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
410 if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && ! on_sig_stack(sp))
411 sp = current->sas_ss_sp + current->sas_ss_size;
413 return (void __user *)((sp - frame_size) & -8ul);
416 void sa_default_restorer(void); /* See comments below */
417 void sa_default_rt_restorer(void); /* See comments below */
419 static void setup_frame(int sig, struct k_sigaction *ka,
420 sigset_t *set, struct pt_regs *regs)
422 struct sigframe __user *frame;
426 frame = get_sigframe(ka, regs->regs[REG_SP], 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 |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
439 /* Give up earlier as i386, in case */
443 if (_NSIG_WORDS > 1) {
444 err |= __copy_to_user(frame->extramask, &set->sig[1],
445 sizeof(frame->extramask)); }
447 /* Give up earlier as i386, in case */
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 DEREF_REG_PR = (unsigned long) ka->sa.sa_restorer | 0x1;
457 * On SH5 all edited pointers are subject to NEFF
459 DEREF_REG_PR = (DEREF_REG_PR & NEFF_SIGN) ?
460 (DEREF_REG_PR | NEFF_MASK) : DEREF_REG_PR;
463 * Different approach on SH5.
464 * . Endianness independent asm code gets placed in entry.S .
465 * This is limited to four ASM instructions corresponding
466 * to two long longs in size.
467 * . err checking is done on the else branch only
468 * . flush_icache_range() is called upon __put_user() only
469 * . all edited pointers are subject to NEFF
470 * . being code, linker turns ShMedia bit on, always
471 * dereference index -1.
473 DEREF_REG_PR = (unsigned long) frame->retcode | 0x01;
474 DEREF_REG_PR = (DEREF_REG_PR & NEFF_SIGN) ?
475 (DEREF_REG_PR | NEFF_MASK) : DEREF_REG_PR;
477 if (__copy_to_user(frame->retcode,
478 (unsigned long long)sa_default_restorer & (~1), 16) != 0)
481 /* Cohere the trampoline with the I-cache. */
482 flush_cache_sigtramp(DEREF_REG_PR-1, DEREF_REG_PR-1+16);
486 * Set up registers for signal handler.
487 * All edited pointers are subject to NEFF.
489 regs->regs[REG_SP] = (unsigned long) frame;
490 regs->regs[REG_SP] = (regs->regs[REG_SP] & NEFF_SIGN) ?
491 (regs->regs[REG_SP] | NEFF_MASK) : regs->regs[REG_SP];
492 regs->regs[REG_ARG1] = signal; /* Arg for signal handler */
495 The glibc profiling support for SH-5 needs to be passed a sigcontext
496 so it can retrieve the PC. At some point during 2003 the glibc
497 support was changed to receive the sigcontext through the 2nd
498 argument, but there are still versions of libc.so in use that use
499 the 3rd argument. Until libc.so is stabilised, pass the sigcontext
500 through both 2nd and 3rd arguments.
503 regs->regs[REG_ARG2] = (unsigned long long)(unsigned long)(signed long)&frame->sc;
504 regs->regs[REG_ARG3] = (unsigned long long)(unsigned long)(signed long)&frame->sc;
506 regs->pc = (unsigned long) ka->sa.sa_handler;
507 regs->pc = (regs->pc & NEFF_SIGN) ? (regs->pc | NEFF_MASK) : regs->pc;
513 printk("SIG deliver (#%d,%s:%d): sp=%p pc=%08Lx%08Lx link=%08Lx%08Lx\n",
515 current->comm, current->pid, frame,
516 regs->pc >> 32, regs->pc & 0xffffffff,
517 DEREF_REG_PR >> 32, DEREF_REG_PR & 0xffffffff);
524 ka->sa.sa_handler = SIG_DFL;
525 force_sig(SIGSEGV, current);
528 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
529 sigset_t *set, struct pt_regs *regs)
531 struct rt_sigframe __user *frame;
535 frame = get_sigframe(ka, regs->regs[REG_SP], sizeof(*frame));
537 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
540 signal = current_thread_info()->exec_domain
541 && current_thread_info()->exec_domain->signal_invmap
543 ? current_thread_info()->exec_domain->signal_invmap[sig]
546 err |= __put_user(&frame->info, &frame->pinfo);
547 err |= __put_user(&frame->uc, &frame->puc);
548 err |= copy_siginfo_to_user(&frame->info, info);
550 /* Give up earlier as i386, in case */
554 /* Create the ucontext. */
555 err |= __put_user(0, &frame->uc.uc_flags);
556 err |= __put_user(0, &frame->uc.uc_link);
557 err |= __put_user((void *)current->sas_ss_sp,
558 &frame->uc.uc_stack.ss_sp);
559 err |= __put_user(sas_ss_flags(regs->regs[REG_SP]),
560 &frame->uc.uc_stack.ss_flags);
561 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
562 err |= setup_sigcontext(&frame->uc.uc_mcontext,
564 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
566 /* Give up earlier as i386, in case */
570 /* Set up to return from userspace. If provided, use a stub
571 already in userspace. */
572 if (ka->sa.sa_flags & SA_RESTORER) {
573 DEREF_REG_PR = (unsigned long) ka->sa.sa_restorer | 0x1;
576 * On SH5 all edited pointers are subject to NEFF
578 DEREF_REG_PR = (DEREF_REG_PR & NEFF_SIGN) ?
579 (DEREF_REG_PR | NEFF_MASK) : DEREF_REG_PR;
582 * Different approach on SH5.
583 * . Endianness independent asm code gets placed in entry.S .
584 * This is limited to four ASM instructions corresponding
585 * to two long longs in size.
586 * . err checking is done on the else branch only
587 * . flush_icache_range() is called upon __put_user() only
588 * . all edited pointers are subject to NEFF
589 * . being code, linker turns ShMedia bit on, always
590 * dereference index -1.
593 DEREF_REG_PR = (unsigned long) frame->retcode | 0x01;
594 DEREF_REG_PR = (DEREF_REG_PR & NEFF_SIGN) ?
595 (DEREF_REG_PR | NEFF_MASK) : DEREF_REG_PR;
597 if (__copy_to_user(frame->retcode,
598 (unsigned long long)sa_default_rt_restorer & (~1), 16) != 0)
601 flush_icache_range(DEREF_REG_PR-1, DEREF_REG_PR-1+15);
605 * Set up registers for signal handler.
606 * All edited pointers are subject to NEFF.
608 regs->regs[REG_SP] = (unsigned long) frame;
609 regs->regs[REG_SP] = (regs->regs[REG_SP] & NEFF_SIGN) ?
610 (regs->regs[REG_SP] | NEFF_MASK) : regs->regs[REG_SP];
611 regs->regs[REG_ARG1] = signal; /* Arg for signal handler */
612 regs->regs[REG_ARG2] = (unsigned long long)(unsigned long)(signed long)&frame->info;
613 regs->regs[REG_ARG3] = (unsigned long long)(unsigned long)(signed long)&frame->uc.uc_mcontext;
614 regs->pc = (unsigned long) ka->sa.sa_handler;
615 regs->pc = (regs->pc & NEFF_SIGN) ? (regs->pc | NEFF_MASK) : regs->pc;
621 printk("SIG deliver (#%d,%s:%d): sp=%p pc=%08Lx%08Lx link=%08Lx%08Lx\n",
623 current->comm, current->pid, frame,
624 regs->pc >> 32, regs->pc & 0xffffffff,
625 DEREF_REG_PR >> 32, DEREF_REG_PR & 0xffffffff);
632 ka->sa.sa_handler = SIG_DFL;
633 force_sig(SIGSEGV, current);
637 * OK, we're invoking a handler
641 handle_signal(unsigned long sig, siginfo_t *info, sigset_t *oldset,
642 struct pt_regs * regs)
644 struct k_sigaction *ka = ¤t->sighand->action[sig-1];
646 /* Are we from a system call? */
647 if (regs->syscall_nr >= 0) {
648 /* If so, check system call restarting.. */
649 switch (regs->regs[REG_RET]) {
650 case -ERESTARTNOHAND:
651 regs->regs[REG_RET] = -EINTR;
655 if (!(ka->sa.sa_flags & SA_RESTART)) {
656 regs->regs[REG_RET] = -EINTR;
660 case -ERESTARTNOINTR:
661 /* Decode syscall # */
662 regs->regs[REG_RET] = regs->syscall_nr;
667 /* Set up the stack frame */
668 if (ka->sa.sa_flags & SA_SIGINFO)
669 setup_rt_frame(sig, ka, info, oldset, regs);
671 setup_frame(sig, ka, oldset, regs);
673 if (ka->sa.sa_flags & SA_ONESHOT)
674 ka->sa.sa_handler = SIG_DFL;
676 if (!(ka->sa.sa_flags & SA_NODEFER)) {
677 spin_lock_irq(¤t->sighand->siglock);
678 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
679 sigaddset(¤t->blocked,sig);
681 spin_unlock_irq(¤t->sighand->siglock);
686 * Note that 'init' is a special process: it doesn't get signals it doesn't
687 * want to handle. Thus you cannot kill init even with a SIGKILL even by
690 * Note that we go through the signals twice: once to check the signals that
691 * the kernel can handle, and then we build all the user-level signal handling
692 * stack-frames in one go after that.
694 int do_signal(struct pt_regs *regs, sigset_t *oldset)
700 * We want the common case to go fast, which
701 * is why we may in certain cases get here from
702 * kernel mode. Just return without doing anything
705 if (!user_mode(regs))
708 if (current->flags & PF_FREEZE) {
714 oldset = ¤t->blocked;
716 signr = get_signal_to_deliver(&info, regs, 0);
719 /* Whee! Actually deliver the signal. */
720 handle_signal(signr, &info, oldset, regs);
725 /* Did we come from a system call? */
726 if (regs->syscall_nr >= 0) {
727 /* Restart the system call - no handlers present */
728 if (regs->regs[REG_RET] == -ERESTARTNOHAND ||
729 regs->regs[REG_RET] == -ERESTARTSYS ||
730 regs->regs[REG_RET] == -ERESTARTNOINTR) {
731 /* Decode Syscall # */
732 regs->regs[REG_RET] = regs->syscall_nr;