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