rump.c revision 1.160 1 /* $NetBSD: rump.c,v 1.160 2010/04/14 10:27:53 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.160 2010/04/14 10:27:53 pooka Exp $");
32
33 #include <sys/param.h>
34 #include <sys/atomic.h>
35 #include <sys/buf.h>
36 #include <sys/callout.h>
37 #include <sys/conf.h>
38 #include <sys/cpu.h>
39 #include <sys/device.h>
40 #include <sys/evcnt.h>
41 #include <sys/event.h>
42 #include <sys/exec_elf.h>
43 #include <sys/filedesc.h>
44 #include <sys/iostat.h>
45 #include <sys/kauth.h>
46 #include <sys/kernel.h>
47 #include <sys/kmem.h>
48 #include <sys/kprintf.h>
49 #include <sys/ksyms.h>
50 #include <sys/msgbuf.h>
51 #include <sys/module.h>
52 #include <sys/once.h>
53 #include <sys/percpu.h>
54 #include <sys/pipe.h>
55 #include <sys/queue.h>
56 #include <sys/reboot.h>
57 #include <sys/resourcevar.h>
58 #include <sys/select.h>
59 #include <sys/sysctl.h>
60 #include <sys/syscall.h>
61 #include <sys/timetc.h>
62 #include <sys/tty.h>
63 #include <sys/uidinfo.h>
64 #include <sys/vmem.h>
65 #include <sys/xcall.h>
66
67 #include <rump/rumpuser.h>
68
69 #include <secmodel/suser/suser.h>
70
71 #include <prop/proplib.h>
72
73 #include <uvm/uvm_readahead.h>
74
75 #include "rump_private.h"
76 #include "rump_net_private.h"
77 #include "rump_vfs_private.h"
78 #include "rump_dev_private.h"
79
80 struct proc proc0;
81 struct session rump_session = {
82 .s_count = 1,
83 .s_flags = 0,
84 .s_leader = &proc0,
85 .s_login = "rumphobo",
86 .s_sid = 0,
87 };
88 struct pgrp rump_pgrp = {
89 .pg_members = LIST_HEAD_INITIALIZER(pg_members),
90 .pg_session = &rump_session,
91 .pg_jobc = 1,
92 };
93 struct pstats rump_stats;
94 struct plimit rump_limits;
95 struct filedesc rump_filedesc0;
96 struct proclist allproc;
97 char machine[] = "rump";
98 static kauth_cred_t rump_susercred;
99
100 /* pretend the master rump proc is init */
101 struct proc *initproc = &proc0;
102
103 struct rumpuser_mtx *rump_giantlock;
104
105 sigset_t sigcantmask;
106
107 struct device rump_rootdev = {
108 .dv_class = DV_VIRTUAL
109 };
110
111 #ifdef RUMP_WITHOUT_THREADS
112 int rump_threads = 0;
113 #else
114 int rump_threads = 1;
115 #endif
116
117 static char rump_msgbuf[16*1024]; /* 16k should be enough for std rump needs */
118
119 static void
120 rump_aiodone_worker(struct work *wk, void *dummy)
121 {
122 struct buf *bp = (struct buf *)wk;
123
124 KASSERT(&bp->b_work == wk);
125 bp->b_iodone(bp);
126 }
127
128 static int rump_inited;
129 static struct emul emul_rump = {
130 .e_vm_default_addr = uvm_default_mapaddr,
131 };
132
133 int rump__unavailable(void);
134 int rump__unavailable() {return EOPNOTSUPP;}
135 __weak_alias(rump_net_init,rump__unavailable);
136 __weak_alias(rump_vfs_init,rump__unavailable);
137 __weak_alias(rump_dev_init,rump__unavailable);
138
139 __weak_alias(rump_vfs_fini,rump__unavailable);
140
141 __weak_alias(biodone,rump__unavailable);
142 __weak_alias(sopoll,rump__unavailable);
143
144 void rump__unavailable_vfs_panic(void);
145 void rump__unavailable_vfs_panic() {panic("vfs component not available");}
146 __weak_alias(usermount_common_policy,rump__unavailable_vfs_panic);
147
148 rump_proc_vfs_init_fn rump_proc_vfs_init;
149 rump_proc_vfs_release_fn rump_proc_vfs_release;
150
151 static void add_linkedin_modules(const struct modinfo *const *, size_t);
152
153 static void __noinline
154 messthestack(void)
155 {
156 volatile uint32_t mess[64];
157 uint64_t d1, d2;
158 int i, error;
159
160 for (i = 0; i < 64; i++) {
161 rumpuser_gettime(&d1, &d2, &error);
162 mess[i] = d2;
163 }
164 }
165
166 /*
167 * Create kern.hostname. why only this you ask. well, init_sysctl
168 * is a kitchen sink in need of some gardening. but i want to use
169 * kern.hostname today.
170 */
171 static void
172 mksysctls(void)
173 {
174
175 sysctl_createv(NULL, 0, NULL, NULL,
176 CTLFLAG_PERMANENT, CTLTYPE_NODE, "kern", NULL,
177 NULL, 0, NULL, 0, CTL_KERN, CTL_EOL);
178
179 /* XXX: setting hostnamelen is missing */
180 sysctl_createv(NULL, 0, NULL, NULL,
181 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_STRING, "hostname",
182 SYSCTL_DESCR("System hostname"), NULL, 0,
183 &hostname, MAXHOSTNAMELEN, CTL_KERN, KERN_HOSTNAME, CTL_EOL);
184 }
185
186 int
187 rump__init(int rump_version)
188 {
189 char buf[256];
190 struct timespec ts;
191 uint64_t sec, nsec;
192 struct proc *p;
193 struct lwp *l;
194 int i;
195 int error;
196
197 /* not reentrant */
198 if (rump_inited)
199 return 0;
200 else if (rump_inited == -1)
201 panic("rump_init: host process restart required");
202 else
203 rump_inited = 1;
204
205 if (rumpuser_getenv("RUMP_VERBOSE", buf, sizeof(buf), &error) == 0) {
206 if (*buf != '0')
207 boothowto = AB_VERBOSE;
208 }
209
210 rumpuser_gettime(&sec, &nsec, &error);
211 boottime.tv_sec = sec;
212 boottime.tv_nsec = nsec;
213
214 initmsgbuf(rump_msgbuf, sizeof(rump_msgbuf));
215 aprint_verbose("%s%s", copyright, version);
216
217 /*
218 * Seed arc4random() with a "reasonable" amount of randomness.
219 * Yes, this is a quick kludge which depends on the arc4random
220 * implementation.
221 */
222 messthestack();
223 arc4random();
224
225 if (rump_version != RUMP_VERSION) {
226 printf("rump version mismatch, %d vs. %d\n",
227 rump_version, RUMP_VERSION);
228 return EPROGMISMATCH;
229 }
230
231 if (rumpuser_getenv("RUMP_THREADS", buf, sizeof(buf), &error) == 0) {
232 rump_threads = *buf != '0';
233 }
234 rumpuser_thrinit(rump_user_schedule, rump_user_unschedule,
235 rump_threads);
236 rump_intr_init();
237
238 /* init minimal lwp/cpu context */
239 l = &lwp0;
240 l->l_lid = 1;
241 l->l_cpu = rump_cpu;
242 rumpuser_set_curlwp(l);
243
244 mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_NONE);
245 rumpuser_mutex_recursive_init(&rump_giantlock);
246 ksyms_init();
247 rumpvm_init();
248 evcnt_init();
249
250 once_init();
251 prop_kern_init();
252
253 pool_subsystem_init();
254 kmem_init();
255
256 uvm_ra_init();
257
258 mutex_obj_init();
259 callout_startup();
260
261 kprintf_init();
262 loginit();
263
264 kauth_init();
265 rump_susercred = rump_cred_create(0, 0, 0, NULL);
266
267 /* init proc0 and rest of lwp0 now that we can allocate memory */
268 p = &proc0;
269 p->p_stats = &rump_stats;
270 p->p_limit = &rump_limits;
271 p->p_pgrp = &rump_pgrp;
272 p->p_pid = 0;
273 p->p_fd = &rump_filedesc0;
274 p->p_vmspace = &rump_vmspace;
275 p->p_emul = &emul_rump;
276 p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
277 l->l_cred = rump_cred_suserget();
278 l->l_proc = p;
279 LIST_INIT(&allproc);
280 LIST_INSERT_HEAD(&allproc, &proc0, p_list);
281 proc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
282 lwpinit_specificdata();
283
284 rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
285 rump_limits.pl_rlimit[RLIMIT_NOFILE].rlim_cur = RLIM_INFINITY;
286 rump_limits.pl_rlimit[RLIMIT_SBSIZE].rlim_cur = RLIM_INFINITY;
287
288 rump_scheduler_init();
289 /* revert temporary context and schedule a real context */
290 l->l_cpu = NULL;
291 rumpuser_set_curlwp(NULL);
292 rump_schedule();
293
294 percpu_init();
295 inittimecounter();
296 ntp_init();
297
298 rumpuser_gettime(&sec, &nsec, &error);
299 ts.tv_sec = sec;
300 ts.tv_nsec = nsec;
301 tc_setclock(&ts);
302
303 /* we are mostly go. do per-cpu subsystem init */
304 for (i = 0; i < ncpu; i++) {
305 struct cpu_info *ci = cpu_lookup(i);
306
307 callout_init_cpu(ci);
308 softint_init(ci);
309 xc_init_cpu(ci);
310 pool_cache_cpu_init(ci);
311 selsysinit(ci);
312 percpu_init_cpu(ci);
313 }
314
315 sysctl_init();
316 kqueue_init();
317 iostat_init();
318 uid_init();
319 fd_sys_init();
320 module_init();
321 devsw_init();
322 pipe_init();
323
324 rumpuser_dl_bootstrap(add_linkedin_modules, rump_kernelfsym_load);
325
326 /* these do nothing if not present */
327 rump_vfs_init();
328 rump_net_init();
329 rump_dev_init();
330 cold = 0;
331
332 /* aieeeedondest */
333 if (rump_threads) {
334 if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
335 rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE))
336 panic("aiodoned");
337 }
338
339 mksysctls();
340 sysctl_finalize();
341
342 module_init_class(MODULE_CLASS_ANY);
343
344 rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
345 hostnamelen = strlen(hostname);
346
347 sigemptyset(&sigcantmask);
348
349 lwp0.l_fd = proc0.p_fd = fd_init(&rump_filedesc0);
350
351 if (rump_threads)
352 vmem_rehash_start();
353
354 rump_unschedule();
355
356 return 0;
357 }
358
359 /* maybe support sys_reboot some day for remote shutdown */
360 void
361 rump_reboot(int howto)
362 {
363
364 /* dump means we really take the dive here */
365 if ((howto & RB_DUMP) || panicstr) {
366 rumpuser_exit(RUMPUSER_PANIC);
367 /*NOTREACHED*/
368 }
369
370 /* try to sync */
371 if (!((howto & RB_NOSYNC) || panicstr)) {
372 rump_vfs_fini();
373 }
374
375 /* your wish is my command */
376 if (howto & RB_HALT) {
377 for (;;) {
378 uint64_t sec = 5, nsec = 0;
379 int error;
380
381 rumpuser_nanosleep(&sec, &nsec, &error);
382 }
383 }
384 rump_inited = -1;
385 }
386
387 struct uio *
388 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
389 {
390 struct uio *uio;
391 enum uio_rw uiorw;
392
393 switch (rw) {
394 case RUMPUIO_READ:
395 uiorw = UIO_READ;
396 break;
397 case RUMPUIO_WRITE:
398 uiorw = UIO_WRITE;
399 break;
400 default:
401 panic("%s: invalid rw %d", __func__, rw);
402 }
403
404 uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
405 uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
406
407 uio->uio_iov->iov_base = buf;
408 uio->uio_iov->iov_len = bufsize;
409
410 uio->uio_iovcnt = 1;
411 uio->uio_offset = offset;
412 uio->uio_resid = bufsize;
413 uio->uio_rw = uiorw;
414 uio->uio_vmspace = UIO_VMSPACE_SYS;
415
416 return uio;
417 }
418
419 size_t
420 rump_uio_getresid(struct uio *uio)
421 {
422
423 return uio->uio_resid;
424 }
425
426 off_t
427 rump_uio_getoff(struct uio *uio)
428 {
429
430 return uio->uio_offset;
431 }
432
433 size_t
434 rump_uio_free(struct uio *uio)
435 {
436 size_t resid;
437
438 resid = uio->uio_resid;
439 kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
440 kmem_free(uio, sizeof(*uio));
441
442 return resid;
443 }
444
445 static pid_t nextpid = 1;
446 struct lwp *
447 rump_newproc_switch()
448 {
449 struct lwp *l;
450 pid_t mypid;
451
452 mypid = atomic_inc_uint_nv(&nextpid);
453 if (__predict_false(mypid == 0))
454 mypid = atomic_inc_uint_nv(&nextpid);
455
456 l = rump_lwp_alloc(mypid, 0);
457 rump_lwp_switch(l);
458
459 return l;
460 }
461
462 struct lwp *
463 rump_lwp_alloc_and_switch(pid_t pid, lwpid_t lid)
464 {
465 struct lwp *l;
466
467 l = rump_lwp_alloc(pid, lid);
468 rump_lwp_switch(l);
469
470 return l;
471 }
472
473 struct lwp *
474 rump_lwp_alloc(pid_t pid, lwpid_t lid)
475 {
476 struct lwp *l;
477 struct proc *p;
478
479 l = kmem_zalloc(sizeof(*l), KM_SLEEP);
480 if (pid != 0) {
481 p = kmem_zalloc(sizeof(*p), KM_SLEEP);
482 if (rump_proc_vfs_init)
483 rump_proc_vfs_init(p);
484 p->p_stats = &rump_stats;
485 p->p_limit = &rump_limits;
486 p->p_pid = pid;
487 p->p_vmspace = &rump_vmspace;
488 p->p_emul = &emul_rump;
489 p->p_fd = fd_init(NULL);
490 p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
491 l->l_cred = rump_cred_suserget();
492 } else {
493 p = &proc0;
494 l->l_cred = rump_susercred;
495 }
496
497 l->l_proc = p;
498 l->l_lid = lid;
499 l->l_fd = p->p_fd;
500 l->l_cpu = NULL;
501 lwp_initspecific(l);
502 LIST_INSERT_HEAD(&alllwp, l, l_list);
503
504 return l;
505 }
506
507 void
508 rump_lwp_switch(struct lwp *newlwp)
509 {
510 struct lwp *l = curlwp;
511
512 rumpuser_set_curlwp(NULL);
513 newlwp->l_cpu = l->l_cpu;
514 newlwp->l_mutex = l->l_mutex;
515 l->l_mutex = NULL;
516 l->l_cpu = NULL;
517 rumpuser_set_curlwp(newlwp);
518 if (l->l_flag & LW_WEXIT)
519 rump_lwp_free(l);
520 }
521
522 /* XXX: this has effect only on non-pid0 lwps */
523 void
524 rump_lwp_release(struct lwp *l)
525 {
526 struct proc *p;
527
528 p = l->l_proc;
529 if (p->p_pid != 0) {
530 mutex_obj_free(p->p_lock);
531 fd_free();
532 if (rump_proc_vfs_release)
533 rump_proc_vfs_release(p);
534 rump_cred_put(l->l_cred);
535 kmem_free(p, sizeof(*p));
536 }
537 KASSERT((l->l_flag & LW_WEXIT) == 0);
538 l->l_flag |= LW_WEXIT;
539 }
540
541 void
542 rump_lwp_free(struct lwp *l)
543 {
544
545 KASSERT(l->l_flag & LW_WEXIT);
546 KASSERT(l->l_mutex == NULL);
547 if (l->l_name)
548 kmem_free(l->l_name, MAXCOMLEN);
549 lwp_finispecific(l);
550 LIST_REMOVE(l, l_list);
551 kmem_free(l, sizeof(*l));
552 }
553
554 struct lwp *
555 rump_lwp_curlwp(void)
556 {
557 struct lwp *l = curlwp;
558
559 if (l->l_flag & LW_WEXIT)
560 return NULL;
561 return l;
562 }
563
564 /* rump private. NEEDS WORK! */
565 void
566 rump_set_vmspace(struct vmspace *vm)
567 {
568 struct proc *p = curproc;
569
570 p->p_vmspace = vm;
571 }
572
573 kauth_cred_t
574 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
575 {
576 kauth_cred_t cred;
577 int rv;
578
579 cred = kauth_cred_alloc();
580 kauth_cred_setuid(cred, uid);
581 kauth_cred_seteuid(cred, uid);
582 kauth_cred_setsvuid(cred, uid);
583 kauth_cred_setgid(cred, gid);
584 kauth_cred_setgid(cred, gid);
585 kauth_cred_setegid(cred, gid);
586 kauth_cred_setsvgid(cred, gid);
587 rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
588 /* oh this is silly. and by "this" I mean kauth_cred_setgroups() */
589 assert(rv == 0);
590
591 return cred;
592 }
593
594 void
595 rump_cred_put(kauth_cred_t cred)
596 {
597
598 kauth_cred_free(cred);
599 }
600
601 kauth_cred_t
602 rump_cred_suserget(void)
603 {
604
605 kauth_cred_hold(rump_susercred);
606 return rump_susercred;
607 }
608
609 /*
610 * Return the next system lwpid
611 */
612 lwpid_t
613 rump_nextlid(void)
614 {
615 lwpid_t retid;
616
617 mutex_enter(proc0.p_lock);
618 /*
619 * Take next one, don't return 0
620 * XXX: most likely we'll have collisions in case this
621 * wraps around.
622 */
623 if (++proc0.p_nlwpid == 0)
624 ++proc0.p_nlwpid;
625 retid = proc0.p_nlwpid;
626 mutex_exit(proc0.p_lock);
627
628 return retid;
629 }
630
631 static int compcounter[RUMP_COMPONENT_MAX];
632
633 static void
634 rump_component_init_cb(struct rump_component *rc, int type)
635 {
636
637 KASSERT(type < RUMP_COMPONENT_MAX);
638 if (rc->rc_type == type) {
639 rc->rc_init();
640 compcounter[type]++;
641 }
642 }
643
644 int
645 rump_component_count(enum rump_component_type type)
646 {
647
648 KASSERT(type <= RUMP_COMPONENT_MAX);
649 return compcounter[type];
650 }
651
652 void
653 rump_component_init(enum rump_component_type type)
654 {
655
656 rumpuser_dl_component_init(type, rump_component_init_cb);
657 }
658
659 /*
660 * Initialize a module which has already been loaded and linked
661 * with dlopen(). This is fundamentally the same as a builtin module.
662 */
663 int
664 rump_module_init(const struct modinfo * const *mip, size_t nmodinfo)
665 {
666
667 return module_builtin_add(mip, nmodinfo, true);
668 }
669
670 /*
671 * Finish module (flawless victory, fatality!).
672 */
673 int
674 rump_module_fini(const struct modinfo *mi)
675 {
676
677 return module_builtin_remove(mi, true);
678 }
679
680 /*
681 * Add loaded and linked module to the builtin list. It will
682 * later be initialized with module_init_class().
683 */
684
685 static void
686 add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo)
687 {
688
689 module_builtin_add(mip, nmodinfo, false);
690 }
691
692 int
693 rump_kernelfsym_load(void *symtab, uint64_t symsize,
694 char *strtab, uint64_t strsize)
695 {
696 static int inited = 0;
697 Elf64_Ehdr ehdr;
698
699 if (inited)
700 return EBUSY;
701 inited = 1;
702
703 /*
704 * Use 64bit header since it's bigger. Shouldn't make a
705 * difference, since we're passing in all zeroes anyway.
706 */
707 memset(&ehdr, 0, sizeof(ehdr));
708 ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize);
709
710 return 0;
711 }
712
713 static int
714 rump_sysproxy_local(int num, void *arg, uint8_t *data, size_t dlen,
715 register_t *retval)
716 {
717 struct lwp *l;
718 struct sysent *callp;
719 int rv;
720
721 if (__predict_false(num >= SYS_NSYSENT))
722 return ENOSYS;
723
724 callp = rump_sysent + num;
725 rump_schedule();
726 l = curlwp;
727 rv = callp->sy_call(l, (void *)data, retval);
728 rump_unschedule();
729
730 return rv;
731 }
732
733 int
734 rump_boot_gethowto()
735 {
736
737 return boothowto;
738 }
739
740 void
741 rump_boot_sethowto(int howto)
742 {
743
744 boothowto = howto;
745 }
746
747 rump_sysproxy_t rump_sysproxy = rump_sysproxy_local;
748 void *rump_sysproxy_arg;
749
750 /*
751 * This whole syscall-via-rpc is still taking form. For example, it
752 * may be necessary to set syscalls individually instead of lobbing
753 * them all to the same place. So don't think this interface is
754 * set in stone.
755 */
756 int
757 rump_sysproxy_set(rump_sysproxy_t proxy, void *arg)
758 {
759
760 if (rump_sysproxy_arg)
761 return EBUSY;
762
763 rump_sysproxy_arg = arg;
764 rump_sysproxy = proxy;
765
766 return 0;
767 }
768
769 int
770 rump_getversion(void)
771 {
772
773 return __NetBSD_Version__;
774 }
775