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

1.78    ! manu        1: /*     $NetBSD: linux_exec.c,v 1.77 2005/06/22 15:10:51 manu 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.78    ! manu       41: __KERNEL_RCSID(0, "$NetBSD: linux_exec.c,v 1.77 2005/06/22 15:10:51 manu 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>
1.78    ! manu       68: #include <compat/linux/common/linux_futex.h>
1.32      christos   69: 
                     70: #include <compat/linux/linux_syscallargs.h>
1.4       christos   71: #include <compat/linux/linux_syscall.h>
1.38      jdolecek   72: #include <compat/linux/common/linux_misc.h>
                     73: #include <compat/linux/common/linux_errno.h>
1.39      jdolecek   74: #include <compat/linux/common/linux_emuldata.h>
1.8       fvdl       75: 
1.38      jdolecek   76: extern struct sysent linux_sysent[];
                     77: extern const char * const linux_syscallnames[];
                     78: extern char linux_sigcode[], linux_esigcode[];
1.1       fvdl       79: 
1.39      jdolecek   80: static void linux_e_proc_exec __P((struct proc *, struct exec_package *));
1.71      jdolecek   81: static void linux_e_proc_fork __P((struct proc *, struct proc *, int));
1.39      jdolecek   82: static void linux_e_proc_exit __P((struct proc *));
1.71      jdolecek   83: static void linux_e_proc_init __P((struct proc *, struct proc *, int));
1.39      jdolecek   84: 
1.77      manu       85: static void linux_userret_settid __P((struct lwp *, void *));
                     86: 
1.1       fvdl       87: /*
                     88:  * Execve(2). Just check the alternate emulation path, and pass it on
                     89:  * to the NetBSD execve().
                     90:  */
                     91: int
1.61      thorpej    92: linux_sys_execve(l, v, retval)
                     93:        struct lwp *l;
1.11      thorpej    94:        void *v;
                     95:        register_t *retval;
                     96: {
1.12      mycroft    97:        struct linux_sys_execve_args /* {
1.35      christos   98:                syscallarg(const char *) path;
1.1       fvdl       99:                syscallarg(char **) argv;
                    100:                syscallarg(char **) envp;
1.11      thorpej   101:        } */ *uap = v;
1.61      thorpej   102:        struct proc *p = l->l_proc;
1.16      mycroft   103:        struct sys_execve_args ap;
1.1       fvdl      104:        caddr_t sg;
                    105: 
1.57      christos  106:        sg = stackgap_init(p, 0);
1.64      fvdl      107:        CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
1.1       fvdl      108: 
1.16      mycroft   109:        SCARG(&ap, path) = SCARG(uap, path);
                    110:        SCARG(&ap, argp) = SCARG(uap, argp);
                    111:        SCARG(&ap, envp) = SCARG(uap, envp);
                    112: 
1.61      thorpej   113:        return sys_execve(l, &ap, retval);
1.1       fvdl      114: }
1.38      jdolecek  115: 
                    116: /*
                    117:  * Emulation switch.
                    118:  */
1.65      chs       119: 
                    120: struct uvm_object *emul_linux_object;
                    121: 
1.38      jdolecek  122: const struct emul emul_linux = {
                    123:        "linux",
1.40      jdolecek  124:        "/emul/linux",
1.44      mycroft   125: #ifndef __HAVE_MINIMAL_EMUL
1.51      manu      126:        0,
1.76      tsutsui   127:        (const int *)native_to_linux_errno,
1.38      jdolecek  128:        LINUX_SYS_syscall,
1.60      jdolecek  129:        LINUX_SYS_NSYSENT,
1.44      mycroft   130: #endif
1.38      jdolecek  131:        linux_sysent,
                    132:        linux_syscallnames,
1.44      mycroft   133:        linux_sendsig,
1.52      christos  134:        linux_trapsignal,
1.70      fvdl      135:        NULL,
1.38      jdolecek  136:        linux_sigcode,
                    137:        linux_esigcode,
1.65      chs       138:        &emul_linux_object,
1.53      jdolecek  139:        linux_setregs,
1.39      jdolecek  140:        linux_e_proc_exec,
                    141:        linux_e_proc_fork,
                    142:        linux_e_proc_exit,
1.69      manu      143:        NULL,
                    144:        NULL,
1.44      mycroft   145: #ifdef __HAVE_SYSCALL_INTERN
                    146:        linux_syscall_intern,
1.42      jdolecek  147: #else
1.62      thorpej   148: #error Implement __HAVE_SYSCALL_INTERN for this platform
1.41      jdolecek  149: #endif
1.67      atatat    150:        NULL,
1.59      manu      151:        NULL,
1.73      fvdl      152: 
1.74      christos  153:        uvm_default_mapaddr,
1.75      fvdl      154: 
                    155:        linux_usertrap,
1.38      jdolecek  156: };
1.39      jdolecek  157: 
                    158: static void
1.71      jdolecek  159: linux_e_proc_init(p, parent, forkflags)
                    160:        struct proc *p, *parent;
                    161:        int forkflags;
1.39      jdolecek  162: {
1.71      jdolecek  163:        struct linux_emuldata *e = p->p_emuldata;
                    164:        struct linux_emuldata_shared *s;
1.77      manu      165:        struct linux_emuldata *ep = NULL;
1.71      jdolecek  166: 
                    167:        if (!e) {
1.39      jdolecek  168:                /* allocate new Linux emuldata */
1.71      jdolecek  169:                MALLOC(e, void *, sizeof(struct linux_emuldata),
                    170:                        M_EMULDATA, M_WAITOK);
                    171:        } else  {
1.72      jdolecek  172:                e->s->refs--;
1.71      jdolecek  173:                if (e->s->refs == 0)
                    174:                        FREE(e->s, M_EMULDATA);
                    175:        }
                    176: 
                    177:        memset(e, '\0', sizeof(struct linux_emuldata));
                    178: 
1.77      manu      179:        if (parent)
                    180:                ep = parent->p_emuldata;
                    181: 
1.72      jdolecek  182:        if (forkflags & FORK_SHAREVM) {
1.77      manu      183: #ifdef DIAGNOSTIC
                    184:                if (ep == NULL) {
                    185:                        killproc(p, "FORK_SHAREVM while emuldata is NULL\n");
                    186:                        return;
                    187:                }
                    188: #endif
                    189:                s = ep->s;
1.71      jdolecek  190:                s->refs++;
                    191:        } else {
                    192:                struct vmspace *vm;
                    193: 
                    194:                MALLOC(s, void *, sizeof(struct linux_emuldata_shared),
1.39      jdolecek  195:                        M_EMULDATA, M_WAITOK);
1.71      jdolecek  196:                s->refs = 1;
                    197: 
                    198:                /*
                    199:                 * Set the process idea of the break to the real value.
                    200:                 * For fork, we use parent's vmspace since our's
                    201:                 * is not setup at the time of this call and is going
                    202:                 * to be copy of parent's anyway. For exec, just
                    203:                 * use our own vmspace.
                    204:                 */
                    205:                vm = (parent) ? parent->p_vmspace : p->p_vmspace;
                    206:                s->p_break = vm->vm_daddr + ctob(vm->vm_dsize);
                    207: 
1.77      manu      208:                /*
                    209:                 * Linux threads are emulated as NetBSD processes (not lwp)
                    210:                 * We use native PID for Linux TID. The Linux TID is the
                    211:                 * PID of the first process in the group. It is stored
                    212:                 * here
                    213:                 */
                    214:                s->group_pid = p->p_pid;
1.39      jdolecek  215:        }
                    216: 
1.71      jdolecek  217:        e->s = s;
1.77      manu      218: 
                    219:        /* 
                    220:         * initialize TID pointers. ep->child_clear_tid and 
                    221:         * ep->child_set_tid will not be used beyond this point.
                    222:         */
                    223:        e->child_clear_tid = NULL;
                    224:        e->child_set_tid = NULL;
                    225:        if (ep != NULL) {
                    226:                e->clear_tid = ep->child_clear_tid;
                    227:                e->set_tid = ep->child_set_tid;
                    228:                ep->child_clear_tid = NULL;
                    229:                ep->child_set_tid = NULL;
                    230:        } else {
                    231:                e->clear_tid = NULL;
                    232:                e->set_tid = NULL;
                    233:        }
                    234: 
1.71      jdolecek  235:        p->p_emuldata = e;
1.48      jdolecek  236: }
                    237: 
                    238: /*
1.71      jdolecek  239:  * Allocate new per-process structures. Called when executing Linux
1.48      jdolecek  240:  * process. We can reuse the old emuldata - if it's not null,
                    241:  * the executed process is of same emulation as original forked one.
                    242:  */
                    243: static void
                    244: linux_e_proc_exec(p, epp)
                    245:        struct proc *p;
                    246:        struct exec_package *epp;
                    247: {
                    248:        /* exec, use our vmspace */
1.71      jdolecek  249:        linux_e_proc_init(p, NULL, 0);
1.39      jdolecek  250: }
                    251: 
                    252: /*
                    253:  * Emulation per-process exit hook.
                    254:  */
                    255: static void
                    256: linux_e_proc_exit(p)
                    257:        struct proc *p;
                    258: {
1.71      jdolecek  259:        struct linux_emuldata *e = p->p_emuldata;
                    260: 
1.77      manu      261:        /* Emulate LINUX_CLONE_CHILD_CLEARTID */
                    262:        if (e->clear_tid != NULL) {
                    263:                int error;
                    264:                int null = 0;
1.78    ! manu      265:                struct linux_sys_futex_args cup;
        !           266:                register_t retval;
        !           267:                struct lwp *l;
1.77      manu      268: 
                    269:                if ((error = copyout(&null, 
                    270:                    e->clear_tid, 
                    271:                    sizeof(null))) != 0)
                    272:                        printf("linux_e_proc_exit: cannot clear TID\n");
                    273: 
1.78    ! manu      274:                l = proc_representative_lwp(p);
        !           275:                SCARG(&cup, uaddr) = e->clear_tid;
        !           276:                SCARG(&cup, op) = LINUX_FUTEX_WAKE;
        !           277:                SCARG(&cup, val) = 0;
        !           278:                SCARG(&cup, timeout) = NULL;
        !           279:                SCARG(&cup, uaddr2) = NULL;
        !           280:                SCARG(&cup, val3) = 0;
        !           281:                if ((error = linux_sys_futex(l, &cup, &retval)) != 0)
1.77      manu      282:                        printf("linux_e_proc_exit: linux_sys_futex failed\n");
                    283:        }
                    284: 
1.39      jdolecek  285:        /* free Linux emuldata and set the pointer to null */
1.71      jdolecek  286:        e->s->refs--;
                    287:        if (e->s->refs == 0)
                    288:                FREE(e->s, M_EMULDATA);
                    289:        FREE(e, M_EMULDATA);
1.39      jdolecek  290:        p->p_emuldata = NULL;
                    291: }
                    292: 
                    293: /*
                    294:  * Emulation fork hook.
                    295:  */
                    296: static void
1.71      jdolecek  297: linux_e_proc_fork(p, parent, forkflags)
1.39      jdolecek  298:        struct proc *p, *parent;
1.71      jdolecek  299:        int forkflags;
1.39      jdolecek  300: {
1.77      manu      301:        struct linux_emuldata *e;
                    302: 
1.39      jdolecek  303:        /*
1.71      jdolecek  304:         * The new process might share some vmspace-related stuff
                    305:         * with parent, depending on fork flags (CLONE_VM et.al).
                    306:         * Force allocation of new base emuldata, and share the
                    307:         * VM-related parts only if necessary.
1.39      jdolecek  308:         */
                    309:        p->p_emuldata = NULL;
1.71      jdolecek  310:        linux_e_proc_init(p, parent, forkflags);
1.77      manu      311: 
                    312:        /* 
                    313:         * Emulate LINUX_CLONE_CHILD_SETTID: This cannot be done  
                    314:         * right now because the child VM is not set up. We will
                    315:         * do it at userret time.
                    316:         */
                    317:        e = p->p_emuldata;
                    318:        if (e->set_tid != NULL) 
                    319:                p->p_userret = (*linux_userret_settid);
                    320: 
                    321:        return;
                    322: }
                    323: 
                    324: static void
                    325: linux_userret_settid(l, arg)
                    326:        struct lwp *l;
                    327:        void *arg;
                    328: {
                    329:        struct proc *p = l->l_proc;
                    330:        struct linux_emuldata *led = p->p_emuldata;
                    331:        int error;
                    332: 
                    333:        p->p_userret = NULL;
                    334: 
                    335:        /* Emulate LINUX_CLONE_CHILD_SETTID  */
                    336:        if (led->set_tid != NULL) {
                    337:                if ((error = copyout(&p->p_pid,
                    338:                    led->set_tid, sizeof(p->p_pid))) != 0)
                    339:                        printf("linux_userret_settid: cannot set TID\n");
                    340:        }
                    341: 
                    342:        return; 
1.39      jdolecek  343: }

CVSweb <webmaster@jp.NetBSD.org>