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: releng/12.0/sys/compat/linux/linux_emul.c 332333 2018-04-09 17:24:01Z emaste $");
   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/syscallsubr.h>
   46 #include <sys/sysent.h>
   47 
   48 #include <compat/linux/linux_emul.h>
   49 #include <compat/linux/linux_misc.h>
   50 #include <compat/linux/linux_persona.h>
   51 #include <compat/linux/linux_util.h>
   52 
   53 #if BYTE_ORDER == LITTLE_ENDIAN
   54 #define SHELLMAGIC      0x2123 /* #! */
   55 #else
   56 #define SHELLMAGIC      0x2321
   57 #endif
   58 
   59 /*
   60  * This returns reference to the thread emuldata entry (if found)
   61  *
   62  * Hold PROC_LOCK when referencing emuldata from other threads.
   63  */
   64 struct linux_emuldata *
   65 em_find(struct thread *td)
   66 {
   67         struct linux_emuldata *em;
   68 
   69         em = td->td_emuldata;
   70 
   71         return (em);
   72 }
   73 
   74 /*
   75  * This returns reference to the proc pemuldata entry (if found)
   76  *
   77  * Hold PROC_LOCK when referencing proc pemuldata from other threads.
   78  * Hold LINUX_PEM_LOCK wher referencing pemuldata members.
   79  */
   80 struct linux_pemuldata *
   81 pem_find(struct proc *p)
   82 {
   83         struct linux_pemuldata *pem;
   84 
   85         pem = p->p_emuldata;
   86 
   87         return (pem);
   88 }
   89 
   90 void
   91 linux_proc_init(struct thread *td, struct thread *newtd, int flags)
   92 {
   93         struct linux_emuldata *em;
   94         struct linux_pemuldata *pem;
   95         struct epoll_emuldata *emd;
   96         struct proc *p;
   97 
   98         if (newtd != NULL) {
   99                 p = newtd->td_proc;
  100 
  101                 /* non-exec call */
  102                 em = malloc(sizeof(*em), M_TEMP, M_WAITOK | M_ZERO);
  103                 if (flags & LINUX_CLONE_THREAD) {
  104                         LINUX_CTR1(proc_init, "thread newtd(%d)",
  105                             newtd->td_tid);
  106 
  107                         em->em_tid = newtd->td_tid;
  108                 } else {
  109                         LINUX_CTR1(proc_init, "fork newtd(%d)", p->p_pid);
  110 
  111                         em->em_tid = p->p_pid;
  112 
  113                         pem = malloc(sizeof(*pem), M_LINUX, M_WAITOK | M_ZERO);
  114                         sx_init(&pem->pem_sx, "lpemlk");
  115                         p->p_emuldata = pem;
  116                 }
  117                 newtd->td_emuldata = em;
  118         } else {
  119                 p = td->td_proc;
  120 
  121                 /* exec */
  122                 LINUX_CTR1(proc_init, "exec newtd(%d)", p->p_pid);
  123 
  124                 /* lookup the old one */
  125                 em = em_find(td);
  126                 KASSERT(em != NULL, ("proc_init: emuldata not found in exec case.\n"));
  127 
  128                 em->em_tid = p->p_pid;
  129                 em->flags = 0;
  130                 em->pdeath_signal = 0;
  131                 em->robust_futexes = NULL;
  132                 em->child_clear_tid = NULL;
  133                 em->child_set_tid = NULL;
  134 
  135                  /* epoll should be destroyed in a case of exec. */
  136                 pem = pem_find(p);
  137                 KASSERT(pem != NULL, ("proc_exit: proc emuldata not found.\n"));
  138                 pem->persona = 0;
  139                 if (pem->epoll != NULL) {
  140                         emd = pem->epoll;
  141                         pem->epoll = NULL;
  142                         free(emd, M_EPOLL);
  143                 }
  144         }
  145 
  146 }
  147 
  148 void
  149 linux_proc_exit(void *arg __unused, struct proc *p)
  150 {
  151         struct linux_pemuldata *pem;
  152         struct epoll_emuldata *emd;
  153         struct thread *td = curthread;
  154 
  155         if (__predict_false(SV_CURPROC_ABI() != SV_ABI_LINUX))
  156                 return;
  157 
  158         LINUX_CTR3(proc_exit, "thread(%d) proc(%d) p %p",
  159             td->td_tid, p->p_pid, p);
  160 
  161         pem = pem_find(p);
  162         if (pem == NULL)
  163                 return;
  164         (p->p_sysent->sv_thread_detach)(td);
  165 
  166         p->p_emuldata = NULL;
  167 
  168         if (pem->epoll != NULL) {
  169                 emd = pem->epoll;
  170                 pem->epoll = NULL;
  171                 free(emd, M_EPOLL);
  172         }
  173 
  174         sx_destroy(&pem->pem_sx);
  175         free(pem, M_LINUX);
  176 }
  177 
  178 /*
  179  * If a Linux binary is exec'ing something, try this image activator
  180  * first.  We override standard shell script execution in order to
  181  * be able to modify the interpreter path.  We only do this if a Linux
  182  * binary is doing the exec, so we do not create an EXEC module for it.
  183  */
  184 int
  185 linux_exec_imgact_try(struct image_params *imgp)
  186 {
  187         const char *head = (const char *)imgp->image_header;
  188         char *rpath;
  189         int error = -1;
  190 
  191         /*
  192          * The interpreter for shell scripts run from a Linux binary needs
  193          * to be located in /compat/linux if possible in order to recursively
  194          * maintain Linux path emulation.
  195          */
  196         if (((const short *)head)[0] == SHELLMAGIC) {
  197                 /*
  198                  * Run our normal shell image activator.  If it succeeds attempt
  199                  * to use the alternate path for the interpreter.  If an
  200                  * alternate path is found, use our stringspace to store it.
  201                  */
  202                 if ((error = exec_shell_imgact(imgp)) == 0) {
  203                         linux_emul_convpath(FIRST_THREAD_IN_PROC(imgp->proc),
  204                             imgp->interpreter_name, UIO_SYSSPACE, &rpath, 0,
  205                             AT_FDCWD);
  206                         if (rpath != NULL)
  207                                 imgp->args->fname_buf =
  208                                     imgp->interpreter_name = rpath;
  209                 }
  210         }
  211         return (error);
  212 }
  213 
  214 int
  215 linux_common_execve(struct thread *td, struct image_args *eargs)
  216 {
  217         struct linux_pemuldata *pem;
  218         struct epoll_emuldata *emd;
  219         struct vmspace *oldvmspace;
  220         struct linux_emuldata *em;
  221         struct proc *p;
  222         int error;
  223 
  224         p = td->td_proc;
  225 
  226         error = pre_execve(td, &oldvmspace);
  227         if (error != 0)
  228                 return (error);
  229 
  230         error = kern_execve(td, eargs, NULL);
  231         post_execve(td, error, oldvmspace);
  232         if (error != EJUSTRETURN)
  233                 return (error);
  234 
  235         /*
  236          * In a case of transition from Linux binary execing to
  237          * FreeBSD binary we destroy Linux emuldata thread & proc entries.
  238          */
  239         if (SV_CURPROC_ABI() != SV_ABI_LINUX) {
  240                 PROC_LOCK(p);
  241                 em = em_find(td);
  242                 KASSERT(em != NULL, ("proc_exec: thread emuldata not found.\n"));
  243                 td->td_emuldata = NULL;
  244 
  245                 pem = pem_find(p);
  246                 KASSERT(pem != NULL, ("proc_exec: proc pemuldata not found.\n"));
  247                 p->p_emuldata = NULL;
  248                 PROC_UNLOCK(p);
  249 
  250                 if (pem->epoll != NULL) {
  251                         emd = pem->epoll;
  252                         pem->epoll = NULL;
  253                         free(emd, M_EPOLL);
  254                 }
  255 
  256                 free(em, M_TEMP);
  257                 free(pem, M_LINUX);
  258         }
  259         return (EJUSTRETURN);
  260 }
  261 
  262 void
  263 linux_proc_exec(void *arg __unused, struct proc *p, struct image_params *imgp)
  264 {
  265         struct thread *td = curthread;
  266         struct thread *othertd;
  267 #if defined(__amd64__)
  268         struct linux_pemuldata *pem;
  269 #endif
  270 
  271         /*
  272          * In a case of execing from Linux binary properly detach
  273          * other threads from the user space.
  274          */
  275         if (__predict_false(SV_PROC_ABI(p) == SV_ABI_LINUX)) {
  276                 FOREACH_THREAD_IN_PROC(p, othertd) {
  277                         if (td != othertd)
  278                                 (p->p_sysent->sv_thread_detach)(othertd);
  279                 }
  280         }
  281 
  282         /*
  283          * In a case of execing to Linux binary we create Linux
  284          * emuldata thread entry.
  285          */
  286         if (__predict_false((imgp->sysent->sv_flags & SV_ABI_MASK) ==
  287             SV_ABI_LINUX)) {
  288 
  289                 if (SV_PROC_ABI(p) == SV_ABI_LINUX)
  290                         linux_proc_init(td, NULL, 0);
  291                 else
  292                         linux_proc_init(td, td, 0);
  293 #if defined(__amd64__)
  294                 /*
  295                  * An IA32 executable which has executable stack will have the
  296                  * READ_IMPLIES_EXEC personality flag set automatically.
  297                  */
  298                 if (SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
  299                     imgp->stack_prot & VM_PROT_EXECUTE) {
  300                         pem = pem_find(p);
  301                         pem->persona |= LINUX_READ_IMPLIES_EXEC;
  302                 }
  303 #endif
  304         }
  305 }
  306 
  307 void
  308 linux_thread_dtor(void *arg __unused, struct thread *td)
  309 {
  310         struct linux_emuldata *em;
  311 
  312         em = em_find(td);
  313         if (em == NULL)
  314                 return;
  315         td->td_emuldata = NULL;
  316 
  317         LINUX_CTR1(thread_dtor, "thread(%d)", em->em_tid);
  318 
  319         free(em, M_TEMP);
  320 }
  321 
  322 void
  323 linux_schedtail(struct thread *td)
  324 {
  325         struct linux_emuldata *em;
  326         struct proc *p;
  327         int error = 0;
  328         int *child_set_tid;
  329 
  330         p = td->td_proc;
  331 
  332         em = em_find(td);
  333         KASSERT(em != NULL, ("linux_schedtail: thread emuldata not found.\n"));
  334         child_set_tid = em->child_set_tid;
  335 
  336         if (child_set_tid != NULL) {
  337                 error = copyout(&em->em_tid, child_set_tid,
  338                     sizeof(em->em_tid));
  339                 LINUX_CTR4(schedtail, "thread(%d) %p stored %d error %d",
  340                     td->td_tid, child_set_tid, em->em_tid, error);
  341         } else
  342                 LINUX_CTR1(schedtail, "thread(%d)", em->em_tid);
  343 }

Cache object: e3f908f670949036dbcb7741e2d30211


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