1 /* $NetBSD: rump.c,v 1.155 2010/03/05 18:41:46 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.155 2010/03/05 18:41:46 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/device.h> 40 #include <sys/evcnt.h> 41 #include <sys/event.h> 42 #include <sys/exec_elf.h> 43 #include <sys/filedesc.h> 44 #include <sys/iostat.h> 45 #include <sys/kauth.h> 46 #include <sys/kernel.h> 47 #include <sys/kmem.h> 48 #include <sys/kprintf.h> 49 #include <sys/ksyms.h> 50 #include <sys/msgbuf.h> 51 #include <sys/module.h> 52 #include <sys/once.h> 53 #include <sys/percpu.h> 54 #include <sys/pipe.h> 55 #include <sys/queue.h> 56 #include <sys/reboot.h> 57 #include <sys/resourcevar.h> 58 #include <sys/select.h> 59 #include <sys/sysctl.h> 60 #include <sys/syscall.h> 61 #include <sys/tty.h> 62 #include <sys/uidinfo.h> 63 #include <sys/vmem.h> 64 #include <sys/xcall.h> 65 66 #include <rump/rumpuser.h> 67 68 #include <secmodel/suser/suser.h> 69 70 #include <prop/proplib.h> 71 72 #include <uvm/uvm_readahead.h> 73 74 #include "rump_private.h" 75 #include "rump_net_private.h" 76 #include "rump_vfs_private.h" 77 #include "rump_dev_private.h" 78 79 struct proc proc0; 80 struct session rump_session = { 81 .s_count = 1, 82 .s_flags = 0, 83 .s_leader = &proc0, 84 .s_login = "rumphobo", 85 .s_sid = 0, 86 }; 87 struct pgrp rump_pgrp = { 88 .pg_members = LIST_HEAD_INITIALIZER(pg_members), 89 .pg_session = &rump_session, 90 .pg_jobc = 1, 91 }; 92 struct pstats rump_stats; 93 struct plimit rump_limits; 94 struct filedesc rump_filedesc0; 95 struct proclist allproc; 96 char machine[] = "rump"; 97 static kauth_cred_t rump_susercred; 98 99 /* pretend the master rump proc is init */ 100 struct proc *initproc = &proc0; 101 102 struct rumpuser_mtx *rump_giantlock; 103 104 sigset_t sigcantmask; 105 106 struct device rump_rootdev = { 107 .dv_class = DV_VIRTUAL 108 }; 109 110 #ifdef RUMP_WITHOUT_THREADS 111 int rump_threads = 0; 112 #else 113 int rump_threads = 1; 114 #endif 115 116 static void 117 rump_aiodone_worker(struct work *wk, void *dummy) 118 { 119 struct buf *bp = (struct buf *)wk; 120 121 KASSERT(&bp->b_work == wk); 122 bp->b_iodone(bp); 123 } 124 125 static int rump_inited; 126 static struct emul emul_rump = { 127 .e_vm_default_addr = uvm_default_mapaddr, 128 }; 129 130 int rump__unavailable(void); 131 int rump__unavailable() {return EOPNOTSUPP;} 132 __weak_alias(rump_net_init,rump__unavailable); 133 __weak_alias(rump_vfs_init,rump__unavailable); 134 __weak_alias(rump_dev_init,rump__unavailable); 135 136 __weak_alias(rump_vfs_fini,rump__unavailable); 137 138 __weak_alias(biodone,rump__unavailable); 139 __weak_alias(sopoll,rump__unavailable); 140 141 void rump__unavailable_vfs_panic(void); 142 void rump__unavailable_vfs_panic() {panic("vfs component not available");} 143 __weak_alias(usermount_common_policy,rump__unavailable_vfs_panic); 144 145 rump_proc_vfs_init_fn rump_proc_vfs_init; 146 rump_proc_vfs_release_fn rump_proc_vfs_release; 147 148 static void add_linkedin_modules(const struct modinfo *const *, size_t); 149 150 static void __noinline 151 messthestack(void) 152 { 153 volatile uint32_t mess[64]; 154 uint64_t d1, d2; 155 int i, error; 156 157 for (i = 0; i < 64; i++) { 158 rumpuser_gettime(&d1, &d2, &error); 159 mess[i] = d2; 160 } 161 } 162 163 int 164 rump__init(int rump_version) 165 { 166 char buf[256]; 167 struct proc *p; 168 struct lwp *l; 169 int i; 170 int error; 171 172 /* not reentrant */ 173 if (rump_inited) 174 return 0; 175 else if (rump_inited == -1) 176 panic("rump_init: host process restart required"); 177 else 178 rump_inited = 1; 179 180 if (rumpuser_getenv("RUMP_VERBOSE", buf, sizeof(buf), &error) == 0) { 181 if (*buf != '0') 182 boothowto = AB_VERBOSE; 183 } 184 185 /* Print some silly banners for spammy bootstrap. */ 186 if (boothowto & AB_VERBOSE) { 187 printf("%s%s", copyright, version); 188 } 189 190 /* 191 * Seed arc4random() with a "reasonable" amount of randomness. 192 * Yes, this is a quick kludge which depends on the arc4random 193 * implementation. 194 */ 195 messthestack(); 196 arc4random(); 197 198 if (rump_version != RUMP_VERSION) { 199 printf("rump version mismatch, %d vs. %d\n", 200 rump_version, RUMP_VERSION); 201 return EPROGMISMATCH; 202 } 203 204 if (rumpuser_getenv("RUMP_THREADS", buf, sizeof(buf), &error) == 0) { 205 rump_threads = *buf != '0'; 206 } 207 rumpuser_thrinit(rump_user_schedule, rump_user_unschedule, 208 rump_threads); 209 rump_intr_init(); 210 211 /* init minimal lwp/cpu context */ 212 l = &lwp0; 213 l->l_lid = 1; 214 l->l_cpu = rump_cpu; 215 rumpuser_set_curlwp(l); 216 217 mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_NONE); 218 rumpuser_mutex_recursive_init(&rump_giantlock); 219 ksyms_init(); 220 rumpvm_init(); 221 evcnt_init(); 222 223 once_init(); 224 prop_kern_init(); 225 226 pool_subsystem_init(); 227 kmem_init(); 228 229 uvm_ra_init(); 230 231 mutex_obj_init(); 232 callout_startup(); 233 234 kprintf_init(); 235 loginit(); 236 237 kauth_init(); 238 rump_susercred = rump_cred_create(0, 0, 0, NULL); 239 240 /* init proc0 and rest of lwp0 now that we can allocate memory */ 241 p = &proc0; 242 p->p_stats = &rump_stats; 243 p->p_limit = &rump_limits; 244 p->p_pgrp = &rump_pgrp; 245 p->p_pid = 0; 246 p->p_fd = &rump_filedesc0; 247 p->p_vmspace = &rump_vmspace; 248 p->p_emul = &emul_rump; 249 p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); 250 l->l_cred = rump_cred_suserget(); 251 l->l_proc = p; 252 LIST_INIT(&allproc); 253 LIST_INSERT_HEAD(&allproc, &proc0, p_list); 254 proc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); 255 256 rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 257 rump_limits.pl_rlimit[RLIMIT_NOFILE].rlim_cur = RLIM_INFINITY; 258 rump_limits.pl_rlimit[RLIMIT_SBSIZE].rlim_cur = RLIM_INFINITY; 259 260 rump_scheduler_init(); 261 /* revert temporary context and schedule a real context */ 262 l->l_cpu = NULL; 263 rumpuser_set_curlwp(NULL); 264 rump_schedule(); 265 266 percpu_init(); 267 268 /* we are mostly go. do per-cpu subsystem init */ 269 for (i = 0; i < ncpu; i++) { 270 struct cpu_info *ci = cpu_lookup(i); 271 272 callout_init_cpu(ci); 273 softint_init(ci); 274 xc_init_cpu(ci); 275 pool_cache_cpu_init(ci); 276 selsysinit(ci); 277 percpu_init_cpu(ci); 278 } 279 280 sysctl_init(); 281 kqueue_init(); 282 iostat_init(); 283 uid_init(); 284 fd_sys_init(); 285 module_init(); 286 devsw_init(); 287 pipe_init(); 288 289 rumpuser_dl_bootstrap(add_linkedin_modules, rump_kernelfsym_load); 290 291 /* these do nothing if not present */ 292 rump_vfs_init(); 293 rump_net_init(); 294 rump_dev_init(); 295 cold = 0; 296 297 /* aieeeedondest */ 298 if (rump_threads) { 299 if (workqueue_create(&uvm.aiodone_queue, "aiodoned", 300 rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE)) 301 panic("aiodoned"); 302 } 303 304 sysctl_finalize(); 305 306 module_init_class(MODULE_CLASS_ANY); 307 308 rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error); 309 hostnamelen = strlen(hostname); 310 311 sigemptyset(&sigcantmask); 312 313 lwp0.l_fd = proc0.p_fd = fd_init(&rump_filedesc0); 314 315 if (rump_threads) 316 vmem_rehash_start(); 317 318 rump_unschedule(); 319 320 return 0; 321 } 322 323 /* maybe support sys_reboot some day for remote shutdown */ 324 void 325 rump_reboot(int howto) 326 { 327 328 /* dump means we really take the dive here */ 329 if ((howto & RB_DUMP) || panicstr) { 330 rumpuser_exit(RUMPUSER_PANIC); 331 /*NOTREACHED*/ 332 } 333 334 /* try to sync */ 335 if (!((howto & RB_NOSYNC) || panicstr)) { 336 rump_vfs_fini(); 337 } 338 339 /* your wish is my command */ 340 if (howto & RB_HALT) { 341 for (;;) { 342 uint64_t sec = 5, nsec = 0; 343 int error; 344 345 rumpuser_nanosleep(&sec, &nsec, &error); 346 } 347 } 348 rump_inited = -1; 349 } 350 351 struct uio * 352 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw) 353 { 354 struct uio *uio; 355 enum uio_rw uiorw; 356 357 switch (rw) { 358 case RUMPUIO_READ: 359 uiorw = UIO_READ; 360 break; 361 case RUMPUIO_WRITE: 362 uiorw = UIO_WRITE; 363 break; 364 default: 365 panic("%s: invalid rw %d", __func__, rw); 366 } 367 368 uio = kmem_alloc(sizeof(struct uio), KM_SLEEP); 369 uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP); 370 371 uio->uio_iov->iov_base = buf; 372 uio->uio_iov->iov_len = bufsize; 373 374 uio->uio_iovcnt = 1; 375 uio->uio_offset = offset; 376 uio->uio_resid = bufsize; 377 uio->uio_rw = uiorw; 378 uio->uio_vmspace = UIO_VMSPACE_SYS; 379 380 return uio; 381 } 382 383 size_t 384 rump_uio_getresid(struct uio *uio) 385 { 386 387 return uio->uio_resid; 388 } 389 390 off_t 391 rump_uio_getoff(struct uio *uio) 392 { 393 394 return uio->uio_offset; 395 } 396 397 size_t 398 rump_uio_free(struct uio *uio) 399 { 400 size_t resid; 401 402 resid = uio->uio_resid; 403 kmem_free(uio->uio_iov, sizeof(*uio->uio_iov)); 404 kmem_free(uio, sizeof(*uio)); 405 406 return resid; 407 } 408 409 static pid_t nextpid = 1; 410 struct lwp * 411 rump_newproc_switch() 412 { 413 struct lwp *l; 414 pid_t mypid; 415 416 mypid = atomic_inc_uint_nv(&nextpid); 417 if (__predict_false(mypid == 0)) 418 mypid = atomic_inc_uint_nv(&nextpid); 419 420 l = rump_lwp_alloc(mypid, 0); 421 rump_lwp_switch(l); 422 423 return l; 424 } 425 426 struct lwp * 427 rump_lwp_alloc_and_switch(pid_t pid, lwpid_t lid) 428 { 429 struct lwp *l; 430 431 l = rump_lwp_alloc(pid, lid); 432 rump_lwp_switch(l); 433 434 return l; 435 } 436 437 struct lwp * 438 rump_lwp_alloc(pid_t pid, lwpid_t lid) 439 { 440 struct lwp *l; 441 struct proc *p; 442 443 l = kmem_zalloc(sizeof(*l), KM_SLEEP); 444 if (pid != 0) { 445 p = kmem_zalloc(sizeof(*p), KM_SLEEP); 446 if (rump_proc_vfs_init) 447 rump_proc_vfs_init(p); 448 p->p_stats = &rump_stats; 449 p->p_limit = &rump_limits; 450 p->p_pid = pid; 451 p->p_vmspace = &rump_vmspace; 452 p->p_emul = &emul_rump; 453 p->p_fd = fd_init(NULL); 454 p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); 455 l->l_cred = rump_cred_suserget(); 456 } else { 457 p = &proc0; 458 l->l_cred = rump_susercred; 459 } 460 461 l->l_proc = p; 462 l->l_lid = lid; 463 l->l_fd = p->p_fd; 464 l->l_cpu = NULL; 465 466 return l; 467 } 468 469 void 470 rump_lwp_switch(struct lwp *newlwp) 471 { 472 struct lwp *l = curlwp; 473 474 rumpuser_set_curlwp(NULL); 475 newlwp->l_cpu = l->l_cpu; 476 newlwp->l_mutex = l->l_mutex; 477 l->l_mutex = NULL; 478 l->l_cpu = NULL; 479 rumpuser_set_curlwp(newlwp); 480 if (l->l_flag & LW_WEXIT) 481 rump_lwp_free(l); 482 } 483 484 /* XXX: this has effect only on non-pid0 lwps */ 485 void 486 rump_lwp_release(struct lwp *l) 487 { 488 struct proc *p; 489 490 p = l->l_proc; 491 if (p->p_pid != 0) { 492 mutex_obj_free(p->p_lock); 493 fd_free(); 494 if (rump_proc_vfs_release) 495 rump_proc_vfs_release(p); 496 rump_cred_put(l->l_cred); 497 kmem_free(p, sizeof(*p)); 498 } 499 KASSERT((l->l_flag & LW_WEXIT) == 0); 500 l->l_flag |= LW_WEXIT; 501 } 502 503 void 504 rump_lwp_free(struct lwp *l) 505 { 506 507 KASSERT(l->l_flag & LW_WEXIT); 508 KASSERT(l->l_mutex == NULL); 509 if (l->l_name) 510 kmem_free(l->l_name, MAXCOMLEN); 511 kmem_free(l, sizeof(*l)); 512 } 513 514 struct lwp * 515 rump_lwp_curlwp(void) 516 { 517 struct lwp *l = curlwp; 518 519 if (l->l_flag & LW_WEXIT) 520 return NULL; 521 return l; 522 } 523 524 /* rump private. NEEDS WORK! */ 525 void 526 rump_set_vmspace(struct vmspace *vm) 527 { 528 struct proc *p = curproc; 529 530 p->p_vmspace = vm; 531 } 532 533 kauth_cred_t 534 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups) 535 { 536 kauth_cred_t cred; 537 int rv; 538 539 cred = kauth_cred_alloc(); 540 kauth_cred_setuid(cred, uid); 541 kauth_cred_seteuid(cred, uid); 542 kauth_cred_setsvuid(cred, uid); 543 kauth_cred_setgid(cred, gid); 544 kauth_cred_setgid(cred, gid); 545 kauth_cred_setegid(cred, gid); 546 kauth_cred_setsvgid(cred, gid); 547 rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE); 548 /* oh this is silly. and by "this" I mean kauth_cred_setgroups() */ 549 assert(rv == 0); 550 551 return cred; 552 } 553 554 void 555 rump_cred_put(kauth_cred_t cred) 556 { 557 558 kauth_cred_free(cred); 559 } 560 561 kauth_cred_t 562 rump_cred_suserget(void) 563 { 564 565 kauth_cred_hold(rump_susercred); 566 return rump_susercred; 567 } 568 569 /* 570 * Return the next system lwpid 571 */ 572 lwpid_t 573 rump_nextlid(void) 574 { 575 lwpid_t retid; 576 577 mutex_enter(proc0.p_lock); 578 /* 579 * Take next one, don't return 0 580 * XXX: most likely we'll have collisions in case this 581 * wraps around. 582 */ 583 if (++proc0.p_nlwpid == 0) 584 ++proc0.p_nlwpid; 585 retid = proc0.p_nlwpid; 586 mutex_exit(proc0.p_lock); 587 588 return retid; 589 } 590 591 static int compcounter[RUMP_COMPONENT_MAX]; 592 593 static void 594 rump_component_init_cb(struct rump_component *rc, int type) 595 { 596 597 KASSERT(type < RUMP_COMPONENT_MAX); 598 if (rc->rc_type == type) { 599 rc->rc_init(); 600 compcounter[type]++; 601 } 602 } 603 604 int 605 rump_component_count(enum rump_component_type type) 606 { 607 608 KASSERT(type <= RUMP_COMPONENT_MAX); 609 return compcounter[type]; 610 } 611 612 void 613 rump_component_init(enum rump_component_type type) 614 { 615 616 rumpuser_dl_component_init(type, rump_component_init_cb); 617 } 618 619 /* 620 * Initialize a module which has already been loaded and linked 621 * with dlopen(). This is fundamentally the same as a builtin module. 622 */ 623 int 624 rump_module_init(const struct modinfo * const *mip, size_t nmodinfo) 625 { 626 627 return module_builtin_add(mip, nmodinfo, true); 628 } 629 630 /* 631 * Finish module (flawless victory, fatality!). 632 */ 633 int 634 rump_module_fini(const struct modinfo *mi) 635 { 636 637 return module_builtin_remove(mi, true); 638 } 639 640 /* 641 * Add loaded and linked module to the builtin list. It will 642 * later be initialized with module_init_class(). 643 */ 644 645 static void 646 add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo) 647 { 648 649 module_builtin_add(mip, nmodinfo, false); 650 } 651 652 int 653 rump_kernelfsym_load(void *symtab, uint64_t symsize, 654 char *strtab, uint64_t strsize) 655 { 656 static int inited = 0; 657 Elf64_Ehdr ehdr; 658 659 if (inited) 660 return EBUSY; 661 inited = 1; 662 663 /* 664 * Use 64bit header since it's bigger. Shouldn't make a 665 * difference, since we're passing in all zeroes anyway. 666 */ 667 memset(&ehdr, 0, sizeof(ehdr)); 668 ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize); 669 670 return 0; 671 } 672 673 static int 674 rump_sysproxy_local(int num, void *arg, uint8_t *data, size_t dlen, 675 register_t *retval) 676 { 677 struct lwp *l; 678 struct sysent *callp; 679 int rv; 680 681 if (__predict_false(num >= SYS_NSYSENT)) 682 return ENOSYS; 683 684 callp = rump_sysent + num; 685 rump_schedule(); 686 l = curlwp; 687 rv = callp->sy_call(l, (void *)data, retval); 688 rump_unschedule(); 689 690 return rv; 691 } 692 693 int 694 rump_boot_gethowto() 695 { 696 697 return boothowto; 698 } 699 700 void 701 rump_boot_sethowto(int howto) 702 { 703 704 boothowto = howto; 705 } 706 707 rump_sysproxy_t rump_sysproxy = rump_sysproxy_local; 708 void *rump_sysproxy_arg; 709 710 /* 711 * This whole syscall-via-rpc is still taking form. For example, it 712 * may be necessary to set syscalls individually instead of lobbing 713 * them all to the same place. So don't think this interface is 714 * set in stone. 715 */ 716 int 717 rump_sysproxy_set(rump_sysproxy_t proxy, void *arg) 718 { 719 720 if (rump_sysproxy_arg) 721 return EBUSY; 722 723 rump_sysproxy_arg = arg; 724 rump_sysproxy = proxy; 725 726 return 0; 727 } 728 729 int 730 rump_getversion(void) 731 { 732 733 return __NetBSD_Version__; 734 } 735