Deleted Added
sdiff udiff text old ( 158311 ) new ( 161310 )
full compact
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: head/sys/compat/linux/linux_util.c 161310 2006-08-15 12:54:30Z netchild $");
34
35#include "opt_compat.h"
36
37#include <sys/param.h>
38#include <sys/bus.h>
39#include <sys/lock.h>
40#include <sys/malloc.h>
41#include <sys/linker_set.h>
42#include <sys/mutex.h>
43#include <sys/namei.h>
44#include <sys/proc.h>
45#include <sys/syscallsubr.h>
46#include <sys/systm.h>
47#include <sys/vnode.h>
48
49#include <machine/stdarg.h>
50
51#include <compat/linux/linux_util.h>
52#ifdef COMPAT_LINUX32
53#include <machine/../linux32/linux.h>
54#else
55#include <machine/../linux/linux.h>
56#endif
57
58const char linux_emul_path[] = "/compat/linux";
59
60/*
61 * Search an alternate path before passing pathname arguments on
62 * to system calls. Useful for keeping a separate 'emulation tree'.
63 *
64 * If cflag is set, we check if an attempt can be made to create
65 * the named file, i.e. we check if the directory it should
66 * be in exists.
67 */
68int
69linux_emul_convpath(td, path, pathseg, pbuf, cflag)
70 struct thread *td;
71 char *path;
72 enum uio_seg pathseg;
73 char **pbuf;
74 int cflag;
75{
76
77 return (kern_alternate_path(td, linux_emul_path, path, pathseg, pbuf,
78 cflag));
79}
80
81void
82linux_msg(const struct thread *td, const char *fmt, ...)
83{
84 va_list ap;
85 struct proc *p;
86
87 p = td->td_proc;
88 printf("linux: pid %d (%s): ", (int)p->p_pid, p->p_comm);
89 va_start(ap, fmt);
90 vprintf(fmt, ap);
91 va_end(ap);
92 printf("\n");
93}
94
95struct device_element
96{
97 TAILQ_ENTRY(device_element) list;
98 struct linux_device_handler entry;
99};
100
101static TAILQ_HEAD(, device_element) devices =
102 TAILQ_HEAD_INITIALIZER(devices);
103
104static struct linux_device_handler null_handler =
105 { "mem", "mem", "null", "null", 1, 3, 1};
106
107DATA_SET(linux_device_handler_set, null_handler);
108
109char *
110linux_driver_get_name_dev(device_t dev)
111{
112 struct device_element *de;
113 const char *device_name = device_get_name(dev);
114
115 if (device_name == NULL)
116 return NULL;
117 TAILQ_FOREACH(de, &devices, list) {
118 if (strcmp(device_name, de->entry.bsd_driver_name) == 0)
119 return (de->entry.linux_driver_name);
120 }
121
122 return NULL;
123}
124
125int
126linux_driver_get_major_minor(char *node, int *major, int *minor)
127{
128 struct device_element *de;
129
130 if (node == NULL || major == NULL || minor == NULL)
131 return 1;
132 TAILQ_FOREACH(de, &devices, list) {
133 if (strcmp(node, de->entry.bsd_device_name) == 0) {
134 *major = de->entry.linux_major;
135 *minor = de->entry.linux_minor;
136 return 0;
137 }
138 }
139
140 return 1;
141}
142
143char *
144linux_get_char_devices()
145{
146 struct device_element *de;
147 char *temp, *string, *last;
148 char formated[256];
149 int current_size = 0, string_size = 1024;
150
151 MALLOC(string, char *, string_size, M_LINUX, M_WAITOK);
152 string[0] = '\000';
153 last = "";
154 TAILQ_FOREACH(de, &devices, list) {
155 if (!de->entry.linux_char_device)
156 continue;
157 temp = string;
158 if (strcmp(last, de->entry.bsd_driver_name) != 0) {
159 last = de->entry.bsd_driver_name;
160
161 snprintf(formated, sizeof(formated), "%3d %s\n",
162 de->entry.linux_major,
163 de->entry.linux_device_name);
164 if (strlen(formated) + current_size
165 >= string_size) {
166 string_size *= 2;
167 MALLOC(string, char *, string_size,
168 M_LINUX, M_WAITOK);
169 bcopy(temp, string, current_size);
170 FREE(temp, M_LINUX);
171 }
172 strcat(string, formated);
173 current_size = strlen(string);
174 }
175 }
176
177 return string;
178}
179
180void
181linux_free_get_char_devices(char *string)
182{
183 FREE(string, M_LINUX);
184}
185
186static int linux_major_starting = 200;
187
188int
189linux_device_register_handler(struct linux_device_handler *d)
190{
191 struct device_element *de;
192
193 if (d == NULL)
194 return (EINVAL);
195
196 MALLOC(de, struct device_element *, sizeof(*de),
197 M_LINUX, M_WAITOK);
198 if (d->linux_major < 0) {
199 d->linux_major = linux_major_starting++;
200 }
201 bcopy(d, &de->entry, sizeof(*d));
202
203 /* Add the element to the list, sorted on span. */
204 TAILQ_INSERT_TAIL(&devices, de, list);
205
206 return (0);
207}
208
209int
210linux_device_unregister_handler(struct linux_device_handler *d)
211{
212 struct device_element *de;
213
214 if (d == NULL)
215 return (EINVAL);
216
217 TAILQ_FOREACH(de, &devices, list) {
218 if (bcmp(d, &de->entry, sizeof(*d)) == 0) {
219 TAILQ_REMOVE(&devices, de, list);
220 FREE(de, M_LINUX);
221 return (0);
222 }
223 }
224
225 return (EINVAL);
226}