1 /*- 2 * Copyright (c) 1999 Marcel Moolenaar 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 16 * 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. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/kernel.h> 34 #include <sys/systm.h> 35 #include <sys/sysctl.h> 36 #include <sys/proc.h> 37 #include <sys/malloc.h> 38 #include <sys/jail.h> 39 #include <sys/lock.h> 40 #include <sys/mutex.h> 41 42 #include "opt_compat.h" 43 44 #ifdef COMPAT_LINUX32 45 #include <machine/../linux32/linux.h> 46 #else 47 #include <machine/../linux/linux.h> 48 #endif 49 #include <compat/linux/linux_mib.h> 50 51 struct linux_prison { 52 char pr_osname[LINUX_MAX_UTSNAME]; 53 char pr_osrelease[LINUX_MAX_UTSNAME]; 54 int pr_oss_version; 55 }; 56 57 SYSCTL_NODE(_compat, OID_AUTO, linux, CTLFLAG_RW, 0, 58 "Linux mode"); 59 60 static struct mtx osname_lock; 61 MTX_SYSINIT(linux_osname, &osname_lock, "linux osname", MTX_DEF); 62 63 static char linux_osname[LINUX_MAX_UTSNAME] = "Linux"; 64 65 static int 66 linux_sysctl_osname(SYSCTL_HANDLER_ARGS) 67 { 68 char osname[LINUX_MAX_UTSNAME]; 69 int error; 70 71 linux_get_osname(req->td, osname); 72 error = sysctl_handle_string(oidp, osname, LINUX_MAX_UTSNAME, req); 73 if (error || req->newptr == NULL) 74 return (error); 75 error = linux_set_osname(req->td, osname); 76 return (error); 77 } 78 79 SYSCTL_PROC(_compat_linux, OID_AUTO, osname, 80 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON, 81 0, 0, linux_sysctl_osname, "A", 82 "Linux kernel OS name"); 83 84 static char linux_osrelease[LINUX_MAX_UTSNAME] = "2.4.2"; 85 86 static int 87 linux_sysctl_osrelease(SYSCTL_HANDLER_ARGS) 88 { 89 char osrelease[LINUX_MAX_UTSNAME]; 90 int error; 91 92 linux_get_osrelease(req->td, osrelease); 93 error = sysctl_handle_string(oidp, osrelease, LINUX_MAX_UTSNAME, req); 94 if (error || req->newptr == NULL) 95 return (error); 96 error = linux_set_osrelease(req->td, osrelease); 97 return (error); 98 } 99 100 SYSCTL_PROC(_compat_linux, OID_AUTO, osrelease, 101 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON, 102 0, 0, linux_sysctl_osrelease, "A", 103 "Linux kernel OS release"); 104 105 static int linux_oss_version = 0x030600; 106 107 static int 108 linux_sysctl_oss_version(SYSCTL_HANDLER_ARGS) 109 { 110 int oss_version; 111 int error; 112 113 oss_version = linux_get_oss_version(req->td); 114 error = sysctl_handle_int(oidp, &oss_version, 0, req); 115 if (error || req->newptr == NULL) 116 return (error); 117 error = linux_set_oss_version(req->td, oss_version); 118 return (error); 119 } 120 121 SYSCTL_PROC(_compat_linux, OID_AUTO, oss_version, 122 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON, 123 0, 0, linux_sysctl_oss_version, "I", 124 "Linux OSS version"); 125 126 /* 127 * Returns holding the prison mutex if return non-NULL. 128 */ 129 static struct prison * 130 linux_get_prison(struct thread *td) 131 { 132 register struct prison *pr; 133 register struct linux_prison *lpr; 134 135 KASSERT(td == curthread, ("linux_get_prison() called on !curthread")); 136 if (!jailed(td->td_ucred)) 137 return (NULL); 138 pr = td->td_ucred->cr_prison; 139 mtx_lock(&pr->pr_mtx); 140 if (pr->pr_linux == NULL) { 141 /* 142 * If we don't have a linux prison structure yet, allocate 143 * one. We have to handle the race where another thread 144 * could be adding a linux prison to this process already. 145 */ 146 mtx_unlock(&pr->pr_mtx); 147 lpr = malloc(sizeof(struct linux_prison), M_PRISON, 148 M_WAITOK | M_ZERO); 149 mtx_lock(&pr->pr_mtx); 150 if (pr->pr_linux == NULL) 151 pr->pr_linux = lpr; 152 else 153 free(lpr, M_PRISON); 154 } 155 return (pr); 156 } 157 158 void 159 linux_get_osname(struct thread *td, char *dst) 160 { 161 register struct prison *pr; 162 register struct linux_prison *lpr; 163 164 pr = td->td_ucred->cr_prison; 165 if (pr != NULL) { 166 mtx_lock(&pr->pr_mtx); 167 if (pr->pr_linux != NULL) { 168 lpr = (struct linux_prison *)pr->pr_linux; 169 if (lpr->pr_osname[0]) { 170 bcopy(lpr->pr_osname, dst, LINUX_MAX_UTSNAME); 171 mtx_unlock(&pr->pr_mtx); 172 return; 173 } 174 } 175 mtx_unlock(&pr->pr_mtx); 176 } 177 178 mtx_lock(&osname_lock); 179 bcopy(linux_osname, dst, LINUX_MAX_UTSNAME); 180 mtx_unlock(&osname_lock); 181 } 182 183 int 184 linux_set_osname(struct thread *td, char *osname) 185 { 186 struct prison *pr; 187 struct linux_prison *lpr; 188 189 pr = linux_get_prison(td); 190 if (pr != NULL) { 191 lpr = (struct linux_prison *)pr->pr_linux; 192 strcpy(lpr->pr_osname, osname); 193 mtx_unlock(&pr->pr_mtx); 194 } else { 195 mtx_lock(&osname_lock); 196 strcpy(linux_osname, osname); 197 mtx_unlock(&osname_lock); 198 } 199 200 return (0); 201 } 202 203 void 204 linux_get_osrelease(struct thread *td, char *dst) 205 { 206 register struct prison *pr; 207 struct linux_prison *lpr; 208 209 pr = td->td_ucred->cr_prison; 210 if (pr != NULL) { 211 mtx_lock(&pr->pr_mtx); 212 if (pr->pr_linux != NULL) { 213 lpr = (struct linux_prison *)pr->pr_linux; 214 if (lpr->pr_osrelease[0]) { 215 bcopy(lpr->pr_osrelease, dst, 216 LINUX_MAX_UTSNAME); 217 mtx_unlock(&pr->pr_mtx); 218 return; 219 } 220 } 221 mtx_unlock(&pr->pr_mtx); 222 } 223 224 mtx_lock(&osname_lock); 225 bcopy(linux_osrelease, dst, LINUX_MAX_UTSNAME); 226 mtx_unlock(&osname_lock); 227 } 228 229 int 230 linux_set_osrelease(struct thread *td, char *osrelease) 231 { 232 struct prison *pr; 233 struct linux_prison *lpr; 234 235 pr = linux_get_prison(td); 236 if (pr != NULL) { 237 lpr = (struct linux_prison *)pr->pr_linux; 238 strcpy(lpr->pr_osrelease, osrelease); 239 mtx_unlock(&pr->pr_mtx); 240 } else { 241 mtx_lock(&osname_lock); 242 strcpy(linux_osrelease, osrelease); 243 mtx_unlock(&osname_lock); 244 } 245 246 return (0); 247 } 248 249 int 250 linux_get_oss_version(struct thread *td) 251 { 252 register struct prison *pr; 253 register struct linux_prison *lpr; 254 int version; 255 256 pr = td->td_ucred->cr_prison; 257 if (pr != NULL) { 258 mtx_lock(&pr->pr_mtx); 259 if (pr->pr_linux != NULL) { 260 lpr = (struct linux_prison *)pr->pr_linux; 261 if (lpr->pr_oss_version) { 262 version = lpr->pr_oss_version; 263 mtx_unlock(&pr->pr_mtx); 264 return (version); 265 } 266 } 267 mtx_unlock(&pr->pr_mtx); 268 } 269 270 mtx_lock(&osname_lock); 271 version = linux_oss_version; 272 mtx_unlock(&osname_lock); 273 return (version); 274 } 275 276 int 277 linux_set_oss_version(struct thread *td, int oss_version) 278 { 279 struct prison *pr; 280 struct linux_prison *lpr; 281 282 pr = linux_get_prison(td); 283 if (pr != NULL) { 284 lpr = (struct linux_prison *)pr->pr_linux; 285 lpr->pr_oss_version = oss_version; 286 mtx_unlock(&pr->pr_mtx); 287 } else { 288 mtx_lock(&osname_lock); 289 linux_oss_version = oss_version; 290 mtx_unlock(&osname_lock); 291 } 292 293 return (0); 294 } 295 296 #ifdef DEBUG 297 298 u_char linux_debug_map[howmany(LINUX_SYS_MAXSYSCALL, sizeof(u_char))]; 299 300 static int 301 linux_debug(int syscall, int toggle, int global) 302 { 303 304 if (global) { 305 char c = toggle ? 0 : 0xff; 306 307 memset(linux_debug_map, c, sizeof(linux_debug_map)); 308 return (0); 309 } 310 if (syscall < 0 || syscall >= LINUX_SYS_MAXSYSCALL) 311 return (EINVAL); 312 if (toggle) 313 clrbit(linux_debug_map, syscall); 314 else 315 setbit(linux_debug_map, syscall); 316 return (0); 317 } 318 319 /* 320 * Usage: sysctl linux.debug=<syscall_nr>.<0/1> 321 * 322 * E.g.: sysctl linux.debug=21.0 323 * 324 * As a special case, syscall "all" will apply to all syscalls globally. 325 */ 326 #define LINUX_MAX_DEBUGSTR 16 327 static int 328 linux_sysctl_debug(SYSCTL_HANDLER_ARGS) 329 { 330 char value[LINUX_MAX_DEBUGSTR], *p; 331 int error, sysc, toggle; 332 int global = 0; 333 334 value[0] = '\0'; 335 error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req); 336 if (error || req->newptr == NULL) 337 return (error); 338 for (p = value; *p != '\0' && *p != '.'; p++); 339 if (*p == '\0') 340 return (EINVAL); 341 *p++ = '\0'; 342 sysc = strtol(value, NULL, 0); 343 toggle = strtol(p, NULL, 0); 344 if (strcmp(value, "all") == 0) 345 global = 1; 346 error = linux_debug(sysc, toggle, global); 347 return (error); 348 } 349 350 SYSCTL_PROC(_compat_linux, OID_AUTO, debug, 351 CTLTYPE_STRING | CTLFLAG_RW, 352 0, 0, linux_sysctl_debug, "A", 353 "Linux debugging control"); 354 355 #endif /* DEBUG */ 356