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 /* If we have a valid auxargs ptr, prepare some room on the stack. */ 313 if (imgp->auxargs) { 314 /* 315 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 316 * lower compatibility. 317 */ 318 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size : 319 (LINUX_AT_COUNT * 2); 320 /* 321 * The '+ 2' is for the null pointers at the end of each of 322 * the arg and env vector sets,and imgp->auxarg_size is room 323 * for argument of Runtime loader. 324 */ 325 vectp = (char **)(destp - (imgp->args->argc + 326 imgp->args->envc + 2 + imgp->auxarg_size) * sizeof(char *)); 327 } else { 328 /* 329 * The '+ 2' is for the null pointers at the end of each of 330 * the arg and env vector sets 331 */ 332 vectp = (char **)(destp - (imgp->args->argc + imgp->args->envc + 2) * 333 sizeof(char *)); 334 } 335 336 /* vectp also becomes our initial stack base. */ 337 stack_base = (register_t *)vectp; 338 339 stringp = imgp->args->begin_argv; 340 argc = imgp->args->argc; 341 envc = imgp->args->envc; 342 343 /* Copy out strings - arguments and environment. */ 344 copyout(stringp, destp, ARG_MAX - imgp->args->stringspace); 345 346 /* Fill in "ps_strings" struct for ps, w, etc. */ 347 suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 348 suword(&arginfo->ps_nargvstr, argc); 349 350 /* Fill in argument portion of vector table. */ 351 for (; argc > 0; --argc) { 352 suword(vectp++, (long)(intptr_t)destp); 353 while (*stringp++ != 0) 354 destp++; 355 destp++; 356 } 357 358 /* A null vector table pointer separates the argp's from the envp's. */ 359 suword(vectp++, 0); 360 361 suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 362 suword(&arginfo->ps_nenvstr, envc); 363 364 /* Fill in environment portion of vector table. */ 365 for (; envc > 0; --envc) { 366 suword(vectp++, (long)(intptr_t)destp); 367 while (*stringp++ != 0) 368 destp++; 369 destp++; 370 } 371 372 /* The end of the vector table is a null pointer. */ 373 suword(vectp, 0); 374 375 return (stack_base); 376 } 377 378 static void 379 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 380 { 381 struct thread *td = curthread; 382 struct proc *p = td->td_proc; 383 struct sigacts *psp; 384 struct trapframe *regs; 385 struct l_rt_sigframe *fp, frame; 386 int sig, code; 387 int oonstack; 388 389 sig = ksi->ksi_signo; 390 code = ksi->ksi_code; 391 PROC_LOCK_ASSERT(p, MA_OWNED); 392 psp = p->p_sigacts; 393 mtx_assert(&psp->ps_mtx, MA_OWNED); 394 regs = td->td_frame; 395 oonstack = sigonstack(regs->tf_esp); 396 397 #ifdef DEBUG 398 if (ldebug(rt_sendsig)) 399 printf(ARGS(rt_sendsig, "%p, %d, %p, %u"), 400 catcher, sig, (void*)mask, code); 401 #endif 402 /* Allocate space for the signal handler context. */ 403 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack && 404 SIGISMEMBER(psp->ps_sigonstack, sig)) { 405 fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp + 406 td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe)); 407 } else 408 fp = (struct l_rt_sigframe *)regs->tf_esp - 1; 409 mtx_unlock(&psp->ps_mtx); 410 411 /* Build the argument list for the signal handler. */ 412 sig = bsd_to_linux_signal(sig); 413 414 bzero(&frame, sizeof(frame)); 415 416 frame.sf_handler = catcher; 417 frame.sf_sig = sig; 418 frame.sf_siginfo = &fp->sf_si; 419 frame.sf_ucontext = &fp->sf_sc; 420 421 /* Fill in POSIX parts. */ 422 ksiginfo_to_lsiginfo(ksi, &frame.sf_si, sig); 423 424 /* Build the signal context to be used by sigreturn. */ 425 frame.sf_sc.uc_flags = 0; /* XXX ??? */ 426 frame.sf_sc.uc_link = NULL; /* XXX ??? */ 427 428 frame.sf_sc.uc_stack.ss_sp = td->td_sigstk.ss_sp; 429 frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size; 430 frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) 431 ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE; 432 PROC_UNLOCK(p); 433 434 bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask); 435 436 frame.sf_sc.uc_mcontext.sc_mask = frame.sf_sc.uc_sigmask.__mask; 437 frame.sf_sc.uc_mcontext.sc_gs = rgs(); 438 frame.sf_sc.uc_mcontext.sc_fs = regs->tf_fs; 439 frame.sf_sc.uc_mcontext.sc_es = regs->tf_es; 440 frame.sf_sc.uc_mcontext.sc_ds = regs->tf_ds; 441 frame.sf_sc.uc_mcontext.sc_edi = regs->tf_edi; 442 frame.sf_sc.uc_mcontext.sc_esi = regs->tf_esi; 443 frame.sf_sc.uc_mcontext.sc_ebp = regs->tf_ebp; 444 frame.sf_sc.uc_mcontext.sc_ebx = regs->tf_ebx; 445 frame.sf_sc.uc_mcontext.sc_esp = regs->tf_esp; 446 frame.sf_sc.uc_mcontext.sc_edx = regs->tf_edx; 447 frame.sf_sc.uc_mcontext.sc_ecx = regs->tf_ecx; 448 frame.sf_sc.uc_mcontext.sc_eax = regs->tf_eax; 449 frame.sf_sc.uc_mcontext.sc_eip = regs->tf_eip; 450 frame.sf_sc.uc_mcontext.sc_cs = regs->tf_cs; 451 frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_eflags; 452 frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_esp; 453 frame.sf_sc.uc_mcontext.sc_ss = regs->tf_ss; 454 frame.sf_sc.uc_mcontext.sc_err = regs->tf_err; 455 frame.sf_sc.uc_mcontext.sc_cr2 = (register_t)ksi->ksi_addr; 456 frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code); 457 458 #ifdef DEBUG 459 if (ldebug(rt_sendsig)) 460 printf(LMSG("rt_sendsig flags: 0x%x, sp: %p, ss: 0x%x, mask: 0x%x"), 461 frame.sf_sc.uc_stack.ss_flags, td->td_sigstk.ss_sp, 462 td->td_sigstk.ss_size, frame.sf_sc.uc_mcontext.sc_mask); 463 #endif 464 465 if (copyout(&frame, fp, sizeof(frame)) != 0) { 466 /* 467 * Process has trashed its stack; give it an illegal 468 * instruction to halt it in its tracks. 469 */ 470 #ifdef DEBUG 471 if (ldebug(rt_sendsig)) 472 printf(LMSG("rt_sendsig: bad stack %p, oonstack=%x"), 473 fp, oonstack); 474 #endif 475 PROC_LOCK(p); 476 sigexit(td, SIGILL); 477 } 478 479 /* Build context to run handler in. */ 480 regs->tf_esp = (int)fp; 481 regs->tf_eip = linux_rt_sigcode; 482 regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D); 483 regs->tf_cs = _ucodesel; 484 regs->tf_ds = _udatasel; 485 regs->tf_es = _udatasel; 486 regs->tf_fs = _udatasel; 487 regs->tf_ss = _udatasel; 488 PROC_LOCK(p); 489 mtx_lock(&psp->ps_mtx); 490 } 491 492 493 /* 494 * Send an interrupt to process. 495 * 496 * Stack is set up to allow sigcode stored 497 * in u. to call routine, followed by kcall 498 * to sigreturn routine below. After sigreturn 499 * resets the signal mask, the stack, and the 500 * frame pointer, it returns to the user 501 * specified pc, psl. 502 */ 503 static void 504 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 505 { 506 struct thread *td = curthread; 507 struct proc *p = td->td_proc; 508 struct sigacts *psp; 509 struct trapframe *regs; 510 struct l_sigframe *fp, frame; 511 l_sigset_t lmask; 512 int sig, code; 513 int oonstack; 514 515 PROC_LOCK_ASSERT(p, MA_OWNED); 516 psp = p->p_sigacts; 517 sig = ksi->ksi_signo; 518 code = ksi->ksi_code; 519 mtx_assert(&psp->ps_mtx, MA_OWNED); 520 if (SIGISMEMBER(psp->ps_siginfo, sig)) { 521 /* Signal handler installed with SA_SIGINFO. */ 522 linux_rt_sendsig(catcher, ksi, mask); 523 return; 524 } 525 regs = td->td_frame; 526 oonstack = sigonstack(regs->tf_esp); 527 528 #ifdef DEBUG 529 if (ldebug(sendsig)) 530 printf(ARGS(sendsig, "%p, %d, %p, %u"), 531 catcher, sig, (void*)mask, code); 532 #endif 533 534 /* Allocate space for the signal handler context. */ 535 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack && 536 SIGISMEMBER(psp->ps_sigonstack, sig)) { 537 fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp + 538 td->td_sigstk.ss_size - sizeof(struct l_sigframe)); 539 } else 540 fp = (struct l_sigframe *)regs->tf_esp - 1; 541 mtx_unlock(&psp->ps_mtx); 542 PROC_UNLOCK(p); 543 544 /* Build the argument list for the signal handler. */ 545 sig = bsd_to_linux_signal(sig); 546 547 bzero(&frame, sizeof(frame)); 548 549 frame.sf_handler = catcher; 550 frame.sf_sig = sig; 551 552 bsd_to_linux_sigset(mask, &lmask); 553 554 /* Build the signal context to be used by sigreturn. */ 555 frame.sf_sc.sc_mask = lmask.__mask; 556 frame.sf_sc.sc_gs = rgs(); 557 frame.sf_sc.sc_fs = regs->tf_fs; 558 frame.sf_sc.sc_es = regs->tf_es; 559 frame.sf_sc.sc_ds = regs->tf_ds; 560 frame.sf_sc.sc_edi = regs->tf_edi; 561 frame.sf_sc.sc_esi = regs->tf_esi; 562 frame.sf_sc.sc_ebp = regs->tf_ebp; 563 frame.sf_sc.sc_ebx = regs->tf_ebx; 564 frame.sf_sc.sc_esp = regs->tf_esp; 565 frame.sf_sc.sc_edx = regs->tf_edx; 566 frame.sf_sc.sc_ecx = regs->tf_ecx; 567 frame.sf_sc.sc_eax = regs->tf_eax; 568 frame.sf_sc.sc_eip = regs->tf_eip; 569 frame.sf_sc.sc_cs = regs->tf_cs; 570 frame.sf_sc.sc_eflags = regs->tf_eflags; 571 frame.sf_sc.sc_esp_at_signal = regs->tf_esp; 572 frame.sf_sc.sc_ss = regs->tf_ss; 573 frame.sf_sc.sc_err = regs->tf_err; 574 frame.sf_sc.sc_cr2 = (register_t)ksi->ksi_addr; 575 frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(ksi->ksi_trapno); 576 577 frame.sf_extramask[0] = lmask.__mask; 578 579 if (copyout(&frame, fp, sizeof(frame)) != 0) { 580 /* 581 * Process has trashed its stack; give it an illegal 582 * instruction to halt it in its tracks. 583 */ 584 PROC_LOCK(p); 585 sigexit(td, SIGILL); 586 } 587 588 /* Build context to run handler in. */ 589 regs->tf_esp = (int)fp; 590 regs->tf_eip = linux_sigcode; 591 regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D); 592 regs->tf_cs = _ucodesel; 593 regs->tf_ds = _udatasel; 594 regs->tf_es = _udatasel; 595 regs->tf_fs = _udatasel; 596 regs->tf_ss = _udatasel; 597 PROC_LOCK(p); 598 mtx_lock(&psp->ps_mtx); 599 } 600 601 /* 602 * System call to cleanup state after a signal 603 * has been taken. Reset signal mask and 604 * stack state from context left by sendsig (above). 605 * Return to previous pc and psl as specified by 606 * context left by sendsig. Check carefully to 607 * make sure that the user has not modified the 608 * psl to gain improper privileges or to cause 609 * a machine fault. 610 */ 611 int 612 linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args) 613 { 614 struct l_sigframe frame; 615 struct trapframe *regs; 616 l_sigset_t lmask; 617 sigset_t bmask; 618 int eflags; 619 ksiginfo_t ksi; 620 621 regs = td->td_frame; 622 623 #ifdef DEBUG 624 if (ldebug(sigreturn)) 625 printf(ARGS(sigreturn, "%p"), (void *)args->sfp); 626 #endif 627 /* 628 * The trampoline code hands us the sigframe. 629 * It is unsafe to keep track of it ourselves, in the event that a 630 * program jumps out of a signal handler. 631 */ 632 if (copyin(args->sfp, &frame, sizeof(frame)) != 0) 633 return (EFAULT); 634 635 /* Check for security violations. */ 636 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 637 eflags = frame.sf_sc.sc_eflags; 638 if (!EFLAGS_SECURE(eflags, regs->tf_eflags)) 639 return (EINVAL); 640 641 /* 642 * Don't allow users to load a valid privileged %cs. Let the 643 * hardware check for invalid selectors, excess privilege in 644 * other selectors, invalid %eip's and invalid %esp's. 645 */ 646 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 647 if (!CS_SECURE(frame.sf_sc.sc_cs)) { 648 ksiginfo_init_trap(&ksi); 649 ksi.ksi_signo = SIGBUS; 650 ksi.ksi_code = BUS_OBJERR; 651 ksi.ksi_trapno = T_PROTFLT; 652 ksi.ksi_addr = (void *)regs->tf_eip; 653 trapsignal(td, &ksi); 654 return (EINVAL); 655 } 656 657 lmask.__mask = frame.sf_sc.sc_mask; 658 linux_to_bsd_sigset(&lmask, &bmask); 659 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0); 660 661 /* Restore signal context. */ 662 /* %gs was restored by the trampoline. */ 663 regs->tf_fs = frame.sf_sc.sc_fs; 664 regs->tf_es = frame.sf_sc.sc_es; 665 regs->tf_ds = frame.sf_sc.sc_ds; 666 regs->tf_edi = frame.sf_sc.sc_edi; 667 regs->tf_esi = frame.sf_sc.sc_esi; 668 regs->tf_ebp = frame.sf_sc.sc_ebp; 669 regs->tf_ebx = frame.sf_sc.sc_ebx; 670 regs->tf_edx = frame.sf_sc.sc_edx; 671 regs->tf_ecx = frame.sf_sc.sc_ecx; 672 regs->tf_eax = frame.sf_sc.sc_eax; 673 regs->tf_eip = frame.sf_sc.sc_eip; 674 regs->tf_cs = frame.sf_sc.sc_cs; 675 regs->tf_eflags = eflags; 676 regs->tf_esp = frame.sf_sc.sc_esp_at_signal; 677 regs->tf_ss = frame.sf_sc.sc_ss; 678 679 return (EJUSTRETURN); 680 } 681 682 /* 683 * System call to cleanup state after a signal 684 * has been taken. Reset signal mask and 685 * stack state from context left by rt_sendsig (above). 686 * Return to previous pc and psl as specified by 687 * context left by sendsig. Check carefully to 688 * make sure that the user has not modified the 689 * psl to gain improper privileges or to cause 690 * a machine fault. 691 */ 692 int 693 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) 694 { 695 struct l_ucontext uc; 696 struct l_sigcontext *context; 697 sigset_t bmask; 698 l_stack_t *lss; 699 stack_t ss; 700 struct trapframe *regs; 701 int eflags; 702 ksiginfo_t ksi; 703 704 regs = td->td_frame; 705 706 #ifdef DEBUG 707 if (ldebug(rt_sigreturn)) 708 printf(ARGS(rt_sigreturn, "%p"), (void *)args->ucp); 709 #endif 710 /* 711 * The trampoline code hands us the ucontext. 712 * It is unsafe to keep track of it ourselves, in the event that a 713 * program jumps out of a signal handler. 714 */ 715 if (copyin(args->ucp, &uc, sizeof(uc)) != 0) 716 return (EFAULT); 717 718 context = &uc.uc_mcontext; 719 720 /* Check for security violations. */ 721 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 722 eflags = context->sc_eflags; 723 if (!EFLAGS_SECURE(eflags, regs->tf_eflags)) 724 return (EINVAL); 725 726 /* 727 * Don't allow users to load a valid privileged %cs. Let the 728 * hardware check for invalid selectors, excess privilege in 729 * other selectors, invalid %eip's and invalid %esp's. 730 */ 731 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 732 if (!CS_SECURE(context->sc_cs)) { 733 ksiginfo_init_trap(&ksi); 734 ksi.ksi_signo = SIGBUS; 735 ksi.ksi_code = BUS_OBJERR; 736 ksi.ksi_trapno = T_PROTFLT; 737 ksi.ksi_addr = (void *)regs->tf_eip; 738 trapsignal(td, &ksi); 739 return (EINVAL); 740 } 741 742 linux_to_bsd_sigset(&uc.uc_sigmask, &bmask); 743 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0); 744 745 /* Restore signal context. */ 746 /* %gs was restored by the trampoline. */ 747 regs->tf_fs = context->sc_fs; 748 regs->tf_es = context->sc_es; 749 regs->tf_ds = context->sc_ds; 750 regs->tf_edi = context->sc_edi; 751 regs->tf_esi = context->sc_esi; 752 regs->tf_ebp = context->sc_ebp; 753 regs->tf_ebx = context->sc_ebx; 754 regs->tf_edx = context->sc_edx; 755 regs->tf_ecx = context->sc_ecx; 756 regs->tf_eax = context->sc_eax; 757 regs->tf_eip = context->sc_eip; 758 regs->tf_cs = context->sc_cs; 759 regs->tf_eflags = eflags; 760 regs->tf_esp = context->sc_esp_at_signal; 761 regs->tf_ss = context->sc_ss; 762 763 /* Call sigaltstack & ignore results. */ 764 lss = &uc.uc_stack; 765 ss.ss_sp = lss->ss_sp; 766 ss.ss_size = lss->ss_size; 767 ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags); 768 769 #ifdef DEBUG 770 if (ldebug(rt_sigreturn)) 771 printf(LMSG("rt_sigret flags: 0x%x, sp: %p, ss: 0x%x, mask: 0x%x"), 772 ss.ss_flags, ss.ss_sp, ss.ss_size, context->sc_mask); 773 #endif 774 (void)kern_sigaltstack(td, &ss, NULL); 775 776 return (EJUSTRETURN); 777 } 778 779 static int 780 linux_fetch_syscall_args(struct thread *td) 781 { 782 struct proc *p; 783 struct trapframe *frame; 784 struct syscall_args *sa; 785 786 p = td->td_proc; 787 frame = td->td_frame; 788 sa = &td->td_sa; 789 790 sa->code = frame->tf_eax; 791 sa->args[0] = frame->tf_ebx; 792 sa->args[1] = frame->tf_ecx; 793 sa->args[2] = frame->tf_edx; 794 sa->args[3] = frame->tf_esi; 795 sa->args[4] = frame->tf_edi; 796 sa->args[5] = frame->tf_ebp; /* Unconfirmed */ 797 798 if (sa->code >= p->p_sysent->sv_size) 799 /* nosys */ 800 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1]; 801 else 802 sa->callp = &p->p_sysent->sv_table[sa->code]; 803 sa->narg = sa->callp->sy_narg; 804 805 td->td_retval[0] = 0; 806 td->td_retval[1] = frame->tf_edx; 807 808 return (0); 809 } 810 811 /* 812 * exec_setregs may initialize some registers differently than Linux 813 * does, thus potentially confusing Linux binaries. If necessary, we 814 * override the exec_setregs default(s) here. 815 */ 816 static void 817 linux_exec_setregs(struct thread *td, struct image_params *imgp, u_long stack) 818 { 819 struct pcb *pcb = td->td_pcb; 820 821 exec_setregs(td, imgp, stack); 822 823 /* Linux sets %gs to 0, we default to _udatasel. */ 824 pcb->pcb_gs = 0; 825 load_gs(0); 826 827 pcb->pcb_initial_npxcw = __LINUX_NPXCW__; 828 } 829 830 static void 831 linux_get_machine(const char **dst) 832 { 833 834 switch (cpu_class) { 835 case CPUCLASS_686: 836 *dst = "i686"; 837 break; 838 case CPUCLASS_586: 839 *dst = "i586"; 840 break; 841 case CPUCLASS_486: 842 *dst = "i486"; 843 break; 844 default: 845 *dst = "i386"; 846 } 847 } 848 849 struct sysentvec linux_sysvec = { 850 .sv_size = LINUX_SYS_MAXSYSCALL, 851 .sv_table = linux_sysent, 852 .sv_mask = 0, 853 .sv_errsize = ELAST + 1, 854 .sv_errtbl = linux_errtbl, 855 .sv_transtrap = linux_translate_traps, 856 .sv_fixup = linux_fixup, 857 .sv_sendsig = linux_sendsig, 858 .sv_sigcode = &_binary_linux_locore_o_start, 859 .sv_szsigcode = &linux_szsigcode, 860 .sv_name = "Linux a.out", 861 .sv_coredump = NULL, 862 .sv_imgact_try = linux_exec_imgact_try, 863 .sv_minsigstksz = LINUX_MINSIGSTKSZ, 864 .sv_pagesize = PAGE_SIZE, 865 .sv_minuser = VM_MIN_ADDRESS, 866 .sv_maxuser = VM_MAXUSER_ADDRESS, 867 .sv_usrstack = LINUX_USRSTACK, 868 .sv_psstrings = PS_STRINGS, 869 .sv_stackprot = VM_PROT_ALL, 870 .sv_copyout_strings = exec_copyout_strings, 871 .sv_setregs = linux_exec_setregs, 872 .sv_fixlimit = NULL, 873 .sv_maxssiz = NULL, 874 .sv_flags = SV_ABI_LINUX | SV_AOUT | SV_IA32 | SV_ILP32, 875 .sv_set_syscall_retval = cpu_set_syscall_retval, 876 .sv_fetch_syscall_args = linux_fetch_syscall_args, 877 .sv_syscallnames = NULL, 878 .sv_shared_page_base = LINUX_SHAREDPAGE, 879 .sv_shared_page_len = PAGE_SIZE, 880 .sv_schedtail = linux_schedtail, 881 .sv_thread_detach = linux_thread_detach, 882 .sv_trap = NULL, 883 }; 884 INIT_SYSENTVEC(aout_sysvec, &linux_sysvec); 885 886 struct sysentvec elf_linux_sysvec = { 887 .sv_size = LINUX_SYS_MAXSYSCALL, 888 .sv_table = linux_sysent, 889 .sv_mask = 0, 890 .sv_errsize = ELAST + 1, 891 .sv_errtbl = linux_errtbl, 892 .sv_transtrap = linux_translate_traps, 893 .sv_fixup = linux_fixup_elf, 894 .sv_sendsig = linux_sendsig, 895 .sv_sigcode = &_binary_linux_locore_o_start, 896 .sv_szsigcode = &linux_szsigcode, 897 .sv_name = "Linux ELF", 898 .sv_coredump = elf32_coredump, 899 .sv_imgact_try = linux_exec_imgact_try, 900 .sv_minsigstksz = LINUX_MINSIGSTKSZ, 901 .sv_pagesize = PAGE_SIZE, 902 .sv_minuser = VM_MIN_ADDRESS, 903 .sv_maxuser = VM_MAXUSER_ADDRESS, 904 .sv_usrstack = LINUX_USRSTACK, 905 .sv_psstrings = LINUX_PS_STRINGS, 906 .sv_stackprot = VM_PROT_ALL, 907 .sv_copyout_strings = linux_copyout_strings, 908 .sv_setregs = linux_exec_setregs, 909 .sv_fixlimit = NULL, 910 .sv_maxssiz = NULL, 911 .sv_flags = SV_ABI_LINUX | SV_IA32 | SV_ILP32 | SV_SHP, 912 .sv_set_syscall_retval = cpu_set_syscall_retval, 913 .sv_fetch_syscall_args = linux_fetch_syscall_args, 914 .sv_syscallnames = NULL, 915 .sv_shared_page_base = LINUX_SHAREDPAGE, 916 .sv_shared_page_len = PAGE_SIZE, 917 .sv_schedtail = linux_schedtail, 918 .sv_thread_detach = linux_thread_detach, 919 .sv_trap = NULL, 920 }; 921 922 static void 923 linux_vdso_install(void *param) 924 { 925 926 linux_szsigcode = (&_binary_linux_locore_o_end - 927 &_binary_linux_locore_o_start); 928 929 if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len) 930 panic("Linux invalid vdso size\n"); 931 932 __elfN(linux_vdso_fixup)(&elf_linux_sysvec); 933 934 linux_shared_page_obj = __elfN(linux_shared_page_init) 935 (&linux_shared_page_mapping); 936 937 __elfN(linux_vdso_reloc)(&elf_linux_sysvec); 938 939 bcopy(elf_linux_sysvec.sv_sigcode, linux_shared_page_mapping, 940 linux_szsigcode); 941 elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj; 942 } 943 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY, 944 linux_vdso_install, NULL); 945 946 static void 947 linux_vdso_deinstall(void *param) 948 { 949 950 __elfN(linux_shared_page_fini)(linux_shared_page_obj); 951 } 952 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST, 953 linux_vdso_deinstall, NULL); 954 955 static char GNU_ABI_VENDOR[] = "GNU"; 956 static int GNULINUX_ABI_DESC = 0; 957 958 static bool 959 linux_trans_osrel(const Elf_Note *note, int32_t *osrel) 960 { 961 const Elf32_Word *desc; 962 uintptr_t p; 963 964 p = (uintptr_t)(note + 1); 965 p += roundup2(note->n_namesz, sizeof(Elf32_Addr)); 966 967 desc = (const Elf32_Word *)p; 968 if (desc[0] != GNULINUX_ABI_DESC) 969 return (false); 970 971 /* 972 * For Linux we encode osrel as follows (see linux_mib.c): 973 * VVVMMMIII (version, major, minor), see linux_mib.c. 974 */ 975 *osrel = desc[1] * 1000000 + desc[2] * 1000 + desc[3]; 976 977 return (true); 978 } 979 980 static Elf_Brandnote linux_brandnote = { 981 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), 982 .hdr.n_descsz = 16, /* XXX at least 16 */ 983 .hdr.n_type = 1, 984 .vendor = GNU_ABI_VENDOR, 985 .flags = BN_TRANSLATE_OSREL, 986 .trans_osrel = linux_trans_osrel 987 }; 988 989 static Elf32_Brandinfo linux_brand = { 990 .brand = ELFOSABI_LINUX, 991 .machine = EM_386, 992 .compat_3_brand = "Linux", 993 .emul_path = "/compat/linux", 994 .interp_path = "/lib/ld-linux.so.1", 995 .sysvec = &elf_linux_sysvec, 996 .interp_newpath = NULL, 997 .brand_note = &linux_brandnote, 998 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 999 }; 1000 1001 static Elf32_Brandinfo linux_glibc2brand = { 1002 .brand = ELFOSABI_LINUX, 1003 .machine = EM_386, 1004 .compat_3_brand = "Linux", 1005 .emul_path = "/compat/linux", 1006 .interp_path = "/lib/ld-linux.so.2", 1007 .sysvec = &elf_linux_sysvec, 1008 .interp_newpath = NULL, 1009 .brand_note = &linux_brandnote, 1010 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 1011 }; 1012 1013 static Elf32_Brandinfo linux_muslbrand = { 1014 .brand = ELFOSABI_LINUX, 1015 .machine = EM_386, 1016 .compat_3_brand = "Linux", 1017 .emul_path = "/compat/linux", 1018 .interp_path = "/lib/ld-musl-i386.so.1", 1019 .sysvec = &elf_linux_sysvec, 1020 .interp_newpath = NULL, 1021 .brand_note = &linux_brandnote, 1022 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 1023 }; 1024 1025 Elf32_Brandinfo *linux_brandlist[] = { 1026 &linux_brand, 1027 &linux_glibc2brand, 1028 &linux_muslbrand, 1029 NULL 1030 }; 1031 1032 static int 1033 linux_elf_modevent(module_t mod, int type, void *data) 1034 { 1035 Elf32_Brandinfo **brandinfo; 1036 int error; 1037 struct linux_ioctl_handler **lihp; 1038 1039 error = 0; 1040 1041 switch(type) { 1042 case MOD_LOAD: 1043 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 1044 ++brandinfo) 1045 if (elf32_insert_brand_entry(*brandinfo) < 0) 1046 error = EINVAL; 1047 if (error == 0) { 1048 SET_FOREACH(lihp, linux_ioctl_handler_set) 1049 linux_ioctl_register_handler(*lihp); 1050 LIST_INIT(&futex_list); 1051 mtx_init(&futex_mtx, "ftllk", NULL, MTX_DEF); 1052 linux_exit_tag = EVENTHANDLER_REGISTER(process_exit, linux_proc_exit, 1053 NULL, 1000); 1054 linux_exec_tag = EVENTHANDLER_REGISTER(process_exec, linux_proc_exec, 1055 NULL, 1000); 1056 linux_thread_dtor_tag = EVENTHANDLER_REGISTER(thread_dtor, 1057 linux_thread_dtor, NULL, EVENTHANDLER_PRI_ANY); 1058 linux_get_machine(&linux_kplatform); 1059 linux_szplatform = roundup(strlen(linux_kplatform) + 1, 1060 sizeof(char *)); 1061 linux_osd_jail_register(); 1062 stclohz = (stathz ? stathz : hz); 1063 if (bootverbose) 1064 printf("Linux ELF exec handler installed\n"); 1065 } else 1066 printf("cannot insert Linux ELF brand handler\n"); 1067 break; 1068 case MOD_UNLOAD: 1069 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; 1070 ++brandinfo) 1071 if (elf32_brand_inuse(*brandinfo)) 1072 error = EBUSY; 1073 if (error == 0) { 1074 for (brandinfo = &linux_brandlist[0]; 1075 *brandinfo != NULL; ++brandinfo) 1076 if (elf32_remove_brand_entry(*brandinfo) < 0) 1077 error = EINVAL; 1078 } 1079 if (error == 0) { 1080 SET_FOREACH(lihp, linux_ioctl_handler_set) 1081 linux_ioctl_unregister_handler(*lihp); 1082 mtx_destroy(&futex_mtx); 1083 EVENTHANDLER_DEREGISTER(process_exit, linux_exit_tag); 1084 EVENTHANDLER_DEREGISTER(process_exec, linux_exec_tag); 1085 EVENTHANDLER_DEREGISTER(thread_dtor, linux_thread_dtor_tag); 1086 linux_osd_jail_deregister(); 1087 if (bootverbose) 1088 printf("Linux ELF exec handler removed\n"); 1089 } else 1090 printf("Could not deinstall ELF interpreter entry\n"); 1091 break; 1092 default: 1093 return (EOPNOTSUPP); 1094 } 1095 return (error); 1096 } 1097 1098 static moduledata_t linux_elf_mod = { 1099 "linuxelf", 1100 linux_elf_modevent, 1101 0 1102 }; 1103 1104 DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY); 1105 FEATURE(linux, "Linux 32bit support"); 1106