The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/compat/linux/linux_emul.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 1994-1996 Søren Schmidt
    5  * Copyright (c) 2006 Roman Divacky
    6  * Copyright (c) 2013 Dmitry Chagin
    7  * All rights reserved.
    8  *
    9  * Redistribution and use in source and binary forms, with or without
   10  * modification, are permitted provided that the following conditions
   11  * are met:
   12  * 1. Redistributions of source code must retain the above copyright
   13  *    notice, this list of conditions and the following disclaimer.
   14  * 2. Redistributions in binary form must reproduce the above copyright
   15  *    notice, this list of conditions and the following disclaimer in the
   16  *    documentation and/or other materials provided with the distribution.
   17  *
   18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   28  * SUCH DAMAGE.
   29  */
   30 
   31 #include <sys/cdefs.h>
   32 __FBSDID("$FreeBSD$");
   33 
   34 #include <sys/param.h>
   35 #include <sys/systm.h>
   36 #include <sys/fcntl.h>
   37 #include <sys/imgact.h>
   38 #include <sys/kernel.h>
   39 #include <sys/ktr.h>
   40 #include <sys/lock.h>
   41 #include <sys/malloc.h>
   42 #include <sys/mutex.h>
   43 #include <sys/sx.h>
   44 #include <sys/proc.h>
   45 #include <sys/resourcevar.h>
   46 #include <sys/syscallsubr.h>
   47 #include <sys/sysent.h>
   48 
   49 #include <compat/linux/linux_emul.h>
   50 #include <compat/linux/linux_mib.h>
   51 #include <compat/linux/linux_misc.h>
   52 #include <compat/linux/linux_persona.h>
   53 #include <compat/linux/linux_util.h>
   54 
   55 #if BYTE_ORDER == LITTLE_ENDIAN
   56 #define SHELLMAGIC      0x2123 /* #! */
   57 #else
   58 #define SHELLMAGIC      0x2321
   59 #endif
   60 
   61 /*
   62  * This returns reference to the thread emuldata entry (if found)
   63  *
   64  * Hold PROC_LOCK when referencing emuldata from other threads.
   65  */
   66 struct linux_emuldata *
   67 em_find(struct thread *td)
   68 {
   69         struct linux_emuldata *em;
   70 
   71         em = td->td_emuldata;
   72 
   73         return (em);
   74 }
   75 
   76 /*
   77  * This returns reference to the proc pemuldata entry (if found)
   78  *
   79  * Hold PROC_LOCK when referencing proc pemuldata from other threads.
   80  * Hold LINUX_PEM_LOCK wher referencing pemuldata members.
   81  */
   82 struct linux_pemuldata *
   83 pem_find(struct proc *p)
   84 {
   85         struct linux_pemuldata *pem;
   86 
   87         pem = p->p_emuldata;
   88 
   89         return (pem);
   90 }
   91 
   92 /*
   93  * Linux apps generally expect the soft open file limit to be set
   94  * to 1024, often iterating over all the file descriptors up to that
   95  * limit instead of using closefrom(2).  Give them what they want,
   96  * unless there already is a resource limit in place.
   97  */
   98 static void
   99 linux_set_default_openfiles(struct thread *td, struct proc *p)
  100 {
  101         struct rlimit rlim;
  102         int error;
  103 
  104         if (linux_default_openfiles < 0)
  105                 return;
  106 
  107         PROC_LOCK(p);
  108         lim_rlimit_proc(p, RLIMIT_NOFILE, &rlim);
  109         PROC_UNLOCK(p);
  110         if (rlim.rlim_cur != rlim.rlim_max ||
  111             rlim.rlim_cur <= linux_default_openfiles)
  112                 return;
  113         rlim.rlim_cur = linux_default_openfiles;
  114         error = kern_proc_setrlimit(td, p, RLIMIT_NOFILE, &rlim);
  115         KASSERT(error == 0, ("kern_proc_setrlimit failed"));
  116 }
  117 
  118 void
  119 linux_proc_init(struct thread *td, struct thread *newtd, int flags)
  120 {
  121         struct linux_emuldata *em;
  122         struct linux_pemuldata *pem;
  123         struct epoll_emuldata *emd;
  124         struct proc *p;
  125 
  126         if (newtd != NULL) {
  127                 p = newtd->td_proc;
  128 
  129                 /* non-exec call */
  130                 em = malloc(sizeof(*em), M_TEMP, M_WAITOK | M_ZERO);
  131                 if (flags & LINUX_CLONE_THREAD) {
  132                         LINUX_CTR1(proc_init, "thread newtd(%d)",
  133                             newtd->td_tid);
  134 
  135                         em->em_tid = newtd->td_tid;
  136                 } else {
  137                         LINUX_CTR1(proc_init, "fork newtd(%d)", p->p_pid);
  138 
  139                         em->em_tid = p->p_pid;
  140 
  141                         pem = malloc(sizeof(*pem), M_LINUX, M_WAITOK | M_ZERO);
  142                         sx_init(&pem->pem_sx, "lpemlk");
  143                         p->p_emuldata = pem;
  144                 }
  145                 newtd->td_emuldata = em;
  146 
  147                 linux_set_default_openfiles(td, p);
  148         } else {
  149                 p = td->td_proc;
  150 
  151                 /* exec */
  152                 LINUX_CTR1(proc_init, "exec newtd(%d)", p->p_pid);
  153 
  154                 /* lookup the old one */
  155                 em = em_find(td);
  156                 KASSERT(em != NULL, ("proc_init: emuldata not found in exec case.\n"));
  157 
  158                 em->em_tid = p->p_pid;
  159                 em->flags = 0;
  160                 em->robust_futexes = NULL;
  161                 em->child_clear_tid = NULL;
  162                 em->child_set_tid = NULL;
  163 
  164                  /* epoll should be destroyed in a case of exec. */
  165                 pem = pem_find(p);
  166                 KASSERT(pem != NULL, ("proc_exit: proc emuldata not found.\n"));
  167                 pem->persona = 0;
  168                 if (pem->epoll != NULL) {
  169                         emd = pem->epoll;
  170                         pem->epoll = NULL;
  171                         free(emd, M_EPOLL);
  172                 }
  173         }
  174 
  175 }
  176 
  177 void
  178 linux_proc_exit(void *arg __unused, struct proc *p)
  179 {
  180         struct linux_pemuldata *pem;
  181         struct epoll_emuldata *emd;
  182         struct thread *td = curthread;
  183 
  184         if (__predict_false(SV_CURPROC_ABI() != SV_ABI_LINUX))
  185                 return;
  186 
  187         LINUX_CTR3(proc_exit, "thread(%d) proc(%d) p %p",
  188             td->td_tid, p->p_pid, p);
  189 
  190         pem = pem_find(p);
  191         if (pem == NULL)
  192                 return;
  193         (p->p_sysent->sv_thread_detach)(td);
  194 
  195         p->p_emuldata = NULL;
  196 
  197         if (pem->epoll != NULL) {
  198                 emd = pem->epoll;
  199                 pem->epoll = NULL;
  200                 free(emd, M_EPOLL);
  201         }
  202 
  203         sx_destroy(&pem->pem_sx);
  204         free(pem, M_LINUX);
  205 }
  206 
  207 /*
  208  * If a Linux binary is exec'ing something, try this image activator
  209  * first.  We override standard shell script execution in order to
  210  * be able to modify the interpreter path.  We only do this if a Linux
  211  * binary is doing the exec, so we do not create an EXEC module for it.
  212  */
  213 int
  214 linux_exec_imgact_try(struct image_params *imgp)
  215 {
  216         const char *head = (const char *)imgp->image_header;
  217         char *rpath;
  218         int error = -1;
  219 
  220         /*
  221          * The interpreter for shell scripts run from a Linux binary needs
  222          * to be located in /compat/linux if possible in order to recursively
  223          * maintain Linux path emulation.
  224          */
  225         if (((const short *)head)[0] == SHELLMAGIC) {
  226                 /*
  227                  * Run our normal shell image activator.  If it succeeds attempt
  228                  * to use the alternate path for the interpreter.  If an
  229                  * alternate path is found, use our stringspace to store it.
  230                  */
  231                 if ((error = exec_shell_imgact(imgp)) == 0) {
  232                         linux_emul_convpath(FIRST_THREAD_IN_PROC(imgp->proc),
  233                             imgp->interpreter_name, UIO_SYSSPACE, &rpath, 0,
  234                             AT_FDCWD);
  235                         if (rpath != NULL)
  236                                 imgp->args->fname_buf =
  237                                     imgp->interpreter_name = rpath;
  238                 }
  239         }
  240         return (error);
  241 }
  242 
  243 int
  244 linux_common_execve(struct thread *td, struct image_args *eargs)
  245 {
  246         struct linux_pemuldata *pem;
  247         struct epoll_emuldata *emd;
  248         struct vmspace *oldvmspace;
  249         struct linux_emuldata *em;
  250         struct proc *p;
  251         int error;
  252 
  253         p = td->td_proc;
  254 
  255         error = pre_execve(td, &oldvmspace);
  256         if (error != 0)
  257                 return (error);
  258 
  259         error = kern_execve(td, eargs, NULL, oldvmspace);
  260         post_execve(td, error, oldvmspace);
  261         if (error != EJUSTRETURN)
  262                 return (error);
  263 
  264         /*
  265          * In a case of transition from Linux binary execing to
  266          * FreeBSD binary we destroy Linux emuldata thread & proc entries.
  267          */
  268         if (SV_CURPROC_ABI() != SV_ABI_LINUX) {
  269                 PROC_LOCK(p);
  270                 em = em_find(td);
  271                 KASSERT(em != NULL, ("proc_exec: thread emuldata not found.\n"));
  272                 td->td_emuldata = NULL;
  273 
  274                 pem = pem_find(p);
  275                 KASSERT(pem != NULL, ("proc_exec: proc pemuldata not found.\n"));
  276                 p->p_emuldata = NULL;
  277                 PROC_UNLOCK(p);
  278 
  279                 if (pem->epoll != NULL) {
  280                         emd = pem->epoll;
  281                         pem->epoll = NULL;
  282                         free(emd, M_EPOLL);
  283                 }
  284 
  285                 free(em, M_TEMP);
  286                 free(pem, M_LINUX);
  287         }
  288         return (EJUSTRETURN);
  289 }
  290 
  291 void
  292 linux_proc_exec(void *arg __unused, struct proc *p, struct image_params *imgp)
  293 {
  294         struct thread *td;
  295         struct thread *othertd;
  296 #if defined(__amd64__)
  297         struct linux_pemuldata *pem;
  298 #endif
  299 
  300         td = curthread;
  301 
  302         /*
  303          * In a case of execing to Linux binary we create Linux
  304          * emuldata thread entry.
  305          */
  306         if (__predict_false((imgp->sysent->sv_flags & SV_ABI_MASK) ==
  307             SV_ABI_LINUX)) {
  308                 if (SV_PROC_ABI(p) == SV_ABI_LINUX) {
  309                         /*
  310                          * Process already was under Linuxolator
  311                          * before exec.  Update emuldata to reflect
  312                          * single-threaded cleaned state after exec.
  313                          */
  314                         linux_proc_init(td, NULL, 0);
  315                 } else {
  316                         /*
  317                          * We are switching the process to Linux emulator.
  318                          */
  319                         linux_proc_init(td, td, 0);
  320 
  321                         /*
  322                          * Create a transient td_emuldata for all suspended
  323                          * threads, so that p->p_sysent->sv_thread_detach() ==
  324                          * linux_thread_detach() can find expected but unused
  325                          * emuldata.
  326                          */
  327                         FOREACH_THREAD_IN_PROC(td->td_proc, othertd) {
  328                                 if (othertd != td) {
  329                                         linux_proc_init(td, othertd,
  330                                             LINUX_CLONE_THREAD);
  331                                 }
  332                         }
  333                 }
  334 #if defined(__amd64__)
  335                 /*
  336                  * An IA32 executable which has executable stack will have the
  337                  * READ_IMPLIES_EXEC personality flag set automatically.
  338                  */
  339                 if (SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
  340                     imgp->stack_prot & VM_PROT_EXECUTE) {
  341                         pem = pem_find(p);
  342                         pem->persona |= LINUX_READ_IMPLIES_EXEC;
  343                 }
  344 #endif
  345         }
  346 }
  347 
  348 void
  349 linux_thread_dtor(void *arg __unused, struct thread *td)
  350 {
  351         struct linux_emuldata *em;
  352 
  353         em = em_find(td);
  354         if (em == NULL)
  355                 return;
  356         td->td_emuldata = NULL;
  357 
  358         LINUX_CTR1(thread_dtor, "thread(%d)", em->em_tid);
  359 
  360         free(em, M_TEMP);
  361 }
  362 
  363 void
  364 linux_schedtail(struct thread *td)
  365 {
  366         struct linux_emuldata *em;
  367         struct proc *p;
  368         int error = 0;
  369         int *child_set_tid;
  370 
  371         p = td->td_proc;
  372 
  373         em = em_find(td);
  374         KASSERT(em != NULL, ("linux_schedtail: thread emuldata not found.\n"));
  375         child_set_tid = em->child_set_tid;
  376 
  377         if (child_set_tid != NULL) {
  378                 error = copyout(&em->em_tid, child_set_tid,
  379                     sizeof(em->em_tid));
  380                 LINUX_CTR4(schedtail, "thread(%d) %p stored %d error %d",
  381                     td->td_tid, child_set_tid, em->em_tid, error);
  382         } else
  383                 LINUX_CTR1(schedtail, "thread(%d)", em->em_tid);
  384 }

Cache object: 2a7e77617a3061409e0791eb2f72115d


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.