1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 1994-1996 Søren Schmidt 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #define __ELF_WORD_SIZE 32 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/exec.h> 37 #include <sys/fcntl.h> 38 #include <sys/imgact.h> 39 #include <sys/imgact_aout.h> 40 #include <sys/imgact_elf.h> 41 #include <sys/kernel.h> 42 #include <sys/malloc.h> 43 #include <sys/module.h> 44 #include <sys/proc.h> 45 #include <sys/stddef.h> 46 #include <sys/signalvar.h> 47 #include <sys/syscallsubr.h> 48 #include <sys/sysctl.h> 49 #include <sys/sysent.h> 50 #include <sys/sysproto.h> 51 #include <sys/vnode.h> 52 53 #include <vm/vm.h> 54 #include <vm/pmap.h> 55 #include <vm/vm_extern.h> 56 #include <vm/vm_map.h> 57 #include <vm/vm_object.h> 58 #include <vm/vm_page.h> 59 #include <vm/vm_param.h> 60 61 #include <machine/cpu.h> 62 #include <machine/cputypes.h> 63 #include <machine/md_var.h> 64 #include <machine/pcb.h> 65 #include <machine/trap.h> 66 67 #include <x86/linux/linux_x86.h> 68 #include <i386/linux/linux.h> 69 #include <i386/linux/linux_proto.h> 70 #include <compat/linux/linux_elf.h> 71 #include <compat/linux/linux_emul.h> 72 #include <compat/linux/linux_fork.h> 73 #include <compat/linux/linux_ioctl.h> 74 #include <compat/linux/linux_mib.h> 75 #include <compat/linux/linux_misc.h> 76 #include <compat/linux/linux_signal.h> 77 #include <compat/linux/linux_util.h> 78 #include <compat/linux/linux_vdso.h> 79 80 #include <x86/linux/linux_x86_sigframe.h> 81 82 MODULE_VERSION(linux, 1); 83 84 #define LINUX_VDSOPAGE_SIZE PAGE_SIZE * 2 85 #define LINUX_VDSOPAGE (VM_MAXUSER_ADDRESS - LINUX_VDSOPAGE_SIZE) 86 #define LINUX_SHAREDPAGE (LINUX_VDSOPAGE - PAGE_SIZE) 87 /* 88 * PAGE_SIZE - the size 89 * of the native SHAREDPAGE 90 */ 91 #define LINUX_USRSTACK LINUX_SHAREDPAGE 92 #define LINUX_PS_STRINGS (LINUX_USRSTACK - sizeof(struct ps_strings)) 93 94 static int linux_szsigcode; 95 static vm_object_t linux_vdso_obj; 96 static char *linux_vdso_mapping; 97 extern char _binary_linux_vdso_so_o_start; 98 extern char _binary_linux_vdso_so_o_end; 99 static vm_offset_t linux_vdso_base; 100 101 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL]; 102 extern const char *linux_syscallnames[]; 103 104 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler); 105 106 static int linux_fixup(uintptr_t *stack_base, 107 struct image_params *iparams); 108 static void linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask); 109 static void linux_exec_setregs(struct thread *td, 110 struct image_params *imgp, uintptr_t stack); 111 static void linux_exec_sysvec_init(void *param); 112 static int linux_on_exec_vmspace(struct proc *p, 113 struct image_params *imgp); 114 static void linux_set_fork_retval(struct thread *td); 115 static void linux_vdso_install(const void *param); 116 static void linux_vdso_deinstall(const void *param); 117 static void linux_vdso_reloc(char *mapping, Elf_Addr offset); 118 119 LINUX_VDSO_SYM_CHAR(linux_platform); 120 LINUX_VDSO_SYM_INTPTR(__kernel_vsyscall); 121 LINUX_VDSO_SYM_INTPTR(linux_vdso_sigcode); 122 LINUX_VDSO_SYM_INTPTR(linux_vdso_rt_sigcode); 123 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base); 124 LINUX_VDSO_SYM_INTPTR(kern_tsc_selector); 125 LINUX_VDSO_SYM_INTPTR(kern_cpu_selector); 126 127 static int 128 linux_fixup(uintptr_t *stack_base, struct image_params *imgp) 129 { 130 register_t *base, *argv, *envp; 131 132 base = (register_t *)*stack_base; 133 argv = base; 134 envp = base + (imgp->args->argc + 1); 135 base--; 136 suword(base, (intptr_t)envp); 137 base--; 138 suword(base, (intptr_t)argv); 139 base--; 140 suword(base, imgp->args->argc); 141 *stack_base = (uintptr_t)base; 142 return (0); 143 } 144 145 static int 146 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base) 147 { 148 Elf32_Auxargs *args; 149 Elf32_Auxinfo *argarray, *pos; 150 int error, issetugid; 151 152 issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0; 153 args = (Elf32_Auxargs *)imgp->auxargs; 154 argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP, 155 M_WAITOK | M_ZERO); 156 157 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, linux_vdso_base); 158 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO, __kernel_vsyscall); 159 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature); 160 161 /* 162 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0, 163 * as it has appeared in the 2.4.0-rc7 first time. 164 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK), 165 * glibc falls back to the hard-coded CLK_TCK value when aux entry 166 * is not present. 167 * Also see linux_times() implementation. 168 */ 169 if (linux_kernver(curthread) >= LINUX_KERNVER_2004000) 170 AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz); 171 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 172 AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 173 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 174 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 175 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 176 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 177 AUXARGS_ENTRY(pos, AT_BASE, args->base); 178 AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid); 179 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid); 180 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid); 181 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid); 182 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid); 183 AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform)); 184 AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary); 185 if (imgp->execpathp != 0) 186 AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp); 187 if (args->execfd != -1) 188 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 189 AUXARGS_ENTRY(pos, AT_NULL, 0); 190 191 free(imgp->auxargs, M_TEMP); 192 imgp->auxargs = NULL; 193 KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs")); 194 195 error = copyout(argarray, (void *)base, 196 sizeof(*argarray) * LINUX_AT_COUNT); 197 free(argarray, M_TEMP); 198 return (error); 199 } 200 201 static void 202 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 203 { 204 struct thread *td = curthread; 205 struct proc *p = td->td_proc; 206 struct sigacts *psp; 207 struct trapframe *regs; 208 struct l_rt_sigframe *fp, frame; 209 int sig, code; 210 int oonstack; 211 212 sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno); 213 code = ksi->ksi_code; 214 PROC_LOCK_ASSERT(p, MA_OWNED); 215 psp = p->p_sigacts; 216 mtx_assert(&psp->ps_mtx, MA_OWNED); 217 regs = td->td_frame; 218 oonstack = sigonstack(regs->tf_esp); 219 220 /* Allocate space for the signal handler context. */ 221 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack && 222 SIGISMEMBER(psp->ps_sigonstack, sig)) { 223 fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp + 224 td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe)); 225 } else 226 fp = (struct l_rt_sigframe *)regs->tf_esp - 1; 227 mtx_unlock(&psp->ps_mtx); 228 229 /* Build the argument list for the signal handler. */ 230 sig = bsd_to_linux_signal(sig); 231 232 bzero(&frame, sizeof(frame)); 233 234 frame.sf_sig = sig; 235 frame.sf_siginfo = PTROUT(&fp->sf_si); 236 frame.sf_ucontext = PTROUT(&fp->sf_uc); 237 238 /* Fill in POSIX parts. */ 239 siginfo_to_lsiginfo(&ksi->ksi_info, &frame.sf_si, sig); 240 241 /* Build the signal context to be used by sigreturn. */ 242 frame.sf_uc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp); 243 frame.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size; 244 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) 245 ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE; 246 PROC_UNLOCK(p); 247 248 bsd_to_linux_sigset(mask, &frame.sf_uc.uc_sigmask); 249 250 frame.sf_uc.uc_mcontext.sc_mask = frame.sf_uc.uc_sigmask.__mask; 251 frame.sf_uc.uc_mcontext.sc_gs = rgs(); 252 frame.sf_uc.uc_mcontext.sc_fs = regs->tf_fs; 253 frame.sf_uc.uc_mcontext.sc_es = regs->tf_es; 254 frame.sf_uc.uc_mcontext.sc_ds = regs->tf_ds; 255 frame.sf_uc.uc_mcontext.sc_edi = regs->tf_edi; 256 frame.sf_uc.uc_mcontext.sc_esi = regs->tf_esi; 257 frame.sf_uc.uc_mcontext.sc_ebp = regs->tf_ebp; 258 frame.sf_uc.uc_mcontext.sc_ebx = regs->tf_ebx; 259 frame.sf_uc.uc_mcontext.sc_esp = regs->tf_esp; 260 frame.sf_uc.uc_mcontext.sc_edx = regs->tf_edx; 261 frame.sf_uc.uc_mcontext.sc_ecx = regs->tf_ecx; 262 frame.sf_uc.uc_mcontext.sc_eax = regs->tf_eax; 263 frame.sf_uc.uc_mcontext.sc_eip = regs->tf_eip; 264 frame.sf_uc.uc_mcontext.sc_cs = regs->tf_cs; 265 frame.sf_uc.uc_mcontext.sc_eflags = regs->tf_eflags; 266 frame.sf_uc.uc_mcontext.sc_esp_at_signal = regs->tf_esp; 267 frame.sf_uc.uc_mcontext.sc_ss = regs->tf_ss; 268 frame.sf_uc.uc_mcontext.sc_err = regs->tf_err; 269 frame.sf_uc.uc_mcontext.sc_cr2 = (register_t)ksi->ksi_addr; 270 frame.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code); 271 272 if (copyout(&frame, fp, sizeof(frame)) != 0) { 273 /* 274 * Process has trashed its stack; give it an illegal 275 * instruction to halt it in its tracks. 276 */ 277 PROC_LOCK(p); 278 sigexit(td, SIGILL); 279 } 280 281 /* Build context to run handler in. */ 282 regs->tf_esp = PTROUT(fp); 283 regs->tf_eip = linux_vdso_rt_sigcode; 284 regs->tf_edi = PTROUT(catcher); 285 regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D); 286 regs->tf_cs = _ucodesel; 287 regs->tf_ds = _udatasel; 288 regs->tf_es = _udatasel; 289 regs->tf_fs = _udatasel; 290 regs->tf_ss = _udatasel; 291 PROC_LOCK(p); 292 mtx_lock(&psp->ps_mtx); 293 } 294 295 /* 296 * Send an interrupt to process. 297 * 298 * Stack is set up to allow sigcode stored 299 * in u. to call routine, followed by kcall 300 * to sigreturn routine below. After sigreturn 301 * resets the signal mask, the stack, and the 302 * frame pointer, it returns to the user 303 * specified pc, psl. 304 */ 305 static void 306 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 307 { 308 struct thread *td = curthread; 309 struct proc *p = td->td_proc; 310 struct sigacts *psp; 311 struct trapframe *regs; 312 struct l_sigframe *fp, frame; 313 l_sigset_t lmask; 314 int sig; 315 int oonstack; 316 317 PROC_LOCK_ASSERT(p, MA_OWNED); 318 psp = p->p_sigacts; 319 sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno); 320 mtx_assert(&psp->ps_mtx, MA_OWNED); 321 if (SIGISMEMBER(psp->ps_siginfo, sig)) { 322 /* Signal handler installed with SA_SIGINFO. */ 323 linux_rt_sendsig(catcher, ksi, mask); 324 return; 325 } 326 regs = td->td_frame; 327 oonstack = sigonstack(regs->tf_esp); 328 329 /* Allocate space for the signal handler context. */ 330 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack && 331 SIGISMEMBER(psp->ps_sigonstack, sig)) { 332 fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp + 333 td->td_sigstk.ss_size - sizeof(struct l_sigframe)); 334 } else 335 fp = (struct l_sigframe *)regs->tf_esp - 1; 336 mtx_unlock(&psp->ps_mtx); 337 PROC_UNLOCK(p); 338 339 /* Build the argument list for the signal handler. */ 340 sig = bsd_to_linux_signal(sig); 341 342 bzero(&frame, sizeof(frame)); 343 344 frame.sf_sig = sig; 345 frame.sf_sigmask = *mask; 346 bsd_to_linux_sigset(mask, &lmask); 347 348 /* Build the signal context to be used by sigreturn. */ 349 frame.sf_sc.sc_mask = lmask.__mask; 350 frame.sf_sc.sc_gs = rgs(); 351 frame.sf_sc.sc_fs = regs->tf_fs; 352 frame.sf_sc.sc_es = regs->tf_es; 353 frame.sf_sc.sc_ds = regs->tf_ds; 354 frame.sf_sc.sc_edi = regs->tf_edi; 355 frame.sf_sc.sc_esi = regs->tf_esi; 356 frame.sf_sc.sc_ebp = regs->tf_ebp; 357 frame.sf_sc.sc_ebx = regs->tf_ebx; 358 frame.sf_sc.sc_esp = regs->tf_esp; 359 frame.sf_sc.sc_edx = regs->tf_edx; 360 frame.sf_sc.sc_ecx = regs->tf_ecx; 361 frame.sf_sc.sc_eax = regs->tf_eax; 362 frame.sf_sc.sc_eip = regs->tf_eip; 363 frame.sf_sc.sc_cs = regs->tf_cs; 364 frame.sf_sc.sc_eflags = regs->tf_eflags; 365 frame.sf_sc.sc_esp_at_signal = regs->tf_esp; 366 frame.sf_sc.sc_ss = regs->tf_ss; 367 frame.sf_sc.sc_err = regs->tf_err; 368 frame.sf_sc.sc_cr2 = (register_t)ksi->ksi_addr; 369 frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(ksi->ksi_trapno); 370 371 if (copyout(&frame, fp, sizeof(frame)) != 0) { 372 /* 373 * Process has trashed its stack; give it an illegal 374 * instruction to halt it in its tracks. 375 */ 376 PROC_LOCK(p); 377 sigexit(td, SIGILL); 378 } 379 380 /* Build context to run handler in. */ 381 regs->tf_esp = PTROUT(fp); 382 regs->tf_eip = linux_vdso_sigcode; 383 regs->tf_edi = PTROUT(catcher); 384 regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D); 385 regs->tf_cs = _ucodesel; 386 regs->tf_ds = _udatasel; 387 regs->tf_es = _udatasel; 388 regs->tf_fs = _udatasel; 389 regs->tf_ss = _udatasel; 390 PROC_LOCK(p); 391 mtx_lock(&psp->ps_mtx); 392 } 393 394 /* 395 * System call to cleanup state after a signal 396 * has been taken. Reset signal mask and 397 * stack state from context left by sendsig (above). 398 * Return to previous pc and psl as specified by 399 * context left by sendsig. Check carefully to 400 * make sure that the user has not modified the 401 * psl to gain improper privileges or to cause 402 * a machine fault. 403 */ 404 int 405 linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args) 406 { 407 struct l_sigframe frame; 408 struct trapframe *regs; 409 int eflags; 410 ksiginfo_t ksi; 411 412 regs = td->td_frame; 413 414 /* 415 * The trampoline code hands us the sigframe. 416 * It is unsafe to keep track of it ourselves, in the event that a 417 * program jumps out of a signal handler. 418 */ 419 if (copyin(args->sfp, &frame, sizeof(frame)) != 0) 420 return (EFAULT); 421 422 /* Check for security violations. */ 423 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 424 eflags = frame.sf_sc.sc_eflags; 425 if (!EFLAGS_SECURE(eflags, regs->tf_eflags)) 426 return (EINVAL); 427 428 /* 429 * Don't allow users to load a valid privileged %cs. Let the 430 * hardware check for invalid selectors, excess privilege in 431 * other selectors, invalid %eip's and invalid %esp's. 432 */ 433 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 434 if (!CS_SECURE(frame.sf_sc.sc_cs)) { 435 ksiginfo_init_trap(&ksi); 436 ksi.ksi_signo = SIGBUS; 437 ksi.ksi_code = BUS_OBJERR; 438 ksi.ksi_trapno = T_PROTFLT; 439 ksi.ksi_addr = (void *)regs->tf_eip; 440 trapsignal(td, &ksi); 441 return (EINVAL); 442 } 443 444 kern_sigprocmask(td, SIG_SETMASK, &frame.sf_sigmask, NULL, 0); 445 446 /* Restore signal context. */ 447 /* %gs was restored by the trampoline. */ 448 regs->tf_fs = frame.sf_sc.sc_fs; 449 regs->tf_es = frame.sf_sc.sc_es; 450 regs->tf_ds = frame.sf_sc.sc_ds; 451 regs->tf_edi = frame.sf_sc.sc_edi; 452 regs->tf_esi = frame.sf_sc.sc_esi; 453 regs->tf_ebp = frame.sf_sc.sc_ebp; 454 regs->tf_ebx = frame.sf_sc.sc_ebx; 455 regs->tf_edx = frame.sf_sc.sc_edx; 456 regs->tf_ecx = frame.sf_sc.sc_ecx; 457 regs->tf_eax = frame.sf_sc.sc_eax; 458 regs->tf_eip = frame.sf_sc.sc_eip; 459 regs->tf_cs = frame.sf_sc.sc_cs; 460 regs->tf_eflags = eflags; 461 regs->tf_esp = frame.sf_sc.sc_esp_at_signal; 462 regs->tf_ss = frame.sf_sc.sc_ss; 463 464 return (EJUSTRETURN); 465 } 466 467 /* 468 * System call to cleanup state after a signal 469 * has been taken. Reset signal mask and 470 * stack state from context left by rt_sendsig (above). 471 * Return to previous pc and psl as specified by 472 * context left by sendsig. Check carefully to 473 * make sure that the user has not modified the 474 * psl to gain improper privileges or to cause 475 * a machine fault. 476 */ 477 int 478 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) 479 { 480 struct l_ucontext uc; 481 struct l_sigcontext *context; 482 sigset_t bmask; 483 l_stack_t *lss; 484 stack_t ss; 485 struct trapframe *regs; 486 int eflags; 487 ksiginfo_t ksi; 488 489 regs = td->td_frame; 490 491 /* 492 * The trampoline code hands us the ucontext. 493 * It is unsafe to keep track of it ourselves, in the event that a 494 * program jumps out of a signal handler. 495 */ 496 if (copyin(args->ucp, &uc, sizeof(uc)) != 0) 497 return (EFAULT); 498 499 context = &uc.uc_mcontext; 500 501 /* Check for security violations. */ 502 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 503 eflags = context->sc_eflags; 504 if (!EFLAGS_SECURE(eflags, regs->tf_eflags)) 505 return (EINVAL); 506 507 /* 508 * Don't allow users to load a valid privileged %cs. Let the 509 * hardware check for invalid selectors, excess privilege in 510 * other selectors, invalid %eip's and invalid %esp's. 511 */ 512 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 513 if (!CS_SECURE(context->sc_cs)) { 514 ksiginfo_init_trap(&ksi); 515 ksi.ksi_signo = SIGBUS; 516 ksi.ksi_code = BUS_OBJERR; 517 ksi.ksi_trapno = T_PROTFLT; 518 ksi.ksi_addr = (void *)regs->tf_eip; 519 trapsignal(td, &ksi); 520 return (EINVAL); 521 } 522 523 linux_to_bsd_sigset(&uc.uc_sigmask, &bmask); 524 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0); 525 526 /* Restore signal context. */ 527 /* %gs was restored by the trampoline. */ 528 regs->tf_fs = context->sc_fs; 529 regs->tf_es = context->sc_es; 530 regs->tf_ds = context->sc_ds; 531 regs->tf_edi = context->sc_edi; 532 regs->tf_esi = context->sc_esi; 533 regs->tf_ebp = context->sc_ebp; 534 regs->tf_ebx = context->sc_ebx; 535 regs->tf_edx = context->sc_edx; 536 regs->tf_ecx = context->sc_ecx; 537 regs->tf_eax = context->sc_eax; 538 regs->tf_eip = context->sc_eip; 539 regs->tf_cs = context->sc_cs; 540 regs->tf_eflags = eflags; 541 regs->tf_esp = context->sc_esp_at_signal; 542 regs->tf_ss = context->sc_ss; 543 544 /* Call sigaltstack & ignore results. */ 545 lss = &uc.uc_stack; 546 ss.ss_sp = PTRIN(lss->ss_sp); 547 ss.ss_size = lss->ss_size; 548 ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags); 549 550 (void)kern_sigaltstack(td, &ss, NULL); 551 552 return (EJUSTRETURN); 553 } 554 555 static int 556 linux_fetch_syscall_args(struct thread *td) 557 { 558 struct proc *p; 559 struct trapframe *frame; 560 struct syscall_args *sa; 561 562 p = td->td_proc; 563 frame = td->td_frame; 564 sa = &td->td_sa; 565 566 sa->code = frame->tf_eax; 567 sa->original_code = sa->code; 568 sa->args[0] = frame->tf_ebx; 569 sa->args[1] = frame->tf_ecx; 570 sa->args[2] = frame->tf_edx; 571 sa->args[3] = frame->tf_esi; 572 sa->args[4] = frame->tf_edi; 573 sa->args[5] = frame->tf_ebp; /* Unconfirmed */ 574 575 if (sa->code >= p->p_sysent->sv_size) 576 /* nosys */ 577 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1]; 578 else 579 sa->callp = &p->p_sysent->sv_table[sa->code]; 580 581 td->td_retval[0] = 0; 582 td->td_retval[1] = frame->tf_edx; 583 584 return (0); 585 } 586 587 static void 588 linux_set_syscall_retval(struct thread *td, int error) 589 { 590 struct trapframe *frame = td->td_frame; 591 592 cpu_set_syscall_retval(td, error); 593 594 if (__predict_false(error != 0)) { 595 if (error != ERESTART && error != EJUSTRETURN) 596 frame->tf_eax = bsd_to_linux_errno(error); 597 } 598 } 599 600 static void 601 linux_set_fork_retval(struct thread *td) 602 { 603 struct trapframe *frame = td->td_frame; 604 605 frame->tf_eax = 0; 606 } 607 608 /* 609 * exec_setregs may initialize some registers differently than Linux 610 * does, thus potentially confusing Linux binaries. If necessary, we 611 * override the exec_setregs default(s) here. 612 */ 613 static void 614 linux_exec_setregs(struct thread *td, struct image_params *imgp, 615 uintptr_t stack) 616 { 617 struct pcb *pcb = td->td_pcb; 618 619 exec_setregs(td, imgp, stack); 620 621 /* Linux sets %gs to 0, we default to _udatasel. */ 622 pcb->pcb_gs = 0; 623 load_gs(0); 624 625 pcb->pcb_initial_npxcw = __LINUX_NPXCW__; 626 } 627 628 struct sysentvec linux_sysvec = { 629 .sv_size = LINUX_SYS_MAXSYSCALL, 630 .sv_table = linux_sysent, 631 .sv_fixup = linux_fixup, 632 .sv_sendsig = linux_sendsig, 633 .sv_sigcode = &_binary_linux_vdso_so_o_start, 634 .sv_szsigcode = &linux_szsigcode, 635 .sv_name = "Linux a.out", 636 .sv_coredump = NULL, 637 .sv_imgact_try = linux_exec_imgact_try, 638 .sv_minsigstksz = LINUX_MINSIGSTKSZ, 639 .sv_minuser = VM_MIN_ADDRESS, 640 .sv_maxuser = VM_MAXUSER_ADDRESS, 641 .sv_usrstack = LINUX_USRSTACK, 642 .sv_psstrings = PS_STRINGS, 643 .sv_psstringssz = sizeof(struct ps_strings), 644 .sv_stackprot = VM_PROT_ALL, 645 .sv_copyout_strings = exec_copyout_strings, 646 .sv_setregs = linux_exec_setregs, 647 .sv_fixlimit = NULL, 648 .sv_maxssiz = NULL, 649 .sv_flags = SV_ABI_LINUX | SV_AOUT | SV_IA32 | SV_ILP32 | 650 SV_SIG_DISCIGN | SV_SIG_WAITNDQ, 651 .sv_set_syscall_retval = linux_set_syscall_retval, 652 .sv_fetch_syscall_args = linux_fetch_syscall_args, 653 .sv_syscallnames = linux_syscallnames, 654 .sv_schedtail = linux_schedtail, 655 .sv_thread_detach = linux_thread_detach, 656 .sv_trap = NULL, 657 .sv_onexec = linux_on_exec_vmspace, 658 .sv_onexit = linux_on_exit, 659 .sv_ontdexit = linux_thread_dtor, 660 .sv_setid_allowed = &linux_setid_allowed_query, 661 .sv_set_fork_retval = linux_set_fork_retval, 662 }; 663 INIT_SYSENTVEC(aout_sysvec, &linux_sysvec); 664 665 struct sysentvec elf_linux_sysvec = { 666 .sv_size = LINUX_SYS_MAXSYSCALL, 667 .sv_table = linux_sysent, 668 .sv_fixup = __elfN(freebsd_fixup), 669 .sv_sendsig = linux_sendsig, 670 .sv_sigcode = &_binary_linux_vdso_so_o_start, 671 .sv_szsigcode = &linux_szsigcode, 672 .sv_name = "Linux ELF32", 673 .sv_coredump = elf32_coredump, 674 .sv_elf_core_osabi = ELFOSABI_NONE, 675 .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR, 676 .sv_elf_core_prepare_notes = __linuxN(prepare_notes), 677 .sv_imgact_try = linux_exec_imgact_try, 678 .sv_minsigstksz = LINUX_MINSIGSTKSZ, 679 .sv_minuser = VM_MIN_ADDRESS, 680 .sv_maxuser = VM_MAXUSER_ADDRESS, 681 .sv_usrstack = LINUX_USRSTACK, 682 .sv_psstrings = LINUX_PS_STRINGS, 683 .sv_psstringssz = sizeof(struct ps_strings), 684 .sv_stackprot = VM_PROT_ALL, 685 .sv_copyout_auxargs = linux_copyout_auxargs, 686 .sv_copyout_strings = __linuxN(copyout_strings), 687 .sv_setregs = linux_exec_setregs, 688 .sv_fixlimit = NULL, 689 .sv_maxssiz = NULL, 690 .sv_flags = SV_ABI_LINUX | SV_IA32 | SV_ILP32 | SV_SHP | 691 SV_SIG_DISCIGN | SV_SIG_WAITNDQ | SV_TIMEKEEP, 692 .sv_set_syscall_retval = linux_set_syscall_retval, 693 .sv_fetch_syscall_args = linux_fetch_syscall_args, 694 .sv_syscallnames = NULL, 695 .sv_shared_page_base = LINUX_SHAREDPAGE, 696 .sv_shared_page_len = PAGE_SIZE, 697 .sv_schedtail = linux_schedtail, 698 .sv_thread_detach = linux_thread_detach, 699 .sv_trap = NULL, 700 .sv_onexec = linux_on_exec_vmspace, 701 .sv_onexit = linux_on_exit, 702 .sv_ontdexit = linux_thread_dtor, 703 .sv_setid_allowed = &linux_setid_allowed_query, 704 .sv_set_fork_retval = linux_set_fork_retval, 705 }; 706 707 static int 708 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp) 709 { 710 int error = 0; 711 712 if (SV_PROC_FLAG(p, SV_SHP) != 0) 713 error = linux_map_vdso(p, linux_vdso_obj, 714 linux_vdso_base, LINUX_VDSOPAGE_SIZE, imgp); 715 if (error == 0) 716 linux_on_exec(p, imgp); 717 return (error); 718 } 719 720 /* 721 * linux_vdso_install() and linux_exec_sysvec_init() must be called 722 * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY). 723 */ 724 static void 725 linux_exec_sysvec_init(void *param) 726 { 727 l_uintptr_t *ktimekeep_base, *ktsc_selector; 728 struct sysentvec *sv; 729 ptrdiff_t tkoff; 730 731 sv = param; 732 /* Fill timekeep_base */ 733 exec_sysvec_init(sv); 734 735 tkoff = kern_timekeep_base - linux_vdso_base; 736 ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff); 737 *ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset; 738 739 tkoff = kern_tsc_selector - linux_vdso_base; 740 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff); 741 *ktsc_selector = linux_vdso_tsc_selector_idx(); 742 if (bootverbose) 743 printf("Linux i386 vDSO tsc_selector: %u\n", *ktsc_selector); 744 745 tkoff = kern_cpu_selector - linux_vdso_base; 746 ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff); 747 *ktsc_selector = linux_vdso_cpu_selector_idx(); 748 if (bootverbose) 749 printf("Linux i386 vDSO cpu_selector: %u\n", *ktsc_selector); 750 } 751 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY, 752 linux_exec_sysvec_init, &elf_linux_sysvec); 753 754 static void 755 linux_vdso_install(const void *param) 756 { 757 char *vdso_start = &_binary_linux_vdso_so_o_start; 758 char *vdso_end = &_binary_linux_vdso_so_o_end; 759 760 linux_szsigcode = vdso_end - vdso_start; 761 MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE); 762 763 linux_vdso_base = LINUX_VDSOPAGE; 764 765 __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base); 766 767 linux_vdso_obj = __elfN(linux_shared_page_init) 768 (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE); 769 bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode); 770 771 linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base); 772 } 773 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST, 774 linux_vdso_install, NULL); 775 776 static void 777 linux_vdso_deinstall(const void *param) 778 { 779 780 __elfN(linux_shared_page_fini)(linux_vdso_obj, 781 linux_vdso_mapping, LINUX_VDSOPAGE_SIZE); 782 } 783 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST, 784 linux_vdso_deinstall, NULL); 785 786 static void 787 linux_vdso_reloc(char *mapping, Elf_Addr offset) 788 { 789 const Elf_Shdr *shdr; 790 const Elf_Rel *rel; 791 const Elf_Ehdr *ehdr; 792 Elf_Addr *where; 793 Elf_Size rtype, symidx; 794 Elf_Addr addr, addend; 795 int i, relcnt; 796 797 MPASS(offset != 0); 798 799 relcnt = 0; 800 ehdr = (const Elf_Ehdr *)mapping; 801 shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff); 802 for (i = 0; i < ehdr->e_shnum; i++) 803 { 804 switch (shdr[i].sh_type) { 805 case SHT_REL: 806 rel = (const Elf_Rel *)(mapping + shdr[i].sh_offset); 807 relcnt = shdr[i].sh_size / sizeof(*rel); 808 break; 809 case SHT_RELA: 810 printf("Linux i386 vDSO: unexpected Rela section\n"); 811 break; 812 } 813 } 814 815 for (i = 0; i < relcnt; i++, rel++) { 816 where = (Elf_Addr *)(mapping + rel->r_offset); 817 addend = *where; 818 rtype = ELF_R_TYPE(rel->r_info); 819 symidx = ELF_R_SYM(rel->r_info); 820 821 switch (rtype) { 822 case R_386_NONE: /* none */ 823 break; 824 825 case R_386_RELATIVE: /* B + A */ 826 addr = (Elf_Addr)PTROUT(offset + addend); 827 if (*where != addr) 828 *where = addr; 829 break; 830 831 case R_386_IRELATIVE: 832 printf("Linux i386 vDSO: unexpected ifunc relocation, " 833 "symbol index %d\n", symidx); 834 break; 835 default: 836 printf("Linux i386 vDSO: unexpected relocation type %d, " 837 "symbol index %d\n", rtype, symidx); 838 } 839 } 840 } 841 842 static Elf_Brandnote linux_brandnote = { 843 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), 844 .hdr.n_descsz = 16, /* XXX at least 16 */ 845 .hdr.n_type = 1, 846 .vendor = GNU_ABI_VENDOR, 847 .flags = BN_TRANSLATE_OSREL, 848 .trans_osrel = linux_trans_osrel 849 }; 850 851 static Elf32_Brandinfo linux_brand = { 852 .brand = ELFOSABI_LINUX, 853 .machine = EM_386, 854 .compat_3_brand = "Linux", 855 .emul_path = linux_emul_path, 856 .interp_path = "/lib/ld-linux.so.1", 857 .sysvec = &elf_linux_sysvec, 858 .interp_newpath = NULL, 859 .brand_note = &linux_brandnote, 860 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 861 }; 862 863 static Elf32_Brandinfo linux_glibc2brand = { 864 .brand = ELFOSABI_LINUX, 865 .machine = EM_386, 866 .compat_3_brand = "Linux", 867 .emul_path = linux_emul_path, 868 .interp_path = "/lib/ld-linux.so.2", 869 .sysvec = &elf_linux_sysvec, 870 .interp_newpath = NULL, 871 .brand_note = &linux_brandnote, 872 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 873 }; 874 875 static Elf32_Brandinfo linux_muslbrand = { 876 .brand = ELFOSABI_LINUX, 877 .machine = EM_386, 878 .compat_3_brand = "Linux", 879 .emul_path = linux_emul_path, 880 .interp_path = "/lib/ld-musl-i386.so.1", 881 .sysvec = &elf_linux_sysvec, 882 .interp_newpath = NULL, 883 .brand_note = &linux_brandnote, 884 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE | 885 LINUX_BI_FUTEX_REQUEUE 886 }; 887 888 Elf32_Brandinfo *linux_brandlist[] = { 889 &linux_brand, 890 &linux_glibc2brand, 891 &linux_muslbrand, 892 NULL 893 }; 894 895 static int 896 linux_elf_modevent(module_t mod, int type, void *data) 897 { 898 Elf32_Brandinfo **brandinfo; 899 int error; 900 struct linux_ioctl_handler **lihp; 901 902 error = 0; 903 904 switch(type) { 905 case MOD_LOAD: 906 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 907 ++brandinfo) 908 if (elf32_insert_brand_entry(*brandinfo) < 0) 909 error = EINVAL; 910 if (error == 0) { 911 SET_FOREACH(lihp, linux_ioctl_handler_set) 912 linux_ioctl_register_handler(*lihp); 913 linux_dev_shm_create(); 914 linux_osd_jail_register(); 915 stclohz = (stathz ? stathz : hz); 916 if (bootverbose) 917 printf("Linux ELF exec handler installed\n"); 918 } else 919 printf("cannot insert Linux ELF brand handler\n"); 920 break; 921 case MOD_UNLOAD: 922 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 923 ++brandinfo) 924 if (elf32_brand_inuse(*brandinfo)) 925 error = EBUSY; 926 if (error == 0) { 927 for (brandinfo = &linux_brandlist[0]; 928 *brandinfo != NULL; ++brandinfo) 929 if (elf32_remove_brand_entry(*brandinfo) < 0) 930 error = EINVAL; 931 } 932 if (error == 0) { 933 SET_FOREACH(lihp, linux_ioctl_handler_set) 934 linux_ioctl_unregister_handler(*lihp); 935 linux_dev_shm_destroy(); 936 linux_osd_jail_deregister(); 937 if (bootverbose) 938 printf("Linux ELF exec handler removed\n"); 939 } else 940 printf("Could not deinstall ELF interpreter entry\n"); 941 break; 942 default: 943 return (EOPNOTSUPP); 944 } 945 return (error); 946 } 947 948 static moduledata_t linux_elf_mod = { 949 "linuxelf", 950 linux_elf_modevent, 951 0 952 }; 953 954 DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY); 955 FEATURE(linux, "Linux 32bit support"); 956