rump.c revision 1.210 1 /* $NetBSD: rump.c,v 1.210 2010/12/12 17:11:33 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.210 2010/12/12 17:11:33 pooka Exp $");
32
33 #include <sys/systm.h>
34 #define ELFSIZE ARCH_ELFSIZE
35
36 #include <sys/param.h>
37 #include <sys/atomic.h>
38 #include <sys/buf.h>
39 #include <sys/callout.h>
40 #include <sys/conf.h>
41 #include <sys/cpu.h>
42 #include <sys/device.h>
43 #include <sys/evcnt.h>
44 #include <sys/event.h>
45 #include <sys/exec_elf.h>
46 #include <sys/filedesc.h>
47 #include <sys/iostat.h>
48 #include <sys/kauth.h>
49 #include <sys/kernel.h>
50 #include <sys/kmem.h>
51 #include <sys/kprintf.h>
52 #include <sys/kthread.h>
53 #include <sys/ksyms.h>
54 #include <sys/msgbuf.h>
55 #include <sys/module.h>
56 #include <sys/once.h>
57 #include <sys/percpu.h>
58 #include <sys/pipe.h>
59 #include <sys/pool.h>
60 #include <sys/queue.h>
61 #include <sys/reboot.h>
62 #include <sys/resourcevar.h>
63 #include <sys/select.h>
64 #include <sys/sysctl.h>
65 #include <sys/syscall.h>
66 #include <sys/syscallvar.h>
67 #include <sys/timetc.h>
68 #include <sys/tty.h>
69 #include <sys/uidinfo.h>
70 #include <sys/vmem.h>
71 #include <sys/xcall.h>
72
73 #include <rump/rumpuser.h>
74
75 #include <secmodel/suser/suser.h>
76
77 #include <prop/proplib.h>
78
79 #include <uvm/uvm_extern.h>
80 #include <uvm/uvm_readahead.h>
81
82 #include "rump_private.h"
83 #include "rump_net_private.h"
84 #include "rump_vfs_private.h"
85 #include "rump_dev_private.h"
86
87 char machine[] = MACHINE;
88
89 struct proc *initproc;
90
91 struct device rump_rootdev = {
92 .dv_class = DV_VIRTUAL
93 };
94
95 #ifdef RUMP_WITHOUT_THREADS
96 int rump_threads = 0;
97 #else
98 int rump_threads = 1;
99 #endif
100
101 static int rump_proxy_syscall(int, void *, register_t *);
102 static int rump_proxy_newproc(void *);
103
104 static char rump_msgbuf[16*1024]; /* 16k should be enough for std rump needs */
105
106 static void
107 rump_aiodone_worker(struct work *wk, void *dummy)
108 {
109 struct buf *bp = (struct buf *)wk;
110
111 KASSERT(&bp->b_work == wk);
112 bp->b_iodone(bp);
113 }
114
115 static int rump_inited;
116
117 /*
118 * Make sure pnbuf_cache is available even without vfs
119 */
120 struct pool_cache *pnbuf_cache;
121 int rump_initpnbufpool(void);
122 int rump_initpnbufpool(void)
123 {
124
125 pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
126 NULL, IPL_NONE, NULL, NULL, NULL);
127 return EOPNOTSUPP;
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_initpnbufpool);
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 __weak_alias(rump_vfs_drainbufs,rump__unavailable);
142
143 void rump__unavailable_vfs_panic(void);
144 void rump__unavailable_vfs_panic() {panic("vfs component not available");}
145 __weak_alias(usermount_common_policy,rump__unavailable_vfs_panic);
146
147 rump_proc_vfs_init_fn rump_proc_vfs_init;
148 rump_proc_vfs_release_fn rump_proc_vfs_release;
149
150 static void add_linkedin_modules(const struct modinfo *const *, size_t);
151
152 static void __noinline
153 messthestack(void)
154 {
155 volatile uint32_t mess[64];
156 uint64_t d1, d2;
157 int i, error;
158
159 for (i = 0; i < 64; i++) {
160 rumpuser_gettime(&d1, &d2, &error);
161 mess[i] = d2;
162 }
163 }
164
165 /*
166 * Create kern.hostname. why only this you ask. well, init_sysctl
167 * is a kitchen sink in need of some gardening. but i want to use
168 * kern.hostname today.
169 */
170 static void
171 mksysctls(void)
172 {
173
174 sysctl_createv(NULL, 0, NULL, NULL,
175 CTLFLAG_PERMANENT, CTLTYPE_NODE, "kern", NULL,
176 NULL, 0, NULL, 0, CTL_KERN, CTL_EOL);
177
178 /* XXX: setting hostnamelen is missing */
179 sysctl_createv(NULL, 0, NULL, NULL,
180 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_STRING, "hostname",
181 SYSCTL_DESCR("System hostname"), NULL, 0,
182 &hostname, MAXHOSTNAMELEN, CTL_KERN, KERN_HOSTNAME, CTL_EOL);
183 }
184
185 /* there's no convenient kernel entry point for this, so just craft out own */
186 static pid_t
187 spgetpid(void)
188 {
189
190 return curproc->p_pid;
191 }
192
193 static const struct rumpuser_sp_ops spops = {
194 .spop_schedule = rump_schedule,
195 .spop_unschedule = rump_unschedule,
196 .spop_lwproc_switch = rump_lwproc_switch,
197 .spop_lwproc_release = rump_lwproc_releaselwp,
198 .spop_lwproc_newproc = rump_proxy_newproc,
199 .spop_lwproc_newlwp = rump_lwproc_newlwp,
200 .spop_lwproc_curlwp = rump_lwproc_curlwp,
201 .spop_syscall = rump_proxy_syscall,
202 .spop_getpid = spgetpid,
203 };
204
205 int
206 rump_daemonize_begin(void)
207 {
208
209 if (rump_inited)
210 return EALREADY;
211
212 return rumpuser_daemonize_begin();
213 }
214
215 int
216 rump_daemonize_done(int error)
217 {
218
219 return rumpuser_daemonize_done(error);
220 }
221
222 int
223 rump__init(int rump_version)
224 {
225 char buf[256];
226 struct timespec ts;
227 uint64_t sec, nsec;
228 struct lwp *l;
229 int i, numcpu;
230 int error;
231
232 /* not reentrant */
233 if (rump_inited)
234 return 0;
235 else if (rump_inited == -1)
236 panic("rump_init: host process restart required");
237 else
238 rump_inited = 1;
239
240 if (rumpuser_getversion() != RUMPUSER_VERSION) {
241 /* let's hope the ABI of rumpuser_dprintf is the same ;) */
242 rumpuser_dprintf("rumpuser version mismatch: %d vs. %d\n",
243 rumpuser_getversion(), RUMPUSER_VERSION);
244 return EPROGMISMATCH;
245 }
246
247 if (rumpuser_getenv("RUMP_VERBOSE", buf, sizeof(buf), &error) == 0) {
248 if (*buf != '0')
249 boothowto = AB_VERBOSE;
250 }
251
252 if (rumpuser_getenv("RUMP_NCPU", buf, sizeof(buf), &error) == 0)
253 error = 0;
254 /* non-x86 is missing CPU_INFO_FOREACH() support */
255 #ifdef RUMP_SMP
256 if (error == 0) {
257 numcpu = strtoll(buf, NULL, 10);
258 if (numcpu < 1)
259 numcpu = 1;
260 } else {
261 numcpu = rumpuser_getnhostcpu();
262 }
263 #else
264 if (error == 0)
265 printf("NCPU limited to 1 on this machine architecture\n");
266 numcpu = 1;
267 #endif
268 rump_cpus_bootstrap(&numcpu);
269
270 rumpuser_gettime(&sec, &nsec, &error);
271 boottime.tv_sec = sec;
272 boottime.tv_nsec = nsec;
273
274 initmsgbuf(rump_msgbuf, sizeof(rump_msgbuf));
275 aprint_verbose("%s%s", copyright, version);
276
277 /*
278 * Seed arc4random() with a "reasonable" amount of randomness.
279 * Yes, this is a quick kludge which depends on the arc4random
280 * implementation.
281 */
282 messthestack();
283 arc4random();
284
285 if (rump_version != RUMP_VERSION) {
286 printf("rump version mismatch, %d vs. %d\n",
287 rump_version, RUMP_VERSION);
288 return EPROGMISMATCH;
289 }
290
291 if (rumpuser_getenv("RUMP_THREADS", buf, sizeof(buf), &error) == 0) {
292 rump_threads = *buf != '0';
293 }
294 rumpuser_thrinit(rump_user_schedule, rump_user_unschedule,
295 rump_threads);
296 rump_intr_init(numcpu);
297 rump_tsleep_init();
298
299 /* init minimal lwp/cpu context */
300 l = &lwp0;
301 l->l_lid = 1;
302 l->l_cpu = l->l_target_cpu = rump_cpu;
303 l->l_fd = &filedesc0;
304 rumpuser_set_curlwp(l);
305
306 mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_NONE);
307 rumpuser_mutex_init(&rump_giantlock);
308 ksyms_init();
309 uvm_init();
310 evcnt_init();
311
312 once_init();
313 kernconfig_lock_init();
314 prop_kern_init();
315
316 pool_subsystem_init();
317 kmem_init();
318
319 uvm_ra_init();
320 uao_init();
321
322 mutex_obj_init();
323 callout_startup();
324
325 kprintf_init();
326 loginit();
327
328 kauth_init();
329
330 procinit();
331 proc0_init();
332 uid_init();
333 chgproccnt(0, 1);
334
335 l->l_proc = &proc0;
336 lwp_update_creds(l);
337
338 lwpinit_specificdata();
339 lwp_initspecific(&lwp0);
340
341 rump_scheduler_init(numcpu);
342 /* revert temporary context and schedule a semireal context */
343 l->l_cpu = NULL;
344 rumpuser_set_curlwp(NULL);
345 initproc = &proc0; /* borrow proc0 before we get initproc started */
346 rump_schedule();
347
348 percpu_init();
349 inittimecounter();
350 ntp_init();
351
352 rumpuser_gettime(&sec, &nsec, &error);
353 ts.tv_sec = sec;
354 ts.tv_nsec = nsec;
355 tc_setclock(&ts);
356
357 /* we are mostly go. do per-cpu subsystem init */
358 for (i = 0; i < numcpu; i++) {
359 struct cpu_info *ci = cpu_lookup(i);
360
361 /* attach non-bootstrap CPUs */
362 if (i > 0) {
363 rump_cpu_attach(ci);
364 ncpu++;
365 }
366
367 callout_init_cpu(ci);
368 softint_init(ci);
369 xc_init_cpu(ci);
370 pool_cache_cpu_init(ci);
371 selsysinit(ci);
372 percpu_init_cpu(ci);
373
374 aprint_verbose("cpu%d at thinair0: rump virtual cpu\n", i);
375 }
376
377 sysctl_init();
378 mksysctls();
379 kqueue_init();
380 iostat_init();
381 fd_sys_init();
382 module_init();
383 devsw_init();
384 pipe_init();
385 resource_init();
386
387 /* start page baroness */
388 if (rump_threads) {
389 if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL,
390 uvm_pageout, NULL, &uvm.pagedaemon_lwp, "pdaemon") != 0)
391 panic("pagedaemon create failed");
392 } else
393 uvm.pagedaemon_lwp = NULL; /* doesn't match curlwp */
394
395 /* process dso's */
396 rumpuser_dl_bootstrap(add_linkedin_modules, rump_kernelfsym_load);
397
398 rump_component_init(RUMP_COMPONENT_KERN);
399
400 /* these do nothing if not present */
401 rump_vfs_init();
402 rump_net_init();
403 rump_dev_init();
404
405 rump_component_init(RUMP_COMPONENT_KERN_VFS);
406
407 cold = 0;
408
409 /* aieeeedondest */
410 if (rump_threads) {
411 if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
412 rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE))
413 panic("aiodoned");
414 }
415
416 sysctl_finalize();
417
418 module_init_class(MODULE_CLASS_ANY);
419
420 rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
421 hostnamelen = strlen(hostname);
422
423 sigemptyset(&sigcantmask);
424
425 if (rump_threads)
426 vmem_rehash_start();
427
428 /*
429 * Create init, used to attach implicit threads in rump.
430 * (note: must be done after vfsinit to get cwdi)
431 */
432 (void)rump__lwproc_alloclwp(NULL); /* dummy thread for initproc */
433 mutex_enter(proc_lock);
434 initproc = proc_find_raw(1);
435 mutex_exit(proc_lock);
436 if (initproc == NULL)
437 panic("where in the world is initproc?");
438
439 /* release cpu */
440 rump_unschedule();
441
442 return 0;
443 }
444
445 int
446 rump_init_server(const char *url)
447 {
448
449 return rumpuser_sp_init(&spops, url);
450 }
451
452 void
453 cpu_reboot(int howto, char *bootstr)
454 {
455 int ruhow = 0;
456
457 printf("rump kernel halting...\n");
458 rumpuser_sp_fini();
459
460 /* dump means we really take the dive here */
461 if ((howto & RB_DUMP) || panicstr) {
462 ruhow = RUMPUSER_PANIC;
463 goto out;
464 }
465
466 /* try to sync */
467 if (!((howto & RB_NOSYNC) || panicstr)) {
468 rump_vfs_fini();
469 }
470
471 /* your wish is my command */
472 if (howto & RB_HALT) {
473 printf("rump kernel halted\n");
474 for (;;) {
475 uint64_t sec = 5, nsec = 0;
476 int error;
477
478 rumpuser_nanosleep(&sec, &nsec, &error);
479 }
480 }
481
482 /* this function is __dead, we must exit */
483 out:
484 printf("halted\n");
485 rumpuser_exit(ruhow);
486 }
487
488 struct uio *
489 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
490 {
491 struct uio *uio;
492 enum uio_rw uiorw;
493
494 switch (rw) {
495 case RUMPUIO_READ:
496 uiorw = UIO_READ;
497 break;
498 case RUMPUIO_WRITE:
499 uiorw = UIO_WRITE;
500 break;
501 default:
502 panic("%s: invalid rw %d", __func__, rw);
503 }
504
505 uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
506 uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
507
508 uio->uio_iov->iov_base = buf;
509 uio->uio_iov->iov_len = bufsize;
510
511 uio->uio_iovcnt = 1;
512 uio->uio_offset = offset;
513 uio->uio_resid = bufsize;
514 uio->uio_rw = uiorw;
515 UIO_SETUP_SYSSPACE(uio);
516
517 return uio;
518 }
519
520 size_t
521 rump_uio_getresid(struct uio *uio)
522 {
523
524 return uio->uio_resid;
525 }
526
527 off_t
528 rump_uio_getoff(struct uio *uio)
529 {
530
531 return uio->uio_offset;
532 }
533
534 size_t
535 rump_uio_free(struct uio *uio)
536 {
537 size_t resid;
538
539 resid = uio->uio_resid;
540 kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
541 kmem_free(uio, sizeof(*uio));
542
543 return resid;
544 }
545
546 kauth_cred_t
547 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
548 {
549 kauth_cred_t cred;
550 int rv;
551
552 cred = kauth_cred_alloc();
553 kauth_cred_setuid(cred, uid);
554 kauth_cred_seteuid(cred, uid);
555 kauth_cred_setsvuid(cred, uid);
556 kauth_cred_setgid(cred, gid);
557 kauth_cred_setgid(cred, gid);
558 kauth_cred_setegid(cred, gid);
559 kauth_cred_setsvgid(cred, gid);
560 rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
561 /* oh this is silly. and by "this" I mean kauth_cred_setgroups() */
562 assert(rv == 0);
563
564 return cred;
565 }
566
567 void
568 rump_cred_put(kauth_cred_t cred)
569 {
570
571 kauth_cred_free(cred);
572 }
573
574 static int compcounter[RUMP_COMPONENT_MAX];
575
576 static void
577 rump_component_init_cb(struct rump_component *rc, int type)
578 {
579
580 KASSERT(type < RUMP_COMPONENT_MAX);
581 if (rc->rc_type == type) {
582 rc->rc_init();
583 compcounter[type]++;
584 }
585 }
586
587 int
588 rump_component_count(enum rump_component_type type)
589 {
590
591 KASSERT(type <= RUMP_COMPONENT_MAX);
592 return compcounter[type];
593 }
594
595 void
596 rump_component_init(enum rump_component_type type)
597 {
598
599 rumpuser_dl_component_init(type, rump_component_init_cb);
600 }
601
602 /*
603 * Initialize a module which has already been loaded and linked
604 * with dlopen(). This is fundamentally the same as a builtin module.
605 */
606 int
607 rump_module_init(const struct modinfo * const *mip, size_t nmodinfo)
608 {
609
610 return module_builtin_add(mip, nmodinfo, true);
611 }
612
613 /*
614 * Finish module (flawless victory, fatality!).
615 */
616 int
617 rump_module_fini(const struct modinfo *mi)
618 {
619
620 return module_builtin_remove(mi, true);
621 }
622
623 /*
624 * Add loaded and linked module to the builtin list. It will
625 * later be initialized with module_init_class().
626 */
627
628 static void
629 add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo)
630 {
631
632 module_builtin_add(mip, nmodinfo, false);
633 }
634
635 int
636 rump_kernelfsym_load(void *symtab, uint64_t symsize,
637 char *strtab, uint64_t strsize)
638 {
639 static int inited = 0;
640 Elf64_Ehdr ehdr;
641
642 if (inited)
643 return EBUSY;
644 inited = 1;
645
646 /*
647 * Use 64bit header since it's bigger. Shouldn't make a
648 * difference, since we're passing in all zeroes anyway.
649 */
650 memset(&ehdr, 0, sizeof(ehdr));
651 ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize);
652
653 return 0;
654 }
655
656 static int
657 rump_proxy_syscall(int num, void *arg, register_t *retval)
658 {
659 struct lwp *l;
660 struct sysent *callp;
661 int rv;
662
663 if (__predict_false(num >= SYS_NSYSENT))
664 return ENOSYS;
665
666 callp = rump_sysent + num;
667 l = curlwp;
668 rv = sy_call(callp, l, (void *)arg, retval);
669
670 return rv;
671 }
672
673 static int
674 rump_proxy_newproc(void *priv)
675 {
676 struct vmspace *newspace;
677 int error;
678
679 if ((error = rump_lwproc_newproc()) != 0)
680 return error;
681
682 /*
683 * Since it's a proxy proc, adjust the vmspace.
684 * Refcount will eternally be 1.
685 */
686 newspace = kmem_alloc(sizeof(*newspace), KM_SLEEP);
687 newspace->vm_refcnt = 1;
688 newspace->vm_map.pmap = priv;
689 KASSERT(curproc->p_vmspace == vmspace_kernel());
690 curproc->p_vmspace = newspace;
691
692 return 0;
693 }
694
695 int
696 rump_boot_gethowto()
697 {
698
699 return boothowto;
700 }
701
702 void
703 rump_boot_sethowto(int howto)
704 {
705
706 boothowto = howto;
707 }
708
709 int
710 rump_getversion(void)
711 {
712
713 return __NetBSD_Version__;
714 }
715
716 /*
717 * Note: may be called unscheduled. Not fully safe since no locking
718 * of allevents (currently that's not even available).
719 */
720 void
721 rump_printevcnts()
722 {
723 struct evcnt *ev;
724
725 TAILQ_FOREACH(ev, &allevents, ev_list)
726 rumpuser_dprintf("%s / %s: %" PRIu64 "\n",
727 ev->ev_group, ev->ev_name, ev->ev_count);
728 }
729
730 /*
731 * If you use this interface ... well ... all bets are off.
732 * The original purpose is for the p2k fs server library to be
733 * able to use the same pid/lid for VOPs as the host kernel.
734 */
735 void
736 rump_allbetsareoff_setid(pid_t pid, int lid)
737 {
738 struct lwp *l = curlwp;
739 struct proc *p = l->l_proc;
740
741 l->l_lid = lid;
742 p->p_pid = pid;
743 }
744