Annotation of src/sys/compat/linux/common/linux_exec.c, revision 1.76

1.76    ! tsutsui     1: /*     $NetBSD: linux_exec.c,v 1.75 2005/05/20 12:48:27 fvdl Exp $     */
1.29      christos    2: 
                      3: /*-
1.44      mycroft     4:  * Copyright (c) 1994, 1995, 1998, 2000 The NetBSD Foundation, Inc.
1.29      christos    5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to The NetBSD Foundation
1.33      fvdl        8:  * by Christos Zoulas, Frank van der Linden, Eric Haszlakiewicz and
                      9:  * Thor Lancelot Simon.
1.29      christos   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:  * 3. All advertising materials mentioning features or use of this software
                     20:  *    must display the following acknowledgement:
                     21:  *        This product includes software developed by the NetBSD
                     22:  *        Foundation, Inc. and its contributors.
                     23:  * 4. Neither the name of The NetBSD Foundation nor the names of its
                     24:  *    contributors may be used to endorse or promote products derived
                     25:  *    from this software without specific prior written permission.
                     26:  *
                     27:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     28:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     29:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     30:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     31:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     32:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     33:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     34:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     35:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     36:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     37:  * POSSIBILITY OF SUCH DAMAGE.
1.1       fvdl       38:  */
1.54      lukem      39: 
                     40: #include <sys/cdefs.h>
1.76    ! tsutsui    41: __KERNEL_RCSID(0, "$NetBSD: linux_exec.c,v 1.75 2005/05/20 12:48:27 fvdl Exp $");
1.1       fvdl       42: 
                     43: #include <sys/param.h>
                     44: #include <sys/systm.h>
                     45: #include <sys/kernel.h>
                     46: #include <sys/proc.h>
                     47: #include <sys/malloc.h>
                     48: #include <sys/namei.h>
                     49: #include <sys/vnode.h>
1.13      christos   50: #include <sys/mount.h>
1.25      christos   51: #include <sys/exec.h>
1.8       fvdl       52: #include <sys/exec_elf.h>
1.1       fvdl       53: 
                     54: #include <sys/mman.h>
1.61      thorpej    55: #include <sys/sa.h>
1.13      christos   56: #include <sys/syscallargs.h>
1.1       fvdl       57: 
1.73      fvdl       58: #include <uvm/uvm_extern.h>
                     59: 
1.1       fvdl       60: #include <machine/cpu.h>
                     61: #include <machine/reg.h>
                     62: 
1.32      christos   63: #include <compat/linux/common/linux_types.h>
                     64: #include <compat/linux/common/linux_signal.h>
                     65: #include <compat/linux/common/linux_util.h>
                     66: #include <compat/linux/common/linux_exec.h>
                     67: #include <compat/linux/common/linux_machdep.h>
                     68: 
                     69: #include <compat/linux/linux_syscallargs.h>
1.4       christos   70: #include <compat/linux/linux_syscall.h>
1.38      jdolecek   71: #include <compat/linux/common/linux_misc.h>
                     72: #include <compat/linux/common/linux_errno.h>
1.39      jdolecek   73: #include <compat/linux/common/linux_emuldata.h>
1.8       fvdl       74: 
1.38      jdolecek   75: extern struct sysent linux_sysent[];
                     76: extern const char * const linux_syscallnames[];
                     77: extern char linux_sigcode[], linux_esigcode[];
1.1       fvdl       78: 
1.39      jdolecek   79: static void linux_e_proc_exec __P((struct proc *, struct exec_package *));
1.71      jdolecek   80: static void linux_e_proc_fork __P((struct proc *, struct proc *, int));
1.39      jdolecek   81: static void linux_e_proc_exit __P((struct proc *));
1.71      jdolecek   82: static void linux_e_proc_init __P((struct proc *, struct proc *, int));
1.39      jdolecek   83: 
1.1       fvdl       84: /*
                     85:  * Execve(2). Just check the alternate emulation path, and pass it on
                     86:  * to the NetBSD execve().
                     87:  */
                     88: int
1.61      thorpej    89: linux_sys_execve(l, v, retval)
                     90:        struct lwp *l;
1.11      thorpej    91:        void *v;
                     92:        register_t *retval;
                     93: {
1.12      mycroft    94:        struct linux_sys_execve_args /* {
1.35      christos   95:                syscallarg(const char *) path;
1.1       fvdl       96:                syscallarg(char **) argv;
                     97:                syscallarg(char **) envp;
1.11      thorpej    98:        } */ *uap = v;
1.61      thorpej    99:        struct proc *p = l->l_proc;
1.16      mycroft   100:        struct sys_execve_args ap;
1.1       fvdl      101:        caddr_t sg;
                    102: 
1.57      christos  103:        sg = stackgap_init(p, 0);
1.64      fvdl      104:        CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
1.1       fvdl      105: 
1.16      mycroft   106:        SCARG(&ap, path) = SCARG(uap, path);
                    107:        SCARG(&ap, argp) = SCARG(uap, argp);
                    108:        SCARG(&ap, envp) = SCARG(uap, envp);
                    109: 
1.61      thorpej   110:        return sys_execve(l, &ap, retval);
1.1       fvdl      111: }
1.38      jdolecek  112: 
                    113: /*
                    114:  * Emulation switch.
                    115:  */
1.65      chs       116: 
                    117: struct uvm_object *emul_linux_object;
                    118: 
1.38      jdolecek  119: const struct emul emul_linux = {
                    120:        "linux",
1.40      jdolecek  121:        "/emul/linux",
1.44      mycroft   122: #ifndef __HAVE_MINIMAL_EMUL
1.51      manu      123:        0,
1.76    ! tsutsui   124:        (const int *)native_to_linux_errno,
1.38      jdolecek  125:        LINUX_SYS_syscall,
1.60      jdolecek  126:        LINUX_SYS_NSYSENT,
1.44      mycroft   127: #endif
1.38      jdolecek  128:        linux_sysent,
                    129:        linux_syscallnames,
1.44      mycroft   130:        linux_sendsig,
1.52      christos  131:        linux_trapsignal,
1.70      fvdl      132:        NULL,
1.38      jdolecek  133:        linux_sigcode,
                    134:        linux_esigcode,
1.65      chs       135:        &emul_linux_object,
1.53      jdolecek  136:        linux_setregs,
1.39      jdolecek  137:        linux_e_proc_exec,
                    138:        linux_e_proc_fork,
                    139:        linux_e_proc_exit,
1.69      manu      140:        NULL,
                    141:        NULL,
1.44      mycroft   142: #ifdef __HAVE_SYSCALL_INTERN
                    143:        linux_syscall_intern,
1.42      jdolecek  144: #else
1.62      thorpej   145: #error Implement __HAVE_SYSCALL_INTERN for this platform
1.41      jdolecek  146: #endif
1.67      atatat    147:        NULL,
1.59      manu      148:        NULL,
1.73      fvdl      149: 
1.74      christos  150:        uvm_default_mapaddr,
1.75      fvdl      151: 
                    152:        linux_usertrap,
1.38      jdolecek  153: };
1.39      jdolecek  154: 
                    155: static void
1.71      jdolecek  156: linux_e_proc_init(p, parent, forkflags)
                    157:        struct proc *p, *parent;
                    158:        int forkflags;
1.39      jdolecek  159: {
1.71      jdolecek  160:        struct linux_emuldata *e = p->p_emuldata;
                    161:        struct linux_emuldata_shared *s;
                    162: 
                    163:        if (!e) {
1.39      jdolecek  164:                /* allocate new Linux emuldata */
1.71      jdolecek  165:                MALLOC(e, void *, sizeof(struct linux_emuldata),
                    166:                        M_EMULDATA, M_WAITOK);
                    167:        } else  {
1.72      jdolecek  168:                e->s->refs--;
1.71      jdolecek  169:                if (e->s->refs == 0)
                    170:                        FREE(e->s, M_EMULDATA);
                    171:        }
                    172: 
                    173:        memset(e, '\0', sizeof(struct linux_emuldata));
                    174: 
1.72      jdolecek  175:        if (forkflags & FORK_SHAREVM) {
1.71      jdolecek  176:                struct linux_emuldata *e2 = parent->p_emuldata;
                    177:                s = e2->s;
                    178:                s->refs++;
                    179:        } else {
                    180:                struct vmspace *vm;
                    181: 
                    182:                MALLOC(s, void *, sizeof(struct linux_emuldata_shared),
1.39      jdolecek  183:                        M_EMULDATA, M_WAITOK);
1.71      jdolecek  184:                s->refs = 1;
                    185: 
                    186:                /*
                    187:                 * Set the process idea of the break to the real value.
                    188:                 * For fork, we use parent's vmspace since our's
                    189:                 * is not setup at the time of this call and is going
                    190:                 * to be copy of parent's anyway. For exec, just
                    191:                 * use our own vmspace.
                    192:                 */
                    193:                vm = (parent) ? parent->p_vmspace : p->p_vmspace;
                    194:                s->p_break = vm->vm_daddr + ctob(vm->vm_dsize);
                    195: 
1.39      jdolecek  196:        }
                    197: 
1.71      jdolecek  198:        e->s = s;
                    199:        p->p_emuldata = e;
1.48      jdolecek  200: }
                    201: 
                    202: /*
1.71      jdolecek  203:  * Allocate new per-process structures. Called when executing Linux
1.48      jdolecek  204:  * process. We can reuse the old emuldata - if it's not null,
                    205:  * the executed process is of same emulation as original forked one.
                    206:  */
                    207: static void
                    208: linux_e_proc_exec(p, epp)
                    209:        struct proc *p;
                    210:        struct exec_package *epp;
                    211: {
                    212:        /* exec, use our vmspace */
1.71      jdolecek  213:        linux_e_proc_init(p, NULL, 0);
1.39      jdolecek  214: }
                    215: 
                    216: /*
                    217:  * Emulation per-process exit hook.
                    218:  */
                    219: static void
                    220: linux_e_proc_exit(p)
                    221:        struct proc *p;
                    222: {
1.71      jdolecek  223:        struct linux_emuldata *e = p->p_emuldata;
                    224: 
1.39      jdolecek  225:        /* free Linux emuldata and set the pointer to null */
1.71      jdolecek  226:        e->s->refs--;
                    227:        if (e->s->refs == 0)
                    228:                FREE(e->s, M_EMULDATA);
                    229:        FREE(e, M_EMULDATA);
1.39      jdolecek  230:        p->p_emuldata = NULL;
                    231: }
                    232: 
                    233: /*
                    234:  * Emulation fork hook.
                    235:  */
                    236: static void
1.71      jdolecek  237: linux_e_proc_fork(p, parent, forkflags)
1.39      jdolecek  238:        struct proc *p, *parent;
1.71      jdolecek  239:        int forkflags;
1.39      jdolecek  240: {
                    241:        /*
1.71      jdolecek  242:         * The new process might share some vmspace-related stuff
                    243:         * with parent, depending on fork flags (CLONE_VM et.al).
                    244:         * Force allocation of new base emuldata, and share the
                    245:         * VM-related parts only if necessary.
1.39      jdolecek  246:         */
                    247:        p->p_emuldata = NULL;
1.71      jdolecek  248:        linux_e_proc_init(p, parent, forkflags);
1.39      jdolecek  249: }

CVSweb <webmaster@jp.NetBSD.org>