xref: /freebsd/sys/compat/ia32/ia32_sysvec.c (revision b28624fde638caadd4a89f50c9b7e7da0f98c4d2)
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 #ifdef __amd64__
73 #include <machine/psl.h>
74 #include <machine/segments.h>
75 #include <machine/specialreg.h>
76 #else
77 #include <i386/include/psl.h>
78 #include <i386/include/segments.h>
79 #include <i386/include/specialreg.h>
80 #endif
81 #include <machine/frame.h>
82 #include <machine/md_var.h>
83 #include <machine/pcb.h>
84 #include <machine/cpufunc.h>
85 
86 CTASSERT(sizeof(struct ia32_mcontext) == 640);
87 CTASSERT(sizeof(struct ia32_ucontext) == 704);
88 CTASSERT(sizeof(struct ia32_sigframe) == 800);
89 CTASSERT(sizeof(struct siginfo32) == 64);
90 #ifdef COMPAT_FREEBSD4
91 CTASSERT(sizeof(struct ia32_mcontext4) == 260);
92 CTASSERT(sizeof(struct ia32_ucontext4) == 324);
93 CTASSERT(sizeof(struct ia32_sigframe4) == 408);
94 #endif
95 
96 static register_t *ia32_copyout_strings(struct image_params *imgp);
97 static void ia32_fixlimit(struct rlimit *rl, int which);
98 
99 extern struct sysent freebsd32_sysent[];
100 
101 SYSCTL_NODE(_compat, OID_AUTO, ia32, CTLFLAG_RW, 0, "ia32 mode");
102 
103 static u_long	ia32_maxdsiz = IA32_MAXDSIZ;
104 SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxdsiz, CTLFLAG_RW, &ia32_maxdsiz, 0, "");
105 static u_long	ia32_maxssiz = IA32_MAXSSIZ;
106 SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxssiz, CTLFLAG_RW, &ia32_maxssiz, 0, "");
107 static u_long	ia32_maxvmem = IA32_MAXVMEM;
108 SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxvmem, CTLFLAG_RW, &ia32_maxvmem, 0, "");
109 
110 struct sysentvec ia32_freebsd_sysvec = {
111 	FREEBSD32_SYS_MAXSYSCALL,
112 	freebsd32_sysent,
113 	0,
114 	0,
115 	NULL,
116 	0,
117 	NULL,
118 	NULL,
119 	elf32_freebsd_fixup,
120 	ia32_sendsig,
121 	ia32_sigcode,
122 	&sz_ia32_sigcode,
123 	NULL,
124 	"FreeBSD ELF32",
125 	elf32_coredump,
126 	NULL,
127 	MINSIGSTKSZ,
128 	IA32_PAGE_SIZE,
129 	0,
130 	FREEBSD32_USRSTACK,
131 	FREEBSD32_USRSTACK,
132 	FREEBSD32_PS_STRINGS,
133 	VM_PROT_ALL,
134 	ia32_copyout_strings,
135 	ia32_setregs,
136 	ia32_fixlimit,
137 	&ia32_maxssiz
138 };
139 
140 
141 static Elf32_Brandinfo ia32_brand_info = {
142 						ELFOSABI_FREEBSD,
143 						EM_386,
144 						"FreeBSD",
145 						NULL,
146 						"/libexec/ld-elf.so.1",
147 						&ia32_freebsd_sysvec,
148 						"/libexec/ld-elf32.so.1",
149 						BI_CAN_EXEC_DYN,
150 					  };
151 
152 SYSINIT(ia32, SI_SUB_EXEC, SI_ORDER_ANY,
153 	(sysinit_cfunc_t) elf32_insert_brand_entry,
154 	&ia32_brand_info);
155 
156 static Elf32_Brandinfo ia32_brand_oinfo = {
157 						ELFOSABI_FREEBSD,
158 						EM_386,
159 						"FreeBSD",
160 						NULL,
161 						"/usr/libexec/ld-elf.so.1",
162 						&ia32_freebsd_sysvec,
163 						"/libexec/ld-elf32.so.1",
164 						BI_CAN_EXEC_DYN,
165 					  };
166 
167 SYSINIT(oia32, SI_SUB_EXEC, SI_ORDER_ANY,
168 	(sysinit_cfunc_t) elf32_insert_brand_entry,
169 	&ia32_brand_oinfo);
170 
171 
172 void
173 elf32_dump_thread(struct thread *td __unused, void *dst __unused,
174     size_t *off __unused)
175 {
176 }
177 
178 
179 /* XXX may be freebsd32 MI */
180 static register_t *
181 ia32_copyout_strings(struct image_params *imgp)
182 {
183 	int argc, envc;
184 	u_int32_t *vectp;
185 	char *stringp, *destp;
186 	u_int32_t *stack_base;
187 	struct freebsd32_ps_strings *arginfo;
188 	int szsigcode;
189 
190 	/*
191 	 * Calculate string base and vector table pointers.
192 	 * Also deal with signal trampoline code for this exec type.
193 	 */
194 	arginfo = (struct freebsd32_ps_strings *)FREEBSD32_PS_STRINGS;
195 	szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
196 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
197 		roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
198 
199 	/*
200 	 * install sigcode
201 	 */
202 	if (szsigcode)
203 		copyout(imgp->proc->p_sysent->sv_sigcode,
204 			((caddr_t)arginfo - szsigcode), szsigcode);
205 
206 	/*
207 	 * If we have a valid auxargs ptr, prepare some room
208 	 * on the stack.
209 	 */
210 	if (imgp->auxargs) {
211 		/*
212 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
213 		 * lower compatibility.
214 		 */
215 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
216 			: (AT_COUNT * 2);
217 		/*
218 		 * The '+ 2' is for the null pointers at the end of each of
219 		 * the arg and env vector sets,and imgp->auxarg_size is room
220 		 * for argument of Runtime loader.
221 		 */
222 		vectp = (u_int32_t *) (destp - (imgp->args->argc + imgp->args->envc + 2 +
223 				       imgp->auxarg_size) * sizeof(u_int32_t));
224 
225 	} else
226 		/*
227 		 * The '+ 2' is for the null pointers at the end of each of
228 		 * the arg and env vector sets
229 		 */
230 		vectp = (u_int32_t *)
231 			(destp - (imgp->args->argc + imgp->args->envc + 2) * sizeof(u_int32_t));
232 
233 	/*
234 	 * vectp also becomes our initial stack base
235 	 */
236 	stack_base = vectp;
237 
238 	stringp = imgp->args->begin_argv;
239 	argc = imgp->args->argc;
240 	envc = imgp->args->envc;
241 	/*
242 	 * Copy out strings - arguments and environment.
243 	 */
244 	copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
245 
246 	/*
247 	 * Fill in "ps_strings" struct for ps, w, etc.
248 	 */
249 	suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
250 	suword32(&arginfo->ps_nargvstr, argc);
251 
252 	/*
253 	 * Fill in argument portion of vector table.
254 	 */
255 	for (; argc > 0; --argc) {
256 		suword32(vectp++, (u_int32_t)(intptr_t)destp);
257 		while (*stringp++ != 0)
258 			destp++;
259 		destp++;
260 	}
261 
262 	/* a null vector table pointer separates the argp's from the envp's */
263 	suword32(vectp++, 0);
264 
265 	suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
266 	suword32(&arginfo->ps_nenvstr, envc);
267 
268 	/*
269 	 * Fill in environment portion of vector table.
270 	 */
271 	for (; envc > 0; --envc) {
272 		suword32(vectp++, (u_int32_t)(intptr_t)destp);
273 		while (*stringp++ != 0)
274 			destp++;
275 		destp++;
276 	}
277 
278 	/* end of vector table is a null pointer */
279 	suword32(vectp, 0);
280 
281 	return ((register_t *)stack_base);
282 }
283 
284 static void
285 ia32_fixlimit(struct rlimit *rl, int which)
286 {
287 
288 	switch (which) {
289 	case RLIMIT_DATA:
290 		if (ia32_maxdsiz != 0) {
291 			if (rl->rlim_cur > ia32_maxdsiz)
292 				rl->rlim_cur = ia32_maxdsiz;
293 			if (rl->rlim_max > ia32_maxdsiz)
294 				rl->rlim_max = ia32_maxdsiz;
295 		}
296 		break;
297 	case RLIMIT_STACK:
298 		if (ia32_maxssiz != 0) {
299 			if (rl->rlim_cur > ia32_maxssiz)
300 				rl->rlim_cur = ia32_maxssiz;
301 			if (rl->rlim_max > ia32_maxssiz)
302 				rl->rlim_max = ia32_maxssiz;
303 		}
304 		break;
305 	case RLIMIT_VMEM:
306 		if (ia32_maxvmem != 0) {
307 			if (rl->rlim_cur > ia32_maxvmem)
308 				rl->rlim_cur = ia32_maxvmem;
309 			if (rl->rlim_max > ia32_maxvmem)
310 				rl->rlim_max = ia32_maxvmem;
311 		}
312 		break;
313 	}
314 }
315