1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2002 Doug Rabson
5 * Copyright (c) 2003 Peter Wemm
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
16 *
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
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 #define __ELF_WORD_SIZE 32
32
33 #include <sys/param.h>
34 #include <sys/elf.h>
35 #include <sys/exec.h>
36 #include <sys/fcntl.h>
37 #include <sys/imgact.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/mman.h>
43 #include <sys/namei.h>
44 #include <sys/proc.h>
45 #include <sys/procfs.h>
46 #include <sys/reg.h>
47 #include <sys/resourcevar.h>
48 #include <sys/systm.h>
49 #include <sys/signalvar.h>
50 #include <sys/stat.h>
51 #include <sys/sx.h>
52 #include <sys/syscall.h>
53 #include <sys/sysctl.h>
54 #include <sys/sysent.h>
55 #include <sys/vnode.h>
56 #include <sys/imgact_elf.h>
57
58 #include <vm/vm.h>
59 #include <vm/vm_kern.h>
60 #include <vm/vm_param.h>
61 #include <vm/pmap.h>
62 #include <vm/vm_map.h>
63 #include <vm/vm_object.h>
64 #include <vm/vm_extern.h>
65
66 #include <compat/freebsd32/freebsd32_signal.h>
67 #include <compat/freebsd32/freebsd32_util.h>
68 #include <compat/freebsd32/freebsd32_proto.h>
69 #include <compat/freebsd32/freebsd32_syscall.h>
70 #include <compat/ia32/ia32_signal.h>
71 #include <machine/frame.h>
72 #include <machine/md_var.h>
73 #include <machine/pcb.h>
74 #include <machine/cpufunc.h>
75
76 CTASSERT(sizeof(struct ia32_mcontext) == 640);
77 CTASSERT(sizeof(struct ia32_ucontext) == 704);
78 CTASSERT(sizeof(struct ia32_sigframe) == 800);
79 CTASSERT(sizeof(struct __siginfo32) == 64);
80 #ifdef COMPAT_FREEBSD4
81 CTASSERT(sizeof(struct ia32_freebsd4_mcontext) == 260);
82 CTASSERT(sizeof(struct ia32_freebsd4_ucontext) == 324);
83 CTASSERT(sizeof(struct ia32_freebsd4_sigframe) == 408);
84 #endif
85
86 #include "vdso_ia32_offsets.h"
87
88 extern const char _binary_elf_vdso32_so_1_start[];
89 extern const char _binary_elf_vdso32_so_1_end[];
90 extern char _binary_elf_vdso32_so_1_size;
91
92 extern const char *freebsd32_syscallnames[];
93
94 static SYSCTL_NODE(_compat, OID_AUTO, ia32, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
95 "ia32 mode");
96
97 static u_long ia32_maxdsiz = IA32_MAXDSIZ;
98 SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxdsiz, CTLFLAG_RWTUN, &ia32_maxdsiz, 0, "");
99 u_long ia32_maxssiz = IA32_MAXSSIZ;
100 SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxssiz, CTLFLAG_RWTUN, &ia32_maxssiz, 0, "");
101 static u_long ia32_maxvmem = IA32_MAXVMEM;
102 SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxvmem, CTLFLAG_RWTUN, &ia32_maxvmem, 0, "");
103
104 struct sysentvec ia32_freebsd_sysvec = {
105 .sv_size = FREEBSD32_SYS_MAXSYSCALL,
106 .sv_table = freebsd32_sysent,
107 .sv_fixup = elf32_freebsd_fixup,
108 .sv_sendsig = ia32_sendsig,
109 .sv_sigcode = _binary_elf_vdso32_so_1_start,
110 .sv_szsigcode = (int *)&_binary_elf_vdso32_so_1_size,
111 .sv_sigcodeoff = VDSO_IA32_SIGCODE_OFFSET,
112 .sv_name = "FreeBSD ELF32",
113 .sv_coredump = elf32_coredump,
114 .sv_elf_core_osabi = ELFOSABI_FREEBSD,
115 .sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
116 .sv_elf_core_prepare_notes = elf32_prepare_notes,
117 .sv_minsigstksz = MINSIGSTKSZ,
118 .sv_minuser = FREEBSD32_MINUSER,
119 .sv_maxuser = FREEBSD32_MAXUSER,
120 .sv_usrstack = FREEBSD32_USRSTACK,
121 .sv_psstrings = FREEBSD32_PS_STRINGS,
122 .sv_psstringssz = sizeof(struct freebsd32_ps_strings),
123 .sv_stackprot = VM_PROT_ALL,
124 .sv_copyout_auxargs = elf32_freebsd_copyout_auxargs,
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_ASLR | SV_IA32 | SV_ILP32 |
130 SV_SHP | SV_TIMEKEEP | SV_RNG_SEED_VER |
131 SV_DSO_SIG | SV_SIGSYS,
132 .sv_set_syscall_retval = ia32_set_syscall_retval,
133 .sv_fetch_syscall_args = ia32_fetch_syscall_args,
134 .sv_syscallnames = freebsd32_syscallnames,
135 .sv_shared_page_base = FREEBSD32_SHAREDPAGE,
136 .sv_shared_page_len = PAGE_SIZE,
137 .sv_schedtail = NULL,
138 .sv_thread_detach = NULL,
139 .sv_trap = NULL,
140 .sv_onexec_old = exec_onexec_old,
141 .sv_onexit = exit_onexit,
142 .sv_set_fork_retval = x86_set_fork_retval,
143 .sv_regset_begin = SET_BEGIN(__elfN(regset)),
144 .sv_regset_end = SET_LIMIT(__elfN(regset)),
145 };
146 INIT_SYSENTVEC(elf_ia32_sysvec, &ia32_freebsd_sysvec);
147
148 static Elf32_Brandinfo ia32_brand_info = {
149 .brand = ELFOSABI_FREEBSD,
150 .machine = EM_386,
151 .compat_3_brand = "FreeBSD",
152 .interp_path = "/libexec/ld-elf.so.1",
153 .sysvec = &ia32_freebsd_sysvec,
154 .interp_newpath = "/libexec/ld-elf32.so.1",
155 .brand_note = &elf32_freebsd_brandnote,
156 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
157 };
158
159 SYSINIT(ia32, SI_SUB_EXEC, SI_ORDER_MIDDLE,
160 (sysinit_cfunc_t) elf32_insert_brand_entry,
161 &ia32_brand_info);
162
163 static Elf32_Brandinfo ia32_brand_oinfo = {
164 .brand = ELFOSABI_FREEBSD,
165 .machine = EM_386,
166 .compat_3_brand = "FreeBSD",
167 .interp_path = "/usr/libexec/ld-elf.so.1",
168 .sysvec = &ia32_freebsd_sysvec,
169 .interp_newpath = "/libexec/ld-elf32.so.1",
170 .brand_note = &elf32_freebsd_brandnote,
171 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
172 };
173
174 SYSINIT(oia32, SI_SUB_EXEC, SI_ORDER_ANY,
175 (sysinit_cfunc_t) elf32_insert_brand_entry,
176 &ia32_brand_oinfo);
177
178 static Elf32_Brandinfo kia32_brand_info = {
179 .brand = ELFOSABI_FREEBSD,
180 .machine = EM_386,
181 .compat_3_brand = "FreeBSD",
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
188 SYSINIT(kia32, SI_SUB_EXEC, SI_ORDER_ANY,
189 (sysinit_cfunc_t) elf32_insert_brand_entry,
190 &kia32_brand_info);
191
192 void
elf32_dump_thread(struct thread * td,void * dst,size_t * off)193 elf32_dump_thread(struct thread *td, void *dst, size_t *off)
194 {
195 void *buf;
196 size_t len;
197
198 len = 0;
199 if (use_xsave) {
200 if (dst != NULL) {
201 fpugetregs(td);
202 len += elf32_populate_note(NT_X86_XSTATE,
203 get_pcb_user_save_td(td), dst,
204 cpu_max_ext_state_size, &buf);
205 *(uint64_t *)((char *)buf + X86_XSTATE_XCR0_OFFSET) =
206 xsave_mask;
207 } else
208 len += elf32_populate_note(NT_X86_XSTATE, NULL, NULL,
209 cpu_max_ext_state_size, NULL);
210 }
211 *off = len;
212 }
213
214 void
ia32_fixlimit(struct rlimit * rl,int which)215 ia32_fixlimit(struct rlimit *rl, int which)
216 {
217
218 switch (which) {
219 case RLIMIT_DATA:
220 if (ia32_maxdsiz != 0) {
221 if (rl->rlim_cur > ia32_maxdsiz)
222 rl->rlim_cur = ia32_maxdsiz;
223 if (rl->rlim_max > ia32_maxdsiz)
224 rl->rlim_max = ia32_maxdsiz;
225 }
226 break;
227 case RLIMIT_STACK:
228 if (ia32_maxssiz != 0) {
229 if (rl->rlim_cur > ia32_maxssiz)
230 rl->rlim_cur = ia32_maxssiz;
231 if (rl->rlim_max > ia32_maxssiz)
232 rl->rlim_max = ia32_maxssiz;
233 }
234 break;
235 case RLIMIT_VMEM:
236 if (ia32_maxvmem != 0) {
237 if (rl->rlim_cur > ia32_maxvmem)
238 rl->rlim_cur = ia32_maxvmem;
239 if (rl->rlim_max > ia32_maxvmem)
240 rl->rlim_max = ia32_maxvmem;
241 }
242 break;
243 }
244 }
245