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