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>
38 #include <sys/types.h>
41 #include <sys/fcntl.h>
43 #include <sys/malloc.h>
44 #include <sys/namei.h>
47 #include <sys/syscallsubr.h>
48 #include <sys/vnode.h>
50 #include <machine/stdarg.h>
52 #include <compat/linux/linux_dtrace.h>
53 #include <compat/linux/linux_mib.h>
54 #include <compat/linux/linux_util.h>
56 MALLOC_DEFINE(M_LINUX, "linux", "Linux mode structures");
57 MALLOC_DEFINE(M_EPOLL, "lepoll", "Linux events structures");
59 FEATURE(linuxulator_v4l, "V4L ioctl wrapper support in the linuxulator");
60 FEATURE(linuxulator_v4l2, "V4L2 ioctl wrapper support in the linuxulator");
63 * Special DTrace provider for the linuxulator.
65 * In this file we define the provider for the entire linuxulator. All
66 * modules (= files of the linuxulator) use it.
68 * We define a different name depending on the emulated bitsize, see
69 * ../../<ARCH>/linux{,32}/linux.h, e.g.:
70 * native bitsize = linuxulator
71 * amd64, 32bit emulation = linuxulator32
73 LIN_SDT_PROVIDER_DEFINE(linuxulator);
74 LIN_SDT_PROVIDER_DEFINE(linuxulator32);
76 char linux_emul_path[MAXPATHLEN] = "/compat/linux";
78 SYSCTL_STRING(_compat_linux, OID_AUTO, emul_path, CTLFLAG_RWTUN,
79 linux_emul_path, sizeof(linux_emul_path),
80 "Linux runtime environment path");
83 * Search an alternate path before passing pathname arguments on to
84 * system calls. Useful for keeping a separate 'emulation tree'.
86 * If cflag is set, we check if an attempt can be made to create the
87 * named file, i.e. we check if the directory it should be in exists.
90 linux_emul_convpath(const char *path, enum uio_seg pathseg,
91 char **pbuf, int cflag, int dfd)
95 retval = kern_alternate_path(linux_emul_path, path, pathseg, pbuf,
102 linux_msg(const struct thread *td, const char *fmt, ...)
107 if (linux_debug == 0)
111 printf("linux: jid %d pid %d (%s): ", p->p_ucred->cr_prison->pr_id,
112 (int)p->p_pid, p->p_comm);
119 struct device_element
121 TAILQ_ENTRY(device_element) list;
122 struct linux_device_handler entry;
125 static TAILQ_HEAD(, device_element) devices =
126 TAILQ_HEAD_INITIALIZER(devices);
128 static struct linux_device_handler null_handler =
129 { "mem", "mem", "null", "null", 1, 3, 1};
131 DATA_SET(linux_device_handler_set, null_handler);
134 linux_driver_get_name_dev(device_t dev)
136 struct device_element *de;
137 const char *device_name = device_get_name(dev);
139 if (device_name == NULL)
141 TAILQ_FOREACH(de, &devices, list) {
142 if (strcmp(device_name, de->entry.bsd_driver_name) == 0)
143 return (de->entry.linux_driver_name);
150 linux_driver_get_major_minor(const char *node, int *major, int *minor)
152 struct device_element *de;
156 if (node == NULL || major == NULL || minor == NULL)
159 sz = sizeof("pts/") - 1;
160 if (strncmp(node, "pts/", sz) == 0 && node[sz] != '\0') {
162 * Linux checks major and minors of the slave device
163 * to make sure it's a pty device, so let's make him
166 devno = strtoul(node + sz, NULL, 10);
167 *major = 136 + (devno / 256);
168 *minor = devno % 256;
172 sz = sizeof("dri/card") - 1;
173 if (strncmp(node, "dri/card", sz) == 0 && node[sz] != '\0') {
174 devno = strtoul(node + sz, NULL, 10);
175 *major = 226 + (devno / 256);
176 *minor = devno % 256;
179 sz = sizeof("dri/controlD") - 1;
180 if (strncmp(node, "dri/controlD", sz) == 0 && node[sz] != '\0') {
181 devno = strtoul(node + sz, NULL, 10);
182 *major = 226 + (devno / 256);
183 *minor = devno % 256;
186 sz = sizeof("dri/renderD") - 1;
187 if (strncmp(node, "dri/renderD", sz) == 0 && node[sz] != '\0') {
188 devno = strtoul(node + sz, NULL, 10);
189 *major = 226 + (devno / 256);
190 *minor = devno % 256;
193 sz = sizeof("drm/") - 1;
194 if (strncmp(node, "drm/", sz) == 0 && node[sz] != '\0') {
195 devno = strtoul(node + sz, NULL, 10);
196 *major = 226 + (devno / 256);
197 *minor = devno % 256;
201 TAILQ_FOREACH(de, &devices, list) {
202 if (strcmp(node, de->entry.bsd_device_name) == 0) {
203 *major = de->entry.linux_major;
204 *minor = de->entry.linux_minor;
213 linux_vn_get_major_minor(const struct vnode *vp, int *major, int *minor)
217 if (vp->v_type != VCHR)
220 if (vp->v_rdev == NULL) {
224 error = linux_driver_get_major_minor(devtoname(vp->v_rdev),
231 translate_vnhook_major_minor(struct vnode *vp, struct stat *sb)
236 sb->st_mode &= ~S_IFMT;
237 sb->st_mode |= S_IFBLK;
241 * Return the same st_dev for every devfs instance. The reason
242 * for this is to work around an idiosyncrasy of glibc getttynam()
243 * implementation: it checks whether st_dev returned for fd 0
244 * is the same as st_dev returned for the target of /proc/self/fd/0
245 * symlink, and with linux chroots having their own devfs instance,
246 * the check will fail if you chroot into it.
248 if (rootdevmp != NULL && vp->v_mount->mnt_vfc == rootdevmp->mnt_vfc)
249 sb->st_dev = rootdevmp->mnt_stat.f_fsid.val[0];
251 if (linux_vn_get_major_minor(vp, &major, &minor) == 0)
252 sb->st_rdev = makedev(major, minor);
256 linux_get_char_devices(void)
258 struct device_element *de;
259 char *temp, *string, *last;
261 int current_size = 0, string_size = 1024;
263 string = malloc(string_size, M_LINUX, M_WAITOK);
266 TAILQ_FOREACH(de, &devices, list) {
267 if (!de->entry.linux_char_device)
270 if (strcmp(last, de->entry.bsd_driver_name) != 0) {
271 last = de->entry.bsd_driver_name;
273 snprintf(formated, sizeof(formated), "%3d %s\n",
274 de->entry.linux_major,
275 de->entry.linux_device_name);
276 if (strlen(formated) + current_size
279 string = malloc(string_size,
281 bcopy(temp, string, current_size);
284 strcat(string, formated);
285 current_size = strlen(string);
293 linux_free_get_char_devices(char *string)
296 free(string, M_LINUX);
299 static int linux_major_starting = 200;
302 linux_device_register_handler(struct linux_device_handler *d)
304 struct device_element *de;
309 de = malloc(sizeof(*de), M_LINUX, M_WAITOK);
310 if (d->linux_major < 0) {
311 d->linux_major = linux_major_starting++;
313 bcopy(d, &de->entry, sizeof(*d));
315 /* Add the element to the list, sorted on span. */
316 TAILQ_INSERT_TAIL(&devices, de, list);
322 linux_device_unregister_handler(struct linux_device_handler *d)
324 struct device_element *de;
329 TAILQ_FOREACH(de, &devices, list) {
330 if (bcmp(d, &de->entry, sizeof(*d)) == 0) {
331 TAILQ_REMOVE(&devices, de, list);