]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/compat/linux/linux_util.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / compat / linux / linux_util.c
1 /*-
2  * Copyright (c) 1994 Christos Zoulas
3  * Copyright (c) 1995 Frank van der Linden
4  * Copyright (c) 1995 Scott Bartram
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  *      from: svr4_util.c,v 1.5 1995/01/22 23:44:50 christos Exp
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_compat.h"
36 #include "opt_kdtrace.h"
37
38 #include <sys/param.h>
39 #include <sys/bus.h>
40 #include <sys/fcntl.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h>
44 #include <sys/linker_set.h>
45 #include <sys/mutex.h>
46 #include <sys/namei.h>
47 #include <sys/proc.h>
48 #include <sys/sdt.h>
49 #include <sys/syscallsubr.h>
50 #include <sys/systm.h>
51 #include <sys/vnode.h>
52
53 #include <machine/stdarg.h>
54
55 #include <compat/linux/linux_util.h>
56 #ifdef COMPAT_LINUX32
57 #include <machine/../linux32/linux.h>
58 #else
59 #include <machine/../linux/linux.h>
60 #endif
61
62 #include <compat/linux/linux_dtrace.h>
63
64 const char      linux_emul_path[] = "/compat/linux";
65
66 /* DTrace init */
67 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
68
69 /**
70  * DTrace probes in this module.
71  */
72 LIN_SDT_PROBE_DEFINE5(util, linux_emul_convpath, entry, "const char *",
73     "enum uio_seg", "char **", "int", "int");
74 LIN_SDT_PROBE_DEFINE1(util, linux_emul_convpath, return, "int");
75 LIN_SDT_PROBE_DEFINE1(util, linux_msg, entry, "const char *");
76 LIN_SDT_PROBE_DEFINE0(util, linux_msg, return);
77 LIN_SDT_PROBE_DEFINE2(util, linux_driver_get_name_dev, entry, "device_t",
78     "const char *");
79 LIN_SDT_PROBE_DEFINE0(util, linux_driver_get_name_dev, nullcall);
80 LIN_SDT_PROBE_DEFINE1(util, linux_driver_get_name_dev, return, "char *");
81 LIN_SDT_PROBE_DEFINE3(util, linux_driver_get_major_minor, entry, "char *",
82     "int *", "int *");
83 LIN_SDT_PROBE_DEFINE0(util, linux_driver_get_major_minor, nullcall);
84 LIN_SDT_PROBE_DEFINE1(util, linux_driver_get_major_minor, notfound, "char *");
85 LIN_SDT_PROBE_DEFINE3(util, linux_driver_get_major_minor, return, "int",
86     "int", "int");
87 LIN_SDT_PROBE_DEFINE0(util, linux_get_char_devices, entry);
88 LIN_SDT_PROBE_DEFINE1(util, linux_get_char_devices, return, "char *");
89 LIN_SDT_PROBE_DEFINE1(util, linux_free_get_char_devices, entry, "char *");
90 LIN_SDT_PROBE_DEFINE0(util, linux_free_get_char_devices, return);
91 LIN_SDT_PROBE_DEFINE1(util, linux_device_register_handler, entry,
92     "struct linux_device_handler *");
93 LIN_SDT_PROBE_DEFINE1(util, linux_device_register_handler, return, "int");
94 LIN_SDT_PROBE_DEFINE1(util, linux_device_unregister_handler, entry,
95     "struct linux_device_handler *");
96 LIN_SDT_PROBE_DEFINE1(util, linux_device_unregister_handler, return, "int");
97
98 /*
99  * Search an alternate path before passing pathname arguments on to
100  * system calls. Useful for keeping a separate 'emulation tree'.
101  *
102  * If cflag is set, we check if an attempt can be made to create the
103  * named file, i.e. we check if the directory it should be in exists.
104  */
105 int
106 linux_emul_convpath(struct thread *td, const char *path, enum uio_seg pathseg,
107     char **pbuf, int cflag, int dfd)
108 {
109         int retval;
110
111         LIN_SDT_PROBE5(util, linux_emul_convpath, entry, path, pathseg, pbuf,
112             cflag, dfd);
113
114         retval = kern_alternate_path(td, linux_emul_path, path, pathseg, pbuf,
115             cflag, dfd);
116
117         LIN_SDT_PROBE1(util, linux_emul_convpath, return, retval);
118         return (retval);
119 }
120
121 void
122 linux_msg(const struct thread *td, const char *fmt, ...)
123 {
124         va_list ap;
125         struct proc *p;
126
127         LIN_SDT_PROBE1(util, linux_msg, entry, fmt);
128
129         p = td->td_proc;
130         printf("linux: pid %d (%s): ", (int)p->p_pid, p->p_comm);
131         va_start(ap, fmt);
132         vprintf(fmt, ap);
133         va_end(ap);
134         printf("\n");
135
136         LIN_SDT_PROBE0(util, linux_msg, return);
137 }
138
139 struct device_element
140 {
141         TAILQ_ENTRY(device_element) list;
142         struct linux_device_handler entry;
143 };
144
145 static TAILQ_HEAD(, device_element) devices =
146         TAILQ_HEAD_INITIALIZER(devices);
147
148 static struct linux_device_handler null_handler =
149         { "mem", "mem", "null", "null", 1, 3, 1};
150
151 DATA_SET(linux_device_handler_set, null_handler);
152
153 char *
154 linux_driver_get_name_dev(device_t dev)
155 {
156         struct device_element *de;
157         const char *device_name = device_get_name(dev);
158
159         LIN_SDT_PROBE2(util, linux_driver_get_name_dev, entry, dev,
160             device_name);
161
162         if (device_name == NULL) {
163                 LIN_SDT_PROBE0(util, linux_driver_get_name_dev, nullcall);
164                 LIN_SDT_PROBE1(util, linux_driver_get_name_dev, return, NULL);
165                 return NULL;
166         }
167         TAILQ_FOREACH(de, &devices, list) {
168                 if (strcmp(device_name, de->entry.bsd_driver_name) == 0) {
169                         LIN_SDT_PROBE1(util, linux_driver_get_name_dev, return,
170                             de->entry.linux_driver_name);
171                         return (de->entry.linux_driver_name);
172                 }
173         }
174
175         LIN_SDT_PROBE1(util, linux_driver_get_name_dev, return, NULL);
176         return NULL;
177 }
178
179 int
180 linux_driver_get_major_minor(const char *node, int *major, int *minor)
181 {
182         struct device_element *de;
183
184         LIN_SDT_PROBE3(util, linux_driver_get_major_minor, entry, node, major,
185             minor);
186
187         if (node == NULL || major == NULL || minor == NULL) {
188                 LIN_SDT_PROBE0(util, linux_driver_get_major_minor, nullcall);
189                 LIN_SDT_PROBE3(util, linux_driver_get_major_minor, return, 1,
190                    0, 0);
191                 return 1;
192         }
193
194         if (strlen(node) > strlen("pts/") &&
195             strncmp(node, "pts/", strlen("pts/")) == 0) {
196                 unsigned long devno;
197
198                 /*
199                  * Linux checks major and minors of the slave device
200                  * to make sure it's a pty device, so let's make him
201                  * believe it is.
202                  */
203                 devno = strtoul(node + strlen("pts/"), NULL, 10);
204                 *major = 136 + (devno / 256);
205                 *minor = devno % 256;
206
207                 LIN_SDT_PROBE3(util, linux_driver_get_major_minor, return, 0,
208                     *major, *minor);
209                 return 0;
210         }
211
212         TAILQ_FOREACH(de, &devices, list) {
213                 if (strcmp(node, de->entry.bsd_device_name) == 0) {
214                         *major = de->entry.linux_major;
215                         *minor = de->entry.linux_minor;
216
217                         LIN_SDT_PROBE3(util, linux_driver_get_major_minor,
218                             return, 0, *major, *minor);
219                         return 0;
220                 }
221         }
222
223         LIN_SDT_PROBE1(util, linux_driver_get_major_minor, notfound, node);
224         LIN_SDT_PROBE3(util, linux_driver_get_major_minor, return, 1, 0, 0);
225         return 1;
226 }
227
228 char *
229 linux_get_char_devices()
230 {
231         struct device_element *de;
232         char *temp, *string, *last;
233         char formated[256];
234         int current_size = 0, string_size = 1024;
235
236         LIN_SDT_PROBE0(util, linux_get_char_devices, entry);
237
238         string = malloc(string_size, M_LINUX, M_WAITOK);
239         string[0] = '\000';
240         last = "";
241         TAILQ_FOREACH(de, &devices, list) {
242                 if (!de->entry.linux_char_device)
243                         continue;
244                 temp = string;
245                 if (strcmp(last, de->entry.bsd_driver_name) != 0) {
246                         last = de->entry.bsd_driver_name;
247
248                         snprintf(formated, sizeof(formated), "%3d %s\n",
249                                  de->entry.linux_major,
250                                  de->entry.linux_device_name);
251                         if (strlen(formated) + current_size
252                             >= string_size) {
253                                 string_size *= 2;
254                                 string = malloc(string_size,
255                                     M_LINUX, M_WAITOK);
256                                 bcopy(temp, string, current_size);
257                                 free(temp, M_LINUX);
258                         }
259                         strcat(string, formated);
260                         current_size = strlen(string);
261                 }
262         }
263
264         LIN_SDT_PROBE1(util, linux_get_char_devices, return, string);
265         return string;
266 }
267
268 void
269 linux_free_get_char_devices(char *string)
270 {
271
272         LIN_SDT_PROBE1(util, linux_get_char_devices, entry, string);
273
274         free(string, M_LINUX);
275
276         LIN_SDT_PROBE0(util, linux_get_char_devices, return);
277 }
278
279 static int linux_major_starting = 200;
280
281 int
282 linux_device_register_handler(struct linux_device_handler *d)
283 {
284         struct device_element *de;
285
286         LIN_SDT_PROBE1(util, linux_device_register_handler, entry, d);
287
288         if (d == NULL) {
289                 LIN_SDT_PROBE1(util, linux_device_register_handler, return,
290                     EINVAL);
291                 return (EINVAL);
292         }
293
294         de = malloc(sizeof(*de), M_LINUX, M_WAITOK);
295         if (d->linux_major < 0) {
296                 d->linux_major = linux_major_starting++;
297         }
298         bcopy(d, &de->entry, sizeof(*d));
299
300         /* Add the element to the list, sorted on span. */
301         TAILQ_INSERT_TAIL(&devices, de, list);
302
303         LIN_SDT_PROBE1(util, linux_device_register_handler, return, 0);
304         return (0);
305 }
306
307 int
308 linux_device_unregister_handler(struct linux_device_handler *d)
309 {
310         struct device_element *de;
311
312         LIN_SDT_PROBE1(util, linux_device_unregister_handler, entry, d);
313
314         if (d == NULL) {
315                 LIN_SDT_PROBE1(util, linux_device_unregister_handler, return,
316                     EINVAL);
317                 return (EINVAL);
318         }
319
320         TAILQ_FOREACH(de, &devices, list) {
321                 if (bcmp(d, &de->entry, sizeof(*d)) == 0) {
322                         TAILQ_REMOVE(&devices, de, list);
323                         free(de, M_LINUX);
324
325                         LIN_SDT_PROBE1(util, linux_device_unregister_handler,
326                             return, 0);
327                         return (0);
328                 }
329         }
330
331         LIN_SDT_PROBE1(util, linux_device_unregister_handler, return, EINVAL);
332         return (EINVAL);
333 }