2 * linux/arch/parisc/kernel/signal.c: Architecture-specific signal
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/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/ptrace.h>
25 #include <linux/unistd.h>
26 #include <linux/stddef.h>
27 #include <linux/compat.h>
28 #include <linux/elf.h>
29 #include <linux/personality.h>
30 #include <asm/ucontext.h>
31 #include <asm/rt_sigframe.h>
32 #include <asm/uaccess.h>
33 #include <asm/pgalloc.h>
34 #include <asm/cacheflush.h>
35 #include <asm/asm-offsets.h>
38 #include <linux/compat.h>
43 #define DEBUG_SIG_LEVEL 2
46 #define DBG(LEVEL, ...) \
47 ((DEBUG_SIG_LEVEL >= LEVEL) \
48 ? printk(__VA_ARGS__) : (void) 0)
50 #define DBG(LEVEL, ...)
54 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
56 /* gcc will complain if a pointer is cast to an integer of different
57 * size. If you really need to do this (and we do for an ELF32 user
58 * application in an ELF64 kernel) then you have to do a cast to an
59 * integer of the same size first. The A() macro accomplishes
61 #define A(__x) ((unsigned long)(__x))
63 int do_signal(sigset_t *oldset, struct pt_regs *regs, int in_syscall);
66 * Atomically swap in the new signal mask, and wait for a signal.
73 sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize, struct pt_regs *regs)
75 sigset_t saveset, newset;
77 compat_sigset_t newset32;
79 if(personality(current->personality) == PER_LINUX32){
80 /* XXX: Don't preclude handling different sized sigset_t's. */
81 if (sigsetsize != sizeof(compat_sigset_t))
83 if (copy_from_user(&newset32, (compat_sigset_t __user *)unewset, sizeof(newset32)))
85 sigset_32to64(&newset,&newset32);
90 /* XXX: Don't preclude handling different sized sigset_t's. */
91 if (sigsetsize != sizeof(sigset_t))
94 if (copy_from_user(&newset, unewset, sizeof(newset)))
98 sigdelsetmask(&newset, ~_BLOCKABLE);
100 spin_lock_irq(¤t->sighand->siglock);
101 saveset = current->blocked;
102 current->blocked = newset;
104 spin_unlock_irq(¤t->sighand->siglock);
106 regs->gr[28] = -EINTR;
108 current->state = TASK_INTERRUPTIBLE;
110 if (do_signal(&saveset, regs, 1))
116 * Do a signal return - restore sigcontext.
119 /* Trampoline for calling rt_sigreturn() */
120 #define INSN_LDI_R25_0 0x34190000 /* ldi 0,%r25 (in_syscall=0) */
121 #define INSN_LDI_R25_1 0x34190002 /* ldi 1,%r25 (in_syscall=1) */
122 #define INSN_LDI_R20 0x3414015a /* ldi __NR_rt_sigreturn,%r20 */
123 #define INSN_BLE_SR2_R0 0xe4008200 /* be,l 0x100(%sr2,%r0),%sr0,%r31 */
124 #define INSN_NOP 0x08000240 /* nop */
126 #define INSN_DIE_HORRIBLY 0x68000ccc /* stw %r0,0x666(%sr0,%r0) */
129 restore_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
133 err |= __copy_from_user(regs->gr, sc->sc_gr, sizeof(regs->gr));
134 err |= __copy_from_user(regs->fr, sc->sc_fr, sizeof(regs->fr));
135 err |= __copy_from_user(regs->iaoq, sc->sc_iaoq, sizeof(regs->iaoq));
136 err |= __copy_from_user(regs->iasq, sc->sc_iasq, sizeof(regs->iasq));
137 err |= __get_user(regs->sar, &sc->sc_sar);
138 DBG(2,"restore_sigcontext: iaoq is 0x%#lx / 0x%#lx\n",
139 regs->iaoq[0],regs->iaoq[1]);
140 DBG(2,"restore_sigcontext: r28 is %ld\n", regs->gr[28]);
145 sys_rt_sigreturn(struct pt_regs *regs, int in_syscall)
147 struct rt_sigframe __user *frame;
150 unsigned long usp = (regs->gr[30] & ~(0x01UL));
151 unsigned long sigframe_size = PARISC_RT_SIGFRAME_SIZE;
153 compat_sigset_t compat_set;
154 struct compat_rt_sigframe __user * compat_frame;
156 if(personality(current->personality) == PER_LINUX32)
157 sigframe_size = PARISC_RT_SIGFRAME_SIZE32;
161 /* Unwind the user stack to get the rt_sigframe structure. */
162 frame = (struct rt_sigframe __user *)
163 (usp - sigframe_size);
164 DBG(2,"sys_rt_sigreturn: frame is %p\n", frame);
167 compat_frame = (struct compat_rt_sigframe __user *)frame;
169 if(personality(current->personality) == PER_LINUX32){
170 DBG(2,"sys_rt_sigreturn: ELF32 process.\n");
171 if (__copy_from_user(&compat_set, &compat_frame->uc.uc_sigmask, sizeof(compat_set)))
173 sigset_32to64(&set,&compat_set);
177 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
181 sigdelsetmask(&set, ~_BLOCKABLE);
182 spin_lock_irq(¤t->sighand->siglock);
183 current->blocked = set;
185 spin_unlock_irq(¤t->sighand->siglock);
187 /* Good thing we saved the old gr[30], eh? */
189 if(personality(current->personality) == PER_LINUX32){
190 DBG(1,"sys_rt_sigreturn: compat_frame->uc.uc_mcontext 0x%p\n",
191 &compat_frame->uc.uc_mcontext);
192 // FIXME: Load upper half from register file
193 if (restore_sigcontext32(&compat_frame->uc.uc_mcontext,
194 &compat_frame->regs, regs))
196 DBG(1,"sys_rt_sigreturn: usp %#08lx stack 0x%p\n",
197 usp, &compat_frame->uc.uc_stack);
198 if (do_sigaltstack32(&compat_frame->uc.uc_stack, NULL, usp) == -EFAULT)
203 DBG(1,"sys_rt_sigreturn: frame->uc.uc_mcontext 0x%p\n",
204 &frame->uc.uc_mcontext);
205 if (restore_sigcontext(&frame->uc.uc_mcontext, regs))
207 DBG(1,"sys_rt_sigreturn: usp %#08lx stack 0x%p\n",
208 usp, &frame->uc.uc_stack);
209 if (do_sigaltstack(&frame->uc.uc_stack, NULL, usp) == -EFAULT)
215 /* If we are on the syscall path IAOQ will not be restored, and
216 * if we are on the interrupt path we must not corrupt gr31.
219 regs->gr[31] = regs->iaoq[0];
221 DBG(1,"sys_rt_sigreturn: returning to %#lx, DUMPING REGS:\n", regs->iaoq[0]);
227 DBG(1,"sys_rt_sigreturn: Sending SIGSEGV\n");
228 si.si_signo = SIGSEGV;
230 si.si_code = SI_KERNEL;
231 si.si_pid = current->pid;
232 si.si_uid = current->uid;
233 si.si_addr = &frame->uc;
234 force_sig_info(SIGSEGV, &si, current);
239 * Set up a signal frame.
242 static inline void __user *
243 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
245 /*FIXME: ELF32 vs. ELF64 has different frame_size, but since we
246 don't use the parameter it doesn't matter */
248 DBG(1,"get_sigframe: ka = %#lx, sp = %#lx, frame_size = %#lx\n",
249 (unsigned long)ka, sp, frame_size);
251 if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && ! on_sig_stack(sp))
252 sp = current->sas_ss_sp; /* Stacks grow up! */
254 DBG(1,"get_sigframe: Returning sp = %#lx\n", (unsigned long)sp);
255 return (void __user *) sp; /* Stacks grow up. Fun. */
259 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, int in_syscall)
262 unsigned long flags = 0;
265 if (on_sig_stack((unsigned long) sc))
266 flags |= PARISC_SC_FLAG_ONSTACK;
268 flags |= PARISC_SC_FLAG_IN_SYSCALL;
269 /* regs->iaoq is undefined in the syscall return path */
270 err |= __put_user(regs->gr[31], &sc->sc_iaoq[0]);
271 err |= __put_user(regs->gr[31]+4, &sc->sc_iaoq[1]);
272 err |= __put_user(regs->sr[3], &sc->sc_iasq[0]);
273 err |= __put_user(regs->sr[3], &sc->sc_iasq[1]);
274 DBG(1,"setup_sigcontext: iaoq %#lx / %#lx (in syscall)\n",
275 regs->gr[31], regs->gr[31]+4);
277 err |= __copy_to_user(sc->sc_iaoq, regs->iaoq, sizeof(regs->iaoq));
278 err |= __copy_to_user(sc->sc_iasq, regs->iasq, sizeof(regs->iasq));
279 DBG(1,"setup_sigcontext: iaoq %#lx / %#lx (not in syscall)\n",
280 regs->iaoq[0], regs->iaoq[1]);
283 err |= __put_user(flags, &sc->sc_flags);
284 err |= __copy_to_user(sc->sc_gr, regs->gr, sizeof(regs->gr));
285 err |= __copy_to_user(sc->sc_fr, regs->fr, sizeof(regs->fr));
286 err |= __put_user(regs->sar, &sc->sc_sar);
287 DBG(1,"setup_sigcontext: r28 is %ld\n", regs->gr[28]);
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 __user *frame;
297 unsigned long rp, usp;
298 unsigned long haddr, sigframe_size;
301 compat_int_t compat_val;
302 struct compat_rt_sigframe __user * compat_frame;
303 compat_sigset_t compat_set;
306 usp = (regs->gr[30] & ~(0x01UL));
307 /*FIXME: frame_size parameter is unused, remove it. */
308 frame = get_sigframe(ka, usp, sizeof(*frame));
310 DBG(1,"SETUP_RT_FRAME: START\n");
311 DBG(1,"setup_rt_frame: frame %p info %p\n", frame, info);
316 compat_frame = (struct compat_rt_sigframe __user *)frame;
318 if(personality(current->personality) == PER_LINUX32) {
319 DBG(1,"setup_rt_frame: frame->info = 0x%p\n", &compat_frame->info);
320 err |= compat_copy_siginfo_to_user(&compat_frame->info, info);
321 DBG(1,"SETUP_RT_FRAME: 1\n");
322 compat_val = (compat_int_t)current->sas_ss_sp;
323 err |= __put_user(compat_val, &compat_frame->uc.uc_stack.ss_sp);
324 DBG(1,"SETUP_RT_FRAME: 2\n");
325 compat_val = (compat_int_t)current->sas_ss_size;
326 err |= __put_user(compat_val, &compat_frame->uc.uc_stack.ss_size);
327 DBG(1,"SETUP_RT_FRAME: 3\n");
328 compat_val = sas_ss_flags(regs->gr[30]);
329 err |= __put_user(compat_val, &compat_frame->uc.uc_stack.ss_flags);
330 DBG(1,"setup_rt_frame: frame->uc = 0x%p\n", &compat_frame->uc);
331 DBG(1,"setup_rt_frame: frame->uc.uc_mcontext = 0x%p\n", &compat_frame->uc.uc_mcontext);
332 err |= setup_sigcontext32(&compat_frame->uc.uc_mcontext,
333 &compat_frame->regs, regs, in_syscall);
334 sigset_64to32(&compat_set,set);
335 err |= __copy_to_user(&compat_frame->uc.uc_sigmask, &compat_set, sizeof(compat_set));
339 DBG(1,"setup_rt_frame: frame->info = 0x%p\n", &frame->info);
340 err |= copy_siginfo_to_user(&frame->info, info);
341 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
342 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
343 err |= __put_user(sas_ss_flags(regs->gr[30]),
344 &frame->uc.uc_stack.ss_flags);
345 DBG(1,"setup_rt_frame: frame->uc = 0x%p\n", &frame->uc);
346 DBG(1,"setup_rt_frame: frame->uc.uc_mcontext = 0x%p\n", &frame->uc.uc_mcontext);
347 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, in_syscall);
348 /* FIXME: Should probably be converted aswell for the compat case */
349 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
355 /* Set up to return from userspace. If provided, use a stub
356 already in userspace. The first words of tramp are used to
357 save the previous sigrestartblock trampoline that might be
358 on the stack. We start the sigreturn trampoline at
359 SIGRESTARTBLOCK_TRAMP+X. */
360 err |= __put_user(in_syscall ? INSN_LDI_R25_1 : INSN_LDI_R25_0,
361 &frame->tramp[SIGRESTARTBLOCK_TRAMP+0]);
362 err |= __put_user(INSN_LDI_R20,
363 &frame->tramp[SIGRESTARTBLOCK_TRAMP+1]);
364 err |= __put_user(INSN_BLE_SR2_R0,
365 &frame->tramp[SIGRESTARTBLOCK_TRAMP+2]);
366 err |= __put_user(INSN_NOP, &frame->tramp[SIGRESTARTBLOCK_TRAMP+3]);
369 /* Assert that we're flushing in the correct space... */
372 asm ("mfsp %%sr3,%0" : "=r" (sid));
373 DBG(1,"setup_rt_frame: Flushing 64 bytes at space %#x offset %p\n",
378 flush_user_dcache_range((unsigned long) &frame->tramp[0],
379 (unsigned long) &frame->tramp[TRAMP_SIZE]);
380 flush_user_icache_range((unsigned long) &frame->tramp[0],
381 (unsigned long) &frame->tramp[TRAMP_SIZE]);
383 /* TRAMP Words 0-4, Lenght 5 = SIGRESTARTBLOCK_TRAMP
384 * TRAMP Words 5-9, Length 4 = SIGRETURN_TRAMP
385 * So the SIGRETURN_TRAMP is at the end of SIGRESTARTBLOCK_TRAMP
387 rp = (unsigned long) &frame->tramp[SIGRESTARTBLOCK_TRAMP];
392 haddr = A(ka->sa.sa_handler);
393 /* The sa_handler may be a pointer to a function descriptor */
395 if(personality(current->personality) == PER_LINUX32) {
397 if (haddr & PA_PLABEL_FDESC) {
399 Elf32_Fdesc __user *ufdesc = (Elf32_Fdesc __user *)A(haddr & ~3);
401 err = __copy_from_user(&fdesc, ufdesc, sizeof(fdesc));
407 regs->gr[19] = fdesc.gp;
412 Elf64_Fdesc __user *ufdesc = (Elf64_Fdesc __user *)A(haddr & ~3);
414 err = __copy_from_user(&fdesc, ufdesc, sizeof(fdesc));
420 regs->gr[19] = fdesc.gp;
421 DBG(1,"setup_rt_frame: 64 bit signal, exe=%#lx, r19=%#lx, in_syscall=%d\n",
422 haddr, regs->gr[19], in_syscall);
426 /* The syscall return path will create IAOQ values from r31.
428 sigframe_size = PARISC_RT_SIGFRAME_SIZE;
430 if(personality(current->personality) == PER_LINUX32)
431 sigframe_size = PARISC_RT_SIGFRAME_SIZE32;
434 regs->gr[31] = haddr;
436 if(personality(current->personality) == PER_LINUX)
440 unsigned long psw = USER_PSW;
442 if(personality(current->personality) == PER_LINUX)
446 /* If we are singlestepping, arrange a trap to be delivered
447 when we return to userspace. Note the semantics -- we
448 should trap before the first insn in the handler is
450 http://sources.redhat.com/ml/gdb/2004-11/msg00245.html
452 if (pa_psw(current)->r) {
453 pa_psw(current)->r = 0;
459 regs->iaoq[0] = haddr | 3;
460 regs->iaoq[1] = regs->iaoq[0] + 4;
463 regs->gr[2] = rp; /* userland return pointer */
464 regs->gr[26] = sig; /* signal number */
467 if(personality(current->personality) == PER_LINUX32){
468 regs->gr[25] = A(&compat_frame->info); /* siginfo pointer */
469 regs->gr[24] = A(&compat_frame->uc); /* ucontext pointer */
473 regs->gr[25] = A(&frame->info); /* siginfo pointer */
474 regs->gr[24] = A(&frame->uc); /* ucontext pointer */
477 DBG(1,"setup_rt_frame: making sigreturn frame: %#lx + %#lx = %#lx\n",
478 regs->gr[30], sigframe_size,
479 regs->gr[30] + sigframe_size);
480 /* Raise the user stack pointer to make a proper call frame. */
481 regs->gr[30] = (A(frame) + sigframe_size);
484 DBG(1,"setup_rt_frame: sig deliver (%s,%d) frame=0x%p sp=%#lx iaoq=%#lx/%#lx rp=%#lx\n",
485 current->comm, current->pid, frame, regs->gr[30],
486 regs->iaoq[0], regs->iaoq[1], rp);
491 DBG(1,"setup_rt_frame: sending SIGSEGV\n");
492 force_sigsegv(sig, current);
497 * OK, we're invoking a handler.
501 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
502 sigset_t *oldset, struct pt_regs *regs, int in_syscall)
504 DBG(1,"handle_signal: sig=%ld, ka=%p, info=%p, oldset=%p, regs=%p\n",
505 sig, ka, info, oldset, regs);
507 /* Set up the stack frame */
508 if (!setup_rt_frame(sig, ka, info, oldset, regs, in_syscall))
511 spin_lock_irq(¤t->sighand->siglock);
512 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
513 if (!(ka->sa.sa_flags & SA_NODEFER))
514 sigaddset(¤t->blocked,sig);
516 spin_unlock_irq(¤t->sighand->siglock);
521 * Note that 'init' is a special process: it doesn't get signals it doesn't
522 * want to handle. Thus you cannot kill init even with a SIGKILL even by
525 * We need to be able to restore the syscall arguments (r21-r26) to
526 * restart syscalls. Thus, the syscall path should save them in the
527 * pt_regs structure (it's okay to do so since they are caller-save
528 * registers). As noted below, the syscall number gets restored for
529 * us due to the magic of delayed branching.
533 do_signal(sigset_t *oldset, struct pt_regs *regs, int in_syscall)
536 struct k_sigaction ka;
539 DBG(1,"\ndo_signal: oldset=0x%p, regs=0x%p, sr7 %#lx, in_syscall=%d\n",
540 oldset, regs, regs->sr[7], in_syscall);
542 /* Everyone else checks to see if they are in kernel mode at
543 this point and exits if that's the case. I'm not sure why
544 we would be called in that case, but for some reason we
548 oldset = ¤t->blocked;
550 DBG(1,"do_signal: oldset %08lx / %08lx\n",
551 oldset->sig[0], oldset->sig[1]);
554 /* May need to force signal if handle_signal failed to deliver */
557 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
558 DBG(3,"do_signal: signr = %d, regs->gr[28] = %ld\n", signr, regs->gr[28]);
563 /* Restart a system call if necessary. */
565 /* Check the return code */
566 switch (regs->gr[28]) {
567 case -ERESTART_RESTARTBLOCK:
568 current_thread_info()->restart_block.fn = do_no_restart_syscall;
569 case -ERESTARTNOHAND:
570 DBG(1,"ERESTARTNOHAND: returning -EINTR\n");
571 regs->gr[28] = -EINTR;
575 if (!(ka.sa.sa_flags & SA_RESTART)) {
576 DBG(1,"ERESTARTSYS: putting -EINTR\n");
577 regs->gr[28] = -EINTR;
581 case -ERESTARTNOINTR:
582 /* A syscall is just a branch, so all
583 we have to do is fiddle the return pointer. */
584 regs->gr[31] -= 8; /* delayed branching */
585 /* Preserve original r28. */
586 regs->gr[28] = regs->orig_r28;
590 /* Whee! Actually deliver the signal. If the
591 delivery failed, we need to continue to iterate in
592 this loop so we can deliver the SIGSEGV... */
593 if (handle_signal(signr, &info, &ka, oldset, regs, in_syscall)) {
594 DBG(1,KERN_DEBUG "do_signal: Exit (success), regs->gr[28] = %ld\n",
599 /* end of while(1) looping forever if we can't force a signal */
601 /* Did we come from a system call? */
603 /* Restart the system call - no handlers present */
604 if (regs->gr[28] == -ERESTART_RESTARTBLOCK) {
605 unsigned int *usp = (unsigned int *)regs->gr[30];
607 /* Setup a trampoline to restart the syscall
608 * with __NR_restart_syscall
610 * 0: <return address (orig r31)>
611 * 4: <2nd half for 64-bit>
612 * 8: ldw 0(%sp), %r31
613 * 12: be 0x100(%sr2, %r0)
614 * 16: ldi __NR_restart_syscall, %r20
617 put_user(regs->gr[31], &usp[0]);
618 put_user(0x0fc0109f, &usp[2]);
620 put_user(regs->gr[31] >> 32, &usp[0]);
621 put_user(regs->gr[31] & 0xffffffff, &usp[1]);
622 put_user(0x0fc010df, &usp[2]);
624 put_user(0xe0008200, &usp[3]);
625 put_user(0x34140000, &usp[4]);
627 /* Stack is 64-byte aligned, and we only need
628 * to flush 1 cache line.
629 * Flushing one cacheline is cheap.
630 * "sync" on bigger (> 4 way) boxes is not.
632 asm("fdc %%r0(%%sr3, %0)\n"
634 "fic %%r0(%%sr3, %0)\n"
636 : : "r"(regs->gr[30]));
638 regs->gr[31] = regs->gr[30] + 8;
639 /* Preserve original r28. */
640 regs->gr[28] = regs->orig_r28;
641 } else if (regs->gr[28] == -ERESTARTNOHAND ||
642 regs->gr[28] == -ERESTARTSYS ||
643 regs->gr[28] == -ERESTARTNOINTR) {
644 /* Hooray for delayed branching. We don't
645 have to restore %r20 (the system call
646 number) because it gets loaded in the delay
647 slot of the branch external instruction. */
649 /* Preserve original r28. */
650 regs->gr[28] = regs->orig_r28;
654 DBG(1,"do_signal: Exit (not delivered), regs->gr[28] = %ld\n",