Home | History | Annotate | Line # | Download | only in common
linux_exec.c revision 1.118.2.1
      1  1.118.2.1  pgoyette /*	$NetBSD: linux_exec.c,v 1.118.2.1 2018/05/21 04:36:03 pgoyette Exp $	*/
      2       1.29  christos 
      3       1.29  christos /*-
      4      1.103        ad  * Copyright (c) 1994, 1995, 1998, 2000, 2007, 2008 The NetBSD Foundation, Inc.
      5       1.29  christos  * All rights reserved.
      6       1.29  christos  *
      7       1.29  christos  * This code is derived from software contributed to The NetBSD Foundation
      8       1.33      fvdl  * by Christos Zoulas, Frank van der Linden, Eric Haszlakiewicz and
      9       1.33      fvdl  * Thor Lancelot Simon.
     10       1.29  christos  *
     11       1.29  christos  * Redistribution and use in source and binary forms, with or without
     12       1.29  christos  * modification, are permitted provided that the following conditions
     13       1.29  christos  * are met:
     14       1.29  christos  * 1. Redistributions of source code must retain the above copyright
     15       1.29  christos  *    notice, this list of conditions and the following disclaimer.
     16       1.29  christos  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.29  christos  *    notice, this list of conditions and the following disclaimer in the
     18       1.29  christos  *    documentation and/or other materials provided with the distribution.
     19       1.29  christos  *
     20       1.29  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21       1.29  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22       1.29  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23       1.29  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24       1.29  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25       1.29  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26       1.29  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27       1.29  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28       1.29  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29       1.29  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30       1.29  christos  * POSSIBILITY OF SUCH DAMAGE.
     31        1.1      fvdl  */
     32       1.54     lukem 
     33       1.54     lukem #include <sys/cdefs.h>
     34  1.118.2.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: linux_exec.c,v 1.118.2.1 2018/05/21 04:36:03 pgoyette Exp $");
     35        1.1      fvdl 
     36        1.1      fvdl #include <sys/param.h>
     37        1.1      fvdl #include <sys/systm.h>
     38        1.1      fvdl #include <sys/kernel.h>
     39        1.1      fvdl #include <sys/proc.h>
     40        1.1      fvdl #include <sys/namei.h>
     41        1.1      fvdl #include <sys/vnode.h>
     42       1.13  christos #include <sys/mount.h>
     43       1.25  christos #include <sys/exec.h>
     44        1.8      fvdl #include <sys/exec_elf.h>
     45        1.1      fvdl 
     46        1.1      fvdl #include <sys/mman.h>
     47       1.13  christos #include <sys/syscallargs.h>
     48        1.1      fvdl 
     49       1.85      manu #include <sys/ptrace.h>	/* For proc_reparent() */
     50       1.85      manu 
     51       1.73      fvdl #include <uvm/uvm_extern.h>
     52       1.73      fvdl 
     53       1.96        ad #include <sys/cpu.h>
     54        1.1      fvdl #include <machine/reg.h>
     55        1.1      fvdl 
     56       1.32  christos #include <compat/linux/common/linux_types.h>
     57       1.32  christos #include <compat/linux/common/linux_signal.h>
     58       1.32  christos #include <compat/linux/common/linux_util.h>
     59       1.85      manu #include <compat/linux/common/linux_sched.h>
     60       1.85      manu #include <compat/linux/common/linux_machdep.h>
     61       1.32  christos #include <compat/linux/common/linux_exec.h>
     62       1.78      manu #include <compat/linux/common/linux_futex.h>
     63       1.97     njoly #include <compat/linux/common/linux_ipc.h>
     64       1.97     njoly #include <compat/linux/common/linux_sem.h>
     65       1.32  christos 
     66       1.32  christos #include <compat/linux/linux_syscallargs.h>
     67        1.4  christos #include <compat/linux/linux_syscall.h>
     68       1.38  jdolecek #include <compat/linux/common/linux_misc.h>
     69       1.38  jdolecek #include <compat/linux/common/linux_errno.h>
     70       1.39  jdolecek #include <compat/linux/common/linux_emuldata.h>
     71        1.8      fvdl 
     72       1.38  jdolecek extern struct sysent linux_sysent[];
     73       1.38  jdolecek extern const char * const linux_syscallnames[];
     74       1.38  jdolecek extern char linux_sigcode[], linux_esigcode[];
     75        1.1      fvdl 
     76        1.1      fvdl /*
     77       1.38  jdolecek  * Emulation switch.
     78       1.38  jdolecek  */
     79       1.65       chs 
     80       1.65       chs struct uvm_object *emul_linux_object;
     81       1.65       chs 
     82      1.109        ad struct emul emul_linux = {
     83      1.113     rmind 	.e_name =		"linux",
     84      1.113     rmind 	.e_path =		"/emul/linux",
     85       1.44   mycroft #ifndef __HAVE_MINIMAL_EMUL
     86      1.113     rmind 	.e_flags =		0,
     87      1.114       chs 	.e_errno =		native_to_linux_errno,
     88      1.113     rmind 	.e_nosys =		LINUX_SYS_syscall,
     89      1.113     rmind 	.e_nsysent =		LINUX_SYS_NSYSENT,
     90       1.44   mycroft #endif
     91      1.113     rmind 	.e_sysent =		linux_sysent,
     92      1.113     rmind 	.e_syscallnames =	linux_syscallnames,
     93      1.113     rmind 	.e_sendsig =		linux_sendsig,
     94      1.113     rmind 	.e_trapsignal =		linux_trapsignal,
     95      1.113     rmind 	.e_sigcode =		linux_sigcode,
     96      1.113     rmind 	.e_esigcode =		linux_esigcode,
     97      1.113     rmind 	.e_sigobject =		&emul_linux_object,
     98      1.113     rmind 	.e_setregs =		linux_setregs,
     99      1.113     rmind 	.e_proc_exec =		linux_e_proc_exec,
    100      1.113     rmind 	.e_proc_fork =		linux_e_proc_fork,
    101      1.113     rmind 	.e_proc_exit =		linux_e_proc_exit,
    102      1.114       chs 	.e_lwp_fork =		linux_e_lwp_fork,
    103      1.114       chs 	.e_lwp_exit =		linux_e_lwp_exit,
    104       1.44   mycroft #ifdef __HAVE_SYSCALL_INTERN
    105      1.113     rmind 	.e_syscall_intern =	linux_syscall_intern,
    106       1.42  jdolecek #else
    107       1.62   thorpej #error Implement __HAVE_SYSCALL_INTERN for this platform
    108       1.41  jdolecek #endif
    109      1.113     rmind 	.e_sysctlovly =		NULL,
    110      1.113     rmind 	.e_vm_default_addr =	uvm_default_mapaddr,
    111      1.113     rmind 	.e_usertrap =		linux_usertrap,
    112      1.113     rmind 	.e_ucsize =		0,
    113      1.113     rmind 	.e_startlwp =		NULL
    114       1.38  jdolecek };
    115       1.39  jdolecek 
    116      1.114       chs void
    117      1.114       chs linux_e_proc_exec(struct proc *p, struct exec_package *epp)
    118       1.39  jdolecek {
    119      1.114       chs 	struct lwp *l;
    120       1.84      manu 
    121      1.114       chs 	l = LIST_FIRST(&p->p_lwps);
    122      1.114       chs 	if (l->l_emuldata == NULL) {
    123      1.114       chs 		l->l_emuldata = kmem_zalloc(sizeof(struct linux_emuldata), KM_SLEEP);
    124       1.71  jdolecek 	} else {
    125      1.114       chs 		memset(l->l_emuldata, 0, sizeof (struct linux_emuldata));
    126       1.77      manu 	}
    127       1.77      manu 
    128      1.114       chs 	KASSERT(p->p_nlwps == 1);
    129      1.114       chs 	l = LIST_FIRST(&p->p_lwps);
    130      1.114       chs 	mutex_enter(p->p_lock);
    131      1.114       chs 	l->l_lid = p->p_pid;
    132      1.114       chs 	mutex_exit(p->p_lock);
    133       1.48  jdolecek }
    134       1.48  jdolecek 
    135      1.114       chs void
    136      1.100       dsl linux_e_proc_exit(struct proc *p)
    137       1.39  jdolecek {
    138      1.114       chs 	struct lwp *l;
    139       1.71  jdolecek 
    140      1.114       chs 	KASSERT(p->p_nlwps == 1);
    141      1.114       chs 	l = LIST_FIRST(&p->p_lwps);
    142      1.114       chs 	linux_e_lwp_exit(l);
    143       1.39  jdolecek }
    144       1.39  jdolecek 
    145      1.114       chs void
    146      1.114       chs linux_e_proc_fork(struct proc *p2, struct lwp *l1, int flags)
    147       1.39  jdolecek {
    148      1.114       chs 	struct linux_emuldata *led1, *led2;
    149      1.114       chs 	struct lwp *l2;
    150      1.103        ad 
    151      1.114       chs 	KASSERT(p2->p_nlwps == 1);
    152      1.114       chs 	l2 = LIST_FIRST(&p2->p_lwps);
    153      1.114       chs 	l2->l_lid = p2->p_pid;
    154      1.114       chs 	led1 = l1->l_emuldata;
    155      1.114       chs 	led2 = l2->l_emuldata;
    156      1.114       chs 	led2->led_child_tidptr = led1->led_child_tidptr;
    157       1.77      manu }
    158       1.77      manu 
    159       1.85      manu void
    160      1.114       chs linux_e_lwp_fork(struct lwp *l1, struct lwp *l2)
    161       1.77      manu {
    162      1.114       chs 	struct linux_emuldata *led2;
    163       1.77      manu 
    164      1.115  christos 	led2 = kmem_zalloc(sizeof(*led2), KM_SLEEP);
    165      1.114       chs 	l2->l_emuldata = led2;
    166       1.39  jdolecek }
    167       1.85      manu 
    168       1.85      manu void
    169      1.114       chs linux_e_lwp_exit(struct lwp *l)
    170       1.85      manu {
    171      1.114       chs 	struct linux_emuldata *led;
    172      1.114       chs 	struct linux_sys_futex_args cup;
    173      1.114       chs 	register_t retval;
    174      1.114       chs 	int error, zero = 0;
    175      1.114       chs 
    176      1.114       chs 	led = l->l_emuldata;
    177      1.114       chs 	if (led->led_clear_tid == NULL) {
    178      1.114       chs 		return;
    179       1.85      manu 	}
    180       1.85      manu 
    181       1.85      manu 	/* Emulate LINUX_CLONE_CHILD_CLEARTID */
    182      1.114       chs 	error = copyout(&zero, led->led_clear_tid, sizeof(zero));
    183       1.85      manu #ifdef DEBUG_LINUX
    184      1.114       chs 	if (error != 0)
    185      1.114       chs 		printf("%s: cannot clear TID\n", __func__);
    186       1.85      manu #endif
    187       1.85      manu 
    188      1.114       chs 	SCARG(&cup, uaddr) = led->led_clear_tid;
    189      1.114       chs 	SCARG(&cup, op) = LINUX_FUTEX_WAKE;
    190      1.114       chs 	SCARG(&cup, val) = 0x7fffffff; /* Awake everyone */
    191      1.114       chs 	SCARG(&cup, timeout) = NULL;
    192      1.114       chs 	SCARG(&cup, uaddr2) = NULL;
    193      1.114       chs 	SCARG(&cup, val3) = 0;
    194      1.114       chs 	if ((error = linux_sys_futex(curlwp, &cup, &retval)) != 0)
    195      1.114       chs 		printf("%s: linux_sys_futex failed\n", __func__);
    196      1.114       chs 
    197      1.114       chs 	release_futexes(l);
    198      1.114       chs 
    199      1.114       chs 	led = l->l_emuldata;
    200      1.114       chs 	l->l_emuldata = NULL;
    201      1.115  christos 	kmem_free(led, sizeof(*led));
    202       1.85      manu }
    203