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