linux_exec.c revision 1.121 1 1.121 ad /* $NetBSD: linux_exec.c,v 1.121 2020/02/15 17:13:55 ad Exp $ */
2 1.29 christos
3 1.29 christos /*-
4 1.121 ad * Copyright (c) 1994, 1995, 1998, 2000, 2007, 2008, 2020
5 1.121 ad * The NetBSD Foundation, Inc.
6 1.29 christos * All rights reserved.
7 1.29 christos *
8 1.29 christos * This code is derived from software contributed to The NetBSD Foundation
9 1.33 fvdl * by Christos Zoulas, Frank van der Linden, Eric Haszlakiewicz and
10 1.33 fvdl * Thor Lancelot Simon.
11 1.29 christos *
12 1.29 christos * Redistribution and use in source and binary forms, with or without
13 1.29 christos * modification, are permitted provided that the following conditions
14 1.29 christos * are met:
15 1.29 christos * 1. Redistributions of source code must retain the above copyright
16 1.29 christos * notice, this list of conditions and the following disclaimer.
17 1.29 christos * 2. Redistributions in binary form must reproduce the above copyright
18 1.29 christos * notice, this list of conditions and the following disclaimer in the
19 1.29 christos * documentation and/or other materials provided with the distribution.
20 1.29 christos *
21 1.29 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 1.29 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 1.29 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.29 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 1.29 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.29 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.29 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.29 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.29 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.29 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.29 christos * POSSIBILITY OF SUCH DAMAGE.
32 1.1 fvdl */
33 1.54 lukem
34 1.54 lukem #include <sys/cdefs.h>
35 1.121 ad __KERNEL_RCSID(0, "$NetBSD: linux_exec.c,v 1.121 2020/02/15 17:13:55 ad Exp $");
36 1.1 fvdl
37 1.1 fvdl #include <sys/param.h>
38 1.1 fvdl #include <sys/systm.h>
39 1.1 fvdl #include <sys/kernel.h>
40 1.121 ad #include <sys/lwp.h>
41 1.1 fvdl #include <sys/proc.h>
42 1.1 fvdl #include <sys/namei.h>
43 1.1 fvdl #include <sys/vnode.h>
44 1.13 christos #include <sys/mount.h>
45 1.25 christos #include <sys/exec.h>
46 1.8 fvdl #include <sys/exec_elf.h>
47 1.1 fvdl
48 1.1 fvdl #include <sys/mman.h>
49 1.13 christos #include <sys/syscallargs.h>
50 1.1 fvdl
51 1.85 manu #include <sys/ptrace.h> /* For proc_reparent() */
52 1.85 manu
53 1.73 fvdl #include <uvm/uvm_extern.h>
54 1.73 fvdl
55 1.96 ad #include <sys/cpu.h>
56 1.1 fvdl #include <machine/reg.h>
57 1.1 fvdl
58 1.32 christos #include <compat/linux/common/linux_types.h>
59 1.32 christos #include <compat/linux/common/linux_signal.h>
60 1.32 christos #include <compat/linux/common/linux_util.h>
61 1.85 manu #include <compat/linux/common/linux_sched.h>
62 1.85 manu #include <compat/linux/common/linux_machdep.h>
63 1.32 christos #include <compat/linux/common/linux_exec.h>
64 1.78 manu #include <compat/linux/common/linux_futex.h>
65 1.97 njoly #include <compat/linux/common/linux_ipc.h>
66 1.97 njoly #include <compat/linux/common/linux_sem.h>
67 1.32 christos
68 1.32 christos #include <compat/linux/linux_syscallargs.h>
69 1.4 christos #include <compat/linux/linux_syscall.h>
70 1.38 jdolecek #include <compat/linux/common/linux_misc.h>
71 1.38 jdolecek #include <compat/linux/common/linux_errno.h>
72 1.39 jdolecek #include <compat/linux/common/linux_emuldata.h>
73 1.8 fvdl
74 1.38 jdolecek extern struct sysent linux_sysent[];
75 1.120 pgoyette extern const uint32_t linux_sysent_nomodbits[];
76 1.38 jdolecek extern const char * const linux_syscallnames[];
77 1.38 jdolecek extern char linux_sigcode[], linux_esigcode[];
78 1.1 fvdl
79 1.1 fvdl /*
80 1.38 jdolecek * Emulation switch.
81 1.38 jdolecek */
82 1.65 chs
83 1.65 chs struct uvm_object *emul_linux_object;
84 1.65 chs
85 1.109 ad struct emul emul_linux = {
86 1.113 rmind .e_name = "linux",
87 1.113 rmind .e_path = "/emul/linux",
88 1.44 mycroft #ifndef __HAVE_MINIMAL_EMUL
89 1.113 rmind .e_flags = 0,
90 1.114 chs .e_errno = native_to_linux_errno,
91 1.113 rmind .e_nosys = LINUX_SYS_syscall,
92 1.113 rmind .e_nsysent = LINUX_SYS_NSYSENT,
93 1.44 mycroft #endif
94 1.113 rmind .e_sysent = linux_sysent,
95 1.120 pgoyette .e_nomodbits = linux_sysent_nomodbits,
96 1.113 rmind .e_syscallnames = linux_syscallnames,
97 1.113 rmind .e_sendsig = linux_sendsig,
98 1.113 rmind .e_trapsignal = linux_trapsignal,
99 1.113 rmind .e_sigcode = linux_sigcode,
100 1.113 rmind .e_esigcode = linux_esigcode,
101 1.113 rmind .e_sigobject = &emul_linux_object,
102 1.113 rmind .e_setregs = linux_setregs,
103 1.113 rmind .e_proc_exec = linux_e_proc_exec,
104 1.113 rmind .e_proc_fork = linux_e_proc_fork,
105 1.113 rmind .e_proc_exit = linux_e_proc_exit,
106 1.114 chs .e_lwp_fork = linux_e_lwp_fork,
107 1.114 chs .e_lwp_exit = linux_e_lwp_exit,
108 1.44 mycroft #ifdef __HAVE_SYSCALL_INTERN
109 1.113 rmind .e_syscall_intern = linux_syscall_intern,
110 1.42 jdolecek #else
111 1.62 thorpej #error Implement __HAVE_SYSCALL_INTERN for this platform
112 1.41 jdolecek #endif
113 1.113 rmind .e_sysctlovly = NULL,
114 1.113 rmind .e_vm_default_addr = uvm_default_mapaddr,
115 1.113 rmind .e_usertrap = linux_usertrap,
116 1.113 rmind .e_ucsize = 0,
117 1.113 rmind .e_startlwp = NULL
118 1.38 jdolecek };
119 1.39 jdolecek
120 1.114 chs void
121 1.114 chs linux_e_proc_exec(struct proc *p, struct exec_package *epp)
122 1.39 jdolecek {
123 1.114 chs struct lwp *l;
124 1.84 manu
125 1.114 chs l = LIST_FIRST(&p->p_lwps);
126 1.114 chs if (l->l_emuldata == NULL) {
127 1.114 chs l->l_emuldata = kmem_zalloc(sizeof(struct linux_emuldata), KM_SLEEP);
128 1.71 jdolecek } else {
129 1.114 chs memset(l->l_emuldata, 0, sizeof (struct linux_emuldata));
130 1.77 manu }
131 1.77 manu
132 1.114 chs KASSERT(p->p_nlwps == 1);
133 1.114 chs l = LIST_FIRST(&p->p_lwps);
134 1.121 ad lwp_renumber(l, p->p_pid);
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.121 ad lwp_renumber(l2, 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