rump.c revision 1.192 1 /* $NetBSD: rump.c,v 1.192 2010/10/28 11:30:07 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.192 2010/10/28 11:30:07 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 /* pretend the master rump proc is init */
90 struct proc *initproc = &proc0;
91
92 struct rumpuser_mtx *rump_giantlock;
93
94 struct device rump_rootdev = {
95 .dv_class = DV_VIRTUAL
96 };
97
98 #ifdef RUMP_WITHOUT_THREADS
99 int rump_threads = 0;
100 #else
101 int rump_threads = 1;
102 #endif
103
104 /*
105 * System call proxying support. These deserve another look later,
106 * but good enough for now.
107 */
108 static struct vmspace sp_vmspace;
109 static int rumpsp_type;
110
111 static char rump_msgbuf[16*1024]; /* 16k should be enough for std rump needs */
112
113 static void
114 rump_aiodone_worker(struct work *wk, void *dummy)
115 {
116 struct buf *bp = (struct buf *)wk;
117
118 KASSERT(&bp->b_work == wk);
119 bp->b_iodone(bp);
120 }
121
122 static int rump_inited;
123
124 /*
125 * Make sure pnbuf_cache is available even without vfs
126 */
127 struct pool_cache *pnbuf_cache;
128 int rump_initpnbufpool(void);
129 int rump_initpnbufpool(void)
130 {
131
132 pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
133 NULL, IPL_NONE, NULL, NULL, NULL);
134 return EOPNOTSUPP;
135 }
136
137 int rump__unavailable(void);
138 int rump__unavailable() {return EOPNOTSUPP;}
139 __weak_alias(rump_net_init,rump__unavailable);
140 __weak_alias(rump_vfs_init,rump_initpnbufpool);
141 __weak_alias(rump_dev_init,rump__unavailable);
142
143 __weak_alias(rump_vfs_fini,rump__unavailable);
144
145 __weak_alias(biodone,rump__unavailable);
146 __weak_alias(sopoll,rump__unavailable);
147
148 __weak_alias(rump_vfs_drainbufs,rump__unavailable);
149
150 void rump__unavailable_vfs_panic(void);
151 void rump__unavailable_vfs_panic() {panic("vfs component not available");}
152 __weak_alias(usermount_common_policy,rump__unavailable_vfs_panic);
153
154 rump_proc_vfs_init_fn rump_proc_vfs_init;
155 rump_proc_vfs_release_fn rump_proc_vfs_release;
156
157 static void add_linkedin_modules(const struct modinfo *const *, size_t);
158
159 static void __noinline
160 messthestack(void)
161 {
162 volatile uint32_t mess[64];
163 uint64_t d1, d2;
164 int i, error;
165
166 for (i = 0; i < 64; i++) {
167 rumpuser_gettime(&d1, &d2, &error);
168 mess[i] = d2;
169 }
170 }
171
172 /*
173 * Create kern.hostname. why only this you ask. well, init_sysctl
174 * is a kitchen sink in need of some gardening. but i want to use
175 * kern.hostname today.
176 */
177 static void
178 mksysctls(void)
179 {
180
181 sysctl_createv(NULL, 0, NULL, NULL,
182 CTLFLAG_PERMANENT, CTLTYPE_NODE, "kern", NULL,
183 NULL, 0, NULL, 0, CTL_KERN, CTL_EOL);
184
185 /* XXX: setting hostnamelen is missing */
186 sysctl_createv(NULL, 0, NULL, NULL,
187 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_STRING, "hostname",
188 SYSCTL_DESCR("System hostname"), NULL, 0,
189 &hostname, MAXHOSTNAMELEN, CTL_KERN, KERN_HOSTNAME, CTL_EOL);
190 }
191
192 int
193 rump__init(int rump_version)
194 {
195 char buf[256];
196 struct timespec ts;
197 uint64_t sec, nsec;
198 struct lwp *l;
199 int i, numcpu;
200 int error;
201
202 /* not reentrant */
203 if (rump_inited)
204 return 0;
205 else if (rump_inited == -1)
206 panic("rump_init: host process restart required");
207 else
208 rump_inited = 1;
209
210 /* Check our role as a rump proxy */
211 if ((error = rumpuser_sp_init(&rumpsp_type)) != 0)
212 return error;
213
214 /* If we're a client, we can return directly. Otherwise, proceed. */
215 switch (rumpsp_type) {
216 case RUMP_SP_CLIENT:
217 return 0;
218 case RUMP_SP_SERVER:
219 case RUMP_SP_NONE:
220 break;
221 }
222
223 if (rumpuser_getversion() != RUMPUSER_VERSION) {
224 /* let's hope the ABI of rumpuser_dprintf is the same ;) */
225 rumpuser_dprintf("rumpuser version mismatch: %d vs. %d\n",
226 rumpuser_getversion(), RUMPUSER_VERSION);
227 return EPROGMISMATCH;
228 }
229
230 if (rumpuser_getenv("RUMP_VERBOSE", buf, sizeof(buf), &error) == 0) {
231 if (*buf != '0')
232 boothowto = AB_VERBOSE;
233 }
234
235 if (rumpuser_getenv("RUMP_NCPU", buf, sizeof(buf), &error) == 0)
236 error = 0;
237 /* non-x86 is missing CPU_INFO_FOREACH() support */
238 #if defined(__i386__) || defined(__x86_64__)
239 if (error == 0) {
240 numcpu = strtoll(buf, NULL, 10);
241 if (numcpu < 1)
242 numcpu = 1;
243 } else {
244 numcpu = rumpuser_getnhostcpu();
245 }
246 #else
247 if (error == 0)
248 printf("NCPU limited to 1 on this machine architecture\n");
249 numcpu = 1;
250 #endif
251 rump_cpus_bootstrap(numcpu);
252
253 rumpuser_gettime(&sec, &nsec, &error);
254 boottime.tv_sec = sec;
255 boottime.tv_nsec = nsec;
256
257 initmsgbuf(rump_msgbuf, sizeof(rump_msgbuf));
258 aprint_verbose("%s%s", copyright, version);
259
260 /*
261 * Seed arc4random() with a "reasonable" amount of randomness.
262 * Yes, this is a quick kludge which depends on the arc4random
263 * implementation.
264 */
265 messthestack();
266 arc4random();
267
268 if (rump_version != RUMP_VERSION) {
269 printf("rump version mismatch, %d vs. %d\n",
270 rump_version, RUMP_VERSION);
271 return EPROGMISMATCH;
272 }
273
274 if (rumpuser_getenv("RUMP_THREADS", buf, sizeof(buf), &error) == 0) {
275 rump_threads = *buf != '0';
276 }
277 rumpuser_thrinit(rump_user_schedule, rump_user_unschedule,
278 rump_threads);
279 rump_intr_init(numcpu);
280 rump_tsleep_init();
281
282 /* init minimal lwp/cpu context */
283 l = &lwp0;
284 l->l_lid = 1;
285 l->l_cpu = l->l_target_cpu = rump_cpu;
286 l->l_fd = &filedesc0;
287 rumpuser_set_curlwp(l);
288
289 mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_NONE);
290 rumpuser_mutex_recursive_init(&rump_giantlock);
291 ksyms_init();
292 uvm_init();
293 evcnt_init();
294
295 once_init();
296 kernconfig_lock_init();
297 prop_kern_init();
298
299 pool_subsystem_init();
300 kmem_init();
301
302 uvm_ra_init();
303 uao_init();
304
305 mutex_obj_init();
306 callout_startup();
307
308 kprintf_init();
309 loginit();
310
311 kauth_init();
312
313 procinit();
314 proc0_init();
315 uid_init();
316 chgproccnt(0, 1);
317
318 l->l_proc = &proc0;
319 lwp_update_creds(l);
320
321 lwpinit_specificdata();
322 lwp_initspecific(&lwp0);
323
324 rump_scheduler_init(numcpu);
325 /* revert temporary context and schedule a real context */
326 l->l_cpu = NULL;
327 rumpuser_set_curlwp(NULL);
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 rump_unschedule();
411
412 return 0;
413 }
414
415 /* maybe support sys_reboot some day for remote shutdown */
416 void
417 rump_reboot(int howto)
418 {
419
420 /* dump means we really take the dive here */
421 if ((howto & RB_DUMP) || panicstr) {
422 rumpuser_exit(RUMPUSER_PANIC);
423 /*NOTREACHED*/
424 }
425
426 /* try to sync */
427 if (!((howto & RB_NOSYNC) || panicstr)) {
428 rump_vfs_fini();
429 }
430
431 /* your wish is my command */
432 if (howto & RB_HALT) {
433 for (;;) {
434 uint64_t sec = 5, nsec = 0;
435 int error;
436
437 rumpuser_nanosleep(&sec, &nsec, &error);
438 }
439 }
440 rump_inited = -1;
441 }
442
443 struct uio *
444 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
445 {
446 struct uio *uio;
447 enum uio_rw uiorw;
448
449 switch (rw) {
450 case RUMPUIO_READ:
451 uiorw = UIO_READ;
452 break;
453 case RUMPUIO_WRITE:
454 uiorw = UIO_WRITE;
455 break;
456 default:
457 panic("%s: invalid rw %d", __func__, rw);
458 }
459
460 uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
461 uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
462
463 uio->uio_iov->iov_base = buf;
464 uio->uio_iov->iov_len = bufsize;
465
466 uio->uio_iovcnt = 1;
467 uio->uio_offset = offset;
468 uio->uio_resid = bufsize;
469 uio->uio_rw = uiorw;
470 uio->uio_vmspace = UIO_VMSPACE_SYS;
471
472 return uio;
473 }
474
475 size_t
476 rump_uio_getresid(struct uio *uio)
477 {
478
479 return uio->uio_resid;
480 }
481
482 off_t
483 rump_uio_getoff(struct uio *uio)
484 {
485
486 return uio->uio_offset;
487 }
488
489 size_t
490 rump_uio_free(struct uio *uio)
491 {
492 size_t resid;
493
494 resid = uio->uio_resid;
495 kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
496 kmem_free(uio, sizeof(*uio));
497
498 return resid;
499 }
500
501 /* rump private. NEEDS WORK! */
502 void
503 rump_set_vmspace(struct vmspace *vm)
504 {
505 struct proc *p = curproc;
506
507 p->p_vmspace = vm;
508 }
509
510 kauth_cred_t
511 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
512 {
513 kauth_cred_t cred;
514 int rv;
515
516 cred = kauth_cred_alloc();
517 kauth_cred_setuid(cred, uid);
518 kauth_cred_seteuid(cred, uid);
519 kauth_cred_setsvuid(cred, uid);
520 kauth_cred_setgid(cred, gid);
521 kauth_cred_setgid(cred, gid);
522 kauth_cred_setegid(cred, gid);
523 kauth_cred_setsvgid(cred, gid);
524 rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
525 /* oh this is silly. and by "this" I mean kauth_cred_setgroups() */
526 assert(rv == 0);
527
528 return cred;
529 }
530
531 void
532 rump_cred_put(kauth_cred_t cred)
533 {
534
535 kauth_cred_free(cred);
536 }
537
538 static int compcounter[RUMP_COMPONENT_MAX];
539
540 static void
541 rump_component_init_cb(struct rump_component *rc, int type)
542 {
543
544 KASSERT(type < RUMP_COMPONENT_MAX);
545 if (rc->rc_type == type) {
546 rc->rc_init();
547 compcounter[type]++;
548 }
549 }
550
551 int
552 rump_component_count(enum rump_component_type type)
553 {
554
555 KASSERT(type <= RUMP_COMPONENT_MAX);
556 return compcounter[type];
557 }
558
559 void
560 rump_component_init(enum rump_component_type type)
561 {
562
563 rumpuser_dl_component_init(type, rump_component_init_cb);
564 }
565
566 /*
567 * Initialize a module which has already been loaded and linked
568 * with dlopen(). This is fundamentally the same as a builtin module.
569 */
570 int
571 rump_module_init(const struct modinfo * const *mip, size_t nmodinfo)
572 {
573
574 return module_builtin_add(mip, nmodinfo, true);
575 }
576
577 /*
578 * Finish module (flawless victory, fatality!).
579 */
580 int
581 rump_module_fini(const struct modinfo *mi)
582 {
583
584 return module_builtin_remove(mi, true);
585 }
586
587 /*
588 * Add loaded and linked module to the builtin list. It will
589 * later be initialized with module_init_class().
590 */
591
592 static void
593 add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo)
594 {
595
596 module_builtin_add(mip, nmodinfo, false);
597 }
598
599 int
600 rump_kernelfsym_load(void *symtab, uint64_t symsize,
601 char *strtab, uint64_t strsize)
602 {
603 static int inited = 0;
604 Elf64_Ehdr ehdr;
605
606 if (inited)
607 return EBUSY;
608 inited = 1;
609
610 /*
611 * Use 64bit header since it's bigger. Shouldn't make a
612 * difference, since we're passing in all zeroes anyway.
613 */
614 memset(&ehdr, 0, sizeof(ehdr));
615 ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize);
616
617 return 0;
618 }
619
620 void *rump_sysproxy_arg; /* XXX: nukeme */
621 int
622 rump_syscall(int num, void *arg, register_t *retval)
623 {
624 struct lwp *l;
625 struct sysent *callp;
626 int rv;
627
628 if (__predict_false(num >= SYS_NSYSENT))
629 return ENOSYS;
630
631 callp = rump_sysent + num;
632 l = curlwp;
633 if (rumpsp_type == RUMP_SP_SERVER)
634 curproc->p_vmspace = &sp_vmspace;
635 rv = sy_call(callp, l, (void *)arg, retval);
636 if (rumpsp_type == RUMP_SP_SERVER)
637 curproc->p_vmspace = &vmspace0;
638
639 return rv;
640 }
641
642 int
643 rump_sysproxy(int num, void *arg, uint8_t *data, size_t dlen,
644 register_t *retval)
645 {
646 int rv;
647
648 if (rumpsp_type == RUMP_SP_CLIENT) {
649 rv = rumpuser_sp_syscall(num, data, dlen, retval);
650 } else {
651 rump_schedule();
652 rv = rump_syscall(num, data, retval);
653 rump_unschedule();
654 }
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