xref: /freebsd/sys/compat/linux/linux_util.c (revision 1a34e9fa)
1898b0535SWarner Losh /*-
27f2d13d6SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
37f2d13d6SPedro F. Giffuni  *
4d66a5066SPeter Wemm  * Copyright (c) 1994 Christos Zoulas
5d66a5066SPeter Wemm  * Copyright (c) 1995 Frank van der Linden
6d66a5066SPeter Wemm  * Copyright (c) 1995 Scott Bartram
7d66a5066SPeter Wemm  * All rights reserved.
8d66a5066SPeter Wemm  *
9d66a5066SPeter Wemm  * Redistribution and use in source and binary forms, with or without
10d66a5066SPeter Wemm  * modification, are permitted provided that the following conditions
11d66a5066SPeter Wemm  * are met:
12d66a5066SPeter Wemm  * 1. Redistributions of source code must retain the above copyright
13d66a5066SPeter Wemm  *    notice, this list of conditions and the following disclaimer.
14d66a5066SPeter Wemm  * 2. Redistributions in binary form must reproduce the above copyright
15d66a5066SPeter Wemm  *    notice, this list of conditions and the following disclaimer in the
16d66a5066SPeter Wemm  *    documentation and/or other materials provided with the distribution.
17d66a5066SPeter Wemm  * 3. The name of the author may not be used to endorse or promote products
18d66a5066SPeter Wemm  *    derived from this software without specific prior written permission
19d66a5066SPeter Wemm  *
20d66a5066SPeter Wemm  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21d66a5066SPeter Wemm  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22d66a5066SPeter Wemm  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23d66a5066SPeter Wemm  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24d66a5066SPeter Wemm  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25d66a5066SPeter Wemm  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26d66a5066SPeter Wemm  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27d66a5066SPeter Wemm  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28d66a5066SPeter Wemm  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29d66a5066SPeter Wemm  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30d66a5066SPeter Wemm  *
31d66a5066SPeter Wemm  *	from: svr4_util.c,v 1.5 1995/01/22 23:44:50 christos Exp
32d66a5066SPeter Wemm  */
33d66a5066SPeter Wemm 
3416dbc7f2SDavid E. O'Brien #include <sys/cdefs.h>
3516dbc7f2SDavid E. O'Brien __FBSDID("$FreeBSD$");
3616dbc7f2SDavid E. O'Brien 
37d66a5066SPeter Wemm #include <sys/param.h>
38060e4882SDoug Ambrisko #include <sys/bus.h>
391a34e9faSEdward Tomasz Napierala #include <sys/conf.h>
4057b4252eSKonstantin Belousov #include <sys/fcntl.h>
41d4364109SBruce Evans #include <sys/lock.h>
42d4364109SBruce Evans #include <sys/malloc.h>
4319e252baSAlexander Leidinger #include <sys/kernel.h>
44060e4882SDoug Ambrisko #include <sys/linker_set.h>
45d4364109SBruce Evans #include <sys/mutex.h>
46d66a5066SPeter Wemm #include <sys/namei.h>
47d66a5066SPeter Wemm #include <sys/proc.h>
4819e252baSAlexander Leidinger #include <sys/sdt.h>
4925771ec2SJohn Baldwin #include <sys/syscallsubr.h>
50b5f20658SEdward Tomasz Napierala #include <sys/sysctl.h>
51d4364109SBruce Evans #include <sys/systm.h>
52d66a5066SPeter Wemm #include <sys/vnode.h>
53d66a5066SPeter Wemm 
54e15583ceSAlfred Perlstein #include <machine/stdarg.h>
55e15583ceSAlfred Perlstein 
56b5f20658SEdward Tomasz Napierala #include <compat/linux/linux_mib.h>
57ac951e62SMarcel Moolenaar #include <compat/linux/linux_util.h>
58762e6b85SEivind Eklund 
5967d39748SDmitry Chagin MALLOC_DEFINE(M_LINUX, "linux", "Linux mode structures");
60e16fe1c7SDmitry Chagin MALLOC_DEFINE(M_EPOLL, "lepoll", "Linux events structures");
61e0d3ea8cSDmitry Chagin MALLOC_DEFINE(M_FUTEX, "futex", "Linux futexes");
62e0d3ea8cSDmitry Chagin MALLOC_DEFINE(M_FUTEX_WP, "futex wp", "Linux futex waiting proc");
6319e252baSAlexander Leidinger 
64b5f20658SEdward Tomasz Napierala char linux_emul_path[MAXPATHLEN] = "/compat/linux";
65b5f20658SEdward Tomasz Napierala 
66b5f20658SEdward Tomasz Napierala SYSCTL_STRING(_compat_linux, OID_AUTO, emul_path, CTLFLAG_RWTUN,
67b5f20658SEdward Tomasz Napierala     linux_emul_path, sizeof(linux_emul_path),
68b5f20658SEdward Tomasz Napierala     "Linux runtime environment path");
69d66a5066SPeter Wemm 
70d66a5066SPeter Wemm /*
71802e08a3SAlexander Leidinger  * Search an alternate path before passing pathname arguments on to
72802e08a3SAlexander Leidinger  * system calls. Useful for keeping a separate 'emulation tree'.
73d66a5066SPeter Wemm  *
74802e08a3SAlexander Leidinger  * If cflag is set, we check if an attempt can be made to create the
75802e08a3SAlexander Leidinger  * named file, i.e. we check if the directory it should be in exists.
76d66a5066SPeter Wemm  */
77d66a5066SPeter Wemm int
7819e252baSAlexander Leidinger linux_emul_convpath(struct thread *td, const char *path, enum uio_seg pathseg,
7919e252baSAlexander Leidinger     char **pbuf, int cflag, int dfd)
80ff766321SIan Dowse {
8119e252baSAlexander Leidinger 	int retval;
82d66a5066SPeter Wemm 
8319e252baSAlexander Leidinger 	retval = kern_alternate_path(td, linux_emul_path, path, pathseg, pbuf,
8419e252baSAlexander Leidinger 	    cflag, dfd);
8519e252baSAlexander Leidinger 
8619e252baSAlexander Leidinger 	return (retval);
87d66a5066SPeter Wemm }
88e15583ceSAlfred Perlstein 
89e15583ceSAlfred Perlstein void
90e15583ceSAlfred Perlstein linux_msg(const struct thread *td, const char *fmt, ...)
91e15583ceSAlfred Perlstein {
92e15583ceSAlfred Perlstein 	va_list ap;
93e15583ceSAlfred Perlstein 	struct proc *p;
94e15583ceSAlfred Perlstein 
95462171d9SEdward Tomasz Napierala 	if (linux_debug == 0)
96462171d9SEdward Tomasz Napierala 		return;
97462171d9SEdward Tomasz Napierala 
98e15583ceSAlfred Perlstein 	p = td->td_proc;
99e15583ceSAlfred Perlstein 	printf("linux: pid %d (%s): ", (int)p->p_pid, p->p_comm);
100e15583ceSAlfred Perlstein 	va_start(ap, fmt);
101e15583ceSAlfred Perlstein 	vprintf(fmt, ap);
102e15583ceSAlfred Perlstein 	va_end(ap);
103e15583ceSAlfred Perlstein 	printf("\n");
104e15583ceSAlfred Perlstein }
105060e4882SDoug Ambrisko 
106060e4882SDoug Ambrisko struct device_element
107060e4882SDoug Ambrisko {
108060e4882SDoug Ambrisko 	TAILQ_ENTRY(device_element) list;
109060e4882SDoug Ambrisko 	struct linux_device_handler entry;
110060e4882SDoug Ambrisko };
111060e4882SDoug Ambrisko 
112060e4882SDoug Ambrisko static TAILQ_HEAD(, device_element) devices =
113060e4882SDoug Ambrisko 	TAILQ_HEAD_INITIALIZER(devices);
114060e4882SDoug Ambrisko 
115060e4882SDoug Ambrisko static struct linux_device_handler null_handler =
116060e4882SDoug Ambrisko 	{ "mem", "mem", "null", "null", 1, 3, 1};
117060e4882SDoug Ambrisko 
118060e4882SDoug Ambrisko DATA_SET(linux_device_handler_set, null_handler);
119060e4882SDoug Ambrisko 
120060e4882SDoug Ambrisko char *
121060e4882SDoug Ambrisko linux_driver_get_name_dev(device_t dev)
122060e4882SDoug Ambrisko {
123060e4882SDoug Ambrisko 	struct device_element *de;
124060e4882SDoug Ambrisko 	const char *device_name = device_get_name(dev);
125060e4882SDoug Ambrisko 
12667d39748SDmitry Chagin 	if (device_name == NULL)
127340f4a8dSEd Maste 		return (NULL);
128060e4882SDoug Ambrisko 	TAILQ_FOREACH(de, &devices, list) {
12967d39748SDmitry Chagin 		if (strcmp(device_name, de->entry.bsd_driver_name) == 0)
130060e4882SDoug Ambrisko 			return (de->entry.linux_driver_name);
131060e4882SDoug Ambrisko 	}
132060e4882SDoug Ambrisko 
13367d39748SDmitry Chagin 	return (NULL);
134060e4882SDoug Ambrisko }
135060e4882SDoug Ambrisko 
136060e4882SDoug Ambrisko int
1377870adb6SEd Schouten linux_driver_get_major_minor(const char *node, int *major, int *minor)
138060e4882SDoug Ambrisko {
139060e4882SDoug Ambrisko 	struct device_element *de;
1402d347b2eSConrad Meyer 	unsigned long devno;
141f3792e07STijl Coosemans 	size_t sz;
142060e4882SDoug Ambrisko 
14367d39748SDmitry Chagin 	if (node == NULL || major == NULL || minor == NULL)
144340f4a8dSEd Maste 		return (1);
145a147e6caSEd Schouten 
146f3792e07STijl Coosemans 	sz = sizeof("pts/") - 1;
147f3792e07STijl Coosemans 	if (strncmp(node, "pts/", sz) == 0 && node[sz] != '\0') {
148a147e6caSEd Schouten 		/*
149a147e6caSEd Schouten 		 * Linux checks major and minors of the slave device
150a147e6caSEd Schouten 		 * to make sure it's a pty device, so let's make him
151a147e6caSEd Schouten 		 * believe it is.
152a147e6caSEd Schouten 		 */
153f3792e07STijl Coosemans 		devno = strtoul(node + sz, NULL, 10);
154a147e6caSEd Schouten 		*major = 136 + (devno / 256);
155a147e6caSEd Schouten 		*minor = devno % 256;
15667d39748SDmitry Chagin 		return (0);
157a147e6caSEd Schouten 	}
158a147e6caSEd Schouten 
159834804f3STijl Coosemans 	sz = sizeof("dri/card") - 1;
160834804f3STijl Coosemans 	if (strncmp(node, "dri/card", sz) == 0 && node[sz] != '\0') {
161834804f3STijl Coosemans 		devno = strtoul(node + sz, NULL, 10);
162834804f3STijl Coosemans 		*major = 226 + (devno / 256);
163834804f3STijl Coosemans 		*minor = devno % 256;
164834804f3STijl Coosemans 		return (0);
165834804f3STijl Coosemans 	}
166834804f3STijl Coosemans 	sz = sizeof("dri/controlD") - 1;
167834804f3STijl Coosemans 	if (strncmp(node, "dri/controlD", sz) == 0 && node[sz] != '\0') {
168834804f3STijl Coosemans 		devno = strtoul(node + sz, NULL, 10);
169834804f3STijl Coosemans 		*major = 226 + (devno / 256);
170834804f3STijl Coosemans 		*minor = devno % 256;
171834804f3STijl Coosemans 		return (0);
172834804f3STijl Coosemans 	}
173834804f3STijl Coosemans 	sz = sizeof("dri/renderD") - 1;
174834804f3STijl Coosemans 	if (strncmp(node, "dri/renderD", sz) == 0 && node[sz] != '\0') {
175834804f3STijl Coosemans 		devno = strtoul(node + sz, NULL, 10);
176834804f3STijl Coosemans 		*major = 226 + (devno / 256);
177834804f3STijl Coosemans 		*minor = devno % 256;
178834804f3STijl Coosemans 		return (0);
179834804f3STijl Coosemans 	}
180f3792e07STijl Coosemans 	sz = sizeof("drm/") - 1;
181f3792e07STijl Coosemans 	if (strncmp(node, "drm/", sz) == 0 && node[sz] != '\0') {
182f3792e07STijl Coosemans 		devno = strtoul(node + sz, NULL, 10);
1832d347b2eSConrad Meyer 		*major = 226 + (devno / 256);
1842d347b2eSConrad Meyer 		*minor = devno % 256;
1852d347b2eSConrad Meyer 		return (0);
1862d347b2eSConrad Meyer 	}
1872d347b2eSConrad Meyer 
188060e4882SDoug Ambrisko 	TAILQ_FOREACH(de, &devices, list) {
189060e4882SDoug Ambrisko 		if (strcmp(node, de->entry.bsd_device_name) == 0) {
190060e4882SDoug Ambrisko 			*major = de->entry.linux_major;
191060e4882SDoug Ambrisko 			*minor = de->entry.linux_minor;
19267d39748SDmitry Chagin 			return (0);
193060e4882SDoug Ambrisko 		}
194060e4882SDoug Ambrisko 	}
195060e4882SDoug Ambrisko 
19667d39748SDmitry Chagin 	return (1);
197060e4882SDoug Ambrisko }
198060e4882SDoug Ambrisko 
1991a34e9faSEdward Tomasz Napierala int
2001a34e9faSEdward Tomasz Napierala linux_vn_get_major_minor(const struct vnode *vp, int *major, int *minor)
2011a34e9faSEdward Tomasz Napierala {
2021a34e9faSEdward Tomasz Napierala 	int error;
2031a34e9faSEdward Tomasz Napierala 
2041a34e9faSEdward Tomasz Napierala 	if (vp->v_type != VCHR)
2051a34e9faSEdward Tomasz Napierala 		return (ENOTBLK);
2061a34e9faSEdward Tomasz Napierala 	dev_lock();
2071a34e9faSEdward Tomasz Napierala 	if (vp->v_rdev == NULL) {
2081a34e9faSEdward Tomasz Napierala 		dev_unlock();
2091a34e9faSEdward Tomasz Napierala 		return (ENXIO);
2101a34e9faSEdward Tomasz Napierala 	}
2111a34e9faSEdward Tomasz Napierala 	error = linux_driver_get_major_minor(devtoname(vp->v_rdev),
2121a34e9faSEdward Tomasz Napierala 	    major, minor);
2131a34e9faSEdward Tomasz Napierala 	dev_unlock();
2141a34e9faSEdward Tomasz Napierala 	return (error);
2151a34e9faSEdward Tomasz Napierala }
2161a34e9faSEdward Tomasz Napierala 
217060e4882SDoug Ambrisko char *
218060e4882SDoug Ambrisko linux_get_char_devices()
219060e4882SDoug Ambrisko {
220060e4882SDoug Ambrisko 	struct device_element *de;
221060e4882SDoug Ambrisko 	char *temp, *string, *last;
222060e4882SDoug Ambrisko 	char formated[256];
223060e4882SDoug Ambrisko 	int current_size = 0, string_size = 1024;
224060e4882SDoug Ambrisko 
2251ede983cSDag-Erling Smørgrav 	string = malloc(string_size, M_LINUX, M_WAITOK);
226060e4882SDoug Ambrisko 	string[0] = '\000';
227060e4882SDoug Ambrisko 	last = "";
228060e4882SDoug Ambrisko 	TAILQ_FOREACH(de, &devices, list) {
229060e4882SDoug Ambrisko 		if (!de->entry.linux_char_device)
230060e4882SDoug Ambrisko 			continue;
231060e4882SDoug Ambrisko 		temp = string;
232060e4882SDoug Ambrisko 		if (strcmp(last, de->entry.bsd_driver_name) != 0) {
233060e4882SDoug Ambrisko 			last = de->entry.bsd_driver_name;
234060e4882SDoug Ambrisko 
235060e4882SDoug Ambrisko 			snprintf(formated, sizeof(formated), "%3d %s\n",
236060e4882SDoug Ambrisko 				 de->entry.linux_major,
237060e4882SDoug Ambrisko 				 de->entry.linux_device_name);
238060e4882SDoug Ambrisko 			if (strlen(formated) + current_size
239060e4882SDoug Ambrisko 			    >= string_size) {
240060e4882SDoug Ambrisko 				string_size *= 2;
2411ede983cSDag-Erling Smørgrav 				string = malloc(string_size,
242060e4882SDoug Ambrisko 				    M_LINUX, M_WAITOK);
243060e4882SDoug Ambrisko 				bcopy(temp, string, current_size);
2441ede983cSDag-Erling Smørgrav 				free(temp, M_LINUX);
245060e4882SDoug Ambrisko 			}
246060e4882SDoug Ambrisko 			strcat(string, formated);
247060e4882SDoug Ambrisko 			current_size = strlen(string);
248060e4882SDoug Ambrisko 		}
249060e4882SDoug Ambrisko 	}
250060e4882SDoug Ambrisko 
25167d39748SDmitry Chagin 	return (string);
252060e4882SDoug Ambrisko }
253060e4882SDoug Ambrisko 
254060e4882SDoug Ambrisko void
255060e4882SDoug Ambrisko linux_free_get_char_devices(char *string)
256060e4882SDoug Ambrisko {
25719e252baSAlexander Leidinger 
2581ede983cSDag-Erling Smørgrav 	free(string, M_LINUX);
259060e4882SDoug Ambrisko }
260060e4882SDoug Ambrisko 
261060e4882SDoug Ambrisko static int linux_major_starting = 200;
262060e4882SDoug Ambrisko 
263060e4882SDoug Ambrisko int
264060e4882SDoug Ambrisko linux_device_register_handler(struct linux_device_handler *d)
265060e4882SDoug Ambrisko {
266060e4882SDoug Ambrisko 	struct device_element *de;
267060e4882SDoug Ambrisko 
26867d39748SDmitry Chagin 	if (d == NULL)
26919e252baSAlexander Leidinger 		return (EINVAL);
27019e252baSAlexander Leidinger 
27119e252baSAlexander Leidinger 	de = malloc(sizeof(*de), M_LINUX, M_WAITOK);
272060e4882SDoug Ambrisko 	if (d->linux_major < 0) {
273060e4882SDoug Ambrisko 		d->linux_major = linux_major_starting++;
274060e4882SDoug Ambrisko 	}
275060e4882SDoug Ambrisko 	bcopy(d, &de->entry, sizeof(*d));
276060e4882SDoug Ambrisko 
277060e4882SDoug Ambrisko 	/* Add the element to the list, sorted on span. */
278060e4882SDoug Ambrisko 	TAILQ_INSERT_TAIL(&devices, de, list);
279060e4882SDoug Ambrisko 
280060e4882SDoug Ambrisko 	return (0);
281060e4882SDoug Ambrisko }
282060e4882SDoug Ambrisko 
283060e4882SDoug Ambrisko int
284060e4882SDoug Ambrisko linux_device_unregister_handler(struct linux_device_handler *d)
285060e4882SDoug Ambrisko {
286060e4882SDoug Ambrisko 	struct device_element *de;
287060e4882SDoug Ambrisko 
28867d39748SDmitry Chagin 	if (d == NULL)
289060e4882SDoug Ambrisko 		return (EINVAL);
290060e4882SDoug Ambrisko 
291060e4882SDoug Ambrisko 	TAILQ_FOREACH(de, &devices, list) {
292060e4882SDoug Ambrisko 		if (bcmp(d, &de->entry, sizeof(*d)) == 0) {
293060e4882SDoug Ambrisko 			TAILQ_REMOVE(&devices, de, list);
2941ede983cSDag-Erling Smørgrav 			free(de, M_LINUX);
29519e252baSAlexander Leidinger 
296060e4882SDoug Ambrisko 			return (0);
297060e4882SDoug Ambrisko 		}
298060e4882SDoug Ambrisko 	}
299060e4882SDoug Ambrisko 
300060e4882SDoug Ambrisko 	return (EINVAL);
301060e4882SDoug Ambrisko }
302