rump.c revision 1.209 1 /* $NetBSD: rump.c,v 1.209 2010/12/12 13:18:07 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.209 2010/12/12 13:18:07 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
459 /* dump means we really take the dive here */
460 if ((howto & RB_DUMP) || panicstr) {
461 ruhow = RUMPUSER_PANIC;
462 goto out;
463 }
464
465 /* try to sync */
466 if (!((howto & RB_NOSYNC) || panicstr)) {
467 rump_vfs_fini();
468 }
469
470 /* your wish is my command */
471 if (howto & RB_HALT) {
472 printf("rump kernel halted\n");
473 for (;;) {
474 uint64_t sec = 5, nsec = 0;
475 int error;
476
477 rumpuser_nanosleep(&sec, &nsec, &error);
478 }
479 }
480
481 /* this function is __dead, we must exit */
482 out:
483 printf("halted\n");
484 rumpuser_exit(ruhow);
485 }
486
487 struct uio *
488 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
489 {
490 struct uio *uio;
491 enum uio_rw uiorw;
492
493 switch (rw) {
494 case RUMPUIO_READ:
495 uiorw = UIO_READ;
496 break;
497 case RUMPUIO_WRITE:
498 uiorw = UIO_WRITE;
499 break;
500 default:
501 panic("%s: invalid rw %d", __func__, rw);
502 }
503
504 uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
505 uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
506
507 uio->uio_iov->iov_base = buf;
508 uio->uio_iov->iov_len = bufsize;
509
510 uio->uio_iovcnt = 1;
511 uio->uio_offset = offset;
512 uio->uio_resid = bufsize;
513 uio->uio_rw = uiorw;
514 UIO_SETUP_SYSSPACE(uio);
515
516 return uio;
517 }
518
519 size_t
520 rump_uio_getresid(struct uio *uio)
521 {
522
523 return uio->uio_resid;
524 }
525
526 off_t
527 rump_uio_getoff(struct uio *uio)
528 {
529
530 return uio->uio_offset;
531 }
532
533 size_t
534 rump_uio_free(struct uio *uio)
535 {
536 size_t resid;
537
538 resid = uio->uio_resid;
539 kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
540 kmem_free(uio, sizeof(*uio));
541
542 return resid;
543 }
544
545 kauth_cred_t
546 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
547 {
548 kauth_cred_t cred;
549 int rv;
550
551 cred = kauth_cred_alloc();
552 kauth_cred_setuid(cred, uid);
553 kauth_cred_seteuid(cred, uid);
554 kauth_cred_setsvuid(cred, uid);
555 kauth_cred_setgid(cred, gid);
556 kauth_cred_setgid(cred, gid);
557 kauth_cred_setegid(cred, gid);
558 kauth_cred_setsvgid(cred, gid);
559 rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
560 /* oh this is silly. and by "this" I mean kauth_cred_setgroups() */
561 assert(rv == 0);
562
563 return cred;
564 }
565
566 void
567 rump_cred_put(kauth_cred_t cred)
568 {
569
570 kauth_cred_free(cred);
571 }
572
573 static int compcounter[RUMP_COMPONENT_MAX];
574
575 static void
576 rump_component_init_cb(struct rump_component *rc, int type)
577 {
578
579 KASSERT(type < RUMP_COMPONENT_MAX);
580 if (rc->rc_type == type) {
581 rc->rc_init();
582 compcounter[type]++;
583 }
584 }
585
586 int
587 rump_component_count(enum rump_component_type type)
588 {
589
590 KASSERT(type <= RUMP_COMPONENT_MAX);
591 return compcounter[type];
592 }
593
594 void
595 rump_component_init(enum rump_component_type type)
596 {
597
598 rumpuser_dl_component_init(type, rump_component_init_cb);
599 }
600
601 /*
602 * Initialize a module which has already been loaded and linked
603 * with dlopen(). This is fundamentally the same as a builtin module.
604 */
605 int
606 rump_module_init(const struct modinfo * const *mip, size_t nmodinfo)
607 {
608
609 return module_builtin_add(mip, nmodinfo, true);
610 }
611
612 /*
613 * Finish module (flawless victory, fatality!).
614 */
615 int
616 rump_module_fini(const struct modinfo *mi)
617 {
618
619 return module_builtin_remove(mi, true);
620 }
621
622 /*
623 * Add loaded and linked module to the builtin list. It will
624 * later be initialized with module_init_class().
625 */
626
627 static void
628 add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo)
629 {
630
631 module_builtin_add(mip, nmodinfo, false);
632 }
633
634 int
635 rump_kernelfsym_load(void *symtab, uint64_t symsize,
636 char *strtab, uint64_t strsize)
637 {
638 static int inited = 0;
639 Elf64_Ehdr ehdr;
640
641 if (inited)
642 return EBUSY;
643 inited = 1;
644
645 /*
646 * Use 64bit header since it's bigger. Shouldn't make a
647 * difference, since we're passing in all zeroes anyway.
648 */
649 memset(&ehdr, 0, sizeof(ehdr));
650 ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize);
651
652 return 0;
653 }
654
655 static int
656 rump_proxy_syscall(int num, void *arg, register_t *retval)
657 {
658 struct lwp *l;
659 struct sysent *callp;
660 int rv;
661
662 if (__predict_false(num >= SYS_NSYSENT))
663 return ENOSYS;
664
665 callp = rump_sysent + num;
666 l = curlwp;
667 rv = sy_call(callp, l, (void *)arg, retval);
668
669 return rv;
670 }
671
672 static int
673 rump_proxy_newproc(void *priv)
674 {
675 struct vmspace *newspace;
676 int error;
677
678 if ((error = rump_lwproc_newproc()) != 0)
679 return error;
680
681 /*
682 * Since it's a proxy proc, adjust the vmspace.
683 * Refcount will eternally be 1.
684 */
685 newspace = kmem_alloc(sizeof(*newspace), KM_SLEEP);
686 newspace->vm_refcnt = 1;
687 newspace->vm_map.pmap = priv;
688 KASSERT(curproc->p_vmspace == vmspace_kernel());
689 curproc->p_vmspace = newspace;
690
691 return 0;
692 }
693
694 int
695 rump_boot_gethowto()
696 {
697
698 return boothowto;
699 }
700
701 void
702 rump_boot_sethowto(int howto)
703 {
704
705 boothowto = howto;
706 }
707
708 int
709 rump_getversion(void)
710 {
711
712 return __NetBSD_Version__;
713 }
714
715 /*
716 * Note: may be called unscheduled. Not fully safe since no locking
717 * of allevents (currently that's not even available).
718 */
719 void
720 rump_printevcnts()
721 {
722 struct evcnt *ev;
723
724 TAILQ_FOREACH(ev, &allevents, ev_list)
725 rumpuser_dprintf("%s / %s: %" PRIu64 "\n",
726 ev->ev_group, ev->ev_name, ev->ev_count);
727 }
728
729 /*
730 * If you use this interface ... well ... all bets are off.
731 * The original purpose is for the p2k fs server library to be
732 * able to use the same pid/lid for VOPs as the host kernel.
733 */
734 void
735 rump_allbetsareoff_setid(pid_t pid, int lid)
736 {
737 struct lwp *l = curlwp;
738 struct proc *p = l->l_proc;
739
740 l->l_lid = lid;
741 p->p_pid = pid;
742 }
743