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