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