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/cdefs.h> 43 __FBSDID("$FreeBSD$"); 44 45 #include <sys/param.h> 46 #include <sys/exec.h> 47 #include <sys/imgact.h> 48 #include <sys/kdb.h> 49 #include <sys/kernel.h> 50 #include <sys/ktr.h> 51 #include <sys/lock.h> 52 #include <sys/mutex.h> 53 #include <sys/proc.h> 54 #include <sys/rwlock.h> 55 #include <sys/syscallsubr.h> 56 #include <sys/sysent.h> 57 #include <sys/sysproto.h> 58 #include <sys/vmmeter.h> 59 60 #include <machine/asm.h> 61 #include <machine/machdep.h> 62 #include <machine/pcb.h> 63 #include <machine/sysarch.h> 64 #include <machine/vfp.h> 65 #include <machine/vmparam.h> 66 67 #include <vm/vm.h> 68 #include <vm/vm_param.h> 69 #include <vm/pmap.h> 70 #include <vm/vm_map.h> 71 72 _Static_assert(sizeof(mcontext_t) == 208, "mcontext_t size incorrect"); 73 _Static_assert(sizeof(ucontext_t) == 260, "ucontext_t size incorrect"); 74 _Static_assert(sizeof(siginfo_t) == 64, "siginfo_t size incorrect"); 75 76 /* 77 * Clear registers on exec 78 */ 79 void 80 exec_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack) 81 { 82 struct trapframe *tf = td->td_frame; 83 84 memset(tf, 0, sizeof(*tf)); 85 tf->tf_usr_sp = stack; 86 tf->tf_usr_lr = imgp->entry_addr; 87 tf->tf_svc_lr = 0x77777777; 88 tf->tf_pc = imgp->entry_addr; 89 tf->tf_spsr = PSR_USR32_MODE; 90 if ((register_t)imgp->entry_addr & 1) 91 tf->tf_spsr |= PSR_T; 92 } 93 94 #ifdef VFP 95 /* 96 * Get machine VFP context. 97 */ 98 void 99 get_vfpcontext(struct thread *td, mcontext_vfp_t *vfp) 100 { 101 struct pcb *pcb; 102 103 MPASS(td == curthread); 104 105 pcb = td->td_pcb; 106 if ((pcb->pcb_fpflags & PCB_FP_STARTED) != 0) { 107 critical_enter(); 108 vfp_store(&pcb->pcb_vfpstate, false); 109 critical_exit(); 110 } 111 KASSERT(pcb->pcb_vfpsaved == &pcb->pcb_vfpstate, 112 ("Called get_vfpcontext while the kernel is using the 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 * Set machine VFP context. 120 */ 121 void 122 set_vfpcontext(struct thread *td, mcontext_vfp_t *vfp) 123 { 124 struct pcb *pcb; 125 126 MPASS(td == curthread); 127 128 pcb = td->td_pcb; 129 if ((pcb->pcb_fpflags & PCB_FP_STARTED) != 0) { 130 critical_enter(); 131 vfp_discard(td); 132 critical_exit(); 133 } 134 KASSERT(pcb->pcb_vfpsaved == &pcb->pcb_vfpstate, 135 ("Called set_vfpcontext while the kernel is using the VFP")); 136 memcpy(pcb->pcb_vfpstate.reg, vfp->mcv_reg, 137 sizeof(pcb->pcb_vfpstate.reg)); 138 pcb->pcb_vfpstate.fpscr = vfp->mcv_fpscr; 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 mcontext_vfp_t mcontext_vfp; 175 int rv; 176 177 if (clear_ret & GET_MC_CLEAR_RET) { 178 gr[_REG_R0] = 0; 179 gr[_REG_CPSR] = tf->tf_spsr & ~PSR_C; 180 } else { 181 gr[_REG_R0] = tf->tf_r0; 182 gr[_REG_CPSR] = tf->tf_spsr; 183 } 184 gr[_REG_R1] = tf->tf_r1; 185 gr[_REG_R2] = tf->tf_r2; 186 gr[_REG_R3] = tf->tf_r3; 187 gr[_REG_R4] = tf->tf_r4; 188 gr[_REG_R5] = tf->tf_r5; 189 gr[_REG_R6] = tf->tf_r6; 190 gr[_REG_R7] = tf->tf_r7; 191 gr[_REG_R8] = tf->tf_r8; 192 gr[_REG_R9] = tf->tf_r9; 193 gr[_REG_R10] = tf->tf_r10; 194 gr[_REG_R11] = tf->tf_r11; 195 gr[_REG_R12] = tf->tf_r12; 196 gr[_REG_SP] = tf->tf_usr_sp; 197 gr[_REG_LR] = tf->tf_usr_lr; 198 gr[_REG_PC] = tf->tf_pc; 199 200 #ifdef VFP 201 if (mcp->mc_vfp_size != sizeof(mcontext_vfp_t)) 202 return (EINVAL); 203 get_vfpcontext(td, &mcontext_vfp); 204 #else 205 bzero(&mcontext_vfp, sizeof(mcontext_vfp)); 206 #endif 207 208 if (mcp->mc_vfp_ptr != NULL) { 209 rv = copyout(&mcontext_vfp, mcp->mc_vfp_ptr, sizeof(mcontext_vfp)); 210 if (rv != 0) 211 return (rv); 212 } 213 214 return (0); 215 } 216 217 /* 218 * Set machine context. 219 * 220 * However, we don't set any but the user modifiable flags, and we won't 221 * touch the cs selector. 222 */ 223 int 224 set_mcontext(struct thread *td, mcontext_t *mcp) 225 { 226 mcontext_vfp_t mc_vfp, *vfp; 227 struct trapframe *tf = td->td_frame; 228 const __greg_t *gr = mcp->__gregs; 229 int spsr; 230 231 /* 232 * Make sure the processor mode has not been tampered with and 233 * interrupts have not been disabled. 234 */ 235 spsr = gr[_REG_CPSR]; 236 if ((spsr & PSR_MODE) != PSR_USR32_MODE || 237 (spsr & (PSR_I | PSR_F)) != 0) 238 return (EINVAL); 239 240 #ifdef WITNESS 241 if (mcp->mc_vfp_size != 0 && mcp->mc_vfp_size != sizeof(mc_vfp)) { 242 printf("%s: %s: Malformed mc_vfp_size: %d (0x%08X)\n", 243 td->td_proc->p_comm, __func__, 244 mcp->mc_vfp_size, mcp->mc_vfp_size); 245 } else if (mcp->mc_vfp_size != 0 && mcp->mc_vfp_ptr == NULL) { 246 printf("%s: %s: c_vfp_size != 0 but mc_vfp_ptr == NULL\n", 247 td->td_proc->p_comm, __func__); 248 } 249 #endif 250 251 if (mcp->mc_vfp_size == sizeof(mc_vfp) && mcp->mc_vfp_ptr != NULL) { 252 if (copyin(mcp->mc_vfp_ptr, &mc_vfp, sizeof(mc_vfp)) != 0) 253 return (EFAULT); 254 vfp = &mc_vfp; 255 } else { 256 vfp = NULL; 257 } 258 259 tf->tf_r0 = gr[_REG_R0]; 260 tf->tf_r1 = gr[_REG_R1]; 261 tf->tf_r2 = gr[_REG_R2]; 262 tf->tf_r3 = gr[_REG_R3]; 263 tf->tf_r4 = gr[_REG_R4]; 264 tf->tf_r5 = gr[_REG_R5]; 265 tf->tf_r6 = gr[_REG_R6]; 266 tf->tf_r7 = gr[_REG_R7]; 267 tf->tf_r8 = gr[_REG_R8]; 268 tf->tf_r9 = gr[_REG_R9]; 269 tf->tf_r10 = gr[_REG_R10]; 270 tf->tf_r11 = gr[_REG_R11]; 271 tf->tf_r12 = gr[_REG_R12]; 272 tf->tf_usr_sp = gr[_REG_SP]; 273 tf->tf_usr_lr = gr[_REG_LR]; 274 tf->tf_pc = gr[_REG_PC]; 275 tf->tf_spsr = gr[_REG_CPSR]; 276 #ifdef VFP 277 if (vfp != NULL) 278 set_vfpcontext(td, vfp); 279 #endif 280 return (0); 281 } 282 283 void 284 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 285 { 286 struct thread *td; 287 struct proc *p; 288 struct trapframe *tf; 289 struct sigframe *fp, frame; 290 struct sigacts *psp; 291 struct sysentvec *sysent; 292 int onstack; 293 int sig; 294 295 td = curthread; 296 p = td->td_proc; 297 PROC_LOCK_ASSERT(p, MA_OWNED); 298 sig = ksi->ksi_signo; 299 psp = p->p_sigacts; 300 mtx_assert(&psp->ps_mtx, MA_OWNED); 301 tf = td->td_frame; 302 onstack = sigonstack(tf->tf_usr_sp); 303 304 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm, 305 catcher, sig); 306 307 /* Allocate and validate space for the signal handler context. */ 308 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !(onstack) && 309 SIGISMEMBER(psp->ps_sigonstack, sig)) { 310 fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp + 311 td->td_sigstk.ss_size); 312 #if defined(COMPAT_43) 313 td->td_sigstk.ss_flags |= SS_ONSTACK; 314 #endif 315 } else 316 fp = (struct sigframe *)td->td_frame->tf_usr_sp; 317 318 /* make room on the stack */ 319 fp--; 320 321 /* make the stack aligned */ 322 fp = (struct sigframe *)STACKALIGN(fp); 323 /* Populate the siginfo frame. */ 324 bzero(&frame, sizeof(frame)); 325 get_mcontext(td, &frame.sf_uc.uc_mcontext, 0); 326 frame.sf_si = ksi->ksi_info; 327 frame.sf_uc.uc_sigmask = *mask; 328 frame.sf_uc.uc_stack = td->td_sigstk; 329 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ? 330 (onstack ? SS_ONSTACK : 0) : SS_DISABLE; 331 mtx_unlock(&psp->ps_mtx); 332 PROC_UNLOCK(td->td_proc); 333 334 /* Copy the sigframe out to the user's stack. */ 335 if (copyout(&frame, fp, sizeof(*fp)) != 0) { 336 /* Process has trashed its stack. Kill it. */ 337 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp); 338 PROC_LOCK(p); 339 sigexit(td, SIGILL); 340 } 341 342 /* 343 * Build context to run handler in. We invoke the handler 344 * directly, only returning via the trampoline. Note the 345 * trampoline version numbers are coordinated with machine- 346 * dependent code in libc. 347 */ 348 349 tf->tf_r0 = sig; 350 tf->tf_r1 = (register_t)&fp->sf_si; 351 tf->tf_r2 = (register_t)&fp->sf_uc; 352 353 /* the trampoline uses r5 as the uc address */ 354 tf->tf_r5 = (register_t)&fp->sf_uc; 355 tf->tf_pc = (register_t)catcher; 356 tf->tf_usr_sp = (register_t)fp; 357 sysent = p->p_sysent; 358 if (PROC_HAS_SHP(p)) 359 tf->tf_usr_lr = (register_t)PROC_SIGCODE(p); 360 else 361 tf->tf_usr_lr = (register_t)(PROC_PS_STRINGS(p) - 362 *(sysent->sv_szsigcode)); 363 /* Set the mode to enter in the signal handler */ 364 #if __ARM_ARCH >= 7 365 if ((register_t)catcher & 1) 366 tf->tf_spsr |= PSR_T; 367 else 368 tf->tf_spsr &= ~PSR_T; 369 #endif 370 371 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_usr_lr, 372 tf->tf_usr_sp); 373 374 PROC_LOCK(p); 375 mtx_lock(&psp->ps_mtx); 376 } 377 378 int 379 sys_sigreturn(struct thread *td, struct sigreturn_args *uap) 380 { 381 ucontext_t uc; 382 int error; 383 384 if (uap == NULL) 385 return (EFAULT); 386 if (copyin(uap->sigcntxp, &uc, sizeof(uc))) 387 return (EFAULT); 388 /* Restore register context. */ 389 error = set_mcontext(td, &uc.uc_mcontext); 390 if (error != 0) 391 return (error); 392 393 /* Restore signal mask. */ 394 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0); 395 396 return (EJUSTRETURN); 397 } 398