1 /* $NetBSD: compat_16_machdep.c,v 1.13 2008/11/21 06:57:35 he 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 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright 1996 The Board of Trustees of The Leland Stanford 35 * Junior University. All Rights Reserved. 36 * 37 * Permission to use, copy, modify, and distribute this 38 * software and its documentation for any purpose and without 39 * fee is hereby granted, provided that the above copyright 40 * notice appear in all copies. Stanford University 41 * makes no representations about the suitability of this 42 * software for any purpose. It is provided "as is" without 43 * express or implied warranty. 44 */ 45 46 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 47 48 __KERNEL_RCSID(0, "$NetBSD: compat_16_machdep.c,v 1.13 2008/11/21 06:57:35 he Exp $"); 49 50 #ifdef _KERNEL_OPT 51 #include "opt_cputype.h" 52 #include "opt_compat_netbsd.h" 53 #include "opt_compat_ultrix.h" 54 #endif 55 56 #include <sys/param.h> 57 #include <sys/systm.h> 58 #include <sys/kernel.h> 59 #include <sys/proc.h> 60 #include <sys/user.h> 61 #include <sys/signal.h> 62 #include <sys/signalvar.h> 63 #include <sys/mount.h> 64 #include <sys/syscallargs.h> 65 66 #include <compat/sys/signal.h> 67 #include <compat/sys/signalvar.h> 68 69 #include <machine/cpu.h> 70 71 #include <mips/regnum.h> 72 #include <mips/frame.h> 73 74 #ifdef DEBUG 75 int sigdebug = 0; 76 int sigpid = 0; 77 #define SDB_FOLLOW 0x01 78 #define SDB_KSTACK 0x02 79 #define SDB_FPSTATE 0x04 80 #endif 81 82 /* 83 * Send a signal to process. 84 */ 85 void 86 sendsig_sigcontext(const ksiginfo_t *ksi, const sigset_t *returnmask) 87 { 88 int sig = ksi->ksi_signo; 89 struct lwp *l = curlwp; 90 struct proc *p = l->l_proc; 91 struct sigacts *ps = p->p_sigacts; 92 int onstack, error; 93 struct sigcontext *scp = getframe(l, sig, &onstack), ksc; 94 struct frame *f; 95 sig_t catcher = SIGACTION(p, sig).sa_handler; 96 97 f = (struct frame *)l->l_md.md_regs; 98 99 scp--; 100 101 #ifdef DEBUG 102 if ((sigdebug & SDB_FOLLOW) || 103 ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)) 104 printf("sendsig(%d): sig %d ssp %p scp %p\n", 105 p->p_pid, sig, &onstack, scp); 106 #endif 107 108 /* Build stack frame for signal trampoline. */ 109 ksc.sc_pc = f->f_regs[_R_PC]; 110 ksc.mullo = f->f_regs[_R_MULLO]; 111 ksc.mulhi = f->f_regs[_R_MULHI]; 112 113 /* Save register context. */ 114 ksc.sc_regs[_R_ZERO] = 0xACEDBADE; /* magic number */ 115 memcpy(&ksc.sc_regs[1], &f->f_regs[1], 116 sizeof(ksc.sc_regs) - sizeof(ksc.sc_regs[0])); 117 118 /* Save the FP state, if necessary, then copy it. */ 119 #ifndef SOFTFLOAT 120 ksc.sc_fpused = l->l_md.md_flags & MDP_FPUSED; 121 if (ksc.sc_fpused) { 122 /* if FPU has current state, save it first */ 123 if (l == fpcurlwp) 124 savefpregs(l); 125 *(struct fpreg *)ksc.sc_fpregs = l->l_addr->u_pcb.pcb_fpregs; 126 } 127 #else 128 *(struct fpreg *)ksc.sc_fpregs = l->l_addr->u_pcb.pcb_fpregs; 129 #endif 130 131 /* Save signal stack. */ 132 ksc.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK; 133 134 /* Save signal mask. */ 135 ksc.sc_mask = *returnmask; 136 137 #if defined(COMPAT_13) || defined(COMPAT_ULTRIX) 138 /* 139 * XXX We always have to save an old style signal mask because 140 * XXX we might be delivering a signal to a process which will 141 * XXX escape from the signal in a non-standard way and invoke 142 * XXX sigreturn() directly. 143 */ 144 native_sigset_to_sigset13(returnmask, &ksc.__sc_mask13); 145 #endif 146 147 sendsig_reset(l, sig); 148 149 mutex_exit(p->p_lock); 150 error = copyout(&ksc, (void *)scp, sizeof(ksc)); 151 mutex_enter(p->p_lock); 152 153 if (error != 0) { 154 /* 155 * Process has trashed its stack; give it an illegal 156 * instruction to halt it in its tracks. 157 */ 158 #ifdef DEBUG 159 if ((sigdebug & SDB_FOLLOW) || 160 ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)) 161 printf("sendsig(%d): copyout failed on sig %d\n", 162 p->p_pid, sig); 163 #endif 164 sigexit(l, SIGILL); 165 /* NOTREACHED */ 166 } 167 168 /* 169 * Set up the registers to directly invoke the signal 170 * handler. The return address will be set up to point 171 * to the signal trampoline to bounce us back. 172 */ 173 f->f_regs[_R_A0] = sig; 174 f->f_regs[_R_A1] = ksi->ksi_trap; 175 f->f_regs[_R_A2] = (intptr_t)scp; 176 f->f_regs[_R_A3] = (intptr_t)catcher; /* XXX ??? */ 177 178 f->f_regs[_R_PC] = (intptr_t)catcher; 179 f->f_regs[_R_T9] = (intptr_t)catcher; 180 f->f_regs[_R_SP] = (intptr_t)scp; 181 182 switch (ps->sa_sigdesc[sig].sd_vers) { 183 case 0: /* legacy on-stack sigtramp */ 184 f->f_regs[_R_RA] = (intptr_t)p->p_sigctx.ps_sigcode; 185 break; 186 #ifdef COMPAT_16 187 case 1: 188 f->f_regs[_R_RA] = (intptr_t)ps->sa_sigdesc[sig].sd_tramp; 189 break; 190 #endif 191 default: 192 /* Don't know what trampoline version; kill it. */ 193 sigexit(l, SIGILL); 194 } 195 196 /* Remember that we're now on the signal stack. */ 197 if (onstack) 198 l->l_sigstk.ss_flags |= SS_ONSTACK; 199 200 #ifdef DEBUG 201 if ((sigdebug & SDB_FOLLOW) || 202 ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)) 203 printf("sendsig(%d): sig %d returns\n", 204 p->p_pid, sig); 205 #endif 206 } 207 208 #ifdef COMPAT_16 /* not needed if COMPAT_ULTRIX only */ 209 /* 210 * System call to cleanup state after a signal 211 * has been taken. Reset signal mask and 212 * stack state from context left by sendsig (above). 213 * Return to previous pc and psl as specified by 214 * context left by sendsig. Check carefully to 215 * make sure that the user has not modified the 216 * psl to gain improper privileges or to cause 217 * a machine fault. 218 */ 219 /* ARGSUSED */ 220 int 221 compat_16_sys___sigreturn14(struct lwp *l, const struct compat_16_sys___sigreturn14_args *uap, register_t *retval) 222 { 223 /* { 224 syscallarg(struct sigcontext *) sigcntxp; 225 } */ 226 struct sigcontext *scp, ksc; 227 struct frame *f; 228 struct proc *p = l->l_proc; 229 int error; 230 231 /* 232 * The trampoline code hands us the context. 233 * It is unsafe to keep track of it ourselves, in the event that a 234 * program jumps out of a signal handler. 235 */ 236 scp = SCARG(uap, sigcntxp); 237 #ifdef DEBUG 238 if (sigdebug & SDB_FOLLOW) 239 printf("sigreturn: pid %d, scp %p\n", l->l_proc->p_pid, scp); 240 #endif 241 if ((error = copyin(scp, &ksc, sizeof(ksc))) != 0) 242 return (error); 243 244 if ((u_int) ksc.sc_regs[_R_ZERO] != 0xacedbadeU)/* magic number */ 245 return (EINVAL); 246 247 /* Restore the register context. */ 248 f = (struct frame *)l->l_md.md_regs; 249 f->f_regs[_R_PC] = ksc.sc_pc; 250 f->f_regs[_R_MULLO] = ksc.mullo; 251 f->f_regs[_R_MULHI] = ksc.mulhi; 252 memcpy(&f->f_regs[1], &scp->sc_regs[1], 253 sizeof(scp->sc_regs) - sizeof(scp->sc_regs[0])); 254 #ifndef SOFTFLOAT 255 if (scp->sc_fpused) { 256 /* Disable the FPU to fault in FP registers. */ 257 f->f_regs[_R_SR] &= ~MIPS_SR_COP_1_BIT; 258 if (l == fpcurlwp) 259 fpcurlwp = NULL; 260 l->l_addr->u_pcb.pcb_fpregs = *(struct fpreg *)scp->sc_fpregs; 261 } 262 #else 263 l->l_addr->u_pcb.pcb_fpregs = *(struct fpreg *)scp->sc_fpregs; 264 #endif 265 266 mutex_enter(p->p_lock); 267 /* Restore signal stack. */ 268 if (ksc.sc_onstack & SS_ONSTACK) 269 l->l_sigstk.ss_flags |= SS_ONSTACK; 270 else 271 l->l_sigstk.ss_flags &= ~SS_ONSTACK; 272 /* Restore signal mask. */ 273 (void) sigprocmask1(l, SIG_SETMASK, &ksc.sc_mask, 0); 274 mutex_exit(p->p_lock); 275 276 return (EJUSTRETURN); 277 } 278 #endif /* COMPAT_16 */ 279