linux_exec.c revision 1.78 1 1.78 manu /* $NetBSD: linux_exec.c,v 1.78 2005/11/04 16:54:11 manu Exp $ */
2 1.29 christos
3 1.29 christos /*-
4 1.44 mycroft * Copyright (c) 1994, 1995, 1998, 2000 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 * 3. All advertising materials mentioning features or use of this software
20 1.29 christos * must display the following acknowledgement:
21 1.29 christos * This product includes software developed by the NetBSD
22 1.29 christos * Foundation, Inc. and its contributors.
23 1.29 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.29 christos * contributors may be used to endorse or promote products derived
25 1.29 christos * from this software without specific prior written permission.
26 1.29 christos *
27 1.29 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.29 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.29 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.29 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.29 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.29 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.29 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.29 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.29 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.29 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.29 christos * POSSIBILITY OF SUCH DAMAGE.
38 1.1 fvdl */
39 1.54 lukem
40 1.54 lukem #include <sys/cdefs.h>
41 1.78 manu __KERNEL_RCSID(0, "$NetBSD: linux_exec.c,v 1.78 2005/11/04 16:54:11 manu Exp $");
42 1.1 fvdl
43 1.1 fvdl #include <sys/param.h>
44 1.1 fvdl #include <sys/systm.h>
45 1.1 fvdl #include <sys/kernel.h>
46 1.1 fvdl #include <sys/proc.h>
47 1.1 fvdl #include <sys/malloc.h>
48 1.1 fvdl #include <sys/namei.h>
49 1.1 fvdl #include <sys/vnode.h>
50 1.13 christos #include <sys/mount.h>
51 1.25 christos #include <sys/exec.h>
52 1.8 fvdl #include <sys/exec_elf.h>
53 1.1 fvdl
54 1.1 fvdl #include <sys/mman.h>
55 1.61 thorpej #include <sys/sa.h>
56 1.13 christos #include <sys/syscallargs.h>
57 1.1 fvdl
58 1.73 fvdl #include <uvm/uvm_extern.h>
59 1.73 fvdl
60 1.1 fvdl #include <machine/cpu.h>
61 1.1 fvdl #include <machine/reg.h>
62 1.1 fvdl
63 1.32 christos #include <compat/linux/common/linux_types.h>
64 1.32 christos #include <compat/linux/common/linux_signal.h>
65 1.32 christos #include <compat/linux/common/linux_util.h>
66 1.32 christos #include <compat/linux/common/linux_exec.h>
67 1.32 christos #include <compat/linux/common/linux_machdep.h>
68 1.78 manu #include <compat/linux/common/linux_futex.h>
69 1.32 christos
70 1.32 christos #include <compat/linux/linux_syscallargs.h>
71 1.4 christos #include <compat/linux/linux_syscall.h>
72 1.38 jdolecek #include <compat/linux/common/linux_misc.h>
73 1.38 jdolecek #include <compat/linux/common/linux_errno.h>
74 1.39 jdolecek #include <compat/linux/common/linux_emuldata.h>
75 1.8 fvdl
76 1.38 jdolecek extern struct sysent linux_sysent[];
77 1.38 jdolecek extern const char * const linux_syscallnames[];
78 1.38 jdolecek extern char linux_sigcode[], linux_esigcode[];
79 1.1 fvdl
80 1.39 jdolecek static void linux_e_proc_exec __P((struct proc *, struct exec_package *));
81 1.71 jdolecek static void linux_e_proc_fork __P((struct proc *, struct proc *, int));
82 1.39 jdolecek static void linux_e_proc_exit __P((struct proc *));
83 1.71 jdolecek static void linux_e_proc_init __P((struct proc *, struct proc *, int));
84 1.39 jdolecek
85 1.77 manu static void linux_userret_settid __P((struct lwp *, void *));
86 1.77 manu
87 1.1 fvdl /*
88 1.1 fvdl * Execve(2). Just check the alternate emulation path, and pass it on
89 1.1 fvdl * to the NetBSD execve().
90 1.1 fvdl */
91 1.1 fvdl int
92 1.61 thorpej linux_sys_execve(l, v, retval)
93 1.61 thorpej struct lwp *l;
94 1.11 thorpej void *v;
95 1.11 thorpej register_t *retval;
96 1.11 thorpej {
97 1.12 mycroft struct linux_sys_execve_args /* {
98 1.35 christos syscallarg(const char *) path;
99 1.1 fvdl syscallarg(char **) argv;
100 1.1 fvdl syscallarg(char **) envp;
101 1.11 thorpej } */ *uap = v;
102 1.61 thorpej struct proc *p = l->l_proc;
103 1.16 mycroft struct sys_execve_args ap;
104 1.1 fvdl caddr_t sg;
105 1.1 fvdl
106 1.57 christos sg = stackgap_init(p, 0);
107 1.64 fvdl CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
108 1.1 fvdl
109 1.16 mycroft SCARG(&ap, path) = SCARG(uap, path);
110 1.16 mycroft SCARG(&ap, argp) = SCARG(uap, argp);
111 1.16 mycroft SCARG(&ap, envp) = SCARG(uap, envp);
112 1.16 mycroft
113 1.61 thorpej return sys_execve(l, &ap, retval);
114 1.1 fvdl }
115 1.38 jdolecek
116 1.38 jdolecek /*
117 1.38 jdolecek * Emulation switch.
118 1.38 jdolecek */
119 1.65 chs
120 1.65 chs struct uvm_object *emul_linux_object;
121 1.65 chs
122 1.38 jdolecek const struct emul emul_linux = {
123 1.38 jdolecek "linux",
124 1.40 jdolecek "/emul/linux",
125 1.44 mycroft #ifndef __HAVE_MINIMAL_EMUL
126 1.51 manu 0,
127 1.76 tsutsui (const int *)native_to_linux_errno,
128 1.38 jdolecek LINUX_SYS_syscall,
129 1.60 jdolecek LINUX_SYS_NSYSENT,
130 1.44 mycroft #endif
131 1.38 jdolecek linux_sysent,
132 1.38 jdolecek linux_syscallnames,
133 1.44 mycroft linux_sendsig,
134 1.52 christos linux_trapsignal,
135 1.70 fvdl NULL,
136 1.38 jdolecek linux_sigcode,
137 1.38 jdolecek linux_esigcode,
138 1.65 chs &emul_linux_object,
139 1.53 jdolecek linux_setregs,
140 1.39 jdolecek linux_e_proc_exec,
141 1.39 jdolecek linux_e_proc_fork,
142 1.39 jdolecek linux_e_proc_exit,
143 1.69 manu NULL,
144 1.69 manu NULL,
145 1.44 mycroft #ifdef __HAVE_SYSCALL_INTERN
146 1.44 mycroft linux_syscall_intern,
147 1.42 jdolecek #else
148 1.62 thorpej #error Implement __HAVE_SYSCALL_INTERN for this platform
149 1.41 jdolecek #endif
150 1.67 atatat NULL,
151 1.59 manu NULL,
152 1.73 fvdl
153 1.74 christos uvm_default_mapaddr,
154 1.75 fvdl
155 1.75 fvdl linux_usertrap,
156 1.38 jdolecek };
157 1.39 jdolecek
158 1.39 jdolecek static void
159 1.71 jdolecek linux_e_proc_init(p, parent, forkflags)
160 1.71 jdolecek struct proc *p, *parent;
161 1.71 jdolecek int forkflags;
162 1.39 jdolecek {
163 1.71 jdolecek struct linux_emuldata *e = p->p_emuldata;
164 1.71 jdolecek struct linux_emuldata_shared *s;
165 1.77 manu struct linux_emuldata *ep = NULL;
166 1.71 jdolecek
167 1.71 jdolecek if (!e) {
168 1.39 jdolecek /* allocate new Linux emuldata */
169 1.71 jdolecek MALLOC(e, void *, sizeof(struct linux_emuldata),
170 1.71 jdolecek M_EMULDATA, M_WAITOK);
171 1.71 jdolecek } else {
172 1.72 jdolecek e->s->refs--;
173 1.71 jdolecek if (e->s->refs == 0)
174 1.71 jdolecek FREE(e->s, M_EMULDATA);
175 1.71 jdolecek }
176 1.71 jdolecek
177 1.71 jdolecek memset(e, '\0', sizeof(struct linux_emuldata));
178 1.71 jdolecek
179 1.77 manu if (parent)
180 1.77 manu ep = parent->p_emuldata;
181 1.77 manu
182 1.72 jdolecek if (forkflags & FORK_SHAREVM) {
183 1.77 manu #ifdef DIAGNOSTIC
184 1.77 manu if (ep == NULL) {
185 1.77 manu killproc(p, "FORK_SHAREVM while emuldata is NULL\n");
186 1.77 manu return;
187 1.77 manu }
188 1.77 manu #endif
189 1.77 manu s = ep->s;
190 1.71 jdolecek s->refs++;
191 1.71 jdolecek } else {
192 1.71 jdolecek struct vmspace *vm;
193 1.71 jdolecek
194 1.71 jdolecek MALLOC(s, void *, sizeof(struct linux_emuldata_shared),
195 1.39 jdolecek M_EMULDATA, M_WAITOK);
196 1.71 jdolecek s->refs = 1;
197 1.71 jdolecek
198 1.71 jdolecek /*
199 1.71 jdolecek * Set the process idea of the break to the real value.
200 1.71 jdolecek * For fork, we use parent's vmspace since our's
201 1.71 jdolecek * is not setup at the time of this call and is going
202 1.71 jdolecek * to be copy of parent's anyway. For exec, just
203 1.71 jdolecek * use our own vmspace.
204 1.71 jdolecek */
205 1.71 jdolecek vm = (parent) ? parent->p_vmspace : p->p_vmspace;
206 1.71 jdolecek s->p_break = vm->vm_daddr + ctob(vm->vm_dsize);
207 1.71 jdolecek
208 1.77 manu /*
209 1.77 manu * Linux threads are emulated as NetBSD processes (not lwp)
210 1.77 manu * We use native PID for Linux TID. The Linux TID is the
211 1.77 manu * PID of the first process in the group. It is stored
212 1.77 manu * here
213 1.77 manu */
214 1.77 manu s->group_pid = p->p_pid;
215 1.39 jdolecek }
216 1.39 jdolecek
217 1.71 jdolecek e->s = s;
218 1.77 manu
219 1.77 manu /*
220 1.77 manu * initialize TID pointers. ep->child_clear_tid and
221 1.77 manu * ep->child_set_tid will not be used beyond this point.
222 1.77 manu */
223 1.77 manu e->child_clear_tid = NULL;
224 1.77 manu e->child_set_tid = NULL;
225 1.77 manu if (ep != NULL) {
226 1.77 manu e->clear_tid = ep->child_clear_tid;
227 1.77 manu e->set_tid = ep->child_set_tid;
228 1.77 manu ep->child_clear_tid = NULL;
229 1.77 manu ep->child_set_tid = NULL;
230 1.77 manu } else {
231 1.77 manu e->clear_tid = NULL;
232 1.77 manu e->set_tid = NULL;
233 1.77 manu }
234 1.77 manu
235 1.71 jdolecek p->p_emuldata = e;
236 1.48 jdolecek }
237 1.48 jdolecek
238 1.48 jdolecek /*
239 1.71 jdolecek * Allocate new per-process structures. Called when executing Linux
240 1.48 jdolecek * process. We can reuse the old emuldata - if it's not null,
241 1.48 jdolecek * the executed process is of same emulation as original forked one.
242 1.48 jdolecek */
243 1.48 jdolecek static void
244 1.48 jdolecek linux_e_proc_exec(p, epp)
245 1.48 jdolecek struct proc *p;
246 1.48 jdolecek struct exec_package *epp;
247 1.48 jdolecek {
248 1.48 jdolecek /* exec, use our vmspace */
249 1.71 jdolecek linux_e_proc_init(p, NULL, 0);
250 1.39 jdolecek }
251 1.39 jdolecek
252 1.39 jdolecek /*
253 1.39 jdolecek * Emulation per-process exit hook.
254 1.39 jdolecek */
255 1.39 jdolecek static void
256 1.39 jdolecek linux_e_proc_exit(p)
257 1.39 jdolecek struct proc *p;
258 1.39 jdolecek {
259 1.71 jdolecek struct linux_emuldata *e = p->p_emuldata;
260 1.71 jdolecek
261 1.77 manu /* Emulate LINUX_CLONE_CHILD_CLEARTID */
262 1.77 manu if (e->clear_tid != NULL) {
263 1.77 manu int error;
264 1.77 manu int null = 0;
265 1.78 manu struct linux_sys_futex_args cup;
266 1.78 manu register_t retval;
267 1.78 manu struct lwp *l;
268 1.77 manu
269 1.77 manu if ((error = copyout(&null,
270 1.77 manu e->clear_tid,
271 1.77 manu sizeof(null))) != 0)
272 1.77 manu printf("linux_e_proc_exit: cannot clear TID\n");
273 1.77 manu
274 1.78 manu l = proc_representative_lwp(p);
275 1.78 manu SCARG(&cup, uaddr) = e->clear_tid;
276 1.78 manu SCARG(&cup, op) = LINUX_FUTEX_WAKE;
277 1.78 manu SCARG(&cup, val) = 0;
278 1.78 manu SCARG(&cup, timeout) = NULL;
279 1.78 manu SCARG(&cup, uaddr2) = NULL;
280 1.78 manu SCARG(&cup, val3) = 0;
281 1.78 manu if ((error = linux_sys_futex(l, &cup, &retval)) != 0)
282 1.77 manu printf("linux_e_proc_exit: linux_sys_futex failed\n");
283 1.77 manu }
284 1.77 manu
285 1.39 jdolecek /* free Linux emuldata and set the pointer to null */
286 1.71 jdolecek e->s->refs--;
287 1.71 jdolecek if (e->s->refs == 0)
288 1.71 jdolecek FREE(e->s, M_EMULDATA);
289 1.71 jdolecek FREE(e, M_EMULDATA);
290 1.39 jdolecek p->p_emuldata = NULL;
291 1.39 jdolecek }
292 1.39 jdolecek
293 1.39 jdolecek /*
294 1.39 jdolecek * Emulation fork hook.
295 1.39 jdolecek */
296 1.39 jdolecek static void
297 1.71 jdolecek linux_e_proc_fork(p, parent, forkflags)
298 1.39 jdolecek struct proc *p, *parent;
299 1.71 jdolecek int forkflags;
300 1.39 jdolecek {
301 1.77 manu struct linux_emuldata *e;
302 1.77 manu
303 1.39 jdolecek /*
304 1.71 jdolecek * The new process might share some vmspace-related stuff
305 1.71 jdolecek * with parent, depending on fork flags (CLONE_VM et.al).
306 1.71 jdolecek * Force allocation of new base emuldata, and share the
307 1.71 jdolecek * VM-related parts only if necessary.
308 1.39 jdolecek */
309 1.39 jdolecek p->p_emuldata = NULL;
310 1.71 jdolecek linux_e_proc_init(p, parent, forkflags);
311 1.77 manu
312 1.77 manu /*
313 1.77 manu * Emulate LINUX_CLONE_CHILD_SETTID: This cannot be done
314 1.77 manu * right now because the child VM is not set up. We will
315 1.77 manu * do it at userret time.
316 1.77 manu */
317 1.77 manu e = p->p_emuldata;
318 1.77 manu if (e->set_tid != NULL)
319 1.77 manu p->p_userret = (*linux_userret_settid);
320 1.77 manu
321 1.77 manu return;
322 1.77 manu }
323 1.77 manu
324 1.77 manu static void
325 1.77 manu linux_userret_settid(l, arg)
326 1.77 manu struct lwp *l;
327 1.77 manu void *arg;
328 1.77 manu {
329 1.77 manu struct proc *p = l->l_proc;
330 1.77 manu struct linux_emuldata *led = p->p_emuldata;
331 1.77 manu int error;
332 1.77 manu
333 1.77 manu p->p_userret = NULL;
334 1.77 manu
335 1.77 manu /* Emulate LINUX_CLONE_CHILD_SETTID */
336 1.77 manu if (led->set_tid != NULL) {
337 1.77 manu if ((error = copyout(&p->p_pid,
338 1.77 manu led->set_tid, sizeof(p->p_pid))) != 0)
339 1.77 manu printf("linux_userret_settid: cannot set TID\n");
340 1.77 manu }
341 1.77 manu
342 1.77 manu return;
343 1.39 jdolecek }
344