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