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  * All rights reserved.
    7  * Copyright (c) 2013 Dmitry Chagin <dchagin@FreeBSD.org>
    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 __diagused;
  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 /*
  119  * The default stack size limit in Linux is 8MB.
  120  */
  121 static void
  122 linux_set_default_stacksize(struct thread *td, struct proc *p)
  123 {
  124         struct rlimit rlim;
  125         int error __diagused;
  126 
  127         if (linux_default_stacksize < 0)
  128                 return;
  129 
  130         PROC_LOCK(p);
  131         lim_rlimit_proc(p, RLIMIT_STACK, &rlim);
  132         PROC_UNLOCK(p);
  133         if (rlim.rlim_cur != rlim.rlim_max ||
  134             rlim.rlim_cur <= linux_default_stacksize)
  135                 return;
  136         rlim.rlim_cur = linux_default_stacksize;
  137         error = kern_proc_setrlimit(td, p, RLIMIT_STACK, &rlim);
  138         KASSERT(error == 0, ("kern_proc_setrlimit failed"));
  139 }
  140 
  141 void
  142 linux_proc_init(struct thread *td, struct thread *newtd, bool init_thread)
  143 {
  144         struct linux_emuldata *em;
  145         struct linux_pemuldata *pem;
  146         struct proc *p;
  147 
  148         if (newtd != NULL) {
  149                 p = newtd->td_proc;
  150 
  151                 /* non-exec call */
  152                 em = malloc(sizeof(*em), M_TEMP, M_WAITOK | M_ZERO);
  153                 if (init_thread) {
  154                         LINUX_CTR1(proc_init, "thread newtd(%d)",
  155                             newtd->td_tid);
  156 
  157                         em->em_tid = newtd->td_tid;
  158                 } else {
  159                         LINUX_CTR1(proc_init, "fork newtd(%d)", p->p_pid);
  160 
  161                         em->em_tid = p->p_pid;
  162 
  163                         pem = malloc(sizeof(*pem), M_LINUX, M_WAITOK | M_ZERO);
  164                         sx_init(&pem->pem_sx, "lpemlk");
  165                         p->p_emuldata = pem;
  166                 }
  167                 newtd->td_emuldata = em;
  168 
  169                 linux_set_default_openfiles(td, p);
  170                 linux_set_default_stacksize(td, p);
  171         } else {
  172                 p = td->td_proc;
  173 
  174                 /* exec */
  175                 LINUX_CTR1(proc_init, "exec newtd(%d)", p->p_pid);
  176 
  177                 /* lookup the old one */
  178                 em = em_find(td);
  179                 KASSERT(em != NULL, ("proc_init: thread emuldata not found.\n"));
  180 
  181                 em->em_tid = p->p_pid;
  182                 em->flags = 0;
  183                 em->robust_futexes = NULL;
  184                 em->child_clear_tid = NULL;
  185                 em->child_set_tid = NULL;
  186 
  187                 pem = pem_find(p);
  188                 KASSERT(pem != NULL, ("proc_init: proc emuldata not found.\n"));
  189                 pem->persona = 0;
  190                 pem->oom_score_adj = 0;
  191         }
  192 }
  193 
  194 void
  195 linux_on_exit(struct proc *p)
  196 {
  197         struct linux_pemuldata *pem;
  198         struct thread *td = curthread;
  199 
  200         MPASS(SV_CURPROC_ABI() == SV_ABI_LINUX);
  201 
  202         LINUX_CTR3(proc_exit, "thread(%d) proc(%d) p %p",
  203             td->td_tid, p->p_pid, p);
  204 
  205         pem = pem_find(p);
  206         if (pem == NULL)
  207                 return;
  208         (p->p_sysent->sv_thread_detach)(td);
  209 
  210         p->p_emuldata = NULL;
  211 
  212         sx_destroy(&pem->pem_sx);
  213         free(pem, M_LINUX);
  214 }
  215 
  216 /*
  217  * If a Linux binary is exec'ing something, try this image activator
  218  * first.  We override standard shell script execution in order to
  219  * be able to modify the interpreter path.  We only do this if a Linux
  220  * binary is doing the exec, so we do not create an EXEC module for it.
  221  */
  222 int
  223 linux_exec_imgact_try(struct image_params *imgp)
  224 {
  225         const char *head = (const char *)imgp->image_header;
  226         char *rpath;
  227         int error = -1;
  228 
  229         /*
  230          * The interpreter for shell scripts run from a Linux binary needs
  231          * to be located in /compat/linux if possible in order to recursively
  232          * maintain Linux path emulation.
  233          */
  234         if (((const short *)head)[0] == SHELLMAGIC) {
  235                 /*
  236                  * Run our normal shell image activator.  If it succeeds attempt
  237                  * to use the alternate path for the interpreter.  If an
  238                  * alternate path is found, use our stringspace to store it.
  239                  */
  240                 if ((error = exec_shell_imgact(imgp)) == 0) {
  241                         linux_emul_convpath(imgp->interpreter_name, UIO_SYSSPACE,
  242                             &rpath, 0, AT_FDCWD);
  243                         if (rpath != NULL)
  244                                 imgp->args->fname_buf =
  245                                     imgp->interpreter_name = rpath;
  246                 }
  247         }
  248         return (error);
  249 }
  250 
  251 int
  252 linux_common_execve(struct thread *td, struct image_args *eargs)
  253 {
  254         struct linux_pemuldata *pem;
  255         struct vmspace *oldvmspace;
  256         struct linux_emuldata *em;
  257         struct proc *p;
  258         int error;
  259 
  260         p = td->td_proc;
  261 
  262         error = pre_execve(td, &oldvmspace);
  263         if (error != 0)
  264                 return (error);
  265 
  266         error = kern_execve(td, eargs, NULL, oldvmspace);
  267         post_execve(td, error, oldvmspace);
  268         if (error != EJUSTRETURN)
  269                 return (error);
  270 
  271         /*
  272          * In a case of transition from Linux binary execing to
  273          * FreeBSD binary we destroy Linux emuldata thread & proc entries.
  274          */
  275         if (SV_CURPROC_ABI() != SV_ABI_LINUX) {
  276                 PROC_LOCK(p);
  277                 em = em_find(td);
  278                 KASSERT(em != NULL, ("proc_exec: thread emuldata not found.\n"));
  279                 td->td_emuldata = NULL;
  280 
  281                 pem = pem_find(p);
  282                 KASSERT(pem != NULL, ("proc_exec: proc pemuldata not found.\n"));
  283                 p->p_emuldata = NULL;
  284                 PROC_UNLOCK(p);
  285 
  286                 free(em, M_TEMP);
  287                 free(pem, M_LINUX);
  288         }
  289         return (EJUSTRETURN);
  290 }
  291 
  292 void
  293 linux_on_exec(struct proc *p, struct image_params *imgp)
  294 {
  295         struct thread *td;
  296         struct thread *othertd;
  297 #if defined(__amd64__)
  298         struct linux_pemuldata *pem;
  299 #endif
  300 
  301         td = curthread;
  302         MPASS((imgp->sysent->sv_flags & SV_ABI_MASK) == SV_ABI_LINUX);
  303 
  304         /*
  305          * When execing to Linux binary, we create Linux emuldata
  306          * thread entry.
  307          */
  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, false);
  315         } else {
  316                 /*
  317                  * We are switching the process to Linux emulator.
  318                  */
  319                 linux_proc_init(td, td, false);
  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                                 continue;
  330                         linux_proc_init(td, othertd, true);
  331                 }
  332         }
  333 #if defined(__amd64__)
  334         /*
  335          * An IA32 executable which has executable stack will have the
  336          * READ_IMPLIES_EXEC personality flag set automatically.
  337          */
  338         if (SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
  339             imgp->stack_prot & VM_PROT_EXECUTE) {
  340                 pem = pem_find(p);
  341                 pem->persona |= LINUX_READ_IMPLIES_EXEC;
  342         }
  343 #endif
  344 }
  345 
  346 void
  347 linux_thread_dtor(struct thread *td)
  348 {
  349         struct linux_emuldata *em;
  350 
  351         em = em_find(td);
  352         if (em == NULL)
  353                 return;
  354         td->td_emuldata = NULL;
  355 
  356         LINUX_CTR1(thread_dtor, "thread(%d)", em->em_tid);
  357 
  358         free(em, M_TEMP);
  359 }
  360 
  361 void
  362 linux_schedtail(struct thread *td)
  363 {
  364         struct linux_emuldata *em;
  365 #ifdef KTR
  366         int error;
  367 #else
  368         int error __unused;
  369 #endif
  370         int *child_set_tid;
  371 
  372         em = em_find(td);
  373         KASSERT(em != NULL, ("linux_schedtail: thread emuldata not found.\n"));
  374         child_set_tid = em->child_set_tid;
  375 
  376         if (child_set_tid != NULL) {
  377                 error = copyout(&em->em_tid, child_set_tid,
  378                     sizeof(em->em_tid));
  379                 LINUX_CTR4(schedtail, "thread(%d) %p stored %d error %d",
  380                     td->td_tid, child_set_tid, em->em_tid, error);
  381         } else
  382                 LINUX_CTR1(schedtail, "thread(%d)", em->em_tid);
  383 }

Cache object: fe3f2cd0fb3f02c84d2a0eb85645a450


[ 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.