2 * Copyright (c) 1994 Christos Zoulas
3 * Copyright (c) 1995 Frank van der Linden
4 * Copyright (c) 1995 Scott Bartram
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * from: svr4_util.c,v 1.5 1995/01/22 23:44:50 christos Exp
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_compat.h"
37 #include <sys/param.h>
39 #include <sys/fcntl.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
43 #include <sys/linker_set.h>
44 #include <sys/mutex.h>
45 #include <sys/namei.h>
48 #include <sys/syscallsubr.h>
49 #include <sys/systm.h>
50 #include <sys/vnode.h>
52 #include <machine/stdarg.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");
58 MALLOC_DEFINE(M_FUTEX, "futex", "Linux futexes");
59 MALLOC_DEFINE(M_FUTEX_WP, "futex wp", "Linux futex waiting proc");
61 const char linux_emul_path[] = "/compat/linux";
64 * Search an alternate path before passing pathname arguments on to
65 * system calls. Useful for keeping a separate 'emulation tree'.
67 * If cflag is set, we check if an attempt can be made to create the
68 * named file, i.e. we check if the directory it should be in exists.
71 linux_emul_convpath(struct thread *td, const char *path, enum uio_seg pathseg,
72 char **pbuf, int cflag, int dfd)
76 retval = kern_alternate_path(td, linux_emul_path, path, pathseg, pbuf,
83 linux_msg(const struct thread *td, const char *fmt, ...)
89 printf("linux: pid %d (%s): ", (int)p->p_pid, p->p_comm);
98 TAILQ_ENTRY(device_element) list;
99 struct linux_device_handler entry;
102 static TAILQ_HEAD(, device_element) devices =
103 TAILQ_HEAD_INITIALIZER(devices);
105 static struct linux_device_handler null_handler =
106 { "mem", "mem", "null", "null", 1, 3, 1};
108 DATA_SET(linux_device_handler_set, null_handler);
111 linux_driver_get_name_dev(device_t dev)
113 struct device_element *de;
114 const char *device_name = device_get_name(dev);
116 if (device_name == NULL)
118 TAILQ_FOREACH(de, &devices, list) {
119 if (strcmp(device_name, de->entry.bsd_driver_name) == 0)
120 return (de->entry.linux_driver_name);
127 linux_driver_get_major_minor(const char *node, int *major, int *minor)
129 struct device_element *de;
131 if (node == NULL || major == NULL || minor == NULL)
134 if (strlen(node) > strlen("pts/") &&
135 strncmp(node, "pts/", strlen("pts/")) == 0) {
139 * Linux checks major and minors of the slave device
140 * to make sure it's a pty device, so let's make him
143 devno = strtoul(node + strlen("pts/"), NULL, 10);
144 *major = 136 + (devno / 256);
145 *minor = devno % 256;
150 TAILQ_FOREACH(de, &devices, list) {
151 if (strcmp(node, de->entry.bsd_device_name) == 0) {
152 *major = de->entry.linux_major;
153 *minor = de->entry.linux_minor;
162 linux_get_char_devices()
164 struct device_element *de;
165 char *temp, *string, *last;
167 int current_size = 0, string_size = 1024;
169 string = malloc(string_size, M_LINUX, M_WAITOK);
172 TAILQ_FOREACH(de, &devices, list) {
173 if (!de->entry.linux_char_device)
176 if (strcmp(last, de->entry.bsd_driver_name) != 0) {
177 last = de->entry.bsd_driver_name;
179 snprintf(formated, sizeof(formated), "%3d %s\n",
180 de->entry.linux_major,
181 de->entry.linux_device_name);
182 if (strlen(formated) + current_size
185 string = malloc(string_size,
187 bcopy(temp, string, current_size);
190 strcat(string, formated);
191 current_size = strlen(string);
199 linux_free_get_char_devices(char *string)
202 free(string, M_LINUX);
205 static int linux_major_starting = 200;
208 linux_device_register_handler(struct linux_device_handler *d)
210 struct device_element *de;
215 de = malloc(sizeof(*de), M_LINUX, M_WAITOK);
216 if (d->linux_major < 0) {
217 d->linux_major = linux_major_starting++;
219 bcopy(d, &de->entry, sizeof(*d));
221 /* Add the element to the list, sorted on span. */
222 TAILQ_INSERT_TAIL(&devices, de, list);
228 linux_device_unregister_handler(struct linux_device_handler *d)
230 struct device_element *de;
235 TAILQ_FOREACH(de, &devices, list) {
236 if (bcmp(d, &de->entry, sizeof(*d)) == 0) {
237 TAILQ_REMOVE(&devices, de, list);