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_fixlimits(struct image_params *imgp); 97 98 extern struct sysent freebsd32_sysent[]; 99 100 SYSCTL_NODE(_compat, OID_AUTO, ia32, CTLFLAG_RW, 0, "ia32 mode"); 101 102 struct 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 132 static 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 }; 141 142 SYSINIT(ia32, SI_SUB_EXEC, SI_ORDER_ANY, 143 (sysinit_cfunc_t) elf32_insert_brand_entry, 144 &ia32_brand_info); 145 146 static Elf32_Brandinfo ia32_brand_oinfo = { 147 ELFOSABI_FREEBSD, 148 EM_386, 149 "FreeBSD", 150 NULL, 151 "/usr/libexec/ld-elf.so.1", 152 &ia32_freebsd_sysvec, 153 "/libexec/ld-elf32.so.1", 154 }; 155 156 SYSINIT(oia32, SI_SUB_EXEC, SI_ORDER_ANY, 157 (sysinit_cfunc_t) elf32_insert_brand_entry, 158 &ia32_brand_oinfo); 159 160 /* XXX may be freebsd32 MI */ 161 static register_t * 162 ia32_copyout_strings(struct image_params *imgp) 163 { 164 int argc, envc; 165 u_int32_t *vectp; 166 char *stringp, *destp; 167 u_int32_t *stack_base; 168 struct freebsd32_ps_strings *arginfo; 169 int szsigcode; 170 171 /* 172 * Calculate string base and vector table pointers. 173 * Also deal with signal trampoline code for this exec type. 174 */ 175 arginfo = (struct freebsd32_ps_strings *)FREEBSD32_PS_STRINGS; 176 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); 177 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 178 roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 179 180 /* 181 * install sigcode 182 */ 183 if (szsigcode) 184 copyout(imgp->proc->p_sysent->sv_sigcode, 185 ((caddr_t)arginfo - szsigcode), szsigcode); 186 187 /* 188 * If we have a valid auxargs ptr, prepare some room 189 * on the stack. 190 */ 191 if (imgp->auxargs) { 192 /* 193 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 194 * lower compatibility. 195 */ 196 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size 197 : (AT_COUNT * 2); 198 /* 199 * The '+ 2' is for the null pointers at the end of each of 200 * the arg and env vector sets,and imgp->auxarg_size is room 201 * for argument of Runtime loader. 202 */ 203 vectp = (u_int32_t *) (destp - (imgp->argc + imgp->envc + 2 + 204 imgp->auxarg_size) * sizeof(u_int32_t)); 205 206 } else 207 /* 208 * The '+ 2' is for the null pointers at the end of each of 209 * the arg and env vector sets 210 */ 211 vectp = (u_int32_t *) 212 (destp - (imgp->argc + imgp->envc + 2) * sizeof(u_int32_t)); 213 214 /* 215 * vectp also becomes our initial stack base 216 */ 217 stack_base = vectp; 218 219 stringp = imgp->stringbase; 220 argc = imgp->argc; 221 envc = imgp->envc; 222 /* 223 * Copy out strings - arguments and environment. 224 */ 225 copyout(stringp, destp, ARG_MAX - imgp->stringspace); 226 227 /* 228 * Fill in "ps_strings" struct for ps, w, etc. 229 */ 230 suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp); 231 suword32(&arginfo->ps_nargvstr, argc); 232 233 /* 234 * Fill in argument portion of vector table. 235 */ 236 for (; argc > 0; --argc) { 237 suword32(vectp++, (u_int32_t)(intptr_t)destp); 238 while (*stringp++ != 0) 239 destp++; 240 destp++; 241 } 242 243 /* a null vector table pointer separates the argp's from the envp's */ 244 suword32(vectp++, 0); 245 246 suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp); 247 suword32(&arginfo->ps_nenvstr, envc); 248 249 /* 250 * Fill in environment portion of vector table. 251 */ 252 for (; envc > 0; --envc) { 253 suword32(vectp++, (u_int32_t)(intptr_t)destp); 254 while (*stringp++ != 0) 255 destp++; 256 destp++; 257 } 258 259 /* end of vector table is a null pointer */ 260 suword32(vectp, 0); 261 262 return ((register_t *)stack_base); 263 } 264 265 static u_long ia32_maxdsiz = IA32_MAXDSIZ; 266 SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxdsiz, CTLFLAG_RW, &ia32_maxdsiz, 0, ""); 267 static u_long ia32_maxssiz = IA32_MAXSSIZ; 268 SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxssiz, CTLFLAG_RW, &ia32_maxssiz, 0, ""); 269 static u_long ia32_maxvmem = IA32_MAXVMEM; 270 SYSCTL_ULONG(_compat_ia32, OID_AUTO, maxvmem, CTLFLAG_RW, &ia32_maxvmem, 0, ""); 271 272 static void 273 ia32_fixlimits(struct image_params *imgp) 274 { 275 struct proc *p = imgp->proc; 276 struct plimit *oldlim, *newlim; 277 278 if (ia32_maxdsiz == 0 && ia32_maxssiz == 0 && ia32_maxvmem == 0) 279 return; 280 newlim = lim_alloc(); 281 PROC_LOCK(p); 282 oldlim = p->p_limit; 283 lim_copy(newlim, oldlim); 284 if (ia32_maxdsiz != 0) { 285 if (newlim->pl_rlimit[RLIMIT_DATA].rlim_cur > ia32_maxdsiz) 286 newlim->pl_rlimit[RLIMIT_DATA].rlim_cur = ia32_maxdsiz; 287 if (newlim->pl_rlimit[RLIMIT_DATA].rlim_max > ia32_maxdsiz) 288 newlim->pl_rlimit[RLIMIT_DATA].rlim_max = ia32_maxdsiz; 289 } 290 if (ia32_maxssiz != 0) { 291 if (newlim->pl_rlimit[RLIMIT_STACK].rlim_cur > ia32_maxssiz) 292 newlim->pl_rlimit[RLIMIT_STACK].rlim_cur = ia32_maxssiz; 293 if (newlim->pl_rlimit[RLIMIT_STACK].rlim_max > ia32_maxssiz) 294 newlim->pl_rlimit[RLIMIT_STACK].rlim_max = ia32_maxssiz; 295 } 296 if (ia32_maxvmem != 0) { 297 if (newlim->pl_rlimit[RLIMIT_VMEM].rlim_cur > ia32_maxvmem) 298 newlim->pl_rlimit[RLIMIT_VMEM].rlim_cur = ia32_maxvmem; 299 if (newlim->pl_rlimit[RLIMIT_VMEM].rlim_max > ia32_maxvmem) 300 newlim->pl_rlimit[RLIMIT_VMEM].rlim_max = ia32_maxvmem; 301 } 302 p->p_limit = newlim; 303 PROC_UNLOCK(p); 304 lim_free(oldlim); 305 } 306