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