2 * arch/ppc/kernel/signal.c
5 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
7 * Derived from "arch/i386/kernel/signal.c"
8 * Copyright (C) 1991, 1992 Linus Torvalds
9 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
17 #include <linux/sched.h>
19 #include <linux/smp.h>
20 #include <linux/smp_lock.h>
21 #include <linux/kernel.h>
22 #include <linux/signal.h>
23 #include <linux/errno.h>
24 #include <linux/wait.h>
25 #include <linux/ptrace.h>
26 #include <linux/unistd.h>
27 #include <linux/stddef.h>
28 #include <linux/elf.h>
29 #include <linux/tty.h>
30 #include <linux/binfmts.h>
31 #include <asm/ucontext.h>
32 #include <asm/uaccess.h>
33 #include <asm/pgtable.h>
34 #include <asm/cacheflush.h>
38 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
40 extern void sigreturn_exit(struct pt_regs *);
42 #define GP_REGS_SIZE min(sizeof(elf_gregset_t), sizeof(struct pt_regs))
44 int do_signal(sigset_t *oldset, struct pt_regs *regs);
47 * Atomically swap in the new signal mask, and wait for a signal.
50 sys_sigsuspend(old_sigset_t mask, int p2, int p3, int p4, int p6, int p7,
56 spin_lock_irq(¤t->sighand->siglock);
57 saveset = current->blocked;
58 siginitset(¤t->blocked, mask);
60 spin_unlock_irq(¤t->sighand->siglock);
62 regs->result = -EINTR;
64 regs->ccr |= 0x10000000;
66 current->state = TASK_INTERRUPTIBLE;
68 if (do_signal(&saveset, regs))
74 sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize, int p3, int p4,
75 int p6, int p7, struct pt_regs *regs)
77 sigset_t saveset, newset;
79 /* XXX: Don't preclude handling different sized sigset_t's. */
80 if (sigsetsize != sizeof(sigset_t))
83 if (copy_from_user(&newset, unewset, sizeof(newset)))
85 sigdelsetmask(&newset, ~_BLOCKABLE);
87 spin_lock_irq(¤t->sighand->siglock);
88 saveset = current->blocked;
89 current->blocked = newset;
91 spin_unlock_irq(¤t->sighand->siglock);
93 regs->result = -EINTR;
95 regs->ccr |= 0x10000000;
97 current->state = TASK_INTERRUPTIBLE;
99 if (do_signal(&saveset, regs))
100 sigreturn_exit(regs);
106 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss, int r5,
107 int r6, int r7, int r8, struct pt_regs *regs)
109 return do_sigaltstack(uss, uoss, regs->gpr[1]);
113 sys_sigaction(int sig, const struct old_sigaction __user *act,
114 struct old_sigaction __user *oact)
116 struct k_sigaction new_ka, old_ka;
121 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
122 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
123 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
125 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
126 __get_user(mask, &act->sa_mask);
127 siginitset(&new_ka.sa.sa_mask, mask);
130 ret = do_sigaction(sig, (act? &new_ka: NULL), (oact? &old_ka: NULL));
133 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
134 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
135 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
137 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
138 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
145 * When we have signals to deliver, we set up on the
146 * user stack, going down from the original stack pointer:
148 * a sigcontext struct
149 * a gap of __SIGNAL_FRAMESIZE bytes
151 * Each of these things must be a multiple of 16 bytes in size.
155 struct mcontext mctx; /* all the register values */
156 /* Programs using the rs6000/xcoff abi can save up to 19 gp regs
157 and 18 fp regs below sp before decrementing it. */
161 /* We use the mc_pad field for the signal return trampoline. */
165 * When we have rt signals to deliver, we set up on the
166 * user stack, going down from the original stack pointer:
167 * one rt_sigframe struct (siginfo + ucontext + ABI gap)
168 * a gap of __SIGNAL_FRAMESIZE+16 bytes
169 * (the +16 is to get the siginfo and ucontext in the same
170 * positions as in older kernels).
172 * Each of these things must be a multiple of 16 bytes in size.
179 /* Programs using the rs6000/xcoff abi can save up to 19 gp regs
180 and 18 fp regs below sp before decrementing it. */
185 * Save the current user registers on the user stack.
186 * We only save the altivec/spe registers if the process has used
187 * altivec/spe instructions at some point.
190 save_user_regs(struct pt_regs *regs, struct mcontext *frame, int sigret)
192 /* save general and floating-point registers */
193 CHECK_FULL_REGS(regs);
195 if (regs->msr & MSR_FP)
197 #ifdef CONFIG_ALTIVEC
198 if (current->thread.used_vr && (regs->msr & MSR_VEC))
199 giveup_altivec(current);
200 #endif /* CONFIG_ALTIVEC */
202 if (current->thread.used_spe && (regs->msr & MSR_SPE))
204 #endif /* CONFIG_ALTIVEC */
207 if (__copy_to_user(&frame->mc_gregs, regs, GP_REGS_SIZE)
208 || __copy_to_user(&frame->mc_fregs, current->thread.fpr,
209 ELF_NFPREG * sizeof(double)))
212 current->thread.fpscr = 0; /* turn off all fp exceptions */
214 #ifdef CONFIG_ALTIVEC
215 /* save altivec registers */
216 if (current->thread.used_vr) {
217 if (__copy_to_user(&frame->mc_vregs, current->thread.vr,
218 ELF_NVRREG * sizeof(vector128)))
220 /* set MSR_VEC in the saved MSR value to indicate that
221 frame->mc_vregs contains valid data */
222 if (__put_user(regs->msr | MSR_VEC, &frame->mc_gregs[PT_MSR]))
225 /* else assert((regs->msr & MSR_VEC) == 0) */
227 /* We always copy to/from vrsave, it's 0 if we don't have or don't
228 * use altivec. Since VSCR only contains 32 bits saved in the least
229 * significant bits of a vector, we "cheat" and stuff VRSAVE in the
230 * most significant bits of that same vector. --BenH
232 if (__put_user(current->thread.vrsave, (u32 *)&frame->mc_vregs[32]))
234 #endif /* CONFIG_ALTIVEC */
237 /* save spe registers */
238 if (current->thread.used_spe) {
239 if (__copy_to_user(&frame->mc_vregs, current->thread.evr,
240 ELF_NEVRREG * sizeof(u32)))
242 /* set MSR_SPE in the saved MSR value to indicate that
243 frame->mc_vregs contains valid data */
244 if (__put_user(regs->msr | MSR_SPE, &frame->mc_gregs[PT_MSR]))
247 /* else assert((regs->msr & MSR_SPE) == 0) */
249 /* We always copy to/from spefscr */
250 if (__put_user(current->thread.spefscr, (u32 *)&frame->mc_vregs + ELF_NEVRREG))
252 #endif /* CONFIG_SPE */
255 /* Set up the sigreturn trampoline: li r0,sigret; sc */
256 if (__put_user(0x38000000UL + sigret, &frame->tramp[0])
257 || __put_user(0x44000002UL, &frame->tramp[1]))
259 flush_icache_range((unsigned long) &frame->tramp[0],
260 (unsigned long) &frame->tramp[2]);
267 * Restore the current user register values from the user stack,
271 restore_user_regs(struct pt_regs *regs, struct mcontext __user *sr, int sig)
273 unsigned long save_r2;
274 #if defined(CONFIG_ALTIVEC) || defined(CONFIG_SPE)
278 /* backup/restore the TLS as we don't want it to be modified */
280 save_r2 = regs->gpr[2];
281 /* copy up to but not including MSR */
282 if (__copy_from_user(regs, &sr->mc_gregs, PT_MSR * sizeof(elf_greg_t)))
284 /* copy from orig_r3 (the word after the MSR) up to the end */
285 if (__copy_from_user(®s->orig_gpr3, &sr->mc_gregs[PT_ORIG_R3],
286 GP_REGS_SIZE - PT_ORIG_R3 * sizeof(elf_greg_t)))
289 regs->gpr[2] = save_r2;
291 /* force the process to reload the FP registers from
292 current->thread when it next does FP instructions */
293 regs->msr &= ~MSR_FP;
294 if (__copy_from_user(current->thread.fpr, &sr->mc_fregs,
295 sizeof(sr->mc_fregs)))
298 #ifdef CONFIG_ALTIVEC
299 /* force the process to reload the altivec registers from
300 current->thread when it next does altivec instructions */
301 regs->msr &= ~MSR_VEC;
302 if (!__get_user(msr, &sr->mc_gregs[PT_MSR]) && (msr & MSR_VEC) != 0) {
303 /* restore altivec registers from the stack */
304 if (__copy_from_user(current->thread.vr, &sr->mc_vregs,
305 sizeof(sr->mc_vregs)))
307 } else if (current->thread.used_vr)
308 memset(¤t->thread.vr, 0, ELF_NVRREG * sizeof(vector128));
310 /* Always get VRSAVE back */
311 if (__get_user(current->thread.vrsave, (u32 *)&sr->mc_vregs[32]))
313 #endif /* CONFIG_ALTIVEC */
316 /* force the process to reload the spe registers from
317 current->thread when it next does spe instructions */
318 regs->msr &= ~MSR_SPE;
319 if (!__get_user(msr, &sr->mc_gregs[PT_MSR]) && (msr & MSR_SPE) != 0) {
320 /* restore spe registers from the stack */
321 if (__copy_from_user(current->thread.evr, &sr->mc_vregs,
322 sizeof(sr->mc_vregs)))
324 } else if (current->thread.used_spe)
325 memset(¤t->thread.evr, 0, ELF_NEVRREG * sizeof(u32));
327 /* Always get SPEFSCR back */
328 if (__get_user(current->thread.spefscr, (u32 *)&sr->mc_vregs + ELF_NEVRREG))
330 #endif /* CONFIG_SPE */
336 * Restore the user process's signal mask
339 restore_sigmask(sigset_t *set)
341 sigdelsetmask(set, ~_BLOCKABLE);
342 spin_lock_irq(¤t->sighand->siglock);
343 current->blocked = *set;
345 spin_unlock_irq(¤t->sighand->siglock);
349 * Set up a signal frame for a "real-time" signal handler
350 * (one which gets siginfo).
353 handle_rt_signal(unsigned long sig, struct k_sigaction *ka,
354 siginfo_t *info, sigset_t *oldset, struct pt_regs * regs,
357 struct rt_sigframe __user *rt_sf;
358 struct mcontext __user *frame;
359 unsigned long origsp = newsp;
361 /* Set up Signal Frame */
362 /* Put a Real Time Context onto stack */
363 newsp -= sizeof(*rt_sf);
364 rt_sf = (struct rt_sigframe __user *) newsp;
366 /* create a stack frame for the caller of the handler */
367 newsp -= __SIGNAL_FRAMESIZE + 16;
369 if (verify_area(VERIFY_WRITE, (void __user *) newsp, origsp - newsp))
372 /* Put the siginfo & fill in most of the ucontext */
373 if (copy_siginfo_to_user(&rt_sf->info, info)
374 || __put_user(0, &rt_sf->uc.uc_flags)
375 || __put_user(0, &rt_sf->uc.uc_link)
376 || __put_user(current->sas_ss_sp, &rt_sf->uc.uc_stack.ss_sp)
377 || __put_user(sas_ss_flags(regs->gpr[1]),
378 &rt_sf->uc.uc_stack.ss_flags)
379 || __put_user(current->sas_ss_size, &rt_sf->uc.uc_stack.ss_size)
380 || __put_user(&rt_sf->uc.uc_mcontext, &rt_sf->uc.uc_regs)
381 || __copy_to_user(&rt_sf->uc.uc_sigmask, oldset, sizeof(*oldset)))
384 /* Save user registers on the stack */
385 frame = &rt_sf->uc.uc_mcontext;
386 if (save_user_regs(regs, frame, __NR_rt_sigreturn))
389 if (put_user(regs->gpr[1], (unsigned long __user *)newsp))
391 regs->gpr[1] = newsp;
393 regs->gpr[4] = (unsigned long) &rt_sf->info;
394 regs->gpr[5] = (unsigned long) &rt_sf->uc;
395 regs->gpr[6] = (unsigned long) rt_sf;
396 regs->nip = (unsigned long) ka->sa.sa_handler;
397 regs->link = (unsigned long) frame->tramp;
404 printk("badframe in handle_rt_signal, regs=%p frame=%p newsp=%lx\n",
408 ka->sa.sa_handler = SIG_DFL;
409 force_sig(SIGSEGV, current);
412 static int do_setcontext(struct ucontext __user *ucp, struct pt_regs *regs, int sig)
415 struct mcontext *mcp;
417 if (__copy_from_user(&set, &ucp->uc_sigmask, sizeof(set))
418 || __get_user(mcp, &ucp->uc_regs))
420 restore_sigmask(&set);
421 if (restore_user_regs(regs, mcp, sig))
427 int sys_swapcontext(struct ucontext __user *old_ctx,
428 struct ucontext __user *new_ctx,
429 int ctx_size, int r6, int r7, int r8, struct pt_regs *regs)
433 /* Context size is for future use. Right now, we only make sure
434 * we are passed something we understand
436 if (ctx_size < sizeof(struct ucontext))
439 if (old_ctx != NULL) {
440 if (verify_area(VERIFY_WRITE, old_ctx, sizeof(*old_ctx))
441 || save_user_regs(regs, &old_ctx->uc_mcontext, 0)
442 || __copy_to_user(&old_ctx->uc_sigmask,
443 ¤t->blocked, sizeof(sigset_t))
444 || __put_user(&old_ctx->uc_mcontext, &old_ctx->uc_regs))
449 if (verify_area(VERIFY_READ, new_ctx, sizeof(*new_ctx))
450 || __get_user(tmp, (u8 *) new_ctx)
451 || __get_user(tmp, (u8 *) (new_ctx + 1) - 1))
455 * If we get a fault copying the context into the kernel's
456 * image of the user's registers, we can't just return -EFAULT
457 * because the user's registers will be corrupted. For instance
458 * the NIP value may have been updated but not some of the
459 * other registers. Given that we have done the verify_area
460 * and successfully read the first and last bytes of the region
461 * above, this should only happen in an out-of-memory situation
462 * or if another thread unmaps the region containing the context.
463 * We kill the task with a SIGSEGV in this situation.
465 if (do_setcontext(new_ctx, regs, 0))
467 sigreturn_exit(regs);
468 /* doesn't actually return back to here */
472 int sys_rt_sigreturn(int r3, int r4, int r5, int r6, int r7, int r8,
473 struct pt_regs *regs)
475 struct rt_sigframe __user *rt_sf;
477 /* Always make any pending restarted system calls return -EINTR */
478 current_thread_info()->restart_block.fn = do_no_restart_syscall;
480 rt_sf = (struct rt_sigframe __user *)
481 (regs->gpr[1] + __SIGNAL_FRAMESIZE + 16);
482 if (verify_area(VERIFY_READ, rt_sf, sizeof(struct rt_sigframe)))
484 if (do_setcontext(&rt_sf->uc, regs, 1))
488 * It's not clear whether or why it is desirable to save the
489 * sigaltstack setting on signal delivery and restore it on
490 * signal return. But other architectures do this and we have
491 * always done it up until now so it is probably better not to
492 * change it. -- paulus
494 do_sigaltstack(&rt_sf->uc.uc_stack, NULL, regs->gpr[1]);
496 sigreturn_exit(regs); /* doesn't return here */
500 force_sig(SIGSEGV, current);
505 * OK, we're invoking a handler
508 handle_signal(unsigned long sig, struct k_sigaction *ka,
509 siginfo_t *info, sigset_t *oldset, struct pt_regs * regs,
512 struct sigcontext __user *sc;
513 struct sigregs __user *frame;
514 unsigned long origsp = newsp;
516 /* Set up Signal Frame */
517 newsp -= sizeof(struct sigregs);
518 frame = (struct sigregs __user *) newsp;
520 /* Put a sigcontext on the stack */
521 newsp -= sizeof(*sc);
522 sc = (struct sigcontext __user *) newsp;
524 /* create a stack frame for the caller of the handler */
525 newsp -= __SIGNAL_FRAMESIZE;
527 if (verify_area(VERIFY_WRITE, (void *) newsp, origsp - newsp))
531 #error "Please adjust handle_signal()"
533 if (__put_user((unsigned long) ka->sa.sa_handler, &sc->handler)
534 || __put_user(oldset->sig[0], &sc->oldmask)
535 || __put_user(oldset->sig[1], &sc->_unused[3])
536 || __put_user((struct pt_regs *)frame, &sc->regs)
537 || __put_user(sig, &sc->signal))
540 if (save_user_regs(regs, &frame->mctx, __NR_sigreturn))
543 if (put_user(regs->gpr[1], (unsigned long __user *)newsp))
545 regs->gpr[1] = newsp;
547 regs->gpr[4] = (unsigned long) sc;
548 regs->nip = (unsigned long) ka->sa.sa_handler;
549 regs->link = (unsigned long) frame->mctx.tramp;
556 printk("badframe in handle_signal, regs=%p frame=%p newsp=%lx\n",
560 ka->sa.sa_handler = SIG_DFL;
561 force_sig(SIGSEGV, current);
565 * Do a signal return; undo the signal stack.
567 int sys_sigreturn(int r3, int r4, int r5, int r6, int r7, int r8,
568 struct pt_regs *regs)
570 struct sigcontext __user *sc;
571 struct sigcontext sigctx;
572 struct mcontext __user *sr;
575 /* Always make any pending restarted system calls return -EINTR */
576 current_thread_info()->restart_block.fn = do_no_restart_syscall;
578 sc = (struct sigcontext __user *)(regs->gpr[1] + __SIGNAL_FRAMESIZE);
579 if (copy_from_user(&sigctx, sc, sizeof(sigctx)))
582 set.sig[0] = sigctx.oldmask;
583 set.sig[1] = sigctx._unused[3];
584 restore_sigmask(&set);
586 sr = (struct mcontext *) sigctx.regs;
587 if (verify_area(VERIFY_READ, sr, sizeof(*sr))
588 || restore_user_regs(regs, sr, 1))
591 sigreturn_exit(regs); /* doesn't return */
595 force_sig(SIGSEGV, current);
600 * Note that 'init' is a special process: it doesn't get signals it doesn't
601 * want to handle. Thus you cannot kill init even with a SIGKILL even by
604 int do_signal(sigset_t *oldset, struct pt_regs *regs)
607 struct k_sigaction *ka;
608 unsigned long frame, newsp;
612 oldset = ¤t->blocked;
616 signr = get_signal_to_deliver(&info, regs, NULL);
618 ka = (signr == 0)? NULL: ¤t->sighand->action[signr-1];
620 if (TRAP(regs) == 0x0C00 /* System Call! */
621 && regs->ccr & 0x10000000 /* error signalled */
622 && ((ret = regs->gpr[3]) == ERESTARTSYS
623 || ret == ERESTARTNOHAND || ret == ERESTARTNOINTR
624 || ret == ERESTART_RESTARTBLOCK)) {
627 && (ret == ERESTARTNOHAND || ret == ERESTART_RESTARTBLOCK
628 || (ret == ERESTARTSYS
629 && !(ka->sa.sa_flags & SA_RESTART)))) {
630 /* make the system call return an EINTR error */
631 regs->result = -EINTR;
632 regs->gpr[3] = EINTR;
633 /* note that the cr0.SO bit is already set */
635 regs->nip -= 4; /* Back up & retry system call */
638 if (ret == ERESTART_RESTARTBLOCK)
639 regs->gpr[0] = __NR_restart_syscall;
641 regs->gpr[3] = regs->orig_gpr3;
646 return 0; /* no signals delivered */
648 if ((ka->sa.sa_flags & SA_ONSTACK) && current->sas_ss_size
649 && !on_sig_stack(regs->gpr[1]))
650 newsp = current->sas_ss_sp + current->sas_ss_size;
652 newsp = regs->gpr[1];
655 /* Whee! Actually deliver the signal. */
656 if (ka->sa.sa_flags & SA_SIGINFO)
657 handle_rt_signal(signr, ka, &info, oldset, regs, newsp);
659 handle_signal(signr, ka, &info, oldset, regs, newsp);
661 if (ka->sa.sa_flags & SA_ONESHOT)
662 ka->sa.sa_handler = SIG_DFL;
664 if (!(ka->sa.sa_flags & SA_NODEFER)) {
665 spin_lock_irq(¤t->sighand->siglock);
666 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
667 sigaddset(¤t->blocked, signr);
669 spin_unlock_irq(¤t->sighand->siglock);