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