xref: /freebsd/sys/compat/linux/linux_util.c (revision 7870adb640dfa24455291c665ac965046e5622e9)
1898b0535SWarner Losh /*-
2d66a5066SPeter Wemm  * Copyright (c) 1994 Christos Zoulas
3d66a5066SPeter Wemm  * Copyright (c) 1995 Frank van der Linden
4d66a5066SPeter Wemm  * Copyright (c) 1995 Scott Bartram
5d66a5066SPeter Wemm  * All rights reserved.
6d66a5066SPeter Wemm  *
7d66a5066SPeter Wemm  * Redistribution and use in source and binary forms, with or without
8d66a5066SPeter Wemm  * modification, are permitted provided that the following conditions
9d66a5066SPeter Wemm  * are met:
10d66a5066SPeter Wemm  * 1. Redistributions of source code must retain the above copyright
11d66a5066SPeter Wemm  *    notice, this list of conditions and the following disclaimer.
12d66a5066SPeter Wemm  * 2. Redistributions in binary form must reproduce the above copyright
13d66a5066SPeter Wemm  *    notice, this list of conditions and the following disclaimer in the
14d66a5066SPeter Wemm  *    documentation and/or other materials provided with the distribution.
15d66a5066SPeter Wemm  * 3. The name of the author may not be used to endorse or promote products
16d66a5066SPeter Wemm  *    derived from this software without specific prior written permission
17d66a5066SPeter Wemm  *
18d66a5066SPeter Wemm  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19d66a5066SPeter Wemm  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20d66a5066SPeter Wemm  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21d66a5066SPeter Wemm  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22d66a5066SPeter Wemm  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23d66a5066SPeter Wemm  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24d66a5066SPeter Wemm  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25d66a5066SPeter Wemm  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26d66a5066SPeter Wemm  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27d66a5066SPeter Wemm  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28d66a5066SPeter Wemm  *
29d66a5066SPeter Wemm  *	from: svr4_util.c,v 1.5 1995/01/22 23:44:50 christos Exp
30d66a5066SPeter Wemm  */
31d66a5066SPeter Wemm 
3216dbc7f2SDavid E. O'Brien #include <sys/cdefs.h>
3316dbc7f2SDavid E. O'Brien __FBSDID("$FreeBSD$");
3416dbc7f2SDavid E. O'Brien 
359b44bfc5SAlexander Leidinger #include "opt_compat.h"
369b44bfc5SAlexander Leidinger 
37d66a5066SPeter Wemm #include <sys/param.h>
38060e4882SDoug Ambrisko #include <sys/bus.h>
3957b4252eSKonstantin Belousov #include <sys/fcntl.h>
40d4364109SBruce Evans #include <sys/lock.h>
41d4364109SBruce Evans #include <sys/malloc.h>
42060e4882SDoug Ambrisko #include <sys/linker_set.h>
43d4364109SBruce Evans #include <sys/mutex.h>
44d66a5066SPeter Wemm #include <sys/namei.h>
45d66a5066SPeter Wemm #include <sys/proc.h>
4625771ec2SJohn Baldwin #include <sys/syscallsubr.h>
47d4364109SBruce Evans #include <sys/systm.h>
48d66a5066SPeter Wemm #include <sys/vnode.h>
49d66a5066SPeter Wemm 
50e15583ceSAlfred Perlstein #include <machine/stdarg.h>
51e15583ceSAlfred Perlstein 
52ac951e62SMarcel Moolenaar #include <compat/linux/linux_util.h>
539b44bfc5SAlexander Leidinger #ifdef COMPAT_LINUX32
549b44bfc5SAlexander Leidinger #include <machine/../linux32/linux.h>
559b44bfc5SAlexander Leidinger #else
569b44bfc5SAlexander Leidinger #include <machine/../linux/linux.h>
579b44bfc5SAlexander Leidinger #endif
58762e6b85SEivind Eklund 
59d66a5066SPeter Wemm const char      linux_emul_path[] = "/compat/linux";
60d66a5066SPeter Wemm 
61d66a5066SPeter Wemm /*
62802e08a3SAlexander Leidinger  * Search an alternate path before passing pathname arguments on to
63802e08a3SAlexander Leidinger  * system calls. Useful for keeping a separate 'emulation tree'.
64d66a5066SPeter Wemm  *
65802e08a3SAlexander Leidinger  * If cflag is set, we check if an attempt can be made to create the
66802e08a3SAlexander Leidinger  * named file, i.e. we check if the directory it should be in exists.
67d66a5066SPeter Wemm  */
68d66a5066SPeter Wemm int
6948b05c3fSKonstantin Belousov linux_emul_convpath(td, path, pathseg, pbuf, cflag, dfd)
70ff766321SIan Dowse 	struct thread	 *td;
7148b05c3fSKonstantin Belousov 	const char	 *path;
72ff766321SIan Dowse 	enum uio_seg	  pathseg;
73ff766321SIan Dowse 	char		**pbuf;
74ff766321SIan Dowse 	int		  cflag;
7548b05c3fSKonstantin Belousov 	int		  dfd;
76ff766321SIan Dowse {
77d66a5066SPeter Wemm 
7825771ec2SJohn Baldwin 	return (kern_alternate_path(td, linux_emul_path, path, pathseg, pbuf,
7948b05c3fSKonstantin Belousov 		cflag, dfd));
80d66a5066SPeter Wemm }
81e15583ceSAlfred Perlstein 
82e15583ceSAlfred Perlstein void
83e15583ceSAlfred Perlstein linux_msg(const struct thread *td, const char *fmt, ...)
84e15583ceSAlfred Perlstein {
85e15583ceSAlfred Perlstein 	va_list ap;
86e15583ceSAlfred Perlstein 	struct proc *p;
87e15583ceSAlfred Perlstein 
88e15583ceSAlfred Perlstein 	p = td->td_proc;
89e15583ceSAlfred Perlstein 	printf("linux: pid %d (%s): ", (int)p->p_pid, p->p_comm);
90e15583ceSAlfred Perlstein 	va_start(ap, fmt);
91e15583ceSAlfred Perlstein 	vprintf(fmt, ap);
92e15583ceSAlfred Perlstein 	va_end(ap);
93e15583ceSAlfred Perlstein 	printf("\n");
94e15583ceSAlfred Perlstein }
95060e4882SDoug Ambrisko 
96060e4882SDoug Ambrisko struct device_element
97060e4882SDoug Ambrisko {
98060e4882SDoug Ambrisko 	TAILQ_ENTRY(device_element) list;
99060e4882SDoug Ambrisko 	struct linux_device_handler entry;
100060e4882SDoug Ambrisko };
101060e4882SDoug Ambrisko 
102060e4882SDoug Ambrisko static TAILQ_HEAD(, device_element) devices =
103060e4882SDoug Ambrisko 	TAILQ_HEAD_INITIALIZER(devices);
104060e4882SDoug Ambrisko 
105060e4882SDoug Ambrisko static struct linux_device_handler null_handler =
106060e4882SDoug Ambrisko 	{ "mem", "mem", "null", "null", 1, 3, 1};
107060e4882SDoug Ambrisko 
108060e4882SDoug Ambrisko DATA_SET(linux_device_handler_set, null_handler);
109060e4882SDoug Ambrisko 
110060e4882SDoug Ambrisko char *
111060e4882SDoug Ambrisko linux_driver_get_name_dev(device_t dev)
112060e4882SDoug Ambrisko {
113060e4882SDoug Ambrisko 	struct device_element *de;
114060e4882SDoug Ambrisko 	const char *device_name = device_get_name(dev);
115060e4882SDoug Ambrisko 
116060e4882SDoug Ambrisko 	if (device_name == NULL)
117060e4882SDoug Ambrisko 		return NULL;
118060e4882SDoug Ambrisko 	TAILQ_FOREACH(de, &devices, list) {
119060e4882SDoug Ambrisko 		if (strcmp(device_name, de->entry.bsd_driver_name) == 0)
120060e4882SDoug Ambrisko 			return (de->entry.linux_driver_name);
121060e4882SDoug Ambrisko 	}
122060e4882SDoug Ambrisko 
123060e4882SDoug Ambrisko 	return NULL;
124060e4882SDoug Ambrisko }
125060e4882SDoug Ambrisko 
126060e4882SDoug Ambrisko int
127*7870adb6SEd Schouten linux_driver_get_major_minor(const char *node, int *major, int *minor)
128060e4882SDoug Ambrisko {
129060e4882SDoug Ambrisko 	struct device_element *de;
130060e4882SDoug Ambrisko 
131060e4882SDoug Ambrisko 	if (node == NULL || major == NULL || minor == NULL)
132060e4882SDoug Ambrisko 		return 1;
133a147e6caSEd Schouten 
134a147e6caSEd Schouten 	if (strlen(node) > strlen("pts/") &&
135a147e6caSEd Schouten 	    strncmp(node, "pts/", strlen("pts/")) == 0) {
136a147e6caSEd Schouten 		unsigned long devno;
137a147e6caSEd Schouten 
138a147e6caSEd Schouten 		/*
139a147e6caSEd Schouten 		 * Linux checks major and minors of the slave device
140a147e6caSEd Schouten 		 * to make sure it's a pty device, so let's make him
141a147e6caSEd Schouten 		 * believe it is.
142a147e6caSEd Schouten 		 */
143a147e6caSEd Schouten 		devno = strtoul(node + strlen("pts/"), NULL, 10);
144a147e6caSEd Schouten 		*major = 136 + (devno / 256);
145a147e6caSEd Schouten 		*minor = devno % 256;
146a147e6caSEd Schouten 		return 0;
147a147e6caSEd Schouten 	}
148a147e6caSEd Schouten 
149060e4882SDoug Ambrisko 	TAILQ_FOREACH(de, &devices, list) {
150060e4882SDoug Ambrisko 		if (strcmp(node, de->entry.bsd_device_name) == 0) {
151060e4882SDoug Ambrisko 			*major = de->entry.linux_major;
152060e4882SDoug Ambrisko 			*minor = de->entry.linux_minor;
153060e4882SDoug Ambrisko 			return 0;
154060e4882SDoug Ambrisko 		}
155060e4882SDoug Ambrisko 	}
156060e4882SDoug Ambrisko 
157060e4882SDoug Ambrisko 	return 1;
158060e4882SDoug Ambrisko }
159060e4882SDoug Ambrisko 
160060e4882SDoug Ambrisko char *
161060e4882SDoug Ambrisko linux_get_char_devices()
162060e4882SDoug Ambrisko {
163060e4882SDoug Ambrisko 	struct device_element *de;
164060e4882SDoug Ambrisko 	char *temp, *string, *last;
165060e4882SDoug Ambrisko 	char formated[256];
166060e4882SDoug Ambrisko 	int current_size = 0, string_size = 1024;
167060e4882SDoug Ambrisko 
1681ede983cSDag-Erling Smørgrav 	string = malloc(string_size, M_LINUX, M_WAITOK);
169060e4882SDoug Ambrisko 	string[0] = '\000';
170060e4882SDoug Ambrisko 	last = "";
171060e4882SDoug Ambrisko 	TAILQ_FOREACH(de, &devices, list) {
172060e4882SDoug Ambrisko 		if (!de->entry.linux_char_device)
173060e4882SDoug Ambrisko 			continue;
174060e4882SDoug Ambrisko 		temp = string;
175060e4882SDoug Ambrisko 		if (strcmp(last, de->entry.bsd_driver_name) != 0) {
176060e4882SDoug Ambrisko 			last = de->entry.bsd_driver_name;
177060e4882SDoug Ambrisko 
178060e4882SDoug Ambrisko 			snprintf(formated, sizeof(formated), "%3d %s\n",
179060e4882SDoug Ambrisko 				 de->entry.linux_major,
180060e4882SDoug Ambrisko 				 de->entry.linux_device_name);
181060e4882SDoug Ambrisko 			if (strlen(formated) + current_size
182060e4882SDoug Ambrisko 			    >= string_size) {
183060e4882SDoug Ambrisko 				string_size *= 2;
1841ede983cSDag-Erling Smørgrav 				string = malloc(string_size,
185060e4882SDoug Ambrisko 				    M_LINUX, M_WAITOK);
186060e4882SDoug Ambrisko 				bcopy(temp, string, current_size);
1871ede983cSDag-Erling Smørgrav 				free(temp, M_LINUX);
188060e4882SDoug Ambrisko 			}
189060e4882SDoug Ambrisko 			strcat(string, formated);
190060e4882SDoug Ambrisko 			current_size = strlen(string);
191060e4882SDoug Ambrisko 		}
192060e4882SDoug Ambrisko 	}
193060e4882SDoug Ambrisko 
194060e4882SDoug Ambrisko 	return string;
195060e4882SDoug Ambrisko }
196060e4882SDoug Ambrisko 
197060e4882SDoug Ambrisko void
198060e4882SDoug Ambrisko linux_free_get_char_devices(char *string)
199060e4882SDoug Ambrisko {
2001ede983cSDag-Erling Smørgrav 	free(string, M_LINUX);
201060e4882SDoug Ambrisko }
202060e4882SDoug Ambrisko 
203060e4882SDoug Ambrisko static int linux_major_starting = 200;
204060e4882SDoug Ambrisko 
205060e4882SDoug Ambrisko int
206060e4882SDoug Ambrisko linux_device_register_handler(struct linux_device_handler *d)
207060e4882SDoug Ambrisko {
208060e4882SDoug Ambrisko 	struct device_element *de;
209060e4882SDoug Ambrisko 
210060e4882SDoug Ambrisko 	if (d == NULL)
211060e4882SDoug Ambrisko 		return (EINVAL);
212060e4882SDoug Ambrisko 
2131ede983cSDag-Erling Smørgrav 	de = malloc(sizeof(*de),
214060e4882SDoug Ambrisko 	    M_LINUX, M_WAITOK);
215060e4882SDoug Ambrisko 	if (d->linux_major < 0) {
216060e4882SDoug Ambrisko 		d->linux_major = linux_major_starting++;
217060e4882SDoug Ambrisko 	}
218060e4882SDoug Ambrisko 	bcopy(d, &de->entry, sizeof(*d));
219060e4882SDoug Ambrisko 
220060e4882SDoug Ambrisko 	/* Add the element to the list, sorted on span. */
221060e4882SDoug Ambrisko 	TAILQ_INSERT_TAIL(&devices, de, list);
222060e4882SDoug Ambrisko 
223060e4882SDoug Ambrisko 	return (0);
224060e4882SDoug Ambrisko }
225060e4882SDoug Ambrisko 
226060e4882SDoug Ambrisko int
227060e4882SDoug Ambrisko linux_device_unregister_handler(struct linux_device_handler *d)
228060e4882SDoug Ambrisko {
229060e4882SDoug Ambrisko 	struct device_element *de;
230060e4882SDoug Ambrisko 
231060e4882SDoug Ambrisko 	if (d == NULL)
232060e4882SDoug Ambrisko 		return (EINVAL);
233060e4882SDoug Ambrisko 
234060e4882SDoug Ambrisko 	TAILQ_FOREACH(de, &devices, list) {
235060e4882SDoug Ambrisko 		if (bcmp(d, &de->entry, sizeof(*d)) == 0) {
236060e4882SDoug Ambrisko 			TAILQ_REMOVE(&devices, de, list);
2371ede983cSDag-Erling Smørgrav 			free(de, M_LINUX);
238060e4882SDoug Ambrisko 			return (0);
239060e4882SDoug Ambrisko 		}
240060e4882SDoug Ambrisko 	}
241060e4882SDoug Ambrisko 
242060e4882SDoug Ambrisko 	return (EINVAL);
243060e4882SDoug Ambrisko }
244