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/linux32/common/linux32_exec.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 /*      $NetBSD: linux32_exec.c,v 1.15 2008/10/15 06:51:19 wrstuden Exp $ */
    2 
    3 /*-
    4  * Copyright (c) 1994-2007 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * This code is derived from software contributed to The NetBSD Foundation
    8  * by Christos Zoulas, Frank van der Linden, Eric Haszlakiewicz,
    9  * Thor Lancelot Simon, and Emmanuel Dreyfus.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   30  * POSSIBILITY OF SUCH DAMAGE.
   31  */
   32 
   33 #include <sys/cdefs.h>
   34 __KERNEL_RCSID(0, "$NetBSD: linux32_exec.c,v 1.15 2008/10/15 06:51:19 wrstuden Exp $");
   35 
   36 #include <sys/param.h>
   37 #include <sys/systm.h>
   38 #include <sys/kernel.h>
   39 #include <sys/proc.h>
   40 #include <sys/malloc.h>
   41 #include <sys/namei.h>
   42 #include <sys/vnode.h>
   43 #include <sys/mount.h>
   44 #include <sys/exec.h>
   45 #include <sys/exec_elf.h>
   46 
   47 #include <sys/mman.h>
   48 #include <sys/syscallargs.h>
   49 #include <sys/ptrace.h>         /* For proc_reparent() */
   50 
   51 #include <uvm/uvm_extern.h>
   52 
   53 #include <sys/cpu.h>
   54 #include <machine/reg.h>
   55 
   56 #include <compat/linux/common/linux_types.h>
   57 #include <compat/linux/common/linux_emuldata.h>
   58 
   59 #include <compat/linux32/common/linux32_exec.h>
   60 #include <compat/linux32/common/linux32_types.h>
   61 #include <compat/linux32/common/linux32_signal.h>
   62 #include <compat/linux32/common/linux32_machdep.h>
   63 
   64 #include <compat/linux32/linux32_syscallargs.h>
   65 #include <compat/linux32/linux32_syscall.h>
   66 
   67 extern char linux32_sigcode[1];
   68 extern char linux32_rt_sigcode[1];
   69 extern char linux32_esigcode[1];
   70 
   71 extern struct sysent linux32_sysent[];
   72 extern const char * const linux32_syscallnames[];
   73 
   74 static void linux32_e_proc_exec(struct proc *, struct exec_package *);
   75 static void linux32_e_proc_fork(struct proc *, struct proc *, int);
   76 static void linux32_e_proc_exit(struct proc *);
   77 static void linux32_e_proc_init(struct proc *, struct proc *, int);
   78 
   79 #ifdef LINUX32_NPTL
   80 void linux32_userret(void);
   81 void linux_nptl_proc_fork(struct proc *, struct proc *, void (*luserret)(void));
   82 void linux_nptl_proc_exit(struct proc *);
   83 void linux_nptl_proc_init(struct proc *, struct proc *);
   84 #endif
   85 
   86 /*
   87  * Emulation switch.
   88  */
   89 
   90 struct uvm_object *emul_linux32_object;
   91 
   92 const struct emul emul_linux32 = {
   93         "linux32",
   94         "/emul/linux32",
   95 #ifndef __HAVE_MINIMAL_EMUL
   96         0,
   97         NULL,
   98         LINUX32_SYS_syscall,
   99         LINUX32_SYS_NSYSENT,
  100 #endif
  101         linux32_sysent,
  102         linux32_syscallnames,
  103         linux32_sendsig,
  104         trapsignal,
  105         NULL,
  106         linux32_sigcode,
  107         linux32_esigcode,
  108         &emul_linux32_object,
  109         linux32_setregs,
  110         linux32_e_proc_exec,
  111         linux32_e_proc_fork,
  112         linux32_e_proc_exit,
  113         NULL,
  114         NULL,
  115         linux32_syscall_intern,
  116         NULL,
  117         NULL,
  118         netbsd32_vm_default_addr,
  119         NULL,
  120         NULL,
  121         0,
  122         NULL
  123 };
  124 
  125 static void
  126 linux32_e_proc_init(p, parent, forkflags)
  127         struct proc *p, *parent;
  128         int forkflags;
  129 {
  130         struct linux_emuldata *e = p->p_emuldata;
  131         struct linux_emuldata_shared *s;
  132         struct linux_emuldata *ep = NULL;
  133 
  134         if (!e) {
  135                 /* allocate new Linux emuldata */
  136                 MALLOC(e, void *, sizeof(struct linux_emuldata),
  137                         M_EMULDATA, M_WAITOK);
  138         } else  {
  139                 mutex_enter(proc_lock);
  140                 e->s->refs--;
  141                 if (e->s->refs == 0)
  142                         FREE(e->s, M_EMULDATA);
  143                 mutex_exit(proc_lock);
  144         }
  145 
  146         memset(e, '\0', sizeof(struct linux_emuldata));
  147 
  148         e->proc = p;
  149 
  150         if (parent)
  151                 ep = parent->p_emuldata;
  152 
  153         if (forkflags & FORK_SHAREVM) {
  154                 mutex_enter(proc_lock);
  155 #ifdef DIAGNOSTIC
  156                 if (ep == NULL) {
  157                         killproc(p, "FORK_SHAREVM while emuldata is NULL\n");
  158                         mutex_exit(proc_lock);
  159                         return;
  160                 }
  161 #endif
  162                 s = ep->s;
  163                 s->refs++;
  164         } else {
  165                 struct vmspace *vm;
  166 
  167                 MALLOC(s, void *, sizeof(struct linux_emuldata_shared),
  168                         M_EMULDATA, M_WAITOK);
  169                 s->refs = 1;
  170 
  171                 /*
  172                  * Set the process idea of the break to the real value.
  173                  * For fork, we use parent's vmspace since our's
  174                  * is not setup at the time of this call and is going
  175                  * to be copy of parent's anyway. For exec, just
  176                  * use our own vmspace.
  177                  */
  178                 vm = (parent) ? parent->p_vmspace : p->p_vmspace;
  179                 s->p_break = (char *)vm->vm_daddr + ctob(vm->vm_dsize);
  180 
  181                 /*
  182                  * Linux threads are emulated as NetBSD processes (not lwp)
  183                  * We use native PID for Linux TID. The Linux TID is the
  184                  * PID of the first process in the group. It is stored
  185                  * here
  186                  */
  187                 s->group_pid = p->p_pid;
  188 
  189                 /*
  190                  * Initialize the list of threads in the group
  191                  */
  192                 LIST_INIT(&s->threads); 
  193 
  194                 s->xstat = 0;
  195                 s->flags = 0;
  196                 mutex_enter(proc_lock);
  197         }
  198 
  199         e->s = s;
  200 
  201         /*
  202          * Add this thread in the group thread list
  203          */
  204         LIST_INSERT_HEAD(&s->threads, e, threads);
  205         mutex_exit(proc_lock);
  206 
  207 #ifdef LINUX32_NPTL
  208         linux_nptl_proc_init(p, parent);
  209 #endif /* LINUX32_NPTL */
  210 
  211         p->p_emuldata = e;
  212 }
  213 
  214 /*
  215  * Allocate new per-process structures. Called when executing Linux
  216  * process. We can reuse the old emuldata - if it's not null,
  217  * the executed process is of same emulation as original forked one.
  218  */
  219 static void
  220 linux32_e_proc_exec(struct proc *p, struct exec_package *epp)
  221 {
  222         /* exec, use our vmspace */
  223         linux32_e_proc_init(p, NULL, 0);
  224 }
  225 
  226 /*
  227  * Emulation per-process exit hook.
  228  */
  229 static void
  230 linux32_e_proc_exit(struct proc *p)
  231 {
  232         struct linux_emuldata *e = p->p_emuldata;
  233 
  234 #ifdef LINUX32_NPTL
  235         linux_nptl_proc_exit(p);
  236 #endif /* LINUX32_NPTL */
  237 
  238         /* Remove the thread for the group thread list */
  239         mutex_enter(proc_lock);
  240         LIST_REMOVE(e, threads);
  241 
  242         /* free Linux emuldata and set the pointer to null */
  243         e->s->refs--;
  244         if (e->s->refs == 0)
  245                 FREE(e->s, M_EMULDATA);
  246         p->p_emuldata = NULL;
  247         mutex_exit(proc_lock);
  248         FREE(e, M_EMULDATA);
  249 }
  250 
  251 /*
  252  * Emulation fork hook.
  253  */
  254 static void
  255 linux32_e_proc_fork(p, parent, forkflags)
  256         struct proc *p, *parent;
  257         int forkflags;
  258 {
  259         /*
  260          * The new process might share some vmspace-related stuff
  261          * with parent, depending on fork flags (CLONE_VM et.al).
  262          * Force allocation of new base emuldata, and share the
  263          * VM-related parts only if necessary.
  264          */
  265         p->p_emuldata = NULL;
  266         linux32_e_proc_init(p, parent, forkflags);
  267 
  268 #ifdef LINUX32_NPTL
  269         linux_nptl_proc_fork(p, parent, linux32_userret);
  270 #endif
  271 
  272         return;
  273 }
  274 
  275 #ifdef LINUX32_NPTL
  276 void
  277 linux32_userret(void)
  278 {
  279         struct lwp *l = curlwp;
  280         struct proc *p = l->l_proc;
  281         struct linux_emuldata *led = p->p_emuldata;
  282         int error;
  283 
  284         /* LINUX_CLONE_CHILD_SETTID: copy child's TID to child's memory  */
  285         if (led->clone_flags & LINUX_CLONE_CHILD_SETTID) {
  286                 if ((error = copyout(&l->l_proc->p_pid,
  287                     led->child_tidptr,  sizeof(l->l_proc->p_pid))) != 0)
  288                         printf("linux32_userret: LINUX_CLONE_CHILD_SETTID "
  289                             "failed (led->child_tidptr = %p, p->p_pid = %d)\n",
  290                             led->child_tidptr, p->p_pid);
  291         }
  292 
  293         /* LINUX_CLONE_SETTLS: allocate a new TLS */
  294         if (led->clone_flags & LINUX_CLONE_SETTLS) {
  295                 if (linux32_set_newtls(l, linux32_get_newtls(l)) != 0)
  296                         printf("linux32_userret: linux32_set_tls failed");
  297         }
  298 
  299         return; 
  300 }
  301 #endif /* LINUX32_NPTL */

Cache object: a7e6e34ebaadb81cba745b4e7c208bb5


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