ia32_sysvec.c revision 267992
1184902Srwatson/*-
2184902Srwatson * Copyright (c) 2002 Doug Rabson
3196031Srwatson * Copyright (c) 2003 Peter Wemm
4196031Srwatson * All rights reserved.
5196031Srwatson *
6196031Srwatson * Redistribution and use in source and binary forms, with or without
7196031Srwatson * modification, are permitted provided that the following conditions
8196031Srwatson * are met:
9195740Srwatson * 1. Redistributions of source code must retain the above copyright
10195740Srwatson *    notice, this list of conditions and the following disclaimer.
11195740Srwatson * 2. Redistributions in binary form must reproduce the above copyright
12195740Srwatson *    notice, this list of conditions and the following disclaimer in the
13195740Srwatson *    documentation and/or other materials provided with the distribution.
14195740Srwatson *
15195740Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16195740Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17195740Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18191273Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19191273Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20191273Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21191273Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22191273Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23191273Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24191273Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25191273Srwatson * SUCH DAMAGE.
26191273Srwatson */
27191273Srwatson
28191273Srwatson#include <sys/cdefs.h>
29191273Srwatson__FBSDID("$FreeBSD: head/sys/compat/ia32/ia32_sysvec.c 267992 2014-06-28 03:56:17Z hselasky $");
30191273Srwatson
31191273Srwatson#include "opt_compat.h"
32191273Srwatson
33191273Srwatson#define __ELF_WORD_SIZE 32
34191273Srwatson
35191273Srwatson#include <sys/param.h>
36189279Srwatson#include <sys/exec.h>
37189279Srwatson#include <sys/fcntl.h>
38189279Srwatson#include <sys/imgact.h>
39189279Srwatson#include <sys/kernel.h>
40189279Srwatson#include <sys/lock.h>
41189279Srwatson#include <sys/malloc.h>
42189279Srwatson#include <sys/mutex.h>
43189279Srwatson#include <sys/mman.h>
44189279Srwatson#include <sys/namei.h>
45189279Srwatson#include <sys/pioctl.h>
46189279Srwatson#include <sys/proc.h>
47189279Srwatson#include <sys/procfs.h>
48189279Srwatson#include <sys/resourcevar.h>
49189279Srwatson#include <sys/systm.h>
50189279Srwatson#include <sys/signalvar.h>
51189279Srwatson#include <sys/stat.h>
52189279Srwatson#include <sys/sx.h>
53189279Srwatson#include <sys/syscall.h>
54189279Srwatson#include <sys/sysctl.h>
55187214Srwatson#include <sys/sysent.h>
56187214Srwatson#include <sys/vnode.h>
57187214Srwatson#include <sys/imgact_elf.h>
58187214Srwatson
59187214Srwatson#include <vm/vm.h>
60187214Srwatson#include <vm/vm_kern.h>
61187214Srwatson#include <vm/vm_param.h>
62187214Srwatson#include <vm/pmap.h>
63187214Srwatson#include <vm/vm_map.h>
64187214Srwatson#include <vm/vm_object.h>
65187214Srwatson#include <vm/vm_extern.h>
66187214Srwatson
67187214Srwatson#include <compat/freebsd32/freebsd32_signal.h>
68187214Srwatson#include <compat/freebsd32/freebsd32_util.h>
69187214Srwatson#include <compat/freebsd32/freebsd32_proto.h>
70187214Srwatson#include <compat/freebsd32/freebsd32_syscall.h>
71187214Srwatson#include <compat/ia32/ia32_signal.h>
72187214Srwatson#include <machine/frame.h>
73187214Srwatson#include <machine/md_var.h>
74186647Srwatson#include <machine/pcb.h>
75186647Srwatson#include <machine/cpufunc.h>
76186647Srwatson
77186647SrwatsonCTASSERT(sizeof(struct ia32_mcontext) == 640);
78186647SrwatsonCTASSERT(sizeof(struct ia32_ucontext) == 704);
79186647SrwatsonCTASSERT(sizeof(struct ia32_sigframe) == 800);
80186647SrwatsonCTASSERT(sizeof(struct siginfo32) == 64);
81186647Srwatson#ifdef COMPAT_FREEBSD4
82186647SrwatsonCTASSERT(sizeof(struct ia32_mcontext4) == 260);
83186647SrwatsonCTASSERT(sizeof(struct ia32_ucontext4) == 324);
84186647SrwatsonCTASSERT(sizeof(struct ia32_sigframe4) == 408);
85186647Srwatson#endif
86186647Srwatson
87186647Srwatsonextern const char *freebsd32_syscallnames[];
88186647Srwatson
89186647Srwatsonstatic SYSCTL_NODE(_compat, OID_AUTO, ia32, CTLFLAG_RW, 0, "ia32 mode");
90186647Srwatson
91186647Srwatsonstatic u_long	ia32_maxdsiz = IA32_MAXDSIZ;
92186647SrwatsonSYSCTL_ULONG(_compat_ia32, OID_AUTO, maxdsiz, CTLFLAG_RWTUN, &ia32_maxdsiz, 0, "");
93186647Srwatsonu_long	ia32_maxssiz = IA32_MAXSSIZ;
94186647SrwatsonSYSCTL_ULONG(_compat_ia32, OID_AUTO, maxssiz, CTLFLAG_RWTUN, &ia32_maxssiz, 0, "");
95186647Srwatsonstatic u_long	ia32_maxvmem = IA32_MAXVMEM;
96186647SrwatsonSYSCTL_ULONG(_compat_ia32, OID_AUTO, maxvmem, CTLFLAG_RWTUN, &ia32_maxvmem, 0, "");
97186647Srwatson
98186647Srwatsonstruct sysentvec ia32_freebsd_sysvec = {
99186647Srwatson	.sv_size	= FREEBSD32_SYS_MAXSYSCALL,
100186647Srwatson	.sv_table	= freebsd32_sysent,
101186647Srwatson	.sv_mask	= 0,
102186647Srwatson	.sv_sigsize	= 0,
103186647Srwatson	.sv_sigtbl	= NULL,
104186647Srwatson	.sv_errsize	= 0,
105186647Srwatson	.sv_errtbl	= NULL,
106186647Srwatson	.sv_transtrap	= NULL,
107186647Srwatson	.sv_fixup	= elf32_freebsd_fixup,
108186647Srwatson	.sv_sendsig	= ia32_sendsig,
109186647Srwatson	.sv_sigcode	= ia32_sigcode,
110186647Srwatson	.sv_szsigcode	= &sz_ia32_sigcode,
111186647Srwatson	.sv_prepsyscall	= NULL,
112186647Srwatson	.sv_name	= "FreeBSD ELF32",
113186647Srwatson	.sv_coredump	= elf32_coredump,
114186647Srwatson	.sv_imgact_try	= NULL,
115186647Srwatson	.sv_minsigstksz	= MINSIGSTKSZ,
116186647Srwatson	.sv_pagesize	= IA32_PAGE_SIZE,
117186647Srwatson	.sv_minuser	= FREEBSD32_MINUSER,
118186647Srwatson	.sv_maxuser	= FREEBSD32_MAXUSER,
119184902Srwatson	.sv_usrstack	= FREEBSD32_USRSTACK,
120184902Srwatson	.sv_psstrings	= FREEBSD32_PS_STRINGS,
121184902Srwatson	.sv_stackprot	= VM_PROT_ALL,
122184902Srwatson	.sv_copyout_strings	= freebsd32_copyout_strings,
123184902Srwatson	.sv_setregs	= ia32_setregs,
124184902Srwatson	.sv_fixlimit	= ia32_fixlimit,
125184902Srwatson	.sv_maxssiz	= &ia32_maxssiz,
126184902Srwatson	.sv_flags	= SV_ABI_FREEBSD | SV_IA32 | SV_ILP32 |
127184902Srwatson#ifdef __amd64__
128184902Srwatson		SV_SHP
129184902Srwatson#else
130184902Srwatson		0
131184902Srwatson#endif
132184902Srwatson	,
133184902Srwatson	.sv_set_syscall_retval = ia32_set_syscall_retval,
134184902Srwatson	.sv_fetch_syscall_args = ia32_fetch_syscall_args,
135184902Srwatson	.sv_syscallnames = freebsd32_syscallnames,
136184902Srwatson	.sv_shared_page_base = FREEBSD32_SHAREDPAGE,
137184902Srwatson	.sv_shared_page_len = PAGE_SIZE,
138184902Srwatson	.sv_schedtail	= NULL,
139184902Srwatson};
140184902SrwatsonINIT_SYSENTVEC(elf_ia32_sysvec, &ia32_freebsd_sysvec);
141184902Srwatson
142184902Srwatsonstatic Elf32_Brandinfo ia32_brand_info = {
143184902Srwatson	.brand		= ELFOSABI_FREEBSD,
144184902Srwatson	.machine	= EM_386,
145184902Srwatson	.compat_3_brand	= "FreeBSD",
146184902Srwatson	.emul_path	= NULL,
147184902Srwatson	.interp_path	= "/libexec/ld-elf.so.1",
148184902Srwatson	.sysvec		= &ia32_freebsd_sysvec,
149184902Srwatson	.interp_newpath	= "/libexec/ld-elf32.so.1",
150184902Srwatson	.brand_note	= &elf32_freebsd_brandnote,
151184902Srwatson	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
152184902Srwatson};
153184902Srwatson
154184902SrwatsonSYSINIT(ia32, SI_SUB_EXEC, SI_ORDER_MIDDLE,
155184902Srwatson	(sysinit_cfunc_t) elf32_insert_brand_entry,
156184902Srwatson	&ia32_brand_info);
157184902Srwatson
158184902Srwatsonstatic Elf32_Brandinfo ia32_brand_oinfo = {
159184902Srwatson	.brand		= ELFOSABI_FREEBSD,
160184902Srwatson	.machine	= EM_386,
161184902Srwatson	.compat_3_brand	= "FreeBSD",
162184902Srwatson	.emul_path	= NULL,
163184902Srwatson	.interp_path	= "/usr/libexec/ld-elf.so.1",
164184902Srwatson	.sysvec		= &ia32_freebsd_sysvec,
165184902Srwatson	.interp_newpath	= "/libexec/ld-elf32.so.1",
166184902Srwatson	.brand_note	= &elf32_freebsd_brandnote,
167184902Srwatson	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
168184902Srwatson};
169184902Srwatson
170184902SrwatsonSYSINIT(oia32, SI_SUB_EXEC, SI_ORDER_ANY,
171184902Srwatson	(sysinit_cfunc_t) elf32_insert_brand_entry,
172184902Srwatson	&ia32_brand_oinfo);
173184902Srwatson
174184902Srwatsonstatic Elf32_Brandinfo kia32_brand_info = {
175184902Srwatson	.brand		= ELFOSABI_FREEBSD,
176184902Srwatson	.machine	= EM_386,
177184902Srwatson	.compat_3_brand	= "FreeBSD",
178184902Srwatson	.emul_path	= NULL,
179184902Srwatson	.interp_path	= "/lib/ld.so.1",
180184902Srwatson	.sysvec		= &ia32_freebsd_sysvec,
181184902Srwatson	.brand_note	= &elf32_kfreebsd_brandnote,
182184902Srwatson	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE_MANDATORY
183184902Srwatson};
184184902Srwatson
185184902SrwatsonSYSINIT(kia32, SI_SUB_EXEC, SI_ORDER_ANY,
186184902Srwatson	(sysinit_cfunc_t) elf32_insert_brand_entry,
187184902Srwatson	&kia32_brand_info);
188184902Srwatson
189184902Srwatsonvoid
190184902Srwatsonelf32_dump_thread(struct thread *td __unused, void *dst __unused,
191184902Srwatson    size_t *off __unused)
192184902Srwatson{
193184902Srwatson}
194184902Srwatson
195184902Srwatsonvoid
196184902Srwatsonia32_fixlimit(struct rlimit *rl, int which)
197184902Srwatson{
198184902Srwatson
199184902Srwatson	switch (which) {
200184902Srwatson	case RLIMIT_DATA:
201184902Srwatson		if (ia32_maxdsiz != 0) {
202184902Srwatson			if (rl->rlim_cur > ia32_maxdsiz)
203184902Srwatson				rl->rlim_cur = ia32_maxdsiz;
204184902Srwatson			if (rl->rlim_max > ia32_maxdsiz)
205184902Srwatson				rl->rlim_max = ia32_maxdsiz;
206184902Srwatson		}
207184902Srwatson		break;
208184902Srwatson	case RLIMIT_STACK:
209184902Srwatson		if (ia32_maxssiz != 0) {
210184902Srwatson			if (rl->rlim_cur > ia32_maxssiz)
211184902Srwatson				rl->rlim_cur = ia32_maxssiz;
212184902Srwatson			if (rl->rlim_max > ia32_maxssiz)
213184902Srwatson				rl->rlim_max = ia32_maxssiz;
214184902Srwatson		}
215184902Srwatson		break;
216184902Srwatson	case RLIMIT_VMEM:
217184902Srwatson		if (ia32_maxvmem != 0) {
218184902Srwatson			if (rl->rlim_cur > ia32_maxvmem)
219184902Srwatson				rl->rlim_cur = ia32_maxvmem;
220184902Srwatson			if (rl->rlim_max > ia32_maxvmem)
221184902Srwatson				rl->rlim_max = ia32_maxvmem;
222184902Srwatson		}
223184902Srwatson		break;
224184902Srwatson	}
225184902Srwatson}
226184902Srwatson