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