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 || TD_IS_SUSPENDED(td) || 104 P_SHOULDSTOP(td->td_proc)); 105 106 pcb = td->td_pcb; 107 if ((pcb->pcb_fpflags & PCB_FP_STARTED) != 0 && td == curthread) { 108 critical_enter(); 109 vfp_store(&pcb->pcb_vfpstate, false); 110 critical_exit(); 111 } 112 KASSERT(pcb->pcb_vfpsaved == &pcb->pcb_vfpstate, 113 ("Called get_vfpcontext while the kernel is using the VFP")); 114 memcpy(vfp, &pcb->pcb_vfpstate, sizeof(*vfp)); 115 } 116 117 /* 118 * Set machine VFP context. 119 */ 120 void 121 set_vfpcontext(struct thread *td, mcontext_vfp_t *vfp) 122 { 123 struct pcb *pcb; 124 125 pcb = td->td_pcb; 126 if (td == curthread) { 127 critical_enter(); 128 vfp_discard(td); 129 critical_exit(); 130 } 131 KASSERT(pcb->pcb_vfpsaved == &pcb->pcb_vfpstate, 132 ("Called set_vfpcontext while the kernel is using the VFP")); 133 memcpy(&pcb->pcb_vfpstate, vfp, sizeof(*vfp)); 134 } 135 #endif 136 137 int 138 arm_get_vfpstate(struct thread *td, void *args) 139 { 140 int rv; 141 struct arm_get_vfpstate_args ua; 142 mcontext_vfp_t mcontext_vfp; 143 144 rv = copyin(args, &ua, sizeof(ua)); 145 if (rv != 0) 146 return (rv); 147 if (ua.mc_vfp_size != sizeof(mcontext_vfp_t)) 148 return (EINVAL); 149 #ifdef VFP 150 get_vfpcontext(td, &mcontext_vfp); 151 #else 152 bzero(&mcontext_vfp, sizeof(mcontext_vfp)); 153 #endif 154 155 rv = copyout(&mcontext_vfp, ua.mc_vfp, sizeof(mcontext_vfp)); 156 if (rv != 0) 157 return (rv); 158 return (0); 159 } 160 161 /* 162 * Get machine context. 163 */ 164 int 165 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret) 166 { 167 struct trapframe *tf = td->td_frame; 168 __greg_t *gr = mcp->__gregs; 169 mcontext_vfp_t mcontext_vfp; 170 int rv; 171 172 if (clear_ret & GET_MC_CLEAR_RET) { 173 gr[_REG_R0] = 0; 174 gr[_REG_CPSR] = tf->tf_spsr & ~PSR_C; 175 } else { 176 gr[_REG_R0] = tf->tf_r0; 177 gr[_REG_CPSR] = tf->tf_spsr; 178 } 179 gr[_REG_R1] = tf->tf_r1; 180 gr[_REG_R2] = tf->tf_r2; 181 gr[_REG_R3] = tf->tf_r3; 182 gr[_REG_R4] = tf->tf_r4; 183 gr[_REG_R5] = tf->tf_r5; 184 gr[_REG_R6] = tf->tf_r6; 185 gr[_REG_R7] = tf->tf_r7; 186 gr[_REG_R8] = tf->tf_r8; 187 gr[_REG_R9] = tf->tf_r9; 188 gr[_REG_R10] = tf->tf_r10; 189 gr[_REG_R11] = tf->tf_r11; 190 gr[_REG_R12] = tf->tf_r12; 191 gr[_REG_SP] = tf->tf_usr_sp; 192 gr[_REG_LR] = tf->tf_usr_lr; 193 gr[_REG_PC] = tf->tf_pc; 194 195 #ifdef VFP 196 if (mcp->mc_vfp_size != sizeof(mcontext_vfp_t)) 197 return (EINVAL); 198 get_vfpcontext(td, &mcontext_vfp); 199 #else 200 bzero(&mcontext_vfp, sizeof(mcontext_vfp)); 201 #endif 202 203 if (mcp->mc_vfp_ptr != NULL) { 204 rv = copyout(&mcontext_vfp, mcp->mc_vfp_ptr, sizeof(mcontext_vfp)); 205 if (rv != 0) 206 return (rv); 207 } 208 209 return (0); 210 } 211 212 /* 213 * Set machine context. 214 * 215 * However, we don't set any but the user modifiable flags, and we won't 216 * touch the cs selector. 217 */ 218 int 219 set_mcontext(struct thread *td, mcontext_t *mcp) 220 { 221 mcontext_vfp_t mc_vfp, *vfp; 222 struct trapframe *tf = td->td_frame; 223 const __greg_t *gr = mcp->__gregs; 224 int spsr; 225 226 /* 227 * Make sure the processor mode has not been tampered with and 228 * interrupts have not been disabled. 229 */ 230 spsr = gr[_REG_CPSR]; 231 if ((spsr & PSR_MODE) != PSR_USR32_MODE || 232 (spsr & (PSR_I | PSR_F)) != 0) 233 return (EINVAL); 234 235 #ifdef WITNESS 236 if (mcp->mc_vfp_size != 0 && mcp->mc_vfp_size != sizeof(mc_vfp)) { 237 printf("%s: %s: Malformed mc_vfp_size: %d (0x%08X)\n", 238 td->td_proc->p_comm, __func__, 239 mcp->mc_vfp_size, mcp->mc_vfp_size); 240 } else if (mcp->mc_vfp_size != 0 && mcp->mc_vfp_ptr == NULL) { 241 printf("%s: %s: c_vfp_size != 0 but mc_vfp_ptr == NULL\n", 242 td->td_proc->p_comm, __func__); 243 } 244 #endif 245 246 if (mcp->mc_vfp_size == sizeof(mc_vfp) && mcp->mc_vfp_ptr != NULL) { 247 if (copyin(mcp->mc_vfp_ptr, &mc_vfp, sizeof(mc_vfp)) != 0) 248 return (EFAULT); 249 vfp = &mc_vfp; 250 } else { 251 vfp = NULL; 252 } 253 254 tf->tf_r0 = gr[_REG_R0]; 255 tf->tf_r1 = gr[_REG_R1]; 256 tf->tf_r2 = gr[_REG_R2]; 257 tf->tf_r3 = gr[_REG_R3]; 258 tf->tf_r4 = gr[_REG_R4]; 259 tf->tf_r5 = gr[_REG_R5]; 260 tf->tf_r6 = gr[_REG_R6]; 261 tf->tf_r7 = gr[_REG_R7]; 262 tf->tf_r8 = gr[_REG_R8]; 263 tf->tf_r9 = gr[_REG_R9]; 264 tf->tf_r10 = gr[_REG_R10]; 265 tf->tf_r11 = gr[_REG_R11]; 266 tf->tf_r12 = gr[_REG_R12]; 267 tf->tf_usr_sp = gr[_REG_SP]; 268 tf->tf_usr_lr = gr[_REG_LR]; 269 tf->tf_pc = gr[_REG_PC]; 270 tf->tf_spsr = gr[_REG_CPSR]; 271 #ifdef VFP 272 if (vfp != NULL) 273 set_vfpcontext(td, vfp); 274 #endif 275 return (0); 276 } 277 278 void 279 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 280 { 281 struct thread *td; 282 struct proc *p; 283 struct trapframe *tf; 284 struct sigframe *fp, frame; 285 struct sigacts *psp; 286 struct sysentvec *sysent; 287 int onstack; 288 int sig; 289 290 td = curthread; 291 p = td->td_proc; 292 PROC_LOCK_ASSERT(p, MA_OWNED); 293 sig = ksi->ksi_signo; 294 psp = p->p_sigacts; 295 mtx_assert(&psp->ps_mtx, MA_OWNED); 296 tf = td->td_frame; 297 onstack = sigonstack(tf->tf_usr_sp); 298 299 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm, 300 catcher, sig); 301 302 /* Allocate and validate space for the signal handler context. */ 303 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !(onstack) && 304 SIGISMEMBER(psp->ps_sigonstack, sig)) { 305 fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp + 306 td->td_sigstk.ss_size); 307 #if defined(COMPAT_43) 308 td->td_sigstk.ss_flags |= SS_ONSTACK; 309 #endif 310 } else 311 fp = (struct sigframe *)td->td_frame->tf_usr_sp; 312 313 /* make room on the stack */ 314 fp--; 315 316 /* make the stack aligned */ 317 fp = (struct sigframe *)STACKALIGN(fp); 318 /* Populate the siginfo frame. */ 319 bzero(&frame, sizeof(frame)); 320 get_mcontext(td, &frame.sf_uc.uc_mcontext, 0); 321 frame.sf_si = ksi->ksi_info; 322 frame.sf_uc.uc_sigmask = *mask; 323 frame.sf_uc.uc_stack = td->td_sigstk; 324 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ? 325 (onstack ? SS_ONSTACK : 0) : SS_DISABLE; 326 mtx_unlock(&psp->ps_mtx); 327 PROC_UNLOCK(td->td_proc); 328 329 /* Copy the sigframe out to the user's stack. */ 330 if (copyout(&frame, fp, sizeof(*fp)) != 0) { 331 /* Process has trashed its stack. Kill it. */ 332 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp); 333 PROC_LOCK(p); 334 sigexit(td, SIGILL); 335 } 336 337 /* 338 * Build context to run handler in. We invoke the handler 339 * directly, only returning via the trampoline. Note the 340 * trampoline version numbers are coordinated with machine- 341 * dependent code in libc. 342 */ 343 344 tf->tf_r0 = sig; 345 tf->tf_r1 = (register_t)&fp->sf_si; 346 tf->tf_r2 = (register_t)&fp->sf_uc; 347 348 /* the trampoline uses r5 as the uc address */ 349 tf->tf_r5 = (register_t)&fp->sf_uc; 350 tf->tf_pc = (register_t)catcher; 351 tf->tf_usr_sp = (register_t)fp; 352 sysent = p->p_sysent; 353 if (PROC_HAS_SHP(p)) 354 tf->tf_usr_lr = (register_t)PROC_SIGCODE(p); 355 else 356 tf->tf_usr_lr = (register_t)(PROC_PS_STRINGS(p) - 357 *(sysent->sv_szsigcode)); 358 /* Set the mode to enter in the signal handler */ 359 #if __ARM_ARCH >= 7 360 if ((register_t)catcher & 1) 361 tf->tf_spsr |= PSR_T; 362 else 363 tf->tf_spsr &= ~PSR_T; 364 #endif 365 366 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_usr_lr, 367 tf->tf_usr_sp); 368 369 PROC_LOCK(p); 370 mtx_lock(&psp->ps_mtx); 371 } 372 373 int 374 sys_sigreturn(struct thread *td, struct sigreturn_args *uap) 375 { 376 ucontext_t uc; 377 int error; 378 379 if (uap == NULL) 380 return (EFAULT); 381 if (copyin(uap->sigcntxp, &uc, sizeof(uc))) 382 return (EFAULT); 383 /* Restore register context. */ 384 error = set_mcontext(td, &uc.uc_mcontext); 385 if (error != 0) 386 return (error); 387 388 /* Restore signal mask. */ 389 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0); 390 391 return (EJUSTRETURN); 392 } 393