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/mutex.h>
46 #include <sys/namei.h>
49 #include <sys/syscallsubr.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
52 #include <sys/vnode.h>
54 #include <machine/stdarg.h>
56 #include <compat/linux/linux_mib.h>
57 #include <compat/linux/linux_util.h>
59 MALLOC_DEFINE(M_LINUX, "linux", "Linux mode structures");
60 MALLOC_DEFINE(M_EPOLL, "lepoll", "Linux events structures");
61 MALLOC_DEFINE(M_FUTEX, "futex", "Linux futexes");
62 MALLOC_DEFINE(M_FUTEX_WP, "futex wp", "Linux futex waiting proc");
64 char linux_emul_path[MAXPATHLEN] = "/compat/linux";
66 SYSCTL_STRING(_compat_linux, OID_AUTO, emul_path, CTLFLAG_RWTUN,
67 linux_emul_path, sizeof(linux_emul_path),
68 "Linux runtime environment path");
71 * Search an alternate path before passing pathname arguments on to
72 * system calls. Useful for keeping a separate 'emulation tree'.
74 * If cflag is set, we check if an attempt can be made to create the
75 * named file, i.e. we check if the directory it should be in exists.
78 linux_emul_convpath(struct thread *td, const char *path, enum uio_seg pathseg,
79 char **pbuf, int cflag, int dfd)
83 retval = kern_alternate_path(td, linux_emul_path, path, pathseg, pbuf,
90 linux_msg(const struct thread *td, const char *fmt, ...)
99 printf("linux: pid %d (%s): ", (int)p->p_pid, p->p_comm);
106 struct device_element
108 TAILQ_ENTRY(device_element) list;
109 struct linux_device_handler entry;
112 static TAILQ_HEAD(, device_element) devices =
113 TAILQ_HEAD_INITIALIZER(devices);
115 static struct linux_device_handler null_handler =
116 { "mem", "mem", "null", "null", 1, 3, 1};
118 DATA_SET(linux_device_handler_set, null_handler);
121 linux_driver_get_name_dev(device_t dev)
123 struct device_element *de;
124 const char *device_name = device_get_name(dev);
126 if (device_name == NULL)
128 TAILQ_FOREACH(de, &devices, list) {
129 if (strcmp(device_name, de->entry.bsd_driver_name) == 0)
130 return (de->entry.linux_driver_name);
137 linux_driver_get_major_minor(const char *node, int *major, int *minor)
139 struct device_element *de;
143 if (node == NULL || major == NULL || minor == NULL)
146 sz = sizeof("pts/") - 1;
147 if (strncmp(node, "pts/", sz) == 0 && node[sz] != '\0') {
149 * Linux checks major and minors of the slave device
150 * to make sure it's a pty device, so let's make him
153 devno = strtoul(node + sz, NULL, 10);
154 *major = 136 + (devno / 256);
155 *minor = devno % 256;
159 sz = sizeof("dri/card") - 1;
160 if (strncmp(node, "dri/card", sz) == 0 && node[sz] != '\0') {
161 devno = strtoul(node + sz, NULL, 10);
162 *major = 226 + (devno / 256);
163 *minor = devno % 256;
166 sz = sizeof("dri/controlD") - 1;
167 if (strncmp(node, "dri/controlD", sz) == 0 && node[sz] != '\0') {
168 devno = strtoul(node + sz, NULL, 10);
169 *major = 226 + (devno / 256);
170 *minor = devno % 256;
173 sz = sizeof("dri/renderD") - 1;
174 if (strncmp(node, "dri/renderD", sz) == 0 && node[sz] != '\0') {
175 devno = strtoul(node + sz, NULL, 10);
176 *major = 226 + (devno / 256);
177 *minor = devno % 256;
180 sz = sizeof("drm/") - 1;
181 if (strncmp(node, "drm/", sz) == 0 && node[sz] != '\0') {
182 devno = strtoul(node + sz, NULL, 10);
183 *major = 226 + (devno / 256);
184 *minor = devno % 256;
188 TAILQ_FOREACH(de, &devices, list) {
189 if (strcmp(node, de->entry.bsd_device_name) == 0) {
190 *major = de->entry.linux_major;
191 *minor = de->entry.linux_minor;
200 linux_vn_get_major_minor(const struct vnode *vp, int *major, int *minor)
204 if (vp->v_type != VCHR)
207 if (vp->v_rdev == NULL) {
211 error = linux_driver_get_major_minor(devtoname(vp->v_rdev),
218 linux_get_char_devices()
220 struct device_element *de;
221 char *temp, *string, *last;
223 int current_size = 0, string_size = 1024;
225 string = malloc(string_size, M_LINUX, M_WAITOK);
228 TAILQ_FOREACH(de, &devices, list) {
229 if (!de->entry.linux_char_device)
232 if (strcmp(last, de->entry.bsd_driver_name) != 0) {
233 last = de->entry.bsd_driver_name;
235 snprintf(formated, sizeof(formated), "%3d %s\n",
236 de->entry.linux_major,
237 de->entry.linux_device_name);
238 if (strlen(formated) + current_size
241 string = malloc(string_size,
243 bcopy(temp, string, current_size);
246 strcat(string, formated);
247 current_size = strlen(string);
255 linux_free_get_char_devices(char *string)
258 free(string, M_LINUX);
261 static int linux_major_starting = 200;
264 linux_device_register_handler(struct linux_device_handler *d)
266 struct device_element *de;
271 de = malloc(sizeof(*de), M_LINUX, M_WAITOK);
272 if (d->linux_major < 0) {
273 d->linux_major = linux_major_starting++;
275 bcopy(d, &de->entry, sizeof(*d));
277 /* Add the element to the list, sorted on span. */
278 TAILQ_INSERT_TAIL(&devices, de, list);
284 linux_device_unregister_handler(struct linux_device_handler *d)
286 struct device_element *de;
291 TAILQ_FOREACH(de, &devices, list) {
292 if (bcmp(d, &de->entry, sizeof(*d)) == 0) {
293 TAILQ_REMOVE(&devices, de, list);