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