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 = linux_use_linux26; 237 238 pr = td->td_ucred->cr_prison; 239 if (pr != NULL) { 240 if (pr->pr_linux != NULL) { 241 lpr = (struct linux_prison *)pr->pr_linux; 242 use26 = lpr->pr_use_linux26; 243 } 244 } 245 246 return (use26); 247 } 248 249 int 250 linux_set_osrelease(struct thread *td, char *osrelease) 251 { 252 struct prison *pr; 253 struct linux_prison *lpr; 254 int use26; 255 256 use26 = (strlen(osrelease) >= 3 && osrelease[2] == '6'); 257 258 pr = linux_get_prison(td); 259 if (pr != NULL) { 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); 264 } else { 265 mtx_lock(&osname_lock); 266 strcpy(linux_osrelease, osrelease); 267 linux_use_linux26 = use26; 268 mtx_unlock(&osname_lock); 269 } 270 271 return (0); 272 } 273 274 int 275 linux_get_oss_version(struct thread *td) 276 { 277 register struct prison *pr; 278 register struct linux_prison *lpr; 279 int version; 280 281 pr = td->td_ucred->cr_prison; 282 if (pr != NULL) { 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); 289 return (version); 290 } 291 } 292 mtx_unlock(&pr->pr_mtx); 293 } 294 295 mtx_lock(&osname_lock); 296 version = linux_oss_version; 297 mtx_unlock(&osname_lock); 298 return (version); 299 } 300 301 int 302 linux_set_oss_version(struct thread *td, int oss_version) 303 { 304 struct prison *pr; 305 struct linux_prison *lpr; 306 307 pr = linux_get_prison(td); 308 if (pr != NULL) { 309 lpr = (struct linux_prison *)pr->pr_linux; 310 lpr->pr_oss_version = oss_version; 311 mtx_unlock(&pr->pr_mtx); 312 } else { 313 mtx_lock(&osname_lock); 314 linux_oss_version = oss_version; 315 mtx_unlock(&osname_lock); 316 } 317 318 return (0); 319 } 320 321 #ifdef DEBUG 322 323 u_char linux_debug_map[howmany(LINUX_SYS_MAXSYSCALL, sizeof(u_char))]; 324 325 static int 326 linux_debug(int syscall, int toggle, int global) 327 { 328 329 if (global) { 330 char c = toggle ? 0 : 0xff; 331 332 memset(linux_debug_map, c, sizeof(linux_debug_map)); 333 return (0); 334 } 335 if (syscall < 0 || syscall >= LINUX_SYS_MAXSYSCALL) 336 return (EINVAL); 337 if (toggle) 338 clrbit(linux_debug_map, syscall); 339 else 340 setbit(linux_debug_map, syscall); 341 return (0); 342 } 343 344 /* 345 * Usage: sysctl linux.debug=<syscall_nr>.<0/1> 346 * 347 * E.g.: sysctl linux.debug=21.0 348 * 349 * As a special case, syscall "all" will apply to all syscalls globally. 350 */ 351 #define LINUX_MAX_DEBUGSTR 16 352 static int 353 linux_sysctl_debug(SYSCTL_HANDLER_ARGS) 354 { 355 char value[LINUX_MAX_DEBUGSTR], *p; 356 int error, sysc, toggle; 357 int global = 0; 358 359 value[0] = '\0'; 360 error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req); 361 if (error || req->newptr == NULL) 362 return (error); 363 for (p = value; *p != '\0' && *p != '.'; p++); 364 if (*p == '\0') 365 return (EINVAL); 366 *p++ = '\0'; 367 sysc = strtol(value, NULL, 0); 368 toggle = strtol(p, NULL, 0); 369 if (strcmp(value, "all") == 0) 370 global = 1; 371 error = linux_debug(sysc, toggle, global); 372 return (error); 373 } 374 375 SYSCTL_PROC(_compat_linux, OID_AUTO, debug, 376 CTLTYPE_STRING | CTLFLAG_RW, 377 0, 0, linux_sysctl_debug, "A", 378 "Linux debugging control"); 379 380 #endif /* DEBUG */ 381