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