2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1994 Christos Zoulas
5 * Copyright (c) 1995 Frank van der Linden
6 * Copyright (c) 1995 Scott Bartram
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * from: svr4_util.c,v 1.5 1995/01/22 23:44:50 christos Exp
34 #include <sys/param.h>
37 #include <sys/fcntl.h>
39 #include <sys/malloc.h>
40 #include <sys/namei.h>
43 #include <sys/syscallsubr.h>
44 #include <sys/vnode.h>
46 #include <machine/stdarg.h>
48 #include <compat/linux/linux_dtrace.h>
49 #include <compat/linux/linux_mib.h>
50 #include <compat/linux/linux_util.h>
52 MALLOC_DEFINE(M_LINUX, "linux", "Linux mode structures");
53 MALLOC_DEFINE(M_EPOLL, "lepoll", "Linux events structures");
55 FEATURE(linuxulator_v4l, "V4L ioctl wrapper support in the linuxulator");
56 FEATURE(linuxulator_v4l2, "V4L2 ioctl wrapper support in the linuxulator");
59 * Special DTrace provider for the linuxulator.
61 * In this file we define the provider for the entire linuxulator. All
62 * modules (= files of the linuxulator) use it.
64 * We define a different name depending on the emulated bitsize, see
65 * ../../<ARCH>/linux{,32}/linux.h, e.g.:
66 * native bitsize = linuxulator
67 * amd64, 32bit emulation = linuxulator32
69 LIN_SDT_PROVIDER_DEFINE(linuxulator);
70 LIN_SDT_PROVIDER_DEFINE(linuxulator32);
72 char linux_emul_path[MAXPATHLEN] = "/compat/linux";
74 SYSCTL_STRING(_compat_linux, OID_AUTO, emul_path, CTLFLAG_RWTUN,
75 linux_emul_path, sizeof(linux_emul_path),
76 "Linux runtime environment path");
79 linux_pwd_onexec(struct thread *td)
85 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, linux_emul_path);
89 * Do not bother if we are in chroot or jail.
92 if (pwd->pwd_rdir != rootvnode) {
100 pwd_altroot(td, nd.ni_vp);
106 linux_pwd_onexec_native(struct thread *td)
109 pwd_altroot(td, NULL);
113 linux_msg(const struct thread *td, const char *fmt, ...)
118 if (linux_debug == 0)
122 printf("linux: jid %d pid %d (%s): ", p->p_ucred->cr_prison->pr_id,
123 (int)p->p_pid, p->p_comm);
130 struct device_element
132 TAILQ_ENTRY(device_element) list;
133 struct linux_device_handler entry;
136 static TAILQ_HEAD(, device_element) devices =
137 TAILQ_HEAD_INITIALIZER(devices);
139 static struct linux_device_handler null_handler =
140 { "mem", "mem", "null", "null", 1, 3, 1};
142 DATA_SET(linux_device_handler_set, null_handler);
145 linux_driver_get_name_dev(device_t dev)
147 struct device_element *de;
148 const char *device_name = device_get_name(dev);
150 if (device_name == NULL)
152 TAILQ_FOREACH(de, &devices, list) {
153 if (strcmp(device_name, de->entry.bsd_driver_name) == 0)
154 return (de->entry.linux_driver_name);
161 linux_driver_get_major_minor(const char *node, int *major, int *minor)
163 struct device_element *de;
167 if (node == NULL || major == NULL || minor == NULL)
170 sz = sizeof("pts/") - 1;
171 if (strncmp(node, "pts/", sz) == 0 && node[sz] != '\0') {
173 * Linux checks major and minors of the slave device
174 * to make sure it's a pty device, so let's make him
177 devno = strtoul(node + sz, NULL, 10);
178 *major = 136 + (devno / 256);
179 *minor = devno % 256;
183 sz = sizeof("dri/card") - 1;
184 if (strncmp(node, "dri/card", sz) == 0 && node[sz] != '\0') {
185 devno = strtoul(node + sz, NULL, 10);
186 *major = 226 + (devno / 256);
187 *minor = devno % 256;
190 sz = sizeof("dri/controlD") - 1;
191 if (strncmp(node, "dri/controlD", sz) == 0 && node[sz] != '\0') {
192 devno = strtoul(node + sz, NULL, 10);
193 *major = 226 + (devno / 256);
194 *minor = devno % 256;
197 sz = sizeof("dri/renderD") - 1;
198 if (strncmp(node, "dri/renderD", sz) == 0 && node[sz] != '\0') {
199 devno = strtoul(node + sz, NULL, 10);
200 *major = 226 + (devno / 256);
201 *minor = devno % 256;
204 sz = sizeof("drm/") - 1;
205 if (strncmp(node, "drm/", sz) == 0 && node[sz] != '\0') {
206 devno = strtoul(node + sz, NULL, 10);
207 *major = 226 + (devno / 256);
208 *minor = devno % 256;
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;
224 linux_vn_get_major_minor(const struct vnode *vp, int *major, int *minor)
228 if (vp->v_type != VCHR)
231 if (vp->v_rdev == NULL) {
235 error = linux_driver_get_major_minor(devtoname(vp->v_rdev),
242 translate_vnhook_major_minor(struct vnode *vp, struct stat *sb)
247 sb->st_mode &= ~S_IFMT;
248 sb->st_mode |= S_IFBLK;
252 * Return the same st_dev for every devfs instance. The reason
253 * for this is to work around an idiosyncrasy of glibc getttynam()
254 * implementation: it checks whether st_dev returned for fd 0
255 * is the same as st_dev returned for the target of /proc/self/fd/0
256 * symlink, and with linux chroots having their own devfs instance,
257 * the check will fail if you chroot into it.
259 if (rootdevmp != NULL && vp->v_mount->mnt_vfc == rootdevmp->mnt_vfc)
260 sb->st_dev = rootdevmp->mnt_stat.f_fsid.val[0];
262 if (linux_vn_get_major_minor(vp, &major, &minor) == 0)
263 sb->st_rdev = makedev(major, minor);
267 linux_get_char_devices(void)
269 struct device_element *de;
270 char *temp, *string, *last;
272 int current_size = 0, string_size = 1024;
274 string = malloc(string_size, M_LINUX, M_WAITOK);
277 TAILQ_FOREACH(de, &devices, list) {
278 if (!de->entry.linux_char_device)
281 if (strcmp(last, de->entry.bsd_driver_name) != 0) {
282 last = de->entry.bsd_driver_name;
284 snprintf(formated, sizeof(formated), "%3d %s\n",
285 de->entry.linux_major,
286 de->entry.linux_device_name);
287 if (strlen(formated) + current_size
290 string = malloc(string_size,
292 bcopy(temp, string, current_size);
295 strcat(string, formated);
296 current_size = strlen(string);
304 linux_free_get_char_devices(char *string)
307 free(string, M_LINUX);
310 static int linux_major_starting = 200;
313 linux_device_register_handler(struct linux_device_handler *d)
315 struct device_element *de;
320 de = malloc(sizeof(*de), M_LINUX, M_WAITOK);
321 if (d->linux_major < 0) {
322 d->linux_major = linux_major_starting++;
324 bcopy(d, &de->entry, sizeof(*d));
326 /* Add the element to the list, sorted on span. */
327 TAILQ_INSERT_TAIL(&devices, de, list);
333 linux_device_unregister_handler(struct linux_device_handler *d)
335 struct device_element *de;
340 TAILQ_FOREACH(de, &devices, list) {
341 if (bcmp(d, &de->entry, sizeof(*d)) == 0) {
342 TAILQ_REMOVE(&devices, de, list);