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