2 * Copyright (c) 1998 Mark Newton
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Christos Zoulas.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 /* XXX we use functions that might not exist. */
35 #include "opt_compat.h"
38 #error "Unable to compile SVR4-emulator due to missing COMPAT_43 option!"
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/sysent.h>
45 #include <sys/imgact.h>
46 #include <sys/imgact_elf.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #include <sys/mutex.h>
51 #include <sys/namei.h>
52 #include <sys/socket.h>
53 #include <sys/syscallsubr.h>
54 #include <sys/vnode.h>
57 #include <sys/kernel.h>
58 #include <machine/cpu.h>
59 #include <netinet/in.h>
61 #include <compat/svr4/svr4.h>
62 #include <compat/svr4/svr4_types.h>
63 #include <compat/svr4/svr4_syscall.h>
64 #include <compat/svr4/svr4_signal.h>
65 #include <compat/svr4/svr4_socket.h>
66 #include <compat/svr4/svr4_sockio.h>
67 #include <compat/svr4/svr4_errno.h>
68 #include <compat/svr4/svr4_proto.h>
69 #include <compat/svr4/svr4_siginfo.h>
70 #include <compat/svr4/svr4_util.h>
72 int bsd_to_svr4_errno[ELAST+1] = {
116 SVR4_EPROTONOSUPPORT,
117 SVR4_ESOCKTNOSUPPORT,
160 static int svr4_fixup(register_t **stack_base, struct image_params *imgp);
162 extern struct sysent svr4_sysent[];
166 extern int svr4_szsigcode;
167 extern char svr4_sigcode[];
169 struct sysentvec svr4_sysvec = {
170 .sv_size = SVR4_SYS_MAXSYSCALL,
171 .sv_table = svr4_sysent,
173 .sv_sigsize = SVR4_NSIG-1, /* NB: signal trans table indexed with signno-1 */
174 .sv_sigtbl = bsd_to_svr4_sig+1,
175 .sv_errsize = ELAST, /* ELAST */
176 .sv_errtbl = bsd_to_svr4_errno,
177 .sv_transtrap = NULL,
178 .sv_fixup = svr4_fixup,
179 .sv_sendsig = svr4_sendsig,
180 .sv_sigcode = svr4_sigcode,
181 .sv_szsigcode = &svr4_szsigcode,
182 .sv_prepsyscall = NULL,
184 .sv_coredump = elf32_coredump,
185 .sv_imgact_try = NULL,
186 .sv_minsigstksz = SVR4_MINSIGSTKSZ,
187 .sv_pagesize = PAGE_SIZE,
188 .sv_minuser = VM_MIN_ADDRESS,
189 .sv_maxuser = VM_MAXUSER_ADDRESS,
190 .sv_usrstack = USRSTACK,
191 .sv_psstrings = PS_STRINGS,
192 .sv_stackprot = VM_PROT_ALL,
193 .sv_copyout_strings = exec_copyout_strings,
194 .sv_setregs = exec_setregs,
199 const char svr4_emul_path[] = "/compat/svr4";
201 Elf32_Brandinfo svr4_brand = {
202 .brand = ELFOSABI_SYSV,
203 .machine = EM_386, /* XXX only implemented for x86 so far. */
204 .compat_3_brand = "SVR4",
205 .emul_path = svr4_emul_path,
206 .interp_path = "/lib/libc.so.1",
207 .sysvec = &svr4_sysvec,
208 .interp_newpath = NULL,
214 svr4_fixup(register_t **stack_base, struct image_params *imgp)
219 KASSERT(curthread->td_proc == imgp->proc &&
220 (curthread->td_proc->p_flag & P_SA) == 0,
221 ("unsafe svr4_fixup(), should be curproc"));
222 args = (Elf32_Auxargs *)imgp->auxargs;
223 pos = *stack_base + (imgp->args->argc + imgp->args->envc + 2);
226 AUXARGS_ENTRY(pos, AT_DEBUG, 1);
227 if (args->execfd != -1)
228 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
229 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
230 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
231 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
232 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
233 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
234 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
235 AUXARGS_ENTRY(pos, AT_BASE, args->base);
236 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
237 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
238 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
239 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
240 AUXARGS_ENTRY(pos, AT_NULL, 0);
242 free(imgp->auxargs, M_TEMP);
243 imgp->auxargs = NULL;
246 **stack_base = (register_t)imgp->args->argc;
251 * Search an alternate path before passing pathname arguments on
252 * to system calls. Useful for keeping a separate 'emulation tree'.
254 * If cflag is set, we check if an attempt can be made to create
255 * the named file, i.e. we check if the directory it should
259 svr4_emul_find(struct thread *td, char *path, enum uio_seg pathseg,
260 char **pbuf, int create)
263 return (kern_alternate_path(td, svr4_emul_path, path, pathseg, pbuf,
268 svr4_elf_modevent(module_t mod, int type, void *data)
276 if (elf32_insert_brand_entry(&svr4_brand) < 0) {
277 printf("cannot insert svr4 elf brand handler\n");
282 printf("svr4 ELF exec handler installed\n");
283 svr4_sockcache_init();
286 /* Only allow the emulator to be removed if it isn't in use. */
287 if (elf32_brand_inuse(&svr4_brand) != 0) {
289 } else if (elf32_remove_brand_entry(&svr4_brand) < 0) {
294 printf("Could not deinstall ELF interpreter entry (error %d)\n",
299 printf("svr4 ELF exec handler removed\n");
300 svr4_sockcache_destroy();
309 static moduledata_t svr4_elf_mod = {
314 DECLARE_MODULE(svr4elf, svr4_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
315 MODULE_DEPEND(svr4elf, streams, 1, 1, 1);