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