1 /* $NetBSD: linux32_machdep.c,v 1.48 2023/06/20 15:22:15 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Emmanuel Dreyfus 17 * 4. The name of the author may not be used to endorse or promote 18 * products derived from this software without specific prior written 19 * permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS'' 22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: linux32_machdep.c,v 1.48 2023/06/20 15:22:15 riastradh Exp $"); 35 36 #if defined(_KERNEL_OPT) 37 #include "opt_user_ldt.h" 38 #endif 39 40 #include <sys/param.h> 41 #include <sys/proc.h> 42 #include <sys/exec.h> 43 44 #include <machine/vmparam.h> 45 #include <machine/cpufunc.h> 46 #include <machine/netbsd32_machdep.h> 47 48 #include <x86/fpu.h> 49 50 #include <compat/netbsd32/netbsd32.h> 51 #include <compat/netbsd32/netbsd32_syscallargs.h> 52 53 #include <compat/linux/common/linux_types.h> 54 #include <compat/linux/common/linux_emuldata.h> 55 #include <compat/linux/common/linux_signal.h> 56 #include <compat/linux/common/linux_errno.h> 57 #include <compat/linux/common/linux_exec.h> 58 #include <compat/linux/common/linux_ipc.h> 59 #include <compat/linux/common/linux_sem.h> 60 #include <compat/linux/linux_syscallargs.h> 61 62 #include <compat/linux32/common/linux32_types.h> 63 #include <compat/linux32/common/linux32_errno.h> 64 #include <compat/linux32/common/linux32_machdep.h> 65 #include <compat/linux32/common/linux32_signal.h> 66 #include <compat/linux32/common/linux32_exec.h> 67 #include <compat/linux32/common/linux32_ipc.h> 68 #include <compat/linux32/common/linux32_sem.h> 69 #include <compat/linux32/linux32_syscallargs.h> 70 71 #ifdef DEBUG_LINUX 72 #define DPRINTF(a) uprintf a 73 #else 74 #define DPRINTF(a) 75 #endif 76 77 extern char linux32_sigcode[]; 78 extern char linux32_rt_sigcode[]; 79 extern char linux32_esigcode[]; 80 81 static void linux32_save_ucontext(struct lwp *, struct trapframe *, 82 const sigset_t *, stack_t *, struct linux32_ucontext *); 83 static void linux32_save_sigcontext(struct lwp *, struct trapframe *, 84 const sigset_t *, struct linux32_sigcontext *); 85 static void linux32_rt_sendsig(const ksiginfo_t *, const sigset_t *); 86 static void linux32_old_sendsig(const ksiginfo_t *, const sigset_t *); 87 static int linux32_restore_sigcontext(struct lwp *, 88 struct linux32_sigcontext *, register_t *); 89 90 void 91 linux32_sendsig(const ksiginfo_t *ksi, const sigset_t *mask) 92 { 93 if (SIGACTION(curproc, ksi->ksi_signo).sa_flags & SA_SIGINFO) 94 linux32_rt_sendsig(ksi, mask); 95 else 96 linux32_old_sendsig(ksi, mask); 97 return; 98 } 99 100 void 101 linux32_old_sendsig(const ksiginfo_t *ksi, const sigset_t *mask) 102 { 103 struct lwp *l = curlwp; 104 struct proc *p = l->l_proc; 105 struct trapframe *tf; 106 struct linux32_sigframe *fp, frame; 107 int onstack, error; 108 int sig = ksi->ksi_signo; 109 sig_t catcher = SIGACTION(p, sig).sa_handler; 110 stack_t *sas = &l->l_sigstk; 111 112 tf = l->l_md.md_regs; 113 /* Do we need to jump onto the signal stack? */ 114 onstack = (sas->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 && 115 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0; 116 117 118 /* Allocate space for the signal handler context. */ 119 if (onstack) 120 fp = (struct linux32_sigframe *)((char *)sas->ss_sp + 121 sas->ss_size); 122 else 123 fp = (struct linux32_sigframe *)tf->tf_rsp; 124 fp--; 125 126 DPRINTF(("old: onstack = %d, fp = %p sig = %d rip = 0x%lx\n", 127 onstack, fp, sig, tf->tf_rip)); 128 129 memset(&frame, 0, sizeof(frame)); 130 131 /* Build stack frame for signal trampoline. */ 132 NETBSD32PTR32(frame.sf_handler, catcher); 133 frame.sf_sig = native_to_linux32_signo[sig]; 134 135 linux32_save_sigcontext(l, tf, mask, &frame.sf_sc); 136 137 sendsig_reset(l, sig); 138 mutex_exit(p->p_lock); 139 error = copyout(&frame, fp, sizeof(frame)); 140 mutex_enter(p->p_lock); 141 142 if (error != 0) { 143 /* 144 * Process has trashed its stack; give it an illegal 145 * instruction to halt it in its tracks. 146 */ 147 sigexit(l, SIGILL); 148 /* NOTREACHED */ 149 } 150 151 /* 152 * Build context to run handler in. 153 */ 154 tf->tf_fs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 155 tf->tf_es = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 156 tf->tf_ds = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 157 tf->tf_rip = ((long)p->p_sigctx.ps_sigcode) & 0xffffffff; 158 tf->tf_cs = GSEL(GUCODE32_SEL, SEL_UPL) & 0xffffffff; 159 tf->tf_rflags &= ~PSL_CLEARSIG & 0xffffffff; 160 tf->tf_rsp = (long)fp & 0xffffffff; 161 tf->tf_ss = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 162 163 /* Remember that we're now on the signal stack. */ 164 if (onstack) 165 sas->ss_flags |= SS_ONSTACK; 166 167 return; 168 } 169 170 void 171 linux32_rt_sendsig(const ksiginfo_t *ksi, const sigset_t *mask) 172 { 173 struct lwp *l = curlwp; 174 struct proc *p = l->l_proc; 175 struct trapframe *tf; 176 struct linux32_rt_sigframe *fp, frame; 177 int onstack, error; 178 linux32_siginfo_t *lsi; 179 int sig = ksi->ksi_signo; 180 sig_t catcher = SIGACTION(p, sig).sa_handler; 181 stack_t *sas = &l->l_sigstk; 182 183 tf = l->l_md.md_regs; 184 /* Do we need to jump onto the signal stack? */ 185 onstack = (sas->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 && 186 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0; 187 188 189 /* Allocate space for the signal handler context. */ 190 if (onstack) 191 fp = (struct linux32_rt_sigframe *)((char *)sas->ss_sp + 192 sas->ss_size); 193 else 194 fp = (struct linux32_rt_sigframe *)tf->tf_rsp; 195 fp--; 196 197 /* Build stack frame for signal trampoline. */ 198 memset(&frame, 0, sizeof(frame)); 199 NETBSD32PTR32(frame.sf_handler, catcher); 200 frame.sf_sig = native_to_linux32_signo[sig]; 201 NETBSD32PTR32(frame.sf_sip, &fp->sf_si); 202 NETBSD32PTR32(frame.sf_ucp, &fp->sf_uc); 203 204 DPRINTF(("rt: onstack = %d, fp = %p sig = %d rip = 0x%lx\n", 205 onstack, fp, sig, tf->tf_rip)); 206 207 lsi = &frame.sf_si; 208 (void)memset(lsi, 0, sizeof(frame.sf_si)); 209 lsi->lsi_errno = native_to_linux32_errno[ksi->ksi_errno]; 210 lsi->lsi_code = native_to_linux_si_code(ksi->ksi_code); 211 lsi->lsi_signo = frame.sf_sig; 212 switch (lsi->lsi_signo) { 213 case LINUX32_SIGILL: 214 case LINUX32_SIGFPE: 215 case LINUX32_SIGSEGV: 216 case LINUX32_SIGBUS: 217 case LINUX32_SIGTRAP: 218 NETBSD32PTR32(lsi->lsi_addr, ksi->ksi_addr); 219 break; 220 case LINUX32_SIGCHLD: 221 lsi->lsi_uid = ksi->ksi_uid; 222 lsi->lsi_pid = ksi->ksi_pid; 223 lsi->lsi_utime = ksi->ksi_utime; 224 lsi->lsi_stime = ksi->ksi_stime; 225 lsi->lsi_status = native_to_linux_si_status(ksi->ksi_code, 226 ksi->ksi_status); 227 break; 228 case LINUX32_SIGIO: 229 lsi->lsi_band = ksi->ksi_band; 230 lsi->lsi_fd = ksi->ksi_fd; 231 break; 232 default: 233 lsi->lsi_uid = ksi->ksi_uid; 234 lsi->lsi_pid = ksi->ksi_pid; 235 if (lsi->lsi_signo == LINUX32_SIGALRM || 236 lsi->lsi_signo >= LINUX32_SIGRTMIN) 237 NETBSD32PTR32(lsi->lsi_value.sival_ptr, 238 ksi->ksi_value.sival_ptr); 239 break; 240 } 241 242 /* Save register context. */ 243 linux32_save_ucontext(l, tf, mask, sas, &frame.sf_uc); 244 sendsig_reset(l, sig); 245 mutex_exit(p->p_lock); 246 error = copyout(&frame, fp, sizeof(frame)); 247 mutex_enter(p->p_lock); 248 249 if (error != 0) { 250 /* 251 * Process has trashed its stack; give it an illegal 252 * instruction to halt it in its tracks. 253 */ 254 sigexit(l, SIGILL); 255 /* NOTREACHED */ 256 } 257 258 /* 259 * Build context to run handler in. 260 */ 261 tf->tf_fs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 262 tf->tf_es = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 263 tf->tf_ds = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 264 tf->tf_rip = (((long)p->p_sigctx.ps_sigcode) + 265 (linux32_rt_sigcode - linux32_sigcode)) & 0xffffffff; 266 tf->tf_cs = GSEL(GUCODE32_SEL, SEL_UPL) & 0xffffffff; 267 tf->tf_rflags &= ~PSL_CLEARSIG & 0xffffffff; 268 tf->tf_rsp = (long)fp & 0xffffffff; 269 tf->tf_ss = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 270 271 /* Remember that we're now on the signal stack. */ 272 if (onstack) 273 sas->ss_flags |= SS_ONSTACK; 274 275 return; 276 } 277 278 void 279 linux32_setregs(struct lwp *l, struct exec_package *pack, u_long stack) 280 { 281 struct pcb *pcb = lwp_getpcb(l); 282 struct trapframe *tf; 283 struct proc *p = l->l_proc; 284 285 #if defined(USER_LDT) 286 pmap_ldt_cleanup(l); 287 #endif 288 289 netbsd32_adjust_limits(p); 290 291 fpu_clear(l, __Linux_NPXCW__); 292 293 kpreempt_disable(); 294 pcb->pcb_flags = PCB_COMPAT32; 295 p->p_flag |= PK_32; 296 l->l_md.md_flags = MDL_COMPAT32; /* force iret not sysret */ 297 cpu_segregs32_zero(l); 298 cpu_fsgs_reload(l, GSEL(GUDATA32_SEL, SEL_UPL), 299 GSEL(GUDATA32_SEL, SEL_UPL)); 300 kpreempt_enable(); 301 302 tf = l->l_md.md_regs; 303 tf->tf_rax = 0; 304 tf->tf_rbx = (u_int32_t)p->p_psstrp; 305 tf->tf_rcx = pack->ep_entry & 0xffffffff; 306 tf->tf_rdx = 0; 307 tf->tf_rsi = 0; 308 tf->tf_rdi = 0; 309 tf->tf_rbp = 0; 310 tf->tf_rsp = stack & 0xffffffff; 311 tf->tf_r8 = 0; 312 tf->tf_r9 = 0; 313 tf->tf_r10 = 0; 314 tf->tf_r11 = 0; 315 tf->tf_r12 = 0; 316 tf->tf_r13 = 0; 317 tf->tf_r14 = 0; 318 tf->tf_r15 = 0; 319 tf->tf_rip = pack->ep_entry & 0xffffffff; 320 tf->tf_rflags = PSL_USERSET; 321 tf->tf_cs = GSEL(GUCODE32_SEL, SEL_UPL); 322 tf->tf_ss = GSEL(GUDATA32_SEL, SEL_UPL); 323 tf->tf_ds = GSEL(GUDATA32_SEL, SEL_UPL); 324 tf->tf_es = GSEL(GUDATA32_SEL, SEL_UPL); 325 } 326 327 static void 328 linux32_save_ucontext(struct lwp *l, struct trapframe *tf, 329 const sigset_t *mask, stack_t *sas, struct linux32_ucontext *uc) 330 { 331 332 uc->uc_flags = 0; 333 NETBSD32PTR32(uc->uc_link, NULL); 334 native_to_linux32_sigaltstack(&uc->uc_stack, sas); 335 linux32_save_sigcontext(l, tf, mask, &uc->uc_mcontext); 336 native_to_linux32_sigset(&uc->uc_sigmask, mask); 337 (void)memset(&uc->uc_fpregs_mem, 0, sizeof(uc->uc_fpregs_mem)); 338 } 339 340 static void 341 linux32_save_sigcontext(struct lwp *l, struct trapframe *tf, 342 const sigset_t *mask, struct linux32_sigcontext *sc) 343 { 344 struct pcb *pcb = lwp_getpcb(l); 345 346 /* Save register context. */ 347 sc->sc_gs = tf->tf_gs & 0xFFFF; 348 sc->sc_fs = tf->tf_fs & 0xFFFF; 349 sc->sc_es = tf->tf_es & 0xFFFF; 350 sc->sc_ds = tf->tf_ds & 0xFFFF; 351 sc->sc_eflags = tf->tf_rflags; 352 sc->sc_edi = tf->tf_rdi; 353 sc->sc_esi = tf->tf_rsi; 354 sc->sc_esp = tf->tf_rsp; 355 sc->sc_ebp = tf->tf_rbp; 356 sc->sc_ebx = tf->tf_rbx; 357 sc->sc_edx = tf->tf_rdx; 358 sc->sc_ecx = tf->tf_rcx; 359 sc->sc_eax = tf->tf_rax; 360 sc->sc_eip = tf->tf_rip; 361 sc->sc_cs = tf->tf_cs & 0xFFFF; 362 sc->sc_esp_at_signal = tf->tf_rsp; 363 sc->sc_ss = tf->tf_ss & 0xFFFF; 364 sc->sc_err = tf->tf_err; 365 sc->sc_trapno = tf->tf_trapno; 366 sc->sc_cr2 = pcb->pcb_cr2; 367 NETBSD32PTR32(sc->sc_387, NULL); 368 369 /* Save signal stack. */ 370 /* Linux doesn't save the onstack flag in sigframe */ 371 372 /* Save signal mask. */ 373 native_to_linux32_old_sigset(&sc->sc_mask, mask); 374 } 375 376 int 377 linux32_sys_sigreturn(struct lwp *l, 378 const struct linux32_sys_sigreturn_args *uap, register_t *retval) 379 { 380 /* { 381 syscallarg(linux32_sigcontextp_t) scp; 382 } */ 383 struct linux32_sigcontext ctx; 384 int error; 385 386 if ((error = copyin(SCARG_P32(uap, scp), &ctx, sizeof(ctx))) != 0) 387 return error; 388 389 return linux32_restore_sigcontext(l, &ctx, retval); 390 } 391 392 int 393 linux32_sys_rt_sigreturn(struct lwp *l, 394 const struct linux32_sys_rt_sigreturn_args *uap, register_t *retval) 395 { 396 /* { 397 syscallarg(linux32_ucontextp_t) ucp; 398 } */ 399 struct linux32_ucontext ctx; 400 int error; 401 402 if ((error = copyin(SCARG_P32(uap, ucp), &ctx, sizeof(ctx))) != 0) 403 return error; 404 405 return linux32_restore_sigcontext(l, &ctx.uc_mcontext, retval); 406 } 407 408 static int 409 linux32_restore_sigcontext(struct lwp *l, struct linux32_sigcontext *scp, 410 register_t *retval) 411 { 412 struct trapframe *tf; 413 struct proc *p = l->l_proc; 414 stack_t *sas = &l->l_sigstk; 415 struct pcb *pcb; 416 sigset_t mask; 417 ssize_t ss_gap; 418 register_t fssel, gssel; 419 420 /* Restore register context. */ 421 tf = l->l_md.md_regs; 422 pcb = lwp_getpcb(l); 423 DPRINTF(("sigreturn enter rsp=0x%lx rip=0x%lx\n", tf->tf_rsp, 424 tf->tf_rip)); 425 426 /* 427 * Check for security violations. 428 */ 429 if (((scp->sc_eflags ^ tf->tf_rflags) & PSL_USERSTATIC) != 0) 430 return EINVAL; 431 if (!VALID_USER_CSEL32(scp->sc_cs)) 432 return EINVAL; 433 434 if (scp->sc_fs != 0 && !VALID_USER_DSEL32(scp->sc_fs) && 435 !(VALID_USER_FSEL32(scp->sc_fs) && pcb->pcb_fs != 0)) 436 return EINVAL; 437 438 if (scp->sc_gs != 0 && !VALID_USER_DSEL32(scp->sc_gs) && 439 !(VALID_USER_GSEL32(scp->sc_gs) && pcb->pcb_gs != 0)) 440 return EINVAL; 441 442 if (scp->sc_es != 0 && !VALID_USER_DSEL32(scp->sc_es)) 443 return EINVAL; 444 445 if (!VALID_USER_DSEL32(scp->sc_ds) || 446 !VALID_USER_DSEL32(scp->sc_ss)) 447 return EINVAL; 448 449 if (scp->sc_eip >= VM_MAXUSER_ADDRESS32) 450 return EINVAL; 451 452 gssel = (register_t)scp->sc_gs & 0xffff; 453 fssel = (register_t)scp->sc_fs & 0xffff; 454 cpu_fsgs_reload(l, fssel, gssel); 455 tf->tf_es = (register_t)scp->sc_es & 0xffff; 456 tf->tf_ds = (register_t)scp->sc_ds & 0xffff; 457 tf->tf_rflags &= ~PSL_USER; 458 tf->tf_rflags |= ((register_t)scp->sc_eflags & PSL_USER); 459 tf->tf_rdi = (register_t)scp->sc_edi & 0xffffffff; 460 tf->tf_rsi = (register_t)scp->sc_esi & 0xffffffff; 461 tf->tf_rbp = (register_t)scp->sc_ebp & 0xffffffff; 462 tf->tf_rbx = (register_t)scp->sc_ebx & 0xffffffff; 463 tf->tf_rdx = (register_t)scp->sc_edx & 0xffffffff; 464 tf->tf_rcx = (register_t)scp->sc_ecx & 0xffffffff; 465 tf->tf_rax = (register_t)scp->sc_eax & 0xffffffff; 466 tf->tf_rip = (register_t)scp->sc_eip & 0xffffffff; 467 tf->tf_cs = (register_t)scp->sc_cs & 0xffff; 468 tf->tf_rsp = (register_t)scp->sc_esp_at_signal & 0xffffffff; 469 tf->tf_ss = (register_t)scp->sc_ss & 0xffff; 470 471 mutex_enter(p->p_lock); 472 473 /* Restore signal stack. */ 474 ss_gap = (ssize_t) 475 ((char *)NETBSD32IPTR64(scp->sc_esp_at_signal) 476 - (char *)sas->ss_sp); 477 if (ss_gap >= 0 && ss_gap < sas->ss_size) 478 sas->ss_flags |= SS_ONSTACK; 479 else 480 sas->ss_flags &= ~SS_ONSTACK; 481 482 /* Restore signal mask. */ 483 linux32_old_to_native_sigset(&mask, &scp->sc_mask); 484 (void) sigprocmask1(l, SIG_SETMASK, &mask, 0); 485 486 mutex_exit(p->p_lock); 487 488 DPRINTF(("linux32_sigreturn: rip = 0x%lx, rsp = 0x%lx, flags = 0x%lx\n", 489 tf->tf_rip, tf->tf_rsp, tf->tf_rflags)); 490 return EJUSTRETURN; 491 } 492 493 int 494 linux32_sys_set_thread_area(struct lwp *l, 495 const struct linux32_sys_set_thread_area_args *uap, register_t *retval) 496 { 497 /* { 498 syscallarg(linux32_user_descp_t) desc; 499 } */ 500 501 return linux_lwp_setprivate(l, SCARG_P32(uap, desc)); 502 } 503 504 int 505 linux32_sys_get_thread_area(struct lwp *l, 506 const struct linux32_sys_get_thread_area_args *uap, register_t *retval) 507 { 508 /* { 509 syscallarg(linux32_user_descp_t) desc; 510 } */ 511 512 /* glibc doesn't actually call this. */ 513 return ENOSYS; 514 } 515 516 int 517 linux32_sys_modify_ldt(struct lwp *l, const struct linux32_sys_modify_ldt_args *uap, register_t *retval) 518 { 519 /* { 520 syscallarg(int) func; 521 syscallarg(netbsd32_charp) ptr; 522 syscallarg(netbsd32_size_t) bytecount; 523 } */ 524 struct linux_sys_modify_ldt_args ua; 525 526 NETBSD32TO64_UAP(func); 527 NETBSD32TOP_UAP(ptr, void *); 528 NETBSD32TOX_UAP(bytecount, size_t); 529 return linux_sys_modify_ldt(l, &ua, retval); 530 } 531