rump.c revision 1.137 1 /* $NetBSD: rump.c,v 1.137 2009/11/26 09:20:07 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.137 2009/11/26 09:20:07 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/queue.h>
54 #include <sys/reboot.h>
55 #include <sys/resourcevar.h>
56 #include <sys/select.h>
57 #include <sys/sysctl.h>
58 #include <sys/syscall.h>
59 #include <sys/tty.h>
60 #include <sys/uidinfo.h>
61 #include <sys/vmem.h>
62
63 #include <rump/rumpuser.h>
64
65 #include <secmodel/suser/suser.h>
66
67 #include <prop/proplib.h>
68
69 #include <uvm/uvm_readahead.h>
70
71 #include "rump_private.h"
72 #include "rump_net_private.h"
73 #include "rump_vfs_private.h"
74 #include "rump_dev_private.h"
75
76 struct proc proc0;
77 struct session rump_session = {
78 .s_count = 1,
79 .s_flags = 0,
80 .s_leader = &proc0,
81 .s_login = "rumphobo",
82 .s_sid = 0,
83 };
84 struct pgrp rump_pgrp = {
85 .pg_members = LIST_HEAD_INITIALIZER(pg_members),
86 .pg_session = &rump_session,
87 .pg_jobc = 1,
88 };
89 struct pstats rump_stats;
90 struct plimit rump_limits;
91 struct filedesc rump_filedesc0;
92 struct proclist allproc;
93 char machine[] = "rump";
94 static kauth_cred_t rump_susercred;
95
96 /* pretend the master rump proc is init */
97 struct proc *initproc = &proc0;
98
99 struct rumpuser_mtx *rump_giantlock;
100
101 sigset_t sigcantmask;
102
103 #ifdef RUMP_WITHOUT_THREADS
104 int rump_threads = 0;
105 #else
106 int rump_threads = 1;
107 #endif
108
109 static void
110 rump_aiodone_worker(struct work *wk, void *dummy)
111 {
112 struct buf *bp = (struct buf *)wk;
113
114 KASSERT(&bp->b_work == wk);
115 bp->b_iodone(bp);
116 }
117
118 static int rump_inited;
119 static struct emul emul_rump;
120
121 int rump__unavailable(void);
122 int rump__unavailable() {return EOPNOTSUPP;}
123 __weak_alias(rump_net_init,rump__unavailable);
124 __weak_alias(rump_vfs_init,rump__unavailable);
125 __weak_alias(rump_dev_init,rump__unavailable);
126
127 __weak_alias(rump_vfs_fini,rump__unavailable);
128
129 __weak_alias(biodone,rump__unavailable);
130 __weak_alias(sopoll,rump__unavailable);
131
132 void rump__unavailable_vfs_panic(void);
133 void rump__unavailable_vfs_panic() {panic("vfs component not available");}
134 __weak_alias(vn_open,rump__unavailable_vfs_panic);
135 __weak_alias(vn_rdwr,rump__unavailable_vfs_panic);
136 __weak_alias(vn_stat,rump__unavailable_vfs_panic);
137 __weak_alias(vn_close,rump__unavailable_vfs_panic);
138 __weak_alias(namei,rump__unavailable_vfs_panic);
139 __weak_alias(usermount_common_policy,rump__unavailable_vfs_panic);
140
141 static void
142 pvfsinit_nop(struct proc *p)
143 {
144
145 return;
146 }
147
148 static void
149 pvfsrele_nop(struct proc *p)
150 {
151
152 return;
153 }
154
155 rump_proc_vfs_init_fn rump_proc_vfs_init = pvfsinit_nop;
156 rump_proc_vfs_release_fn rump_proc_vfs_release = pvfsrele_nop;
157
158 /*
159 * Stir up the stack a bit. These are exported functions to help
160 * convince the compiler that we don't want these routines completely
161 * optimized out or inlined. Is there an easier way to do this?
162 */
163 void nullfn(uint32_t *);
164 void nullfn(uint32_t *arg){}
165 void messthestack(void);
166 void
167 messthestack(void)
168 {
169 uint32_t mess[64];
170 uint64_t d1, d2;
171 int i, error;
172
173 for (i = 0; i < 64; i++) {
174 rumpuser_gettime(&d1, &d2, &error);
175 mess[i] = d2;
176 }
177 nullfn(mess);
178 }
179
180 int
181 rump__init(int rump_version)
182 {
183 char buf[256];
184 struct proc *p;
185 struct lwp *l;
186 int error;
187
188 /* not reentrant */
189 if (rump_inited)
190 return 0;
191 else if (rump_inited == -1)
192 panic("rump_init: host process restart required");
193 else
194 rump_inited = 1;
195
196 /*
197 * Seed arc4random() with a "reasonable" amount of randomness.
198 * Yes, this is a quick kludge which depends on the arc4random
199 * implementation.
200 */
201 messthestack();
202 arc4random();
203
204 if (rump_version != RUMP_VERSION) {
205 printf("rump version mismatch, %d vs. %d\n",
206 rump_version, RUMP_VERSION);
207 return EPROGMISMATCH;
208 }
209
210 if (rumpuser_getenv("RUMP_THREADS", buf, sizeof(buf), &error) == 0) {
211 rump_threads = *buf != '0';
212 }
213 rumpuser_thrinit(rump_user_schedule, rump_user_unschedule,
214 rump_threads);
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 rump_sleepers_init();
232
233 pool_subsystem_init();
234 kmem_init();
235
236 uvm_ra_init();
237
238 mutex_obj_init();
239
240 kprintf_init();
241 loginit();
242
243 kauth_init();
244 rump_susercred = rump_cred_create(0, 0, 0, NULL);
245
246 /* init proc0 and rest of lwp0 now that we can allocate memory */
247 p = &proc0;
248 p->p_stats = &rump_stats;
249 p->p_limit = &rump_limits;
250 p->p_pgrp = &rump_pgrp;
251 p->p_pid = 0;
252 p->p_fd = &rump_filedesc0;
253 p->p_vmspace = &rump_vmspace;
254 p->p_emul = &emul_rump;
255 p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
256 l->l_cred = rump_cred_suserget();
257 l->l_proc = p;
258 LIST_INIT(&allproc);
259 LIST_INSERT_HEAD(&allproc, &proc0, p_list);
260 proc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
261
262 rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
263 rump_limits.pl_rlimit[RLIMIT_NOFILE].rlim_cur = RLIM_INFINITY;
264 rump_limits.pl_rlimit[RLIMIT_SBSIZE].rlim_cur = RLIM_INFINITY;
265
266 rump_scheduler_init();
267 /* revert temporary context and schedule a real context */
268 l->l_cpu = NULL;
269 rumpuser_set_curlwp(NULL);
270 rump_schedule();
271
272 callout_startup();
273 callout_init_cpu(rump_cpu);
274 selsysinit(rump_cpu);
275
276 sysctl_init();
277 kqueue_init();
278 iostat_init();
279 uid_init();
280 percpu_init();
281 fd_sys_init();
282 module_init();
283 softint_init(rump_cpu);
284 devsw_init();
285
286 /* these do nothing if not present */
287 rump_vfs_init();
288 rump_net_init();
289 rump_dev_init();
290 cold = 0;
291
292 /* aieeeedondest */
293 if (rump_threads) {
294 if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
295 rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE))
296 panic("aiodoned");
297 }
298
299 sysctl_finalize();
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 #ifdef RUMP_USE_REAL_ALLOCATORS
309 if (rump_threads)
310 vmem_rehash_start();
311 #endif
312
313 rumpuser_dl_module_bootstrap(rump_module_init);
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 rump_proc_vfs_init(p);
443 p->p_stats = &rump_stats;
444 p->p_limit = &rump_limits;
445 p->p_pid = pid;
446 p->p_vmspace = &rump_vmspace;
447 p->p_fd = fd_init(NULL);
448 p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
449 l->l_cred = rump_cred_suserget();
450 } else {
451 p = &proc0;
452 l->l_cred = rump_susercred;
453 }
454
455 l->l_proc = p;
456 l->l_lid = lid;
457 l->l_fd = p->p_fd;
458 l->l_cpu = NULL;
459
460 return l;
461 }
462
463 void
464 rump_lwp_switch(struct lwp *newlwp)
465 {
466 struct lwp *l = curlwp;
467
468 rumpuser_set_curlwp(NULL);
469 newlwp->l_cpu = l->l_cpu;
470 newlwp->l_mutex = l->l_mutex;
471 l->l_mutex = NULL;
472 l->l_cpu = NULL;
473 rumpuser_set_curlwp(newlwp);
474 if (l->l_flag & LW_WEXIT)
475 rump_lwp_free(l);
476 }
477
478 /* XXX: this has effect only on non-pid0 lwps */
479 void
480 rump_lwp_release(struct lwp *l)
481 {
482 struct proc *p;
483
484 p = l->l_proc;
485 if (p->p_pid != 0) {
486 mutex_obj_free(p->p_lock);
487 fd_free();
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