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 int pr_use_linux26; /* flag to determine whether to use 2.6 emulation */ 56 }; 57 58 SYSCTL_NODE(_compat, OID_AUTO, linux, CTLFLAG_RW, 0, 59 "Linux mode"); 60 61 static struct mtx osname_lock; 62 MTX_SYSINIT(linux_osname, &osname_lock, "linux osname", MTX_DEF); 63 64 static char linux_osname[LINUX_MAX_UTSNAME] = "Linux"; 65 66 static int 67 linux_sysctl_osname(SYSCTL_HANDLER_ARGS) 68 { 69 char osname[LINUX_MAX_UTSNAME]; 70 int error; 71 72 linux_get_osname(req->td, osname); 73 error = sysctl_handle_string(oidp, osname, LINUX_MAX_UTSNAME, req); 74 if (error || req->newptr == NULL) 75 return (error); 76 error = linux_set_osname(req->td, osname); 77 return (error); 78 } 79 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"); 84 85 static char linux_osrelease[LINUX_MAX_UTSNAME] = "2.4.2"; 86 static int linux_use_linux26 = 0; 87 88 static int 89 linux_sysctl_osrelease(SYSCTL_HANDLER_ARGS) 90 { 91 char osrelease[LINUX_MAX_UTSNAME]; 92 int error; 93 94 linux_get_osrelease(req->td, osrelease); 95 error = sysctl_handle_string(oidp, osrelease, LINUX_MAX_UTSNAME, req); 96 if (error || req->newptr == NULL) 97 return (error); 98 error = linux_set_osrelease(req->td, osrelease); 99 return (error); 100 } 101 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"); 106 107 static int linux_oss_version = 0x030600; 108 109 static int 110 linux_sysctl_oss_version(SYSCTL_HANDLER_ARGS) 111 { 112 int oss_version; 113 int error; 114 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) 118 return (error); 119 error = linux_set_oss_version(req->td, oss_version); 120 return (error); 121 } 122 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"); 127 128 /* 129 * Returns holding the prison mutex if return non-NULL. 130 */ 131 static struct prison * 132 linux_get_prison(struct thread *td) 133 { 134 register struct prison *pr; 135 register struct linux_prison *lpr; 136 137 KASSERT(td == curthread, ("linux_get_prison() called on !curthread")); 138 if (!jailed(td->td_ucred)) 139 return (NULL); 140 pr = td->td_ucred->cr_prison; 141 mtx_lock(&pr->pr_mtx); 142 if (pr->pr_linux == NULL) { 143 /* 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. 147 */ 148 mtx_unlock(&pr->pr_mtx); 149 lpr = malloc(sizeof(struct linux_prison), M_PRISON, 150 M_WAITOK | M_ZERO); 151 mtx_lock(&pr->pr_mtx); 152 if (pr->pr_linux == NULL) 153 pr->pr_linux = lpr; 154 else 155 free(lpr, M_PRISON); 156 } 157 return (pr); 158 } 159 160 void 161 linux_get_osname(struct thread *td, char *dst) 162 { 163 register struct prison *pr; 164 register struct linux_prison *lpr; 165 166 pr = td->td_ucred->cr_prison; 167 if (pr != NULL) { 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); 174 return; 175 } 176 } 177 mtx_unlock(&pr->pr_mtx); 178 } 179 180 mtx_lock(&osname_lock); 181 bcopy(linux_osname, dst, LINUX_MAX_UTSNAME); 182 mtx_unlock(&osname_lock); 183 } 184 185 int 186 linux_set_osname(struct thread *td, char *osname) 187 { 188 struct prison *pr; 189 struct linux_prison *lpr; 190 191 pr = linux_get_prison(td); 192 if (pr != NULL) { 193 lpr = (struct linux_prison *)pr->pr_linux; 194 strcpy(lpr->pr_osname, osname); 195 mtx_unlock(&pr->pr_mtx); 196 } else { 197 mtx_lock(&osname_lock); 198 strcpy(linux_osname, osname); 199 mtx_unlock(&osname_lock); 200 } 201 202 return (0); 203 } 204 205 void 206 linux_get_osrelease(struct thread *td, char *dst) 207 { 208 register struct prison *pr; 209 struct linux_prison *lpr; 210 211 pr = td->td_ucred->cr_prison; 212 if (pr != NULL) { 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, 218 LINUX_MAX_UTSNAME); 219 mtx_unlock(&pr->pr_mtx); 220 return; 221 } 222 } 223 mtx_unlock(&pr->pr_mtx); 224 } 225 226 mtx_lock(&osname_lock); 227 bcopy(linux_osrelease, dst, LINUX_MAX_UTSNAME); 228 mtx_unlock(&osname_lock); 229 } 230 231 int 232 linux_use26(struct thread *td) 233 { 234 struct prison *pr; 235 struct linux_prison *lpr; 236 int use26 = 0; /* defaults to off */ 237 238 pr = td->td_ucred->cr_prison; 239 if (pr != NULL) { 240 mtx_lock(&pr->pr_mtx); 241 if (pr->pr_linux != NULL) { 242 lpr = (struct linux_prison *)pr->pr_linux; 243 use26 = lpr->pr_use_linux26; 244 } 245 mtx_unlock(&pr->pr_mtx); 246 } else 247 use26 = linux_use_linux26; 248 249 return (use26); 250 } 251 252 int 253 linux_set_osrelease(struct thread *td, char *osrelease) 254 { 255 struct prison *pr; 256 struct linux_prison *lpr; 257 int use26; 258 259 use26 = (strlen(osrelease) >= 3 && osrelease[2] == '6'); 260 261 pr = linux_get_prison(td); 262 if (pr != NULL) { 263 lpr = (struct linux_prison *)pr->pr_linux; 264 strcpy(lpr->pr_osrelease, osrelease); 265 lpr->pr_use_linux26 = use26; 266 mtx_unlock(&pr->pr_mtx); 267 } else { 268 mtx_lock(&osname_lock); 269 strcpy(linux_osrelease, osrelease); 270 linux_use_linux26 = use26; 271 mtx_unlock(&osname_lock); 272 } 273 274 return (0); 275 } 276 277 int 278 linux_get_oss_version(struct thread *td) 279 { 280 register struct prison *pr; 281 register struct linux_prison *lpr; 282 int version; 283 284 pr = td->td_ucred->cr_prison; 285 if (pr != NULL) { 286 mtx_lock(&pr->pr_mtx); 287 if (pr->pr_linux != NULL) { 288 lpr = (struct linux_prison *)pr->pr_linux; 289 if (lpr->pr_oss_version) { 290 version = lpr->pr_oss_version; 291 mtx_unlock(&pr->pr_mtx); 292 return (version); 293 } 294 } 295 mtx_unlock(&pr->pr_mtx); 296 } 297 298 mtx_lock(&osname_lock); 299 version = linux_oss_version; 300 mtx_unlock(&osname_lock); 301 return (version); 302 } 303 304 int 305 linux_set_oss_version(struct thread *td, int oss_version) 306 { 307 struct prison *pr; 308 struct linux_prison *lpr; 309 310 pr = linux_get_prison(td); 311 if (pr != NULL) { 312 lpr = (struct linux_prison *)pr->pr_linux; 313 lpr->pr_oss_version = oss_version; 314 mtx_unlock(&pr->pr_mtx); 315 } else { 316 mtx_lock(&osname_lock); 317 linux_oss_version = oss_version; 318 mtx_unlock(&osname_lock); 319 } 320 321 return (0); 322 } 323 324 #ifdef DEBUG 325 326 u_char linux_debug_map[howmany(LINUX_SYS_MAXSYSCALL, sizeof(u_char))]; 327 328 static int 329 linux_debug(int syscall, int toggle, int global) 330 { 331 332 if (global) { 333 char c = toggle ? 0 : 0xff; 334 335 memset(linux_debug_map, c, sizeof(linux_debug_map)); 336 return (0); 337 } 338 if (syscall < 0 || syscall >= LINUX_SYS_MAXSYSCALL) 339 return (EINVAL); 340 if (toggle) 341 clrbit(linux_debug_map, syscall); 342 else 343 setbit(linux_debug_map, syscall); 344 return (0); 345 } 346 347 /* 348 * Usage: sysctl linux.debug=<syscall_nr>.<0/1> 349 * 350 * E.g.: sysctl linux.debug=21.0 351 * 352 * As a special case, syscall "all" will apply to all syscalls globally. 353 */ 354 #define LINUX_MAX_DEBUGSTR 16 355 static int 356 linux_sysctl_debug(SYSCTL_HANDLER_ARGS) 357 { 358 char value[LINUX_MAX_DEBUGSTR], *p; 359 int error, sysc, toggle; 360 int global = 0; 361 362 value[0] = '\0'; 363 error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req); 364 if (error || req->newptr == NULL) 365 return (error); 366 for (p = value; *p != '\0' && *p != '.'; p++); 367 if (*p == '\0') 368 return (EINVAL); 369 *p++ = '\0'; 370 sysc = strtol(value, NULL, 0); 371 toggle = strtol(p, NULL, 0); 372 if (strcmp(value, "all") == 0) 373 global = 1; 374 error = linux_debug(sysc, toggle, global); 375 return (error); 376 } 377 378 SYSCTL_PROC(_compat_linux, OID_AUTO, debug, 379 CTLTYPE_STRING | CTLFLAG_RW, 380 0, 0, linux_sysctl_debug, "A", 381 "Linux debugging control"); 382 383 #endif /* DEBUG */ 384