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 registers if the process has used
187 * altivec 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 */
203 if (__copy_to_user(&frame->mc_gregs, regs, GP_REGS_SIZE)
204 || __copy_to_user(&frame->mc_fregs, current->thread.fpr,
205 ELF_NFPREG * sizeof(double)))
208 current->thread.fpscr = 0; /* turn off all fp exceptions */
210 #ifdef CONFIG_ALTIVEC
211 /* save altivec registers */
212 if (current->thread.used_vr) {
213 if (__copy_to_user(&frame->mc_vregs, current->thread.vr,
214 ELF_NVRREG * sizeof(vector128)))
216 /* set MSR_VEC in the saved MSR value to indicate that
217 frame->mc_vregs contains valid data */
218 if (__put_user(regs->msr | MSR_VEC, &frame->mc_gregs[PT_MSR]))
221 /* else assert((regs->msr & MSR_VEC) == 0) */
223 /* We always copy to/from vrsave, it's 0 if we don't have or don't
224 * use altivec. Since VSCR only contains 32 bits saved in the least
225 * significant bits of a vector, we "cheat" and stuff VRSAVE in the
226 * most significant bits of that same vector. --BenH
228 if (__put_user(current->thread.vrsave, (u32 *)&frame->mc_vregs[32]))
230 #endif /* CONFIG_ALTIVEC */
233 /* Set up the sigreturn trampoline: li r0,sigret; sc */
234 if (__put_user(0x38000000UL + sigret, &frame->tramp[0])
235 || __put_user(0x44000002UL, &frame->tramp[1]))
237 flush_icache_range((unsigned long) &frame->tramp[0],
238 (unsigned long) &frame->tramp[2]);
245 * Restore the current user register values from the user stack,
249 restore_user_regs(struct pt_regs *regs, struct mcontext __user *sr, int sig)
251 unsigned long save_r2;
252 #ifdef CONFIG_ALTIVEC
256 /* backup/restore the TLS as we don't want it to be modified */
258 save_r2 = regs->gpr[2];
259 /* copy up to but not including MSR */
260 if (__copy_from_user(regs, &sr->mc_gregs, PT_MSR * sizeof(elf_greg_t)))
262 /* copy from orig_r3 (the word after the MSR) up to the end */
263 if (__copy_from_user(®s->orig_gpr3, &sr->mc_gregs[PT_ORIG_R3],
264 GP_REGS_SIZE - PT_ORIG_R3 * sizeof(elf_greg_t)))
267 regs->gpr[2] = save_r2;
269 /* force the process to reload the FP registers from
270 current->thread when it next does FP instructions */
271 regs->msr &= ~MSR_FP;
272 if (__copy_from_user(current->thread.fpr, &sr->mc_fregs,
273 sizeof(sr->mc_fregs)))
276 #ifdef CONFIG_ALTIVEC
277 /* force the process to reload the altivec registers from
278 current->thread when it next does altivec instructions */
279 regs->msr &= ~MSR_VEC;
280 if (!__get_user(msr, &sr->mc_gregs[PT_MSR]) && (msr & MSR_VEC) != 0) {
281 /* restore altivec registers from the stack */
282 if (__copy_from_user(current->thread.vr, &sr->mc_vregs,
283 sizeof(sr->mc_vregs)))
285 } else if (current->thread.used_vr)
286 memset(¤t->thread.vr, 0, ELF_NVRREG * sizeof(vector128));
288 /* Always get VRSAVE back */
289 if (__get_user(current->thread.vrsave, (u32 *)&sr->mc_vregs[32]))
291 #endif /* CONFIG_ALTIVEC */
297 * Restore the user process's signal mask
300 restore_sigmask(sigset_t *set)
302 sigdelsetmask(set, ~_BLOCKABLE);
303 spin_lock_irq(¤t->sighand->siglock);
304 current->blocked = *set;
306 spin_unlock_irq(¤t->sighand->siglock);
310 * Set up a signal frame for a "real-time" signal handler
311 * (one which gets siginfo).
314 handle_rt_signal(unsigned long sig, struct k_sigaction *ka,
315 siginfo_t *info, sigset_t *oldset, struct pt_regs * regs,
318 struct rt_sigframe __user *rt_sf;
319 struct mcontext __user *frame;
320 unsigned long origsp = newsp;
322 /* Set up Signal Frame */
323 /* Put a Real Time Context onto stack */
324 newsp -= sizeof(*rt_sf);
325 rt_sf = (struct rt_sigframe __user *) newsp;
327 /* create a stack frame for the caller of the handler */
328 newsp -= __SIGNAL_FRAMESIZE + 16;
330 if (verify_area(VERIFY_WRITE, (void __user *) newsp, origsp - newsp))
333 /* Put the siginfo & fill in most of the ucontext */
334 if (copy_siginfo_to_user(&rt_sf->info, info)
335 || __put_user(0, &rt_sf->uc.uc_flags)
336 || __put_user(0, &rt_sf->uc.uc_link)
337 || __put_user(current->sas_ss_sp, &rt_sf->uc.uc_stack.ss_sp)
338 || __put_user(sas_ss_flags(regs->gpr[1]),
339 &rt_sf->uc.uc_stack.ss_flags)
340 || __put_user(current->sas_ss_size, &rt_sf->uc.uc_stack.ss_size)
341 || __put_user(&rt_sf->uc.uc_mcontext, &rt_sf->uc.uc_regs)
342 || __copy_to_user(&rt_sf->uc.uc_sigmask, oldset, sizeof(*oldset)))
345 /* Save user registers on the stack */
346 frame = &rt_sf->uc.uc_mcontext;
347 if (save_user_regs(regs, frame, __NR_rt_sigreturn))
350 if (put_user(regs->gpr[1], (unsigned long __user *)newsp))
352 regs->gpr[1] = newsp;
354 regs->gpr[4] = (unsigned long) &rt_sf->info;
355 regs->gpr[5] = (unsigned long) &rt_sf->uc;
356 regs->gpr[6] = (unsigned long) rt_sf;
357 regs->nip = (unsigned long) ka->sa.sa_handler;
358 regs->link = (unsigned long) frame->tramp;
365 printk("badframe in handle_rt_signal, regs=%p frame=%p newsp=%lx\n",
369 ka->sa.sa_handler = SIG_DFL;
370 force_sig(SIGSEGV, current);
373 static int do_setcontext(struct ucontext __user *ucp, struct pt_regs *regs, int sig)
376 struct mcontext *mcp;
378 if (__copy_from_user(&set, &ucp->uc_sigmask, sizeof(set))
379 || __get_user(mcp, &ucp->uc_regs))
381 restore_sigmask(&set);
382 if (restore_user_regs(regs, mcp, sig))
388 int sys_swapcontext(struct ucontext __user *old_ctx,
389 struct ucontext __user *new_ctx,
390 int ctx_size, int r6, int r7, int r8, struct pt_regs *regs)
394 /* Context size is for future use. Right now, we only make sure
395 * we are passed something we understand
397 if (ctx_size < sizeof(struct ucontext))
400 if (old_ctx != NULL) {
401 if (verify_area(VERIFY_WRITE, old_ctx, sizeof(*old_ctx))
402 || save_user_regs(regs, &old_ctx->uc_mcontext, 0)
403 || __copy_to_user(&old_ctx->uc_sigmask,
404 ¤t->blocked, sizeof(sigset_t))
405 || __put_user(&old_ctx->uc_mcontext, &old_ctx->uc_regs))
410 if (verify_area(VERIFY_READ, new_ctx, sizeof(*new_ctx))
411 || __get_user(tmp, (u8 *) new_ctx)
412 || __get_user(tmp, (u8 *) (new_ctx + 1) - 1))
416 * If we get a fault copying the context into the kernel's
417 * image of the user's registers, we can't just return -EFAULT
418 * because the user's registers will be corrupted. For instance
419 * the NIP value may have been updated but not some of the
420 * other registers. Given that we have done the verify_area
421 * and successfully read the first and last bytes of the region
422 * above, this should only happen in an out-of-memory situation
423 * or if another thread unmaps the region containing the context.
424 * We kill the task with a SIGSEGV in this situation.
426 if (do_setcontext(new_ctx, regs, 0))
428 sigreturn_exit(regs);
429 /* doesn't actually return back to here */
433 int sys_rt_sigreturn(int r3, int r4, int r5, int r6, int r7, int r8,
434 struct pt_regs *regs)
436 struct rt_sigframe __user *rt_sf;
438 /* Always make any pending restarted system calls return -EINTR */
439 current_thread_info()->restart_block.fn = do_no_restart_syscall;
441 rt_sf = (struct rt_sigframe __user *)
442 (regs->gpr[1] + __SIGNAL_FRAMESIZE + 16);
443 if (verify_area(VERIFY_READ, rt_sf, sizeof(struct rt_sigframe)))
445 if (do_setcontext(&rt_sf->uc, regs, 1))
449 * It's not clear whether or why it is desirable to save the
450 * sigaltstack setting on signal delivery and restore it on
451 * signal return. But other architectures do this and we have
452 * always done it up until now so it is probably better not to
453 * change it. -- paulus
455 do_sigaltstack(&rt_sf->uc.uc_stack, NULL, regs->gpr[1]);
457 sigreturn_exit(regs); /* doesn't return here */
461 force_sig(SIGSEGV, current);
466 * OK, we're invoking a handler
469 handle_signal(unsigned long sig, struct k_sigaction *ka,
470 siginfo_t *info, sigset_t *oldset, struct pt_regs * regs,
473 struct sigcontext __user *sc;
474 struct sigregs __user *frame;
475 unsigned long origsp = newsp;
477 /* Set up Signal Frame */
478 newsp -= sizeof(struct sigregs);
479 frame = (struct sigregs __user *) newsp;
481 /* Put a sigcontext on the stack */
482 newsp -= sizeof(*sc);
483 sc = (struct sigcontext __user *) newsp;
485 /* create a stack frame for the caller of the handler */
486 newsp -= __SIGNAL_FRAMESIZE;
488 if (verify_area(VERIFY_WRITE, (void *) newsp, origsp - newsp))
492 #error "Please adjust handle_signal()"
494 if (__put_user((unsigned long) ka->sa.sa_handler, &sc->handler)
495 || __put_user(oldset->sig[0], &sc->oldmask)
496 || __put_user(oldset->sig[1], &sc->_unused[3])
497 || __put_user((struct pt_regs *)frame, &sc->regs)
498 || __put_user(sig, &sc->signal))
501 if (save_user_regs(regs, &frame->mctx, __NR_sigreturn))
504 if (put_user(regs->gpr[1], (unsigned long __user *)newsp))
506 regs->gpr[1] = newsp;
508 regs->gpr[4] = (unsigned long) sc;
509 regs->nip = (unsigned long) ka->sa.sa_handler;
510 regs->link = (unsigned long) frame->mctx.tramp;
517 printk("badframe in handle_signal, regs=%p frame=%p newsp=%lx\n",
521 ka->sa.sa_handler = SIG_DFL;
522 force_sig(SIGSEGV, current);
526 * Do a signal return; undo the signal stack.
528 int sys_sigreturn(int r3, int r4, int r5, int r6, int r7, int r8,
529 struct pt_regs *regs)
531 struct sigcontext __user *sc;
532 struct sigcontext sigctx;
533 struct mcontext __user *sr;
536 /* Always make any pending restarted system calls return -EINTR */
537 current_thread_info()->restart_block.fn = do_no_restart_syscall;
539 sc = (struct sigcontext __user *)(regs->gpr[1] + __SIGNAL_FRAMESIZE);
540 if (copy_from_user(&sigctx, sc, sizeof(sigctx)))
543 set.sig[0] = sigctx.oldmask;
544 set.sig[1] = sigctx._unused[3];
545 restore_sigmask(&set);
547 sr = (struct mcontext *) sigctx.regs;
548 if (verify_area(VERIFY_READ, sr, sizeof(*sr))
549 || restore_user_regs(regs, sr, 1))
552 sigreturn_exit(regs); /* doesn't return */
556 force_sig(SIGSEGV, current);
561 * Note that 'init' is a special process: it doesn't get signals it doesn't
562 * want to handle. Thus you cannot kill init even with a SIGKILL even by
565 int do_signal(sigset_t *oldset, struct pt_regs *regs)
568 struct k_sigaction *ka;
569 unsigned long frame, newsp;
573 oldset = ¤t->blocked;
577 signr = get_signal_to_deliver(&info, regs, NULL);
579 ka = (signr == 0)? NULL: ¤t->sighand->action[signr-1];
581 if (TRAP(regs) == 0x0C00 /* System Call! */
582 && regs->ccr & 0x10000000 /* error signalled */
583 && ((ret = regs->gpr[3]) == ERESTARTSYS
584 || ret == ERESTARTNOHAND || ret == ERESTARTNOINTR
585 || ret == ERESTART_RESTARTBLOCK)) {
588 && (ret == ERESTARTNOHAND || ret == ERESTART_RESTARTBLOCK
589 || (ret == ERESTARTSYS
590 && !(ka->sa.sa_flags & SA_RESTART)))) {
591 /* make the system call return an EINTR error */
592 regs->result = -EINTR;
593 regs->gpr[3] = EINTR;
594 /* note that the cr0.SO bit is already set */
596 regs->nip -= 4; /* Back up & retry system call */
599 if (ret == ERESTART_RESTARTBLOCK)
600 regs->gpr[0] = __NR_restart_syscall;
602 regs->gpr[3] = regs->orig_gpr3;
607 return 0; /* no signals delivered */
609 if ((ka->sa.sa_flags & SA_ONSTACK) && current->sas_ss_size
610 && !on_sig_stack(regs->gpr[1]))
611 newsp = current->sas_ss_sp + current->sas_ss_size;
613 newsp = regs->gpr[1];
616 /* Whee! Actually deliver the signal. */
617 if (ka->sa.sa_flags & SA_SIGINFO)
618 handle_rt_signal(signr, ka, &info, oldset, regs, newsp);
620 handle_signal(signr, ka, &info, oldset, regs, newsp);
622 if (ka->sa.sa_flags & SA_ONESHOT)
623 ka->sa.sa_handler = SIG_DFL;
625 if (!(ka->sa.sa_flags & SA_NODEFER)) {
626 spin_lock_irq(¤t->sighand->siglock);
627 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
628 sigaddset(¤t->blocked, signr);
630 spin_unlock_irq(¤t->sighand->siglock);