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