ia32_sysvec.c revision 227309
11590Srgrimes/*-
260059Sobrien * Copyright (c) 2002 Doug Rabson
31590Srgrimes * Copyright (c) 2003 Peter Wemm
41590Srgrimes * All rights reserved.
51590Srgrimes *
61590Srgrimes * 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: head/sys/compat/ia32/ia32_sysvec.c 227309 2011-11-07 15:43:11Z ed $");
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#ifdef __amd64__
73#include <machine/psl.h>
74#include <machine/segments.h>
75#include <machine/specialreg.h>
76#else
77#include <i386/include/psl.h>
78#include <i386/include/segments.h>
79#include <i386/include/specialreg.h>
80#endif
81#include <machine/frame.h>
82#include <machine/md_var.h>
83#include <machine/pcb.h>
84#include <machine/cpufunc.h>
85
86CTASSERT(sizeof(struct ia32_mcontext) == 640);
87CTASSERT(sizeof(struct ia32_ucontext) == 704);
88CTASSERT(sizeof(struct ia32_sigframe) == 800);
89CTASSERT(sizeof(struct siginfo32) == 64);
90#ifdef COMPAT_FREEBSD4
91CTASSERT(sizeof(struct ia32_mcontext4) == 260);
92CTASSERT(sizeof(struct ia32_ucontext4) == 324);
93CTASSERT(sizeof(struct ia32_sigframe4) == 408);
94#endif
95
96extern const char *freebsd32_syscallnames[];
97
98static SYSCTL_NODE(_compat, OID_AUTO, ia32, CTLFLAG_RW, 0, "ia32 mode");
99
100static u_long	ia32_maxdsiz = IA32_MAXDSIZ;
101SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxdsiz, CTLFLAG_RW, &ia32_maxdsiz, 0, "");
102TUNABLE_ULONG("compat.ia32.maxdsiz", &ia32_maxdsiz);
103u_long	ia32_maxssiz = IA32_MAXSSIZ;
104SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxssiz, CTLFLAG_RW, &ia32_maxssiz, 0, "");
105TUNABLE_ULONG("compat.ia32.maxssiz", &ia32_maxssiz);
106static u_long	ia32_maxvmem = IA32_MAXVMEM;
107SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxvmem, CTLFLAG_RW, &ia32_maxvmem, 0, "");
108TUNABLE_ULONG("compat.ia32.maxvmem", &ia32_maxvmem);
109
110struct sysentvec ia32_freebsd_sysvec = {
111	.sv_size	= FREEBSD32_SYS_MAXSYSCALL,
112	.sv_table	= freebsd32_sysent,
113	.sv_mask	= 0,
114	.sv_sigsize	= 0,
115	.sv_sigtbl	= NULL,
116	.sv_errsize	= 0,
117	.sv_errtbl	= NULL,
118	.sv_transtrap	= NULL,
119	.sv_fixup	= elf32_freebsd_fixup,
120	.sv_sendsig	= ia32_sendsig,
121	.sv_sigcode	= ia32_sigcode,
122	.sv_szsigcode	= &sz_ia32_sigcode,
123	.sv_prepsyscall	= NULL,
124	.sv_name	= "FreeBSD ELF32",
125	.sv_coredump	= elf32_coredump,
126	.sv_imgact_try	= NULL,
127	.sv_minsigstksz	= MINSIGSTKSZ,
128	.sv_pagesize	= IA32_PAGE_SIZE,
129	.sv_minuser	= 0,
130	.sv_maxuser	= FREEBSD32_MAXUSER,
131	.sv_usrstack	= FREEBSD32_USRSTACK,
132	.sv_psstrings	= FREEBSD32_PS_STRINGS,
133	.sv_stackprot	= VM_PROT_ALL,
134	.sv_copyout_strings	= freebsd32_copyout_strings,
135	.sv_setregs	= ia32_setregs,
136	.sv_fixlimit	= ia32_fixlimit,
137	.sv_maxssiz	= &ia32_maxssiz,
138	.sv_flags	= SV_ABI_FREEBSD | SV_IA32 | SV_ILP32 |
139#ifdef __amd64__
140		SV_SHP
141#else
142		0
143#endif
144	,
145	.sv_set_syscall_retval = ia32_set_syscall_retval,
146	.sv_fetch_syscall_args = ia32_fetch_syscall_args,
147	.sv_syscallnames = freebsd32_syscallnames,
148	.sv_shared_page_base = FREEBSD32_SHAREDPAGE,
149	.sv_shared_page_len = PAGE_SIZE,
150	.sv_schedtail	= NULL,
151};
152INIT_SYSENTVEC(elf_ia32_sysvec, &ia32_freebsd_sysvec);
153
154static Elf32_Brandinfo ia32_brand_info = {
155	.brand		= ELFOSABI_FREEBSD,
156	.machine	= EM_386,
157	.compat_3_brand	= "FreeBSD",
158	.emul_path	= NULL,
159	.interp_path	= "/libexec/ld-elf.so.1",
160	.sysvec		= &ia32_freebsd_sysvec,
161	.interp_newpath	= "/libexec/ld-elf32.so.1",
162	.brand_note	= &elf32_freebsd_brandnote,
163	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
164};
165
166SYSINIT(ia32, SI_SUB_EXEC, SI_ORDER_MIDDLE,
167	(sysinit_cfunc_t) elf32_insert_brand_entry,
168	&ia32_brand_info);
169
170static Elf32_Brandinfo ia32_brand_oinfo = {
171	.brand		= ELFOSABI_FREEBSD,
172	.machine	= EM_386,
173	.compat_3_brand	= "FreeBSD",
174	.emul_path	= NULL,
175	.interp_path	= "/usr/libexec/ld-elf.so.1",
176	.sysvec		= &ia32_freebsd_sysvec,
177	.interp_newpath	= "/libexec/ld-elf32.so.1",
178	.brand_note	= &elf32_freebsd_brandnote,
179	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
180};
181
182SYSINIT(oia32, SI_SUB_EXEC, SI_ORDER_ANY,
183	(sysinit_cfunc_t) elf32_insert_brand_entry,
184	&ia32_brand_oinfo);
185
186static Elf32_Brandinfo kia32_brand_info = {
187	.brand		= ELFOSABI_FREEBSD,
188	.machine	= EM_386,
189	.compat_3_brand	= "FreeBSD",
190	.emul_path	= NULL,
191	.interp_path	= "/lib/ld.so.1",
192	.sysvec		= &ia32_freebsd_sysvec,
193	.brand_note	= &elf32_kfreebsd_brandnote,
194	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE_MANDATORY
195};
196
197SYSINIT(kia32, SI_SUB_EXEC, SI_ORDER_ANY,
198	(sysinit_cfunc_t) elf32_insert_brand_entry,
199	&kia32_brand_info);
200
201void
202elf32_dump_thread(struct thread *td __unused, void *dst __unused,
203    size_t *off __unused)
204{
205}
206
207void
208ia32_fixlimit(struct rlimit *rl, int which)
209{
210
211	switch (which) {
212	case RLIMIT_DATA:
213		if (ia32_maxdsiz != 0) {
214			if (rl->rlim_cur > ia32_maxdsiz)
215				rl->rlim_cur = ia32_maxdsiz;
216			if (rl->rlim_max > ia32_maxdsiz)
217				rl->rlim_max = ia32_maxdsiz;
218		}
219		break;
220	case RLIMIT_STACK:
221		if (ia32_maxssiz != 0) {
222			if (rl->rlim_cur > ia32_maxssiz)
223				rl->rlim_cur = ia32_maxssiz;
224			if (rl->rlim_max > ia32_maxssiz)
225				rl->rlim_max = ia32_maxssiz;
226		}
227		break;
228	case RLIMIT_VMEM:
229		if (ia32_maxvmem != 0) {
230			if (rl->rlim_cur > ia32_maxvmem)
231				rl->rlim_cur = ia32_maxvmem;
232			if (rl->rlim_max > ia32_maxvmem)
233				rl->rlim_max = ia32_maxvmem;
234		}
235		break;
236	}
237}
238