1 /* $NetBSD: sig_machdep.c,v 1.16 2008/04/28 20:23:28 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 2003 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 33 34 __KERNEL_RCSID(0, "$NetBSD: sig_machdep.c,v 1.16 2008/04/28 20:23:28 martin Exp $"); 35 36 #include "opt_cputype.h" 37 #include "opt_compat_netbsd.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/proc.h> 43 #include <sys/user.h> 44 #include <sys/signal.h> 45 #include <sys/signalvar.h> 46 #include <sys/mount.h> 47 #include <sys/syscallargs.h> 48 49 #include <machine/cpu.h> 50 51 #include <mips/frame.h> 52 #include <mips/regnum.h> 53 54 void * 55 getframe(struct lwp *l, int sig, int *onstack) 56 { 57 struct proc *p = l->l_proc; 58 struct frame *fp = l->l_md.md_regs; 59 60 /* Do we need to jump onto the signal stack? */ 61 *onstack = (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 62 && (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0; 63 if (*onstack) 64 return (char *)l->l_sigstk.ss_sp + l->l_sigstk.ss_size; 65 else 66 return (void *)fp->f_regs[_R_SP]; 67 } 68 69 struct sigframe_siginfo { 70 siginfo_t sf_si; 71 ucontext_t sf_uc; 72 }; 73 74 /* 75 * Send a signal to process. 76 */ 77 static void 78 sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask) 79 { 80 struct lwp *l = curlwp; 81 struct proc *p = l->l_proc; 82 struct sigacts *ps = p->p_sigacts; 83 int onstack, error; 84 int sig = ksi->ksi_signo; 85 struct sigframe_siginfo *fp = getframe(l, sig, &onstack); 86 struct frame *tf; 87 ucontext_t uc; 88 size_t ucsz; 89 sig_t catcher = SIGACTION(p, sig).sa_handler; 90 91 tf = (struct frame *)l->l_md.md_regs; 92 fp--; 93 94 /* Build stack frame for signal trampoline. */ 95 switch (ps->sa_sigdesc[sig].sd_vers) { 96 case 0: /* handled by sendsig_sigcontext */ 97 case 1: /* handled by sendsig_sigcontext */ 98 default: /* unknown version */ 99 printf("sendsig_siginfo: bad version %d\n", 100 ps->sa_sigdesc[sig].sd_vers); 101 sigexit(l, SIGILL); 102 case 2: 103 break; 104 } 105 106 uc.uc_flags = _UC_SIGMASK 107 | ((l->l_sigstk.ss_flags & SS_ONSTACK) 108 ? _UC_SETSTACK : _UC_CLRSTACK); 109 uc.uc_sigmask = *mask; 110 uc.uc_link = l->l_ctxlink; 111 memset(&uc.uc_stack, 0, sizeof(uc.uc_stack)); 112 ucsz = (char *)&uc.__uc_pad - (char *)&uc; 113 sendsig_reset(l, sig); 114 mutex_exit(p->p_lock); 115 cpu_getmcontext(l, &uc.uc_mcontext, &uc.uc_flags); 116 error = copyout(&ksi->ksi_info, &fp->sf_si, sizeof(ksi->ksi_info)); 117 if (error == 0) 118 error = copyout(&uc, &fp->sf_uc, ucsz); 119 mutex_enter(p->p_lock); 120 121 if (error != 0) { 122 /* 123 * Process has trashed its stack; give it an illegal 124 * instruction to halt it in its tracks. 125 */ 126 sigexit(l, SIGILL); 127 /* NOTREACHED */ 128 } 129 130 /* 131 * Set up the registers to directly invoke the signal 132 * handler. The return address will be set up to point 133 * to the signal trampoline to bounce us back. 134 */ 135 tf->f_regs[_R_A0] = sig; 136 tf->f_regs[_R_A1] = (__greg_t)&fp->sf_si; 137 tf->f_regs[_R_A2] = (__greg_t)&fp->sf_uc; 138 139 tf->f_regs[_R_PC] = (__greg_t)catcher; 140 tf->f_regs[_R_T9] = (__greg_t)catcher; 141 tf->f_regs[_R_SP] = (__greg_t)fp; 142 tf->f_regs[_R_RA] = (__greg_t)ps->sa_sigdesc[sig].sd_tramp; 143 144 /* Remember that we're now on the signal stack. */ 145 if (onstack) 146 l->l_sigstk.ss_flags |= SS_ONSTACK; 147 } 148 149 void 150 sendsig(const ksiginfo_t *ksi, const sigset_t *mask) 151 { 152 #ifdef COMPAT_16 153 if (curproc->p_sigacts->sa_sigdesc[ksi->ksi_signo].sd_vers < 2) 154 sendsig_sigcontext(ksi, mask); 155 else 156 #endif 157 sendsig_siginfo(ksi, mask); 158 } 159