sig_machdep.c revision 1.11
1/* $NetBSD: sig_machdep.c,v 1.11 2003/02/03 21:48:02 matt Exp $ */ 2 3/* 4 * Copyright (C) 1995, 1996 Wolfgang Solfrank. 5 * Copyright (C) 1995, 1996 TooLs GmbH. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by TooLs GmbH. 19 * 4. The name of TooLs GmbH may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34#include "opt_compat_netbsd.h" 35#include "opt_ppcarch.h" 36 37#include <sys/param.h> 38#include <sys/mount.h> 39#include <sys/proc.h> 40#include <sys/sa.h> 41#include <sys/savar.h> 42#include <sys/syscallargs.h> 43#include <sys/systm.h> 44#include <sys/ucontext.h> 45#include <sys/user.h> 46 47#include <machine/fpu.h> 48 49/* 50 * Send a signal to process. 51 */ 52void 53sendsig(sig, mask, code) 54 int sig; 55 sigset_t *mask; 56 u_long code; 57{ 58 struct lwp *l = curlwp; 59 struct proc *p = l->l_proc; 60 struct sigacts *ps = p->p_sigacts; 61 struct sigframe *fp, frame; 62 struct trapframe *tf; 63 struct utrapframe *utf = &frame.sf_sc.sc_frame; 64 int onstack; 65 sig_t catcher = SIGACTION(p, sig).sa_handler; 66 67 tf = trapframe(l); 68 69 /* Do we need to jump onto the signal stack? */ 70 onstack = 71 (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 && 72 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0; 73 74 /* Allocate space for the signal handler context. */ 75 if (onstack) 76 fp = (struct sigframe *)((caddr_t)p->p_sigctx.ps_sigstk.ss_sp + 77 p->p_sigctx.ps_sigstk.ss_size); 78 else 79 fp = (struct sigframe *)tf->fixreg[1]; 80 fp = (struct sigframe *)((uintptr_t)(fp - 1) & ~0xf); 81 82 /* Save register context. */ 83 memcpy(utf->fixreg, tf->fixreg, sizeof(utf->fixreg)); 84 utf->lr = tf->lr; 85 utf->cr = tf->cr; 86 utf->xer = tf->xer; 87 utf->ctr = tf->ctr; 88 utf->srr0 = tf->srr0; 89 utf->srr1 = tf->srr1; 90#ifdef PPC_OEA 91 utf->vrsave = tf->tf_xtra[TF_VRSAVE]; 92 utf->mq = tf->tf_xtra[TF_MQ]; 93#endif 94 95 /* Save signal stack. */ 96 frame.sf_sc.sc_onstack = p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK; 97 98 /* Save signal mask. */ 99 frame.sf_sc.sc_mask = *mask; 100 101#ifdef COMPAT_13 102 /* 103 * XXX We always have to save an old style signal mask because 104 * XXX we might be delivering a signal to a process which will 105 * XXX escape from the signal in a non-standard way and invoke 106 * XXX sigreturn() directly. 107 */ 108 native_sigset_to_sigset13(mask, &frame.sf_sc.__sc_mask13); 109#endif 110 111 if (copyout(&frame, fp, sizeof frame) != 0) { 112 /* 113 * Process has trashed its stack; give it an illegal 114 * instructoin to halt it in its tracks. 115 */ 116 sigexit(l, SIGILL); 117 /* NOTREACHED */ 118 } 119 120 /* 121 * Build context to run handler in. Note the trampoline version 122 * numbers are coordinated with machine-dependent code in libc. 123 */ 124 switch (ps->sa_sigdesc[sig].sd_vers) { 125#if 1 /* COMPAT_16 */ 126 case 0: /* legacy on-stack sigtramp */ 127 tf->fixreg[1] = (register_t)fp; 128 tf->lr = (register_t)catcher; 129 tf->fixreg[3] = (register_t)sig; 130 tf->fixreg[4] = (register_t)code; 131 tf->fixreg[5] = (register_t)&fp->sf_sc; 132 tf->srr0 = (register_t)p->p_sigctx.ps_sigcode; 133 break; 134#endif /* COMPAT_16 */ 135 136 case 1: 137 tf->fixreg[1] = (register_t)fp; 138 tf->lr = (register_t)catcher; 139 tf->fixreg[3] = (register_t)sig; 140 tf->fixreg[4] = (register_t)code; 141 tf->fixreg[5] = (register_t)&fp->sf_sc; 142 tf->srr0 = (register_t)ps->sa_sigdesc[sig].sd_tramp; 143 break; 144 145 default: 146 /* Don't know what trampoline version; kill it. */ 147 sigexit(l, SIGILL); 148 } 149 150 /* Remember that we're now on the signal stack. */ 151 if (onstack) 152 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK; 153} 154 155/* 156 * System call to cleanup state after a signal handler returns. 157 */ 158int 159sys___sigreturn14(l, v, retval) 160 struct lwp *l; 161 void *v; 162 register_t *retval; 163{ 164 struct sys___sigreturn14_args /* { 165 syscallarg(struct sigcontext *) sigcntxp; 166 } */ *uap = v; 167 struct proc *p = l->l_proc; 168 struct sigcontext sc; 169 struct trapframe *tf; 170 struct utrapframe * const utf = &sc.sc_frame; 171 int error; 172 173 /* 174 * The trampoline hands us the context. 175 * It is unsafe to keep track of it ourselves, in the event that a 176 * program jumps out of a signal hander. 177 */ 178 if ((error = copyin(SCARG(uap, sigcntxp), &sc, sizeof sc)) != 0) 179 return (error); 180 181 /* Restore the register context. */ 182 tf = trapframe(l); 183 if ((sc.sc_frame.srr1 & PSL_USERSTATIC) != (tf->srr1 & PSL_USERSTATIC)) 184 return (EINVAL); 185 186 /* Restore register context. */ 187 memcpy(tf->fixreg, utf->fixreg, sizeof(tf->fixreg)); 188 tf->lr = utf->lr; 189 tf->cr = utf->cr; 190 tf->xer = utf->xer; 191 tf->ctr = utf->ctr; 192 tf->srr0 = utf->srr0; 193 tf->srr1 = utf->srr1; 194#ifdef PPC_OEA 195 tf->tf_xtra[TF_VRSAVE] = utf->vrsave; 196 tf->tf_xtra[TF_MQ] = utf->mq; 197#endif 198 199 /* Restore signal stack. */ 200 if (sc.sc_onstack & SS_ONSTACK) 201 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK; 202 else 203 p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK; 204 205 /* Restore signal mask. */ 206 (void) sigprocmask1(p, SIG_SETMASK, &sc.sc_mask, 0); 207 208 return (EJUSTRETURN); 209} 210 211void 212cpu_getmcontext(l, mcp, flagp) 213 struct lwp *l; 214 mcontext_t *mcp; 215 unsigned int *flagp; 216{ 217 const struct trapframe *tf = trapframe(l); 218 __greg_t *gr = mcp->__gregs; 219#ifdef PPC_HAVE_FPU 220 struct pcb *pcb = &l->l_addr->u_pcb; 221#endif 222 223 /* Save GPR context. */ 224 (void)memcpy(gr, &tf->fixreg, 32 * sizeof (gr[0])); /* GR0-31 */ 225 gr[_REG_CR] = tf->cr; 226 gr[_REG_LR] = tf->lr; 227 gr[_REG_PC] = tf->srr0; 228 gr[_REG_MSR] = tf->srr1; 229 gr[_REG_CTR] = tf->ctr; 230 gr[_REG_XER] = tf->xer; 231#ifdef PPC_OEA 232 gr[_REG_MQ] = tf->tf_xtra[TF_MQ]; 233#else 234 gr[_REG_MQ] = 0; 235#endif 236 *flagp |= _UC_CPU; 237 238#ifdef PPC_HAVE_FPU 239 /* Save FPR context, if any. */ 240 if ((pcb->pcb_flags & PCB_FPU) != 0) { 241 /* If we're the FPU owner, dump its context to the PCB first. */ 242 if (pcb->pcb_fpcpu) 243 save_fpu_lwp(l); 244 (void)memcpy(mcp->__fpregs.__fpu_regs, pcb->pcb_fpu.fpr, 245 sizeof (mcp->__fpregs.__fpu_regs)); 246 mcp->__fpregs.__fpu_fpscr = 247 ((int *)&pcb->pcb_fpu.fpscr)[_QUAD_LOWWORD]; 248 mcp->__fpregs.__fpu_valid = 1; 249 *flagp |= _UC_FPU; 250 } else 251#endif 252 mcp->__fpregs.__fpu_valid = 0; 253 254 /* No AltiVec support, for now. */ 255 memset(&mcp->__vrf, 0, sizeof (mcp->__vrf)); 256} 257 258int 259cpu_setmcontext(l, mcp, flags) 260 struct lwp *l; 261 const mcontext_t *mcp; 262 unsigned int flags; 263{ 264 struct trapframe *tf = trapframe(l); 265 __greg_t *gr = mcp->__gregs; 266#ifdef PPC_HAVE_FPU 267 struct pcb *pcb = &l->l_addr->u_pcb; 268#endif 269 270 /* Restore GPR context, if any. */ 271 if (flags & _UC_CPU) { 272 if ((gr[_REG_MSR] & PSL_USERSTATIC) != 273 (tf->srr1 & PSL_USERSTATIC)) 274 return (EINVAL); 275 276 (void)memcpy(&tf->fixreg, gr, 32 * sizeof (gr[0])); 277 tf->cr = gr[_REG_CR]; 278 tf->lr = gr[_REG_LR]; 279 tf->srr0 = gr[_REG_PC]; 280 tf->srr1 = gr[_REG_MSR]; 281 tf->ctr = gr[_REG_CTR]; 282 tf->xer = gr[_REG_XER]; 283#ifdef PPC_OEA 284 tf->tf_xtra[TF_MQ] = gr[_REG_MQ]; 285#endif 286 } 287 288#ifdef PPC_HAVE_FPU 289 /* Restore FPR context, if any. */ 290 if ((flags & _UC_FPU) && mcp->__fpregs.__fpu_valid != 0) { 291 /* XXX we don't need to save the state, just to drop it */ 292 save_fpu_lwp(l); 293 (void)memcpy(&pcb->pcb_fpu.fpr, &mcp->__fpregs.__fpu_regs, 294 sizeof (pcb->pcb_fpu.fpr)); 295 pcb->pcb_fpu.fpscr = *(double *)&mcp->__fpregs.__fpu_fpscr; 296 } 297#endif 298 299 return (0); 300} 301