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