2 * Copyright (c) 1994 Christos Zoulas
3 * Copyright (c) 1995 Frank van der Linden
4 * Copyright (c) 1995 Scott Bartram
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
29 * from: svr4_util.c,v 1.5 1995/01/22 23:44:50 christos Exp
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_compat.h"
36 #include "opt_kdtrace.h"
38 #include <sys/param.h>
40 #include <sys/fcntl.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>
49 #include <sys/syscallsubr.h>
50 #include <sys/systm.h>
51 #include <sys/vnode.h>
53 #include <machine/stdarg.h>
55 #include <compat/linux/linux_util.h>
57 #include <machine/../linux32/linux.h>
59 #include <machine/../linux/linux.h>
62 #include <compat/linux/linux_dtrace.h>
64 const char linux_emul_path[] = "/compat/linux";
67 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
70 * DTrace probes in this module.
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",
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 *",
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",
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");
99 * Search an alternate path before passing pathname arguments on to
100 * system calls. Useful for keeping a separate 'emulation tree'.
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.
106 linux_emul_convpath(struct thread *td, const char *path, enum uio_seg pathseg,
107 char **pbuf, int cflag, int dfd)
111 LIN_SDT_PROBE5(util, linux_emul_convpath, entry, path, pathseg, pbuf,
114 retval = kern_alternate_path(td, linux_emul_path, path, pathseg, pbuf,
117 LIN_SDT_PROBE1(util, linux_emul_convpath, return, retval);
122 linux_msg(const struct thread *td, const char *fmt, ...)
127 LIN_SDT_PROBE1(util, linux_msg, entry, fmt);
130 printf("linux: pid %d (%s): ", (int)p->p_pid, p->p_comm);
136 LIN_SDT_PROBE0(util, linux_msg, return);
139 struct device_element
141 TAILQ_ENTRY(device_element) list;
142 struct linux_device_handler entry;
145 static TAILQ_HEAD(, device_element) devices =
146 TAILQ_HEAD_INITIALIZER(devices);
148 static struct linux_device_handler null_handler =
149 { "mem", "mem", "null", "null", 1, 3, 1};
151 DATA_SET(linux_device_handler_set, null_handler);
154 linux_driver_get_name_dev(device_t dev)
156 struct device_element *de;
157 const char *device_name = device_get_name(dev);
159 LIN_SDT_PROBE2(util, linux_driver_get_name_dev, entry, dev,
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);
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);
175 LIN_SDT_PROBE1(util, linux_driver_get_name_dev, return, NULL);
180 linux_driver_get_major_minor(const char *node, int *major, int *minor)
182 struct device_element *de;
184 LIN_SDT_PROBE3(util, linux_driver_get_major_minor, entry, node, major,
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,
194 if (strlen(node) > strlen("pts/") &&
195 strncmp(node, "pts/", strlen("pts/")) == 0) {
199 * Linux checks major and minors of the slave device
200 * to make sure it's a pty device, so let's make him
203 devno = strtoul(node + strlen("pts/"), NULL, 10);
204 *major = 136 + (devno / 256);
205 *minor = devno % 256;
207 LIN_SDT_PROBE3(util, linux_driver_get_major_minor, return, 0,
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;
217 LIN_SDT_PROBE3(util, linux_driver_get_major_minor,
218 return, 0, *major, *minor);
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);
229 linux_get_char_devices()
231 struct device_element *de;
232 char *temp, *string, *last;
234 int current_size = 0, string_size = 1024;
236 LIN_SDT_PROBE0(util, linux_get_char_devices, entry);
238 string = malloc(string_size, M_LINUX, M_WAITOK);
241 TAILQ_FOREACH(de, &devices, list) {
242 if (!de->entry.linux_char_device)
245 if (strcmp(last, de->entry.bsd_driver_name) != 0) {
246 last = de->entry.bsd_driver_name;
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
254 string = malloc(string_size,
256 bcopy(temp, string, current_size);
259 strcat(string, formated);
260 current_size = strlen(string);
264 LIN_SDT_PROBE1(util, linux_get_char_devices, return, string);
269 linux_free_get_char_devices(char *string)
272 LIN_SDT_PROBE1(util, linux_get_char_devices, entry, string);
274 free(string, M_LINUX);
276 LIN_SDT_PROBE0(util, linux_get_char_devices, return);
279 static int linux_major_starting = 200;
282 linux_device_register_handler(struct linux_device_handler *d)
284 struct device_element *de;
286 LIN_SDT_PROBE1(util, linux_device_register_handler, entry, d);
289 LIN_SDT_PROBE1(util, linux_device_register_handler, return,
294 de = malloc(sizeof(*de), M_LINUX, M_WAITOK);
295 if (d->linux_major < 0) {
296 d->linux_major = linux_major_starting++;
298 bcopy(d, &de->entry, sizeof(*d));
300 /* Add the element to the list, sorted on span. */
301 TAILQ_INSERT_TAIL(&devices, de, list);
303 LIN_SDT_PROBE1(util, linux_device_register_handler, return, 0);
308 linux_device_unregister_handler(struct linux_device_handler *d)
310 struct device_element *de;
312 LIN_SDT_PROBE1(util, linux_device_unregister_handler, entry, d);
315 LIN_SDT_PROBE1(util, linux_device_unregister_handler, return,
320 TAILQ_FOREACH(de, &devices, list) {
321 if (bcmp(d, &de->entry, sizeof(*d)) == 0) {
322 TAILQ_REMOVE(&devices, de, list);
325 LIN_SDT_PROBE1(util, linux_device_unregister_handler,
331 LIN_SDT_PROBE1(util, linux_device_unregister_handler, return, EINVAL);