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