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