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