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