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/param.h> 35 #include <sys/bus.h> 36 #include <sys/conf.h> 37 #include <sys/fcntl.h> 38 #include <sys/jail.h> 39 #include <sys/malloc.h> 40 #include <sys/namei.h> 41 #include <sys/proc.h> 42 #include <sys/stat.h> 43 #include <sys/syscallsubr.h> 44 #include <sys/vnode.h> 45 46 #include <machine/stdarg.h> 47 48 #include <compat/linux/linux_dtrace.h> 49 #include <compat/linux/linux_mib.h> 50 #include <compat/linux/linux_util.h> 51 52 MALLOC_DEFINE(M_LINUX, "linux", "Linux mode structures"); 53 MALLOC_DEFINE(M_EPOLL, "lepoll", "Linux events structures"); 54 55 FEATURE(linuxulator_v4l, "V4L ioctl wrapper support in the linuxulator"); 56 FEATURE(linuxulator_v4l2, "V4L2 ioctl wrapper support in the linuxulator"); 57 58 /** 59 * Special DTrace provider for the linuxulator. 60 * 61 * In this file we define the provider for the entire linuxulator. All 62 * modules (= files of the linuxulator) use it. 63 * 64 * We define a different name depending on the emulated bitsize, see 65 * ../../<ARCH>/linux{,32}/linux.h, e.g.: 66 * native bitsize = linuxulator 67 * amd64, 32bit emulation = linuxulator32 68 */ 69 LIN_SDT_PROVIDER_DEFINE(linuxulator); 70 LIN_SDT_PROVIDER_DEFINE(linuxulator32); 71 72 char linux_emul_path[MAXPATHLEN] = "/compat/linux"; 73 74 SYSCTL_STRING(_compat_linux, OID_AUTO, emul_path, CTLFLAG_RWTUN, 75 linux_emul_path, sizeof(linux_emul_path), 76 "Linux runtime environment path"); 77 78 int 79 linux_pwd_onexec(struct thread *td) 80 { 81 struct nameidata nd; 82 struct pwd *pwd; 83 int error; 84 85 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, linux_emul_path); 86 error = namei(&nd); 87 if (error != 0) { 88 /* 89 * Do not bother if we are in chroot or jail. 90 */ 91 pwd = pwd_hold(td); 92 if (pwd->pwd_rdir != rootvnode) { 93 pwd_drop(pwd); 94 return (0); 95 } 96 pwd_drop(pwd); 97 return (error); 98 } 99 NDFREE_PNBUF(&nd); 100 pwd_altroot(td, nd.ni_vp); 101 vrele(nd.ni_vp); 102 return (0); 103 } 104 105 void 106 linux_pwd_onexec_native(struct thread *td) 107 { 108 109 pwd_altroot(td, NULL); 110 } 111 112 void 113 linux_msg(const struct thread *td, const char *fmt, ...) 114 { 115 va_list ap; 116 struct proc *p; 117 118 if (linux_debug == 0) 119 return; 120 121 p = td->td_proc; 122 printf("linux: jid %d pid %d (%s): ", p->p_ucred->cr_prison->pr_id, 123 (int)p->p_pid, p->p_comm); 124 va_start(ap, fmt); 125 vprintf(fmt, ap); 126 va_end(ap); 127 printf("\n"); 128 } 129 130 struct device_element 131 { 132 TAILQ_ENTRY(device_element) list; 133 struct linux_device_handler entry; 134 }; 135 136 static TAILQ_HEAD(, device_element) devices = 137 TAILQ_HEAD_INITIALIZER(devices); 138 139 static struct linux_device_handler null_handler = 140 { "mem", "mem", "null", "null", 1, 3, 1}; 141 142 DATA_SET(linux_device_handler_set, null_handler); 143 144 char * 145 linux_driver_get_name_dev(device_t dev) 146 { 147 struct device_element *de; 148 const char *device_name = device_get_name(dev); 149 150 if (device_name == NULL) 151 return (NULL); 152 TAILQ_FOREACH(de, &devices, list) { 153 if (strcmp(device_name, de->entry.bsd_driver_name) == 0) 154 return (de->entry.linux_driver_name); 155 } 156 157 return (NULL); 158 } 159 160 int 161 linux_driver_get_major_minor(const char *node, int *major, int *minor) 162 { 163 struct device_element *de; 164 unsigned long devno; 165 size_t sz; 166 167 if (node == NULL || major == NULL || minor == NULL) 168 return (1); 169 170 sz = sizeof("pts/") - 1; 171 if (strncmp(node, "pts/", sz) == 0 && node[sz] != '\0') { 172 /* 173 * Linux checks major and minors of the slave device 174 * to make sure it's a pty device, so let's make him 175 * believe it is. 176 */ 177 devno = strtoul(node + sz, NULL, 10); 178 *major = 136 + (devno / 256); 179 *minor = devno % 256; 180 return (0); 181 } 182 183 sz = sizeof("dri/card") - 1; 184 if (strncmp(node, "dri/card", sz) == 0 && node[sz] != '\0') { 185 devno = strtoul(node + sz, NULL, 10); 186 *major = 226 + (devno / 256); 187 *minor = devno % 256; 188 return (0); 189 } 190 sz = sizeof("dri/controlD") - 1; 191 if (strncmp(node, "dri/controlD", sz) == 0 && node[sz] != '\0') { 192 devno = strtoul(node + sz, NULL, 10); 193 *major = 226 + (devno / 256); 194 *minor = devno % 256; 195 return (0); 196 } 197 sz = sizeof("dri/renderD") - 1; 198 if (strncmp(node, "dri/renderD", sz) == 0 && node[sz] != '\0') { 199 devno = strtoul(node + sz, NULL, 10); 200 *major = 226 + (devno / 256); 201 *minor = devno % 256; 202 return (0); 203 } 204 sz = sizeof("drm/") - 1; 205 if (strncmp(node, "drm/", sz) == 0 && node[sz] != '\0') { 206 devno = strtoul(node + sz, NULL, 10); 207 *major = 226 + (devno / 256); 208 *minor = devno % 256; 209 return (0); 210 } 211 212 TAILQ_FOREACH(de, &devices, list) { 213 if (strcmp(node, de->entry.bsd_device_name) == 0) { 214 *major = de->entry.linux_major; 215 *minor = de->entry.linux_minor; 216 return (0); 217 } 218 } 219 220 return (1); 221 } 222 223 int 224 linux_vn_get_major_minor(const struct vnode *vp, int *major, int *minor) 225 { 226 int error; 227 228 if (vp->v_type != VCHR) 229 return (ENOTBLK); 230 dev_lock(); 231 if (vp->v_rdev == NULL) { 232 dev_unlock(); 233 return (ENXIO); 234 } 235 error = linux_driver_get_major_minor(devtoname(vp->v_rdev), 236 major, minor); 237 dev_unlock(); 238 return (error); 239 } 240 241 void 242 translate_vnhook_major_minor(struct vnode *vp, struct stat *sb) 243 { 244 int major, minor; 245 246 if (vn_isdisk(vp)) { 247 sb->st_mode &= ~S_IFMT; 248 sb->st_mode |= S_IFBLK; 249 } 250 251 /* 252 * Return the same st_dev for every devfs instance. The reason 253 * for this is to work around an idiosyncrasy of glibc getttynam() 254 * implementation: it checks whether st_dev returned for fd 0 255 * is the same as st_dev returned for the target of /proc/self/fd/0 256 * symlink, and with linux chroots having their own devfs instance, 257 * the check will fail if you chroot into it. 258 */ 259 if (rootdevmp != NULL && vp->v_mount->mnt_vfc == rootdevmp->mnt_vfc) 260 sb->st_dev = rootdevmp->mnt_stat.f_fsid.val[0]; 261 262 if (linux_vn_get_major_minor(vp, &major, &minor) == 0) 263 sb->st_rdev = makedev(major, minor); 264 } 265 266 char * 267 linux_get_char_devices(void) 268 { 269 struct device_element *de; 270 char *temp, *string, *last; 271 char formated[256]; 272 int current_size = 0, string_size = 1024; 273 274 string = malloc(string_size, M_LINUX, M_WAITOK); 275 string[0] = '\000'; 276 last = ""; 277 TAILQ_FOREACH(de, &devices, list) { 278 if (!de->entry.linux_char_device) 279 continue; 280 temp = string; 281 if (strcmp(last, de->entry.bsd_driver_name) != 0) { 282 last = de->entry.bsd_driver_name; 283 284 snprintf(formated, sizeof(formated), "%3d %s\n", 285 de->entry.linux_major, 286 de->entry.linux_device_name); 287 if (strlen(formated) + current_size 288 >= string_size) { 289 string_size *= 2; 290 string = malloc(string_size, 291 M_LINUX, M_WAITOK); 292 bcopy(temp, string, current_size); 293 free(temp, M_LINUX); 294 } 295 strcat(string, formated); 296 current_size = strlen(string); 297 } 298 } 299 300 return (string); 301 } 302 303 void 304 linux_free_get_char_devices(char *string) 305 { 306 307 free(string, M_LINUX); 308 } 309 310 static int linux_major_starting = 200; 311 312 int 313 linux_device_register_handler(struct linux_device_handler *d) 314 { 315 struct device_element *de; 316 317 if (d == NULL) 318 return (EINVAL); 319 320 de = malloc(sizeof(*de), M_LINUX, M_WAITOK); 321 if (d->linux_major < 0) { 322 d->linux_major = linux_major_starting++; 323 } 324 bcopy(d, &de->entry, sizeof(*d)); 325 326 /* Add the element to the list, sorted on span. */ 327 TAILQ_INSERT_TAIL(&devices, de, list); 328 329 return (0); 330 } 331 332 int 333 linux_device_unregister_handler(struct linux_device_handler *d) 334 { 335 struct device_element *de; 336 337 if (d == NULL) 338 return (EINVAL); 339 340 TAILQ_FOREACH(de, &devices, list) { 341 if (bcmp(d, &de->entry, sizeof(*d)) == 0) { 342 TAILQ_REMOVE(&devices, de, list); 343 free(de, M_LINUX); 344 345 return (0); 346 } 347 } 348 349 return (EINVAL); 350 } 351