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