2 * Copyright (c) 1999 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/sysctl.h>
37 #include <sys/malloc.h>
40 #include <sys/mutex.h>
42 #include "opt_compat.h"
45 #include <machine/../linux32/linux.h>
47 #include <machine/../linux/linux.h>
49 #include <compat/linux/linux_mib.h>
52 char pr_osname[LINUX_MAX_UTSNAME];
53 char pr_osrelease[LINUX_MAX_UTSNAME];
55 int pr_use_linux26; /* flag to determine whether to use 2.6 emulation */
58 SYSCTL_NODE(_compat, OID_AUTO, linux, CTLFLAG_RW, 0,
61 static struct mtx osname_lock;
62 MTX_SYSINIT(linux_osname, &osname_lock, "linux osname", MTX_DEF);
64 static char linux_osname[LINUX_MAX_UTSNAME] = "Linux";
67 linux_sysctl_osname(SYSCTL_HANDLER_ARGS)
69 char osname[LINUX_MAX_UTSNAME];
72 linux_get_osname(req->td, osname);
73 error = sysctl_handle_string(oidp, osname, LINUX_MAX_UTSNAME, req);
74 if (error || req->newptr == NULL)
76 error = linux_set_osname(req->td, osname);
80 SYSCTL_PROC(_compat_linux, OID_AUTO, osname,
81 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON,
82 0, 0, linux_sysctl_osname, "A",
83 "Linux kernel OS name");
85 static char linux_osrelease[LINUX_MAX_UTSNAME] = "2.4.2";
86 static int linux_use_linux26 = 0;
89 linux_sysctl_osrelease(SYSCTL_HANDLER_ARGS)
91 char osrelease[LINUX_MAX_UTSNAME];
94 linux_get_osrelease(req->td, osrelease);
95 error = sysctl_handle_string(oidp, osrelease, LINUX_MAX_UTSNAME, req);
96 if (error || req->newptr == NULL)
98 error = linux_set_osrelease(req->td, osrelease);
102 SYSCTL_PROC(_compat_linux, OID_AUTO, osrelease,
103 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON,
104 0, 0, linux_sysctl_osrelease, "A",
105 "Linux kernel OS release");
107 static int linux_oss_version = 0x030600;
110 linux_sysctl_oss_version(SYSCTL_HANDLER_ARGS)
115 oss_version = linux_get_oss_version(req->td);
116 error = sysctl_handle_int(oidp, &oss_version, 0, req);
117 if (error || req->newptr == NULL)
119 error = linux_set_oss_version(req->td, oss_version);
123 SYSCTL_PROC(_compat_linux, OID_AUTO, oss_version,
124 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON,
125 0, 0, linux_sysctl_oss_version, "I",
126 "Linux OSS version");
129 * Returns holding the prison mutex if return non-NULL.
131 static struct prison *
132 linux_get_prison(struct thread *td)
134 register struct prison *pr;
135 register struct linux_prison *lpr;
137 KASSERT(td == curthread, ("linux_get_prison() called on !curthread"));
138 if (!jailed(td->td_ucred))
140 pr = td->td_ucred->cr_prison;
141 mtx_lock(&pr->pr_mtx);
142 if (pr->pr_linux == NULL) {
144 * If we don't have a linux prison structure yet, allocate
145 * one. We have to handle the race where another thread
146 * could be adding a linux prison to this process already.
148 mtx_unlock(&pr->pr_mtx);
149 lpr = malloc(sizeof(struct linux_prison), M_PRISON,
151 mtx_lock(&pr->pr_mtx);
152 if (pr->pr_linux == NULL)
161 linux_get_osname(struct thread *td, char *dst)
163 register struct prison *pr;
164 register struct linux_prison *lpr;
166 pr = td->td_ucred->cr_prison;
168 mtx_lock(&pr->pr_mtx);
169 if (pr->pr_linux != NULL) {
170 lpr = (struct linux_prison *)pr->pr_linux;
171 if (lpr->pr_osname[0]) {
172 bcopy(lpr->pr_osname, dst, LINUX_MAX_UTSNAME);
173 mtx_unlock(&pr->pr_mtx);
177 mtx_unlock(&pr->pr_mtx);
180 mtx_lock(&osname_lock);
181 bcopy(linux_osname, dst, LINUX_MAX_UTSNAME);
182 mtx_unlock(&osname_lock);
186 linux_set_osname(struct thread *td, char *osname)
189 struct linux_prison *lpr;
191 pr = linux_get_prison(td);
193 lpr = (struct linux_prison *)pr->pr_linux;
194 strcpy(lpr->pr_osname, osname);
195 mtx_unlock(&pr->pr_mtx);
197 mtx_lock(&osname_lock);
198 strcpy(linux_osname, osname);
199 mtx_unlock(&osname_lock);
206 linux_get_osrelease(struct thread *td, char *dst)
208 register struct prison *pr;
209 struct linux_prison *lpr;
211 pr = td->td_ucred->cr_prison;
213 mtx_lock(&pr->pr_mtx);
214 if (pr->pr_linux != NULL) {
215 lpr = (struct linux_prison *)pr->pr_linux;
216 if (lpr->pr_osrelease[0]) {
217 bcopy(lpr->pr_osrelease, dst,
219 mtx_unlock(&pr->pr_mtx);
223 mtx_unlock(&pr->pr_mtx);
226 mtx_lock(&osname_lock);
227 bcopy(linux_osrelease, dst, LINUX_MAX_UTSNAME);
228 mtx_unlock(&osname_lock);
232 linux_use26(struct thread *td)
235 struct linux_prison *lpr;
236 int use26 = linux_use_linux26;
238 pr = td->td_ucred->cr_prison;
240 if (pr->pr_linux != NULL) {
241 lpr = (struct linux_prison *)pr->pr_linux;
242 use26 = lpr->pr_use_linux26;
250 linux_set_osrelease(struct thread *td, char *osrelease)
253 struct linux_prison *lpr;
256 use26 = (strlen(osrelease) >= 3 && osrelease[2] == '6');
258 pr = linux_get_prison(td);
260 lpr = (struct linux_prison *)pr->pr_linux;
261 strcpy(lpr->pr_osrelease, osrelease);
262 lpr->pr_use_linux26 = use26;
263 mtx_unlock(&pr->pr_mtx);
265 mtx_lock(&osname_lock);
266 strcpy(linux_osrelease, osrelease);
267 linux_use_linux26 = use26;
268 mtx_unlock(&osname_lock);
275 linux_get_oss_version(struct thread *td)
277 register struct prison *pr;
278 register struct linux_prison *lpr;
281 pr = td->td_ucred->cr_prison;
283 mtx_lock(&pr->pr_mtx);
284 if (pr->pr_linux != NULL) {
285 lpr = (struct linux_prison *)pr->pr_linux;
286 if (lpr->pr_oss_version) {
287 version = lpr->pr_oss_version;
288 mtx_unlock(&pr->pr_mtx);
292 mtx_unlock(&pr->pr_mtx);
295 mtx_lock(&osname_lock);
296 version = linux_oss_version;
297 mtx_unlock(&osname_lock);
302 linux_set_oss_version(struct thread *td, int oss_version)
305 struct linux_prison *lpr;
307 pr = linux_get_prison(td);
309 lpr = (struct linux_prison *)pr->pr_linux;
310 lpr->pr_oss_version = oss_version;
311 mtx_unlock(&pr->pr_mtx);
313 mtx_lock(&osname_lock);
314 linux_oss_version = oss_version;
315 mtx_unlock(&osname_lock);
323 u_char linux_debug_map[howmany(LINUX_SYS_MAXSYSCALL, sizeof(u_char))];
326 linux_debug(int syscall, int toggle, int global)
330 char c = toggle ? 0 : 0xff;
332 memset(linux_debug_map, c, sizeof(linux_debug_map));
335 if (syscall < 0 || syscall >= LINUX_SYS_MAXSYSCALL)
338 clrbit(linux_debug_map, syscall);
340 setbit(linux_debug_map, syscall);
345 * Usage: sysctl linux.debug=<syscall_nr>.<0/1>
347 * E.g.: sysctl linux.debug=21.0
349 * As a special case, syscall "all" will apply to all syscalls globally.
351 #define LINUX_MAX_DEBUGSTR 16
353 linux_sysctl_debug(SYSCTL_HANDLER_ARGS)
355 char value[LINUX_MAX_DEBUGSTR], *p;
356 int error, sysc, toggle;
360 error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req);
361 if (error || req->newptr == NULL)
363 for (p = value; *p != '\0' && *p != '.'; p++);
367 sysc = strtol(value, NULL, 0);
368 toggle = strtol(p, NULL, 0);
369 if (strcmp(value, "all") == 0)
371 error = linux_debug(sysc, toggle, global);
375 SYSCTL_PROC(_compat_linux, OID_AUTO, debug,
376 CTLTYPE_STRING | CTLFLAG_RW,
377 0, 0, linux_sysctl_debug, "A",
378 "Linux debugging control");