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/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/types.h>
41 #include <sys/fcntl.h>
43 #include <sys/malloc.h>
44 #include <sys/namei.h>
47 #include <sys/syscallsubr.h>
48 #include <sys/vnode.h>
50 #include <machine/stdarg.h>
52 #include <compat/linux/linux_dtrace.h>
53 #include <compat/linux/linux_mib.h>
54 #include <compat/linux/linux_util.h>
56 MALLOC_DEFINE(M_LINUX, "linux", "Linux mode structures");
57 MALLOC_DEFINE(M_EPOLL, "lepoll", "Linux events structures");
59 FEATURE(linuxulator_v4l, "V4L ioctl wrapper support in the linuxulator");
60 FEATURE(linuxulator_v4l2, "V4L2 ioctl wrapper support in the linuxulator");
63 * Special DTrace provider for the linuxulator.
65 * In this file we define the provider for the entire linuxulator. All
66 * modules (= files of the linuxulator) use it.
68 * We define a different name depending on the emulated bitsize, see
69 * ../../<ARCH>/linux{,32}/linux.h, e.g.:
70 * native bitsize = linuxulator
71 * amd64, 32bit emulation = linuxulator32
73 LIN_SDT_PROVIDER_DEFINE(linuxulator);
74 LIN_SDT_PROVIDER_DEFINE(linuxulator32);
76 char linux_emul_path[MAXPATHLEN] = "/compat/linux";
78 SYSCTL_STRING(_compat_linux, OID_AUTO, emul_path, CTLFLAG_RWTUN,
79 linux_emul_path, sizeof(linux_emul_path),
80 "Linux runtime environment path");
83 linux_pwd_onexec(struct thread *td)
89 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, linux_emul_path);
93 * Do not bother if we are in chroot or jail.
96 if (pwd->pwd_rdir != rootvnode) {
104 pwd_altroot(td, nd.ni_vp);
110 linux_pwd_onexec_native(struct thread *td)
113 pwd_altroot(td, NULL);
117 linux_msg(const struct thread *td, const char *fmt, ...)
122 if (linux_debug == 0)
126 printf("linux: jid %d pid %d (%s): ", p->p_ucred->cr_prison->pr_id,
127 (int)p->p_pid, p->p_comm);
134 struct device_element
136 TAILQ_ENTRY(device_element) list;
137 struct linux_device_handler entry;
140 static TAILQ_HEAD(, device_element) devices =
141 TAILQ_HEAD_INITIALIZER(devices);
143 static struct linux_device_handler null_handler =
144 { "mem", "mem", "null", "null", 1, 3, 1};
146 DATA_SET(linux_device_handler_set, null_handler);
149 linux_driver_get_name_dev(device_t dev)
151 struct device_element *de;
152 const char *device_name = device_get_name(dev);
154 if (device_name == NULL)
156 TAILQ_FOREACH(de, &devices, list) {
157 if (strcmp(device_name, de->entry.bsd_driver_name) == 0)
158 return (de->entry.linux_driver_name);
165 linux_driver_get_major_minor(const char *node, int *major, int *minor)
167 struct device_element *de;
171 if (node == NULL || major == NULL || minor == NULL)
174 sz = sizeof("pts/") - 1;
175 if (strncmp(node, "pts/", sz) == 0 && node[sz] != '\0') {
177 * Linux checks major and minors of the slave device
178 * to make sure it's a pty device, so let's make him
181 devno = strtoul(node + sz, NULL, 10);
182 *major = 136 + (devno / 256);
183 *minor = devno % 256;
187 sz = sizeof("dri/card") - 1;
188 if (strncmp(node, "dri/card", sz) == 0 && node[sz] != '\0') {
189 devno = strtoul(node + sz, NULL, 10);
190 *major = 226 + (devno / 256);
191 *minor = devno % 256;
194 sz = sizeof("dri/controlD") - 1;
195 if (strncmp(node, "dri/controlD", sz) == 0 && node[sz] != '\0') {
196 devno = strtoul(node + sz, NULL, 10);
197 *major = 226 + (devno / 256);
198 *minor = devno % 256;
201 sz = sizeof("dri/renderD") - 1;
202 if (strncmp(node, "dri/renderD", sz) == 0 && node[sz] != '\0') {
203 devno = strtoul(node + sz, NULL, 10);
204 *major = 226 + (devno / 256);
205 *minor = devno % 256;
208 sz = sizeof("drm/") - 1;
209 if (strncmp(node, "drm/", sz) == 0 && node[sz] != '\0') {
210 devno = strtoul(node + sz, NULL, 10);
211 *major = 226 + (devno / 256);
212 *minor = devno % 256;
216 TAILQ_FOREACH(de, &devices, list) {
217 if (strcmp(node, de->entry.bsd_device_name) == 0) {
218 *major = de->entry.linux_major;
219 *minor = de->entry.linux_minor;
228 linux_vn_get_major_minor(const struct vnode *vp, int *major, int *minor)
232 if (vp->v_type != VCHR)
235 if (vp->v_rdev == NULL) {
239 error = linux_driver_get_major_minor(devtoname(vp->v_rdev),
246 translate_vnhook_major_minor(struct vnode *vp, struct stat *sb)
251 sb->st_mode &= ~S_IFMT;
252 sb->st_mode |= S_IFBLK;
256 * Return the same st_dev for every devfs instance. The reason
257 * for this is to work around an idiosyncrasy of glibc getttynam()
258 * implementation: it checks whether st_dev returned for fd 0
259 * is the same as st_dev returned for the target of /proc/self/fd/0
260 * symlink, and with linux chroots having their own devfs instance,
261 * the check will fail if you chroot into it.
263 if (rootdevmp != NULL && vp->v_mount->mnt_vfc == rootdevmp->mnt_vfc)
264 sb->st_dev = rootdevmp->mnt_stat.f_fsid.val[0];
266 if (linux_vn_get_major_minor(vp, &major, &minor) == 0)
267 sb->st_rdev = makedev(major, minor);
271 linux_get_char_devices(void)
273 struct device_element *de;
274 char *temp, *string, *last;
276 int current_size = 0, string_size = 1024;
278 string = malloc(string_size, M_LINUX, M_WAITOK);
281 TAILQ_FOREACH(de, &devices, list) {
282 if (!de->entry.linux_char_device)
285 if (strcmp(last, de->entry.bsd_driver_name) != 0) {
286 last = de->entry.bsd_driver_name;
288 snprintf(formated, sizeof(formated), "%3d %s\n",
289 de->entry.linux_major,
290 de->entry.linux_device_name);
291 if (strlen(formated) + current_size
294 string = malloc(string_size,
296 bcopy(temp, string, current_size);
299 strcat(string, formated);
300 current_size = strlen(string);
308 linux_free_get_char_devices(char *string)
311 free(string, M_LINUX);
314 static int linux_major_starting = 200;
317 linux_device_register_handler(struct linux_device_handler *d)
319 struct device_element *de;
324 de = malloc(sizeof(*de), M_LINUX, M_WAITOK);
325 if (d->linux_major < 0) {
326 d->linux_major = linux_major_starting++;
328 bcopy(d, &de->entry, sizeof(*d));
330 /* Add the element to the list, sorted on span. */
331 TAILQ_INSERT_TAIL(&devices, de, list);
337 linux_device_unregister_handler(struct linux_device_handler *d)
339 struct device_element *de;
344 TAILQ_FOREACH(de, &devices, list) {
345 if (bcmp(d, &de->entry, sizeof(*d)) == 0) {
346 TAILQ_REMOVE(&devices, de, list);