linux_exec.c revision 1.67 1 /* $NetBSD: linux_exec.c,v 1.67 2003/12/04 19:38:23 atatat Exp $ */
2
3 /*-
4 * Copyright (c) 1994, 1995, 1998, 2000 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 and
9 * Thor Lancelot Simon.
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: linux_exec.c,v 1.67 2003/12/04 19:38:23 atatat 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/sa.h>
56 #include <sys/syscallargs.h>
57
58 #include <machine/cpu.h>
59 #include <machine/reg.h>
60
61 #include <compat/linux/common/linux_types.h>
62 #include <compat/linux/common/linux_signal.h>
63 #include <compat/linux/common/linux_util.h>
64 #include <compat/linux/common/linux_exec.h>
65 #include <compat/linux/common/linux_machdep.h>
66
67 #include <compat/linux/linux_syscallargs.h>
68 #include <compat/linux/linux_syscall.h>
69 #include <compat/linux/common/linux_misc.h>
70 #include <compat/linux/common/linux_errno.h>
71 #include <compat/linux/common/linux_emuldata.h>
72
73 extern struct sysent linux_sysent[];
74 extern const char * const linux_syscallnames[];
75 extern char linux_sigcode[], linux_esigcode[];
76
77 static void linux_e_proc_exec __P((struct proc *, struct exec_package *));
78 static void linux_e_proc_fork __P((struct proc *, struct proc *));
79 static void linux_e_proc_exit __P((struct proc *));
80 static void linux_e_proc_init __P((struct proc *, struct vmspace *));
81
82 /*
83 * Execve(2). Just check the alternate emulation path, and pass it on
84 * to the NetBSD execve().
85 */
86 int
87 linux_sys_execve(l, v, retval)
88 struct lwp *l;
89 void *v;
90 register_t *retval;
91 {
92 struct linux_sys_execve_args /* {
93 syscallarg(const char *) path;
94 syscallarg(char **) argv;
95 syscallarg(char **) envp;
96 } */ *uap = v;
97 struct proc *p = l->l_proc;
98 struct sys_execve_args ap;
99 caddr_t sg;
100
101 sg = stackgap_init(p, 0);
102 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
103
104 SCARG(&ap, path) = SCARG(uap, path);
105 SCARG(&ap, argp) = SCARG(uap, argp);
106 SCARG(&ap, envp) = SCARG(uap, envp);
107
108 return sys_execve(l, &ap, retval);
109 }
110
111 /*
112 * Emulation switch.
113 */
114
115 struct uvm_object *emul_linux_object;
116
117 const struct emul emul_linux = {
118 "linux",
119 "/emul/linux",
120 #ifndef __HAVE_MINIMAL_EMUL
121 0,
122 (int*)native_to_linux_errno,
123 LINUX_SYS_syscall,
124 LINUX_SYS_NSYSENT,
125 #endif
126 linux_sysent,
127 linux_syscallnames,
128 linux_sendsig,
129 linux_trapsignal,
130 NULL,
131 linux_sigcode,
132 linux_esigcode,
133 &emul_linux_object,
134 linux_setregs,
135 linux_e_proc_exec,
136 linux_e_proc_fork,
137 linux_e_proc_exit,
138 #ifdef __HAVE_SYSCALL_INTERN
139 linux_syscall_intern,
140 #else
141 #error Implement __HAVE_SYSCALL_INTERN for this platform
142 #endif
143 NULL,
144 NULL,
145 };
146
147 static void
148 linux_e_proc_init(p, vmspace)
149 struct proc *p;
150 struct vmspace *vmspace;
151 {
152 if (!p->p_emuldata) {
153 /* allocate new Linux emuldata */
154 MALLOC(p->p_emuldata, void *, sizeof(struct linux_emuldata),
155 M_EMULDATA, M_WAITOK);
156 }
157
158 memset(p->p_emuldata, '\0', sizeof(struct linux_emuldata));
159
160 /* Set the process idea of the break to the real value */
161 ((struct linux_emuldata*)(p->p_emuldata))->p_break =
162 vmspace->vm_daddr + ctob(vmspace->vm_dsize);
163 }
164
165 /*
166 * Allocate per-process structures. Called when executing Linux
167 * process. We can reuse the old emuldata - if it's not null,
168 * the executed process is of same emulation as original forked one.
169 */
170 static void
171 linux_e_proc_exec(p, epp)
172 struct proc *p;
173 struct exec_package *epp;
174 {
175 /* exec, use our vmspace */
176 linux_e_proc_init(p, p->p_vmspace);
177 }
178
179 /*
180 * Emulation per-process exit hook.
181 */
182 static void
183 linux_e_proc_exit(p)
184 struct proc *p;
185 {
186 /* free Linux emuldata and set the pointer to null */
187 FREE(p->p_emuldata, M_EMULDATA);
188 p->p_emuldata = NULL;
189 }
190
191 /*
192 * Emulation fork hook.
193 */
194 static void
195 linux_e_proc_fork(p, parent)
196 struct proc *p, *parent;
197 {
198 /*
199 * It could be desirable to copy some stuff from parent's
200 * emuldata. We don't need anything like that for now.
201 * So just allocate new emuldata for the new process.
202 */
203 p->p_emuldata = NULL;
204
205 /* fork, use parent's vmspace (our vmspace may not be setup yet) */
206 linux_e_proc_init(p, parent->p_vmspace);
207 }
208