1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2004 Tim J. Robbins 5 * Copyright (c) 2003 Peter Wemm 6 * Copyright (c) 2002 Doug Rabson 7 * Copyright (c) 1998-1999 Andrew Gallatin 8 * Copyright (c) 1994-1996 Søren Schmidt 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer 16 * in this position and unchanged. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #ifndef COMPAT_FREEBSD32 36 #error "Unable to compile Linux-emulator due to missing COMPAT_FREEBSD32 option!" 37 #endif 38 39 #define __ELF_WORD_SIZE 32 40 41 #include <sys/param.h> 42 #include <sys/exec.h> 43 #include <sys/fcntl.h> 44 #include <sys/imgact.h> 45 #include <sys/imgact_elf.h> 46 #include <sys/kernel.h> 47 #include <sys/lock.h> 48 #include <sys/malloc.h> 49 #include <sys/module.h> 50 #include <sys/mutex.h> 51 #include <sys/proc.h> 52 #include <sys/stddef.h> 53 #include <sys/syscallsubr.h> 54 #include <sys/sysctl.h> 55 #include <sys/sysent.h> 56 57 #include <vm/pmap.h> 58 #include <vm/vm.h> 59 #include <vm/vm_map.h> 60 #include <vm/vm_page.h> 61 62 #include <machine/cpu.h> 63 #include <machine/md_var.h> 64 #include <machine/pcb.h> 65 #include <machine/specialreg.h> 66 #include <machine/trap.h> 67 68 #include <x86/linux/linux_x86.h> 69 #include <amd64/linux32/linux.h> 70 #include <amd64/linux32/linux32_proto.h> 71 #include <compat/linux/linux_elf.h> 72 #include <compat/linux/linux_emul.h> 73 #include <compat/linux/linux_fork.h> 74 #include <compat/linux/linux_ioctl.h> 75 #include <compat/linux/linux_mib.h> 76 #include <compat/linux/linux_misc.h> 77 #include <compat/linux/linux_signal.h> 78 #include <compat/linux/linux_util.h> 79 #include <compat/linux/linux_vdso.h> 80 81 #include <x86/linux/linux_x86_sigframe.h> 82 83 MODULE_VERSION(linux, 1); 84 85 #define LINUX32_MAXUSER ((1ul << 32) - PAGE_SIZE) 86 #define LINUX32_VDSOPAGE_SIZE PAGE_SIZE * 2 87 #define LINUX32_VDSOPAGE (LINUX32_MAXUSER - LINUX32_VDSOPAGE_SIZE) 88 #define LINUX32_SHAREDPAGE (LINUX32_VDSOPAGE - PAGE_SIZE) 89 /* 90 * PAGE_SIZE - the size 91 * of the native SHAREDPAGE 92 */ 93 #define LINUX32_USRSTACK LINUX32_SHAREDPAGE 94 95 static int linux_szsigcode; 96 static vm_object_t linux_vdso_obj; 97 static char *linux_vdso_mapping; 98 extern char _binary_linux32_vdso_so_o_start; 99 extern char _binary_linux32_vdso_so_o_end; 100 static vm_offset_t linux_vdso_base; 101 102 extern struct sysent linux32_sysent[LINUX32_SYS_MAXSYSCALL]; 103 extern const char *linux32_syscallnames[]; 104 105 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler); 106 107 static int linux_copyout_strings(struct image_params *imgp, 108 uintptr_t *stack_base); 109 static void linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask); 110 static void linux_exec_setregs(struct thread *td, 111 struct image_params *imgp, uintptr_t stack); 112 static void linux_exec_sysvec_init(void *param); 113 static int linux_on_exec_vmspace(struct proc *p, 114 struct image_params *imgp); 115 static void linux32_fixlimit(struct rlimit *rl, int which); 116 static void linux_vdso_install(const void *param); 117 static void linux_vdso_deinstall(const void *param); 118 static void linux_vdso_reloc(char *mapping, Elf_Addr offset); 119 static void linux32_set_fork_retval(struct thread *td); 120 static void linux32_set_syscall_retval(struct thread *td, int error); 121 122 struct linux32_ps_strings { 123 u_int32_t ps_argvstr; /* first of 0 or more argument strings */ 124 u_int ps_nargvstr; /* the number of argument strings */ 125 u_int32_t ps_envstr; /* first of 0 or more environment strings */ 126 u_int ps_nenvstr; /* the number of environment strings */ 127 }; 128 #define LINUX32_PS_STRINGS (LINUX32_USRSTACK - \ 129 sizeof(struct linux32_ps_strings)) 130 131 LINUX_VDSO_SYM_INTPTR(__kernel_vsyscall); 132 LINUX_VDSO_SYM_INTPTR(linux32_vdso_sigcode); 133 LINUX_VDSO_SYM_INTPTR(linux32_vdso_rt_sigcode); 134 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base); 135 LINUX_VDSO_SYM_INTPTR(kern_tsc_selector); 136 LINUX_VDSO_SYM_INTPTR(kern_cpu_selector); 137 LINUX_VDSO_SYM_CHAR(linux_platform); 138 139 void 140 linux32_arch_copyout_auxargs(struct image_params *imgp, Elf_Auxinfo **pos) 141 { 142 143 AUXARGS_ENTRY((*pos), LINUX_AT_SYSINFO, __kernel_vsyscall); 144 AUXARGS_ENTRY((*pos), LINUX_AT_SYSINFO_EHDR, linux_vdso_base); 145 AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP, cpu_feature); 146 AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP2, linux_x86_elf_hwcap2()); 147 AUXARGS_ENTRY((*pos), LINUX_AT_PLATFORM, PTROUT(linux_platform)); 148 } 149 150 static void 151 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 152 { 153 struct thread *td = curthread; 154 struct proc *p = td->td_proc; 155 struct sigacts *psp; 156 struct trapframe *regs; 157 struct l_rt_sigframe *fp, frame; 158 int oonstack; 159 int sig; 160 int code; 161 162 sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno); 163 code = ksi->ksi_code; 164 PROC_LOCK_ASSERT(p, MA_OWNED); 165 psp = p->p_sigacts; 166 mtx_assert(&psp->ps_mtx, MA_OWNED); 167 regs = td->td_frame; 168 oonstack = sigonstack(regs->tf_rsp); 169 170 /* Allocate space for the signal handler context. */ 171 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack && 172 SIGISMEMBER(psp->ps_sigonstack, sig)) { 173 fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp + 174 td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe)); 175 } else 176 fp = (struct l_rt_sigframe *)regs->tf_rsp - 1; 177 mtx_unlock(&psp->ps_mtx); 178 179 /* Build the argument list for the signal handler. */ 180 sig = bsd_to_linux_signal(sig); 181 182 bzero(&frame, sizeof(frame)); 183 184 frame.sf_sig = sig; 185 frame.sf_siginfo = PTROUT(&fp->sf_si); 186 frame.sf_ucontext = PTROUT(&fp->sf_uc); 187 188 /* Fill in POSIX parts. */ 189 siginfo_to_lsiginfo(&ksi->ksi_info, &frame.sf_si, sig); 190 191 /* 192 * Build the signal context to be used by sigreturn and libgcc unwind. 193 */ 194 frame.sf_uc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp); 195 frame.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size; 196 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) 197 ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE; 198 PROC_UNLOCK(p); 199 200 bsd_to_linux_sigset(mask, &frame.sf_uc.uc_sigmask); 201 202 frame.sf_uc.uc_mcontext.sc_mask = frame.sf_uc.uc_sigmask.__mask; 203 frame.sf_uc.uc_mcontext.sc_edi = regs->tf_rdi; 204 frame.sf_uc.uc_mcontext.sc_esi = regs->tf_rsi; 205 frame.sf_uc.uc_mcontext.sc_ebp = regs->tf_rbp; 206 frame.sf_uc.uc_mcontext.sc_ebx = regs->tf_rbx; 207 frame.sf_uc.uc_mcontext.sc_esp = regs->tf_rsp; 208 frame.sf_uc.uc_mcontext.sc_edx = regs->tf_rdx; 209 frame.sf_uc.uc_mcontext.sc_ecx = regs->tf_rcx; 210 frame.sf_uc.uc_mcontext.sc_eax = regs->tf_rax; 211 frame.sf_uc.uc_mcontext.sc_eip = regs->tf_rip; 212 frame.sf_uc.uc_mcontext.sc_cs = regs->tf_cs; 213 frame.sf_uc.uc_mcontext.sc_gs = regs->tf_gs; 214 frame.sf_uc.uc_mcontext.sc_fs = regs->tf_fs; 215 frame.sf_uc.uc_mcontext.sc_es = regs->tf_es; 216 frame.sf_uc.uc_mcontext.sc_ds = regs->tf_ds; 217 frame.sf_uc.uc_mcontext.sc_eflags = regs->tf_rflags; 218 frame.sf_uc.uc_mcontext.sc_esp_at_signal = regs->tf_rsp; 219 frame.sf_uc.uc_mcontext.sc_ss = regs->tf_ss; 220 frame.sf_uc.uc_mcontext.sc_err = regs->tf_err; 221 frame.sf_uc.uc_mcontext.sc_cr2 = (u_int32_t)(uintptr_t)ksi->ksi_addr; 222 frame.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code); 223 224 if (copyout(&frame, fp, sizeof(frame)) != 0) { 225 /* 226 * Process has trashed its stack; give it an illegal 227 * instruction to halt it in its tracks. 228 */ 229 PROC_LOCK(p); 230 sigexit(td, SIGILL); 231 } 232 233 /* Build context to run handler in. */ 234 regs->tf_rsp = PTROUT(fp); 235 regs->tf_rip = linux32_vdso_rt_sigcode; 236 regs->tf_rdi = PTROUT(catcher); 237 regs->tf_rflags &= ~(PSL_T | PSL_D); 238 regs->tf_cs = _ucode32sel; 239 regs->tf_ss = _udatasel; 240 regs->tf_ds = _udatasel; 241 regs->tf_es = _udatasel; 242 regs->tf_fs = _ufssel; 243 regs->tf_gs = _ugssel; 244 regs->tf_flags = TF_HASSEGS; 245 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 246 PROC_LOCK(p); 247 mtx_lock(&psp->ps_mtx); 248 } 249 250 /* 251 * Send an interrupt to process. 252 * 253 * Stack is set up to allow sigcode stored 254 * in u. to call routine, followed by kcall 255 * to sigreturn routine below. After sigreturn 256 * resets the signal mask, the stack, and the 257 * frame pointer, it returns to the user 258 * specified pc, psl. 259 */ 260 static void 261 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 262 { 263 struct thread *td = curthread; 264 struct proc *p = td->td_proc; 265 struct sigacts *psp; 266 struct trapframe *regs; 267 struct l_sigframe *fp, frame; 268 l_sigset_t lmask; 269 int oonstack; 270 int sig, code; 271 272 sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno); 273 code = ksi->ksi_code; 274 PROC_LOCK_ASSERT(p, MA_OWNED); 275 psp = p->p_sigacts; 276 mtx_assert(&psp->ps_mtx, MA_OWNED); 277 if (SIGISMEMBER(psp->ps_siginfo, sig)) { 278 /* Signal handler installed with SA_SIGINFO. */ 279 linux_rt_sendsig(catcher, ksi, mask); 280 return; 281 } 282 283 regs = td->td_frame; 284 oonstack = sigonstack(regs->tf_rsp); 285 286 /* Allocate space for the signal handler context. */ 287 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack && 288 SIGISMEMBER(psp->ps_sigonstack, sig)) { 289 fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp + 290 td->td_sigstk.ss_size - sizeof(struct l_sigframe)); 291 } else 292 fp = (struct l_sigframe *)regs->tf_rsp - 1; 293 mtx_unlock(&psp->ps_mtx); 294 PROC_UNLOCK(p); 295 296 /* Build the argument list for the signal handler. */ 297 sig = bsd_to_linux_signal(sig); 298 299 bzero(&frame, sizeof(frame)); 300 301 frame.sf_sig = sig; 302 frame.sf_sigmask = *mask; 303 bsd_to_linux_sigset(mask, &lmask); 304 305 /* Build the signal context to be used by sigreturn. */ 306 frame.sf_sc.sc_mask = lmask.__mask; 307 frame.sf_sc.sc_gs = regs->tf_gs; 308 frame.sf_sc.sc_fs = regs->tf_fs; 309 frame.sf_sc.sc_es = regs->tf_es; 310 frame.sf_sc.sc_ds = regs->tf_ds; 311 frame.sf_sc.sc_edi = regs->tf_rdi; 312 frame.sf_sc.sc_esi = regs->tf_rsi; 313 frame.sf_sc.sc_ebp = regs->tf_rbp; 314 frame.sf_sc.sc_ebx = regs->tf_rbx; 315 frame.sf_sc.sc_esp = regs->tf_rsp; 316 frame.sf_sc.sc_edx = regs->tf_rdx; 317 frame.sf_sc.sc_ecx = regs->tf_rcx; 318 frame.sf_sc.sc_eax = regs->tf_rax; 319 frame.sf_sc.sc_eip = regs->tf_rip; 320 frame.sf_sc.sc_cs = regs->tf_cs; 321 frame.sf_sc.sc_eflags = regs->tf_rflags; 322 frame.sf_sc.sc_esp_at_signal = regs->tf_rsp; 323 frame.sf_sc.sc_ss = regs->tf_ss; 324 frame.sf_sc.sc_err = regs->tf_err; 325 frame.sf_sc.sc_cr2 = (u_int32_t)(uintptr_t)ksi->ksi_addr; 326 frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(code); 327 328 if (copyout(&frame, fp, sizeof(frame)) != 0) { 329 /* 330 * Process has trashed its stack; give it an illegal 331 * instruction to halt it in its tracks. 332 */ 333 PROC_LOCK(p); 334 sigexit(td, SIGILL); 335 } 336 337 /* Build context to run handler in. */ 338 regs->tf_rsp = PTROUT(fp); 339 regs->tf_rip = linux32_vdso_sigcode; 340 regs->tf_rdi = PTROUT(catcher); 341 regs->tf_rflags &= ~(PSL_T | PSL_D); 342 regs->tf_cs = _ucode32sel; 343 regs->tf_ss = _udatasel; 344 regs->tf_ds = _udatasel; 345 regs->tf_es = _udatasel; 346 regs->tf_fs = _ufssel; 347 regs->tf_gs = _ugssel; 348 regs->tf_flags = TF_HASSEGS; 349 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 350 PROC_LOCK(p); 351 mtx_lock(&psp->ps_mtx); 352 } 353 354 /* 355 * System call to cleanup state after a signal 356 * has been taken. Reset signal mask and 357 * stack state from context left by sendsig (above). 358 * Return to previous pc and psl as specified by 359 * context left by sendsig. Check carefully to 360 * make sure that the user has not modified the 361 * psl to gain improper privileges or to cause 362 * a machine fault. 363 */ 364 int 365 linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args) 366 { 367 struct l_sigframe frame; 368 struct trapframe *regs; 369 int eflags; 370 ksiginfo_t ksi; 371 372 regs = td->td_frame; 373 374 /* 375 * The trampoline code hands us the sigframe. 376 * It is unsafe to keep track of it ourselves, in the event that a 377 * program jumps out of a signal handler. 378 */ 379 if (copyin(args->sfp, &frame, sizeof(frame)) != 0) 380 return (EFAULT); 381 382 /* Check for security violations. */ 383 eflags = frame.sf_sc.sc_eflags; 384 if (!EFL_SECURE(eflags, regs->tf_rflags)) 385 return(EINVAL); 386 387 /* 388 * Don't allow users to load a valid privileged %cs. Let the 389 * hardware check for invalid selectors, excess privilege in 390 * other selectors, invalid %eip's and invalid %esp's. 391 */ 392 if (!CS_SECURE(frame.sf_sc.sc_cs)) { 393 ksiginfo_init_trap(&ksi); 394 ksi.ksi_signo = SIGBUS; 395 ksi.ksi_code = BUS_OBJERR; 396 ksi.ksi_trapno = T_PROTFLT; 397 ksi.ksi_addr = (void *)regs->tf_rip; 398 trapsignal(td, &ksi); 399 return(EINVAL); 400 } 401 402 kern_sigprocmask(td, SIG_SETMASK, &frame.sf_sigmask, NULL, 0); 403 404 /* Restore signal context. */ 405 regs->tf_rdi = frame.sf_sc.sc_edi; 406 regs->tf_rsi = frame.sf_sc.sc_esi; 407 regs->tf_rbp = frame.sf_sc.sc_ebp; 408 regs->tf_rbx = frame.sf_sc.sc_ebx; 409 regs->tf_rdx = frame.sf_sc.sc_edx; 410 regs->tf_rcx = frame.sf_sc.sc_ecx; 411 regs->tf_rax = frame.sf_sc.sc_eax; 412 regs->tf_rip = frame.sf_sc.sc_eip; 413 regs->tf_cs = frame.sf_sc.sc_cs; 414 regs->tf_ds = frame.sf_sc.sc_ds; 415 regs->tf_es = frame.sf_sc.sc_es; 416 regs->tf_fs = frame.sf_sc.sc_fs; 417 regs->tf_gs = frame.sf_sc.sc_gs; 418 regs->tf_rflags = eflags; 419 regs->tf_rsp = frame.sf_sc.sc_esp_at_signal; 420 regs->tf_ss = frame.sf_sc.sc_ss; 421 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 422 423 return (EJUSTRETURN); 424 } 425 426 /* 427 * System call to cleanup state after a signal 428 * has been taken. Reset signal mask and 429 * stack state from context left by rt_sendsig (above). 430 * Return to previous pc and psl as specified by 431 * context left by sendsig. Check carefully to 432 * make sure that the user has not modified the 433 * psl to gain improper privileges or to cause 434 * a machine fault. 435 */ 436 int 437 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) 438 { 439 struct l_ucontext uc; 440 struct l_sigcontext *context; 441 sigset_t bmask; 442 l_stack_t *lss; 443 stack_t ss; 444 struct trapframe *regs; 445 int eflags; 446 ksiginfo_t ksi; 447 448 regs = td->td_frame; 449 450 /* 451 * The trampoline code hands us the ucontext. 452 * It is unsafe to keep track of it ourselves, in the event that a 453 * program jumps out of a signal handler. 454 */ 455 if (copyin(args->ucp, &uc, sizeof(uc)) != 0) 456 return (EFAULT); 457 458 context = &uc.uc_mcontext; 459 460 /* Check for security violations. */ 461 eflags = context->sc_eflags; 462 if (!EFL_SECURE(eflags, regs->tf_rflags)) 463 return(EINVAL); 464 465 /* 466 * Don't allow users to load a valid privileged %cs. Let the 467 * hardware check for invalid selectors, excess privilege in 468 * other selectors, invalid %eip's and invalid %esp's. 469 */ 470 if (!CS_SECURE(context->sc_cs)) { 471 ksiginfo_init_trap(&ksi); 472 ksi.ksi_signo = SIGBUS; 473 ksi.ksi_code = BUS_OBJERR; 474 ksi.ksi_trapno = T_PROTFLT; 475 ksi.ksi_addr = (void *)regs->tf_rip; 476 trapsignal(td, &ksi); 477 return(EINVAL); 478 } 479 480 linux_to_bsd_sigset(&uc.uc_sigmask, &bmask); 481 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0); 482 483 /* 484 * Restore signal context 485 */ 486 regs->tf_gs = context->sc_gs; 487 regs->tf_fs = context->sc_fs; 488 regs->tf_es = context->sc_es; 489 regs->tf_ds = context->sc_ds; 490 regs->tf_rdi = context->sc_edi; 491 regs->tf_rsi = context->sc_esi; 492 regs->tf_rbp = context->sc_ebp; 493 regs->tf_rbx = context->sc_ebx; 494 regs->tf_rdx = context->sc_edx; 495 regs->tf_rcx = context->sc_ecx; 496 regs->tf_rax = context->sc_eax; 497 regs->tf_rip = context->sc_eip; 498 regs->tf_cs = context->sc_cs; 499 regs->tf_rflags = eflags; 500 regs->tf_rsp = context->sc_esp_at_signal; 501 regs->tf_ss = context->sc_ss; 502 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 503 504 /* 505 * call sigaltstack & ignore results.. 506 */ 507 lss = &uc.uc_stack; 508 ss.ss_sp = PTRIN(lss->ss_sp); 509 ss.ss_size = lss->ss_size; 510 ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags); 511 512 (void)kern_sigaltstack(td, &ss, NULL); 513 514 return (EJUSTRETURN); 515 } 516 517 static int 518 linux32_fetch_syscall_args(struct thread *td) 519 { 520 struct proc *p; 521 struct trapframe *frame; 522 struct syscall_args *sa; 523 524 p = td->td_proc; 525 frame = td->td_frame; 526 sa = &td->td_sa; 527 528 sa->args[0] = frame->tf_rbx; 529 sa->args[1] = frame->tf_rcx; 530 sa->args[2] = frame->tf_rdx; 531 sa->args[3] = frame->tf_rsi; 532 sa->args[4] = frame->tf_rdi; 533 sa->args[5] = frame->tf_rbp; /* Unconfirmed */ 534 sa->code = frame->tf_rax; 535 sa->original_code = sa->code; 536 537 if (sa->code >= p->p_sysent->sv_size) 538 /* nosys */ 539 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1]; 540 else 541 sa->callp = &p->p_sysent->sv_table[sa->code]; 542 543 td->td_retval[0] = 0; 544 td->td_retval[1] = frame->tf_rdx; 545 546 return (0); 547 } 548 549 static void 550 linux32_set_syscall_retval(struct thread *td, int error) 551 { 552 struct trapframe *frame = td->td_frame; 553 554 cpu_set_syscall_retval(td, error); 555 556 if (__predict_false(error != 0)) { 557 if (error != ERESTART && error != EJUSTRETURN) 558 frame->tf_rax = bsd_to_linux_errno(error); 559 } 560 } 561 562 static void 563 linux32_set_fork_retval(struct thread *td) 564 { 565 struct trapframe *frame = td->td_frame; 566 567 frame->tf_rax = 0; 568 } 569 570 /* 571 * Clear registers on exec 572 * XXX copied from ia32_signal.c. 573 */ 574 static void 575 linux_exec_setregs(struct thread *td, struct image_params *imgp, 576 uintptr_t stack) 577 { 578 struct trapframe *regs = td->td_frame; 579 struct pcb *pcb = td->td_pcb; 580 register_t saved_rflags; 581 582 regs = td->td_frame; 583 pcb = td->td_pcb; 584 585 if (td->td_proc->p_md.md_ldt != NULL) 586 user_ldt_free(td); 587 588 critical_enter(); 589 wrmsr(MSR_FSBASE, 0); 590 wrmsr(MSR_KGSBASE, 0); /* User value while we're in the kernel */ 591 pcb->pcb_fsbase = 0; 592 pcb->pcb_gsbase = 0; 593 critical_exit(); 594 pcb->pcb_initial_fpucw = __LINUX_NPXCW__; 595 596 saved_rflags = regs->tf_rflags & PSL_T; 597 bzero((char *)regs, sizeof(struct trapframe)); 598 regs->tf_rip = imgp->entry_addr; 599 regs->tf_rsp = stack; 600 regs->tf_rflags = PSL_USER | saved_rflags; 601 regs->tf_gs = _ugssel; 602 regs->tf_fs = _ufssel; 603 regs->tf_es = _udatasel; 604 regs->tf_ds = _udatasel; 605 regs->tf_ss = _udatasel; 606 regs->tf_flags = TF_HASSEGS; 607 regs->tf_cs = _ucode32sel; 608 regs->tf_rbx = (register_t)imgp->ps_strings; 609 610 x86_clear_dbregs(pcb); 611 612 fpstate_drop(td); 613 614 /* Do full restore on return so that we can change to a different %cs */ 615 set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET); 616 } 617 618 /* 619 * XXX copied from ia32_sysvec.c. 620 */ 621 static int 622 linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base) 623 { 624 int argc, envc, error; 625 u_int32_t *vectp; 626 char *stringp; 627 uintptr_t destp, ustringp; 628 struct linux32_ps_strings *arginfo; 629 char canary[LINUX_AT_RANDOM_LEN]; 630 size_t execpath_len; 631 632 arginfo = (struct linux32_ps_strings *)PROC_PS_STRINGS(imgp->proc); 633 destp = (uintptr_t)arginfo; 634 635 if (imgp->execpath != NULL && imgp->auxargs != NULL) { 636 execpath_len = strlen(imgp->execpath) + 1; 637 destp -= execpath_len; 638 destp = rounddown2(destp, sizeof(uint32_t)); 639 imgp->execpathp = (void *)destp; 640 error = copyout(imgp->execpath, imgp->execpathp, execpath_len); 641 if (error != 0) 642 return (error); 643 } 644 645 /* Prepare the canary for SSP. */ 646 arc4rand(canary, sizeof(canary), 0); 647 destp -= roundup(sizeof(canary), sizeof(uint32_t)); 648 imgp->canary = (void *)destp; 649 error = copyout(canary, imgp->canary, sizeof(canary)); 650 if (error != 0) 651 return (error); 652 653 /* Allocate room for the argument and environment strings. */ 654 destp -= ARG_MAX - imgp->args->stringspace; 655 destp = rounddown2(destp, sizeof(uint32_t)); 656 ustringp = destp; 657 658 if (imgp->auxargs) { 659 /* 660 * Allocate room on the stack for the ELF auxargs 661 * array. It has LINUX_AT_COUNT entries. 662 */ 663 destp -= LINUX_AT_COUNT * sizeof(Elf32_Auxinfo); 664 destp = rounddown2(destp, sizeof(uint32_t)); 665 } 666 667 vectp = (uint32_t *)destp; 668 669 /* 670 * Allocate room for the argv[] and env vectors including the 671 * terminating NULL pointers. 672 */ 673 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1; 674 675 /* vectp also becomes our initial stack base. */ 676 *stack_base = (uintptr_t)vectp; 677 678 stringp = imgp->args->begin_argv; 679 argc = imgp->args->argc; 680 envc = imgp->args->envc; 681 682 /* Copy out strings - arguments and environment. */ 683 error = copyout(stringp, (void *)ustringp, 684 ARG_MAX - imgp->args->stringspace); 685 if (error != 0) 686 return (error); 687 688 /* Fill in "ps_strings" struct for ps, w, etc. */ 689 if (suword32(&arginfo->ps_argvstr, (uint32_t)(intptr_t)vectp) != 0 || 690 suword32(&arginfo->ps_nargvstr, argc) != 0) 691 return (EFAULT); 692 693 /* Fill in argument portion of vector table. */ 694 for (; argc > 0; --argc) { 695 if (suword32(vectp++, ustringp) != 0) 696 return (EFAULT); 697 while (*stringp++ != 0) 698 ustringp++; 699 ustringp++; 700 } 701 702 /* A null vector table pointer separates the argp's from the envp's. */ 703 if (suword32(vectp++, 0) != 0) 704 return (EFAULT); 705 706 if (suword32(&arginfo->ps_envstr, (uint32_t)(intptr_t)vectp) != 0 || 707 suword32(&arginfo->ps_nenvstr, envc) != 0) 708 return (EFAULT); 709 710 /* Fill in environment portion of vector table. */ 711 for (; envc > 0; --envc) { 712 if (suword32(vectp++, ustringp) != 0) 713 return (EFAULT); 714 while (*stringp++ != 0) 715 ustringp++; 716 ustringp++; 717 } 718 719 /* The end of the vector table is a null pointer. */ 720 if (suword32(vectp, 0) != 0) 721 return (EFAULT); 722 723 if (imgp->auxargs) { 724 vectp++; 725 error = imgp->sysent->sv_copyout_auxargs(imgp, 726 (uintptr_t)vectp); 727 if (error != 0) 728 return (error); 729 } 730 731 return (0); 732 } 733 734 static SYSCTL_NODE(_compat, OID_AUTO, linux32, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 735 "32-bit Linux emulation"); 736 737 static u_long linux32_maxdsiz = LINUX32_MAXDSIZ; 738 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxdsiz, CTLFLAG_RW, 739 &linux32_maxdsiz, 0, ""); 740 static u_long linux32_maxssiz = LINUX32_MAXSSIZ; 741 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxssiz, CTLFLAG_RW, 742 &linux32_maxssiz, 0, ""); 743 static u_long linux32_maxvmem = LINUX32_MAXVMEM; 744 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxvmem, CTLFLAG_RW, 745 &linux32_maxvmem, 0, ""); 746 bool linux32_emulate_i386 = false; 747 SYSCTL_BOOL(_compat_linux32, OID_AUTO, emulate_i386, CTLFLAG_RWTUN, 748 &linux32_emulate_i386, 0, "Emulate the real i386"); 749 750 static void 751 linux32_fixlimit(struct rlimit *rl, int which) 752 { 753 754 switch (which) { 755 case RLIMIT_DATA: 756 if (linux32_maxdsiz != 0) { 757 if (rl->rlim_cur > linux32_maxdsiz) 758 rl->rlim_cur = linux32_maxdsiz; 759 if (rl->rlim_max > linux32_maxdsiz) 760 rl->rlim_max = linux32_maxdsiz; 761 } 762 break; 763 case RLIMIT_STACK: 764 if (linux32_maxssiz != 0) { 765 if (rl->rlim_cur > linux32_maxssiz) 766 rl->rlim_cur = linux32_maxssiz; 767 if (rl->rlim_max > linux32_maxssiz) 768 rl->rlim_max = linux32_maxssiz; 769 } 770 break; 771 case RLIMIT_VMEM: 772 if (linux32_maxvmem != 0) { 773 if (rl->rlim_cur > linux32_maxvmem) 774 rl->rlim_cur = linux32_maxvmem; 775 if (rl->rlim_max > linux32_maxvmem) 776 rl->rlim_max = linux32_maxvmem; 777 } 778 break; 779 } 780 } 781 782 struct sysentvec elf_linux_sysvec = { 783 .sv_size = LINUX32_SYS_MAXSYSCALL, 784 .sv_table = linux32_sysent, 785 .sv_fixup = elf32_freebsd_fixup, 786 .sv_sendsig = linux_sendsig, 787 .sv_sigcode = &_binary_linux32_vdso_so_o_start, 788 .sv_szsigcode = &linux_szsigcode, 789 .sv_name = "Linux ELF32", 790 .sv_coredump = elf32_coredump, 791 .sv_elf_core_osabi = ELFOSABI_NONE, 792 .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR, 793 .sv_elf_core_prepare_notes = linux32_prepare_notes, 794 .sv_minsigstksz = LINUX_MINSIGSTKSZ, 795 .sv_minuser = VM_MIN_ADDRESS, 796 .sv_maxuser = LINUX32_MAXUSER, 797 .sv_usrstack = LINUX32_USRSTACK, 798 .sv_psstrings = LINUX32_PS_STRINGS, 799 .sv_psstringssz = sizeof(struct linux32_ps_strings), 800 .sv_stackprot = VM_PROT_ALL, 801 .sv_copyout_auxargs = __linuxN(copyout_auxargs), 802 .sv_copyout_strings = linux_copyout_strings, 803 .sv_setregs = linux_exec_setregs, 804 .sv_fixlimit = linux32_fixlimit, 805 .sv_maxssiz = &linux32_maxssiz, 806 .sv_flags = SV_ABI_LINUX | SV_ILP32 | SV_IA32 | SV_SHP | 807 SV_SIG_DISCIGN | SV_SIG_WAITNDQ | SV_TIMEKEEP, 808 .sv_set_syscall_retval = linux32_set_syscall_retval, 809 .sv_fetch_syscall_args = linux32_fetch_syscall_args, 810 .sv_syscallnames = linux32_syscallnames, 811 .sv_shared_page_base = LINUX32_SHAREDPAGE, 812 .sv_shared_page_len = PAGE_SIZE, 813 .sv_schedtail = linux_schedtail, 814 .sv_thread_detach = linux_thread_detach, 815 .sv_trap = NULL, 816 .sv_hwcap = NULL, 817 .sv_hwcap2 = NULL, 818 .sv_onexec = linux_on_exec_vmspace, 819 .sv_onexit = linux_on_exit, 820 .sv_ontdexit = linux_thread_dtor, 821 .sv_setid_allowed = &linux_setid_allowed_query, 822 .sv_set_fork_retval = linux32_set_fork_retval, 823 }; 824 825 static int 826 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp) 827 { 828 int error; 829 830 error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base, 831 LINUX32_VDSOPAGE_SIZE, imgp); 832 if (error == 0) 833 error = linux_on_exec(p, imgp); 834 return (error); 835 } 836 837 /* 838 * linux_vdso_install() and linux_exec_sysvec_init() must be called 839 * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY). 840 */ 841 static void 842 linux_exec_sysvec_init(void *param) 843 { 844 l_uintptr_t *ktimekeep_base, *ktsc_selector; 845 struct sysentvec *sv; 846 ptrdiff_t tkoff; 847 848 sv = param; 849 /* Fill timekeep_base */ 850 exec_sysvec_init(sv); 851 852 tkoff = kern_timekeep_base - linux_vdso_base; 853 ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff); 854 *ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset; 855 856 tkoff = kern_tsc_selector - linux_vdso_base; 857 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff); 858 *ktsc_selector = linux_vdso_tsc_selector_idx(); 859 if (bootverbose) 860 printf("Linux i386 vDSO tsc_selector: %u\n", *ktsc_selector); 861 862 tkoff = kern_cpu_selector - linux_vdso_base; 863 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff); 864 *ktsc_selector = linux_vdso_cpu_selector_idx(); 865 if (bootverbose) 866 printf("Linux i386 vDSO cpu_selector: %u\n", *ktsc_selector); 867 } 868 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY, 869 linux_exec_sysvec_init, &elf_linux_sysvec); 870 871 static void 872 linux_vdso_install(const void *param) 873 { 874 char *vdso_start = &_binary_linux32_vdso_so_o_start; 875 char *vdso_end = &_binary_linux32_vdso_so_o_end; 876 877 linux_szsigcode = vdso_end - vdso_start; 878 MPASS(linux_szsigcode <= LINUX32_VDSOPAGE_SIZE); 879 880 linux_vdso_base = LINUX32_VDSOPAGE; 881 882 __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base); 883 884 linux_vdso_obj = __elfN(linux_shared_page_init) 885 (&linux_vdso_mapping, LINUX32_VDSOPAGE_SIZE); 886 bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode); 887 888 linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base); 889 } 890 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST, 891 linux_vdso_install, NULL); 892 893 static void 894 linux_vdso_deinstall(const void *param) 895 { 896 897 __elfN(linux_shared_page_fini)(linux_vdso_obj, 898 linux_vdso_mapping, LINUX32_VDSOPAGE_SIZE); 899 } 900 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST, 901 linux_vdso_deinstall, NULL); 902 903 static void 904 linux_vdso_reloc(char *mapping, Elf_Addr offset) 905 { 906 const Elf_Shdr *shdr; 907 const Elf_Rel *rel; 908 const Elf_Ehdr *ehdr; 909 Elf32_Addr *where; 910 Elf_Size rtype, symidx; 911 Elf32_Addr addr, addend; 912 int i, relcnt; 913 914 MPASS(offset != 0); 915 916 relcnt = 0; 917 ehdr = (const Elf_Ehdr *)mapping; 918 shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff); 919 for (i = 0; i < ehdr->e_shnum; i++) 920 { 921 switch (shdr[i].sh_type) { 922 case SHT_REL: 923 rel = (const Elf_Rel *)(mapping + shdr[i].sh_offset); 924 relcnt = shdr[i].sh_size / sizeof(*rel); 925 break; 926 case SHT_RELA: 927 printf("Linux i386 vDSO: unexpected Rela section\n"); 928 break; 929 } 930 } 931 932 for (i = 0; i < relcnt; i++, rel++) { 933 where = (Elf32_Addr *)(mapping + rel->r_offset); 934 addend = *where; 935 rtype = ELF_R_TYPE(rel->r_info); 936 symidx = ELF_R_SYM(rel->r_info); 937 938 switch (rtype) { 939 case R_386_NONE: /* none */ 940 break; 941 942 case R_386_RELATIVE: /* B + A */ 943 addr = (Elf32_Addr)PTROUT(offset + addend); 944 if (*where != addr) 945 *where = addr; 946 break; 947 948 case R_386_IRELATIVE: 949 printf("Linux i386 vDSO: unexpected ifunc relocation, " 950 "symbol index %ld\n", (intmax_t)symidx); 951 break; 952 default: 953 printf("Linux i386 vDSO: unexpected relocation type %ld, " 954 "symbol index %ld\n", (intmax_t)rtype, (intmax_t)symidx); 955 } 956 } 957 } 958 959 static Elf_Brandnote linux32_brandnote = { 960 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), 961 .hdr.n_descsz = 16, /* XXX at least 16 */ 962 .hdr.n_type = 1, 963 .vendor = GNU_ABI_VENDOR, 964 .flags = BN_TRANSLATE_OSREL, 965 .trans_osrel = linux_trans_osrel 966 }; 967 968 static Elf32_Brandinfo linux_brand = { 969 .brand = ELFOSABI_LINUX, 970 .machine = EM_386, 971 .compat_3_brand = "Linux", 972 .interp_path = "/lib/ld-linux.so.1", 973 .sysvec = &elf_linux_sysvec, 974 .interp_newpath = NULL, 975 .brand_note = &linux32_brandnote, 976 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 977 }; 978 979 static Elf32_Brandinfo linux_glibc2brand = { 980 .brand = ELFOSABI_LINUX, 981 .machine = EM_386, 982 .compat_3_brand = "Linux", 983 .interp_path = "/lib/ld-linux.so.2", 984 .sysvec = &elf_linux_sysvec, 985 .interp_newpath = NULL, 986 .brand_note = &linux32_brandnote, 987 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 988 }; 989 990 static Elf32_Brandinfo linux_muslbrand = { 991 .brand = ELFOSABI_LINUX, 992 .machine = EM_386, 993 .compat_3_brand = "Linux", 994 .interp_path = "/lib/ld-musl-i386.so.1", 995 .sysvec = &elf_linux_sysvec, 996 .interp_newpath = NULL, 997 .brand_note = &linux32_brandnote, 998 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE | 999 LINUX_BI_FUTEX_REQUEUE 1000 }; 1001 1002 Elf32_Brandinfo *linux_brandlist[] = { 1003 &linux_brand, 1004 &linux_glibc2brand, 1005 &linux_muslbrand, 1006 NULL 1007 }; 1008 1009 static int 1010 linux_elf_modevent(module_t mod, int type, void *data) 1011 { 1012 Elf32_Brandinfo **brandinfo; 1013 int error; 1014 struct linux_ioctl_handler **lihp; 1015 1016 error = 0; 1017 1018 switch(type) { 1019 case MOD_LOAD: 1020 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 1021 ++brandinfo) 1022 if (elf32_insert_brand_entry(*brandinfo) < 0) 1023 error = EINVAL; 1024 if (error == 0) { 1025 SET_FOREACH(lihp, linux_ioctl_handler_set) 1026 linux32_ioctl_register_handler(*lihp); 1027 stclohz = (stathz ? stathz : hz); 1028 if (bootverbose) 1029 printf("Linux i386 ELF exec handler installed\n"); 1030 } else 1031 printf("cannot insert Linux i386 ELF brand handler\n"); 1032 break; 1033 case MOD_UNLOAD: 1034 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 1035 ++brandinfo) 1036 if (elf32_brand_inuse(*brandinfo)) 1037 error = EBUSY; 1038 if (error == 0) { 1039 for (brandinfo = &linux_brandlist[0]; 1040 *brandinfo != NULL; ++brandinfo) 1041 if (elf32_remove_brand_entry(*brandinfo) < 0) 1042 error = EINVAL; 1043 } 1044 if (error == 0) { 1045 SET_FOREACH(lihp, linux_ioctl_handler_set) 1046 linux32_ioctl_unregister_handler(*lihp); 1047 if (bootverbose) 1048 printf("Linux i386 ELF exec handler removed\n"); 1049 } else 1050 printf("Could not deinstall Linux i386 ELF interpreter entry\n"); 1051 break; 1052 default: 1053 return (EOPNOTSUPP); 1054 } 1055 return (error); 1056 } 1057 1058 static moduledata_t linux_elf_mod = { 1059 "linuxelf", 1060 linux_elf_modevent, 1061 0 1062 }; 1063 1064 DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY); 1065 MODULE_DEPEND(linuxelf, linux_common, 1, 1, 1); 1066 FEATURE(linux, "Linux 32bit support"); 1067