2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1994 Christos Zoulas
5 * Copyright (c) 1995 Frank van der Linden
6 * Copyright (c) 1995 Scott Bartram
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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.
31 * from: svr4_util.c,v 1.5 1995/01/22 23:44:50 christos Exp
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
40 #include <sys/fcntl.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h>
44 #include <sys/linker_set.h>
45 #include <sys/namei.h>
48 #include <sys/syscallsubr.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
51 #include <sys/vnode.h>
54 #include <net/if_var.h>
55 #include <net/if_types.h>
57 #include <machine/stdarg.h>
59 #include <compat/linux/linux_dtrace.h>
60 #include <compat/linux/linux_mib.h>
61 #include <compat/linux/linux_util.h>
63 MALLOC_DEFINE(M_LINUX, "linux", "Linux mode structures");
64 MALLOC_DEFINE(M_EPOLL, "lepoll", "Linux events structures");
66 FEATURE(linuxulator_v4l, "V4L ioctl wrapper support in the linuxulator");
67 FEATURE(linuxulator_v4l2, "V4L2 ioctl wrapper support in the linuxulator");
70 * Special DTrace provider for the linuxulator.
72 * In this file we define the provider for the entire linuxulator. All
73 * modules (= files of the linuxulator) use it.
75 * We define a different name depending on the emulated bitsize, see
76 * ../../<ARCH>/linux{,32}/linux.h, e.g.:
77 * native bitsize = linuxulator
78 * amd64, 32bit emulation = linuxulator32
80 LIN_SDT_PROVIDER_DEFINE(linuxulator);
81 LIN_SDT_PROVIDER_DEFINE(linuxulator32);
83 char linux_emul_path[MAXPATHLEN] = "/compat/linux";
85 SYSCTL_STRING(_compat_linux, OID_AUTO, emul_path, CTLFLAG_RWTUN,
86 linux_emul_path, sizeof(linux_emul_path),
87 "Linux runtime environment path");
89 static bool use_real_ifnames = false;
90 SYSCTL_BOOL(_compat_linux, OID_AUTO, use_real_ifnames, CTLFLAG_RWTUN,
92 "Use FreeBSD interface names instead of generating ethN aliases");
95 * Search an alternate path before passing pathname arguments on to
96 * system calls. Useful for keeping a separate 'emulation tree'.
98 * If cflag is set, we check if an attempt can be made to create the
99 * named file, i.e. we check if the directory it should be in exists.
102 linux_emul_convpath(const char *path, enum uio_seg pathseg,
103 char **pbuf, int cflag, int dfd)
107 retval = kern_alternate_path(linux_emul_path, path, pathseg, pbuf,
114 linux_msg(const struct thread *td, const char *fmt, ...)
119 if (linux_debug == 0)
123 printf("linux: jid %d pid %d (%s): ", p->p_ucred->cr_prison->pr_id,
124 (int)p->p_pid, p->p_comm);
131 struct device_element
133 TAILQ_ENTRY(device_element) list;
134 struct linux_device_handler entry;
137 static TAILQ_HEAD(, device_element) devices =
138 TAILQ_HEAD_INITIALIZER(devices);
140 static struct linux_device_handler null_handler =
141 { "mem", "mem", "null", "null", 1, 3, 1};
143 DATA_SET(linux_device_handler_set, null_handler);
146 linux_driver_get_name_dev(device_t dev)
148 struct device_element *de;
149 const char *device_name = device_get_name(dev);
151 if (device_name == NULL)
153 TAILQ_FOREACH(de, &devices, list) {
154 if (strcmp(device_name, de->entry.bsd_driver_name) == 0)
155 return (de->entry.linux_driver_name);
162 linux_driver_get_major_minor(const char *node, int *major, int *minor)
164 struct device_element *de;
168 if (node == NULL || major == NULL || minor == NULL)
171 sz = sizeof("pts/") - 1;
172 if (strncmp(node, "pts/", sz) == 0 && node[sz] != '\0') {
174 * Linux checks major and minors of the slave device
175 * to make sure it's a pty device, so let's make him
178 devno = strtoul(node + sz, NULL, 10);
179 *major = 136 + (devno / 256);
180 *minor = devno % 256;
184 sz = sizeof("dri/card") - 1;
185 if (strncmp(node, "dri/card", sz) == 0 && node[sz] != '\0') {
186 devno = strtoul(node + sz, NULL, 10);
187 *major = 226 + (devno / 256);
188 *minor = devno % 256;
191 sz = sizeof("dri/controlD") - 1;
192 if (strncmp(node, "dri/controlD", sz) == 0 && node[sz] != '\0') {
193 devno = strtoul(node + sz, NULL, 10);
194 *major = 226 + (devno / 256);
195 *minor = devno % 256;
198 sz = sizeof("dri/renderD") - 1;
199 if (strncmp(node, "dri/renderD", sz) == 0 && node[sz] != '\0') {
200 devno = strtoul(node + sz, NULL, 10);
201 *major = 226 + (devno / 256);
202 *minor = devno % 256;
205 sz = sizeof("drm/") - 1;
206 if (strncmp(node, "drm/", sz) == 0 && node[sz] != '\0') {
207 devno = strtoul(node + sz, NULL, 10);
208 *major = 226 + (devno / 256);
209 *minor = devno % 256;
213 TAILQ_FOREACH(de, &devices, list) {
214 if (strcmp(node, de->entry.bsd_device_name) == 0) {
215 *major = de->entry.linux_major;
216 *minor = de->entry.linux_minor;
225 linux_vn_get_major_minor(const struct vnode *vp, int *major, int *minor)
229 if (vp->v_type != VCHR)
232 if (vp->v_rdev == NULL) {
236 error = linux_driver_get_major_minor(devtoname(vp->v_rdev),
243 linux_get_char_devices()
245 struct device_element *de;
246 char *temp, *string, *last;
248 int current_size = 0, string_size = 1024;
250 string = malloc(string_size, M_LINUX, M_WAITOK);
253 TAILQ_FOREACH(de, &devices, list) {
254 if (!de->entry.linux_char_device)
257 if (strcmp(last, de->entry.bsd_driver_name) != 0) {
258 last = de->entry.bsd_driver_name;
260 snprintf(formated, sizeof(formated), "%3d %s\n",
261 de->entry.linux_major,
262 de->entry.linux_device_name);
263 if (strlen(formated) + current_size
266 string = malloc(string_size,
268 bcopy(temp, string, current_size);
271 strcat(string, formated);
272 current_size = strlen(string);
280 linux_free_get_char_devices(char *string)
283 free(string, M_LINUX);
286 static int linux_major_starting = 200;
289 linux_device_register_handler(struct linux_device_handler *d)
291 struct device_element *de;
296 de = malloc(sizeof(*de), M_LINUX, M_WAITOK);
297 if (d->linux_major < 0) {
298 d->linux_major = linux_major_starting++;
300 bcopy(d, &de->entry, sizeof(*d));
302 /* Add the element to the list, sorted on span. */
303 TAILQ_INSERT_TAIL(&devices, de, list);
309 linux_device_unregister_handler(struct linux_device_handler *d)
311 struct device_element *de;
316 TAILQ_FOREACH(de, &devices, list) {
317 if (bcmp(d, &de->entry, sizeof(*d)) == 0) {
318 TAILQ_REMOVE(&devices, de, list);
329 linux_use_real_ifname(const struct ifnet *ifp)
331 return (use_real_ifnames || !IFP_IS_ETH(ifp));