1 /* $NetBSD: mach_exec.c,v 1.70 2008/10/15 06:51:19 wrstuden Exp $ */
2
3 /*-
4 * Copyright (c) 2001-2003 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 and Emmanuel Dreyfus.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: mach_exec.c,v 1.70 2008/10/15 06:51:19 wrstuden Exp $");
34
35 #include "opt_syscall_debug.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/proc.h>
40 #include <sys/exec.h>
41 #include <sys/queue.h>
42 #include <sys/exec_macho.h>
43 #include <sys/malloc.h>
44
45 #include <sys/syscall.h>
46
47 #include <uvm/uvm_extern.h>
48 #include <uvm/uvm_param.h>
49
50 #include <compat/mach/mach_types.h>
51 #include <compat/mach/mach_message.h>
52 #include <compat/mach/mach_port.h>
53 #include <compat/mach/mach_semaphore.h>
54 #include <compat/mach/mach_notify.h>
55 #include <compat/mach/mach_exec.h>
56
57 static int mach_cold = 1; /* Have we initialized COMPAT_MACH structures? */
58 static void mach_init(void);
59
60 extern struct sysent sysent[];
61 #ifdef SYSCALL_DEBUG
62 extern const char * const syscallnames[];
63 #endif
64 #ifndef __HAVE_SYSCALL_INTERN
65 void syscall(void);
66 #else
67 void mach_syscall_intern(struct proc *);
68 #endif
69
70 #ifdef COMPAT_16
71 extern char sigcode[], esigcode[];
72 struct uvm_object *emul_mach_object;
73 #endif
74
75 const struct emul emul_mach = {
76 "mach",
77 "/emul/mach",
78 #ifndef __HAVE_MINIMAL_EMUL
79 0,
80 0,
81 SYS_syscall,
82 SYS_NSYSENT,
83 #endif
84 sysent,
85 #ifdef SYSCALL_DEBUG
86 syscallnames,
87 #else
88 NULL,
89 #endif
90 sendsig,
91 mach_trapsignal,
92 NULL,
93 #ifdef COMPAT_16
94 sigcode,
95 esigcode,
96 &emul_mach_object,
97 #else
98 NULL,
99 NULL,
100 NULL,
101 #endif
102 setregs,
103 mach_e_proc_exec,
104 mach_e_proc_fork,
105 mach_e_proc_exit,
106 mach_e_lwp_fork,
107 mach_e_lwp_exit,
108 #ifdef __HAVE_SYSCALL_INTERN
109 mach_syscall_intern,
110 #else
111 syscall,
112 #endif
113 NULL, /* e_fault */
114 NULL, /* e_vm_default_addr */
115
116 uvm_default_mapaddr,
117 NULL, /* e_usertrap */
118 NULL, /* e_sa */
119 0, /* e_ucsize */
120 NULL, /* e_startlwp */
121 };
122
123 /*
124 * Copy arguments onto the stack in the normal way, but add some
125 * extra information in case of dynamic binding.
126 * XXX This needs a cleanup: it is not used anymore by the Darwin
127 * emulation, and it probably contains Darwin specific bits.
128 */
129 int
130 exec_mach_copyargs(struct lwp *l, struct exec_package *pack, struct ps_strings *arginfo, char **stackp, void *argp)
131 {
132 struct exec_macho_emul_arg *emea;
133 struct exec_macho_object_header *macho_hdr;
134 size_t len;
135 size_t zero = 0;
136 int error;
137
138 *stackp = (char *)(((unsigned long)*stackp - 1) & ~0xfUL);
139
140 emea = (struct exec_macho_emul_arg *)pack->ep_emul_arg;
141 macho_hdr = (struct exec_macho_object_header *)emea->macho_hdr;
142 if ((error = copyout(&macho_hdr, *stackp, sizeof(macho_hdr))) != 0)
143 return error;
144
145 *stackp += sizeof(macho_hdr);
146
147 if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0) {
148 DPRINTF(("mach: copyargs failed\n"));
149 return error;
150 }
151
152 if ((error = copyout(&zero, *stackp, sizeof(zero))) != 0)
153 return error;
154 *stackp += sizeof(zero);
155
156 if ((error = copyoutstr(emea->filename,
157 *stackp, MAXPATHLEN, &len)) != 0) {
158 DPRINTF(("mach: copyout path failed\n"));
159 return error;
160 }
161 *stackp += len + 1;
162
163 /* We don't need this anymore */
164 free(pack->ep_emul_arg, M_TEMP);
165 pack->ep_emul_arg = NULL;
166
167 len = len % sizeof(zero);
168 if (len) {
169 if ((error = copyout(&zero, *stackp, len)) != 0)
170 return error;
171 *stackp += len;
172 }
173
174 if ((error = copyout(&zero, *stackp, sizeof(zero))) != 0)
175 return error;
176 *stackp += sizeof(zero);
177
178 return 0;
179 }
180
181 int
182 exec_mach_probe(const char **path)
183 {
184 *path = emul_mach.e_path;
185 return 0;
186 }
187
188 void
189 mach_e_proc_exec(struct proc *p, struct exec_package *epp)
190 {
191 mach_e_proc_init(p, p->p_vmspace);
192
193 if (p->p_emul != epp->ep_esch->es_emul) {
194 struct lwp *l = LIST_FIRST(&p->p_lwps);
195 KASSERT(l != NULL);
196 mach_e_lwp_fork(NULL, l);
197 }
198
199 return;
200 }
201
202 void
203 mach_e_proc_fork(struct proc *p, struct proc *parent, int forkflags)
204 {
205 mach_e_proc_fork1(p, parent, 1);
206 return;
207 }
208
209 void
210 mach_e_proc_fork1(struct proc *p, struct proc *parent, int allocate)
211 {
212 struct mach_emuldata *med1;
213 struct mach_emuldata *med2;
214 int i;
215
216 /*
217 * For Darwin binaries, p->p_emuldata has already been
218 * allocated, no need to throw it away and allocate it again.
219 */
220 if (allocate)
221 p->p_emuldata = NULL;
222
223 /* Use parent's vmspace because our vmspace may not be set up yet */
224 mach_e_proc_init(p, parent->p_vmspace);
225
226 med1 = p->p_emuldata;
227 med2 = parent->p_emuldata;
228
229 /*
230 * Exception ports are inherited between forks,
231 * but we need to double their reference counts,
232 * since the ports are referenced by rights in the
233 * parent and in the child.
234 *
235 * XXX we need to convert all the parent's rights
236 * to the child namespace. This will make the
237 * following fixup obsolete.
238 */
239 for (i = 0; i <= MACH_EXC_MAX; i++) {
240 med1->med_exc[i] = med2->med_exc[i];
241 if (med1->med_exc[i] != NULL)
242 med1->med_exc[i]->mp_refcount *= 2;
243 }
244
245 return;
246 }
247
248 void
249 mach_e_proc_init(struct proc *p, struct vmspace *vmspace)
250 {
251 struct mach_emuldata *med;
252 struct mach_right *mr;
253
254 /*
255 * Initialize various things if needed.
256 * XXX Not the best place for this.
257 */
258 if (mach_cold == 1)
259 mach_init();
260
261 /*
262 * For Darwin binaries, p->p_emuldata is always allocated:
263 * from the previous program if it had the same emulation,
264 * or from darwin_e_proc_exec(). In the latter situation,
265 * everything has been set to zero.
266 */
267 if (!p->p_emuldata) {
268 #ifdef DIAGNOSTIC
269 if (p->p_emul != &emul_mach)
270 printf("mach_emuldata allocated for non Mach binary\n");
271 #endif
272 p->p_emuldata = malloc(sizeof(struct mach_emuldata),
273 M_EMULDATA, M_WAITOK | M_ZERO);
274 }
275
276 med = (struct mach_emuldata *)p->p_emuldata;
277
278 /*
279 * p->p_emudata has med_inited set if we inherited it from
280 * the program that called exec(). In that situation, we
281 * must free anything that will not be used anymore.
282 */
283 if (med->med_inited != 0) {
284 rw_enter(&med->med_rightlock, RW_WRITER);
285 while ((mr = LIST_FIRST(&med->med_right)) != NULL)
286 mach_right_put_exclocked(mr, MACH_PORT_TYPE_ALL_RIGHTS);
287 rw_exit(&med->med_rightlock);
288
289 /*
290 * Do not touch special ports. Some other process (eg: gdb)
291 * might have grabbed them to control the process, and the
292 * controller intend to keep in control even after exec().
293 */
294 } else {
295 /*
296 * p->p_emuldata is uninitialized. Go ahead and initialize it.
297 */
298 LIST_INIT(&med->med_right);
299 rw_init(&med->med_rightlock);
300 rw_init(&med->med_exclock);
301
302 /*
303 * For debugging purpose, it's convenient to have each process
304 * using distinct port names, so we prefix the first port name
305 * by the PID. Darwin does not do that, but we can remove it
306 * when we want, it will not hurt.
307 */
308 med->med_nextright = p->p_pid << 16;
309
310 /*
311 * Initialize special ports. Bootstrap port is shared
312 * among all Mach processes in our implementation.
313 */
314 med->med_kernel = mach_port_get();
315 med->med_host = mach_port_get();
316
317 med->med_kernel->mp_flags |= MACH_MP_INKERNEL;
318 med->med_host->mp_flags |= MACH_MP_INKERNEL;
319
320 med->med_kernel->mp_data = (void *)p;
321 med->med_host->mp_data = (void *)p;
322
323 med->med_kernel->mp_datatype = MACH_MP_PROC;
324 med->med_host->mp_datatype = MACH_MP_PROC;
325
326 MACH_PORT_REF(med->med_kernel);
327 MACH_PORT_REF(med->med_host);
328
329 med->med_bootstrap = mach_bootstrap_port;
330 MACH_PORT_REF(med->med_bootstrap);
331 }
332
333 /*
334 * Exception ports are inherited accross exec() calls.
335 * If the structure is initialized, the ports are just
336 * here, so leave them untouched. If the structure is
337 * uninitalized, the ports are all set to zero, which
338 * is the default, so do not touch them either.
339 */
340
341 med->med_dirty_thid = 1;
342 med->med_suspend = 0;
343 med->med_inited = 1;
344
345 return;
346 }
347
348 void
349 mach_e_proc_exit(struct proc *p)
350 {
351 struct mach_emuldata *med;
352 struct mach_right *mr;
353 struct lwp *l;
354 int i;
355
356 /* There is only one lwp remaining... */
357 l = LIST_FIRST(&p->p_lwps);
358 KASSERT(l != NULL);
359 mach_e_lwp_exit(l);
360
361 med = (struct mach_emuldata *)p->p_emuldata;
362
363 rw_enter(&med->med_rightlock, RW_WRITER);
364 while ((mr = LIST_FIRST(&med->med_right)) != NULL)
365 mach_right_put_exclocked(mr, MACH_PORT_TYPE_ALL_RIGHTS);
366 rw_exit(&med->med_rightlock);
367
368 MACH_PORT_UNREF(med->med_bootstrap);
369
370 /*
371 * If the lock on this task exception handler is held,
372 * release it now as it will never be released by the
373 * exception handler.
374 */
375 if (rw_lock_held(&med->med_exclock))
376 wakeup(&med->med_exclock);
377
378 /*
379 * If the kernel and host port are still referenced, remove
380 * the pointer to this process' struct proc, as it will
381 * become invalid once the process will exit.
382 */
383 med->med_kernel->mp_datatype = MACH_MP_NONE;
384 med->med_kernel->mp_data = NULL;
385 MACH_PORT_UNREF(med->med_kernel);
386
387 med->med_host->mp_datatype = MACH_MP_NONE;
388 med->med_host->mp_data = NULL;
389 MACH_PORT_UNREF(med->med_host);
390
391 for (i = 0; i <= MACH_EXC_MAX; i++)
392 if (med->med_exc[i] != NULL)
393 MACH_PORT_UNREF(med->med_exc[i]);
394
395 rw_destroy(&med->med_exclock);
396 rw_destroy(&med->med_rightlock);
397 free(med, M_EMULDATA);
398 p->p_emuldata = NULL;
399
400 return;
401 }
402
403 void
404 mach_e_lwp_fork(struct lwp *l1, struct lwp *l2)
405 {
406 struct mach_lwp_emuldata *mle;
407
408 mle = malloc(sizeof(*mle), M_EMULDATA, M_WAITOK);
409 l2->l_emuldata = mle;
410
411 mle->mle_kernel = mach_port_get();
412 MACH_PORT_REF(mle->mle_kernel);
413
414 mle->mle_kernel->mp_flags |= MACH_MP_INKERNEL;
415 mle->mle_kernel->mp_datatype = MACH_MP_LWP;
416 mle->mle_kernel->mp_data = (void *)l2;
417
418 #if 0
419 /* Nothing to copy from parent thread for now */
420 if (l1 != NULL);
421 #endif
422
423 return;
424 }
425
426 void
427 mach_e_lwp_exit(struct lwp *l)
428 {
429 struct mach_lwp_emuldata *mle;
430
431 mach_semaphore_cleanup(l);
432
433 #ifdef DIAGNOSTIC
434 if (l->l_emuldata == NULL) {
435 printf("lwp_emuldata already freed\n");
436 return;
437 }
438 #endif
439 mle = l->l_emuldata;
440
441 mle->mle_kernel->mp_data = NULL;
442 mle->mle_kernel->mp_datatype = MACH_MP_NONE;
443 MACH_PORT_UNREF(mle->mle_kernel);
444
445 free(mle, M_EMULDATA);
446 l->l_emuldata = NULL;
447
448 return;
449 }
450
451 static void
452 mach_init(void)
453 {
454 mach_semaphore_init();
455 mach_message_init();
456 mach_port_init();
457
458 mach_cold = 0;
459
460 return;
461 }
Cache object: 7ce9759ace9fb63bc1efffa63d4d817d
|