Home | History | Annotate | Line # | Download | only in common
linux_exec.c revision 1.119.2.1
      1  1.119.2.1  christos /*	$NetBSD: linux_exec.c,v 1.119.2.1 2019/06/10 22:07:00 christos 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.119.2.1  christos __KERNEL_RCSID(0, "$NetBSD: linux_exec.c,v 1.119.2.1 2019/06/10 22:07:00 christos 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.119.2.1  christos extern const uint32_t linux_sysent_nomodbits[];
     74       1.38  jdolecek extern const char * const linux_syscallnames[];
     75       1.38  jdolecek extern char linux_sigcode[], linux_esigcode[];
     76        1.1      fvdl 
     77        1.1      fvdl /*
     78       1.38  jdolecek  * Emulation switch.
     79       1.38  jdolecek  */
     80       1.65       chs 
     81       1.65       chs struct uvm_object *emul_linux_object;
     82       1.65       chs 
     83      1.109        ad struct emul emul_linux = {
     84      1.113     rmind 	.e_name =		"linux",
     85      1.113     rmind 	.e_path =		"/emul/linux",
     86       1.44   mycroft #ifndef __HAVE_MINIMAL_EMUL
     87      1.113     rmind 	.e_flags =		0,
     88      1.114       chs 	.e_errno =		native_to_linux_errno,
     89      1.113     rmind 	.e_nosys =		LINUX_SYS_syscall,
     90      1.113     rmind 	.e_nsysent =		LINUX_SYS_NSYSENT,
     91       1.44   mycroft #endif
     92      1.113     rmind 	.e_sysent =		linux_sysent,
     93  1.119.2.1  christos 	.e_nomodbits =		linux_sysent_nomodbits,
     94      1.113     rmind 	.e_syscallnames =	linux_syscallnames,
     95      1.113     rmind 	.e_sendsig =		linux_sendsig,
     96      1.113     rmind 	.e_trapsignal =		linux_trapsignal,
     97      1.113     rmind 	.e_sigcode =		linux_sigcode,
     98      1.113     rmind 	.e_esigcode =		linux_esigcode,
     99      1.113     rmind 	.e_sigobject =		&emul_linux_object,
    100      1.113     rmind 	.e_setregs =		linux_setregs,
    101      1.113     rmind 	.e_proc_exec =		linux_e_proc_exec,
    102      1.113     rmind 	.e_proc_fork =		linux_e_proc_fork,
    103      1.113     rmind 	.e_proc_exit =		linux_e_proc_exit,
    104      1.114       chs 	.e_lwp_fork =		linux_e_lwp_fork,
    105      1.114       chs 	.e_lwp_exit =		linux_e_lwp_exit,
    106       1.44   mycroft #ifdef __HAVE_SYSCALL_INTERN
    107      1.113     rmind 	.e_syscall_intern =	linux_syscall_intern,
    108       1.42  jdolecek #else
    109       1.62   thorpej #error Implement __HAVE_SYSCALL_INTERN for this platform
    110       1.41  jdolecek #endif
    111      1.113     rmind 	.e_sysctlovly =		NULL,
    112      1.113     rmind 	.e_vm_default_addr =	uvm_default_mapaddr,
    113      1.113     rmind 	.e_usertrap =		linux_usertrap,
    114      1.113     rmind 	.e_ucsize =		0,
    115      1.113     rmind 	.e_startlwp =		NULL
    116       1.38  jdolecek };
    117       1.39  jdolecek 
    118      1.114       chs void
    119      1.114       chs linux_e_proc_exec(struct proc *p, struct exec_package *epp)
    120       1.39  jdolecek {
    121      1.114       chs 	struct lwp *l;
    122       1.84      manu 
    123      1.114       chs 	l = LIST_FIRST(&p->p_lwps);
    124      1.114       chs 	if (l->l_emuldata == NULL) {
    125      1.114       chs 		l->l_emuldata = kmem_zalloc(sizeof(struct linux_emuldata), KM_SLEEP);
    126       1.71  jdolecek 	} else {
    127      1.114       chs 		memset(l->l_emuldata, 0, sizeof (struct linux_emuldata));
    128       1.77      manu 	}
    129       1.77      manu 
    130      1.114       chs 	KASSERT(p->p_nlwps == 1);
    131      1.114       chs 	l = LIST_FIRST(&p->p_lwps);
    132      1.114       chs 	mutex_enter(p->p_lock);
    133      1.114       chs 	l->l_lid = p->p_pid;
    134      1.114       chs 	mutex_exit(p->p_lock);
    135       1.48  jdolecek }
    136       1.48  jdolecek 
    137      1.114       chs void
    138      1.100       dsl linux_e_proc_exit(struct proc *p)
    139       1.39  jdolecek {
    140      1.114       chs 	struct lwp *l;
    141       1.71  jdolecek 
    142      1.114       chs 	KASSERT(p->p_nlwps == 1);
    143      1.114       chs 	l = LIST_FIRST(&p->p_lwps);
    144      1.114       chs 	linux_e_lwp_exit(l);
    145       1.39  jdolecek }
    146       1.39  jdolecek 
    147      1.114       chs void
    148      1.114       chs linux_e_proc_fork(struct proc *p2, struct lwp *l1, int flags)
    149       1.39  jdolecek {
    150      1.114       chs 	struct linux_emuldata *led1, *led2;
    151      1.114       chs 	struct lwp *l2;
    152      1.103        ad 
    153      1.114       chs 	KASSERT(p2->p_nlwps == 1);
    154      1.114       chs 	l2 = LIST_FIRST(&p2->p_lwps);
    155      1.114       chs 	l2->l_lid = p2->p_pid;
    156      1.114       chs 	led1 = l1->l_emuldata;
    157      1.114       chs 	led2 = l2->l_emuldata;
    158      1.114       chs 	led2->led_child_tidptr = led1->led_child_tidptr;
    159       1.77      manu }
    160       1.77      manu 
    161       1.85      manu void
    162      1.114       chs linux_e_lwp_fork(struct lwp *l1, struct lwp *l2)
    163       1.77      manu {
    164      1.114       chs 	struct linux_emuldata *led2;
    165       1.77      manu 
    166      1.115  christos 	led2 = kmem_zalloc(sizeof(*led2), KM_SLEEP);
    167      1.114       chs 	l2->l_emuldata = led2;
    168       1.39  jdolecek }
    169       1.85      manu 
    170       1.85      manu void
    171      1.114       chs linux_e_lwp_exit(struct lwp *l)
    172       1.85      manu {
    173      1.114       chs 	struct linux_emuldata *led;
    174      1.114       chs 	struct linux_sys_futex_args cup;
    175      1.114       chs 	register_t retval;
    176      1.114       chs 	int error, zero = 0;
    177      1.114       chs 
    178      1.114       chs 	led = l->l_emuldata;
    179      1.114       chs 	if (led->led_clear_tid == NULL) {
    180      1.114       chs 		return;
    181       1.85      manu 	}
    182       1.85      manu 
    183       1.85      manu 	/* Emulate LINUX_CLONE_CHILD_CLEARTID */
    184      1.114       chs 	error = copyout(&zero, led->led_clear_tid, sizeof(zero));
    185       1.85      manu #ifdef DEBUG_LINUX
    186      1.114       chs 	if (error != 0)
    187      1.114       chs 		printf("%s: cannot clear TID\n", __func__);
    188       1.85      manu #endif
    189       1.85      manu 
    190      1.114       chs 	SCARG(&cup, uaddr) = led->led_clear_tid;
    191      1.114       chs 	SCARG(&cup, op) = LINUX_FUTEX_WAKE;
    192      1.114       chs 	SCARG(&cup, val) = 0x7fffffff; /* Awake everyone */
    193      1.114       chs 	SCARG(&cup, timeout) = NULL;
    194      1.114       chs 	SCARG(&cup, uaddr2) = NULL;
    195      1.114       chs 	SCARG(&cup, val3) = 0;
    196      1.114       chs 	if ((error = linux_sys_futex(curlwp, &cup, &retval)) != 0)
    197      1.114       chs 		printf("%s: linux_sys_futex failed\n", __func__);
    198      1.114       chs 
    199      1.114       chs 	release_futexes(l);
    200      1.114       chs 
    201      1.114       chs 	led = l->l_emuldata;
    202      1.114       chs 	l->l_emuldata = NULL;
    203      1.115  christos 	kmem_free(led, sizeof(*led));
    204       1.85      manu }
    205