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