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