svr4_sysvec.c revision 177997
1/*-
2 * Copyright (c) 1998 Mark Newton
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
18 *
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.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/sys/compat/svr4/svr4_sysvec.c 177997 2008-04-08 09:45:49Z kib $");
33
34/* XXX we use functions that might not exist. */
35#include "opt_compat.h"
36
37#ifndef COMPAT_43
38#error "Unable to compile SVR4-emulator due to missing COMPAT_43 option!"
39#endif
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/proc.h>
44#include <sys/sysent.h>
45#include <sys/imgact.h>
46#include <sys/imgact_elf.h>
47#include <sys/fcntl.h>
48#include <sys/lock.h>
49#include <sys/malloc.h>
50#include <sys/module.h>
51#include <sys/mutex.h>
52#include <sys/namei.h>
53#include <sys/socket.h>
54#include <sys/syscallsubr.h>
55#include <sys/vnode.h>
56#include <vm/vm.h>
57#include <sys/exec.h>
58#include <sys/kernel.h>
59#include <machine/cpu.h>
60#include <netinet/in.h>
61
62#include <compat/svr4/svr4.h>
63#include <compat/svr4/svr4_types.h>
64#include <compat/svr4/svr4_syscall.h>
65#include <compat/svr4/svr4_signal.h>
66#include <compat/svr4/svr4_socket.h>
67#include <compat/svr4/svr4_sockio.h>
68#include <compat/svr4/svr4_errno.h>
69#include <compat/svr4/svr4_proto.h>
70#include <compat/svr4/svr4_siginfo.h>
71#include <compat/svr4/svr4_util.h>
72
73int bsd_to_svr4_errno[ELAST+1] = {
74        0,
75        SVR4_EPERM,
76        SVR4_ENOENT,
77        SVR4_ESRCH,
78        SVR4_EINTR,
79        SVR4_EIO,
80        SVR4_ENXIO,
81        SVR4_E2BIG,
82        SVR4_ENOEXEC,
83        SVR4_EBADF,
84        SVR4_ECHILD,
85        SVR4_EDEADLK,
86        SVR4_ENOMEM,
87        SVR4_EACCES,
88        SVR4_EFAULT,
89        SVR4_ENOTBLK,
90        SVR4_EBUSY,
91        SVR4_EEXIST,
92        SVR4_EXDEV,
93        SVR4_ENODEV,
94        SVR4_ENOTDIR,
95        SVR4_EISDIR,
96        SVR4_EINVAL,
97        SVR4_ENFILE,
98        SVR4_EMFILE,
99        SVR4_ENOTTY,
100        SVR4_ETXTBSY,
101        SVR4_EFBIG,
102        SVR4_ENOSPC,
103        SVR4_ESPIPE,
104        SVR4_EROFS,
105        SVR4_EMLINK,
106        SVR4_EPIPE,
107        SVR4_EDOM,
108        SVR4_ERANGE,
109        SVR4_EAGAIN,
110        SVR4_EINPROGRESS,
111        SVR4_EALREADY,
112        SVR4_ENOTSOCK,
113        SVR4_EDESTADDRREQ,
114        SVR4_EMSGSIZE,
115        SVR4_EPROTOTYPE,
116        SVR4_ENOPROTOOPT,
117        SVR4_EPROTONOSUPPORT,
118        SVR4_ESOCKTNOSUPPORT,
119        SVR4_EOPNOTSUPP,
120        SVR4_EPFNOSUPPORT,
121        SVR4_EAFNOSUPPORT,
122        SVR4_EADDRINUSE,
123        SVR4_EADDRNOTAVAIL,
124        SVR4_ENETDOWN,
125        SVR4_ENETUNREACH,
126        SVR4_ENETRESET,
127        SVR4_ECONNABORTED,
128        SVR4_ECONNRESET,
129        SVR4_ENOBUFS,
130        SVR4_EISCONN,
131        SVR4_ENOTCONN,
132        SVR4_ESHUTDOWN,
133        SVR4_ETOOMANYREFS,
134        SVR4_ETIMEDOUT,
135        SVR4_ECONNREFUSED,
136        SVR4_ELOOP,
137        SVR4_ENAMETOOLONG,
138        SVR4_EHOSTDOWN,
139        SVR4_EHOSTUNREACH,
140        SVR4_ENOTEMPTY,
141        SVR4_EPROCLIM,
142        SVR4_EUSERS,
143        SVR4_EDQUOT,
144        SVR4_ESTALE,
145        SVR4_EREMOTE,
146        SVR4_EBADRPC,
147        SVR4_ERPCMISMATCH,
148        SVR4_EPROGUNAVAIL,
149        SVR4_EPROGMISMATCH,
150        SVR4_EPROCUNAVAIL,
151        SVR4_ENOLCK,
152        SVR4_ENOSYS,
153        SVR4_EFTYPE,
154        SVR4_EAUTH,
155        SVR4_ENEEDAUTH,
156        SVR4_EIDRM,
157        SVR4_ENOMSG,
158};
159
160
161static int 	svr4_fixup(register_t **stack_base, struct image_params *imgp);
162
163extern struct sysent svr4_sysent[];
164#undef szsigcode
165#undef sigcode
166
167extern int svr4_szsigcode;
168extern char svr4_sigcode[];
169
170struct sysentvec svr4_sysvec = {
171  SVR4_SYS_MAXSYSCALL,
172  svr4_sysent,
173  0xff,
174  SVR4_NSIG-1,		/* NB: signal trans table indexed with signno-1 */
175  bsd_to_svr4_sig+1,
176  ELAST,  /* ELAST */
177  bsd_to_svr4_errno,
178  NULL,
179  svr4_fixup,
180  svr4_sendsig,
181  svr4_sigcode,
182  &svr4_szsigcode,
183  NULL,
184  "SVR4",
185  elf32_coredump,
186  NULL,
187  SVR4_MINSIGSTKSZ,
188  PAGE_SIZE,
189  VM_MIN_ADDRESS,
190  VM_MAXUSER_ADDRESS,
191  USRSTACK,
192  PS_STRINGS,
193  VM_PROT_ALL,
194  exec_copyout_strings,
195  exec_setregs,
196  NULL
197};
198
199const char      svr4_emul_path[] = "/compat/svr4";
200
201Elf32_Brandinfo svr4_brand = {
202  ELFOSABI_SYSV,
203  EM_386,			/* XXX only implemented for x86 so far. */
204  "SVR4",
205  svr4_emul_path,
206  "/lib/libc.so.1",
207  &svr4_sysvec,
208  NULL,
209};
210
211static int
212svr4_fixup(register_t **stack_base, struct image_params *imgp)
213{
214	Elf32_Auxargs *args;
215	register_t *pos;
216
217	KASSERT(curthread->td_proc == imgp->proc,
218	    ("unsafe svr4_fixup(), should be curproc"));
219	args = (Elf32_Auxargs *)imgp->auxargs;
220	pos = *stack_base + (imgp->args->argc + imgp->args->envc + 2);
221
222	if (args->trace)
223		AUXARGS_ENTRY(pos, AT_DEBUG, 1);
224	if (args->execfd != -1)
225		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
226	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
227	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
228	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
229	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
230	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
231	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
232	AUXARGS_ENTRY(pos, AT_BASE, args->base);
233	AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
234	AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
235	AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
236	AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
237	AUXARGS_ENTRY(pos, AT_NULL, 0);
238
239	free(imgp->auxargs, M_TEMP);
240	imgp->auxargs = NULL;
241
242	(*stack_base)--;
243	**stack_base = (register_t)imgp->args->argc;
244	return 0;
245}
246
247/*
248 * Search an alternate path before passing pathname arguments on
249 * to system calls. Useful for keeping a separate 'emulation tree'.
250 *
251 * If cflag is set, we check if an attempt can be made to create
252 * the named file, i.e. we check if the directory it should
253 * be in exists.
254 */
255int
256svr4_emul_find(struct thread *td, char *path, enum uio_seg pathseg,
257    char **pbuf, int create)
258{
259
260	return (kern_alternate_path(td, svr4_emul_path, path, pathseg, pbuf,
261	    create, AT_FDCWD));
262}
263
264static int
265svr4_elf_modevent(module_t mod, int type, void *data)
266{
267	int error;
268
269	error = 0;
270
271	switch(type) {
272	case MOD_LOAD:
273		if (elf32_insert_brand_entry(&svr4_brand) < 0) {
274			printf("cannot insert svr4 elf brand handler\n");
275			error = EINVAL;
276			break;
277		}
278		if (bootverbose)
279			printf("svr4 ELF exec handler installed\n");
280		svr4_sockcache_init();
281		break;
282	case MOD_UNLOAD:
283		/* Only allow the emulator to be removed if it isn't in use. */
284		if (elf32_brand_inuse(&svr4_brand) != 0) {
285			error = EBUSY;
286		} else if (elf32_remove_brand_entry(&svr4_brand) < 0) {
287			error = EINVAL;
288		}
289
290		if (error) {
291			printf("Could not deinstall ELF interpreter entry (error %d)\n",
292			       error);
293			break;
294		}
295		if (bootverbose)
296			printf("svr4 ELF exec handler removed\n");
297		svr4_sockcache_destroy();
298		break;
299	default:
300		return (EOPNOTSUPP);
301		break;
302	}
303	return error;
304}
305
306static moduledata_t svr4_elf_mod = {
307	"svr4elf",
308	svr4_elf_modevent,
309	0
310};
311DECLARE_MODULE(svr4elf, svr4_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
312MODULE_DEPEND(svr4elf, streams, 1, 1, 1);
313