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