1 /* $NetBSD: sig_machdep.c,v 1.28 2021/10/27 04:14:59 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2002 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Matthew Fredette. 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 /* 33 * Copyright (c) 1988 University of Utah. 34 * Copyright (c) 1982, 1986, 1990, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * This code is derived from software contributed to Berkeley by 38 * the Systems Programming Group of the University of Utah Computer 39 * Science Department. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 3. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * from: Utah Hdr: machdep.c 1.74 92/12/20 66 * from: @(#)machdep.c 8.10 (Berkeley) 4/20/94 67 */ 68 69 #include <sys/cdefs.h> 70 __KERNEL_RCSID(0, "$NetBSD: sig_machdep.c,v 1.28 2021/10/27 04:14:59 thorpej Exp $"); 71 72 #include "opt_compat_netbsd.h" 73 74 #include <sys/param.h> 75 #include <sys/systm.h> 76 #include <sys/kernel.h> 77 #include <sys/proc.h> 78 #include <sys/signal.h> 79 #include <sys/signalvar.h> 80 81 #include <sys/mount.h> 82 #include <sys/syscallargs.h> 83 84 #include <machine/cpu.h> 85 #include <machine/pcb.h> 86 #include <machine/reg.h> 87 #include <machine/frame.h> 88 89 #ifdef DEBUG 90 int sigdebug = 0; 91 int sigpid = 0; 92 #define SDB_FOLLOW 0x01 93 #define SDB_KSTACK 0x02 94 #define SDB_FPSTATE 0x04 95 #endif 96 97 static void *getframe(struct lwp *, int, int *); 98 static void * 99 getframe(struct lwp *l, int sig, int *onstack) 100 { 101 struct proc *p = l->l_proc; 102 struct trapframe *tf = l->l_md.md_regs; 103 104 /* Do we need to jump onto the signal stack? */ 105 *onstack = (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 106 && (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0; 107 if (*onstack) 108 return (void *)HPPA_FRAME_ROUND(l->l_sigstk.ss_sp); 109 else 110 return (void *)HPPA_FRAME_ROUND(tf->tf_sp); 111 } 112 113 struct sigframe_siginfo { 114 siginfo_t sf_si; 115 ucontext_t sf_uc; 116 }; 117 118 void 119 sendsig_siginfo(const struct ksiginfo *ksi, const sigset_t *mask) 120 { 121 struct lwp *l = curlwp; 122 struct proc *p = l->l_proc; 123 struct pcb *pcb = lwp_getpcb(l); 124 struct sigacts *ps = p->p_sigacts; 125 struct sigframe_siginfo *fp, frame; 126 struct trapframe *tf; 127 int sig = ksi->ksi_signo; 128 sig_t catcher = SIGACTION(p, sig).sa_handler; 129 int onstack, error; 130 131 fp = getframe(l, sig, &onstack); 132 tf = (struct trapframe *)l->l_md.md_regs; 133 134 /* Build stack frame for signal trampoline. */ 135 switch (ps->sa_sigdesc[sig].sd_vers) { 136 default: 137 printf("sendsig_siginfo: bad version %d\n", 138 ps->sa_sigdesc[sig].sd_vers); 139 sigexit(l, SIGILL); 140 case __SIGTRAMP_SIGINFO_VERSION: 141 break; 142 } 143 144 memset(&frame, 0, sizeof(frame)); 145 frame.sf_si._info = ksi->ksi_info; 146 frame.sf_uc.uc_flags = _UC_SIGMASK | 147 ((l->l_sigstk.ss_flags & SS_ONSTACK) ? 148 _UC_SETSTACK : _UC_CLRSTACK); 149 frame.sf_uc.uc_sigmask = *mask; 150 frame.sf_uc.uc_link = l->l_ctxlink; 151 sendsig_reset(l, sig); 152 mutex_exit(p->p_lock); 153 cpu_getmcontext(l, &frame.sf_uc.uc_mcontext, &frame.sf_uc.uc_flags); 154 error = copyout(&frame, fp, sizeof(frame)); 155 mutex_enter(p->p_lock); 156 157 if (error != 0) { 158 159 /* 160 * Process has trashed its stack; give it an illegal 161 * instruction to halt it in its tracks. 162 */ 163 sigexit(l, SIGILL); 164 /* NOTREACHED */ 165 } 166 167 /* 168 * Set up the registers to invoke the signal trampoline. 169 */ 170 tf->tf_arg0 = sig; 171 tf->tf_arg1 = (__greg_t)&fp->sf_si; 172 tf->tf_arg2 = (__greg_t)&fp->sf_uc; 173 tf->tf_r3 = (__greg_t)&fp->sf_uc; 174 175 fp++; 176 tf->tf_iisq_head = tf->tf_iisq_tail = pcb->pcb_space; 177 tf->tf_iioq_head = 178 (__greg_t)ps->sa_sigdesc[sig].sd_tramp | HPPA_PC_PRIV_USER; 179 tf->tf_iioq_tail = tf->tf_iioq_head + 4; 180 tf->tf_arg3 = (__greg_t)catcher; 181 tf->tf_sp = HPPA_FRAME_ROUND(fp); 182 183 /* Remember that we're now on the signal stack. */ 184 if (onstack) 185 l->l_sigstk.ss_flags |= SS_ONSTACK; 186 } 187