1 1.3 msaitoh /* $NetBSD: linux32_machdep.c,v 1.3 2024/02/07 04:20:28 msaitoh Exp $ */ 2 1.1 ryo 3 1.1 ryo /*- 4 1.3 msaitoh * Copyright (c) 2021 Ryo Shimizu 5 1.1 ryo * All rights reserved. 6 1.1 ryo * 7 1.1 ryo * Redistribution and use in source and binary forms, with or without 8 1.1 ryo * modification, are permitted provided that the following conditions 9 1.1 ryo * are met: 10 1.1 ryo * 1. Redistributions of source code must retain the above copyright 11 1.1 ryo * notice, this list of conditions and the following disclaimer. 12 1.1 ryo * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 ryo * notice, this list of conditions and the following disclaimer in the 14 1.1 ryo * documentation and/or other materials provided with the distribution. 15 1.1 ryo * 16 1.1 ryo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 17 1.1 ryo * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 1.1 ryo * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 1.1 ryo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 20 1.1 ryo * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 1.1 ryo * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 1.1 ryo * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 1.1 ryo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 1.1 ryo * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 25 1.1 ryo * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 ryo * POSSIBILITY OF SUCH DAMAGE. 27 1.1 ryo */ 28 1.1 ryo 29 1.1 ryo #include <sys/cdefs.h> 30 1.3 msaitoh __KERNEL_RCSID(0, "$NetBSD: linux32_machdep.c,v 1.3 2024/02/07 04:20:28 msaitoh Exp $"); 31 1.1 ryo 32 1.1 ryo #include <sys/param.h> 33 1.1 ryo #include <sys/systm.h> 34 1.1 ryo #include <sys/proc.h> 35 1.1 ryo #include <sys/exec.h> 36 1.1 ryo 37 1.1 ryo #include <compat/netbsd32/netbsd32.h> 38 1.1 ryo #include <compat/netbsd32/netbsd32_exec.h> 39 1.1 ryo 40 1.1 ryo #include <compat/linux/common/linux_types.h> 41 1.1 ryo #include <compat/linux/common/linux_signal.h> 42 1.1 ryo #include <compat/linux/common/linux_errno.h> 43 1.1 ryo #include <compat/linux/common/linux_machdep.h> 44 1.1 ryo 45 1.1 ryo #include <compat/linux32/common/linux32_types.h> 46 1.1 ryo #include <compat/linux32/common/linux32_signal.h> 47 1.1 ryo #include <compat/linux32/common/linux32_errno.h> 48 1.1 ryo #include <compat/linux32/common/linux32_exec.h> 49 1.1 ryo #include <compat/linux32/common/linux32_machdep.h> 50 1.1 ryo #include <compat/linux32/linux32_syscall.h> 51 1.1 ryo #include <compat/linux32/linux32_syscallargs.h> 52 1.1 ryo 53 1.1 ryo #include <machine/netbsd32_machdep.h> 54 1.1 ryo #include <arm/vfpreg.h> 55 1.1 ryo 56 1.1 ryo static void 57 1.1 ryo linux32_save_sigcontext(struct lwp *l, struct linux32_ucontext *luc) 58 1.1 ryo { 59 1.1 ryo ucontext32_t uc; 60 1.1 ryo __greg32_t *gr = uc.uc_mcontext.__gregs; 61 1.1 ryo __vfpregset32_t *vfpregs = &uc.uc_mcontext.__vfpregs; 62 1.1 ryo struct linux32_aux_sigframe *aux; 63 1.1 ryo int i; 64 1.1 ryo 65 1.1 ryo cpu_getmcontext32(l, &uc.uc_mcontext, &uc.uc_flags); 66 1.1 ryo 67 1.1 ryo luc->luc_mcontext.arm_r0 = gr[_REG_R0]; 68 1.1 ryo luc->luc_mcontext.arm_r1 = gr[_REG_R1]; 69 1.1 ryo luc->luc_mcontext.arm_r2 = gr[_REG_R2]; 70 1.1 ryo luc->luc_mcontext.arm_r3 = gr[_REG_R3]; 71 1.1 ryo luc->luc_mcontext.arm_r4 = gr[_REG_R4]; 72 1.1 ryo luc->luc_mcontext.arm_r5 = gr[_REG_R5]; 73 1.1 ryo luc->luc_mcontext.arm_r6 = gr[_REG_R6]; 74 1.1 ryo luc->luc_mcontext.arm_r7 = gr[_REG_R7]; 75 1.1 ryo luc->luc_mcontext.arm_r8 = gr[_REG_R8]; 76 1.1 ryo luc->luc_mcontext.arm_r9 = gr[_REG_R9]; 77 1.1 ryo luc->luc_mcontext.arm_r10 = gr[_REG_R10]; 78 1.1 ryo luc->luc_mcontext.arm_fp = gr[_REG_R11]; 79 1.1 ryo luc->luc_mcontext.arm_ip = gr[_REG_R12]; 80 1.1 ryo luc->luc_mcontext.arm_sp = gr[_REG_R13]; 81 1.1 ryo luc->luc_mcontext.arm_lr = gr[_REG_R14]; 82 1.1 ryo luc->luc_mcontext.arm_pc = gr[_REG_R15]; 83 1.1 ryo luc->luc_mcontext.arm_cpsr = gr[_REG_CPSR]; 84 1.1 ryo luc->luc_mcontext.trap_no = 0; 85 1.1 ryo luc->luc_mcontext.error_code = 0; 86 1.1 ryo luc->luc_mcontext.oldmask = 0; 87 1.1 ryo luc->luc_mcontext.fault_address = 0; 88 1.1 ryo 89 1.1 ryo if (uc.uc_flags & _UC_FPU) { 90 1.1 ryo aux = (struct linux32_aux_sigframe *)luc->luc_regspace; 91 1.1 ryo #define LINUX32_VFP_MAGIC 0x56465001 92 1.1 ryo aux->vfp.magic = LINUX32_VFP_MAGIC; 93 1.1 ryo aux->vfp.size = sizeof(struct linux32_vfp_sigframe); 94 1.1 ryo 95 1.1 ryo CTASSERT(__arraycount(aux->vfp.ufp.fpregs) == 96 1.1 ryo __arraycount(vfpregs->__vfp_fstmx)); 97 1.1 ryo for (i = 0; i < __arraycount(aux->vfp.ufp.fpregs); i++) 98 1.1 ryo aux->vfp.ufp.fpregs[i] = vfpregs->__vfp_fstmx[i]; 99 1.1 ryo aux->vfp.ufp.fpscr = vfpregs->__vfp_fpscr; 100 1.1 ryo 101 1.1 ryo aux->vfp.ufp_exc.fpexc = VFP_FPEXC_EN | VFP_FPEXC_VECITR; 102 1.1 ryo aux->vfp.ufp_exc.fpinst = 0; 103 1.1 ryo aux->vfp.ufp_exc.fpinst2 = 0; 104 1.1 ryo aux->end_magic = 0 ; 105 1.1 ryo } 106 1.1 ryo } 107 1.1 ryo 108 1.1 ryo static int 109 1.1 ryo linux32_restore_sigcontext(struct lwp *l, struct linux32_ucontext *luc) 110 1.1 ryo { 111 1.2 ryo struct proc * const p = l->l_proc; 112 1.1 ryo ucontext32_t uc; 113 1.1 ryo __greg32_t *gr = uc.uc_mcontext.__gregs; 114 1.1 ryo __vfpregset32_t *vfpregs = &uc.uc_mcontext.__vfpregs; 115 1.1 ryo struct linux32_aux_sigframe *aux; 116 1.2 ryo int i, error; 117 1.1 ryo 118 1.1 ryo memset(&uc, 0, sizeof(uc)); 119 1.2 ryo 120 1.2 ryo /* build .uc_sigmask */ 121 1.2 ryo linux32_to_native_sigset(&uc.uc_sigmask, &luc->luc_sigmask); 122 1.2 ryo uc.uc_flags |= _UC_SIGMASK; 123 1.2 ryo 124 1.2 ryo /* build .uc_stack */ 125 1.2 ryo if (luc->luc_stack.ss_flags & LINUX_SS_ONSTACK) 126 1.2 ryo uc.uc_stack.ss_flags |= SS_ONSTACK; 127 1.2 ryo if (luc->luc_stack.ss_flags & LINUX_SS_DISABLE) 128 1.2 ryo uc.uc_stack.ss_flags |= SS_DISABLE; 129 1.2 ryo uc.uc_flags |= _UC_STACK; 130 1.2 ryo 131 1.2 ryo /* build .uc_mcontext */ 132 1.1 ryo gr[_REG_R0] = luc->luc_mcontext.arm_r0; 133 1.1 ryo gr[_REG_R1] = luc->luc_mcontext.arm_r1; 134 1.1 ryo gr[_REG_R2] = luc->luc_mcontext.arm_r2; 135 1.1 ryo gr[_REG_R3] = luc->luc_mcontext.arm_r3; 136 1.1 ryo gr[_REG_R4] = luc->luc_mcontext.arm_r4; 137 1.1 ryo gr[_REG_R5] = luc->luc_mcontext.arm_r5; 138 1.1 ryo gr[_REG_R6] = luc->luc_mcontext.arm_r6; 139 1.1 ryo gr[_REG_R7] = luc->luc_mcontext.arm_r7; 140 1.1 ryo gr[_REG_R8] = luc->luc_mcontext.arm_r8; 141 1.1 ryo gr[_REG_R9] = luc->luc_mcontext.arm_r9; 142 1.1 ryo gr[_REG_R10] = luc->luc_mcontext.arm_r10; 143 1.1 ryo gr[_REG_R11] = luc->luc_mcontext.arm_fp; 144 1.1 ryo gr[_REG_R12] = luc->luc_mcontext.arm_ip; 145 1.1 ryo gr[_REG_R13] = luc->luc_mcontext.arm_sp; 146 1.1 ryo gr[_REG_R14] = luc->luc_mcontext.arm_lr; 147 1.1 ryo gr[_REG_R15] = luc->luc_mcontext.arm_pc; 148 1.1 ryo gr[_REG_CPSR] = luc->luc_mcontext.arm_cpsr; 149 1.1 ryo uc.uc_flags |= _UC_CPU; 150 1.1 ryo 151 1.1 ryo aux = (struct linux32_aux_sigframe *)luc->luc_regspace; 152 1.1 ryo if (aux->vfp.magic == LINUX32_VFP_MAGIC && 153 1.1 ryo aux->vfp.size == sizeof(struct linux32_vfp_sigframe)) { 154 1.1 ryo 155 1.1 ryo CTASSERT(__arraycount(vfpregs->__vfp_fstmx) == 156 1.1 ryo __arraycount(aux->vfp.ufp.fpregs)); 157 1.1 ryo for (i = 0; i < __arraycount(vfpregs->__vfp_fstmx); i++) 158 1.1 ryo vfpregs->__vfp_fstmx[i] = aux->vfp.ufp.fpregs[i]; 159 1.1 ryo vfpregs->__vfp_fpscr = aux->vfp.ufp.fpscr; 160 1.1 ryo 161 1.1 ryo uc.uc_flags |= _UC_FPU; 162 1.1 ryo } 163 1.1 ryo 164 1.2 ryo mutex_enter(p->p_lock); 165 1.2 ryo error = setucontext32(l, &uc); 166 1.2 ryo mutex_exit(p->p_lock); 167 1.2 ryo 168 1.2 ryo return error; 169 1.1 ryo } 170 1.1 ryo 171 1.1 ryo void 172 1.1 ryo linux32_sendsig(const ksiginfo_t *ksi, const sigset_t *mask) 173 1.1 ryo { 174 1.1 ryo struct lwp * const l = curlwp; 175 1.1 ryo struct proc * const p = l->l_proc; 176 1.1 ryo struct trapframe * const tf = lwp_trapframe(l); 177 1.1 ryo stack_t * const ss = &l->l_sigstk; 178 1.1 ryo const int sig = ksi->ksi_signo; 179 1.1 ryo const sig_t handler = SIGACTION(p, sig).sa_handler; 180 1.1 ryo struct linux32_rt_sigframe rt_sigframe_buf, *u_rt_sigframe; 181 1.1 ryo struct linux32_sigframe *tmp_sigframe, *u_sigframe; 182 1.1 ryo struct linux32_siginfo *tmp_siginfo, *u_siginfo; 183 1.1 ryo vaddr_t sp; 184 1.1 ryo int error; 185 1.1 ryo const bool onstack_p = /* use signal stack? */ 186 1.1 ryo (ss->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 && 187 1.1 ryo (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0; 188 1.1 ryo const bool rt_p = !!(SIGACTION(curproc, ksi->ksi_signo).sa_flags & 189 1.1 ryo SA_SIGINFO); 190 1.1 ryo 191 1.1 ryo sp = onstack_p ? ((vaddr_t)ss->ss_sp + ss->ss_size) & -16 : 192 1.1 ryo tf->tf_reg[13]; 193 1.1 ryo 194 1.1 ryo memset(&rt_sigframe_buf, 0, sizeof(rt_sigframe_buf)); 195 1.1 ryo 196 1.1 ryo if (rt_p) { 197 1.1 ryo /* allocate rt_sigframe on stack */ 198 1.1 ryo sp -= sizeof(struct linux32_rt_sigframe); 199 1.1 ryo u_rt_sigframe = (struct linux32_rt_sigframe *)sp; 200 1.1 ryo u_sigframe = &u_rt_sigframe->sig; 201 1.1 ryo u_siginfo = &u_rt_sigframe->info; 202 1.1 ryo tmp_sigframe = &rt_sigframe_buf.sig; 203 1.1 ryo tmp_siginfo = &rt_sigframe_buf.info; 204 1.1 ryo } else { 205 1.1 ryo /* allocate sigframe on stack */ 206 1.1 ryo sp -= sizeof(struct linux32_sigframe); 207 1.1 ryo u_rt_sigframe = NULL; 208 1.1 ryo u_sigframe = (struct linux32_sigframe *)sp; 209 1.1 ryo u_siginfo = NULL; 210 1.1 ryo tmp_sigframe = &rt_sigframe_buf.sig; 211 1.1 ryo tmp_siginfo = NULL; 212 1.1 ryo } 213 1.1 ryo 214 1.1 ryo if ((vaddr_t)sp >= VM_MAXUSER_ADDRESS32) { 215 1.1 ryo sigexit(l, SIGILL); 216 1.1 ryo return; 217 1.1 ryo } 218 1.1 ryo 219 1.1 ryo /* build linux sigframe, and copyout to user stack */ 220 1.1 ryo tmp_sigframe->uc.luc_flags = 0; 221 1.1 ryo tmp_sigframe->uc.luc_link = NULL; 222 1.1 ryo NETBSD32PTR32(tmp_sigframe->uc.luc_stack.ss_sp, ss->ss_sp); 223 1.1 ryo tmp_sigframe->uc.luc_stack.ss_size = ss->ss_size; 224 1.1 ryo tmp_sigframe->uc.luc_stack.ss_flags = 0; 225 1.1 ryo if (ss->ss_flags & SS_ONSTACK) 226 1.1 ryo tmp_sigframe->uc.luc_stack.ss_flags |= LINUX_SS_ONSTACK; 227 1.1 ryo if (ss->ss_flags & SS_DISABLE) 228 1.1 ryo tmp_sigframe->uc.luc_stack.ss_flags |= LINUX_SS_DISABLE; 229 1.1 ryo native_to_linux32_sigset(&tmp_sigframe->uc.luc_sigmask, mask); 230 1.1 ryo if (tmp_siginfo != NULL) 231 1.1 ryo native_to_linux32_siginfo(tmp_siginfo, &ksi->ksi_info); 232 1.1 ryo sendsig_reset(l, sig); 233 1.1 ryo 234 1.1 ryo mutex_exit(p->p_lock); 235 1.1 ryo linux32_save_sigcontext(l, &tmp_sigframe->uc); 236 1.1 ryo 237 1.1 ryo /* copy linux sigframe onto the user stack */ 238 1.1 ryo if (rt_p) { 239 1.1 ryo error = copyout(&rt_sigframe_buf, u_rt_sigframe, 240 1.1 ryo sizeof(rt_sigframe_buf)); 241 1.1 ryo } else { 242 1.1 ryo error = copyout(tmp_sigframe, u_sigframe, 243 1.1 ryo sizeof(*tmp_sigframe)); 244 1.1 ryo } 245 1.1 ryo 246 1.1 ryo mutex_enter(p->p_lock); 247 1.1 ryo 248 1.1 ryo if (error != 0 || (vaddr_t)handler >= VM_MAXUSER_ADDRESS32) { 249 1.1 ryo sigexit(l, SIGILL); 250 1.1 ryo return; 251 1.1 ryo } 252 1.1 ryo 253 1.1 ryo /* build context to run handler in. */ 254 1.1 ryo tf->tf_reg[0] = native_to_linux_signo[sig]; 255 1.1 ryo tf->tf_reg[1] = NETBSD32PTR32I(u_siginfo); 256 1.1 ryo tf->tf_reg[2] = NETBSD32PTR32I(&u_sigframe->uc); 257 1.1 ryo tf->tf_pc = (uint64_t)handler; 258 1.1 ryo tf->tf_reg[13] = sp; 259 1.1 ryo 260 1.1 ryo /* sigreturn trampoline */ 261 1.1 ryo extern char linux32_sigcode[], linux32_rt_sigcode[]; 262 1.1 ryo vsize_t linux_sigcode_offset; 263 1.1 ryo if (rt_p) { 264 1.1 ryo /* set return address to linux32_rt_sigcode() */ 265 1.1 ryo linux_sigcode_offset = linux32_rt_sigcode - linux32_sigcode; 266 1.1 ryo } else { 267 1.1 ryo /* set return address to linux32_sigcode() */ 268 1.1 ryo linux_sigcode_offset = linux32_sigcode - linux32_sigcode; 269 1.1 ryo } 270 1.1 ryo /* tf->tf_reg[14] is aarch32 lr */ 271 1.1 ryo tf->tf_reg[14] = NETBSD32PTR32I((char *)p->p_sigctx.ps_sigcode + 272 1.1 ryo linux_sigcode_offset); 273 1.1 ryo 274 1.1 ryo if (onstack_p) 275 1.1 ryo ss->ss_flags |= SS_ONSTACK; 276 1.1 ryo } 277 1.1 ryo 278 1.1 ryo int 279 1.1 ryo linux32_sys_sigreturn(struct lwp *l, 280 1.1 ryo const struct linux32_sys_sigreturn_args *uap, register_t *retval) 281 1.1 ryo { 282 1.1 ryo struct trapframe * const tf = lwp_trapframe(l); 283 1.1 ryo struct linux32_sigframe sigframe; 284 1.1 ryo int error; 285 1.1 ryo 286 1.1 ryo error = copyin((void *)tf->tf_reg[13], &sigframe, sizeof(sigframe)); 287 1.1 ryo if (error != 0) 288 1.1 ryo goto done; 289 1.1 ryo 290 1.1 ryo error = linux32_restore_sigcontext(l, &sigframe.uc); 291 1.1 ryo if (error != 0) 292 1.1 ryo goto done; 293 1.1 ryo error = EJUSTRETURN; 294 1.1 ryo 295 1.1 ryo done: 296 1.1 ryo return error; 297 1.1 ryo } 298 1.1 ryo 299 1.1 ryo int 300 1.1 ryo linux32_sys_rt_sigreturn(struct lwp *l, 301 1.1 ryo const struct linux32_sys_rt_sigreturn_args *uap, register_t *retval) 302 1.1 ryo { 303 1.1 ryo struct trapframe * const tf = lwp_trapframe(l); 304 1.1 ryo struct linux32_rt_sigframe rt_sigframe; 305 1.1 ryo int error; 306 1.1 ryo 307 1.1 ryo error = copyin((void *)tf->tf_reg[13], &rt_sigframe, 308 1.1 ryo sizeof(rt_sigframe)); 309 1.1 ryo if (error != 0) 310 1.1 ryo goto done; 311 1.1 ryo 312 1.1 ryo error = linux32_restore_sigcontext(l, &rt_sigframe.sig.uc); 313 1.1 ryo if (error != 0) 314 1.1 ryo goto done; 315 1.1 ryo error = EJUSTRETURN; 316 1.1 ryo 317 1.1 ryo done: 318 1.1 ryo return error; 319 1.1 ryo } 320 1.1 ryo 321 1.1 ryo void 322 1.1 ryo linux32_setregs(struct lwp *l, struct exec_package *pack, u_long stack) 323 1.1 ryo { 324 1.1 ryo struct trapframe * const tf = lwp_trapframe(l); 325 1.1 ryo 326 1.1 ryo netbsd32_setregs(l, pack, stack); 327 1.1 ryo 328 1.1 ryo /* Same as netbsd32_setregs(), but some registers are set for linux */ 329 1.1 ryo tf->tf_reg[0] = 0; 330 1.1 ryo tf->tf_reg[12] = 0; 331 1.1 ryo tf->tf_reg[13] = stack; /* sp */ 332 1.1 ryo tf->tf_reg[14] = pack->ep_entry;/* lr */ 333 1.1 ryo tf->tf_reg[18] = 0; 334 1.1 ryo tf->tf_pc = pack->ep_entry; 335 1.1 ryo } 336 1.1 ryo 337 1.1 ryo int 338 1.1 ryo linux32_sys_set_tls(struct lwp *l, const struct linux32_sys_set_tls_args *uap, 339 1.1 ryo register_t *retval) 340 1.1 ryo { 341 1.1 ryo /* { 342 1.1 ryo syscallarg(netbsd32_voidp) tls; 343 1.1 ryo } */ 344 1.1 ryo return lwp_setprivate(l, SCARG_P32(uap, tls)); 345 1.1 ryo } 346 1.1 ryo 347 1.1 ryo int 348 1.1 ryo linux32_sys_get_tls(struct lwp *l, const void *uap, register_t *retval) 349 1.1 ryo { 350 1.1 ryo retval[0] = NETBSD32PTR32I(l->l_private); 351 1.1 ryo return 0; 352 1.1 ryo } 353