- Alan Cox: synch. PA-RISC arch and bitops cleanups
[davej-history.git] / arch / parisc / kernel / signal.c
bloba910f43da54c2d77109393ca9f330f8147af7711
1 /*
2 * linux/arch/parisc/kernel/signal.c: Architecture-specific signal
3 * handling support.
5 * Copyright (C) 2000 David Huggins-Daines <dhd@debian.org>
6 * Copyright (C) 2000 Linuxcare, Inc.
8 * Based on the ia64, i386, and alpha versions.
10 * Like the IA-64, we are a recent enough port (we are *starting*
11 * with glibc2.2) that we do not need to support the old non-realtime
12 * Linux signals. Therefore we don't. HP/UX signals will go in
13 * arch/parisc/hpux/signal.c when we figure out how to do them.
16 #include <linux/config.h>
18 #include <linux/version.h>
19 #include <linux/sched.h>
20 #include <linux/mm.h>
21 #include <linux/smp.h>
22 #include <linux/smp_lock.h>
23 #include <linux/kernel.h>
24 #include <linux/signal.h>
25 #include <linux/errno.h>
26 #include <linux/wait.h>
27 #include <linux/ptrace.h>
28 #include <linux/unistd.h>
29 #include <linux/stddef.h>
30 #include <asm/ucontext.h>
31 #include <asm/uaccess.h>
32 #include <asm/pgalloc.h>
34 #define DEBUG_SIG 0
36 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
38 extern long sys_wait4 (int, int *, int, struct rusage *);
39 int do_signal(sigset_t *oldset, struct pt_regs *regs, int in_syscall);
41 int copy_siginfo_to_user(siginfo_t *to, siginfo_t *from)
43 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
44 return -EFAULT;
45 if (from->si_code < 0)
46 return __copy_to_user(to, from, sizeof(siginfo_t));
47 else {
48 int err;
51 * If you change siginfo_t structure, please be sure
52 * this code is fixed accordingly. It should never
53 * copy any pad contained in the structure to avoid
54 * security leaks, but must copy the generic 3 ints
55 * plus the relevant union member.
57 err = __put_user(from->si_signo, &to->si_signo);
58 err |= __put_user(from->si_errno, &to->si_errno);
59 err |= __put_user((short)from->si_code, &to->si_code);
60 switch (from->si_code >> 16) {
61 case __SI_FAULT >> 16:
62 /* FIXME: should we put the interruption code here? */
63 case __SI_POLL >> 16:
64 err |= __put_user(from->si_addr, &to->si_addr);
65 break;
66 case __SI_CHLD >> 16:
67 err |= __put_user(from->si_utime, &to->si_utime);
68 err |= __put_user(from->si_stime, &to->si_stime);
69 err |= __put_user(from->si_status, &to->si_status);
70 default:
71 err |= __put_user(from->si_uid, &to->si_uid);
72 err |= __put_user(from->si_pid, &to->si_pid);
73 break;
74 /* case __SI_RT: This is not generated by the kernel as of now. */
76 return err;
81 * Atomically swap in the new signal mask, and wait for a signal.
83 #ifdef __LP64__
84 #include "sys32.h"
85 #endif
87 asmlinkage int
88 sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize, struct pt_regs *regs)
90 sigset_t saveset, newset;
91 #ifdef __LP64__
92 /* XXX FIXME -- assumes 32-bit user app! */
93 sigset_t32 newset32;
95 /* XXX: Don't preclude handling different sized sigset_t's. */
96 if (sigsetsize != sizeof(sigset_t32))
97 return -EINVAL;
99 if (copy_from_user(&newset32, (sigset_t32 *)unewset, sizeof(newset32)))
100 return -EFAULT;
102 newset.sig[0] = newset32.sig[0] | ((unsigned long)newset32.sig[1] << 32);
103 #else
105 /* XXX: Don't preclude handling different sized sigset_t's. */
106 if (sigsetsize != sizeof(sigset_t))
107 return -EINVAL;
109 if (copy_from_user(&newset, unewset, sizeof(newset)))
110 return -EFAULT;
111 #endif
112 sigdelsetmask(&newset, ~_BLOCKABLE);
114 spin_lock_irq(&current->sigmask_lock);
115 saveset = current->blocked;
116 current->blocked = newset;
117 recalc_sigpending(current);
118 spin_unlock_irq(&current->sigmask_lock);
120 regs->gr[28] = -EINTR;
121 while (1) {
122 current->state = TASK_INTERRUPTIBLE;
123 schedule();
124 if (do_signal(&saveset, regs, 1))
125 return -EINTR;
130 * Do a signal return - restore sigcontext.
133 struct rt_sigframe {
134 unsigned int tramp[4];
135 struct siginfo info;
136 struct ucontext uc;
139 /* Trampoline for calling rt_sigreturn() */
140 #define INSN_LDI_R25_0 0x34190000 /* ldi 0,%r25 (in_syscall=0) */
141 #define INSN_LDI_R25_1 0x34190002 /* ldi 1,%r25 (in_syscall=1) */
142 #define INSN_LDI_R20 0x3414015a /* ldi __NR_rt_sigreturn,%r20 */
143 #define INSN_BLE_SR2_R0 0xe4008200 /* be,l 0x100(%sr2,%r0),%sr0,%r31 */
144 #define INSN_NOP 0x80000240 /* nop */
145 /* For debugging */
146 #define INSN_DIE_HORRIBLY 0x68000ccc /* stw %r0,0x666(%sr0,%r0) */
149 * The 32-bit ABI wants at least 48 bytes for a function call frame:
150 * 16 bytes for arg0-arg3, and 32 bytes for magic (the only part of
151 * which Linux/parisc uses is sp-20 for the saved return pointer...)
152 * Then, the stack pointer must be rounded to a cache line (64 bytes).
154 #define PARISC_RT_SIGFRAME_SIZE \
155 (((sizeof(struct rt_sigframe) + 48) + 63) & -64)
157 static long
158 restore_sigcontext(struct sigcontext *sc, struct pt_regs *regs)
160 long err = 0;
162 err |= __copy_from_user(regs->gr, sc->sc_gr, sizeof(regs->gr));
163 err |= __copy_from_user(regs->fr, sc->sc_fr, sizeof(regs->fr));
164 err |= __copy_from_user(regs->iaoq, sc->sc_iaoq, sizeof(regs->iaoq));
165 err |= __copy_from_user(regs->iasq, sc->sc_iasq, sizeof(regs->iasq));
166 err |= __get_user(regs->sar, &sc->sc_sar);
168 #if DEBUG_SIG
169 printk("restore_sigcontext: r28 is %ld\n", regs->gr[28]);
170 #endif
171 return err;
174 void
175 sys_rt_sigreturn(struct pt_regs *regs, int in_syscall)
177 struct rt_sigframe *frame;
178 struct siginfo si;
179 sigset_t set;
180 unsigned long usp = regs->gr[30];
182 /* Unwind the user stack to get the rt_sigframe structure. */
183 frame = (struct rt_sigframe *)
184 (usp - PARISC_RT_SIGFRAME_SIZE);
185 #if DEBUG_SIG
186 printk("in sys_rt_sigreturn, frame is %p\n", frame);
187 #endif
189 /* Verify that it's a good sigcontext before using it */
190 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
191 goto give_sigsegv;
192 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
193 goto give_sigsegv;
195 sigdelsetmask(&set, ~_BLOCKABLE);
196 spin_lock_irq(&current->sigmask_lock);
197 current->blocked = set;
198 recalc_sigpending(current);
199 spin_unlock_irq(&current->sigmask_lock);
201 /* Good thing we saved the old gr[30], eh? */
202 if (restore_sigcontext(&frame->uc.uc_mcontext, regs))
203 goto give_sigsegv;
205 #if DEBUG_SIG
206 printk("usp: %#08lx stack %p",
207 usp, &frame->uc.uc_stack);
208 #endif
210 /* I don't know why everyone else assumes they can call this
211 with a pointer to a stack_t on the kernel stack. That
212 makes no sense. Anyway we'll do it like m68k, since we
213 also are using segmentation in the same way as them. */
214 if (do_sigaltstack(&frame->uc.uc_stack, NULL, usp) == -EFAULT)
215 goto give_sigsegv;
217 /* If we are on the syscall path IAOQ will not be restored, and
218 * if we are on the interrupt path we must not corrupt gr31.
220 if (in_syscall)
221 regs->gr[31] = regs->iaoq[0];
222 #if DEBUG_SIG
223 printk("returning to %#lx\n", regs->iaoq[0]);
224 printk("in sys_rt_sigreturn:\n");
225 show_regs(regs);
226 #endif
227 return;
229 give_sigsegv:
230 #if DEBUG_SIG
231 printk("fuckup in sys_rt_sigreturn, sending SIGSEGV\n");
232 #endif
233 si.si_signo = SIGSEGV;
234 si.si_errno = 0;
235 si.si_code = SI_KERNEL;
236 si.si_pid = current->pid;
237 si.si_uid = current->uid;
238 si.si_addr = &frame->uc;
239 force_sig_info(SIGSEGV, &si, current);
240 return;
244 * Set up a signal frame.
247 static inline void *
248 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
250 if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && ! on_sig_stack(sp))
251 sp = current->sas_ss_sp + current->sas_ss_size;
253 return (void *) sp; /* Stacks grow up. Fun. */
256 static long
257 setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs, int in_syscall)
260 unsigned long flags = 0;
261 long err = 0;
263 if (on_sig_stack((unsigned long) sc))
264 flags |= PARISC_SC_FLAG_ONSTACK;
265 if (in_syscall) {
266 flags |= PARISC_SC_FLAG_IN_SYSCALL;
267 /* regs->iaoq is undefined in the syscall return path */
268 err |= __put_user(regs->gr[31], &sc->sc_iaoq[0]);
269 err |= __put_user(regs->gr[31]+4, &sc->sc_iaoq[1]);
270 #if DEBUG_SIG
271 printk("setup_sigcontext: iaoq %#lx/%#lx\n", regs->gr[31], regs->gr[31]);
272 #endif
273 } else {
274 err |= __copy_to_user(sc->sc_iaoq, regs->iaoq, sizeof(regs->iaoq));
275 err |= __copy_to_user(sc->sc_iasq, regs->iasq, sizeof(regs->iasq));
276 #if DEBUG_SIG
277 printk("setup_sigcontext: iaoq %#lx/%#lx\n", regs->iaoq[0], regs->iaoq[1]);
278 #endif
281 err |= __put_user(flags, &sc->sc_flags);
282 err |= __copy_to_user(sc->sc_gr, regs->gr, sizeof(regs->gr));
283 err |= __copy_to_user(sc->sc_fr, regs->fr, sizeof(regs->fr));
284 err |= __put_user(regs->sar, &sc->sc_sar);
285 #if DEBUG_SIG
286 printk("setup_sigcontext: r28 is %ld\n", regs->gr[28]);
287 #endif
289 return err;
292 static long
293 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
294 sigset_t *set, struct pt_regs *regs, int in_syscall)
296 struct rt_sigframe *frame;
297 unsigned long rp, usp, haddr;
298 struct siginfo si;
299 int err = 0;
301 usp = regs->gr[30];
302 /* access_ok is broken, so do a simplistic "are we stomping on
303 kernel space" assertion. */
304 if (usp > PAGE_OFFSET) {
305 printk("setup_rt_frame: called on kernel space (usp=%#lx), NOW YOU MUST DIE!!!\n",
306 usp);
307 show_regs(regs);
308 while(1);
311 frame = get_sigframe(ka, usp, sizeof(*frame));
312 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
313 goto give_sigsegv;
315 #if DEBUG_SIG
316 printk("setup_rt_frame 1: frame %p info %p\n", frame, info);
317 #endif
319 err |= __copy_to_user(&frame->info, info, sizeof(siginfo_t));
320 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
321 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
322 err |= __put_user(sas_ss_flags(regs->gr[30]),
323 &frame->uc.uc_stack.ss_flags);
324 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, in_syscall);
325 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
326 if (err)
327 goto give_sigsegv;
329 /* Set up to return from userspace. If provided, use a stub
330 already in userspace. */
331 err |= __put_user(in_syscall ? INSN_LDI_R25_1 : INSN_LDI_R25_0,
332 &frame->tramp[0]);
333 err |= __put_user(INSN_LDI_R20, &frame->tramp[1]);
334 err |= __put_user(INSN_BLE_SR2_R0, &frame->tramp[2]);
335 err |= __put_user(INSN_NOP, &frame->tramp[3]);
337 #if DEBUG_SIG
338 /* Assert that we're flushing in the correct space... */
340 int sid;
341 asm ("mfsp %%sr3,%0" : "=r" (sid));
342 printk("flushing 64 bytes at space %#x offset %p\n",
343 sid, frame->tramp);
345 #endif
347 #if CACHE_FLUSHING_IS_NOT_BROKEN
348 flush_icache_range((unsigned long) &frame->tramp[0],
349 (unsigned long) &frame->tramp[4]);
350 #else
351 /* It should *always* be cache line-aligned, but the compiler
352 sometimes screws up. */
353 asm volatile("fdc 0(%%sr3,%0)\n\t"
354 "fdc %1(%%sr3,%0)\n\t"
355 "sync\n\t"
356 "fic 0(%%sr3,%0)\n\t"
357 "fic %1(%%sr3,%0)\n\t"
358 "sync\n\t"
359 : : "r" (frame->tramp), "r" (L1_CACHE_BYTES));
360 #endif
361 rp = (unsigned long) frame->tramp;
363 if (err)
364 goto give_sigsegv;
366 #ifdef __LP64__
367 /* Much more has to happen with signals than this -- but it'll at least */
368 /* provide a pointer to some places which definitely need a look. */
369 #define HACK unsigned int
370 #else
371 #define HACK unsigned long
372 #endif
373 haddr = (HACK) ka->sa.sa_handler;
374 /* ARGH! Fucking brain damage. You don't want to know. */
375 if (haddr & 2) {
376 HACK *plabel;
377 HACK ltp;
379 plabel = (HACK *) (haddr & ~3);
380 err |= __get_user(haddr, plabel);
381 err |= __get_user(ltp, plabel + 1);
382 if (err)
383 goto give_sigsegv;
384 regs->gr[19] = ltp;
387 /* The syscall return path will create IAOQ values from r31.
389 if (in_syscall)
390 regs->gr[31] = (HACK) haddr;
391 else {
392 regs->iaoq[0] = (HACK) haddr | 3;
393 regs->iaoq[1] = regs->iaoq[0] + 4;
396 regs->gr[2] = rp; /* userland return pointer */
397 regs->gr[26] = sig; /* signal number */
398 regs->gr[25] = (HACK) &frame->info; /* siginfo pointer */
399 regs->gr[24] = (HACK) &frame->uc; /* ucontext pointer */
400 #if DEBUG_SIG
401 printk("making sigreturn frame: %#lx + %#lx = %#lx\n",
402 regs->gr[30], PARISC_RT_SIGFRAME_SIZE,
403 regs->gr[30] + PARISC_RT_SIGFRAME_SIZE);
404 #endif
405 /* Raise the user stack pointer to make a proper call frame. */
406 regs->gr[30] = ((HACK) frame + PARISC_RT_SIGFRAME_SIZE);
408 #if DEBUG_SIG
409 printk("SIG deliver (%s:%d): frame=0x%p sp=%#lx iaoq=%#lx/%#lx rp=%#lx\n",
410 current->comm, current->pid, frame, regs->gr[30],
411 regs->iaoq[0], regs->iaoq[1], rp);
412 #endif
414 return 1;
416 give_sigsegv:
417 #if DEBUG_SIG
418 printk("fuckup in setup_rt_frame, sending SIGSEGV\n");
419 #endif
420 if (sig == SIGSEGV)
421 ka->sa.sa_handler = SIG_DFL;
422 si.si_signo = SIGSEGV;
423 si.si_errno = 0;
424 si.si_code = SI_KERNEL;
425 si.si_pid = current->pid;
426 si.si_uid = current->uid;
427 si.si_addr = frame;
428 force_sig_info(SIGSEGV, &si, current);
429 return 0;
433 * OK, we're invoking a handler.
436 static long
437 handle_signal(unsigned long sig, struct k_sigaction *ka,
438 siginfo_t *info, sigset_t *oldset,
439 struct pt_regs *regs, int in_syscall)
441 #if DEBUG_SIG
442 printk("handle_signal(sig=%ld, ka=%p, info=%p, oldset=%p, regs=%p)\n",
443 sig, ka, info, oldset, regs);
444 #endif
445 /* Set up the stack frame */
446 if (!setup_rt_frame(sig, ka, info, oldset, regs, in_syscall))
447 return 0;
449 if (ka->sa.sa_flags & SA_ONESHOT)
450 ka->sa.sa_handler = SIG_DFL;
452 if (!(ka->sa.sa_flags & SA_NODEFER)) {
453 spin_lock_irq(&current->sigmask_lock);
454 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
455 sigaddset(&current->blocked,sig);
456 recalc_sigpending(current);
457 spin_unlock_irq(&current->sigmask_lock);
459 return 1;
463 * Note that 'init' is a special process: it doesn't get signals it doesn't
464 * want to handle. Thus you cannot kill init even with a SIGKILL even by
465 * mistake.
467 * We need to be able to restore the syscall arguments (r21-r26) to
468 * restart syscalls. Thus, the syscall path should save them in the
469 * pt_regs structure (it's okay to do so since they are caller-save
470 * registers). As noted below, the syscall number gets restored for
471 * us due to the magic of delayed branching.
473 asmlinkage int
474 do_signal(sigset_t *oldset, struct pt_regs *regs, int in_syscall)
476 siginfo_t info;
477 struct k_sigaction *ka;
479 #if DEBUG_SIG
480 printk("do_signal(oldset=0x%p, regs=0x%p, sr7 %#lx, pending %d, in_syscall=%d\n",
481 oldset, regs, regs->sr[7], current->sigpending, in_syscall);
482 #endif
483 /* Everyone else checks to see if they are in kernel mode at
484 this point and exits if that's the case. I'm not sure why
485 we would be called in that case, but for some reason we
486 are. */
488 if (!oldset)
489 oldset = &current->blocked;
491 #if DEBUG_SIG
492 printk("do_signal: oldset %08lx:%08lx\n", oldset->sig[0], oldset->sig[1]);
493 #endif
495 for (;;) {
496 unsigned long signr;
498 spin_lock_irq(&current->sigmask_lock);
499 signr = dequeue_signal(&current->blocked, &info);
500 spin_unlock_irq(&current->sigmask_lock);
501 #if DEBUG_SIG
502 printk("do_signal: signr=%ld, pid=%d\n", signr, current->pid);
503 #endif
505 if (!signr)
506 break;
508 if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
509 /* Let the debugger run. */
510 current->exit_code = signr;
511 set_current_state(TASK_STOPPED);
512 notify_parent(current, SIGCHLD);
513 schedule();
515 /* We're back. Did the debugger cancel the sig? */
516 if (!(signr = current->exit_code))
517 continue;
518 current->exit_code = 0;
520 /* The debugger continued. Ignore SIGSTOP. */
521 if (signr == SIGSTOP)
522 continue;
524 /* Update the siginfo structure. Is this good? */
525 if (signr != info.si_signo) {
526 info.si_signo = signr;
527 info.si_errno = 0;
528 info.si_code = SI_USER;
529 info.si_pid = current->p_pptr->pid;
530 info.si_uid = current->p_pptr->uid;
533 /* If the (new) signal is now blocked, requeue it. */
534 if (sigismember(&current->blocked, signr)) {
535 send_sig_info(signr, &info, current);
536 continue;
540 ka = &current->sig->action[signr-1];
541 #if DEBUG_SIG
542 printk("sa_handler is %lx\n", ka->sa.sa_handler);
543 #endif
544 if ((unsigned long) ka->sa.sa_handler == (unsigned long) SIG_IGN) {
545 if (signr != SIGCHLD)
546 continue;
547 while (sys_wait4(-1, NULL, WNOHANG, NULL) > 0)
548 /* nothing */;
549 continue;
552 if ((unsigned long) ka->sa.sa_handler == (unsigned long) SIG_DFL) {
553 int exit_code = signr;
555 /* Init gets no signals it doesn't want. */
556 if (current->pid == 1)
557 continue;
559 switch (signr) {
560 case SIGCONT: case SIGCHLD: case SIGWINCH:
561 continue;
563 case SIGTSTP: case SIGTTIN: case SIGTTOU:
564 if (is_orphaned_pgrp(current->pgrp))
565 continue;
566 /* FALLTHRU */
568 case SIGSTOP:
569 set_current_state(TASK_STOPPED);
570 current->exit_code = signr;
571 if (!(current->p_pptr->sig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
572 notify_parent(current, SIGCHLD);
573 schedule();
574 continue;
576 case SIGQUIT: case SIGILL: case SIGTRAP:
577 case SIGABRT: case SIGFPE: case SIGSEGV:
578 case SIGBUS: case SIGSYS: case SIGXCPU: case SIGXFSZ:
579 if (signr == SIGQUIT) /* Userspace debugging */
580 show_regs(regs);
581 if (do_coredump(signr, regs))
582 exit_code |= 0x80;
583 /* FALLTHRU */
585 default:
586 lock_kernel();
587 sigaddset(&current->pending.signal, signr);
588 recalc_sigpending(current);
589 current->flags |= PF_SIGNALED;
590 do_exit(exit_code);
591 /* NOTREACHED */
595 /* Restart a system call if necessary. */
596 if (in_syscall) {
597 /* Check the return code */
598 switch (regs->gr[28]) {
599 case -ERESTARTNOHAND:
600 #if DEBUG_SIG
601 printk("ERESTARTNOHAND: returning -EINTR\n");
602 #endif
603 regs->gr[28] = -EINTR;
604 break;
606 case -ERESTARTSYS:
607 if (!(ka->sa.sa_flags & SA_RESTART)) {
608 #if DEBUG_SIG
609 printk("ERESTARTSYS: putting -EINTR\n");
610 #endif
611 regs->gr[28] = -EINTR;
612 break;
614 /* fallthrough */
615 case -ERESTARTNOINTR:
616 /* A syscall is just a branch, so all
617 we have to do is fiddle the return
618 pointer. */
619 regs->gr[31] -= 8; /* delayed branching */
620 /* Preserve original r28. */
621 regs->gr[28] = regs->orig_r28;
622 break;
625 /* Whee! Actually deliver the signal. If the
626 delivery failed, we need to continue to iterate in
627 this loop so we can deliver the SIGSEGV... */
628 if (handle_signal(signr, ka, &info, oldset, regs, in_syscall)) {
629 #if DEBUG_SIG
630 printk("Exiting do_signal (success), regs->gr[28] = %ld\n", regs->gr[28]);
631 #endif
632 return 1;
636 /* Did we come from a system call? */
637 if (in_syscall) {
638 /* Restart the system call - no handlers present */
639 if (regs->gr[28] == -ERESTARTNOHAND ||
640 regs->gr[28] == -ERESTARTSYS ||
641 regs->gr[28] == -ERESTARTNOINTR) {
642 /* Hooray for delayed branching. We don't
643 have to restore %r20 (the system call
644 number) because it gets loaded in the delay
645 slot of the branch external instruction. */
646 regs->gr[31] -= 8;
647 /* Preserve original r28. */
648 regs->gr[28] = regs->orig_r28;
651 #if DEBUG_SIG
652 printk("Exiting do_signal (not delivered), regs->gr[28] = %ld\n", regs->gr[28]);
653 #endif
654 return 0;