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