Home | History | Annotate | Line # | Download | only in common
linux_exec.c revision 1.121
      1 /*	$NetBSD: linux_exec.c,v 1.121 2020/02/15 17:13:55 ad 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.121 2020/02/15 17:13:55 ad 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 
     48 #include <sys/mman.h>
     49 #include <sys/syscallargs.h>
     50 
     51 #include <sys/ptrace.h>	/* For proc_reparent() */
     52 
     53 #include <uvm/uvm_extern.h>
     54 
     55 #include <sys/cpu.h>
     56 #include <machine/reg.h>
     57 
     58 #include <compat/linux/common/linux_types.h>
     59 #include <compat/linux/common/linux_signal.h>
     60 #include <compat/linux/common/linux_util.h>
     61 #include <compat/linux/common/linux_sched.h>
     62 #include <compat/linux/common/linux_machdep.h>
     63 #include <compat/linux/common/linux_exec.h>
     64 #include <compat/linux/common/linux_futex.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 	l = LIST_FIRST(&p->p_lwps);
    134 	lwp_renumber(l, p->p_pid);
    135 }
    136 
    137 void
    138 linux_e_proc_exit(struct proc *p)
    139 {
    140 	struct lwp *l;
    141 
    142 	KASSERT(p->p_nlwps == 1);
    143 	l = LIST_FIRST(&p->p_lwps);
    144 	linux_e_lwp_exit(l);
    145 }
    146 
    147 void
    148 linux_e_proc_fork(struct proc *p2, struct lwp *l1, int flags)
    149 {
    150 	struct linux_emuldata *led1, *led2;
    151 	struct lwp *l2;
    152 
    153 	KASSERT(p2->p_nlwps == 1);
    154 	l2 = LIST_FIRST(&p2->p_lwps);
    155 	lwp_renumber(l2, p2->p_pid);
    156 	led1 = l1->l_emuldata;
    157 	led2 = l2->l_emuldata;
    158 	led2->led_child_tidptr = led1->led_child_tidptr;
    159 }
    160 
    161 void
    162 linux_e_lwp_fork(struct lwp *l1, struct lwp *l2)
    163 {
    164 	struct linux_emuldata *led2;
    165 
    166 	led2 = kmem_zalloc(sizeof(*led2), KM_SLEEP);
    167 	l2->l_emuldata = led2;
    168 }
    169 
    170 void
    171 linux_e_lwp_exit(struct lwp *l)
    172 {
    173 	struct linux_emuldata *led;
    174 	struct linux_sys_futex_args cup;
    175 	register_t retval;
    176 	int error, zero = 0;
    177 
    178 	led = l->l_emuldata;
    179 	if (led->led_clear_tid == NULL) {
    180 		return;
    181 	}
    182 
    183 	/* Emulate LINUX_CLONE_CHILD_CLEARTID */
    184 	error = copyout(&zero, led->led_clear_tid, sizeof(zero));
    185 #ifdef DEBUG_LINUX
    186 	if (error != 0)
    187 		printf("%s: cannot clear TID\n", __func__);
    188 #endif
    189 
    190 	SCARG(&cup, uaddr) = led->led_clear_tid;
    191 	SCARG(&cup, op) = LINUX_FUTEX_WAKE;
    192 	SCARG(&cup, val) = 0x7fffffff; /* Awake everyone */
    193 	SCARG(&cup, timeout) = NULL;
    194 	SCARG(&cup, uaddr2) = NULL;
    195 	SCARG(&cup, val3) = 0;
    196 	if ((error = linux_sys_futex(curlwp, &cup, &retval)) != 0)
    197 		printf("%s: linux_sys_futex failed\n", __func__);
    198 
    199 	release_futexes(l);
    200 
    201 	led = l->l_emuldata;
    202 	l->l_emuldata = NULL;
    203 	kmem_free(led, sizeof(*led));
    204 }
    205