linux32_exec.c revision 1.1.10.2 1 1.1.10.2 simonb /* $NetBSD: linux32_exec.c,v 1.1.10.2 2006/04/22 11:38:14 simonb Exp $ */
2 1.1.10.2 simonb
3 1.1.10.2 simonb /*-
4 1.1.10.2 simonb * Copyright (c) 1994-2006 The NetBSD Foundation, Inc.
5 1.1.10.2 simonb * All rights reserved.
6 1.1.10.2 simonb *
7 1.1.10.2 simonb * This code is derived from software contributed to The NetBSD Foundation
8 1.1.10.2 simonb * by Christos Zoulas, Frank van der Linden, Eric Haszlakiewicz,
9 1.1.10.2 simonb * Thor Lancelot Simon, and Emmanuel Dreyfus.
10 1.1.10.2 simonb *
11 1.1.10.2 simonb * Redistribution and use in source and binary forms, with or without
12 1.1.10.2 simonb * modification, are permitted provided that the following conditions
13 1.1.10.2 simonb * are met:
14 1.1.10.2 simonb * 1. Redistributions of source code must retain the above copyright
15 1.1.10.2 simonb * notice, this list of conditions and the following disclaimer.
16 1.1.10.2 simonb * 2. Redistributions in binary form must reproduce the above copyright
17 1.1.10.2 simonb * notice, this list of conditions and the following disclaimer in the
18 1.1.10.2 simonb * documentation and/or other materials provided with the distribution.
19 1.1.10.2 simonb * 3. All advertising materials mentioning features or use of this software
20 1.1.10.2 simonb * must display the following acknowledgement:
21 1.1.10.2 simonb * This product includes software developed by the NetBSD
22 1.1.10.2 simonb * Foundation, Inc. and its contributors.
23 1.1.10.2 simonb * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1.10.2 simonb * contributors may be used to endorse or promote products derived
25 1.1.10.2 simonb * from this software without specific prior written permission.
26 1.1.10.2 simonb *
27 1.1.10.2 simonb * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1.10.2 simonb * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1.10.2 simonb * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1.10.2 simonb * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1.10.2 simonb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1.10.2 simonb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1.10.2 simonb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1.10.2 simonb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1.10.2 simonb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1.10.2 simonb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1.10.2 simonb * POSSIBILITY OF SUCH DAMAGE.
38 1.1.10.2 simonb */
39 1.1.10.2 simonb
40 1.1.10.2 simonb #include <sys/cdefs.h>
41 1.1.10.2 simonb __KERNEL_RCSID(0, "$NetBSD: linux32_exec.c,v 1.1.10.2 2006/04/22 11:38:14 simonb Exp $");
42 1.1.10.2 simonb
43 1.1.10.2 simonb #include <sys/param.h>
44 1.1.10.2 simonb #include <sys/systm.h>
45 1.1.10.2 simonb #include <sys/kernel.h>
46 1.1.10.2 simonb #include <sys/proc.h>
47 1.1.10.2 simonb #include <sys/malloc.h>
48 1.1.10.2 simonb #include <sys/namei.h>
49 1.1.10.2 simonb #include <sys/vnode.h>
50 1.1.10.2 simonb #include <sys/mount.h>
51 1.1.10.2 simonb #include <sys/exec.h>
52 1.1.10.2 simonb #include <sys/exec_elf.h>
53 1.1.10.2 simonb
54 1.1.10.2 simonb #include <sys/mman.h>
55 1.1.10.2 simonb #include <sys/sa.h>
56 1.1.10.2 simonb #include <sys/syscallargs.h>
57 1.1.10.2 simonb
58 1.1.10.2 simonb #include <uvm/uvm_extern.h>
59 1.1.10.2 simonb
60 1.1.10.2 simonb #include <machine/cpu.h>
61 1.1.10.2 simonb #include <machine/reg.h>
62 1.1.10.2 simonb
63 1.1.10.2 simonb #include <compat/linux/common/linux_types.h>
64 1.1.10.2 simonb
65 1.1.10.2 simonb #include <compat/linux32/common/linux32_exec.h>
66 1.1.10.2 simonb #include <compat/linux32/common/linux32_types.h>
67 1.1.10.2 simonb #include <compat/linux32/common/linux32_signal.h>
68 1.1.10.2 simonb #include <compat/linux32/common/linux32_machdep.h>
69 1.1.10.2 simonb #include <compat/linux32/common/linux32_emuldata.h>
70 1.1.10.2 simonb
71 1.1.10.2 simonb #include <compat/linux32/linux32_syscallargs.h>
72 1.1.10.2 simonb #include <compat/linux32/linux32_syscall.h>
73 1.1.10.2 simonb
74 1.1.10.2 simonb extern char linux32_sigcode[1];
75 1.1.10.2 simonb extern char linux32_rt_sigcode[1];
76 1.1.10.2 simonb extern char linux32_esigcode[1];
77 1.1.10.2 simonb
78 1.1.10.2 simonb extern struct sysent linux32_sysent[];
79 1.1.10.2 simonb extern const char * const linux32_syscallnames[];
80 1.1.10.2 simonb
81 1.1.10.2 simonb static void linux32_e_proc_exec __P((struct proc *, struct exec_package *));
82 1.1.10.2 simonb static void linux32_e_proc_fork __P((struct proc *, struct proc *, int));
83 1.1.10.2 simonb static void linux32_e_proc_exit __P((struct proc *));
84 1.1.10.2 simonb static void linux32_e_proc_init __P((struct proc *, struct proc *, int));
85 1.1.10.2 simonb
86 1.1.10.2 simonb #ifdef LINUX32_NPTL
87 1.1.10.2 simonb static void linux32_userret __P((struct lwp *, void *));
88 1.1.10.2 simonb #endif
89 1.1.10.2 simonb
90 1.1.10.2 simonb /*
91 1.1.10.2 simonb * Emulation switch.
92 1.1.10.2 simonb */
93 1.1.10.2 simonb
94 1.1.10.2 simonb struct uvm_object *emul_linux32_object;
95 1.1.10.2 simonb
96 1.1.10.2 simonb const struct emul emul_linux32 = {
97 1.1.10.2 simonb "linux32",
98 1.1.10.2 simonb "/emul/linux32",
99 1.1.10.2 simonb #ifndef __HAVE_MINIMAL_EMUL
100 1.1.10.2 simonb 0,
101 1.1.10.2 simonb NULL,
102 1.1.10.2 simonb LINUX32_SYS_syscall,
103 1.1.10.2 simonb LINUX32_SYS_NSYSENT,
104 1.1.10.2 simonb #endif
105 1.1.10.2 simonb linux32_sysent,
106 1.1.10.2 simonb linux32_syscallnames,
107 1.1.10.2 simonb linux32_sendsig,
108 1.1.10.2 simonb trapsignal,
109 1.1.10.2 simonb NULL,
110 1.1.10.2 simonb linux32_sigcode,
111 1.1.10.2 simonb linux32_esigcode,
112 1.1.10.2 simonb &emul_linux32_object,
113 1.1.10.2 simonb linux32_setregs,
114 1.1.10.2 simonb linux32_e_proc_exec,
115 1.1.10.2 simonb linux32_e_proc_fork,
116 1.1.10.2 simonb linux32_e_proc_exit,
117 1.1.10.2 simonb NULL,
118 1.1.10.2 simonb NULL,
119 1.1.10.2 simonb linux32_syscall_intern,
120 1.1.10.2 simonb NULL,
121 1.1.10.2 simonb NULL,
122 1.1.10.2 simonb netbsd32_vm_default_addr,
123 1.1.10.2 simonb };
124 1.1.10.2 simonb
125 1.1.10.2 simonb static void
126 1.1.10.2 simonb linux32_e_proc_init(p, parent, forkflags)
127 1.1.10.2 simonb struct proc *p, *parent;
128 1.1.10.2 simonb int forkflags;
129 1.1.10.2 simonb {
130 1.1.10.2 simonb struct linux32_emuldata *e = p->p_emuldata;
131 1.1.10.2 simonb struct linux32_emuldata_shared *s;
132 1.1.10.2 simonb struct linux32_emuldata *ep = NULL;
133 1.1.10.2 simonb
134 1.1.10.2 simonb if (!e) {
135 1.1.10.2 simonb /* allocate new Linux emuldata */
136 1.1.10.2 simonb MALLOC(e, void *, sizeof(struct linux32_emuldata),
137 1.1.10.2 simonb M_EMULDATA, M_WAITOK);
138 1.1.10.2 simonb } else {
139 1.1.10.2 simonb e->s->refs--;
140 1.1.10.2 simonb if (e->s->refs == 0)
141 1.1.10.2 simonb FREE(e->s, M_EMULDATA);
142 1.1.10.2 simonb }
143 1.1.10.2 simonb
144 1.1.10.2 simonb memset(e, '\0', sizeof(struct linux32_emuldata));
145 1.1.10.2 simonb
146 1.1.10.2 simonb if (parent)
147 1.1.10.2 simonb ep = parent->p_emuldata;
148 1.1.10.2 simonb
149 1.1.10.2 simonb if (forkflags & FORK_SHAREVM) {
150 1.1.10.2 simonb #ifdef DIAGNOSTIC
151 1.1.10.2 simonb if (ep == NULL) {
152 1.1.10.2 simonb killproc(p, "FORK_SHAREVM while emuldata is NULL\n");
153 1.1.10.2 simonb return;
154 1.1.10.2 simonb }
155 1.1.10.2 simonb #endif
156 1.1.10.2 simonb s = ep->s;
157 1.1.10.2 simonb s->refs++;
158 1.1.10.2 simonb } else {
159 1.1.10.2 simonb struct vmspace *vm;
160 1.1.10.2 simonb
161 1.1.10.2 simonb MALLOC(s, void *, sizeof(struct linux32_emuldata_shared),
162 1.1.10.2 simonb M_EMULDATA, M_WAITOK);
163 1.1.10.2 simonb s->refs = 1;
164 1.1.10.2 simonb
165 1.1.10.2 simonb /*
166 1.1.10.2 simonb * Set the process idea of the break to the real value.
167 1.1.10.2 simonb * For fork, we use parent's vmspace since our's
168 1.1.10.2 simonb * is not setup at the time of this call and is going
169 1.1.10.2 simonb * to be copy of parent's anyway. For exec, just
170 1.1.10.2 simonb * use our own vmspace.
171 1.1.10.2 simonb */
172 1.1.10.2 simonb vm = (parent) ? parent->p_vmspace : p->p_vmspace;
173 1.1.10.2 simonb s->p_break = vm->vm_daddr + ctob(vm->vm_dsize);
174 1.1.10.2 simonb
175 1.1.10.2 simonb /*
176 1.1.10.2 simonb * Linux threads are emulated as NetBSD processes (not lwp)
177 1.1.10.2 simonb * We use native PID for Linux TID. The Linux TID is the
178 1.1.10.2 simonb * PID of the first process in the group. It is stored
179 1.1.10.2 simonb * here
180 1.1.10.2 simonb */
181 1.1.10.2 simonb s->group_pid = p->p_pid;
182 1.1.10.2 simonb }
183 1.1.10.2 simonb
184 1.1.10.2 simonb e->s = s;
185 1.1.10.2 simonb
186 1.1.10.2 simonb #ifdef LINUX32_NPTL
187 1.1.10.2 simonb /*
188 1.1.10.2 simonb * initialize TID pointers. ep->child_clear_tid and
189 1.1.10.2 simonb * ep->child_set_tid will not be used beyond this point.
190 1.1.10.2 simonb */
191 1.1.10.2 simonb e->child_clear_tid = NULL;
192 1.1.10.2 simonb e->child_set_tid = NULL;
193 1.1.10.2 simonb if (ep != NULL) {
194 1.1.10.2 simonb e->clear_tid = ep->child_clear_tid;
195 1.1.10.2 simonb e->set_tid = ep->child_set_tid;
196 1.1.10.2 simonb e->set_tls = ep->set_tls;
197 1.1.10.2 simonb ep->child_clear_tid = NULL;
198 1.1.10.2 simonb ep->child_set_tid = NULL;
199 1.1.10.2 simonb ep->set_tls = 0;
200 1.1.10.2 simonb } else {
201 1.1.10.2 simonb e->clear_tid = NULL;
202 1.1.10.2 simonb e->set_tid = NULL;
203 1.1.10.2 simonb e->set_tls = 0;
204 1.1.10.2 simonb }
205 1.1.10.2 simonb #endif /* LINUX32_NPTL */
206 1.1.10.2 simonb
207 1.1.10.2 simonb p->p_emuldata = e;
208 1.1.10.2 simonb }
209 1.1.10.2 simonb
210 1.1.10.2 simonb /*
211 1.1.10.2 simonb * Allocate new per-process structures. Called when executing Linux
212 1.1.10.2 simonb * process. We can reuse the old emuldata - if it's not null,
213 1.1.10.2 simonb * the executed process is of same emulation as original forked one.
214 1.1.10.2 simonb */
215 1.1.10.2 simonb static void
216 1.1.10.2 simonb linux32_e_proc_exec(p, epp)
217 1.1.10.2 simonb struct proc *p;
218 1.1.10.2 simonb struct exec_package *epp;
219 1.1.10.2 simonb {
220 1.1.10.2 simonb /* exec, use our vmspace */
221 1.1.10.2 simonb linux32_e_proc_init(p, NULL, 0);
222 1.1.10.2 simonb }
223 1.1.10.2 simonb
224 1.1.10.2 simonb /*
225 1.1.10.2 simonb * Emulation per-process exit hook.
226 1.1.10.2 simonb */
227 1.1.10.2 simonb static void
228 1.1.10.2 simonb linux32_e_proc_exit(p)
229 1.1.10.2 simonb struct proc *p;
230 1.1.10.2 simonb {
231 1.1.10.2 simonb struct linux32_emuldata *e = p->p_emuldata;
232 1.1.10.2 simonb
233 1.1.10.2 simonb #ifdef LINUX32_NPTL
234 1.1.10.2 simonb /* Emulate LINUX_CLONE_CHILD_CLEARTID */
235 1.1.10.2 simonb if (e->clear_tid != NULL) {
236 1.1.10.2 simonb int error;
237 1.1.10.2 simonb int null = 0;
238 1.1.10.2 simonb struct linux32_sys_futex_args cup;
239 1.1.10.2 simonb register_t retval;
240 1.1.10.2 simonb struct lwp *l;
241 1.1.10.2 simonb
242 1.1.10.2 simonb if ((error = copyout(&null,
243 1.1.10.2 simonb e->clear_tid,
244 1.1.10.2 simonb sizeof(null))) != 0)
245 1.1.10.2 simonb printf("linux32_e_proc_exit: cannot clear TID\n");
246 1.1.10.2 simonb
247 1.1.10.2 simonb l = proc_representative_lwp(p);
248 1.1.10.2 simonb SCARG(&cup, uaddr) = e->clear_tid;
249 1.1.10.2 simonb SCARG(&cup, op) = LINUX_FUTEX_WAKE;
250 1.1.10.2 simonb SCARG(&cup, val) = 0x7fffffff; /* Awake everyone */
251 1.1.10.2 simonb SCARG(&cup, timeout) = NULL;
252 1.1.10.2 simonb SCARG(&cup, uaddr2) = NULL;
253 1.1.10.2 simonb SCARG(&cup, val3) = 0;
254 1.1.10.2 simonb if ((error = linux32_sys_futex(l, &cup, &retval)) != 0)
255 1.1.10.2 simonb printf("linux32_e_proc_exit: linux_sys_futex failed\n");
256 1.1.10.2 simonb }
257 1.1.10.2 simonb #endif /* LINUX32_NPTL */
258 1.1.10.2 simonb
259 1.1.10.2 simonb /* free Linux emuldata and set the pointer to null */
260 1.1.10.2 simonb e->s->refs--;
261 1.1.10.2 simonb if (e->s->refs == 0)
262 1.1.10.2 simonb FREE(e->s, M_EMULDATA);
263 1.1.10.2 simonb FREE(e, M_EMULDATA);
264 1.1.10.2 simonb p->p_emuldata = NULL;
265 1.1.10.2 simonb }
266 1.1.10.2 simonb
267 1.1.10.2 simonb /*
268 1.1.10.2 simonb * Emulation fork hook.
269 1.1.10.2 simonb */
270 1.1.10.2 simonb static void
271 1.1.10.2 simonb linux32_e_proc_fork(p, parent, forkflags)
272 1.1.10.2 simonb struct proc *p, *parent;
273 1.1.10.2 simonb int forkflags;
274 1.1.10.2 simonb {
275 1.1.10.2 simonb #ifdef LINUX32_NPTL
276 1.1.10.2 simonb struct linux32_emuldata *e;
277 1.1.10.2 simonb #endif
278 1.1.10.2 simonb
279 1.1.10.2 simonb /*
280 1.1.10.2 simonb * The new process might share some vmspace-related stuff
281 1.1.10.2 simonb * with parent, depending on fork flags (CLONE_VM et.al).
282 1.1.10.2 simonb * Force allocation of new base emuldata, and share the
283 1.1.10.2 simonb * VM-related parts only if necessary.
284 1.1.10.2 simonb */
285 1.1.10.2 simonb p->p_emuldata = NULL;
286 1.1.10.2 simonb linux32_e_proc_init(p, parent, forkflags);
287 1.1.10.2 simonb
288 1.1.10.2 simonb #ifdef LINUX32_NPTL
289 1.1.10.2 simonb /*
290 1.1.10.2 simonb * Emulate LINUX_CLONE_CHILD_SETTID and LINUX_CLONE_TLS:
291 1.1.10.2 simonb * This cannot be done right now because the child VM
292 1.1.10.2 simonb * is not set up. We will do it at userret time.
293 1.1.10.2 simonb */
294 1.1.10.2 simonb e = p->p_emuldata;
295 1.1.10.2 simonb if ((e->set_tid != NULL) || (e->set_tls != 0))
296 1.1.10.2 simonb p->p_userret = (*linux32_userret);
297 1.1.10.2 simonb #endif
298 1.1.10.2 simonb
299 1.1.10.2 simonb return;
300 1.1.10.2 simonb }
301 1.1.10.2 simonb
302 1.1.10.2 simonb #ifdef LINUX32_NPTL
303 1.1.10.2 simonb static void
304 1.1.10.2 simonb linux32_userret(l, arg)
305 1.1.10.2 simonb struct lwp *l;
306 1.1.10.2 simonb void *arg;
307 1.1.10.2 simonb {
308 1.1.10.2 simonb struct proc *p = l->l_proc;
309 1.1.10.2 simonb struct linux32_emuldata *led = p->p_emuldata;
310 1.1.10.2 simonb int error;
311 1.1.10.2 simonb
312 1.1.10.2 simonb p->p_userret = NULL;
313 1.1.10.2 simonb
314 1.1.10.2 simonb /* Emulate LINUX_CLONE_CHILD_SETTID */
315 1.1.10.2 simonb if (led->set_tid != NULL) {
316 1.1.10.2 simonb if ((error = copyout(&p->p_pid,
317 1.1.10.2 simonb led->set_tid, sizeof(p->p_pid))) != 0)
318 1.1.10.2 simonb printf("linux32_userret: cannot set TID\n");
319 1.1.10.2 simonb }
320 1.1.10.2 simonb
321 1.1.10.2 simonb /* Emulate LINUX_CLONE_NEWTLS */
322 1.1.10.2 simonb if (led->set_tls != 0) {
323 1.1.10.2 simonb if (linux32_set_newtls(l, led->set_tls) != 0)
324 1.1.10.2 simonb printf("linux32_userret: cannot set TLS\n");
325 1.1.10.2 simonb }
326 1.1.10.2 simonb
327 1.1.10.2 simonb return;
328 1.1.10.2 simonb }
329 1.1.10.2 simonb #endif /* LINUX32_NPTL */
330