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