1/*-
2 * Copyright (c) 2002 Doug Rabson
3 * Copyright (c) 2003 Peter Wemm
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD$");
30
31#include "opt_compat.h"
32
33#define __ELF_WORD_SIZE 32
34
35#include <sys/param.h>
36#include <sys/exec.h>
37#include <sys/fcntl.h>
38#include <sys/imgact.h>
39#include <sys/kernel.h>
40#include <sys/lock.h>
41#include <sys/malloc.h>
42#include <sys/mutex.h>
43#include <sys/mman.h>
44#include <sys/namei.h>
45#include <sys/pioctl.h>
46#include <sys/proc.h>
47#include <sys/procfs.h>
48#include <sys/resourcevar.h>
49#include <sys/systm.h>
50#include <sys/signalvar.h>
51#include <sys/stat.h>
52#include <sys/sx.h>
53#include <sys/syscall.h>
54#include <sys/sysctl.h>
55#include <sys/sysent.h>
56#include <sys/vnode.h>
57#include <sys/imgact_elf.h>
58
59#include <vm/vm.h>
60#include <vm/vm_kern.h>
61#include <vm/vm_param.h>
62#include <vm/pmap.h>
63#include <vm/vm_map.h>
64#include <vm/vm_object.h>
65#include <vm/vm_extern.h>
66
67#include <compat/freebsd32/freebsd32_signal.h>
68#include <compat/freebsd32/freebsd32_util.h>
69#include <compat/freebsd32/freebsd32_proto.h>
70#include <compat/freebsd32/freebsd32_syscall.h>
71#include <compat/ia32/ia32_signal.h>
72#include <machine/frame.h>
73#include <machine/md_var.h>
74#include <machine/pcb.h>
75#include <machine/cpufunc.h>
76
77CTASSERT(sizeof(struct ia32_mcontext) == 640);
78CTASSERT(sizeof(struct ia32_ucontext) == 704);
79CTASSERT(sizeof(struct ia32_sigframe) == 800);
80CTASSERT(sizeof(struct siginfo32) == 64);
81#ifdef COMPAT_FREEBSD4
82CTASSERT(sizeof(struct ia32_mcontext4) == 260);
83CTASSERT(sizeof(struct ia32_ucontext4) == 324);
84CTASSERT(sizeof(struct ia32_sigframe4) == 408);
85#endif
86
87extern const char *freebsd32_syscallnames[];
88
89static SYSCTL_NODE(_compat, OID_AUTO, ia32, CTLFLAG_RW, 0, "ia32 mode");
90
91static u_long	ia32_maxdsiz = IA32_MAXDSIZ;
92SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxdsiz, CTLFLAG_RW, &ia32_maxdsiz, 0, "");
93TUNABLE_ULONG("compat.ia32.maxdsiz", &ia32_maxdsiz);
94u_long	ia32_maxssiz = IA32_MAXSSIZ;
95SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxssiz, CTLFLAG_RW, &ia32_maxssiz, 0, "");
96TUNABLE_ULONG("compat.ia32.maxssiz", &ia32_maxssiz);
97static u_long	ia32_maxvmem = IA32_MAXVMEM;
98SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxvmem, CTLFLAG_RW, &ia32_maxvmem, 0, "");
99TUNABLE_ULONG("compat.ia32.maxvmem", &ia32_maxvmem);
100
101struct sysentvec ia32_freebsd_sysvec = {
102	.sv_size	= FREEBSD32_SYS_MAXSYSCALL,
103	.sv_table	= freebsd32_sysent,
104	.sv_mask	= 0,
105	.sv_sigsize	= 0,
106	.sv_sigtbl	= NULL,
107	.sv_errsize	= 0,
108	.sv_errtbl	= NULL,
109	.sv_transtrap	= NULL,
110	.sv_fixup	= elf32_freebsd_fixup,
111	.sv_sendsig	= ia32_sendsig,
112	.sv_sigcode	= ia32_sigcode,
113	.sv_szsigcode	= &sz_ia32_sigcode,
114	.sv_prepsyscall	= NULL,
115	.sv_name	= "FreeBSD ELF32",
116	.sv_coredump	= elf32_coredump,
117	.sv_imgact_try	= NULL,
118	.sv_minsigstksz	= MINSIGSTKSZ,
119	.sv_pagesize	= IA32_PAGE_SIZE,
120	.sv_minuser	= FREEBSD32_MINUSER,
121	.sv_maxuser	= FREEBSD32_MAXUSER,
122	.sv_usrstack	= FREEBSD32_USRSTACK,
123	.sv_psstrings	= FREEBSD32_PS_STRINGS,
124	.sv_stackprot	= VM_PROT_ALL,
125	.sv_copyout_strings	= freebsd32_copyout_strings,
126	.sv_setregs	= ia32_setregs,
127	.sv_fixlimit	= ia32_fixlimit,
128	.sv_maxssiz	= &ia32_maxssiz,
129	.sv_flags	= SV_ABI_FREEBSD | SV_IA32 | SV_ILP32 |
130#ifdef __amd64__
131		SV_SHP
132#else
133		0
134#endif
135	,
136	.sv_set_syscall_retval = ia32_set_syscall_retval,
137	.sv_fetch_syscall_args = ia32_fetch_syscall_args,
138	.sv_syscallnames = freebsd32_syscallnames,
139	.sv_shared_page_base = FREEBSD32_SHAREDPAGE,
140	.sv_shared_page_len = PAGE_SIZE,
141	.sv_schedtail	= NULL,
142};
143INIT_SYSENTVEC(elf_ia32_sysvec, &ia32_freebsd_sysvec);
144
145static Elf32_Brandinfo ia32_brand_info = {
146	.brand		= ELFOSABI_FREEBSD,
147	.machine	= EM_386,
148	.compat_3_brand	= "FreeBSD",
149	.emul_path	= NULL,
150	.interp_path	= "/libexec/ld-elf.so.1",
151	.sysvec		= &ia32_freebsd_sysvec,
152	.interp_newpath	= "/libexec/ld-elf32.so.1",
153	.brand_note	= &elf32_freebsd_brandnote,
154	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
155};
156
157SYSINIT(ia32, SI_SUB_EXEC, SI_ORDER_MIDDLE,
158	(sysinit_cfunc_t) elf32_insert_brand_entry,
159	&ia32_brand_info);
160
161static Elf32_Brandinfo ia32_brand_oinfo = {
162	.brand		= ELFOSABI_FREEBSD,
163	.machine	= EM_386,
164	.compat_3_brand	= "FreeBSD",
165	.emul_path	= NULL,
166	.interp_path	= "/usr/libexec/ld-elf.so.1",
167	.sysvec		= &ia32_freebsd_sysvec,
168	.interp_newpath	= "/libexec/ld-elf32.so.1",
169	.brand_note	= &elf32_freebsd_brandnote,
170	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
171};
172
173SYSINIT(oia32, SI_SUB_EXEC, SI_ORDER_ANY,
174	(sysinit_cfunc_t) elf32_insert_brand_entry,
175	&ia32_brand_oinfo);
176
177static Elf32_Brandinfo kia32_brand_info = {
178	.brand		= ELFOSABI_FREEBSD,
179	.machine	= EM_386,
180	.compat_3_brand	= "FreeBSD",
181	.emul_path	= NULL,
182	.interp_path	= "/lib/ld.so.1",
183	.sysvec		= &ia32_freebsd_sysvec,
184	.brand_note	= &elf32_kfreebsd_brandnote,
185	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE_MANDATORY
186};
187
188SYSINIT(kia32, SI_SUB_EXEC, SI_ORDER_ANY,
189	(sysinit_cfunc_t) elf32_insert_brand_entry,
190	&kia32_brand_info);
191
192void
193elf32_dump_thread(struct thread *td __unused, void *dst __unused,
194    size_t *off __unused)
195{
196}
197
198void
199ia32_fixlimit(struct rlimit *rl, int which)
200{
201
202	switch (which) {
203	case RLIMIT_DATA:
204		if (ia32_maxdsiz != 0) {
205			if (rl->rlim_cur > ia32_maxdsiz)
206				rl->rlim_cur = ia32_maxdsiz;
207			if (rl->rlim_max > ia32_maxdsiz)
208				rl->rlim_max = ia32_maxdsiz;
209		}
210		break;
211	case RLIMIT_STACK:
212		if (ia32_maxssiz != 0) {
213			if (rl->rlim_cur > ia32_maxssiz)
214				rl->rlim_cur = ia32_maxssiz;
215			if (rl->rlim_max > ia32_maxssiz)
216				rl->rlim_max = ia32_maxssiz;
217		}
218		break;
219	case RLIMIT_VMEM:
220		if (ia32_maxvmem != 0) {
221			if (rl->rlim_cur > ia32_maxvmem)
222				rl->rlim_cur = ia32_maxvmem;
223			if (rl->rlim_max > ia32_maxvmem)
224				rl->rlim_max = ia32_maxvmem;
225		}
226		break;
227	}
228}
229