2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002 Doug Rabson
5 * Copyright (c) 2003 Peter Wemm
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #define __ELF_WORD_SIZE 32
35 #include <sys/param.h>
37 #include <sys/fcntl.h>
38 #include <sys/imgact.h>
39 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
44 #include <sys/namei.h>
45 #include <sys/pioctl.h>
47 #include <sys/procfs.h>
48 #include <sys/resourcevar.h>
49 #include <sys/systm.h>
50 #include <sys/signalvar.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>
60 #include <vm/vm_kern.h>
61 #include <vm/vm_param.h>
63 #include <vm/vm_map.h>
64 #include <vm/vm_object.h>
65 #include <vm/vm_extern.h>
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>
77 CTASSERT(sizeof(struct ia32_mcontext) == 640);
78 CTASSERT(sizeof(struct ia32_ucontext) == 704);
79 CTASSERT(sizeof(struct ia32_sigframe) == 800);
80 CTASSERT(sizeof(struct siginfo32) == 64);
81 #ifdef COMPAT_FREEBSD4
82 CTASSERT(sizeof(struct ia32_mcontext4) == 260);
83 CTASSERT(sizeof(struct ia32_ucontext4) == 324);
84 CTASSERT(sizeof(struct ia32_sigframe4) == 408);
87 extern const char *freebsd32_syscallnames[];
89 static SYSCTL_NODE(_compat, OID_AUTO, ia32, CTLFLAG_RW, 0, "ia32 mode");
91 static u_long ia32_maxdsiz = IA32_MAXDSIZ;
92 SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxdsiz, CTLFLAG_RWTUN, &ia32_maxdsiz, 0, "");
93 u_long ia32_maxssiz = IA32_MAXSSIZ;
94 SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxssiz, CTLFLAG_RWTUN, &ia32_maxssiz, 0, "");
95 static u_long ia32_maxvmem = IA32_MAXVMEM;
96 SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxvmem, CTLFLAG_RWTUN, &ia32_maxvmem, 0, "");
98 struct sysentvec ia32_freebsd_sysvec = {
99 .sv_size = FREEBSD32_SYS_MAXSYSCALL,
100 .sv_table = freebsd32_sysent,
103 .sv_transtrap = NULL,
104 .sv_fixup = elf32_freebsd_fixup,
105 .sv_sendsig = ia32_sendsig,
106 .sv_sigcode = ia32_sigcode,
107 .sv_szsigcode = &sz_ia32_sigcode,
108 .sv_name = "FreeBSD ELF32",
109 .sv_coredump = elf32_coredump,
110 .sv_imgact_try = NULL,
111 .sv_minsigstksz = MINSIGSTKSZ,
112 .sv_pagesize = IA32_PAGE_SIZE,
113 .sv_minuser = FREEBSD32_MINUSER,
114 .sv_maxuser = FREEBSD32_MAXUSER,
115 .sv_usrstack = FREEBSD32_USRSTACK,
116 .sv_psstrings = FREEBSD32_PS_STRINGS,
117 .sv_stackprot = VM_PROT_ALL,
118 .sv_copyout_strings = freebsd32_copyout_strings,
119 .sv_setregs = ia32_setregs,
120 .sv_fixlimit = ia32_fixlimit,
121 .sv_maxssiz = &ia32_maxssiz,
122 .sv_flags = SV_ABI_FREEBSD | SV_ASLR | SV_IA32 | SV_ILP32 |
123 SV_SHP | SV_TIMEKEEP,
124 .sv_set_syscall_retval = ia32_set_syscall_retval,
125 .sv_fetch_syscall_args = ia32_fetch_syscall_args,
126 .sv_syscallnames = freebsd32_syscallnames,
127 .sv_shared_page_base = FREEBSD32_SHAREDPAGE,
128 .sv_shared_page_len = PAGE_SIZE,
129 .sv_schedtail = NULL,
130 .sv_thread_detach = NULL,
133 INIT_SYSENTVEC(elf_ia32_sysvec, &ia32_freebsd_sysvec);
135 static Elf32_Brandinfo ia32_brand_info = {
136 .brand = ELFOSABI_FREEBSD,
138 .compat_3_brand = "FreeBSD",
140 .interp_path = "/libexec/ld-elf.so.1",
141 .sysvec = &ia32_freebsd_sysvec,
142 .interp_newpath = "/libexec/ld-elf32.so.1",
143 .brand_note = &elf32_freebsd_brandnote,
144 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
147 SYSINIT(ia32, SI_SUB_EXEC, SI_ORDER_MIDDLE,
148 (sysinit_cfunc_t) elf32_insert_brand_entry,
151 static Elf32_Brandinfo ia32_brand_oinfo = {
152 .brand = ELFOSABI_FREEBSD,
154 .compat_3_brand = "FreeBSD",
156 .interp_path = "/usr/libexec/ld-elf.so.1",
157 .sysvec = &ia32_freebsd_sysvec,
158 .interp_newpath = "/libexec/ld-elf32.so.1",
159 .brand_note = &elf32_freebsd_brandnote,
160 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
163 SYSINIT(oia32, SI_SUB_EXEC, SI_ORDER_ANY,
164 (sysinit_cfunc_t) elf32_insert_brand_entry,
167 static Elf32_Brandinfo kia32_brand_info = {
168 .brand = ELFOSABI_FREEBSD,
170 .compat_3_brand = "FreeBSD",
172 .interp_path = "/lib/ld.so.1",
173 .sysvec = &ia32_freebsd_sysvec,
174 .brand_note = &elf32_kfreebsd_brandnote,
175 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE_MANDATORY
178 SYSINIT(kia32, SI_SUB_EXEC, SI_ORDER_ANY,
179 (sysinit_cfunc_t) elf32_insert_brand_entry,
183 elf32_dump_thread(struct thread *td, void *dst, size_t *off)
192 len += elf32_populate_note(NT_X86_XSTATE,
193 get_pcb_user_save_td(td), dst,
194 cpu_max_ext_state_size, &buf);
195 *(uint64_t *)((char *)buf + X86_XSTATE_XCR0_OFFSET) =
198 len += elf32_populate_note(NT_X86_XSTATE, NULL, NULL,
199 cpu_max_ext_state_size, NULL);
205 ia32_fixlimit(struct rlimit *rl, int which)
210 if (ia32_maxdsiz != 0) {
211 if (rl->rlim_cur > ia32_maxdsiz)
212 rl->rlim_cur = ia32_maxdsiz;
213 if (rl->rlim_max > ia32_maxdsiz)
214 rl->rlim_max = ia32_maxdsiz;
218 if (ia32_maxssiz != 0) {
219 if (rl->rlim_cur > ia32_maxssiz)
220 rl->rlim_cur = ia32_maxssiz;
221 if (rl->rlim_max > ia32_maxssiz)
222 rl->rlim_max = ia32_maxssiz;
226 if (ia32_maxvmem != 0) {
227 if (rl->rlim_cur > ia32_maxvmem)
228 rl->rlim_cur = ia32_maxvmem;
229 if (rl->rlim_max > ia32_maxvmem)
230 rl->rlim_max = ia32_maxvmem;