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