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 #include "opt_compat.h"
35 #define __ELF_WORD_SIZE 32
37 #include <sys/param.h>
39 #include <sys/fcntl.h>
40 #include <sys/imgact.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/mutex.h>
46 #include <sys/namei.h>
47 #include <sys/pioctl.h>
49 #include <sys/procfs.h>
50 #include <sys/resourcevar.h>
51 #include <sys/systm.h>
52 #include <sys/signalvar.h>
55 #include <sys/syscall.h>
56 #include <sys/sysctl.h>
57 #include <sys/sysent.h>
58 #include <sys/vnode.h>
59 #include <sys/imgact_elf.h>
62 #include <vm/vm_kern.h>
63 #include <vm/vm_param.h>
65 #include <vm/vm_map.h>
66 #include <vm/vm_object.h>
67 #include <vm/vm_extern.h>
69 #include <compat/freebsd32/freebsd32_signal.h>
70 #include <compat/freebsd32/freebsd32_util.h>
71 #include <compat/freebsd32/freebsd32_proto.h>
72 #include <compat/freebsd32/freebsd32_syscall.h>
73 #include <compat/ia32/ia32_signal.h>
74 #include <machine/frame.h>
75 #include <machine/md_var.h>
76 #include <machine/pcb.h>
77 #include <machine/cpufunc.h>
79 CTASSERT(sizeof(struct ia32_mcontext) == 640);
80 CTASSERT(sizeof(struct ia32_ucontext) == 704);
81 CTASSERT(sizeof(struct ia32_sigframe) == 800);
82 CTASSERT(sizeof(struct siginfo32) == 64);
83 #ifdef COMPAT_FREEBSD4
84 CTASSERT(sizeof(struct ia32_mcontext4) == 260);
85 CTASSERT(sizeof(struct ia32_ucontext4) == 324);
86 CTASSERT(sizeof(struct ia32_sigframe4) == 408);
89 extern const char *freebsd32_syscallnames[];
91 static SYSCTL_NODE(_compat, OID_AUTO, ia32, CTLFLAG_RW, 0, "ia32 mode");
93 static u_long ia32_maxdsiz = IA32_MAXDSIZ;
94 SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxdsiz, CTLFLAG_RWTUN, &ia32_maxdsiz, 0, "");
95 u_long ia32_maxssiz = IA32_MAXSSIZ;
96 SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxssiz, CTLFLAG_RWTUN, &ia32_maxssiz, 0, "");
97 static u_long ia32_maxvmem = IA32_MAXVMEM;
98 SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxvmem, CTLFLAG_RWTUN, &ia32_maxvmem, 0, "");
100 struct sysentvec ia32_freebsd_sysvec = {
101 .sv_size = FREEBSD32_SYS_MAXSYSCALL,
102 .sv_table = freebsd32_sysent,
106 .sv_transtrap = NULL,
107 .sv_fixup = elf32_freebsd_fixup,
108 .sv_sendsig = ia32_sendsig,
109 .sv_sigcode = ia32_sigcode,
110 .sv_szsigcode = &sz_ia32_sigcode,
111 .sv_name = "FreeBSD ELF32",
112 .sv_coredump = elf32_coredump,
113 .sv_imgact_try = NULL,
114 .sv_minsigstksz = MINSIGSTKSZ,
115 .sv_pagesize = IA32_PAGE_SIZE,
116 .sv_minuser = FREEBSD32_MINUSER,
117 .sv_maxuser = FREEBSD32_MAXUSER,
118 .sv_usrstack = FREEBSD32_USRSTACK,
119 .sv_psstrings = FREEBSD32_PS_STRINGS,
120 .sv_stackprot = VM_PROT_ALL,
121 .sv_copyout_strings = freebsd32_copyout_strings,
122 .sv_setregs = ia32_setregs,
123 .sv_fixlimit = ia32_fixlimit,
124 .sv_maxssiz = &ia32_maxssiz,
125 .sv_flags = SV_ABI_FREEBSD | SV_IA32 | SV_ILP32 |
126 SV_SHP | SV_TIMEKEEP,
127 .sv_set_syscall_retval = ia32_set_syscall_retval,
128 .sv_fetch_syscall_args = ia32_fetch_syscall_args,
129 .sv_syscallnames = freebsd32_syscallnames,
130 .sv_shared_page_base = FREEBSD32_SHAREDPAGE,
131 .sv_shared_page_len = PAGE_SIZE,
132 .sv_schedtail = NULL,
133 .sv_thread_detach = NULL,
136 INIT_SYSENTVEC(elf_ia32_sysvec, &ia32_freebsd_sysvec);
138 static Elf32_Brandinfo ia32_brand_info = {
139 .brand = ELFOSABI_FREEBSD,
141 .compat_3_brand = "FreeBSD",
143 .interp_path = "/libexec/ld-elf.so.1",
144 .sysvec = &ia32_freebsd_sysvec,
145 .interp_newpath = "/libexec/ld-elf32.so.1",
146 .brand_note = &elf32_freebsd_brandnote,
147 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
150 SYSINIT(ia32, SI_SUB_EXEC, SI_ORDER_MIDDLE,
151 (sysinit_cfunc_t) elf32_insert_brand_entry,
154 static Elf32_Brandinfo ia32_brand_oinfo = {
155 .brand = ELFOSABI_FREEBSD,
157 .compat_3_brand = "FreeBSD",
159 .interp_path = "/usr/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
166 SYSINIT(oia32, SI_SUB_EXEC, SI_ORDER_ANY,
167 (sysinit_cfunc_t) elf32_insert_brand_entry,
170 static Elf32_Brandinfo kia32_brand_info = {
171 .brand = ELFOSABI_FREEBSD,
173 .compat_3_brand = "FreeBSD",
175 .interp_path = "/lib/ld.so.1",
176 .sysvec = &ia32_freebsd_sysvec,
177 .brand_note = &elf32_kfreebsd_brandnote,
178 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE_MANDATORY
181 SYSINIT(kia32, SI_SUB_EXEC, SI_ORDER_ANY,
182 (sysinit_cfunc_t) elf32_insert_brand_entry,
186 elf32_dump_thread(struct thread *td, void *dst, size_t *off)
195 len += elf32_populate_note(NT_X86_XSTATE,
196 get_pcb_user_save_td(td), dst,
197 cpu_max_ext_state_size, &buf);
198 *(uint64_t *)((char *)buf + X86_XSTATE_XCR0_OFFSET) =
201 len += elf32_populate_note(NT_X86_XSTATE, NULL, NULL,
202 cpu_max_ext_state_size, NULL);
208 ia32_fixlimit(struct rlimit *rl, int which)
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;
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;
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;