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