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