xref: /freebsd/sys/compat/linux/linux_util.c (revision 9f27341c336aa12f6c7163c17e646e76c813b689)
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/types.h>
39 #include <sys/bus.h>
40 #include <sys/conf.h>
41 #include <sys/fcntl.h>
42 #include <sys/jail.h>
43 #include <sys/malloc.h>
44 #include <sys/namei.h>
45 #include <sys/proc.h>
46 #include <sys/stat.h>
47 #include <sys/syscallsubr.h>
48 #include <sys/vnode.h>
49 
50 #include <machine/stdarg.h>
51 
52 #include <compat/linux/linux_dtrace.h>
53 #include <compat/linux/linux_mib.h>
54 #include <compat/linux/linux_util.h>
55 
56 MALLOC_DEFINE(M_LINUX, "linux", "Linux mode structures");
57 MALLOC_DEFINE(M_EPOLL, "lepoll", "Linux events structures");
58 
59 FEATURE(linuxulator_v4l, "V4L ioctl wrapper support in the linuxulator");
60 FEATURE(linuxulator_v4l2, "V4L2 ioctl wrapper support in the linuxulator");
61 
62 /**
63  * Special DTrace provider for the linuxulator.
64  *
65  * In this file we define the provider for the entire linuxulator. All
66  * modules (= files of the linuxulator) use it.
67  *
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
72  */
73 LIN_SDT_PROVIDER_DEFINE(linuxulator);
74 LIN_SDT_PROVIDER_DEFINE(linuxulator32);
75 
76 char linux_emul_path[MAXPATHLEN] = "/compat/linux";
77 
78 SYSCTL_STRING(_compat_linux, OID_AUTO, emul_path, CTLFLAG_RWTUN,
79     linux_emul_path, sizeof(linux_emul_path),
80     "Linux runtime environment path");
81 
82 /*
83  * Search an alternate path before passing pathname arguments on to
84  * system calls. Useful for keeping a separate 'emulation tree'.
85  *
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.
88  */
89 int
90 linux_emul_convpath(const char *path, enum uio_seg pathseg,
91     char **pbuf, int cflag, int dfd)
92 {
93 	int retval;
94 
95 	retval = kern_alternate_path(linux_emul_path, path, pathseg, pbuf,
96 	    cflag, dfd);
97 
98 	return (retval);
99 }
100 
101 void
102 linux_msg(const struct thread *td, const char *fmt, ...)
103 {
104 	va_list ap;
105 	struct proc *p;
106 
107 	if (linux_debug == 0)
108 		return;
109 
110 	p = td->td_proc;
111 	printf("linux: jid %d pid %d (%s): ", p->p_ucred->cr_prison->pr_id,
112 	    (int)p->p_pid, p->p_comm);
113 	va_start(ap, fmt);
114 	vprintf(fmt, ap);
115 	va_end(ap);
116 	printf("\n");
117 }
118 
119 struct device_element
120 {
121 	TAILQ_ENTRY(device_element) list;
122 	struct linux_device_handler entry;
123 };
124 
125 static TAILQ_HEAD(, device_element) devices =
126 	TAILQ_HEAD_INITIALIZER(devices);
127 
128 static struct linux_device_handler null_handler =
129 	{ "mem", "mem", "null", "null", 1, 3, 1};
130 
131 DATA_SET(linux_device_handler_set, null_handler);
132 
133 char *
134 linux_driver_get_name_dev(device_t dev)
135 {
136 	struct device_element *de;
137 	const char *device_name = device_get_name(dev);
138 
139 	if (device_name == NULL)
140 		return (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);
144 	}
145 
146 	return (NULL);
147 }
148 
149 int
150 linux_driver_get_major_minor(const char *node, int *major, int *minor)
151 {
152 	struct device_element *de;
153 	unsigned long devno;
154 	size_t sz;
155 
156 	if (node == NULL || major == NULL || minor == NULL)
157 		return (1);
158 
159 	sz = sizeof("pts/") - 1;
160 	if (strncmp(node, "pts/", sz) == 0 && node[sz] != '\0') {
161 		/*
162 		 * Linux checks major and minors of the slave device
163 		 * to make sure it's a pty device, so let's make him
164 		 * believe it is.
165 		 */
166 		devno = strtoul(node + sz, NULL, 10);
167 		*major = 136 + (devno / 256);
168 		*minor = devno % 256;
169 		return (0);
170 	}
171 
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;
177 		return (0);
178 	}
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;
184 		return (0);
185 	}
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;
191 		return (0);
192 	}
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;
198 		return (0);
199 	}
200 
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;
205 			return (0);
206 		}
207 	}
208 
209 	return (1);
210 }
211 
212 int
213 linux_vn_get_major_minor(const struct vnode *vp, int *major, int *minor)
214 {
215 	int error;
216 
217 	if (vp->v_type != VCHR)
218 		return (ENOTBLK);
219 	dev_lock();
220 	if (vp->v_rdev == NULL) {
221 		dev_unlock();
222 		return (ENXIO);
223 	}
224 	error = linux_driver_get_major_minor(devtoname(vp->v_rdev),
225 	    major, minor);
226 	dev_unlock();
227 	return (error);
228 }
229 
230 void
231 translate_vnhook_major_minor(struct vnode *vp, struct stat *sb)
232 {
233 	int major, minor;
234 
235 	if (vn_isdisk(vp)) {
236 		sb->st_mode &= ~S_IFMT;
237 		sb->st_mode |= S_IFBLK;
238 	}
239 
240 	/*
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.
247 	 */
248 	if (rootdevmp != NULL && vp->v_mount->mnt_vfc == rootdevmp->mnt_vfc)
249 		sb->st_dev = rootdevmp->mnt_stat.f_fsid.val[0];
250 
251 	if (linux_vn_get_major_minor(vp, &major, &minor) == 0)
252 		sb->st_rdev = makedev(major, minor);
253 }
254 
255 char *
256 linux_get_char_devices(void)
257 {
258 	struct device_element *de;
259 	char *temp, *string, *last;
260 	char formated[256];
261 	int current_size = 0, string_size = 1024;
262 
263 	string = malloc(string_size, M_LINUX, M_WAITOK);
264 	string[0] = '\000';
265 	last = "";
266 	TAILQ_FOREACH(de, &devices, list) {
267 		if (!de->entry.linux_char_device)
268 			continue;
269 		temp = string;
270 		if (strcmp(last, de->entry.bsd_driver_name) != 0) {
271 			last = de->entry.bsd_driver_name;
272 
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
277 			    >= string_size) {
278 				string_size *= 2;
279 				string = malloc(string_size,
280 				    M_LINUX, M_WAITOK);
281 				bcopy(temp, string, current_size);
282 				free(temp, M_LINUX);
283 			}
284 			strcat(string, formated);
285 			current_size = strlen(string);
286 		}
287 	}
288 
289 	return (string);
290 }
291 
292 void
293 linux_free_get_char_devices(char *string)
294 {
295 
296 	free(string, M_LINUX);
297 }
298 
299 static int linux_major_starting = 200;
300 
301 int
302 linux_device_register_handler(struct linux_device_handler *d)
303 {
304 	struct device_element *de;
305 
306 	if (d == NULL)
307 		return (EINVAL);
308 
309 	de = malloc(sizeof(*de), M_LINUX, M_WAITOK);
310 	if (d->linux_major < 0) {
311 		d->linux_major = linux_major_starting++;
312 	}
313 	bcopy(d, &de->entry, sizeof(*d));
314 
315 	/* Add the element to the list, sorted on span. */
316 	TAILQ_INSERT_TAIL(&devices, de, list);
317 
318 	return (0);
319 }
320 
321 int
322 linux_device_unregister_handler(struct linux_device_handler *d)
323 {
324 	struct device_element *de;
325 
326 	if (d == NULL)
327 		return (EINVAL);
328 
329 	TAILQ_FOREACH(de, &devices, list) {
330 		if (bcmp(d, &de->entry, sizeof(*d)) == 0) {
331 			TAILQ_REMOVE(&devices, de, list);
332 			free(de, M_LINUX);
333 
334 			return (0);
335 		}
336 	}
337 
338 	return (EINVAL);
339 }
340