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 __FBSDID("$FreeBSD$"); 36 37 #include <sys/param.h> 38 #include <sys/bus.h> 39 #include <sys/conf.h> 40 #include <sys/fcntl.h> 41 #include <sys/jail.h> 42 #include <sys/malloc.h> 43 #include <sys/kernel.h> 44 #include <sys/linker_set.h> 45 #include <sys/namei.h> 46 #include <sys/proc.h> 47 #include <sys/sdt.h> 48 #include <sys/syscallsubr.h> 49 #include <sys/sysctl.h> 50 #include <sys/systm.h> 51 #include <sys/vnode.h> 52 53 #include <machine/stdarg.h> 54 55 #include <compat/linux/linux_dtrace.h> 56 #include <compat/linux/linux_mib.h> 57 #include <compat/linux/linux_util.h> 58 59 MALLOC_DEFINE(M_LINUX, "linux", "Linux mode structures"); 60 MALLOC_DEFINE(M_EPOLL, "lepoll", "Linux events structures"); 61 62 FEATURE(linuxulator_v4l, "V4L ioctl wrapper support in the linuxulator"); 63 FEATURE(linuxulator_v4l2, "V4L2 ioctl wrapper support in the linuxulator"); 64 65 /** 66 * Special DTrace provider for the linuxulator. 67 * 68 * In this file we define the provider for the entire linuxulator. All 69 * modules (= files of the linuxulator) use it. 70 * 71 * We define a different name depending on the emulated bitsize, see 72 * ../../<ARCH>/linux{,32}/linux.h, e.g.: 73 * native bitsize = linuxulator 74 * amd64, 32bit emulation = linuxulator32 75 */ 76 LIN_SDT_PROVIDER_DEFINE(linuxulator); 77 LIN_SDT_PROVIDER_DEFINE(linuxulator32); 78 79 char linux_emul_path[MAXPATHLEN] = "/compat/linux"; 80 81 SYSCTL_STRING(_compat_linux, OID_AUTO, emul_path, CTLFLAG_RWTUN, 82 linux_emul_path, sizeof(linux_emul_path), 83 "Linux runtime environment path"); 84 85 /* 86 * Search an alternate path before passing pathname arguments on to 87 * system calls. Useful for keeping a separate 'emulation tree'. 88 * 89 * If cflag is set, we check if an attempt can be made to create the 90 * named file, i.e. we check if the directory it should be in exists. 91 */ 92 int 93 linux_emul_convpath(const char *path, enum uio_seg pathseg, 94 char **pbuf, int cflag, int dfd) 95 { 96 int retval; 97 98 retval = kern_alternate_path(linux_emul_path, path, pathseg, pbuf, 99 cflag, dfd); 100 101 return (retval); 102 } 103 104 void 105 linux_msg(const struct thread *td, const char *fmt, ...) 106 { 107 va_list ap; 108 struct proc *p; 109 110 if (linux_debug == 0) 111 return; 112 113 p = td->td_proc; 114 printf("linux: jid %d pid %d (%s): ", p->p_ucred->cr_prison->pr_id, 115 (int)p->p_pid, p->p_comm); 116 va_start(ap, fmt); 117 vprintf(fmt, ap); 118 va_end(ap); 119 printf("\n"); 120 } 121 122 struct device_element 123 { 124 TAILQ_ENTRY(device_element) list; 125 struct linux_device_handler entry; 126 }; 127 128 static TAILQ_HEAD(, device_element) devices = 129 TAILQ_HEAD_INITIALIZER(devices); 130 131 static struct linux_device_handler null_handler = 132 { "mem", "mem", "null", "null", 1, 3, 1}; 133 134 DATA_SET(linux_device_handler_set, null_handler); 135 136 char * 137 linux_driver_get_name_dev(device_t dev) 138 { 139 struct device_element *de; 140 const char *device_name = device_get_name(dev); 141 142 if (device_name == NULL) 143 return (NULL); 144 TAILQ_FOREACH(de, &devices, list) { 145 if (strcmp(device_name, de->entry.bsd_driver_name) == 0) 146 return (de->entry.linux_driver_name); 147 } 148 149 return (NULL); 150 } 151 152 int 153 linux_driver_get_major_minor(const char *node, int *major, int *minor) 154 { 155 struct device_element *de; 156 unsigned long devno; 157 size_t sz; 158 159 if (node == NULL || major == NULL || minor == NULL) 160 return (1); 161 162 sz = sizeof("pts/") - 1; 163 if (strncmp(node, "pts/", sz) == 0 && node[sz] != '\0') { 164 /* 165 * Linux checks major and minors of the slave device 166 * to make sure it's a pty device, so let's make him 167 * believe it is. 168 */ 169 devno = strtoul(node + sz, NULL, 10); 170 *major = 136 + (devno / 256); 171 *minor = devno % 256; 172 return (0); 173 } 174 175 sz = sizeof("dri/card") - 1; 176 if (strncmp(node, "dri/card", sz) == 0 && node[sz] != '\0') { 177 devno = strtoul(node + sz, NULL, 10); 178 *major = 226 + (devno / 256); 179 *minor = devno % 256; 180 return (0); 181 } 182 sz = sizeof("dri/controlD") - 1; 183 if (strncmp(node, "dri/controlD", sz) == 0 && node[sz] != '\0') { 184 devno = strtoul(node + sz, NULL, 10); 185 *major = 226 + (devno / 256); 186 *minor = devno % 256; 187 return (0); 188 } 189 sz = sizeof("dri/renderD") - 1; 190 if (strncmp(node, "dri/renderD", sz) == 0 && node[sz] != '\0') { 191 devno = strtoul(node + sz, NULL, 10); 192 *major = 226 + (devno / 256); 193 *minor = devno % 256; 194 return (0); 195 } 196 sz = sizeof("drm/") - 1; 197 if (strncmp(node, "drm/", sz) == 0 && node[sz] != '\0') { 198 devno = strtoul(node + sz, NULL, 10); 199 *major = 226 + (devno / 256); 200 *minor = devno % 256; 201 return (0); 202 } 203 204 TAILQ_FOREACH(de, &devices, list) { 205 if (strcmp(node, de->entry.bsd_device_name) == 0) { 206 *major = de->entry.linux_major; 207 *minor = de->entry.linux_minor; 208 return (0); 209 } 210 } 211 212 return (1); 213 } 214 215 int 216 linux_vn_get_major_minor(const struct vnode *vp, int *major, int *minor) 217 { 218 int error; 219 220 if (vp->v_type != VCHR) 221 return (ENOTBLK); 222 dev_lock(); 223 if (vp->v_rdev == NULL) { 224 dev_unlock(); 225 return (ENXIO); 226 } 227 error = linux_driver_get_major_minor(devtoname(vp->v_rdev), 228 major, minor); 229 dev_unlock(); 230 return (error); 231 } 232 233 char * 234 linux_get_char_devices() 235 { 236 struct device_element *de; 237 char *temp, *string, *last; 238 char formated[256]; 239 int current_size = 0, string_size = 1024; 240 241 string = malloc(string_size, M_LINUX, M_WAITOK); 242 string[0] = '\000'; 243 last = ""; 244 TAILQ_FOREACH(de, &devices, list) { 245 if (!de->entry.linux_char_device) 246 continue; 247 temp = string; 248 if (strcmp(last, de->entry.bsd_driver_name) != 0) { 249 last = de->entry.bsd_driver_name; 250 251 snprintf(formated, sizeof(formated), "%3d %s\n", 252 de->entry.linux_major, 253 de->entry.linux_device_name); 254 if (strlen(formated) + current_size 255 >= string_size) { 256 string_size *= 2; 257 string = malloc(string_size, 258 M_LINUX, M_WAITOK); 259 bcopy(temp, string, current_size); 260 free(temp, M_LINUX); 261 } 262 strcat(string, formated); 263 current_size = strlen(string); 264 } 265 } 266 267 return (string); 268 } 269 270 void 271 linux_free_get_char_devices(char *string) 272 { 273 274 free(string, M_LINUX); 275 } 276 277 static int linux_major_starting = 200; 278 279 int 280 linux_device_register_handler(struct linux_device_handler *d) 281 { 282 struct device_element *de; 283 284 if (d == NULL) 285 return (EINVAL); 286 287 de = malloc(sizeof(*de), M_LINUX, M_WAITOK); 288 if (d->linux_major < 0) { 289 d->linux_major = linux_major_starting++; 290 } 291 bcopy(d, &de->entry, sizeof(*d)); 292 293 /* Add the element to the list, sorted on span. */ 294 TAILQ_INSERT_TAIL(&devices, de, list); 295 296 return (0); 297 } 298 299 int 300 linux_device_unregister_handler(struct linux_device_handler *d) 301 { 302 struct device_element *de; 303 304 if (d == NULL) 305 return (EINVAL); 306 307 TAILQ_FOREACH(de, &devices, list) { 308 if (bcmp(d, &de->entry, sizeof(*d)) == 0) { 309 TAILQ_REMOVE(&devices, de, list); 310 free(de, M_LINUX); 311 312 return (0); 313 } 314 } 315 316 return (EINVAL); 317 } 318