1 /* $NetBSD: arm32_machdep.c,v 1.44 2004/03/24 15:34:47 atatat Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-4-Clause 5 * 6 * Copyright (c) 2004 Olivier Houchard 7 * Copyright (c) 1994-1998 Mark Brinicombe. 8 * Copyright (c) 1994 Brini. 9 * All rights reserved. 10 * 11 * This code is derived from software written for Brini by Mark Brinicombe 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by Mark Brinicombe 24 * for the NetBSD Project. 25 * 4. The name of the company nor the name of the author may be used to 26 * endorse or promote products derived from this software without specific 27 * prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 31 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 32 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 */ 41 42 #include <sys/param.h> 43 #include <sys/exec.h> 44 #include <sys/imgact.h> 45 #include <sys/kdb.h> 46 #include <sys/kernel.h> 47 #include <sys/ktr.h> 48 #include <sys/lock.h> 49 #include <sys/mutex.h> 50 #include <sys/proc.h> 51 #include <sys/rwlock.h> 52 #include <sys/syscallsubr.h> 53 #include <sys/sysent.h> 54 #include <sys/sysproto.h> 55 #include <sys/vmmeter.h> 56 57 #include <machine/asm.h> 58 #include <machine/machdep.h> 59 #include <machine/pcb.h> 60 #include <machine/sysarch.h> 61 #include <machine/vfp.h> 62 #include <machine/vmparam.h> 63 64 #include <vm/vm.h> 65 #include <vm/vm_param.h> 66 #include <vm/pmap.h> 67 #include <vm/vm_map.h> 68 69 _Static_assert(sizeof(mcontext_t) == 208, "mcontext_t size incorrect"); 70 _Static_assert(sizeof(ucontext_t) == 260, "ucontext_t size incorrect"); 71 _Static_assert(sizeof(siginfo_t) == 64, "siginfo_t size incorrect"); 72 73 /* 74 * Clear registers on exec 75 */ 76 void 77 exec_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack) 78 { 79 struct trapframe *tf = td->td_frame; 80 81 memset(tf, 0, sizeof(*tf)); 82 tf->tf_usr_sp = stack; 83 tf->tf_usr_lr = imgp->entry_addr; 84 tf->tf_svc_lr = 0x77777777; 85 tf->tf_pc = imgp->entry_addr; 86 tf->tf_spsr = PSR_USR32_MODE; 87 if ((register_t)imgp->entry_addr & 1) 88 tf->tf_spsr |= PSR_T; 89 } 90 91 #ifdef VFP 92 /* 93 * Get machine VFP context. 94 */ 95 void 96 get_vfpcontext(struct thread *td, mcontext_vfp_t *vfp) 97 { 98 struct pcb *pcb; 99 100 MPASS(td == curthread || TD_IS_SUSPENDED(td) || 101 P_SHOULDSTOP(td->td_proc)); 102 103 pcb = td->td_pcb; 104 if (td == curthread) { 105 critical_enter(); 106 vfp_store(&pcb->pcb_vfpstate, false); 107 critical_exit(); 108 } 109 KASSERT(pcb->pcb_vfpsaved == &pcb->pcb_vfpstate, 110 ("Called get_vfpcontext while the kernel is using the VFP")); 111 112 memset(vfp, 0, sizeof(*vfp)); 113 memcpy(vfp->mcv_reg, pcb->pcb_vfpstate.reg, 114 sizeof(vfp->mcv_reg)); 115 vfp->mcv_fpscr = pcb->pcb_vfpstate.fpscr; 116 117 } 118 119 /* 120 * Set machine VFP context. 121 */ 122 void 123 set_vfpcontext(struct thread *td, mcontext_vfp_t *vfp) 124 { 125 struct pcb *pcb; 126 127 pcb = td->td_pcb; 128 if (td == curthread) { 129 critical_enter(); 130 vfp_discard(td); 131 critical_exit(); 132 } 133 KASSERT(pcb->pcb_vfpsaved == &pcb->pcb_vfpstate, 134 ("Called set_vfpcontext while the kernel is using the VFP")); 135 memcpy(pcb->pcb_vfpstate.reg, vfp->mcv_reg, 136 sizeof(pcb->pcb_vfpstate.reg)); 137 pcb->pcb_vfpstate.fpscr = vfp->mcv_fpscr; 138 139 } 140 #endif 141 142 int 143 arm_get_vfpstate(struct thread *td, void *args) 144 { 145 int rv; 146 struct arm_get_vfpstate_args ua; 147 mcontext_vfp_t mcontext_vfp; 148 149 rv = copyin(args, &ua, sizeof(ua)); 150 if (rv != 0) 151 return (rv); 152 if (ua.mc_vfp_size != sizeof(mcontext_vfp_t)) 153 return (EINVAL); 154 #ifdef VFP 155 get_vfpcontext(td, &mcontext_vfp); 156 #else 157 bzero(&mcontext_vfp, sizeof(mcontext_vfp)); 158 #endif 159 160 rv = copyout(&mcontext_vfp, ua.mc_vfp, sizeof(mcontext_vfp)); 161 if (rv != 0) 162 return (rv); 163 return (0); 164 } 165 166 /* 167 * Get machine context. 168 */ 169 int 170 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret) 171 { 172 struct trapframe *tf = td->td_frame; 173 __greg_t *gr = mcp->__gregs; 174 175 if (clear_ret & GET_MC_CLEAR_RET) { 176 gr[_REG_R0] = 0; 177 gr[_REG_CPSR] = tf->tf_spsr & ~PSR_C; 178 } else { 179 gr[_REG_R0] = tf->tf_r0; 180 gr[_REG_CPSR] = tf->tf_spsr; 181 } 182 gr[_REG_R1] = tf->tf_r1; 183 gr[_REG_R2] = tf->tf_r2; 184 gr[_REG_R3] = tf->tf_r3; 185 gr[_REG_R4] = tf->tf_r4; 186 gr[_REG_R5] = tf->tf_r5; 187 gr[_REG_R6] = tf->tf_r6; 188 gr[_REG_R7] = tf->tf_r7; 189 gr[_REG_R8] = tf->tf_r8; 190 gr[_REG_R9] = tf->tf_r9; 191 gr[_REG_R10] = tf->tf_r10; 192 gr[_REG_R11] = tf->tf_r11; 193 gr[_REG_R12] = tf->tf_r12; 194 gr[_REG_SP] = tf->tf_usr_sp; 195 gr[_REG_LR] = tf->tf_usr_lr; 196 gr[_REG_PC] = tf->tf_pc; 197 198 mcp->mc_vfp_size = 0; 199 mcp->mc_vfp_ptr = NULL; 200 memset(&mcp->mc_spare, 0, sizeof(mcp->mc_spare)); 201 202 return (0); 203 } 204 205 /* 206 * Set machine context. 207 * 208 * However, we don't set any but the user modifiable flags, and we won't 209 * touch the cs selector. 210 */ 211 int 212 set_mcontext(struct thread *td, mcontext_t *mcp) 213 { 214 mcontext_vfp_t mc_vfp, *vfp; 215 struct trapframe *tf = td->td_frame; 216 const __greg_t *gr = mcp->__gregs; 217 int spsr; 218 219 /* 220 * Make sure the processor mode has not been tampered with and 221 * interrupts have not been disabled. 222 */ 223 spsr = gr[_REG_CPSR]; 224 if ((spsr & PSR_MODE) != PSR_USR32_MODE || 225 (spsr & (PSR_I | PSR_F)) != 0) 226 return (EINVAL); 227 228 #ifdef WITNESS 229 if (mcp->mc_vfp_size != 0 && mcp->mc_vfp_size != sizeof(mc_vfp)) { 230 printf("%s: %s: Malformed mc_vfp_size: %d (0x%08X)\n", 231 td->td_proc->p_comm, __func__, 232 mcp->mc_vfp_size, mcp->mc_vfp_size); 233 } else if (mcp->mc_vfp_size != 0 && mcp->mc_vfp_ptr == NULL) { 234 printf("%s: %s: c_vfp_size != 0 but mc_vfp_ptr == NULL\n", 235 td->td_proc->p_comm, __func__); 236 } 237 #endif 238 239 if (mcp->mc_vfp_size == sizeof(mc_vfp) && mcp->mc_vfp_ptr != NULL) { 240 if (copyin(mcp->mc_vfp_ptr, &mc_vfp, sizeof(mc_vfp)) != 0) 241 return (EFAULT); 242 vfp = &mc_vfp; 243 } else { 244 vfp = NULL; 245 } 246 247 tf->tf_r0 = gr[_REG_R0]; 248 tf->tf_r1 = gr[_REG_R1]; 249 tf->tf_r2 = gr[_REG_R2]; 250 tf->tf_r3 = gr[_REG_R3]; 251 tf->tf_r4 = gr[_REG_R4]; 252 tf->tf_r5 = gr[_REG_R5]; 253 tf->tf_r6 = gr[_REG_R6]; 254 tf->tf_r7 = gr[_REG_R7]; 255 tf->tf_r8 = gr[_REG_R8]; 256 tf->tf_r9 = gr[_REG_R9]; 257 tf->tf_r10 = gr[_REG_R10]; 258 tf->tf_r11 = gr[_REG_R11]; 259 tf->tf_r12 = gr[_REG_R12]; 260 tf->tf_usr_sp = gr[_REG_SP]; 261 tf->tf_usr_lr = gr[_REG_LR]; 262 tf->tf_pc = gr[_REG_PC]; 263 tf->tf_spsr = gr[_REG_CPSR]; 264 #ifdef VFP 265 if (vfp != NULL) 266 set_vfpcontext(td, vfp); 267 #endif 268 return (0); 269 } 270 271 void 272 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 273 { 274 struct thread *td; 275 struct proc *p; 276 struct trapframe *tf; 277 struct sigframe *fp, frame; 278 struct sigacts *psp; 279 struct sysentvec *sysent; 280 int onstack; 281 int sig; 282 283 td = curthread; 284 p = td->td_proc; 285 PROC_LOCK_ASSERT(p, MA_OWNED); 286 sig = ksi->ksi_signo; 287 psp = p->p_sigacts; 288 mtx_assert(&psp->ps_mtx, MA_OWNED); 289 tf = td->td_frame; 290 onstack = sigonstack(tf->tf_usr_sp); 291 292 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm, 293 catcher, sig); 294 295 /* Allocate and validate space for the signal handler context. */ 296 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !(onstack) && 297 SIGISMEMBER(psp->ps_sigonstack, sig)) { 298 fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp + 299 td->td_sigstk.ss_size); 300 #if defined(COMPAT_43) 301 td->td_sigstk.ss_flags |= SS_ONSTACK; 302 #endif 303 } else 304 fp = (struct sigframe *)td->td_frame->tf_usr_sp; 305 306 /* make room on the stack */ 307 fp--; 308 309 /* make the stack aligned */ 310 fp = (struct sigframe *)STACKALIGN(fp); 311 /* Populate the siginfo frame. */ 312 bzero(&frame, sizeof(frame)); 313 get_mcontext(td, &frame.sf_uc.uc_mcontext, 0); 314 315 #ifdef VFP 316 get_vfpcontext(td, &frame.sf_vfp); 317 frame.sf_uc.uc_mcontext.mc_vfp_size = sizeof(fp->sf_vfp); 318 frame.sf_uc.uc_mcontext.mc_vfp_ptr = &fp->sf_vfp; 319 #else 320 frame.sf_uc.uc_mcontext.mc_vfp_size = 0; 321 frame.sf_uc.uc_mcontext.mc_vfp_ptr = NULL; 322 #endif 323 324 frame.sf_si = ksi->ksi_info; 325 frame.sf_uc.uc_sigmask = *mask; 326 frame.sf_uc.uc_stack = td->td_sigstk; 327 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ? 328 (onstack ? SS_ONSTACK : 0) : SS_DISABLE; 329 mtx_unlock(&psp->ps_mtx); 330 PROC_UNLOCK(td->td_proc); 331 332 /* Copy the sigframe out to the user's stack. */ 333 if (copyout(&frame, fp, sizeof(*fp)) != 0) { 334 /* Process has trashed its stack. Kill it. */ 335 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp); 336 PROC_LOCK(p); 337 sigexit(td, SIGILL); 338 } 339 340 /* 341 * Build context to run handler in. We invoke the handler 342 * directly, only returning via the trampoline. Note the 343 * trampoline version numbers are coordinated with machine- 344 * dependent code in libc. 345 */ 346 347 tf->tf_r0 = sig; 348 tf->tf_r1 = (register_t)&fp->sf_si; 349 tf->tf_r2 = (register_t)&fp->sf_uc; 350 351 /* the trampoline uses r5 as the uc address */ 352 tf->tf_r5 = (register_t)&fp->sf_uc; 353 tf->tf_pc = (register_t)catcher; 354 tf->tf_usr_sp = (register_t)fp; 355 sysent = p->p_sysent; 356 if (PROC_HAS_SHP(p)) 357 tf->tf_usr_lr = (register_t)PROC_SIGCODE(p); 358 else 359 tf->tf_usr_lr = (register_t)(PROC_PS_STRINGS(p) - 360 *(sysent->sv_szsigcode)); 361 /* Set the mode to enter in the signal handler */ 362 if ((register_t)catcher & 1) 363 tf->tf_spsr |= PSR_T; 364 else 365 tf->tf_spsr &= ~PSR_T; 366 367 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_usr_lr, 368 tf->tf_usr_sp); 369 370 PROC_LOCK(p); 371 mtx_lock(&psp->ps_mtx); 372 } 373 374 int 375 sys_sigreturn(struct thread *td, struct sigreturn_args *uap) 376 { 377 ucontext_t uc; 378 int error; 379 380 if (uap == NULL) 381 return (EFAULT); 382 if (copyin(uap->sigcntxp, &uc, sizeof(uc))) 383 return (EFAULT); 384 /* Restore register context. */ 385 error = set_mcontext(td, &uc.uc_mcontext); 386 if (error != 0) 387 return (error); 388 389 /* Restore signal mask. */ 390 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0); 391 392 return (EJUSTRETURN); 393 } 394