1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1994 Christos Zoulas 5 * Copyright (c) 1995 Frank van der Linden 6 * Copyright (c) 1995 Scott Bartram 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 * from: svr4_util.c,v 1.5 1995/01/22 23:44:50 christos Exp 32 */ 33 34 #include <sys/cdefs.h> 35 #include <sys/param.h> 36 #include <sys/types.h> 37 #include <sys/bus.h> 38 #include <sys/conf.h> 39 #include <sys/fcntl.h> 40 #include <sys/jail.h> 41 #include <sys/malloc.h> 42 #include <sys/namei.h> 43 #include <sys/proc.h> 44 #include <sys/stat.h> 45 #include <sys/syscallsubr.h> 46 #include <sys/vnode.h> 47 48 #include <machine/stdarg.h> 49 50 #include <compat/linux/linux_dtrace.h> 51 #include <compat/linux/linux_mib.h> 52 #include <compat/linux/linux_util.h> 53 54 MALLOC_DEFINE(M_LINUX, "linux", "Linux mode structures"); 55 MALLOC_DEFINE(M_EPOLL, "lepoll", "Linux events structures"); 56 57 FEATURE(linuxulator_v4l, "V4L ioctl wrapper support in the linuxulator"); 58 FEATURE(linuxulator_v4l2, "V4L2 ioctl wrapper support in the linuxulator"); 59 60 /** 61 * Special DTrace provider for the linuxulator. 62 * 63 * In this file we define the provider for the entire linuxulator. All 64 * modules (= files of the linuxulator) use it. 65 * 66 * We define a different name depending on the emulated bitsize, see 67 * ../../<ARCH>/linux{,32}/linux.h, e.g.: 68 * native bitsize = linuxulator 69 * amd64, 32bit emulation = linuxulator32 70 */ 71 LIN_SDT_PROVIDER_DEFINE(linuxulator); 72 LIN_SDT_PROVIDER_DEFINE(linuxulator32); 73 74 char linux_emul_path[MAXPATHLEN] = "/compat/linux"; 75 76 SYSCTL_STRING(_compat_linux, OID_AUTO, emul_path, CTLFLAG_RWTUN, 77 linux_emul_path, sizeof(linux_emul_path), 78 "Linux runtime environment path"); 79 80 int 81 linux_pwd_onexec(struct thread *td) 82 { 83 struct nameidata nd; 84 struct pwd *pwd; 85 int error; 86 87 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, linux_emul_path); 88 error = namei(&nd); 89 if (error != 0) { 90 /* 91 * Do not bother if we are in chroot or jail. 92 */ 93 pwd = pwd_hold(td); 94 if (pwd->pwd_rdir != rootvnode) { 95 pwd_drop(pwd); 96 return (0); 97 } 98 pwd_drop(pwd); 99 return (error); 100 } 101 NDFREE_PNBUF(&nd); 102 pwd_altroot(td, nd.ni_vp); 103 vrele(nd.ni_vp); 104 return (0); 105 } 106 107 void 108 linux_pwd_onexec_native(struct thread *td) 109 { 110 111 pwd_altroot(td, NULL); 112 } 113 114 void 115 linux_msg(const struct thread *td, const char *fmt, ...) 116 { 117 va_list ap; 118 struct proc *p; 119 120 if (linux_debug == 0) 121 return; 122 123 p = td->td_proc; 124 printf("linux: jid %d pid %d (%s): ", p->p_ucred->cr_prison->pr_id, 125 (int)p->p_pid, p->p_comm); 126 va_start(ap, fmt); 127 vprintf(fmt, ap); 128 va_end(ap); 129 printf("\n"); 130 } 131 132 struct device_element 133 { 134 TAILQ_ENTRY(device_element) list; 135 struct linux_device_handler entry; 136 }; 137 138 static TAILQ_HEAD(, device_element) devices = 139 TAILQ_HEAD_INITIALIZER(devices); 140 141 static struct linux_device_handler null_handler = 142 { "mem", "mem", "null", "null", 1, 3, 1}; 143 144 DATA_SET(linux_device_handler_set, null_handler); 145 146 char * 147 linux_driver_get_name_dev(device_t dev) 148 { 149 struct device_element *de; 150 const char *device_name = device_get_name(dev); 151 152 if (device_name == NULL) 153 return (NULL); 154 TAILQ_FOREACH(de, &devices, list) { 155 if (strcmp(device_name, de->entry.bsd_driver_name) == 0) 156 return (de->entry.linux_driver_name); 157 } 158 159 return (NULL); 160 } 161 162 int 163 linux_driver_get_major_minor(const char *node, int *major, int *minor) 164 { 165 struct device_element *de; 166 unsigned long devno; 167 size_t sz; 168 169 if (node == NULL || major == NULL || minor == NULL) 170 return (1); 171 172 sz = sizeof("pts/") - 1; 173 if (strncmp(node, "pts/", sz) == 0 && node[sz] != '\0') { 174 /* 175 * Linux checks major and minors of the slave device 176 * to make sure it's a pty device, so let's make him 177 * believe it is. 178 */ 179 devno = strtoul(node + sz, NULL, 10); 180 *major = 136 + (devno / 256); 181 *minor = devno % 256; 182 return (0); 183 } 184 185 sz = sizeof("dri/card") - 1; 186 if (strncmp(node, "dri/card", sz) == 0 && node[sz] != '\0') { 187 devno = strtoul(node + sz, NULL, 10); 188 *major = 226 + (devno / 256); 189 *minor = devno % 256; 190 return (0); 191 } 192 sz = sizeof("dri/controlD") - 1; 193 if (strncmp(node, "dri/controlD", sz) == 0 && node[sz] != '\0') { 194 devno = strtoul(node + sz, NULL, 10); 195 *major = 226 + (devno / 256); 196 *minor = devno % 256; 197 return (0); 198 } 199 sz = sizeof("dri/renderD") - 1; 200 if (strncmp(node, "dri/renderD", sz) == 0 && node[sz] != '\0') { 201 devno = strtoul(node + sz, NULL, 10); 202 *major = 226 + (devno / 256); 203 *minor = devno % 256; 204 return (0); 205 } 206 sz = sizeof("drm/") - 1; 207 if (strncmp(node, "drm/", sz) == 0 && node[sz] != '\0') { 208 devno = strtoul(node + sz, NULL, 10); 209 *major = 226 + (devno / 256); 210 *minor = devno % 256; 211 return (0); 212 } 213 214 TAILQ_FOREACH(de, &devices, list) { 215 if (strcmp(node, de->entry.bsd_device_name) == 0) { 216 *major = de->entry.linux_major; 217 *minor = de->entry.linux_minor; 218 return (0); 219 } 220 } 221 222 return (1); 223 } 224 225 int 226 linux_vn_get_major_minor(const struct vnode *vp, int *major, int *minor) 227 { 228 int error; 229 230 if (vp->v_type != VCHR) 231 return (ENOTBLK); 232 dev_lock(); 233 if (vp->v_rdev == NULL) { 234 dev_unlock(); 235 return (ENXIO); 236 } 237 error = linux_driver_get_major_minor(devtoname(vp->v_rdev), 238 major, minor); 239 dev_unlock(); 240 return (error); 241 } 242 243 void 244 translate_vnhook_major_minor(struct vnode *vp, struct stat *sb) 245 { 246 int major, minor; 247 248 if (vn_isdisk(vp)) { 249 sb->st_mode &= ~S_IFMT; 250 sb->st_mode |= S_IFBLK; 251 } 252 253 /* 254 * Return the same st_dev for every devfs instance. The reason 255 * for this is to work around an idiosyncrasy of glibc getttynam() 256 * implementation: it checks whether st_dev returned for fd 0 257 * is the same as st_dev returned for the target of /proc/self/fd/0 258 * symlink, and with linux chroots having their own devfs instance, 259 * the check will fail if you chroot into it. 260 */ 261 if (rootdevmp != NULL && vp->v_mount->mnt_vfc == rootdevmp->mnt_vfc) 262 sb->st_dev = rootdevmp->mnt_stat.f_fsid.val[0]; 263 264 if (linux_vn_get_major_minor(vp, &major, &minor) == 0) 265 sb->st_rdev = makedev(major, minor); 266 } 267 268 char * 269 linux_get_char_devices(void) 270 { 271 struct device_element *de; 272 char *temp, *string, *last; 273 char formated[256]; 274 int current_size = 0, string_size = 1024; 275 276 string = malloc(string_size, M_LINUX, M_WAITOK); 277 string[0] = '\000'; 278 last = ""; 279 TAILQ_FOREACH(de, &devices, list) { 280 if (!de->entry.linux_char_device) 281 continue; 282 temp = string; 283 if (strcmp(last, de->entry.bsd_driver_name) != 0) { 284 last = de->entry.bsd_driver_name; 285 286 snprintf(formated, sizeof(formated), "%3d %s\n", 287 de->entry.linux_major, 288 de->entry.linux_device_name); 289 if (strlen(formated) + current_size 290 >= string_size) { 291 string_size *= 2; 292 string = malloc(string_size, 293 M_LINUX, M_WAITOK); 294 bcopy(temp, string, current_size); 295 free(temp, M_LINUX); 296 } 297 strcat(string, formated); 298 current_size = strlen(string); 299 } 300 } 301 302 return (string); 303 } 304 305 void 306 linux_free_get_char_devices(char *string) 307 { 308 309 free(string, M_LINUX); 310 } 311 312 static int linux_major_starting = 200; 313 314 int 315 linux_device_register_handler(struct linux_device_handler *d) 316 { 317 struct device_element *de; 318 319 if (d == NULL) 320 return (EINVAL); 321 322 de = malloc(sizeof(*de), M_LINUX, M_WAITOK); 323 if (d->linux_major < 0) { 324 d->linux_major = linux_major_starting++; 325 } 326 bcopy(d, &de->entry, sizeof(*d)); 327 328 /* Add the element to the list, sorted on span. */ 329 TAILQ_INSERT_TAIL(&devices, de, list); 330 331 return (0); 332 } 333 334 int 335 linux_device_unregister_handler(struct linux_device_handler *d) 336 { 337 struct device_element *de; 338 339 if (d == NULL) 340 return (EINVAL); 341 342 TAILQ_FOREACH(de, &devices, list) { 343 if (bcmp(d, &de->entry, sizeof(*d)) == 0) { 344 TAILQ_REMOVE(&devices, de, list); 345 free(de, M_LINUX); 346 347 return (0); 348 } 349 } 350 351 return (EINVAL); 352 } 353