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