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