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