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