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