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 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/exec.h> 35 #include <sys/fcntl.h> 36 #include <sys/imgact.h> 37 #include <sys/imgact_aout.h> 38 #include <sys/imgact_elf.h> 39 #include <sys/kernel.h> 40 #include <sys/lock.h> 41 #include <sys/malloc.h> 42 #include <sys/module.h> 43 #include <sys/mutex.h> 44 #include <sys/proc.h> 45 #include <sys/signalvar.h> 46 #include <sys/syscallsubr.h> 47 #include <sys/sysctl.h> 48 #include <sys/sysent.h> 49 #include <sys/sysproto.h> 50 #include <sys/vnode.h> 51 52 #include <vm/vm.h> 53 #include <vm/pmap.h> 54 #include <vm/vm_extern.h> 55 #include <vm/vm_map.h> 56 #include <vm/vm_object.h> 57 #include <vm/vm_page.h> 58 #include <vm/vm_param.h> 59 60 #include <machine/cpu.h> 61 #include <machine/cputypes.h> 62 #include <machine/md_var.h> 63 #include <machine/pcb.h> 64 #include <machine/trap.h> 65 66 #include <i386/linux/linux.h> 67 #include <i386/linux/linux_proto.h> 68 #include <compat/linux/linux_emul.h> 69 #include <compat/linux/linux_ioctl.h> 70 #include <compat/linux/linux_mib.h> 71 #include <compat/linux/linux_misc.h> 72 #include <compat/linux/linux_signal.h> 73 #include <compat/linux/linux_util.h> 74 #include <compat/linux/linux_vdso.h> 75 76 MODULE_VERSION(linux, 1); 77 78 #define LINUX_PS_STRINGS (LINUX_USRSTACK - sizeof(struct ps_strings)) 79 80 static int linux_szsigcode; 81 static vm_object_t linux_shared_page_obj; 82 static char *linux_shared_page_mapping; 83 extern char _binary_linux_locore_o_start; 84 extern char _binary_linux_locore_o_end; 85 86 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL]; 87 88 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler); 89 90 static int linux_fixup(uintptr_t *stack_base, 91 struct image_params *iparams); 92 static int linux_fixup_elf(uintptr_t *stack_base, 93 struct image_params *iparams); 94 static void linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask); 95 static void linux_exec_setregs(struct thread *td, 96 struct image_params *imgp, uintptr_t stack); 97 static int linux_copyout_strings(struct image_params *imgp, 98 uintptr_t *stack_base); 99 static bool linux_trans_osrel(const Elf_Note *note, int32_t *osrel); 100 static void linux_vdso_install(void *param); 101 static void linux_vdso_deinstall(void *param); 102 103 static int linux_szplatform; 104 const char *linux_kplatform; 105 106 #define LINUX_T_UNKNOWN 255 107 static int _bsd_to_linux_trapcode[] = { 108 LINUX_T_UNKNOWN, /* 0 */ 109 6, /* 1 T_PRIVINFLT */ 110 LINUX_T_UNKNOWN, /* 2 */ 111 3, /* 3 T_BPTFLT */ 112 LINUX_T_UNKNOWN, /* 4 */ 113 LINUX_T_UNKNOWN, /* 5 */ 114 16, /* 6 T_ARITHTRAP */ 115 254, /* 7 T_ASTFLT */ 116 LINUX_T_UNKNOWN, /* 8 */ 117 13, /* 9 T_PROTFLT */ 118 1, /* 10 T_TRCTRAP */ 119 LINUX_T_UNKNOWN, /* 11 */ 120 14, /* 12 T_PAGEFLT */ 121 LINUX_T_UNKNOWN, /* 13 */ 122 17, /* 14 T_ALIGNFLT */ 123 LINUX_T_UNKNOWN, /* 15 */ 124 LINUX_T_UNKNOWN, /* 16 */ 125 LINUX_T_UNKNOWN, /* 17 */ 126 0, /* 18 T_DIVIDE */ 127 2, /* 19 T_NMI */ 128 4, /* 20 T_OFLOW */ 129 5, /* 21 T_BOUND */ 130 7, /* 22 T_DNA */ 131 8, /* 23 T_DOUBLEFLT */ 132 9, /* 24 T_FPOPFLT */ 133 10, /* 25 T_TSSFLT */ 134 11, /* 26 T_SEGNPFLT */ 135 12, /* 27 T_STKFLT */ 136 18, /* 28 T_MCHK */ 137 19, /* 29 T_XMMFLT */ 138 15 /* 30 T_RESERVED */ 139 }; 140 #define bsd_to_linux_trapcode(code) \ 141 ((code)<nitems(_bsd_to_linux_trapcode)? \ 142 _bsd_to_linux_trapcode[(code)]: \ 143 LINUX_T_UNKNOWN) 144 145 LINUX_VDSO_SYM_INTPTR(linux_sigcode); 146 LINUX_VDSO_SYM_INTPTR(linux_rt_sigcode); 147 LINUX_VDSO_SYM_INTPTR(linux_vsyscall); 148 149 /* 150 * If FreeBSD & Linux have a difference of opinion about what a trap 151 * means, deal with it here. 152 * 153 * MPSAFE 154 */ 155 static int 156 linux_translate_traps(int signal, int trap_code) 157 { 158 if (signal != SIGBUS) 159 return (signal); 160 switch (trap_code) { 161 case T_PROTFLT: 162 case T_TSSFLT: 163 case T_DOUBLEFLT: 164 case T_PAGEFLT: 165 return (SIGSEGV); 166 default: 167 return (signal); 168 } 169 } 170 171 static int 172 linux_fixup(uintptr_t *stack_base, struct image_params *imgp) 173 { 174 register_t *base, *argv, *envp; 175 176 base = (register_t *)*stack_base; 177 argv = base; 178 envp = base + (imgp->args->argc + 1); 179 base--; 180 suword(base, (intptr_t)envp); 181 base--; 182 suword(base, (intptr_t)argv); 183 base--; 184 suword(base, imgp->args->argc); 185 *stack_base = (uintptr_t)base; 186 return (0); 187 } 188 189 static int 190 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base) 191 { 192 struct proc *p; 193 Elf32_Auxargs *args; 194 Elf32_Auxinfo *argarray, *pos; 195 Elf32_Addr *uplatform; 196 struct ps_strings *arginfo; 197 int error, issetugid; 198 199 p = imgp->proc; 200 issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0; 201 arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings; 202 uplatform = (Elf32_Addr *)((caddr_t)arginfo - linux_szplatform); 203 args = (Elf32_Auxargs *)imgp->auxargs; 204 argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP, 205 M_WAITOK | M_ZERO); 206 207 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, 208 imgp->proc->p_sysent->sv_shared_page_base); 209 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO, linux_vsyscall); 210 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature); 211 212 /* 213 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0, 214 * as it has appeared in the 2.4.0-rc7 first time. 215 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK), 216 * glibc falls back to the hard-coded CLK_TCK value when aux entry 217 * is not present. 218 * Also see linux_times() implementation. 219 */ 220 if (linux_kernver(curthread) >= LINUX_KERNVER_2004000) 221 AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz); 222 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 223 AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 224 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 225 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 226 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 227 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 228 AUXARGS_ENTRY(pos, AT_BASE, args->base); 229 AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid); 230 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid); 231 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid); 232 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid); 233 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid); 234 AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(uplatform)); 235 AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary); 236 if (imgp->execpathp != 0) 237 AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp); 238 if (args->execfd != -1) 239 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 240 AUXARGS_ENTRY(pos, AT_NULL, 0); 241 242 free(imgp->auxargs, M_TEMP); 243 imgp->auxargs = NULL; 244 KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs")); 245 246 error = copyout(argarray, (void *)base, 247 sizeof(*argarray) * LINUX_AT_COUNT); 248 free(argarray, M_TEMP); 249 return (error); 250 } 251 252 static int 253 linux_fixup_elf(uintptr_t *stack_base, struct image_params *imgp) 254 { 255 register_t *base; 256 257 base = (register_t *)*stack_base; 258 base--; 259 if (suword(base, (register_t)imgp->args->argc) == -1) 260 return (EFAULT); 261 *stack_base = (uintptr_t)base; 262 return (0); 263 } 264 265 /* 266 * Copied from kern/kern_exec.c 267 */ 268 static int 269 linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base) 270 { 271 int argc, envc, error; 272 char **vectp; 273 char *stringp; 274 uintptr_t destp, ustringp; 275 struct ps_strings *arginfo; 276 char canary[LINUX_AT_RANDOM_LEN]; 277 size_t execpath_len; 278 struct proc *p; 279 280 /* Calculate string base and vector table pointers. */ 281 p = imgp->proc; 282 if (imgp->execpath != NULL && imgp->auxargs != NULL) 283 execpath_len = strlen(imgp->execpath) + 1; 284 else 285 execpath_len = 0; 286 arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings; 287 destp = (uintptr_t)arginfo; 288 289 /* Install LINUX_PLATFORM. */ 290 destp -= linux_szplatform; 291 destp = rounddown2(destp, sizeof(void *)); 292 error = copyout(linux_kplatform, (void *)destp, linux_szplatform); 293 if (error != 0) 294 return (error); 295 296 if (execpath_len != 0) { 297 destp -= execpath_len; 298 destp = rounddown2(destp, sizeof(void *)); 299 imgp->execpathp = (void *)destp; 300 error = copyout(imgp->execpath, imgp->execpathp, execpath_len); 301 if (error != 0) 302 return (error); 303 } 304 305 /* Prepare the canary for SSP. */ 306 arc4rand(canary, sizeof(canary), 0); 307 destp -= roundup(sizeof(canary), sizeof(void *)); 308 imgp->canary = (void *)destp; 309 error = copyout(canary, imgp->canary, sizeof(canary)); 310 if (error != 0) 311 return (error); 312 313 /* Allocate room for the argument and environment strings. */ 314 destp -= ARG_MAX - imgp->args->stringspace; 315 destp = rounddown2(destp, sizeof(void *)); 316 ustringp = destp; 317 318 if (imgp->auxargs) { 319 /* 320 * Allocate room on the stack for the ELF auxargs 321 * array. It has LINUX_AT_COUNT entries. 322 */ 323 destp -= LINUX_AT_COUNT * sizeof(Elf32_Auxinfo); 324 destp = rounddown2(destp, sizeof(void *)); 325 } 326 327 vectp = (char **)destp; 328 329 /* 330 * Allocate room for the argv[] and env vectors including the 331 * terminating NULL pointers. 332 */ 333 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1; 334 335 /* vectp also becomes our initial stack base. */ 336 *stack_base = (uintptr_t)vectp; 337 338 stringp = imgp->args->begin_argv; 339 argc = imgp->args->argc; 340 envc = imgp->args->envc; 341 342 /* Copy out strings - arguments and environment. */ 343 error = copyout(stringp, (void *)ustringp, 344 ARG_MAX - imgp->args->stringspace); 345 if (error != 0) 346 return (error); 347 348 /* Fill in "ps_strings" struct for ps, w, etc. */ 349 if (suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp) != 0 || 350 suword(&arginfo->ps_nargvstr, argc) != 0) 351 return (EFAULT); 352 353 /* Fill in argument portion of vector table. */ 354 for (; argc > 0; --argc) { 355 if (suword(vectp++, ustringp) != 0) 356 return (EFAULT); 357 while (*stringp++ != 0) 358 ustringp++; 359 ustringp++; 360 } 361 362 /* A null vector table pointer separates the argp's from the envp's. */ 363 if (suword(vectp++, 0) != 0) 364 return (EFAULT); 365 366 if (suword(&arginfo->ps_envstr, (long)(intptr_t)vectp) != 0 || 367 suword(&arginfo->ps_nenvstr, envc) != 0) 368 return (EFAULT); 369 370 /* Fill in environment portion of vector table. */ 371 for (; envc > 0; --envc) { 372 if (suword(vectp++, ustringp) != 0) 373 return (EFAULT); 374 while (*stringp++ != 0) 375 ustringp++; 376 ustringp++; 377 } 378 379 /* The end of the vector table is a null pointer. */ 380 if (suword(vectp, 0) != 0) 381 return (EFAULT); 382 383 if (imgp->auxargs) { 384 vectp++; 385 error = imgp->sysent->sv_copyout_auxargs(imgp, 386 (uintptr_t)vectp); 387 if (error != 0) 388 return (error); 389 } 390 391 return (0); 392 } 393 394 static void 395 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 396 { 397 struct thread *td = curthread; 398 struct proc *p = td->td_proc; 399 struct sigacts *psp; 400 struct trapframe *regs; 401 struct l_rt_sigframe *fp, frame; 402 int sig, code; 403 int oonstack; 404 405 sig = ksi->ksi_signo; 406 code = ksi->ksi_code; 407 PROC_LOCK_ASSERT(p, MA_OWNED); 408 psp = p->p_sigacts; 409 mtx_assert(&psp->ps_mtx, MA_OWNED); 410 regs = td->td_frame; 411 oonstack = sigonstack(regs->tf_esp); 412 413 /* Allocate space for the signal handler context. */ 414 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack && 415 SIGISMEMBER(psp->ps_sigonstack, sig)) { 416 fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp + 417 td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe)); 418 } else 419 fp = (struct l_rt_sigframe *)regs->tf_esp - 1; 420 mtx_unlock(&psp->ps_mtx); 421 422 /* Build the argument list for the signal handler. */ 423 sig = bsd_to_linux_signal(sig); 424 425 bzero(&frame, sizeof(frame)); 426 427 frame.sf_handler = catcher; 428 frame.sf_sig = sig; 429 frame.sf_siginfo = &fp->sf_si; 430 frame.sf_ucontext = &fp->sf_sc; 431 432 /* Fill in POSIX parts. */ 433 siginfo_to_lsiginfo(&ksi->ksi_info, &frame.sf_si, sig); 434 435 /* Build the signal context to be used by sigreturn. */ 436 frame.sf_sc.uc_flags = 0; /* XXX ??? */ 437 frame.sf_sc.uc_link = NULL; /* XXX ??? */ 438 439 frame.sf_sc.uc_stack.ss_sp = td->td_sigstk.ss_sp; 440 frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size; 441 frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) 442 ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE; 443 PROC_UNLOCK(p); 444 445 bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask); 446 447 frame.sf_sc.uc_mcontext.sc_mask = frame.sf_sc.uc_sigmask.__mask; 448 frame.sf_sc.uc_mcontext.sc_gs = rgs(); 449 frame.sf_sc.uc_mcontext.sc_fs = regs->tf_fs; 450 frame.sf_sc.uc_mcontext.sc_es = regs->tf_es; 451 frame.sf_sc.uc_mcontext.sc_ds = regs->tf_ds; 452 frame.sf_sc.uc_mcontext.sc_edi = regs->tf_edi; 453 frame.sf_sc.uc_mcontext.sc_esi = regs->tf_esi; 454 frame.sf_sc.uc_mcontext.sc_ebp = regs->tf_ebp; 455 frame.sf_sc.uc_mcontext.sc_ebx = regs->tf_ebx; 456 frame.sf_sc.uc_mcontext.sc_esp = regs->tf_esp; 457 frame.sf_sc.uc_mcontext.sc_edx = regs->tf_edx; 458 frame.sf_sc.uc_mcontext.sc_ecx = regs->tf_ecx; 459 frame.sf_sc.uc_mcontext.sc_eax = regs->tf_eax; 460 frame.sf_sc.uc_mcontext.sc_eip = regs->tf_eip; 461 frame.sf_sc.uc_mcontext.sc_cs = regs->tf_cs; 462 frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_eflags; 463 frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_esp; 464 frame.sf_sc.uc_mcontext.sc_ss = regs->tf_ss; 465 frame.sf_sc.uc_mcontext.sc_err = regs->tf_err; 466 frame.sf_sc.uc_mcontext.sc_cr2 = (register_t)ksi->ksi_addr; 467 frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code); 468 469 if (copyout(&frame, fp, sizeof(frame)) != 0) { 470 /* 471 * Process has trashed its stack; give it an illegal 472 * instruction to halt it in its tracks. 473 */ 474 PROC_LOCK(p); 475 sigexit(td, SIGILL); 476 } 477 478 /* Build context to run handler in. */ 479 regs->tf_esp = (int)fp; 480 regs->tf_eip = linux_rt_sigcode; 481 regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D); 482 regs->tf_cs = _ucodesel; 483 regs->tf_ds = _udatasel; 484 regs->tf_es = _udatasel; 485 regs->tf_fs = _udatasel; 486 regs->tf_ss = _udatasel; 487 PROC_LOCK(p); 488 mtx_lock(&psp->ps_mtx); 489 } 490 491 /* 492 * Send an interrupt to process. 493 * 494 * Stack is set up to allow sigcode stored 495 * in u. to call routine, followed by kcall 496 * to sigreturn routine below. After sigreturn 497 * resets the signal mask, the stack, and the 498 * frame pointer, it returns to the user 499 * specified pc, psl. 500 */ 501 static void 502 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 503 { 504 struct thread *td = curthread; 505 struct proc *p = td->td_proc; 506 struct sigacts *psp; 507 struct trapframe *regs; 508 struct l_sigframe *fp, frame; 509 l_sigset_t lmask; 510 int sig, code; 511 int oonstack; 512 513 PROC_LOCK_ASSERT(p, MA_OWNED); 514 psp = p->p_sigacts; 515 sig = ksi->ksi_signo; 516 code = ksi->ksi_code; 517 mtx_assert(&psp->ps_mtx, MA_OWNED); 518 if (SIGISMEMBER(psp->ps_siginfo, sig)) { 519 /* Signal handler installed with SA_SIGINFO. */ 520 linux_rt_sendsig(catcher, ksi, mask); 521 return; 522 } 523 regs = td->td_frame; 524 oonstack = sigonstack(regs->tf_esp); 525 526 /* Allocate space for the signal handler context. */ 527 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack && 528 SIGISMEMBER(psp->ps_sigonstack, sig)) { 529 fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp + 530 td->td_sigstk.ss_size - sizeof(struct l_sigframe)); 531 } else 532 fp = (struct l_sigframe *)regs->tf_esp - 1; 533 mtx_unlock(&psp->ps_mtx); 534 PROC_UNLOCK(p); 535 536 /* Build the argument list for the signal handler. */ 537 sig = bsd_to_linux_signal(sig); 538 539 bzero(&frame, sizeof(frame)); 540 541 frame.sf_handler = catcher; 542 frame.sf_sig = sig; 543 544 bsd_to_linux_sigset(mask, &lmask); 545 546 /* Build the signal context to be used by sigreturn. */ 547 frame.sf_sc.sc_mask = lmask.__mask; 548 frame.sf_sc.sc_gs = rgs(); 549 frame.sf_sc.sc_fs = regs->tf_fs; 550 frame.sf_sc.sc_es = regs->tf_es; 551 frame.sf_sc.sc_ds = regs->tf_ds; 552 frame.sf_sc.sc_edi = regs->tf_edi; 553 frame.sf_sc.sc_esi = regs->tf_esi; 554 frame.sf_sc.sc_ebp = regs->tf_ebp; 555 frame.sf_sc.sc_ebx = regs->tf_ebx; 556 frame.sf_sc.sc_esp = regs->tf_esp; 557 frame.sf_sc.sc_edx = regs->tf_edx; 558 frame.sf_sc.sc_ecx = regs->tf_ecx; 559 frame.sf_sc.sc_eax = regs->tf_eax; 560 frame.sf_sc.sc_eip = regs->tf_eip; 561 frame.sf_sc.sc_cs = regs->tf_cs; 562 frame.sf_sc.sc_eflags = regs->tf_eflags; 563 frame.sf_sc.sc_esp_at_signal = regs->tf_esp; 564 frame.sf_sc.sc_ss = regs->tf_ss; 565 frame.sf_sc.sc_err = regs->tf_err; 566 frame.sf_sc.sc_cr2 = (register_t)ksi->ksi_addr; 567 frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(ksi->ksi_trapno); 568 569 frame.sf_extramask[0] = lmask.__mask; 570 571 if (copyout(&frame, fp, sizeof(frame)) != 0) { 572 /* 573 * Process has trashed its stack; give it an illegal 574 * instruction to halt it in its tracks. 575 */ 576 PROC_LOCK(p); 577 sigexit(td, SIGILL); 578 } 579 580 /* Build context to run handler in. */ 581 regs->tf_esp = (int)fp; 582 regs->tf_eip = linux_sigcode; 583 regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D); 584 regs->tf_cs = _ucodesel; 585 regs->tf_ds = _udatasel; 586 regs->tf_es = _udatasel; 587 regs->tf_fs = _udatasel; 588 regs->tf_ss = _udatasel; 589 PROC_LOCK(p); 590 mtx_lock(&psp->ps_mtx); 591 } 592 593 /* 594 * System call to cleanup state after a signal 595 * has been taken. Reset signal mask and 596 * stack state from context left by sendsig (above). 597 * Return to previous pc and psl as specified by 598 * context left by sendsig. Check carefully to 599 * make sure that the user has not modified the 600 * psl to gain improper privileges or to cause 601 * a machine fault. 602 */ 603 int 604 linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args) 605 { 606 struct l_sigframe frame; 607 struct trapframe *regs; 608 l_sigset_t lmask; 609 sigset_t bmask; 610 int eflags; 611 ksiginfo_t ksi; 612 613 regs = td->td_frame; 614 615 /* 616 * The trampoline code hands us the sigframe. 617 * It is unsafe to keep track of it ourselves, in the event that a 618 * program jumps out of a signal handler. 619 */ 620 if (copyin(args->sfp, &frame, sizeof(frame)) != 0) 621 return (EFAULT); 622 623 /* Check for security violations. */ 624 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 625 eflags = frame.sf_sc.sc_eflags; 626 if (!EFLAGS_SECURE(eflags, regs->tf_eflags)) 627 return (EINVAL); 628 629 /* 630 * Don't allow users to load a valid privileged %cs. Let the 631 * hardware check for invalid selectors, excess privilege in 632 * other selectors, invalid %eip's and invalid %esp's. 633 */ 634 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 635 if (!CS_SECURE(frame.sf_sc.sc_cs)) { 636 ksiginfo_init_trap(&ksi); 637 ksi.ksi_signo = SIGBUS; 638 ksi.ksi_code = BUS_OBJERR; 639 ksi.ksi_trapno = T_PROTFLT; 640 ksi.ksi_addr = (void *)regs->tf_eip; 641 trapsignal(td, &ksi); 642 return (EINVAL); 643 } 644 645 lmask.__mask = frame.sf_sc.sc_mask; 646 linux_to_bsd_sigset(&lmask, &bmask); 647 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0); 648 649 /* Restore signal context. */ 650 /* %gs was restored by the trampoline. */ 651 regs->tf_fs = frame.sf_sc.sc_fs; 652 regs->tf_es = frame.sf_sc.sc_es; 653 regs->tf_ds = frame.sf_sc.sc_ds; 654 regs->tf_edi = frame.sf_sc.sc_edi; 655 regs->tf_esi = frame.sf_sc.sc_esi; 656 regs->tf_ebp = frame.sf_sc.sc_ebp; 657 regs->tf_ebx = frame.sf_sc.sc_ebx; 658 regs->tf_edx = frame.sf_sc.sc_edx; 659 regs->tf_ecx = frame.sf_sc.sc_ecx; 660 regs->tf_eax = frame.sf_sc.sc_eax; 661 regs->tf_eip = frame.sf_sc.sc_eip; 662 regs->tf_cs = frame.sf_sc.sc_cs; 663 regs->tf_eflags = eflags; 664 regs->tf_esp = frame.sf_sc.sc_esp_at_signal; 665 regs->tf_ss = frame.sf_sc.sc_ss; 666 667 return (EJUSTRETURN); 668 } 669 670 /* 671 * System call to cleanup state after a signal 672 * has been taken. Reset signal mask and 673 * stack state from context left by rt_sendsig (above). 674 * Return to previous pc and psl as specified by 675 * context left by sendsig. Check carefully to 676 * make sure that the user has not modified the 677 * psl to gain improper privileges or to cause 678 * a machine fault. 679 */ 680 int 681 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) 682 { 683 struct l_ucontext uc; 684 struct l_sigcontext *context; 685 sigset_t bmask; 686 l_stack_t *lss; 687 stack_t ss; 688 struct trapframe *regs; 689 int eflags; 690 ksiginfo_t ksi; 691 692 regs = td->td_frame; 693 694 /* 695 * The trampoline code hands us the ucontext. 696 * It is unsafe to keep track of it ourselves, in the event that a 697 * program jumps out of a signal handler. 698 */ 699 if (copyin(args->ucp, &uc, sizeof(uc)) != 0) 700 return (EFAULT); 701 702 context = &uc.uc_mcontext; 703 704 /* Check for security violations. */ 705 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 706 eflags = context->sc_eflags; 707 if (!EFLAGS_SECURE(eflags, regs->tf_eflags)) 708 return (EINVAL); 709 710 /* 711 * Don't allow users to load a valid privileged %cs. Let the 712 * hardware check for invalid selectors, excess privilege in 713 * other selectors, invalid %eip's and invalid %esp's. 714 */ 715 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 716 if (!CS_SECURE(context->sc_cs)) { 717 ksiginfo_init_trap(&ksi); 718 ksi.ksi_signo = SIGBUS; 719 ksi.ksi_code = BUS_OBJERR; 720 ksi.ksi_trapno = T_PROTFLT; 721 ksi.ksi_addr = (void *)regs->tf_eip; 722 trapsignal(td, &ksi); 723 return (EINVAL); 724 } 725 726 linux_to_bsd_sigset(&uc.uc_sigmask, &bmask); 727 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0); 728 729 /* Restore signal context. */ 730 /* %gs was restored by the trampoline. */ 731 regs->tf_fs = context->sc_fs; 732 regs->tf_es = context->sc_es; 733 regs->tf_ds = context->sc_ds; 734 regs->tf_edi = context->sc_edi; 735 regs->tf_esi = context->sc_esi; 736 regs->tf_ebp = context->sc_ebp; 737 regs->tf_ebx = context->sc_ebx; 738 regs->tf_edx = context->sc_edx; 739 regs->tf_ecx = context->sc_ecx; 740 regs->tf_eax = context->sc_eax; 741 regs->tf_eip = context->sc_eip; 742 regs->tf_cs = context->sc_cs; 743 regs->tf_eflags = eflags; 744 regs->tf_esp = context->sc_esp_at_signal; 745 regs->tf_ss = context->sc_ss; 746 747 /* Call sigaltstack & ignore results. */ 748 lss = &uc.uc_stack; 749 ss.ss_sp = lss->ss_sp; 750 ss.ss_size = lss->ss_size; 751 ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags); 752 753 (void)kern_sigaltstack(td, &ss, NULL); 754 755 return (EJUSTRETURN); 756 } 757 758 static int 759 linux_fetch_syscall_args(struct thread *td) 760 { 761 struct proc *p; 762 struct trapframe *frame; 763 struct syscall_args *sa; 764 765 p = td->td_proc; 766 frame = td->td_frame; 767 sa = &td->td_sa; 768 769 sa->code = frame->tf_eax; 770 sa->args[0] = frame->tf_ebx; 771 sa->args[1] = frame->tf_ecx; 772 sa->args[2] = frame->tf_edx; 773 sa->args[3] = frame->tf_esi; 774 sa->args[4] = frame->tf_edi; 775 sa->args[5] = frame->tf_ebp; /* Unconfirmed */ 776 777 if (sa->code >= p->p_sysent->sv_size) 778 /* nosys */ 779 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1]; 780 else 781 sa->callp = &p->p_sysent->sv_table[sa->code]; 782 783 td->td_retval[0] = 0; 784 td->td_retval[1] = frame->tf_edx; 785 786 return (0); 787 } 788 789 static void 790 linux_set_syscall_retval(struct thread *td, int error) 791 { 792 struct trapframe *frame = td->td_frame; 793 794 cpu_set_syscall_retval(td, error); 795 796 if (__predict_false(error != 0)) { 797 if (error != ERESTART && error != EJUSTRETURN) 798 frame->tf_eax = bsd_to_linux_errno(error); 799 } 800 } 801 802 /* 803 * exec_setregs may initialize some registers differently than Linux 804 * does, thus potentially confusing Linux binaries. If necessary, we 805 * override the exec_setregs default(s) here. 806 */ 807 static void 808 linux_exec_setregs(struct thread *td, struct image_params *imgp, 809 uintptr_t stack) 810 { 811 struct pcb *pcb = td->td_pcb; 812 813 exec_setregs(td, imgp, stack); 814 815 /* Linux sets %gs to 0, we default to _udatasel. */ 816 pcb->pcb_gs = 0; 817 load_gs(0); 818 819 pcb->pcb_initial_npxcw = __LINUX_NPXCW__; 820 } 821 822 static void 823 linux_get_machine(const char **dst) 824 { 825 826 switch (cpu_class) { 827 case CPUCLASS_686: 828 *dst = "i686"; 829 break; 830 case CPUCLASS_586: 831 *dst = "i586"; 832 break; 833 case CPUCLASS_486: 834 *dst = "i486"; 835 break; 836 default: 837 *dst = "i386"; 838 } 839 } 840 841 struct sysentvec linux_sysvec = { 842 .sv_size = LINUX_SYS_MAXSYSCALL, 843 .sv_table = linux_sysent, 844 .sv_transtrap = linux_translate_traps, 845 .sv_fixup = linux_fixup, 846 .sv_sendsig = linux_sendsig, 847 .sv_sigcode = &_binary_linux_locore_o_start, 848 .sv_szsigcode = &linux_szsigcode, 849 .sv_name = "Linux a.out", 850 .sv_coredump = NULL, 851 .sv_imgact_try = linux_exec_imgact_try, 852 .sv_minsigstksz = LINUX_MINSIGSTKSZ, 853 .sv_minuser = VM_MIN_ADDRESS, 854 .sv_maxuser = VM_MAXUSER_ADDRESS, 855 .sv_usrstack = LINUX_USRSTACK, 856 .sv_psstrings = PS_STRINGS, 857 .sv_stackprot = VM_PROT_ALL, 858 .sv_copyout_strings = exec_copyout_strings, 859 .sv_setregs = linux_exec_setregs, 860 .sv_fixlimit = NULL, 861 .sv_maxssiz = NULL, 862 .sv_flags = SV_ABI_LINUX | SV_AOUT | SV_IA32 | SV_ILP32, 863 .sv_set_syscall_retval = linux_set_syscall_retval, 864 .sv_fetch_syscall_args = linux_fetch_syscall_args, 865 .sv_syscallnames = NULL, 866 .sv_shared_page_base = LINUX_SHAREDPAGE, 867 .sv_shared_page_len = PAGE_SIZE, 868 .sv_schedtail = linux_schedtail, 869 .sv_thread_detach = linux_thread_detach, 870 .sv_trap = NULL, 871 .sv_onexec = linux_on_exec, 872 .sv_onexit = linux_on_exit, 873 .sv_ontdexit = linux_thread_dtor, 874 .sv_setid_allowed = &linux_setid_allowed_query, 875 }; 876 INIT_SYSENTVEC(aout_sysvec, &linux_sysvec); 877 878 struct sysentvec elf_linux_sysvec = { 879 .sv_size = LINUX_SYS_MAXSYSCALL, 880 .sv_table = linux_sysent, 881 .sv_transtrap = linux_translate_traps, 882 .sv_fixup = linux_fixup_elf, 883 .sv_sendsig = linux_sendsig, 884 .sv_sigcode = &_binary_linux_locore_o_start, 885 .sv_szsigcode = &linux_szsigcode, 886 .sv_name = "Linux ELF32", 887 .sv_coredump = elf32_coredump, 888 .sv_imgact_try = linux_exec_imgact_try, 889 .sv_minsigstksz = LINUX_MINSIGSTKSZ, 890 .sv_minuser = VM_MIN_ADDRESS, 891 .sv_maxuser = VM_MAXUSER_ADDRESS, 892 .sv_usrstack = LINUX_USRSTACK, 893 .sv_psstrings = LINUX_PS_STRINGS, 894 .sv_stackprot = VM_PROT_ALL, 895 .sv_copyout_auxargs = linux_copyout_auxargs, 896 .sv_copyout_strings = linux_copyout_strings, 897 .sv_setregs = linux_exec_setregs, 898 .sv_fixlimit = NULL, 899 .sv_maxssiz = NULL, 900 .sv_flags = SV_ABI_LINUX | SV_IA32 | SV_ILP32 | SV_SHP, 901 .sv_set_syscall_retval = linux_set_syscall_retval, 902 .sv_fetch_syscall_args = linux_fetch_syscall_args, 903 .sv_syscallnames = NULL, 904 .sv_shared_page_base = LINUX_SHAREDPAGE, 905 .sv_shared_page_len = PAGE_SIZE, 906 .sv_schedtail = linux_schedtail, 907 .sv_thread_detach = linux_thread_detach, 908 .sv_trap = NULL, 909 .sv_onexec = linux_on_exec, 910 .sv_onexit = linux_on_exit, 911 .sv_ontdexit = linux_thread_dtor, 912 .sv_setid_allowed = &linux_setid_allowed_query, 913 }; 914 915 static void 916 linux_vdso_install(void *param) 917 { 918 919 linux_szsigcode = (&_binary_linux_locore_o_end - 920 &_binary_linux_locore_o_start); 921 922 if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len) 923 panic("Linux invalid vdso size\n"); 924 925 __elfN(linux_vdso_fixup)(&elf_linux_sysvec); 926 927 linux_shared_page_obj = __elfN(linux_shared_page_init) 928 (&linux_shared_page_mapping); 929 930 __elfN(linux_vdso_reloc)(&elf_linux_sysvec); 931 932 bcopy(elf_linux_sysvec.sv_sigcode, linux_shared_page_mapping, 933 linux_szsigcode); 934 elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj; 935 } 936 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY, 937 linux_vdso_install, NULL); 938 939 static void 940 linux_vdso_deinstall(void *param) 941 { 942 943 __elfN(linux_shared_page_fini)(linux_shared_page_obj, 944 linux_shared_page_mapping); 945 } 946 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST, 947 linux_vdso_deinstall, NULL); 948 949 static char GNU_ABI_VENDOR[] = "GNU"; 950 static int GNULINUX_ABI_DESC = 0; 951 952 static bool 953 linux_trans_osrel(const Elf_Note *note, int32_t *osrel) 954 { 955 const Elf32_Word *desc; 956 uintptr_t p; 957 958 p = (uintptr_t)(note + 1); 959 p += roundup2(note->n_namesz, sizeof(Elf32_Addr)); 960 961 desc = (const Elf32_Word *)p; 962 if (desc[0] != GNULINUX_ABI_DESC) 963 return (false); 964 965 /* 966 * For Linux we encode osrel using the Linux convention of 967 * (version << 16) | (major << 8) | (minor) 968 * See macro in linux_mib.h 969 */ 970 *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]); 971 972 return (true); 973 } 974 975 static Elf_Brandnote linux_brandnote = { 976 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), 977 .hdr.n_descsz = 16, /* XXX at least 16 */ 978 .hdr.n_type = 1, 979 .vendor = GNU_ABI_VENDOR, 980 .flags = BN_TRANSLATE_OSREL, 981 .trans_osrel = linux_trans_osrel 982 }; 983 984 static Elf32_Brandinfo linux_brand = { 985 .brand = ELFOSABI_LINUX, 986 .machine = EM_386, 987 .compat_3_brand = "Linux", 988 .emul_path = linux_emul_path, 989 .interp_path = "/lib/ld-linux.so.1", 990 .sysvec = &elf_linux_sysvec, 991 .interp_newpath = NULL, 992 .brand_note = &linux_brandnote, 993 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 994 }; 995 996 static Elf32_Brandinfo linux_glibc2brand = { 997 .brand = ELFOSABI_LINUX, 998 .machine = EM_386, 999 .compat_3_brand = "Linux", 1000 .emul_path = linux_emul_path, 1001 .interp_path = "/lib/ld-linux.so.2", 1002 .sysvec = &elf_linux_sysvec, 1003 .interp_newpath = NULL, 1004 .brand_note = &linux_brandnote, 1005 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 1006 }; 1007 1008 static Elf32_Brandinfo linux_muslbrand = { 1009 .brand = ELFOSABI_LINUX, 1010 .machine = EM_386, 1011 .compat_3_brand = "Linux", 1012 .emul_path = linux_emul_path, 1013 .interp_path = "/lib/ld-musl-i386.so.1", 1014 .sysvec = &elf_linux_sysvec, 1015 .interp_newpath = NULL, 1016 .brand_note = &linux_brandnote, 1017 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 1018 }; 1019 1020 Elf32_Brandinfo *linux_brandlist[] = { 1021 &linux_brand, 1022 &linux_glibc2brand, 1023 &linux_muslbrand, 1024 NULL 1025 }; 1026 1027 static int 1028 linux_elf_modevent(module_t mod, int type, void *data) 1029 { 1030 Elf32_Brandinfo **brandinfo; 1031 int error; 1032 struct linux_ioctl_handler **lihp; 1033 1034 error = 0; 1035 1036 switch(type) { 1037 case MOD_LOAD: 1038 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 1039 ++brandinfo) 1040 if (elf32_insert_brand_entry(*brandinfo) < 0) 1041 error = EINVAL; 1042 if (error == 0) { 1043 SET_FOREACH(lihp, linux_ioctl_handler_set) 1044 linux_ioctl_register_handler(*lihp); 1045 LIST_INIT(&futex_list); 1046 mtx_init(&futex_mtx, "ftllk", NULL, MTX_DEF); 1047 linux_get_machine(&linux_kplatform); 1048 linux_szplatform = roundup(strlen(linux_kplatform) + 1, 1049 sizeof(char *)); 1050 linux_dev_shm_create(); 1051 linux_osd_jail_register(); 1052 stclohz = (stathz ? stathz : hz); 1053 if (bootverbose) 1054 printf("Linux ELF exec handler installed\n"); 1055 } else 1056 printf("cannot insert Linux ELF brand handler\n"); 1057 break; 1058 case MOD_UNLOAD: 1059 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 1060 ++brandinfo) 1061 if (elf32_brand_inuse(*brandinfo)) 1062 error = EBUSY; 1063 if (error == 0) { 1064 for (brandinfo = &linux_brandlist[0]; 1065 *brandinfo != NULL; ++brandinfo) 1066 if (elf32_remove_brand_entry(*brandinfo) < 0) 1067 error = EINVAL; 1068 } 1069 if (error == 0) { 1070 SET_FOREACH(lihp, linux_ioctl_handler_set) 1071 linux_ioctl_unregister_handler(*lihp); 1072 mtx_destroy(&futex_mtx); 1073 linux_dev_shm_destroy(); 1074 linux_osd_jail_deregister(); 1075 if (bootverbose) 1076 printf("Linux ELF exec handler removed\n"); 1077 } else 1078 printf("Could not deinstall ELF interpreter entry\n"); 1079 break; 1080 default: 1081 return (EOPNOTSUPP); 1082 } 1083 return (error); 1084 } 1085 1086 static moduledata_t linux_elf_mod = { 1087 "linuxelf", 1088 linux_elf_modevent, 1089 0 1090 }; 1091 1092 DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY); 1093 FEATURE(linux, "Linux 32bit support"); 1094