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