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