1 /* $NetBSD: sig_machdep.c,v 1.27 2024/05/17 21:37:07 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1986, 1990 The Regents of the University of California. 5 * All rights reserved. 6 * 7 * Changed for the VAX port (and for readability) /IC 8 * 9 * This code is derived from software contributed to Berkeley by the Systems 10 * Programming Group of the University of Utah Computer Science Department. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: Utah Hdr: machdep.c 1.63 91/04/24 37 * 38 * @(#)machdep.c 7.16 (Berkeley) 6/3/91 39 */ 40 41 /* 42 * Copyright (c) 2002, Hugh Graham. 43 * Copyright (c) 1994, 1998 Ludd, University of Lule}, Sweden. 44 * Copyright (c) 1993 Adam Glass 45 * Copyright (c) 1988 University of Utah. 46 * 47 * Changed for the VAX port (and for readability) /IC 48 * 49 * This code is derived from software contributed to Berkeley by the Systems 50 * Programming Group of the University of Utah Computer Science Department. 51 * 52 * Redistribution and use in source and binary forms, with or without 53 * modification, are permitted provided that the following conditions 54 * are met: 55 * 1. Redistributions of source code must retain the above copyright 56 * notice, this list of conditions and the following disclaimer. 57 * 2. Redistributions in binary form must reproduce the above copyright 58 * notice, this list of conditions and the following disclaimer in the 59 * documentation and/or other materials provided with the distribution. 60 * 3. Neither the name of the University nor the names of its contributors 61 * may be used to endorse or promote products derived from this software 62 * without specific prior written permission. 63 * 64 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 65 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 66 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 67 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 68 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 69 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 70 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 71 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 72 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 73 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 74 * SUCH DAMAGE. 75 * 76 * from: Utah Hdr: machdep.c 1.63 91/04/24 77 * 78 * @(#)machdep.c 7.16 (Berkeley) 6/3/91 79 */ 80 81 #include <sys/cdefs.h> 82 __KERNEL_RCSID(0, "$NetBSD: sig_machdep.c,v 1.27 2024/05/17 21:37:07 thorpej Exp $"); 83 84 #include "opt_ddb.h" 85 #include "opt_compat_netbsd.h" 86 #include "opt_compat_ultrix.h" 87 #include "opt_multiprocessor.h" 88 #include "opt_lockdebug.h" 89 90 #include <sys/param.h> 91 #include <sys/systm.h> 92 #include <sys/cpu.h> 93 #include <sys/proc.h> 94 #include <sys/signal.h> 95 96 #include <machine/macros.h> 97 #include <machine/reg.h> 98 99 #ifdef DDB 100 #include <machine/db_machdep.h> 101 #include <ddb/db_sym.h> 102 #include <ddb/db_extern.h> 103 #endif 104 105 typedef vaddr_t (*sig_setupstack_t)(const struct ksiginfo *, const sigset_t *, 106 int, struct lwp *, struct trapframe *, vaddr_t, int, vaddr_t); 107 108 static vaddr_t setupstack_siginfo3(const struct ksiginfo *, const sigset_t *, 109 int, struct lwp *, struct trapframe *, vaddr_t, int, vaddr_t); 110 111 const static sig_setupstack_t sig_setupstacks[] = { 112 #if defined(COMPAT_13) || defined(COMPAT_ULTRIX) 113 setupstack_oldsigcontext, /* 0 */ 114 setupstack_oldsigcontext, /* 1 */ 115 #else 116 0, /* 0 */ 117 0, /* 1 */ 118 #endif 119 #if defined(COMPAT_16) || defined(COMPAT_ULTRIX) 120 setupstack_sigcontext2, /* 2 */ 121 #else 122 0, /* 2 */ 123 #endif 124 setupstack_siginfo3, /* 3 */ 125 }; 126 127 /* 128 * Brief description of how sendsig() works: 129 * A struct sigcontext is allocated on the user stack. The relevant 130 * registers are saved in it. Below it is a struct trampframe constructed, it 131 * is actually an argument list for callg. The user 132 * stack pointer is put below all structs. 133 * 134 * The registers will contain when the signal handler is called: 135 * pc, psl - Obvious 136 * sp - An address below all structs 137 * fp - The address of the signal handler 138 * ap - The address to the callg frame 139 * 140 * The trampoline code will save r0-r5 before doing anything else. 141 */ 142 struct trampoline3 { 143 unsigned int narg; /* Argument count (== 3) */ 144 int sig; /* Signal number */ 145 vaddr_t sip; /* Pointer to siginfo_t */ 146 vaddr_t ucp; /* Pointer to ucontext_t */ 147 }; 148 149 static vaddr_t 150 setupstack_siginfo3(const struct ksiginfo *ksi, const sigset_t *mask, int vers, 151 struct lwp *l, struct trapframe *tf, vaddr_t sp, int onstack, 152 vaddr_t handler) 153 { 154 struct trampoline3 tramp; 155 struct proc * const p = l->l_proc; 156 ucontext_t uc; 157 bool error; 158 159 /* 160 * Arguments given to the signal handler. 161 */ 162 memset(&tramp, 0, sizeof(tramp)); 163 tramp.narg = 3; 164 tramp.sig = ksi->ksi_signo; 165 sp -= sizeof(uc); tramp.ucp = sp; 166 sp -= sizeof(siginfo_t); tramp.sip = sp; 167 sp -= sizeof(tramp); 168 169 /* Save register context. */ 170 memset(&uc, 0, sizeof(uc)); 171 uc.uc_flags = _UC_SIGMASK; 172 uc.uc_sigmask = *mask; 173 uc.uc_link = l->l_ctxlink; 174 uc.uc_flags |= (l->l_sigstk.ss_flags & SS_ONSTACK) 175 ? _UC_SETSTACK : _UC_CLRSTACK; 176 sendsig_reset(l, ksi->ksi_signo); 177 mutex_exit(p->p_lock); 178 cpu_getmcontext(l, &uc.uc_mcontext, &uc.uc_flags); 179 180 tf->tf_fp = handler; 181 182 /* Copy the context to the stack. */ 183 error = (copyout(&uc, (char *)tramp.ucp, sizeof(uc)) != 0 || 184 copyout(&ksi->ksi_info, (char *)tramp.sip, sizeof(ksi->ksi_info)) != 0 || 185 copyout(&tramp, (char *)sp, sizeof(tramp)) != 0); 186 187 mutex_enter(p->p_lock); 188 if (error) 189 sigexit(l, SIGILL); 190 191 return sp; 192 }; 193 194 void 195 sendsig_sighelper(const ksiginfo_t *ksi, const sigset_t *mask) 196 { 197 struct lwp * const l = curlwp; 198 struct proc * const p = l->l_proc; 199 struct trapframe * const tf = l->l_md.md_utf; 200 stack_t * const ss = &l->l_sigstk; 201 const struct sigact_sigdesc * const sd = 202 &p->p_sigacts->sa_sigdesc[ksi->ksi_signo]; 203 vaddr_t sp; 204 int onstack; 205 sig_setupstack_t setup; 206 207 /* Figure what stack we are running on. */ 208 onstack = (ss->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 && 209 (sd->sd_sigact.sa_flags & SA_ONSTACK) != 0; 210 sp = onstack ? ((vaddr_t)ss->ss_sp + ss->ss_size) : tf->tf_sp; 211 212 if (sd->sd_vers > __SIGTRAMP_SIGINFO_VERSION || 213 (setup = sig_setupstacks[sd->sd_vers]) == NULL) 214 goto nosupport; 215 216 sp = (*setup)(ksi, mask, sd->sd_vers, l, tf, sp, onstack, 217 (vaddr_t)sd->sd_sigact.sa_handler); 218 if (sp == 0) 219 goto nosupport; 220 221 if (sd->sd_vers == 0) 222 tf->tf_pc = (register_t)p->p_sigctx.ps_sigcode; 223 else 224 tf->tf_pc = (register_t)sd->sd_tramp; 225 226 tf->tf_psl = PSL_U | PSL_PREVU; 227 tf->tf_sp = sp; 228 tf->tf_ap = sp; 229 230 if (onstack) 231 ss->ss_flags |= SS_ONSTACK; 232 return; 233 234 nosupport: 235 /* Don't know what trampoline version; kill it. */ 236 printf("sendsig(sig %d): bad version %d\n", 237 ksi->ksi_signo, sd->sd_vers); 238 sigexit(l, SIGILL); 239 } 240