linux32_exec.c revision 1.11
1/*	$NetBSD: linux32_exec.c,v 1.11 2008/04/24 15:35:27 ad Exp $ */
2
3/*-
4 * Copyright (c) 1994-2007 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.11 2008/04/24 15:35:27 ad 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/syscallargs.h>
56#include <sys/ptrace.h>		/* For proc_reparent() */
57
58#include <uvm/uvm_extern.h>
59
60#include <sys/cpu.h>
61#include <machine/reg.h>
62
63#include <compat/linux/common/linux_types.h>
64#include <compat/linux/common/linux_emuldata.h>
65
66#include <compat/linux32/common/linux32_exec.h>
67#include <compat/linux32/common/linux32_types.h>
68#include <compat/linux32/common/linux32_signal.h>
69#include <compat/linux32/common/linux32_machdep.h>
70
71#include <compat/linux32/linux32_syscallargs.h>
72#include <compat/linux32/linux32_syscall.h>
73
74extern char linux32_sigcode[1];
75extern char linux32_rt_sigcode[1];
76extern char linux32_esigcode[1];
77
78extern struct sysent linux32_sysent[];
79extern const char * const linux32_syscallnames[];
80
81static void linux32_e_proc_exec(struct proc *, struct exec_package *);
82static void linux32_e_proc_fork(struct proc *, struct proc *, int);
83static void linux32_e_proc_exit(struct proc *);
84static void linux32_e_proc_init(struct proc *, struct proc *, int);
85
86#ifdef LINUX32_NPTL
87void linux32_userret(void);
88void linux_nptl_proc_fork(struct proc *, struct proc *, void (*luserret)(void));
89void linux_nptl_proc_exit(struct proc *);
90void linux_nptl_proc_init(struct proc *, struct proc *);
91#endif
92
93/*
94 * Emulation switch.
95 */
96
97struct uvm_object *emul_linux32_object;
98
99const struct emul emul_linux32 = {
100	"linux32",
101	"/emul/linux32",
102#ifndef __HAVE_MINIMAL_EMUL
103	0,
104	NULL,
105	LINUX32_SYS_syscall,
106	LINUX32_SYS_NSYSENT,
107#endif
108	linux32_sysent,
109	linux32_syscallnames,
110	linux32_sendsig,
111	trapsignal,
112	NULL,
113	linux32_sigcode,
114	linux32_esigcode,
115	&emul_linux32_object,
116	linux32_setregs,
117	linux32_e_proc_exec,
118	linux32_e_proc_fork,
119	linux32_e_proc_exit,
120	NULL,
121	NULL,
122	linux32_syscall_intern,
123	NULL,
124	NULL,
125	netbsd32_vm_default_addr,
126};
127
128static void
129linux32_e_proc_init(p, parent, forkflags)
130	struct proc *p, *parent;
131	int forkflags;
132{
133	struct linux_emuldata *e = p->p_emuldata;
134	struct linux_emuldata_shared *s;
135	struct linux_emuldata *ep = NULL;
136
137	if (!e) {
138		/* allocate new Linux emuldata */
139		MALLOC(e, void *, sizeof(struct linux_emuldata),
140			M_EMULDATA, M_WAITOK);
141	} else  {
142		mutex_enter(proc_lock);
143		e->s->refs--;
144		if (e->s->refs == 0)
145			FREE(e->s, M_EMULDATA);
146		mutex_exit(proc_lock);
147	}
148
149	memset(e, '\0', sizeof(struct linux_emuldata));
150
151	e->proc = p;
152
153	if (parent)
154		ep = parent->p_emuldata;
155
156	if (forkflags & FORK_SHAREVM) {
157		mutex_enter(proc_lock);
158#ifdef DIAGNOSTIC
159		if (ep == NULL) {
160			killproc(p, "FORK_SHAREVM while emuldata is NULL\n");
161			mutex_exit(proc_lock);
162			return;
163		}
164#endif
165		mutex_enter(proc_lock);
166		s = ep->s;
167		s->refs++;
168	} else {
169		struct vmspace *vm;
170
171		MALLOC(s, void *, sizeof(struct linux_emuldata_shared),
172			M_EMULDATA, M_WAITOK);
173		s->refs = 1;
174
175		/*
176		 * Set the process idea of the break to the real value.
177		 * For fork, we use parent's vmspace since our's
178		 * is not setup at the time of this call and is going
179		 * to be copy of parent's anyway. For exec, just
180		 * use our own vmspace.
181		 */
182		vm = (parent) ? parent->p_vmspace : p->p_vmspace;
183		s->p_break = (char *)vm->vm_daddr + ctob(vm->vm_dsize);
184
185		/*
186		 * Linux threads are emulated as NetBSD processes (not lwp)
187		 * We use native PID for Linux TID. The Linux TID is the
188		 * PID of the first process in the group. It is stored
189		 * here
190		 */
191		s->group_pid = p->p_pid;
192
193		/*
194		 * Initialize the list of threads in the group
195		 */
196		LIST_INIT(&s->threads);
197
198		s->xstat = 0;
199		s->flags = 0;
200		mutex_enter(proc_lock);
201	}
202
203	e->s = s;
204
205	/*
206	 * Add this thread in the group thread list
207	 */
208	LIST_INSERT_HEAD(&s->threads, e, threads);
209	mutex_exit(proc_lock);
210
211#ifdef LINUX32_NPTL
212	linux_nptl_proc_init(p, parent);
213#endif /* LINUX32_NPTL */
214
215	p->p_emuldata = e;
216}
217
218/*
219 * Allocate new per-process structures. Called when executing Linux
220 * process. We can reuse the old emuldata - if it's not null,
221 * the executed process is of same emulation as original forked one.
222 */
223static void
224linux32_e_proc_exec(struct proc *p, struct exec_package *epp)
225{
226	/* exec, use our vmspace */
227	linux32_e_proc_init(p, NULL, 0);
228}
229
230/*
231 * Emulation per-process exit hook.
232 */
233static void
234linux32_e_proc_exit(struct proc *p)
235{
236	struct linux_emuldata *e = p->p_emuldata;
237
238#ifdef LINUX32_NPTL
239	linux_nptl_proc_exit(p);
240#endif /* LINUX32_NPTL */
241
242	/* Remove the thread for the group thread list */
243	mutex_enter(proc_lock);
244	LIST_REMOVE(e, threads);
245
246	/* free Linux emuldata and set the pointer to null */
247	e->s->refs--;
248	if (e->s->refs == 0)
249		FREE(e->s, M_EMULDATA);
250	p->p_emuldata = NULL;
251	mutex_exit(proc_lock);
252	FREE(e, M_EMULDATA);
253}
254
255/*
256 * Emulation fork hook.
257 */
258static void
259linux32_e_proc_fork(p, parent, forkflags)
260	struct proc *p, *parent;
261	int forkflags;
262{
263	/*
264	 * The new process might share some vmspace-related stuff
265	 * with parent, depending on fork flags (CLONE_VM et.al).
266	 * Force allocation of new base emuldata, and share the
267	 * VM-related parts only if necessary.
268	 */
269	p->p_emuldata = NULL;
270	linux32_e_proc_init(p, parent, forkflags);
271
272#ifdef LINUX32_NPTL
273	linux_nptl_proc_fork(p, parent, linux32_userret);
274#endif
275
276	return;
277}
278
279#ifdef LINUX32_NPTL
280void
281linux32_userret(void)
282{
283	struct lwp *l = curlwp;
284	struct proc *p = l->l_proc;
285	struct linux_emuldata *led = p->p_emuldata;
286	int error;
287
288	/* LINUX_CLONE_CHILD_SETTID: copy child's TID to child's memory  */
289	if (led->clone_flags & LINUX_CLONE_CHILD_SETTID) {
290		if ((error = copyout(&l->l_proc->p_pid,
291		    led->child_tidptr,  sizeof(l->l_proc->p_pid))) != 0)
292			printf("linux32_userret: LINUX_CLONE_CHILD_SETTID "
293			    "failed (led->child_tidptr = %p, p->p_pid = %d)\n",
294			    led->child_tidptr, p->p_pid);
295	}
296
297	/* LINUX_CLONE_SETTLS: allocate a new TLS */
298	if (led->clone_flags & LINUX_CLONE_SETTLS) {
299		if (linux32_set_newtls(l, linux32_get_newtls(l)) != 0)
300			printf("linux32_userret: linux32_set_tls failed");
301	}
302
303	return;
304}
305#endif /* LINUX32_NPTL */
306