xref: /freebsd/sys/compat/ia32/ia32_sysvec.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1 /*-
2  * Copyright (c) 2002 Doug Rabson
3  * Copyright (c) 2003 Peter Wemm
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "opt_compat.h"
32 
33 #define __ELF_WORD_SIZE 32
34 
35 #include <sys/param.h>
36 #include <sys/exec.h>
37 #include <sys/fcntl.h>
38 #include <sys/imgact.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/mman.h>
44 #include <sys/namei.h>
45 #include <sys/pioctl.h>
46 #include <sys/proc.h>
47 #include <sys/procfs.h>
48 #include <sys/resourcevar.h>
49 #include <sys/systm.h>
50 #include <sys/signalvar.h>
51 #include <sys/stat.h>
52 #include <sys/sx.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>
58 
59 #include <vm/vm.h>
60 #include <vm/vm_kern.h>
61 #include <vm/vm_param.h>
62 #include <vm/pmap.h>
63 #include <vm/vm_map.h>
64 #include <vm/vm_object.h>
65 #include <vm/vm_extern.h>
66 
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>
76 
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);
85 #endif
86 
87 extern const char *freebsd32_syscallnames[];
88 
89 static SYSCTL_NODE(_compat, OID_AUTO, ia32, CTLFLAG_RW, 0, "ia32 mode");
90 
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, "");
97 
98 struct sysentvec ia32_freebsd_sysvec = {
99 	.sv_size	= FREEBSD32_SYS_MAXSYSCALL,
100 	.sv_table	= freebsd32_sysent,
101 	.sv_mask	= 0,
102 	.sv_errsize	= 0,
103 	.sv_errtbl	= NULL,
104 	.sv_transtrap	= NULL,
105 	.sv_fixup	= elf32_freebsd_fixup,
106 	.sv_sendsig	= ia32_sendsig,
107 	.sv_sigcode	= ia32_sigcode,
108 	.sv_szsigcode	= &sz_ia32_sigcode,
109 	.sv_name	= "FreeBSD ELF32",
110 	.sv_coredump	= elf32_coredump,
111 	.sv_imgact_try	= NULL,
112 	.sv_minsigstksz	= MINSIGSTKSZ,
113 	.sv_pagesize	= IA32_PAGE_SIZE,
114 	.sv_minuser	= FREEBSD32_MINUSER,
115 	.sv_maxuser	= FREEBSD32_MAXUSER,
116 	.sv_usrstack	= FREEBSD32_USRSTACK,
117 	.sv_psstrings	= FREEBSD32_PS_STRINGS,
118 	.sv_stackprot	= VM_PROT_ALL,
119 	.sv_copyout_strings	= freebsd32_copyout_strings,
120 	.sv_setregs	= ia32_setregs,
121 	.sv_fixlimit	= ia32_fixlimit,
122 	.sv_maxssiz	= &ia32_maxssiz,
123 	.sv_flags	= SV_ABI_FREEBSD | SV_IA32 | SV_ILP32 |
124 #ifdef __amd64__
125 		SV_SHP | SV_TIMEKEEP
126 #else
127 		0
128 #endif
129 	,
130 	.sv_set_syscall_retval = ia32_set_syscall_retval,
131 	.sv_fetch_syscall_args = ia32_fetch_syscall_args,
132 	.sv_syscallnames = freebsd32_syscallnames,
133 	.sv_shared_page_base = FREEBSD32_SHAREDPAGE,
134 	.sv_shared_page_len = PAGE_SIZE,
135 	.sv_schedtail	= NULL,
136 	.sv_thread_detach = NULL,
137 	.sv_trap	= NULL,
138 };
139 INIT_SYSENTVEC(elf_ia32_sysvec, &ia32_freebsd_sysvec);
140 
141 static Elf32_Brandinfo ia32_brand_info = {
142 	.brand		= ELFOSABI_FREEBSD,
143 	.machine	= EM_386,
144 	.compat_3_brand	= "FreeBSD",
145 	.emul_path	= NULL,
146 	.interp_path	= "/libexec/ld-elf.so.1",
147 	.sysvec		= &ia32_freebsd_sysvec,
148 	.interp_newpath	= "/libexec/ld-elf32.so.1",
149 	.brand_note	= &elf32_freebsd_brandnote,
150 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
151 };
152 
153 SYSINIT(ia32, SI_SUB_EXEC, SI_ORDER_MIDDLE,
154 	(sysinit_cfunc_t) elf32_insert_brand_entry,
155 	&ia32_brand_info);
156 
157 static Elf32_Brandinfo ia32_brand_oinfo = {
158 	.brand		= ELFOSABI_FREEBSD,
159 	.machine	= EM_386,
160 	.compat_3_brand	= "FreeBSD",
161 	.emul_path	= NULL,
162 	.interp_path	= "/usr/libexec/ld-elf.so.1",
163 	.sysvec		= &ia32_freebsd_sysvec,
164 	.interp_newpath	= "/libexec/ld-elf32.so.1",
165 	.brand_note	= &elf32_freebsd_brandnote,
166 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
167 };
168 
169 SYSINIT(oia32, SI_SUB_EXEC, SI_ORDER_ANY,
170 	(sysinit_cfunc_t) elf32_insert_brand_entry,
171 	&ia32_brand_oinfo);
172 
173 static Elf32_Brandinfo kia32_brand_info = {
174 	.brand		= ELFOSABI_FREEBSD,
175 	.machine	= EM_386,
176 	.compat_3_brand	= "FreeBSD",
177 	.emul_path	= NULL,
178 	.interp_path	= "/lib/ld.so.1",
179 	.sysvec		= &ia32_freebsd_sysvec,
180 	.brand_note	= &elf32_kfreebsd_brandnote,
181 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE_MANDATORY
182 };
183 
184 SYSINIT(kia32, SI_SUB_EXEC, SI_ORDER_ANY,
185 	(sysinit_cfunc_t) elf32_insert_brand_entry,
186 	&kia32_brand_info);
187 
188 void
189 elf32_dump_thread(struct thread *td, void *dst, size_t *off)
190 {
191 	void *buf;
192 	size_t len;
193 
194 	len = 0;
195 	if (use_xsave) {
196 		if (dst != NULL) {
197 			fpugetregs(td);
198 			len += elf32_populate_note(NT_X86_XSTATE,
199 			    get_pcb_user_save_td(td), dst,
200 			    cpu_max_ext_state_size, &buf);
201 			*(uint64_t *)((char *)buf + X86_XSTATE_XCR0_OFFSET) =
202 			    xsave_mask;
203 		} else
204 			len += elf32_populate_note(NT_X86_XSTATE, NULL, NULL,
205 			    cpu_max_ext_state_size, NULL);
206 	}
207 	*off = len;
208 }
209 
210 void
211 ia32_fixlimit(struct rlimit *rl, int which)
212 {
213 
214 	switch (which) {
215 	case RLIMIT_DATA:
216 		if (ia32_maxdsiz != 0) {
217 			if (rl->rlim_cur > ia32_maxdsiz)
218 				rl->rlim_cur = ia32_maxdsiz;
219 			if (rl->rlim_max > ia32_maxdsiz)
220 				rl->rlim_max = ia32_maxdsiz;
221 		}
222 		break;
223 	case RLIMIT_STACK:
224 		if (ia32_maxssiz != 0) {
225 			if (rl->rlim_cur > ia32_maxssiz)
226 				rl->rlim_cur = ia32_maxssiz;
227 			if (rl->rlim_max > ia32_maxssiz)
228 				rl->rlim_max = ia32_maxssiz;
229 		}
230 		break;
231 	case RLIMIT_VMEM:
232 		if (ia32_maxvmem != 0) {
233 			if (rl->rlim_cur > ia32_maxvmem)
234 				rl->rlim_cur = ia32_maxvmem;
235 			if (rl->rlim_max > ia32_maxvmem)
236 				rl->rlim_max = ia32_maxvmem;
237 		}
238 		break;
239 	}
240 }
241