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