No empty .Rs/.Re
[netbsd-mini2440.git] / sys / compat / linux32 / common / linux32_exec.c
blob081b5c4ac55052a9051b7e5872fd058a0d8bec5c
1 /* $NetBSD: linux32_exec.c,v 1.18 2009/03/14 21:04:18 dsl Exp $ */
3 /*-
4 * Copyright (c) 1994-2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: linux32_exec.c,v 1.18 2009/03/14 21:04:18 dsl Exp $");
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/proc.h>
40 #include <sys/malloc.h>
41 #include <sys/namei.h>
42 #include <sys/vnode.h>
43 #include <sys/mount.h>
44 #include <sys/exec.h>
45 #include <sys/exec_elf.h>
47 #include <sys/mman.h>
48 #include <sys/syscallargs.h>
49 #include <sys/ptrace.h> /* For proc_reparent() */
51 #include <uvm/uvm_extern.h>
53 #include <sys/cpu.h>
54 #include <machine/reg.h>
56 #include <compat/linux/common/linux_types.h>
57 #include <compat/linux/common/linux_emuldata.h>
59 #include <compat/linux32/common/linux32_exec.h>
60 #include <compat/linux32/common/linux32_types.h>
61 #include <compat/linux32/common/linux32_signal.h>
62 #include <compat/linux32/common/linux32_machdep.h>
64 #include <compat/linux32/linux32_syscallargs.h>
65 #include <compat/linux32/linux32_syscall.h>
67 extern char linux32_sigcode[1];
68 extern char linux32_rt_sigcode[1];
69 extern char linux32_esigcode[1];
71 extern struct sysent linux32_sysent[];
72 extern const char * const linux32_syscallnames[];
74 static void linux32_e_proc_exec(struct proc *, struct exec_package *);
75 static void linux32_e_proc_fork(struct proc *, struct proc *, int);
76 static void linux32_e_proc_exit(struct proc *);
77 static void linux32_e_proc_init(struct proc *, struct proc *, int);
79 #ifdef LINUX32_NPTL
80 void linux32_userret(void);
81 void linux_nptl_proc_fork(struct proc *, struct proc *, void (*luserret)(void));
82 void linux_nptl_proc_exit(struct proc *);
83 void linux_nptl_proc_init(struct proc *, struct proc *);
84 #endif
87 * Emulation switch.
90 struct uvm_object *emul_linux32_object;
92 struct emul emul_linux32 = {
93 .e_name = "linux32",
94 .e_path = "/emul/linux32",
95 #ifndef __HAVE_MINIMAL_EMUL
96 .e_flags = 0,
97 .e_errno = NULL,
98 .e_nosys = LINUX32_SYS_syscall,
99 .e_nsysent = LINUX32_SYS_NSYSENT,
100 #endif
101 .e_sysent = linux32_sysent,
102 .e_syscallnames = linux32_syscallnames,
103 .e_sendsig = linux32_sendsig,
104 .e_trapsignal = trapsignal,
105 .e_tracesig = NULL,
106 .e_sigcode = linux32_sigcode,
107 .e_esigcode = linux32_esigcode,
108 .e_sigobject = &emul_linux32_object,
109 .e_setregs = linux32_setregs,
110 .e_proc_exec = linux32_e_proc_exec,
111 .e_proc_fork = linux32_e_proc_fork,
112 .e_proc_exit = linux32_e_proc_exit,
113 .e_lwp_fork = NULL,
114 .e_lwp_exit = NULL,
115 .e_syscall_intern = linux32_syscall_intern,
116 .e_sysctlovly = NULL,
117 .e_fault = NULL,
118 .e_vm_default_addr = netbsd32_vm_default_addr,
119 .e_usertrap = NULL,
120 .e_sa = NULL,
121 .e_ucsize = 0,
122 .e_startlwp = NULL
125 static void
126 linux32_e_proc_init(struct proc *p, struct proc *parent, int forkflags)
128 struct linux_emuldata *e = p->p_emuldata;
129 struct linux_emuldata_shared *s;
130 struct linux_emuldata *ep = NULL;
132 if (!e) {
133 /* allocate new Linux emuldata */
134 e = malloc(sizeof(struct linux_emuldata),
135 M_EMULDATA, M_WAITOK);
136 } else {
137 mutex_enter(proc_lock);
138 e->s->refs--;
139 if (e->s->refs == 0)
140 free(e->s, M_EMULDATA);
141 mutex_exit(proc_lock);
144 memset(e, '\0', sizeof(struct linux_emuldata));
146 e->proc = p;
148 if (parent)
149 ep = parent->p_emuldata;
151 if (forkflags & FORK_SHAREVM) {
152 mutex_enter(proc_lock);
153 #ifdef DIAGNOSTIC
154 if (ep == NULL) {
155 killproc(p, "FORK_SHAREVM while emuldata is NULL\n");
156 mutex_exit(proc_lock);
157 return;
159 #endif
160 s = ep->s;
161 s->refs++;
162 } else {
163 struct vmspace *vm;
165 s = malloc(sizeof(struct linux_emuldata_shared),
166 M_EMULDATA, M_WAITOK);
167 s->refs = 1;
170 * Set the process idea of the break to the real value.
171 * For fork, we use parent's vmspace since our's
172 * is not setup at the time of this call and is going
173 * to be copy of parent's anyway. For exec, just
174 * use our own vmspace.
176 vm = (parent) ? parent->p_vmspace : p->p_vmspace;
177 s->p_break = (char *)vm->vm_daddr + ctob(vm->vm_dsize);
180 * Linux threads are emulated as NetBSD processes (not lwp)
181 * We use native PID for Linux TID. The Linux TID is the
182 * PID of the first process in the group. It is stored
183 * here
185 s->group_pid = p->p_pid;
188 * Initialize the list of threads in the group
190 LIST_INIT(&s->threads);
192 s->xstat = 0;
193 s->flags = 0;
194 mutex_enter(proc_lock);
197 e->s = s;
200 * Add this thread in the group thread list
202 LIST_INSERT_HEAD(&s->threads, e, threads);
203 mutex_exit(proc_lock);
205 #ifdef LINUX32_NPTL
206 linux_nptl_proc_init(p, parent);
207 #endif /* LINUX32_NPTL */
209 p->p_emuldata = e;
213 * Allocate new per-process structures. Called when executing Linux
214 * process. We can reuse the old emuldata - if it's not null,
215 * the executed process is of same emulation as original forked one.
217 static void
218 linux32_e_proc_exec(struct proc *p, struct exec_package *epp)
220 /* exec, use our vmspace */
221 linux32_e_proc_init(p, NULL, 0);
225 * Emulation per-process exit hook.
227 static void
228 linux32_e_proc_exit(struct proc *p)
230 struct linux_emuldata *e = p->p_emuldata;
232 #ifdef LINUX32_NPTL
233 linux_nptl_proc_exit(p);
234 #endif /* LINUX32_NPTL */
236 /* Remove the thread for the group thread list */
237 mutex_enter(proc_lock);
238 LIST_REMOVE(e, threads);
240 /* free Linux emuldata and set the pointer to null */
241 e->s->refs--;
242 if (e->s->refs == 0)
243 free(e->s, M_EMULDATA);
244 p->p_emuldata = NULL;
245 mutex_exit(proc_lock);
246 free(e, M_EMULDATA);
250 * Emulation fork hook.
252 static void
253 linux32_e_proc_fork(struct proc *p, struct proc *parent, int forkflags)
256 * The new process might share some vmspace-related stuff
257 * with parent, depending on fork flags (CLONE_VM et.al).
258 * Force allocation of new base emuldata, and share the
259 * VM-related parts only if necessary.
261 p->p_emuldata = NULL;
262 linux32_e_proc_init(p, parent, forkflags);
264 #ifdef LINUX32_NPTL
265 linux_nptl_proc_fork(p, parent, linux32_userret);
266 #endif
268 return;
271 #ifdef LINUX32_NPTL
272 void
273 linux32_userret(void)
275 struct lwp *l = curlwp;
276 struct proc *p = l->l_proc;
277 struct linux_emuldata *led = p->p_emuldata;
278 int error;
280 /* LINUX_CLONE_CHILD_SETTID: copy child's TID to child's memory */
281 if (led->clone_flags & LINUX_CLONE_CHILD_SETTID) {
282 if ((error = copyout(&l->l_proc->p_pid,
283 led->child_tidptr, sizeof(l->l_proc->p_pid))) != 0)
284 printf("linux32_userret: LINUX_CLONE_CHILD_SETTID "
285 "failed (led->child_tidptr = %p, p->p_pid = %d)\n",
286 led->child_tidptr, p->p_pid);
289 /* LINUX_CLONE_SETTLS: allocate a new TLS */
290 if (led->clone_flags & LINUX_CLONE_SETTLS) {
291 if (linux32_set_newtls(l, linux32_get_newtls(l)) != 0)
292 printf("linux32_userret: linux32_set_tls failed");
295 return;
297 #endif /* LINUX32_NPTL */