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