1 /* $NetBSD: sig_machdep.c,v 1.3 2002/11/10 11:01:15 simonb Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center and by Chris Demetriou. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Copyright 1996 The Board of Trustees of The Leland Stanford 42 * Junior University. All Rights Reserved. 43 * 44 * Permission to use, copy, modify, and distribute this 45 * software and its documentation for any purpose and without 46 * fee is hereby granted, provided that the above copyright 47 * notice appear in all copies. Stanford University 48 * makes no representations about the suitability of this 49 * software for any purpose. It is provided "as is" without 50 * express or implied warranty. 51 */ 52 53 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 54 55 __KERNEL_RCSID(0, "$NetBSD: sig_machdep.c,v 1.3 2002/11/10 11:01:15 simonb Exp $"); 56 57 #include "opt_cputype.h" 58 #include "opt_compat_netbsd.h" 59 #include "opt_compat_ultrix.h" 60 61 #include <sys/param.h> 62 #include <sys/systm.h> 63 #include <sys/kernel.h> 64 #include <sys/proc.h> 65 #include <sys/user.h> 66 #include <sys/signal.h> 67 #include <sys/signalvar.h> 68 #include <sys/mount.h> 69 #include <sys/syscallargs.h> 70 71 #include <machine/cpu.h> 72 73 #include <mips/regnum.h> 74 75 #ifdef DEBUG 76 int sigdebug = 0; 77 int sigpid = 0; 78 #define SDB_FOLLOW 0x01 79 #define SDB_KSTACK 0x02 80 #define SDB_FPSTATE 0x04 81 #endif 82 83 /* 84 * Send a signal to process. 85 */ 86 void 87 sendsig(int sig, sigset_t *returnmask, u_long code) 88 { 89 struct proc *p = curproc; 90 struct sigacts *ps = p->p_sigacts; 91 struct sigcontext *scp, ksc; 92 struct frame *f; 93 int onstack; 94 sig_t catcher = SIGACTION(p, sig).sa_handler; 95 96 f = (struct frame *)p->p_md.md_regs; 97 98 /* Do we need to jump onto the signal stack? */ 99 onstack = 100 (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 && 101 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0; 102 103 /* Allocate space for the signal handler context. */ 104 if (onstack) 105 scp = (struct sigcontext *)((caddr_t)p->p_sigctx.ps_sigstk.ss_sp 106 + p->p_sigctx.ps_sigstk.ss_size); 107 else 108 /* cast for _MIPS_BSD_API == _MIPS_BSD_API_LP32_64CLEAN case */ 109 scp = (struct sigcontext *)(u_int32_t)f->f_regs[SP]; 110 scp--; 111 112 #ifdef DEBUG 113 if ((sigdebug & SDB_FOLLOW) || 114 ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)) 115 printf("sendsig(%d): sig %d ssp %p scp %p\n", 116 p->p_pid, sig, &onstack, scp); 117 #endif 118 119 /* Build stack frame for signal trampoline. */ 120 ksc.sc_pc = f->f_regs[PC]; 121 ksc.mullo = f->f_regs[MULLO]; 122 ksc.mulhi = f->f_regs[MULHI]; 123 124 /* Save register context. */ 125 ksc.sc_regs[ZERO] = 0xACEDBADE; /* magic number */ 126 memcpy(&ksc.sc_regs[1], &f->f_regs[1], 127 sizeof(ksc.sc_regs) - sizeof(ksc.sc_regs[0])); 128 129 /* Save the FP state, if necessary, then copy it. */ 130 #ifndef SOFTFLOAT 131 ksc.sc_fpused = p->p_md.md_flags & MDP_FPUSED; 132 if (ksc.sc_fpused) { 133 /* if FPU has current state, save it first */ 134 if (p == fpcurproc) 135 savefpregs(p); 136 *(struct fpreg *)ksc.sc_fpregs = p->p_addr->u_pcb.pcb_fpregs; 137 } 138 #else 139 *(struct fpreg *)ksc.sc_fpregs = p->p_addr->u_pcb.pcb_fpregs; 140 #endif 141 142 /* Save signal stack. */ 143 ksc.sc_onstack = p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK; 144 145 /* Save signal mask. */ 146 ksc.sc_mask = *returnmask; 147 148 #if defined(COMPAT_13) || defined(COMPAT_ULTRIX) 149 /* 150 * XXX We always have to save an old style signal mask because 151 * XXX we might be delivering a signal to a process which will 152 * XXX escape from the signal in a non-standard way and invoke 153 * XXX sigreturn() directly. 154 */ 155 native_sigset_to_sigset13(returnmask, &ksc.__sc_mask13); 156 #endif 157 158 if (copyout(&ksc, (caddr_t)scp, sizeof(ksc))) { 159 /* 160 * Process has trashed its stack; give it an illegal 161 * instruction to halt it in its tracks. 162 */ 163 #ifdef DEBUG 164 if ((sigdebug & SDB_FOLLOW) || 165 ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)) 166 printf("sendsig(%d): copyout failed on sig %d\n", 167 p->p_pid, sig); 168 #endif 169 sigexit(p, SIGILL); 170 /* NOTREACHED */ 171 } 172 173 /* 174 * Set up the registers to directly invoke the signal 175 * handler. The return address will be set up to point 176 * to the signal trampoline to bounce us back. 177 */ 178 f->f_regs[A0] = sig; 179 f->f_regs[A1] = code; 180 f->f_regs[A2] = (int)scp; 181 f->f_regs[A3] = (int)catcher; /* XXX ??? */ 182 183 f->f_regs[PC] = (int)catcher; 184 f->f_regs[T9] = (int)catcher; 185 f->f_regs[SP] = (int)scp; 186 187 switch (ps->sa_sigdesc[sig].sd_vers) { 188 #if 1 /* COMPAT_16 */ 189 case 0: /* legacy on-stack sigtramp */ 190 f->f_regs[RA] = (int)p->p_sigctx.ps_sigcode; 191 break; 192 #endif /* COMPAT_16 */ 193 194 case 1: 195 f->f_regs[RA] = (int)ps->sa_sigdesc[sig].sd_tramp; 196 break; 197 198 default: 199 /* Don't know what trampoline version; kill it. */ 200 sigexit(p, SIGILL); 201 } 202 203 /* Remember that we're now on the signal stack. */ 204 if (onstack) 205 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK; 206 207 #ifdef DEBUG 208 if ((sigdebug & SDB_FOLLOW) || 209 ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)) 210 printf("sendsig(%d): sig %d returns\n", 211 p->p_pid, sig); 212 #endif 213 } 214 215 /* 216 * System call to cleanup state after a signal 217 * has been taken. Reset signal mask and 218 * stack state from context left by sendsig (above). 219 * Return to previous pc and psl as specified by 220 * context left by sendsig. Check carefully to 221 * make sure that the user has not modified the 222 * psl to gain improper privileges or to cause 223 * a machine fault. 224 */ 225 /* ARGSUSED */ 226 int 227 sys___sigreturn14(struct proc *p, void *v, register_t *retval) 228 { 229 struct sys___sigreturn14_args /* { 230 syscallarg(struct sigcontext *) sigcntxp; 231 } */ *uap = v; 232 struct sigcontext *scp, ksc; 233 struct frame *f; 234 int error; 235 236 /* 237 * The trampoline code hands us the context. 238 * It is unsafe to keep track of it ourselves, in the event that a 239 * program jumps out of a signal handler. 240 */ 241 scp = SCARG(uap, sigcntxp); 242 #ifdef DEBUG 243 if (sigdebug & SDB_FOLLOW) 244 printf("sigreturn: pid %d, scp %p\n", p->p_pid, scp); 245 #endif 246 if ((error = copyin(scp, &ksc, sizeof(ksc))) != 0) 247 return (error); 248 249 if ((u_int) ksc.sc_regs[ZERO] != 0xacedbadeU) /* magic number */ 250 return (EINVAL); 251 252 /* Restore the register context. */ 253 f = (struct frame *)p->p_md.md_regs; 254 f->f_regs[PC] = ksc.sc_pc; 255 f->f_regs[MULLO] = ksc.mullo; 256 f->f_regs[MULHI] = ksc.mulhi; 257 memcpy(&f->f_regs[1], &scp->sc_regs[1], 258 sizeof(scp->sc_regs) - sizeof(scp->sc_regs[0])); 259 #ifndef SOFTFLOAT 260 if (scp->sc_fpused) { 261 /* Disable the FPU to fault in FP registers. */ 262 f->f_regs[SR] &= ~MIPS_SR_COP_1_BIT; 263 if (p == fpcurproc) { 264 fpcurproc = (struct proc *)0; 265 } 266 p->p_addr->u_pcb.pcb_fpregs = *(struct fpreg *)scp->sc_fpregs; 267 } 268 #else 269 p->p_addr->u_pcb.pcb_fpregs = *(struct fpreg *)scp->sc_fpregs; 270 #endif 271 272 /* Restore signal stack. */ 273 if (ksc.sc_onstack & SS_ONSTACK) 274 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK; 275 else 276 p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK; 277 278 /* Restore signal mask. */ 279 (void) sigprocmask1(p, SIG_SETMASK, &ksc.sc_mask, 0); 280 281 return (EJUSTRETURN); 282 } 283