xref: /freebsd/sys/compat/linux/linux_util.c (revision c697fb7f)
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/fcntl.h>
40 #include <sys/lock.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>
46 #include <sys/proc.h>
47 #include <sys/sdt.h>
48 #include <sys/syscallsubr.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
51 #include <sys/vnode.h>
52 
53 #include <machine/stdarg.h>
54 
55 #include <compat/linux/linux_mib.h>
56 #include <compat/linux/linux_util.h>
57 
58 MALLOC_DEFINE(M_LINUX, "linux", "Linux mode structures");
59 MALLOC_DEFINE(M_EPOLL, "lepoll", "Linux events structures");
60 MALLOC_DEFINE(M_FUTEX, "futex", "Linux futexes");
61 MALLOC_DEFINE(M_FUTEX_WP, "futex wp", "Linux futex waiting proc");
62 
63 char linux_emul_path[MAXPATHLEN] = "/compat/linux";
64 
65 SYSCTL_STRING(_compat_linux, OID_AUTO, emul_path, CTLFLAG_RWTUN,
66     linux_emul_path, sizeof(linux_emul_path),
67     "Linux runtime environment path");
68 
69 /*
70  * Search an alternate path before passing pathname arguments on to
71  * system calls. Useful for keeping a separate 'emulation tree'.
72  *
73  * If cflag is set, we check if an attempt can be made to create the
74  * named file, i.e. we check if the directory it should be in exists.
75  */
76 int
77 linux_emul_convpath(struct thread *td, const char *path, enum uio_seg pathseg,
78     char **pbuf, int cflag, int dfd)
79 {
80 	int retval;
81 
82 	retval = kern_alternate_path(td, linux_emul_path, path, pathseg, pbuf,
83 	    cflag, dfd);
84 
85 	return (retval);
86 }
87 
88 void
89 linux_msg(const struct thread *td, const char *fmt, ...)
90 {
91 	va_list ap;
92 	struct proc *p;
93 
94 	p = td->td_proc;
95 	printf("linux: pid %d (%s): ", (int)p->p_pid, p->p_comm);
96 	va_start(ap, fmt);
97 	vprintf(fmt, ap);
98 	va_end(ap);
99 	printf("\n");
100 }
101 
102 struct device_element
103 {
104 	TAILQ_ENTRY(device_element) list;
105 	struct linux_device_handler entry;
106 };
107 
108 static TAILQ_HEAD(, device_element) devices =
109 	TAILQ_HEAD_INITIALIZER(devices);
110 
111 static struct linux_device_handler null_handler =
112 	{ "mem", "mem", "null", "null", 1, 3, 1};
113 
114 DATA_SET(linux_device_handler_set, null_handler);
115 
116 char *
117 linux_driver_get_name_dev(device_t dev)
118 {
119 	struct device_element *de;
120 	const char *device_name = device_get_name(dev);
121 
122 	if (device_name == NULL)
123 		return (NULL);
124 	TAILQ_FOREACH(de, &devices, list) {
125 		if (strcmp(device_name, de->entry.bsd_driver_name) == 0)
126 			return (de->entry.linux_driver_name);
127 	}
128 
129 	return (NULL);
130 }
131 
132 int
133 linux_driver_get_major_minor(const char *node, int *major, int *minor)
134 {
135 	struct device_element *de;
136 	unsigned long devno;
137 	size_t sz;
138 
139 	if (node == NULL || major == NULL || minor == NULL)
140 		return (1);
141 
142 	sz = sizeof("pts/") - 1;
143 	if (strncmp(node, "pts/", sz) == 0 && node[sz] != '\0') {
144 		/*
145 		 * Linux checks major and minors of the slave device
146 		 * to make sure it's a pty device, so let's make him
147 		 * believe it is.
148 		 */
149 		devno = strtoul(node + sz, NULL, 10);
150 		*major = 136 + (devno / 256);
151 		*minor = devno % 256;
152 		return (0);
153 	}
154 
155 	sz = sizeof("dri/card") - 1;
156 	if (strncmp(node, "dri/card", sz) == 0 && node[sz] != '\0') {
157 		devno = strtoul(node + sz, NULL, 10);
158 		*major = 226 + (devno / 256);
159 		*minor = devno % 256;
160 		return (0);
161 	}
162 	sz = sizeof("dri/controlD") - 1;
163 	if (strncmp(node, "dri/controlD", sz) == 0 && node[sz] != '\0') {
164 		devno = strtoul(node + sz, NULL, 10);
165 		*major = 226 + (devno / 256);
166 		*minor = devno % 256;
167 		return (0);
168 	}
169 	sz = sizeof("dri/renderD") - 1;
170 	if (strncmp(node, "dri/renderD", sz) == 0 && node[sz] != '\0') {
171 		devno = strtoul(node + sz, NULL, 10);
172 		*major = 226 + (devno / 256);
173 		*minor = devno % 256;
174 		return (0);
175 	}
176 	sz = sizeof("drm/") - 1;
177 	if (strncmp(node, "drm/", sz) == 0 && node[sz] != '\0') {
178 		devno = strtoul(node + sz, NULL, 10);
179 		*major = 226 + (devno / 256);
180 		*minor = devno % 256;
181 		return (0);
182 	}
183 
184 	TAILQ_FOREACH(de, &devices, list) {
185 		if (strcmp(node, de->entry.bsd_device_name) == 0) {
186 			*major = de->entry.linux_major;
187 			*minor = de->entry.linux_minor;
188 			return (0);
189 		}
190 	}
191 
192 	return (1);
193 }
194 
195 char *
196 linux_get_char_devices()
197 {
198 	struct device_element *de;
199 	char *temp, *string, *last;
200 	char formated[256];
201 	int current_size = 0, string_size = 1024;
202 
203 	string = malloc(string_size, M_LINUX, M_WAITOK);
204 	string[0] = '\000';
205 	last = "";
206 	TAILQ_FOREACH(de, &devices, list) {
207 		if (!de->entry.linux_char_device)
208 			continue;
209 		temp = string;
210 		if (strcmp(last, de->entry.bsd_driver_name) != 0) {
211 			last = de->entry.bsd_driver_name;
212 
213 			snprintf(formated, sizeof(formated), "%3d %s\n",
214 				 de->entry.linux_major,
215 				 de->entry.linux_device_name);
216 			if (strlen(formated) + current_size
217 			    >= string_size) {
218 				string_size *= 2;
219 				string = malloc(string_size,
220 				    M_LINUX, M_WAITOK);
221 				bcopy(temp, string, current_size);
222 				free(temp, M_LINUX);
223 			}
224 			strcat(string, formated);
225 			current_size = strlen(string);
226 		}
227 	}
228 
229 	return (string);
230 }
231 
232 void
233 linux_free_get_char_devices(char *string)
234 {
235 
236 	free(string, M_LINUX);
237 }
238 
239 static int linux_major_starting = 200;
240 
241 int
242 linux_device_register_handler(struct linux_device_handler *d)
243 {
244 	struct device_element *de;
245 
246 	if (d == NULL)
247 		return (EINVAL);
248 
249 	de = malloc(sizeof(*de), M_LINUX, M_WAITOK);
250 	if (d->linux_major < 0) {
251 		d->linux_major = linux_major_starting++;
252 	}
253 	bcopy(d, &de->entry, sizeof(*d));
254 
255 	/* Add the element to the list, sorted on span. */
256 	TAILQ_INSERT_TAIL(&devices, de, list);
257 
258 	return (0);
259 }
260 
261 int
262 linux_device_unregister_handler(struct linux_device_handler *d)
263 {
264 	struct device_element *de;
265 
266 	if (d == NULL)
267 		return (EINVAL);
268 
269 	TAILQ_FOREACH(de, &devices, list) {
270 		if (bcmp(d, &de->entry, sizeof(*d)) == 0) {
271 			TAILQ_REMOVE(&devices, de, list);
272 			free(de, M_LINUX);
273 
274 			return (0);
275 		}
276 	}
277 
278 	return (EINVAL);
279 }
280