ia32_sysvec.c revision 120422
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: head/sys/compat/ia32/ia32_sysvec.c 120422 2003-09-25 01:10:26Z peter $");
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_util.h>
68#include <compat/freebsd32/freebsd32_proto.h>
69#include <compat/ia32/ia32_signal.h>
70#include <machine/psl.h>
71#include <machine/segments.h>
72#include <machine/specialreg.h>
73#include <machine/frame.h>
74#include <machine/md_var.h>
75#include <machine/pcb.h>
76#include <machine/cpufunc.h>
77
78static register_t *ia32_copyout_strings(struct image_params *imgp);
79static void ia32_setregs(struct thread *td, u_long entry, u_long stack,
80    u_long ps_strings);
81static void ia32_fixlimits(struct image_params *imgp);
82
83extern struct sysent freebsd32_sysent[];
84
85SYSCTL_NODE(_compat, OID_AUTO, ia32, CTLFLAG_RW, 0, "ia32 mode");
86
87struct sysentvec ia32_freebsd_sysvec = {
88	SYS_MAXSYSCALL,
89	freebsd32_sysent,
90	0,
91	0,
92	NULL,
93	0,
94	NULL,
95	NULL,
96	elf32_freebsd_fixup,
97	ia32_sendsig,
98	ia32_sigcode,
99	&sz_ia32_sigcode,
100	NULL,
101	"FreeBSD ELF32",
102	elf32_coredump,
103	NULL,
104	MINSIGSTKSZ,
105	PAGE_SIZE,
106	0,
107	FREEBSD32_USRSTACK,
108	FREEBSD32_USRSTACK,
109	FREEBSD32_PS_STRINGS,
110	VM_PROT_ALL,
111	ia32_copyout_strings,
112	ia32_setregs,
113	ia32_fixlimits
114};
115
116
117const char freebsd32_emul_path[] = "/compat/ia32";
118
119static Elf32_Brandinfo ia32_brand_info = {
120						ELFOSABI_FREEBSD,
121						EM_386,
122						"FreeBSD",
123						"/compat/ia32",
124						"/usr/libexec/ld-elf.so.1",
125						&ia32_freebsd_sysvec
126					  };
127
128SYSINIT(ia32, SI_SUB_EXEC, SI_ORDER_ANY,
129	(sysinit_cfunc_t) elf32_insert_brand_entry,
130	&ia32_brand_info);
131
132/* XXX may be freebsd32 MI */
133static register_t *
134ia32_copyout_strings(struct image_params *imgp)
135{
136	int argc, envc;
137	u_int32_t *vectp;
138	char *stringp, *destp;
139	u_int32_t *stack_base;
140	struct freebsd32_ps_strings *arginfo;
141	int szsigcode;
142
143	/*
144	 * Calculate string base and vector table pointers.
145	 * Also deal with signal trampoline code for this exec type.
146	 */
147	arginfo = (struct freebsd32_ps_strings *)FREEBSD32_PS_STRINGS;
148	szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
149	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
150		roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
151
152	/*
153	 * install sigcode
154	 */
155	if (szsigcode)
156		copyout(imgp->proc->p_sysent->sv_sigcode,
157			((caddr_t)arginfo - szsigcode), szsigcode);
158
159	/*
160	 * If we have a valid auxargs ptr, prepare some room
161	 * on the stack.
162	 */
163	if (imgp->auxargs) {
164		/*
165		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
166		 * lower compatibility.
167		 */
168		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
169			: (AT_COUNT * 2);
170		/*
171		 * The '+ 2' is for the null pointers at the end of each of
172		 * the arg and env vector sets,and imgp->auxarg_size is room
173		 * for argument of Runtime loader.
174		 */
175		vectp = (u_int32_t *) (destp - (imgp->argc + imgp->envc + 2 +
176				       imgp->auxarg_size) * sizeof(u_int32_t));
177
178	} else
179		/*
180		 * The '+ 2' is for the null pointers at the end of each of
181		 * the arg and env vector sets
182		 */
183		vectp = (u_int32_t *)
184			(destp - (imgp->argc + imgp->envc + 2) * sizeof(u_int32_t));
185
186	/*
187	 * vectp also becomes our initial stack base
188	 */
189	stack_base = vectp;
190
191	stringp = imgp->stringbase;
192	argc = imgp->argc;
193	envc = imgp->envc;
194	/*
195	 * Copy out strings - arguments and environment.
196	 */
197	copyout(stringp, destp, ARG_MAX - imgp->stringspace);
198
199	/*
200	 * Fill in "ps_strings" struct for ps, w, etc.
201	 */
202	suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
203	suword32(&arginfo->ps_nargvstr, argc);
204
205	/*
206	 * Fill in argument portion of vector table.
207	 */
208	for (; argc > 0; --argc) {
209		suword32(vectp++, (u_int32_t)(intptr_t)destp);
210		while (*stringp++ != 0)
211			destp++;
212		destp++;
213	}
214
215	/* a null vector table pointer separates the argp's from the envp's */
216	suword32(vectp++, 0);
217
218	suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
219	suword32(&arginfo->ps_nenvstr, envc);
220
221	/*
222	 * Fill in environment portion of vector table.
223	 */
224	for (; envc > 0; --envc) {
225		suword32(vectp++, (u_int32_t)(intptr_t)destp);
226		while (*stringp++ != 0)
227			destp++;
228		destp++;
229	}
230
231	/* end of vector table is a null pointer */
232	suword32(vectp, 0);
233
234	return ((register_t *)stack_base);
235}
236
237/*
238 * Clear registers on exec
239 * XXX backend MD
240 */
241extern int _ucode32sel, _udatasel;
242void
243ia32_setregs(td, entry, stack, ps_strings)
244	struct thread *td;
245	u_long entry;
246	u_long stack;
247	u_long ps_strings;
248{
249	struct trapframe *regs = td->td_frame;
250	struct pcb *pcb = td->td_pcb;
251
252	wrmsr(MSR_FSBASE, 0);
253	wrmsr(MSR_KGSBASE, 0);	/* User value while we're in the kernel */
254	pcb->pcb_fsbase = 0;
255	pcb->pcb_gsbase = 0;
256	load_ds(_udatasel);
257	load_es(_udatasel);
258	load_fs(_udatasel);
259	load_gs(_udatasel);
260	pcb->pcb_ds = _udatasel;
261	pcb->pcb_es = _udatasel;
262	pcb->pcb_fs = _udatasel;
263	pcb->pcb_gs = _udatasel;
264
265	bzero((char *)regs, sizeof(struct trapframe));
266	regs->tf_rip = entry;
267	regs->tf_rsp = stack;
268	regs->tf_rflags = PSL_USER | (regs->tf_rflags & PSL_T);
269	regs->tf_ss = _udatasel;
270	regs->tf_cs = _ucode32sel;
271	regs->tf_rbx = ps_strings;
272
273	/*
274	 * Arrange to trap the next npx or `fwait' instruction (see npx.c
275	 * for why fwait must be trapped at least if there is an npx or an
276	 * emulator).  This is mainly to handle the case where npx0 is not
277	 * configured, since the npx routines normally set up the trap
278	 * otherwise.  It should be done only at boot time, but doing it
279	 * here allows modifying `npx_exists' for testing the emulator on
280	 * systems with an npx.
281	 */
282	load_cr0(rcr0() | CR0_MP | CR0_TS);
283
284	fpstate_drop(td);
285
286	/* Return via doreti so that we can change to a different %cs */
287	pcb->pcb_flags |= PCB_FULLCTX;
288	td->td_retval[1] = 0;
289}
290
291static u_long	ia32_maxdsiz = IA32_MAXDSIZ;
292SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxdsiz, CTLFLAG_RW, &ia32_maxdsiz, 0, "");
293static u_long	ia32_maxssiz = IA32_MAXSSIZ;
294SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxssiz, CTLFLAG_RW, &ia32_maxssiz, 0, "");
295static u_long	ia32_maxvmem = IA32_MAXVMEM;
296SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxvmem, CTLFLAG_RW, &ia32_maxvmem, 0, "");
297
298static void
299ia32_fixlimits(struct image_params *imgp)
300{
301	struct proc *p = imgp->proc;
302
303	if (ia32_maxdsiz != 0) {
304		if (p->p_rlimit[RLIMIT_DATA].rlim_cur > ia32_maxdsiz ||
305		    p->p_rlimit[RLIMIT_DATA].rlim_max > ia32_maxdsiz) {
306			if (p->p_limit->p_refcnt > 1) {
307				p->p_limit->p_refcnt--;
308				p->p_limit = limcopy(p->p_limit);
309			}
310			if (p->p_rlimit[RLIMIT_DATA].rlim_cur > ia32_maxdsiz)
311				p->p_rlimit[RLIMIT_DATA].rlim_cur =
312				    ia32_maxdsiz;
313			if (p->p_rlimit[RLIMIT_DATA].rlim_max > ia32_maxdsiz)
314				p->p_rlimit[RLIMIT_DATA].rlim_max =
315				    ia32_maxdsiz;
316		}
317	}
318	if (ia32_maxssiz != 0) {
319		if (p->p_rlimit[RLIMIT_STACK].rlim_cur > ia32_maxssiz ||
320		    p->p_rlimit[RLIMIT_STACK].rlim_max > ia32_maxssiz) {
321			if (p->p_limit->p_refcnt > 1) {
322				p->p_limit->p_refcnt--;
323				p->p_limit = limcopy(p->p_limit);
324			}
325			if (p->p_rlimit[RLIMIT_STACK].rlim_cur > ia32_maxssiz)
326				p->p_rlimit[RLIMIT_STACK].rlim_cur =
327				    ia32_maxssiz;
328			if (p->p_rlimit[RLIMIT_STACK].rlim_max > ia32_maxssiz)
329				p->p_rlimit[RLIMIT_STACK].rlim_max =
330				    ia32_maxssiz;
331		}
332	}
333	if (ia32_maxvmem != 0) {
334		if (p->p_rlimit[RLIMIT_VMEM].rlim_cur > ia32_maxvmem ||
335		    p->p_rlimit[RLIMIT_VMEM].rlim_max > ia32_maxvmem) {
336			if (p->p_limit->p_refcnt > 1) {
337				p->p_limit->p_refcnt--;
338				p->p_limit = limcopy(p->p_limit);
339			}
340			if (p->p_rlimit[RLIMIT_VMEM].rlim_cur > ia32_maxvmem)
341				p->p_rlimit[RLIMIT_VMEM].rlim_cur =
342				    ia32_maxvmem;
343			if (p->p_rlimit[RLIMIT_VMEM].rlim_max > ia32_maxvmem)
344				p->p_rlimit[RLIMIT_VMEM].rlim_max =
345				    ia32_maxvmem;
346		}
347	}
348}
349