sig_machdep.c revision 1.26
1/* $NetBSD: sig_machdep.c,v 1.26 2006/10/24 16:53:01 he 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 <sys/cdefs.h> 35__KERNEL_RCSID(0, "$NetBSD: sig_machdep.c,v 1.26 2006/10/24 16:53:01 he Exp $"); 36 37#include "opt_compat_netbsd.h" 38#include "opt_ppcarch.h" 39#include "opt_altivec.h" 40 41#include <sys/param.h> 42#include <sys/mount.h> 43#include <sys/proc.h> 44#include <sys/sa.h> 45#include <sys/savar.h> 46#include <sys/syscallargs.h> 47#include <sys/systm.h> 48#include <sys/ucontext.h> 49#include <sys/user.h> 50 51#include <powerpc/fpu.h> 52#include <powerpc/altivec.h> 53 54/* 55 * Send a signal to process. 56 */ 57void 58sendsig(const ksiginfo_t *ksi, const sigset_t *mask) 59{ 60 struct lwp * const l = curlwp; 61 struct proc * const p = l->l_proc; 62 struct trapframe * const tf = trapframe(l); 63 struct sigaltstack *ss = &p->p_sigctx.ps_sigstk; 64 const struct sigact_sigdesc *sd = 65 &p->p_sigacts->sa_sigdesc[ksi->ksi_signo]; 66 ucontext_t uc; 67 vaddr_t sp, sip, ucp; 68 int onstack; 69 70 if (sd->sd_vers < 2) { 71#ifdef COMPAT_16 72 sendsig_sigcontext(ksi->ksi_signo, mask, KSI_TRAPCODE(ksi)); 73 return; 74#else 75 goto nosupport; 76#endif 77 } 78 79 /* Do we need to jump onto the signal stack? */ 80 onstack = (ss->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 && 81 (sd->sd_sigact.sa_flags & SA_ONSTACK) != 0; 82 83 /* Find top of stack. */ 84 sp = (onstack ? (vaddr_t)ss->ss_sp + ss->ss_size : tf->fixreg[1]); 85 sp &= ~(CALLFRAMELEN-1); 86 87 /* Allocate space for the ucontext. */ 88 sp -= sizeof(ucontext_t); 89 ucp = sp; 90 91 /* Allocate space for the siginfo. */ 92 sp -= sizeof(siginfo_t); 93 sip = sp; 94 95 sp &= ~(CALLFRAMELEN-1); 96 97 /* Save register context. */ 98 uc.uc_flags = _UC_SIGMASK; 99 uc.uc_sigmask = *mask; 100 uc.uc_link = NULL; 101 memset(&uc.uc_stack, 0, sizeof(uc.uc_stack)); 102 cpu_getmcontext(l, &uc.uc_mcontext, &uc.uc_flags); 103 104 /* 105 * Copy the siginfo and ucontext onto the user's stack. 106 */ 107 if (copyout(&ksi->ksi_info, (caddr_t)sip, sizeof(ksi->ksi_info)) != 0 || 108 copyout(&uc, (caddr_t)ucp, sizeof(uc)) != 0) { 109 /* 110 * Process has trashed its stack; give it an illegal 111 * instruction to halt it in its tracks. 112 */ 113 sigexit(l, SIGILL); 114 /* NOTREACHED */ 115 } 116 117 /* 118 * Build context to run handler in. Note the trampoline version 119 * numbers are coordinated with machine-dependent code in libc. 120 */ 121 switch (sd->sd_vers) { 122 case 2: /* siginfo sigtramp */ 123 tf->fixreg[1] = (register_t)sp - CALLFRAMELEN; 124 tf->fixreg[3] = (register_t)ksi->ksi_signo; 125 tf->fixreg[4] = (register_t)sip; 126 tf->fixreg[5] = (register_t)ucp; 127 /* Preserve ucp across call to signal function */ 128 tf->fixreg[30] = (register_t)ucp; 129 tf->lr = (register_t)sd->sd_tramp; 130 tf->srr0 = (register_t)sd->sd_sigact.sa_handler; 131 break; 132 133 default: 134 goto nosupport; 135 } 136 137 /* Remember that we're now on the signal stack. */ 138 if (onstack) 139 ss->ss_flags |= SS_ONSTACK; 140 return; 141 142 nosupport: 143 /* Don't know what trampoline version; kill it. */ 144 printf("sendsig_siginfo(sig %d): bad version %d\n", 145 ksi->ksi_signo, sd->sd_vers); 146 sigexit(l, SIGILL); 147 /* NOTREACHED */ 148} 149 150void 151cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flagp) 152{ 153 const struct trapframe *tf = trapframe(l); 154 __greg_t *gr = mcp->__gregs; 155#if defined(PPC_HAVE_FPU) || defined(ALTIVEC) 156 struct pcb *pcb = &l->l_addr->u_pcb; 157#endif 158 159 /* Save GPR context. */ 160 (void)memcpy(gr, &tf->fixreg, 32 * sizeof (gr[0])); /* GR0-31 */ 161 gr[_REG_CR] = tf->cr; 162 gr[_REG_LR] = tf->lr; 163 gr[_REG_PC] = tf->srr0; 164 gr[_REG_MSR] = tf->srr1 & PSL_USERSRR1; 165#ifdef PPC_HAVE_FPU 166 gr[_REG_MSR] |= pcb->pcb_flags & (PCB_FE0|PCB_FE1); 167#endif 168#ifdef ALTIVEC 169 gr[_REG_MSR] |= pcb->pcb_flags & PCB_ALTIVEC ? PSL_VEC : 0; 170#endif 171 gr[_REG_CTR] = tf->ctr; 172 gr[_REG_XER] = tf->xer; 173#ifdef PPC_OEA 174 gr[_REG_MQ] = tf->tf_xtra[TF_MQ]; 175#else 176 gr[_REG_MQ] = 0; 177#endif 178 *flagp |= _UC_CPU; 179 180#ifdef PPC_HAVE_FPU 181 /* Save FPR context, if any. */ 182 if ((pcb->pcb_flags & PCB_FPU) != 0) { 183 /* If we're the FPU owner, dump its context to the PCB first. */ 184 if (pcb->pcb_fpcpu) 185 save_fpu_lwp(l, FPU_SAVE); 186 (void)memcpy(mcp->__fpregs.__fpu_regs, pcb->pcb_fpu.fpreg, 187 sizeof (mcp->__fpregs.__fpu_regs)); 188 mcp->__fpregs.__fpu_fpscr = 189 ((int *)&pcb->pcb_fpu.fpscr)[_QUAD_LOWWORD]; 190 mcp->__fpregs.__fpu_valid = 1; 191 *flagp |= _UC_FPU; 192 } else 193#endif 194 memset(&mcp->__fpregs, 0, sizeof(mcp->__fpregs)); 195 196#ifdef ALTIVEC 197 /* Save AltiVec context, if any. */ 198 if ((pcb->pcb_flags & PCB_ALTIVEC) != 0) { 199 /* 200 * If we're the AltiVec owner, dump its context 201 * to the PCB first. 202 */ 203 if (pcb->pcb_veccpu) 204 save_vec_lwp(l, ALTIVEC_SAVE); 205 (void)memcpy(mcp->__vrf.__vrs, pcb->pcb_vr.vreg, 206 sizeof (mcp->__vrf.__vrs)); 207 mcp->__vrf.__vscr = pcb->pcb_vr.vscr; 208 mcp->__vrf.__vrsave = pcb->pcb_vr.vrsave; 209 *flagp |= _UC_POWERPC_VEC; 210 } else 211#endif 212 memset(&mcp->__vrf, 0, sizeof (mcp->__vrf)); 213} 214 215int 216cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags) 217{ 218 struct trapframe *tf = trapframe(l); 219 const __greg_t *gr = mcp->__gregs; 220#ifdef PPC_HAVE_FPU 221 struct pcb *pcb = &l->l_addr->u_pcb; 222#endif 223 224 /* Restore GPR context, if any. */ 225 if (flags & _UC_CPU) { 226#ifdef PPC_HAVE_FPU 227 /* 228 * Always save the FP exception mode in the PCB. 229 */ 230 pcb->pcb_flags &= ~(PCB_FE0|PCB_FE1); 231 pcb->pcb_flags |= gr[_REG_MSR] & (PCB_FE0|PCB_FE1); 232#endif 233 234 (void)memcpy(&tf->fixreg, gr, 32 * sizeof (gr[0])); 235 tf->cr = gr[_REG_CR]; 236 tf->lr = gr[_REG_LR]; 237 tf->srr0 = gr[_REG_PC]; 238 /* 239 * Accept all user-settable bits without complaint; 240 * userland should not need to know the machine-specific 241 * MSR value. 242 */ 243 tf->srr1 = (gr[_REG_MSR] & PSL_USERMOD) | PSL_USERSET; 244 tf->ctr = gr[_REG_CTR]; 245 tf->xer = gr[_REG_XER]; 246#ifdef PPC_OEA 247 tf->tf_xtra[TF_MQ] = gr[_REG_MQ]; 248#endif 249 } 250 251#ifdef PPC_HAVE_FPU /* Restore FPR context, if any. */ 252 if ((flags & _UC_FPU) && mcp->__fpregs.__fpu_valid != 0) { 253 /* we don't need to save the state, just drop it */ 254 save_fpu_lwp(l, FPU_DISCARD); 255 (void)memcpy(&pcb->pcb_fpu.fpreg, &mcp->__fpregs.__fpu_regs, 256 sizeof (pcb->pcb_fpu.fpreg)); 257 ((int *)&pcb->pcb_fpu.fpscr)[_QUAD_LOWWORD] = 258 mcp->__fpregs.__fpu_fpscr; 259 } 260#endif 261 262#ifdef ALTIVEC 263 /* Restore AltiVec context, if any. */ 264 if (flags & _UC_POWERPC_VEC) { 265 /* we don't need to save the state, just drop it */ 266 save_vec_lwp(l, ALTIVEC_DISCARD); 267 (void)memcpy(pcb->pcb_vr.vreg, &mcp->__vrf.__vrs, 268 sizeof (pcb->pcb_vr.vreg)); 269 pcb->pcb_vr.vscr = mcp->__vrf.__vscr; 270 pcb->pcb_vr.vrsave = mcp->__vrf.__vrsave; 271 } 272#endif 273 274 return (0); 275} 276