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