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