1 /* $NetBSD: compat_util.c,v 1.39 2007/12/08 19:29:37 pooka Exp $ */ 2 3 /*- 4 * Copyright (c) 1994 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas and Frank van der Linden. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: compat_util.c,v 1.39 2007/12/08 19:29:37 pooka Exp $"); 41 42 #include "opt_systrace.h" 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/namei.h> 47 #include <sys/proc.h> 48 #include <sys/file.h> 49 #include <sys/stat.h> 50 #include <sys/filedesc.h> 51 #include <sys/exec.h> 52 #include <sys/ioctl.h> 53 #include <sys/kernel.h> 54 #include <sys/malloc.h> 55 #include <sys/vnode.h> 56 #include <sys/syslog.h> 57 #include <sys/mount.h> 58 59 #include <compat/common/compat_util.h> 60 61 void 62 emul_find_root(struct lwp *l, struct exec_package *epp) 63 { 64 struct nameidata nd; 65 const char *emul_path; 66 67 if (epp->ep_emul_root != NULL) 68 /* We've already found it */ 69 return; 70 71 emul_path = epp->ep_esch->es_emul->e_path; 72 if (emul_path == NULL) 73 /* Emulation doesn't have a root */ 74 return; 75 76 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, emul_path); 77 if (namei(&nd) != 0) 78 /* emulation root doesn't exist */ 79 return; 80 81 epp->ep_emul_root = nd.ni_vp; 82 } 83 84 /* 85 * Search the alternate path for dynamic binary interpreter. If not found 86 * there, check if the interpreter exists in within 'proper' tree. 87 */ 88 int 89 emul_find_interp(struct lwp *l, struct exec_package *epp, const char *itp) 90 { 91 int error; 92 struct nameidata nd; 93 unsigned int flags; 94 95 /* If we haven't found the emulation root already, do so now */ 96 /* Maybe we should remember failures somehow ? */ 97 if (epp->ep_esch->es_emul->e_path != 0 && epp->ep_emul_root == NULL) 98 emul_find_root(l, epp); 99 100 if (epp->ep_interp != NULL) 101 vrele(epp->ep_interp); 102 103 /* We need to use the emulation root for the new program, 104 * not the one for the current process. */ 105 if (epp->ep_emul_root == NULL) 106 flags = FOLLOW; 107 else { 108 nd.ni_erootdir = epp->ep_emul_root; 109 /* hack: Pass in the emulation path for ktrace calls */ 110 nd.ni_next = epp->ep_esch->es_emul->e_path; 111 flags = FOLLOW | TRYEMULROOT | EMULROOTSET; 112 } 113 114 NDINIT(&nd, LOOKUP, flags, UIO_SYSSPACE, itp); 115 error = namei(&nd); 116 if (error != 0) { 117 epp->ep_interp = NULL; 118 return error; 119 } 120 121 /* Save interpreter in case we actually need to load it */ 122 epp->ep_interp = nd.ni_vp; 123 124 return 0; 125 } 126 127 /* 128 * Translate one set of flags to another, based on the entries in 129 * the given table. If 'leftover' is specified, it is filled in 130 * with any flags which could not be translated. 131 */ 132 unsigned long 133 emul_flags_translate(const struct emul_flags_xtab *tab, 134 unsigned long in, unsigned long *leftover) 135 { 136 unsigned long out; 137 138 for (out = 0; tab->omask != 0; tab++) { 139 if ((in & tab->omask) == tab->oval) { 140 in &= ~tab->omask; 141 out |= tab->nval; 142 } 143 } 144 if (leftover != NULL) 145 *leftover = in; 146 return (out); 147 } 148 149 /* The only user left of the stackgap is the systrace code. */ 150 #if SYSTRACE 151 void * 152 stackgap_init(const struct proc *p, size_t sz) 153 { 154 if (sz == 0) 155 sz = STACKGAPLEN; 156 if (sz > STACKGAPLEN) 157 panic("size %lu > STACKGAPLEN", (unsigned long)sz); 158 #define szsigcode ((void *)(p->p_emul->e_esigcode - p->p_emul->e_sigcode)) 159 return (void *)(((unsigned long)p->p_psstr - (unsigned long)szsigcode 160 - sz) & ~ALIGNBYTES); 161 #undef szsigcode 162 } 163 164 165 void * 166 stackgap_alloc(const struct proc *p, void **sgp, size_t sz) 167 { 168 void *n = *sgp; 169 void *nsgp; 170 const struct emul *e = p->p_emul; 171 int sigsize = e->e_esigcode - e->e_sigcode; 172 173 sz = ALIGN(sz); 174 nsgp = (char *)*sgp + sz; 175 if (nsgp > (void *)(((char *)p->p_psstr) - sigsize)) 176 return NULL; 177 *sgp = nsgp; 178 return n; 179 } 180 #endif 181 182 void 183 compat_offseterr(struct vnode *vp, const char *msg) 184 { 185 struct mount *mp; 186 187 mp = vp->v_mount; 188 189 log(LOG_ERR, "%s: dir offset too large on fs %s (mounted from %s)\n", 190 msg, mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname); 191 uprintf("%s: dir offset too large for emulated program\n", msg); 192 } 193