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