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