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