1 /* $NetBSD: rump.c,v 1.121 2009/10/09 14:41:36 pooka Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Antti Kantee. All Rights Reserved. 5 * 6 * Development of this software was supported by Google Summer of Code. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __KERNEL_RCSID(0, "$NetBSD: rump.c,v 1.121 2009/10/09 14:41:36 pooka Exp $"); 32 33 #include <sys/param.h> 34 #include <sys/atomic.h> 35 #include <sys/buf.h> 36 #include <sys/callout.h> 37 #include <sys/conf.h> 38 #include <sys/cpu.h> 39 #include <sys/evcnt.h> 40 #include <sys/event.h> 41 #include <sys/filedesc.h> 42 #include <sys/iostat.h> 43 #include <sys/kauth.h> 44 #include <sys/kernel.h> 45 #include <sys/kmem.h> 46 #include <sys/kprintf.h> 47 #include <sys/ksyms.h> 48 #include <sys/msgbuf.h> 49 #include <sys/module.h> 50 #include <sys/once.h> 51 #include <sys/percpu.h> 52 #include <sys/queue.h> 53 #include <sys/reboot.h> 54 #include <sys/resourcevar.h> 55 #include <sys/select.h> 56 #include <sys/sysctl.h> 57 #include <sys/syscall.h> 58 #include <sys/tty.h> 59 #include <sys/uidinfo.h> 60 #include <sys/vmem.h> 61 62 #include <rump/rumpuser.h> 63 64 #include <secmodel/suser/suser.h> 65 66 #include <prop/proplib.h> 67 68 #include "rump_private.h" 69 #include "rump_net_private.h" 70 #include "rump_vfs_private.h" 71 #include "rump_dev_private.h" 72 73 struct proc proc0; 74 struct session rump_session = { 75 .s_count = 1, 76 .s_flags = 0, 77 .s_leader = &proc0, 78 .s_login = "rumphobo", 79 .s_sid = 0, 80 }; 81 struct pgrp rump_pgrp = { 82 .pg_members = LIST_HEAD_INITIALIZER(pg_members), 83 .pg_session = &rump_session, 84 .pg_jobc = 1, 85 }; 86 struct pstats rump_stats; 87 struct plimit rump_limits; 88 struct cpu_info rump_cpu; 89 struct filedesc rump_filedesc0; 90 struct proclist allproc; 91 char machine[] = "rump"; 92 static kauth_cred_t rump_susercred; 93 94 /* pretend the master rump proc is init */ 95 struct proc *initproc = &proc0; 96 97 struct rumpuser_mtx *rump_giantlock; 98 99 sigset_t sigcantmask; 100 101 #ifdef RUMP_WITHOUT_THREADS 102 int rump_threads = 0; 103 #else 104 int rump_threads = 1; 105 #endif 106 107 static void 108 rump_aiodone_worker(struct work *wk, void *dummy) 109 { 110 struct buf *bp = (struct buf *)wk; 111 112 KASSERT(&bp->b_work == wk); 113 bp->b_iodone(bp); 114 } 115 116 static int rump_inited; 117 static struct emul emul_rump; 118 119 void rump__unavailable(void); 120 void rump__unavailable() {} 121 __weak_alias(rump_net_init,rump__unavailable); 122 __weak_alias(rump_vfs_init,rump__unavailable); 123 __weak_alias(rump_dev_init,rump__unavailable); 124 125 __weak_alias(rump_vfs_fini,rump__unavailable); 126 127 __weak_alias(biodone,rump__unavailable); 128 129 void rump__unavailable_vfs_panic(void); 130 void rump__unavailable_vfs_panic() {panic("vfs component not available");} 131 __weak_alias(vn_open,rump__unavailable_vfs_panic); 132 __weak_alias(vn_rdwr,rump__unavailable_vfs_panic); 133 __weak_alias(vn_stat,rump__unavailable_vfs_panic); 134 __weak_alias(vn_close,rump__unavailable_vfs_panic); 135 __weak_alias(namei,rump__unavailable_vfs_panic); 136 137 static void 138 pvfsinit_nop(struct proc *p) 139 { 140 141 return; 142 } 143 144 static void 145 pvfsrele_nop(struct proc *p) 146 { 147 148 return; 149 } 150 151 rump_proc_vfs_init_fn rump_proc_vfs_init = pvfsinit_nop; 152 rump_proc_vfs_release_fn rump_proc_vfs_release = pvfsrele_nop; 153 154 /* 155 * Stir up the stack a bit. These are exported functions to help 156 * convince the compiler that we don't want these routines completely 157 * optimized out or inlined. Is there an easier way to do this? 158 */ 159 void nullfn(uint32_t *); 160 void nullfn(uint32_t *arg){} 161 void messthestack(void); 162 void 163 messthestack(void) 164 { 165 uint32_t mess[64]; 166 uint64_t d1, d2; 167 int i, error; 168 169 for (i = 0; i < 64; i++) { 170 rumpuser_gettime(&d1, &d2, &error); 171 mess[i] = d2; 172 } 173 nullfn(mess); 174 } 175 176 int 177 rump__init(int rump_version) 178 { 179 char buf[256]; 180 struct proc *p; 181 struct lwp *l; 182 int error; 183 184 /* not reentrant */ 185 if (rump_inited) 186 return 0; 187 else if (rump_inited == -1) 188 panic("rump_init: host process restart required"); 189 else 190 rump_inited = 1; 191 192 /* 193 * Seed arc4random() with a "reasonable" amount of randomness. 194 * Yes, this is a quick kludge which depends on the arc4random 195 * implementation. 196 */ 197 messthestack(); 198 arc4random(); 199 200 if (rump_version != RUMP_VERSION) { 201 printf("rump version mismatch, %d vs. %d\n", 202 rump_version, RUMP_VERSION); 203 return EPROGMISMATCH; 204 } 205 206 if (rumpuser_getenv("RUMP_THREADS", buf, sizeof(buf), &error) == 0) { 207 rump_threads = *buf != '0'; 208 } 209 rumpuser_thrinit(_kernel_lock, _kernel_unlock, rump_threads); 210 211 mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_NONE); 212 rumpuser_mutex_recursive_init(&rump_giantlock); 213 ksyms_init(); 214 rumpvm_init(); 215 evcnt_init(); 216 217 once_init(); 218 prop_kern_init(); 219 220 rump_sleepers_init(); 221 222 pool_subsystem_init(); 223 kmem_init(); 224 225 kprintf_init(); 226 loginit(); 227 228 kauth_init(); 229 rump_susercred = rump_cred_create(0, 0, 0, NULL); 230 231 l = &lwp0; 232 p = &proc0; 233 p->p_stats = &rump_stats; 234 p->p_limit = &rump_limits; 235 p->p_pgrp = &rump_pgrp; 236 p->p_pid = 0; 237 p->p_fd = &rump_filedesc0; 238 p->p_vmspace = &rump_vmspace; 239 p->p_emul = &emul_rump; 240 p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); 241 l->l_cred = rump_cred_suserget(); 242 l->l_proc = p; 243 l->l_lid = 1; 244 l->l_cpu = &rump_cpu; 245 LIST_INIT(&allproc); 246 LIST_INSERT_HEAD(&allproc, &proc0, p_list); 247 proc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); 248 249 rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 250 rump_limits.pl_rlimit[RLIMIT_NOFILE].rlim_cur = RLIM_INFINITY; 251 rump_limits.pl_rlimit[RLIMIT_SBSIZE].rlim_cur = RLIM_INFINITY; 252 253 callout_startup(); 254 callout_init_cpu(&rump_cpu); 255 256 sysctl_init(); 257 kqueue_init(); 258 iostat_init(); 259 uid_init(); 260 percpu_init(); 261 fd_sys_init(); 262 module_init(); 263 softint_init(&rump_cpu); 264 devsw_init(); 265 266 /* these do nothing if not present */ 267 rump_vfs_init(); 268 rump_net_init(); 269 rump_dev_init(); 270 cold = 0; 271 272 /* aieeeedondest */ 273 if (rump_threads) { 274 if (workqueue_create(&uvm.aiodone_queue, "aiodoned", 275 rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE)) 276 panic("aiodoned"); 277 } 278 279 sysctl_finalize(); 280 281 rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error); 282 hostnamelen = strlen(hostname); 283 284 sigemptyset(&sigcantmask); 285 286 lwp0.l_fd = proc0.p_fd = fd_init(&rump_filedesc0); 287 288 #ifdef RUMP_USE_REAL_ALLOCATORS 289 if (rump_threads) 290 vmem_rehash_start(); 291 #endif 292 293 rumpuser_dl_module_bootstrap(); 294 295 return 0; 296 } 297 298 /* maybe support sys_reboot some day for remote shutdown */ 299 void 300 rump_reboot(int howto) 301 { 302 303 /* dump means we really take the dive here */ 304 if ((howto & RB_DUMP) || panicstr) { 305 rumpuser_exit(RUMPUSER_PANIC); 306 /*NOTREACHED*/ 307 } 308 309 /* try to sync */ 310 if (!((howto & RB_NOSYNC) || panicstr)) { 311 rump_vfs_fini(); 312 } 313 314 /* your wish is my command */ 315 if (howto & RB_HALT) { 316 for (;;) { 317 uint64_t sec = 5, nsec = 0; 318 int error; 319 320 rumpuser_nanosleep(&sec, &nsec, &error); 321 } 322 } 323 rump_inited = -1; 324 } 325 326 struct uio * 327 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw) 328 { 329 struct uio *uio; 330 enum uio_rw uiorw; 331 332 switch (rw) { 333 case RUMPUIO_READ: 334 uiorw = UIO_READ; 335 break; 336 case RUMPUIO_WRITE: 337 uiorw = UIO_WRITE; 338 break; 339 default: 340 panic("%s: invalid rw %d", __func__, rw); 341 } 342 343 uio = kmem_alloc(sizeof(struct uio), KM_SLEEP); 344 uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP); 345 346 uio->uio_iov->iov_base = buf; 347 uio->uio_iov->iov_len = bufsize; 348 349 uio->uio_iovcnt = 1; 350 uio->uio_offset = offset; 351 uio->uio_resid = bufsize; 352 uio->uio_rw = uiorw; 353 uio->uio_vmspace = UIO_VMSPACE_SYS; 354 355 return uio; 356 } 357 358 size_t 359 rump_uio_getresid(struct uio *uio) 360 { 361 362 return uio->uio_resid; 363 } 364 365 off_t 366 rump_uio_getoff(struct uio *uio) 367 { 368 369 return uio->uio_offset; 370 } 371 372 size_t 373 rump_uio_free(struct uio *uio) 374 { 375 size_t resid; 376 377 resid = uio->uio_resid; 378 kmem_free(uio->uio_iov, sizeof(*uio->uio_iov)); 379 kmem_free(uio, sizeof(*uio)); 380 381 return resid; 382 } 383 384 /* public interface */ 385 static pid_t nextpid = 1; 386 struct lwp * 387 rump_newproc_switch() 388 { 389 struct lwp *oldlwp = curlwp; 390 pid_t mypid; 391 392 mypid = atomic_inc_uint_nv(&nextpid); 393 if (__predict_false(mypid == 0)) 394 mypid = atomic_inc_uint_nv(&nextpid); 395 396 rumpuser_set_curlwp(NULL); 397 rump_setup_curlwp(mypid, 0, 1); 398 399 return oldlwp; 400 } 401 402 /* rump private */ 403 struct lwp * 404 rump_setup_curlwp(pid_t pid, lwpid_t lid, int set) 405 { 406 struct lwp *l; 407 struct proc *p; 408 409 l = kmem_zalloc(sizeof(struct lwp), KM_SLEEP); 410 if (pid != 0) { 411 p = kmem_zalloc(sizeof(struct proc), KM_SLEEP); 412 rump_proc_vfs_init(p); 413 p->p_stats = &rump_stats; 414 p->p_limit = &rump_limits; 415 p->p_pid = pid; 416 p->p_vmspace = &rump_vmspace; 417 p->p_fd = fd_init(NULL); 418 p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); 419 } else { 420 p = &proc0; 421 } 422 423 l->l_cred = rump_cred_suserget(); 424 l->l_proc = p; 425 l->l_lid = lid; 426 l->l_fd = p->p_fd; 427 l->l_mutex = RUMP_LMUTEX_MAGIC; 428 l->l_cpu = &rump_cpu; 429 430 if (set) 431 rumpuser_set_curlwp(l); 432 433 return l; 434 } 435 436 /* rump private. NEEDS WORK! */ 437 void 438 rump_set_vmspace(struct vmspace *vm) 439 { 440 struct proc *p = curproc; 441 442 p->p_vmspace = vm; 443 } 444 445 void 446 rump_clear_curlwp(void) 447 { 448 struct lwp *l; 449 struct proc *p; 450 451 l = rumpuser_get_curlwp(); 452 p = l->l_proc; 453 if (p->p_pid != 0) { 454 mutex_obj_free(p->p_lock); 455 fd_free(); 456 rump_proc_vfs_release(p); 457 rump_cred_put(l->l_cred); 458 kmem_free(p, sizeof(*p)); 459 } 460 kmem_free(l, sizeof(*l)); 461 rumpuser_set_curlwp(NULL); 462 } 463 464 struct lwp * 465 rump_get_curlwp(void) 466 { 467 struct lwp *l; 468 469 l = rumpuser_get_curlwp(); 470 if (l == NULL) 471 l = &lwp0; 472 473 return l; 474 } 475 476 void 477 rump_set_curlwp(struct lwp *l) 478 { 479 480 /* clear current */ 481 rumpuser_set_curlwp(NULL); 482 /* set new */ 483 rumpuser_set_curlwp(l); 484 } 485 486 kauth_cred_t 487 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups) 488 { 489 kauth_cred_t cred; 490 int rv; 491 492 cred = kauth_cred_alloc(); 493 kauth_cred_setuid(cred, uid); 494 kauth_cred_seteuid(cred, uid); 495 kauth_cred_setsvuid(cred, uid); 496 kauth_cred_setgid(cred, gid); 497 kauth_cred_setgid(cred, gid); 498 kauth_cred_setegid(cred, gid); 499 kauth_cred_setsvgid(cred, gid); 500 rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE); 501 /* oh this is silly. and by "this" I mean kauth_cred_setgroups() */ 502 assert(rv == 0); 503 504 return cred; 505 } 506 507 void 508 rump_cred_put(kauth_cred_t cred) 509 { 510 511 kauth_cred_free(cred); 512 } 513 514 kauth_cred_t 515 rump_cred_suserget(void) 516 { 517 518 kauth_cred_hold(rump_susercred); 519 return rump_susercred; 520 } 521 522 /* 523 * Return the next system lwpid 524 */ 525 lwpid_t 526 rump_nextlid(void) 527 { 528 lwpid_t retid; 529 530 mutex_enter(proc0.p_lock); 531 /* 532 * Take next one, don't return 0 533 * XXX: most likely we'll have collisions in case this 534 * wraps around. 535 */ 536 if (++proc0.p_nlwpid == 0) 537 ++proc0.p_nlwpid; 538 retid = proc0.p_nlwpid; 539 mutex_exit(proc0.p_lock); 540 541 return retid; 542 } 543 544 int 545 rump_module_init(struct modinfo *mi, prop_dictionary_t props) 546 { 547 int rv; 548 549 if (!module_compatible(mi->mi_version, __NetBSD_Version__)) 550 return EPROGMISMATCH; 551 552 rv = mi->mi_modcmd(MODULE_CMD_INIT, props); 553 if (rv == 0 && mi->mi_class == MODULE_CLASS_SECMODEL) 554 secmodel_register(); 555 556 return rv; 557 } 558 559 int 560 rump_module_fini(struct modinfo *mi) 561 { 562 int rv; 563 564 rv = mi->mi_modcmd(MODULE_CMD_FINI, NULL); 565 if (rv == 0 && mi->mi_class == MODULE_CLASS_SECMODEL) 566 secmodel_deregister(); 567 568 return rv; 569 } 570 571 int _syspuffs_stub(int, int *); 572 int 573 _syspuffs_stub(int fd, int *newfd) 574 { 575 576 return ENODEV; 577 } 578 __weak_alias(rump_syspuffs_glueinit,_syspuffs_stub); 579 580 static int 581 rump_sysproxy_local(int num, void *arg, uint8_t *data, size_t dlen, 582 register_t *retval) 583 { 584 struct lwp *l; 585 struct sysent *callp; 586 587 if (__predict_false(num >= SYS_NSYSENT)) 588 return ENOSYS; 589 590 l = curlwp; 591 callp = rump_sysent + num; 592 return callp->sy_call(l, (void *)data, retval); 593 } 594 595 rump_sysproxy_t rump_sysproxy = rump_sysproxy_local; 596 void *rump_sysproxy_arg; 597 598 /* 599 * This whole syscall-via-rpc is still taking form. For example, it 600 * may be necessary to set syscalls individually instead of lobbing 601 * them all to the same place. So don't think this interface is 602 * set in stone. 603 */ 604 int 605 rump_sysproxy_set(rump_sysproxy_t proxy, void *arg) 606 { 607 608 if (rump_sysproxy_arg) 609 return EBUSY; 610 611 rump_sysproxy_arg = arg; 612 rump_sysproxy = proxy; 613 614 return 0; 615 } 616 617 int 618 rump_getversion() 619 { 620 621 return __NetBSD_Version__; 622 } 623