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