linux_exec.c revision 1.43 1 /* $NetBSD: linux_exec.c,v 1.43 2000/12/09 12:38:24 jdolecek Exp $ */
2
3 /*-
4 * Copyright (c) 1994, 1995, 1998 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/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/proc.h>
44 #include <sys/malloc.h>
45 #include <sys/namei.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/exec.h>
49 #include <sys/exec_elf.h>
50
51 #include <sys/mman.h>
52 #include <sys/syscallargs.h>
53
54 #include <machine/cpu.h>
55 #include <machine/reg.h>
56
57 #include <compat/linux/common/linux_types.h>
58 #include <compat/linux/common/linux_signal.h>
59 #include <compat/linux/common/linux_util.h>
60 #include <compat/linux/common/linux_exec.h>
61 #include <compat/linux/common/linux_machdep.h>
62
63 #include <compat/linux/linux_syscallargs.h>
64 #include <compat/linux/linux_syscall.h>
65 #include <compat/linux/common/linux_misc.h>
66 #include <compat/linux/common/linux_errno.h>
67 #include <compat/linux/common/linux_emuldata.h>
68
69 extern struct sysent linux_sysent[];
70 extern const char * const linux_syscallnames[];
71 extern char linux_sigcode[], linux_esigcode[];
72 void linux_syscall __P((void));
73 void syscall __P((void));
74
75 static void linux_e_proc_exec __P((struct proc *, struct exec_package *));
76 static void linux_e_proc_fork __P((struct proc *, struct proc *));
77 static void linux_e_proc_exit __P((struct proc *));
78
79 /*
80 * Execve(2). Just check the alternate emulation path, and pass it on
81 * to the NetBSD execve().
82 */
83 int
84 linux_sys_execve(p, v, retval)
85 struct proc *p;
86 void *v;
87 register_t *retval;
88 {
89 struct linux_sys_execve_args /* {
90 syscallarg(const char *) path;
91 syscallarg(char **) argv;
92 syscallarg(char **) envp;
93 } */ *uap = v;
94 struct sys_execve_args ap;
95 caddr_t sg;
96
97 sg = stackgap_init(p->p_emul);
98 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
99
100 SCARG(&ap, path) = SCARG(uap, path);
101 SCARG(&ap, argp) = SCARG(uap, argp);
102 SCARG(&ap, envp) = SCARG(uap, envp);
103
104 return sys_execve(p, &ap, retval);
105 }
106
107 /*
108 * Emulation switch.
109 */
110 const struct emul emul_linux = {
111 "linux",
112 "/emul/linux",
113 native_to_linux_errno,
114 linux_sendsig,
115 LINUX_SYS_syscall,
116 LINUX_SYS_MAXSYSCALL,
117 linux_sysent,
118 linux_syscallnames,
119 linux_sigcode,
120 linux_esigcode,
121 linux_e_proc_exec,
122 linux_e_proc_fork,
123 linux_e_proc_exit,
124 #ifdef LINUX_MACHDEP_PASS_PPID_AND_EUID
125 EMUL_GETPID_PASS_PPID|EMUL_GETID_PASS_EID,
126 #else
127 0,
128 #endif
129 #ifdef LINUX_MACHDEP_HAS_SEPARATED_SYSCALL
130 linux_syscall,
131 #else
132 syscall,
133 #endif
134 };
135
136 /*
137 * Allocate per-process structures. Called when executing Linux
138 * process. We can re-used the old emuldata - if it's not null,
139 * the executed process is of same emulation as original forked one.
140 */
141 static void
142 linux_e_proc_exec(p, epp)
143 struct proc *p;
144 struct exec_package *epp;
145 {
146 if (!p->p_emuldata) {
147 /* allocate new Linux emuldata */
148 MALLOC(p->p_emuldata, void *, sizeof(struct linux_emuldata),
149 M_EMULDATA, M_WAITOK);
150 }
151
152 memset(p->p_emuldata, '\0', sizeof(struct linux_emuldata));
153 }
154
155 /*
156 * Emulation per-process exit hook.
157 */
158 static void
159 linux_e_proc_exit(p)
160 struct proc *p;
161 {
162 /* free Linux emuldata and set the pointer to null */
163 FREE(p->p_emuldata, M_EMULDATA);
164 p->p_emuldata = NULL;
165 }
166
167 /*
168 * Emulation fork hook.
169 */
170 static void
171 linux_e_proc_fork(p, parent)
172 struct proc *p, *parent;
173 {
174 /*
175 * It could be desirable to copy some stuff from parent's
176 * emuldata. We don't need anything like that for now.
177 * So just allocate new emuldata for the new process.
178 */
179 p->p_emuldata = NULL;
180 linux_e_proc_exec(p, NULL);
181 }
182