sig_machdep.c revision 1.39
1/* $NetBSD: sig_machdep.c,v 1.39 2011/02/07 09:39:48 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 <sys/cdefs.h> 35__KERNEL_RCSID(0, "$NetBSD: sig_machdep.c,v 1.39 2011/02/07 09:39:48 matt Exp $"); 36 37#include "opt_ppcarch.h" 38#include "opt_altivec.h" 39 40#include <sys/param.h> 41#include <sys/mount.h> 42#include <sys/proc.h> 43#include <sys/syscallargs.h> 44#include <sys/systm.h> 45#include <sys/ucontext.h> 46 47#include <uvm/uvm_extern.h> 48 49#include <powerpc/fpu.h> 50#include <powerpc/altivec.h> 51#include <powerpc/pcb.h> 52 53/* 54 * Send a signal to process. 55 */ 56void 57sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask) 58{ 59 struct lwp * const l = curlwp; 60 struct proc * const p = l->l_proc; 61 struct trapframe * const tf = l->l_md.md_utf; 62 struct sigaltstack * const ss = &l->l_sigstk; 63 const struct sigact_sigdesc * const sd = 64 &p->p_sigacts->sa_sigdesc[ksi->ksi_signo]; 65 /* save handler before sendsig_reset trashes it! */ 66 const void * const handler = sd->sd_sigact.sa_handler; 67 ucontext_t uc; 68 vaddr_t sp, sip, ucp; 69 int onstack, error; 70 71 /* Do we need to jump onto the signal stack? */ 72 onstack = (ss->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 && 73 (sd->sd_sigact.sa_flags & SA_ONSTACK) != 0; 74 75 /* Find top of stack. */ 76 sp = (onstack ? (vaddr_t)ss->ss_sp + ss->ss_size : tf->tf_fixreg[1]); 77 sp &= ~(CALLFRAMELEN-1); 78 79 /* Allocate space for the ucontext. */ 80 sp -= sizeof(ucontext_t); 81 ucp = sp; 82 83 /* Allocate space for the siginfo. */ 84 sp -= sizeof(siginfo_t); 85 sip = sp; 86 87 sp &= ~(CALLFRAMELEN-1); 88 89 /* Save register context. */ 90 uc.uc_flags = _UC_SIGMASK; 91 uc.uc_sigmask = *mask; 92 uc.uc_link = l->l_ctxlink; 93 memset(&uc.uc_stack, 0, sizeof(uc.uc_stack)); 94 sendsig_reset(l, ksi->ksi_signo); 95 mutex_exit(p->p_lock); 96 cpu_getmcontext(l, &uc.uc_mcontext, &uc.uc_flags); 97 98 /* 99 * Copy the siginfo and ucontext onto the user's stack. 100 */ 101 error = (copyout(&ksi->ksi_info, (void *)sip, sizeof(ksi->ksi_info)) != 0 || 102 copyout(&uc, (void *)ucp, sizeof(uc)) != 0); 103 mutex_enter(p->p_lock); 104 105 if (error) { 106 /* 107 * Process has trashed its stack; give it an illegal 108 * instruction to halt it in its tracks. 109 */ 110 sigexit(l, SIGILL); 111 /* NOTREACHED */ 112 } 113 114 /* 115 * Build context to run handler in. Note the trampoline version 116 * numbers are coordinated with machine-dependent code in libc. 117 */ 118 switch (sd->sd_vers) { 119 case 2: /* siginfo sigtramp */ 120 tf->tf_fixreg[1] = (register_t)sp - CALLFRAMELEN; 121 tf->tf_fixreg[3] = (register_t)ksi->ksi_signo; 122 tf->tf_fixreg[4] = (register_t)sip; 123 tf->tf_fixreg[5] = (register_t)ucp; 124 /* Preserve ucp across call to signal function */ 125 tf->tf_fixreg[30] = (register_t)ucp; 126 tf->tf_lr = (register_t)sd->sd_tramp; 127 tf->tf_srr0 = (register_t)handler; 128 break; 129 130 default: 131 goto nosupport; 132 } 133 134 /* Remember that we're now on the signal stack. */ 135 if (onstack) 136 ss->ss_flags |= SS_ONSTACK; 137 return; 138 139 nosupport: 140 /* Don't know what trampoline version; kill it. */ 141 printf("sendsig_siginfo(sig %d): bad version %d\n", 142 ksi->ksi_signo, sd->sd_vers); 143 sigexit(l, SIGILL); 144 /* NOTREACHED */ 145} 146 147void 148cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flagp) 149{ 150 const struct trapframe * const tf = trapframe(l); 151 __greg_t * const gr = mcp->__gregs; 152#if defined(PPC_HAVE_FPU) 153 struct pcb * const pcb = lwp_getpcb(l); 154#endif 155 156 /* Save GPR context. */ 157 (void)memcpy(gr, &tf->tf_fixreg, 32 * sizeof (gr[0])); /* GR0-31 */ 158 gr[_REG_CR] = tf->tf_cr; 159 gr[_REG_LR] = tf->tf_lr; 160 gr[_REG_PC] = tf->tf_srr0; 161 gr[_REG_MSR] = tf->tf_srr1 & PSL_USERSRR1; 162#ifdef PPC_HAVE_FPU 163 gr[_REG_MSR] |= pcb->pcb_flags & (PCB_FE0|PCB_FE1); 164#endif 165 gr[_REG_CTR] = tf->tf_ctr; 166 gr[_REG_XER] = tf->tf_xer; 167#ifdef PPC_OEA 168 gr[_REG_MQ] = tf->tf_mq; 169#else 170 gr[_REG_MQ] = 0; 171#endif 172 173 *flagp |= _UC_CPU; 174 175#ifdef PPC_HAVE_FPU 176 /* Save FPU context, if any. */ 177 if (!fpu_save_to_mcontext(l, mcp, flagp)) 178#endif 179 memset(&mcp->__fpregs, 0, sizeof(mcp->__fpregs)); 180 181#if defined(ALTIVEC) || defined(PPC_HAVE_SPE) 182 /* Save vector context, if any. */ 183 if (!vec_save_to_mcontext(l, mcp, flagp)) 184#endif 185 memset(&mcp->__vrf, 0, sizeof (mcp->__vrf)); 186} 187 188int 189cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags) 190{ 191 struct trapframe * const tf = trapframe(l); 192 const __greg_t * const gr = mcp->__gregs; 193 194 /* Restore GPR context, if any. */ 195 if (flags & _UC_CPU) { 196#ifdef PPC_HAVE_FPU 197 /* 198 * Always save the FP exception mode in the PCB. 199 */ 200 struct pcb * const pcb = lwp_getpcb(l); 201 pcb->pcb_flags &= ~(PCB_FE0|PCB_FE1); 202 pcb->pcb_flags |= gr[_REG_MSR] & (PCB_FE0|PCB_FE1); 203#endif 204 205 (void)memcpy(&tf->tf_fixreg, gr, 32 * sizeof (gr[0])); 206 tf->tf_cr = gr[_REG_CR]; 207 tf->tf_lr = gr[_REG_LR]; 208 tf->tf_srr0 = gr[_REG_PC]; 209 /* 210 * Accept all user-settable bits without complaint; 211 * userland should not need to know the machine-specific 212 * MSR value. 213 */ 214 tf->tf_srr1 = (gr[_REG_MSR] & PSL_USERMOD) | PSL_USERSET; 215 tf->tf_ctr = gr[_REG_CTR]; 216 tf->tf_xer = gr[_REG_XER]; 217#ifdef PPC_OEA 218 tf->tf_mq = gr[_REG_MQ]; 219#endif 220 } 221 222#ifdef PPC_HAVE_FPU 223 /* Restore FPU context, if any. */ 224 if (flags & _UC_FPU) 225 fpu_restore_from_mcontext(l, mcp); 226#endif 227 228#ifdef ALTIVEC 229 /* Restore AltiVec context, if any. */ 230 if (flags & _UC_POWERPC_VEC) 231 vec_restore_from_mcontext(l, mcp); 232#endif 233 234#ifdef PPC_HAVE_SPE 235 /* Restore SPE context, if any. */ 236 if (flags & _UC_POWERPC_SPE) 237 vec_restore_from_mcontext(l, mcp); 238#endif 239 240 return (0); 241} 242