rump.c revision 1.271 1 /* $NetBSD: rump.c,v 1.271 2013/07/03 17:10:28 njoly Exp $ */
2
3 /*
4 * Copyright (c) 2007-2011 Antti Kantee. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: rump.c,v 1.271 2013/07/03 17:10:28 njoly Exp $");
30
31 #include <sys/systm.h>
32 #define ELFSIZE ARCH_ELFSIZE
33
34 #include <sys/param.h>
35 #include <sys/atomic.h>
36 #include <sys/buf.h>
37 #include <sys/callout.h>
38 #include <sys/conf.h>
39 #include <sys/cpu.h>
40 #include <sys/device.h>
41 #include <sys/evcnt.h>
42 #include <sys/event.h>
43 #include <sys/exec_elf.h>
44 #include <sys/filedesc.h>
45 #include <sys/iostat.h>
46 #include <sys/kauth.h>
47 #include <sys/kcpuset.h>
48 #include <sys/kernel.h>
49 #include <sys/kmem.h>
50 #include <sys/kprintf.h>
51 #include <sys/kthread.h>
52 #include <sys/ksyms.h>
53 #include <sys/msgbuf.h>
54 #include <sys/module.h>
55 #include <sys/namei.h>
56 #include <sys/once.h>
57 #include <sys/percpu.h>
58 #include <sys/pipe.h>
59 #include <sys/pool.h>
60 #include <sys/pserialize.h>
61 #include <sys/queue.h>
62 #include <sys/reboot.h>
63 #include <sys/resourcevar.h>
64 #include <sys/select.h>
65 #include <sys/sysctl.h>
66 #include <sys/syscall.h>
67 #include <sys/syscallvar.h>
68 #include <sys/timetc.h>
69 #include <sys/tty.h>
70 #include <sys/uidinfo.h>
71 #include <sys/vmem.h>
72 #include <sys/xcall.h>
73 #include <sys/simplelock.h>
74 #include <sys/cprng.h>
75
76 #include <rump/rumpuser.h>
77
78 #include <secmodel/suser/suser.h>
79
80 #include <prop/proplib.h>
81
82 #include <uvm/uvm_extern.h>
83 #include <uvm/uvm_readahead.h>
84
85 #include "rump_private.h"
86 #include "rump_net_private.h"
87 #include "rump_vfs_private.h"
88 #include "rump_dev_private.h"
89
90 char machine[] = MACHINE;
91
92 struct proc *initproc;
93
94 struct device rump_rootdev = {
95 .dv_class = DV_VIRTUAL
96 };
97
98 #ifdef RUMP_WITHOUT_THREADS
99 int rump_threads = 0;
100 #else
101 int rump_threads = 1;
102 #endif
103
104 static int rump_hyp_syscall(int, void *, long *);
105 static int rump_hyp_rfork(void *, int, const char *);
106 static void rump_hyp_lwpexit(void);
107 static void rump_hyp_execnotify(const char *);
108
109 static void rump_component_addlocal(void);
110 static void rump_component_load(const struct rump_component *);
111 static struct lwp *bootlwp;
112
113 static char rump_msgbuf[16*1024]; /* 16k should be enough for std rump needs */
114
115 #ifdef LOCKDEBUG
116 const int rump_lockdebug = 1;
117 #else
118 const int rump_lockdebug = 0;
119 #endif
120 bool rump_ttycomponent = false;
121
122 static void
123 rump_aiodone_worker(struct work *wk, void *dummy)
124 {
125 struct buf *bp = (struct buf *)wk;
126
127 KASSERT(&bp->b_work == wk);
128 bp->b_iodone(bp);
129 }
130
131 static int rump_inited;
132
133 void (*rump_vfs_drainbufs)(int);
134 void (*rump_vfs_fini)(void);
135 int (*rump_vfs_makeonedevnode)(dev_t, const char *,
136 devmajor_t, devminor_t) = (void *)nullop;
137 int (*rump_vfs_makedevnodes)(dev_t, const char *, char,
138 devmajor_t, devminor_t, int) = (void *)nullop;
139
140 int rump__unavailable(void);
141 int rump__unavailable() {return EOPNOTSUPP;}
142
143 __weak_alias(biodone,rump__unavailable);
144 __weak_alias(sopoll,rump__unavailable);
145
146 void rump__unavailable_vfs_panic(void);
147 void rump__unavailable_vfs_panic() {panic("vfs component not available");}
148 __weak_alias(usermount_common_policy,rump__unavailable_vfs_panic);
149
150 /* easier to write vfs-less clients */
151 __weak_alias(rump_pub_etfs_register,rump__unavailable);
152 __weak_alias(rump_pub_etfs_register_withsize,rump__unavailable);
153 __weak_alias(rump_pub_etfs_remove,rump__unavailable);
154
155 rump_proc_vfs_init_fn rump_proc_vfs_init;
156 rump_proc_vfs_release_fn rump_proc_vfs_release;
157
158 static void add_linkedin_modules(const struct modinfo *const *, size_t);
159
160 /*
161 * Create kern.hostname. why only this you ask. well, init_sysctl
162 * is a kitchen sink in need of some gardening. but i want to use
163 * kern.hostname today.
164 */
165 static void
166 mksysctls(void)
167 {
168
169 sysctl_createv(NULL, 0, NULL, NULL,
170 CTLFLAG_PERMANENT, CTLTYPE_NODE, "kern", NULL,
171 NULL, 0, NULL, 0, CTL_KERN, CTL_EOL);
172
173 /* XXX: setting hostnamelen is missing */
174 sysctl_createv(NULL, 0, NULL, NULL,
175 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_STRING, "hostname",
176 SYSCTL_DESCR("System hostname"), NULL, 0,
177 hostname, MAXHOSTNAMELEN, CTL_KERN, KERN_HOSTNAME, CTL_EOL);
178 }
179
180 /* there's no convenient kernel entry point for this, so just craft out own */
181 static pid_t
182 spgetpid(void)
183 {
184
185 return curproc->p_pid;
186 }
187
188 static const struct rumpuser_hyperup hyp = {
189 .hyp_schedule = rump_schedule,
190 .hyp_unschedule = rump_unschedule,
191 .hyp_backend_unschedule = rump_user_unschedule,
192 .hyp_backend_schedule = rump_user_schedule,
193 .hyp_lwproc_switch = rump_lwproc_switch,
194 .hyp_lwproc_release = rump_lwproc_releaselwp,
195 .hyp_lwproc_rfork = rump_hyp_rfork,
196 .hyp_lwproc_newlwp = rump_lwproc_newlwp,
197 .hyp_lwproc_curlwp = rump_lwproc_curlwp,
198 .hyp_lwpexit = rump_hyp_lwpexit,
199 .hyp_syscall = rump_hyp_syscall,
200 .hyp_execnotify = rump_hyp_execnotify,
201 .hyp_getpid = spgetpid,
202 };
203
204 int
205 rump_daemonize_begin(void)
206 {
207
208 if (rump_inited)
209 return EALREADY;
210
211 return rumpuser_daemonize_begin();
212 }
213
214 int
215 rump_daemonize_done(int error)
216 {
217
218 return rumpuser_daemonize_done(error);
219 }
220
221 RUMP_COMPONENT(RUMP_COMPONENT_POSTINIT)
222 {
223 __link_set_decl(rump_components, struct rump_component);
224
225 /*
226 * Trick compiler into generating references so that statically
227 * linked rump kernels are generated with the link set symbols.
228 */
229 asm("" :: "r"(__start_link_set_rump_components));
230 asm("" :: "r"(__stop_link_set_rump_components));
231 }
232
233 int
234 rump_init(void)
235 {
236 char buf[256];
237 struct timespec ts;
238 int64_t sec;
239 long nsec;
240 struct lwp *l;
241 int i, numcpu;
242
243 /* not reentrant */
244 if (rump_inited)
245 return 0;
246 else if (rump_inited == -1)
247 panic("rump_init: host process restart required");
248 else
249 rump_inited = 1;
250
251 /* initialize hypervisor */
252 if (rumpuser_init(RUMPUSER_VERSION, &hyp) != 0) {
253 rumpuser_dprintf("rumpuser init failed\n");
254 return EINVAL;
255 }
256
257 /* retrieve env vars which affect the early stage of bootstrap */
258 if (rumpuser_getparam("RUMP_THREADS", buf, sizeof(buf)) == 0) {
259 rump_threads = *buf != '0';
260 }
261 if (rumpuser_getparam("RUMP_VERBOSE", buf, sizeof(buf)) == 0) {
262 if (*buf != '0')
263 boothowto = AB_VERBOSE;
264 }
265
266 if (rumpuser_getparam(RUMPUSER_PARAM_NCPU, buf, sizeof(buf)) != 0)
267 panic("mandatory hypervisor configuration (NCPU) missing");
268 numcpu = strtoll(buf, NULL, 10);
269 if (numcpu < 1) {
270 panic("rump kernels are not lightweight enough for \"%d\" CPUs",
271 numcpu);
272 }
273
274 rump_thread_init();
275 rump_cpus_bootstrap(&numcpu);
276
277 rumpuser_clock_gettime(RUMPUSER_CLOCK_RELWALL, &sec, &nsec);
278 boottime.tv_sec = sec;
279 boottime.tv_nsec = nsec;
280
281 initmsgbuf(rump_msgbuf, sizeof(rump_msgbuf));
282 aprint_verbose("%s%s", copyright, version);
283
284 rump_intr_init(numcpu);
285
286 /* init minimal lwp/cpu context */
287 l = &lwp0;
288 l->l_lid = 1;
289 l->l_cpu = l->l_target_cpu = rump_cpu;
290 l->l_fd = &filedesc0;
291
292 /* lwp0 isn't created like other threads, so notify hypervisor here */
293 rumpuser_curlwpop(RUMPUSER_LWP_CREATE, l);
294 rumpuser_curlwpop(RUMPUSER_LWP_SET, l);
295
296 rump_tsleep_init();
297
298 rumpuser_mutex_init(&rump_giantlock, RUMPUSER_MTX_SPIN);
299 ksyms_init();
300 uvm_init();
301 evcnt_init();
302
303 kcpuset_sysinit();
304 once_init();
305 kernconfig_lock_init();
306 prop_kern_init();
307
308 kmem_init();
309 kmeminit();
310
311 uvm_ra_init();
312 uao_init();
313
314 mutex_obj_init();
315 callout_startup();
316
317 kprintf_init();
318 pserialize_init();
319 loginit();
320
321 kauth_init();
322
323 secmodel_init();
324
325 rnd_init();
326
327 /*
328 * Create the kernel cprng. Yes, it's currently stubbed out
329 * to arc4random() for RUMP, but this won't always be so.
330 */
331 kern_cprng = cprng_strong_create("kernel", IPL_VM,
332 CPRNG_INIT_ANY|CPRNG_REKEY_ANY);
333
334 procinit();
335 proc0_init();
336 sysctl_init();
337 uid_init();
338 chgproccnt(0, 1);
339
340 l->l_proc = &proc0;
341 lwp_update_creds(l);
342
343 lwpinit_specificdata();
344 lwp_initspecific(&lwp0);
345
346 rump_biglock_init();
347
348 rump_scheduler_init(numcpu);
349 /* revert temporary context and schedule a semireal context */
350 rumpuser_curlwpop(RUMPUSER_LWP_CLEAR, l);
351 initproc = &proc0; /* borrow proc0 before we get initproc started */
352 rump_schedule();
353 bootlwp = curlwp;
354
355 percpu_init();
356 inittimecounter();
357 ntp_init();
358
359 ts = boottime;
360 tc_setclock(&ts);
361
362 /* we are mostly go. do per-cpu subsystem init */
363 for (i = 0; i < numcpu; i++) {
364 struct cpu_info *ci = cpu_lookup(i);
365
366 /* attach non-bootstrap CPUs */
367 if (i > 0) {
368 rump_cpu_attach(ci);
369 ncpu++;
370 }
371
372 callout_init_cpu(ci);
373 softint_init(ci);
374 xc_init_cpu(ci);
375 pool_cache_cpu_init(ci);
376 selsysinit(ci);
377 percpu_init_cpu(ci);
378
379 TAILQ_INIT(&ci->ci_data.cpu_ld_locks);
380 __cpu_simple_lock_init(&ci->ci_data.cpu_ld_lock);
381
382 aprint_verbose("cpu%d at thinair0: rump virtual cpu\n", i);
383 }
384
385 /* CPUs are up. allow kernel threads to run */
386 rump_thread_allow();
387
388 mksysctls();
389 kqueue_init();
390 iostat_init();
391 fd_sys_init();
392 module_init();
393 devsw_init();
394 pipe_init();
395 resource_init();
396 procinit_sysctl();
397
398 /* start page baroness */
399 if (rump_threads) {
400 if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL,
401 uvm_pageout, NULL, &uvm.pagedaemon_lwp, "pdaemon") != 0)
402 panic("pagedaemon create failed");
403 } else
404 uvm.pagedaemon_lwp = NULL; /* doesn't match curlwp */
405
406 /* process dso's */
407 rumpuser_dl_bootstrap(add_linkedin_modules,
408 rump_kernelfsym_load, rump_component_load);
409
410 rump_component_addlocal();
411 rump_component_init(RUMP_COMPONENT_KERN);
412
413 /* initialize factions, if present */
414 rump_component_init(RUMP__FACTION_VFS);
415 /* pnbuf_cache is used even without vfs */
416 if (rump_component_count(RUMP__FACTION_VFS) == 0) {
417 pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
418 NULL, IPL_NONE, NULL, NULL, NULL);
419 }
420 rump_component_init(RUMP__FACTION_NET);
421 rump_component_init(RUMP__FACTION_DEV);
422 KASSERT(rump_component_count(RUMP__FACTION_VFS) <= 1
423 && rump_component_count(RUMP__FACTION_NET) <= 1
424 && rump_component_count(RUMP__FACTION_DEV) <= 1);
425
426 rump_component_init(RUMP_COMPONENT_KERN_VFS);
427
428 /*
429 * if we initialized the tty component above, the tyttymtx is
430 * now initialized. otherwise, we need to initialize it.
431 */
432 if (!rump_ttycomponent)
433 mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_VM);
434
435 cold = 0;
436
437 /* aieeeedondest */
438 if (rump_threads) {
439 if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
440 rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE))
441 panic("aiodoned");
442 }
443
444 sysctl_finalize();
445
446 module_init_class(MODULE_CLASS_ANY);
447
448 if (rumpuser_getparam(RUMPUSER_PARAM_HOSTNAME,
449 hostname, MAXHOSTNAMELEN) != 0) {
450 panic("mandatory hypervisor configuration (HOSTNAME) missing");
451 }
452 hostnamelen = strlen(hostname);
453
454 sigemptyset(&sigcantmask);
455
456 if (rump_threads)
457 vmem_rehash_start();
458
459 /*
460 * Create init, used to attach implicit threads in rump.
461 * (note: must be done after vfsinit to get cwdi)
462 */
463 (void)rump__lwproc_alloclwp(NULL); /* dummy thread for initproc */
464 mutex_enter(proc_lock);
465 initproc = proc_find_raw(1);
466 mutex_exit(proc_lock);
467 if (initproc == NULL)
468 panic("where in the world is initproc?");
469
470 /*
471 * Adjust syscall vector in case factions were dlopen()'d
472 * before calling rump_init().
473 * (modules will handle dynamic syscalls the usual way)
474 *
475 * Note: this will adjust the function vectors of
476 * syscalls which use a funcalias (getpid etc.), but
477 * it makes no difference.
478 */
479 for (i = 0; i < SYS_NSYSENT; i++) {
480 void *sym;
481
482 if (rump_sysent[i].sy_flags & SYCALL_NOSYS ||
483 *syscallnames[i] == '#' ||
484 rump_sysent[i].sy_call == sys_nomodule)
485 continue;
486
487 /*
488 * deal with compat wrappers. makesyscalls.sh should
489 * generate the necessary info instead of this hack,
490 * though. ugly, fix it later.
491 */
492 #define CPFX "compat_"
493 #define CPFXLEN (sizeof(CPFX)-1)
494 if (strncmp(syscallnames[i], CPFX, CPFXLEN) == 0) {
495 const char *p = syscallnames[i] + CPFXLEN;
496 size_t namelen;
497
498 /* skip version number */
499 while (*p >= '0' && *p <= '9')
500 p++;
501 if (p == syscallnames[i] + CPFXLEN || *p != '_')
502 panic("invalid syscall name %s\n",
503 syscallnames[i]);
504
505 /* skip over the next underscore */
506 p++;
507 namelen = p + (sizeof("rumpns_")-1) - syscallnames[i];
508
509 strcpy(buf, "rumpns_");
510 strcat(buf, syscallnames[i]);
511 /* XXX: no strncat in the kernel */
512 strcpy(buf+namelen, "sys_");
513 strcat(buf, p);
514 #undef CPFX
515 #undef CPFXLEN
516 } else {
517 sprintf(buf, "rumpns_sys_%s", syscallnames[i]);
518 }
519 if ((sym = rumpuser_dl_globalsym(buf)) != NULL
520 && sym != rump_sysent[i].sy_call) {
521 #if 0
522 rumpuser_dprintf("adjusting %s: %p (old %p)\n",
523 syscallnames[i], sym, rump_sysent[i].sy_call);
524 #endif
525 rump_sysent[i].sy_call = sym;
526 }
527 }
528
529 rump_component_init(RUMP_COMPONENT_POSTINIT);
530
531 /* release cpu */
532 bootlwp = NULL;
533 rump_unschedule();
534
535 return 0;
536 }
537 /* historic compat */
538 __strong_alias(rump__init,rump_init);
539
540 int
541 rump_init_server(const char *url)
542 {
543
544 return rumpuser_sp_init(url, ostype, osrelease, MACHINE);
545 }
546
547 void
548 cpu_reboot(int howto, char *bootstr)
549 {
550 int ruhow = 0;
551 void *finiarg;
552
553 printf("rump kernel halting...\n");
554
555 if (!RUMP_LOCALPROC_P(curproc))
556 finiarg = curproc->p_vmspace->vm_map.pmap;
557 else
558 finiarg = NULL;
559
560 /* dump means we really take the dive here */
561 if ((howto & RB_DUMP) || panicstr) {
562 ruhow = RUMPUSER_PANIC;
563 goto out;
564 }
565
566 /* try to sync */
567 if (!((howto & RB_NOSYNC) || panicstr)) {
568 if (rump_vfs_fini)
569 rump_vfs_fini();
570 }
571
572 doshutdownhooks();
573
574 /* your wish is my command */
575 if (howto & RB_HALT) {
576 printf("rump kernel halted\n");
577 rumpuser_sp_fini(finiarg);
578 for (;;) {
579 rumpuser_clock_sleep(RUMPUSER_CLOCK_RELWALL, 10, 0);
580 }
581 }
582
583 /* this function is __dead, we must exit */
584 out:
585 printf("halted\n");
586 rumpuser_sp_fini(finiarg);
587 rumpuser_exit(ruhow);
588 }
589
590 struct uio *
591 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
592 {
593 struct uio *uio;
594 enum uio_rw uiorw;
595
596 switch (rw) {
597 case RUMPUIO_READ:
598 uiorw = UIO_READ;
599 break;
600 case RUMPUIO_WRITE:
601 uiorw = UIO_WRITE;
602 break;
603 default:
604 panic("%s: invalid rw %d", __func__, rw);
605 }
606
607 uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
608 uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
609
610 uio->uio_iov->iov_base = buf;
611 uio->uio_iov->iov_len = bufsize;
612
613 uio->uio_iovcnt = 1;
614 uio->uio_offset = offset;
615 uio->uio_resid = bufsize;
616 uio->uio_rw = uiorw;
617 UIO_SETUP_SYSSPACE(uio);
618
619 return uio;
620 }
621
622 size_t
623 rump_uio_getresid(struct uio *uio)
624 {
625
626 return uio->uio_resid;
627 }
628
629 off_t
630 rump_uio_getoff(struct uio *uio)
631 {
632
633 return uio->uio_offset;
634 }
635
636 size_t
637 rump_uio_free(struct uio *uio)
638 {
639 size_t resid;
640
641 resid = uio->uio_resid;
642 kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
643 kmem_free(uio, sizeof(*uio));
644
645 return resid;
646 }
647
648 kauth_cred_t
649 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
650 {
651 kauth_cred_t cred;
652 int rv;
653
654 cred = kauth_cred_alloc();
655 kauth_cred_setuid(cred, uid);
656 kauth_cred_seteuid(cred, uid);
657 kauth_cred_setsvuid(cred, uid);
658 kauth_cred_setgid(cred, gid);
659 kauth_cred_setgid(cred, gid);
660 kauth_cred_setegid(cred, gid);
661 kauth_cred_setsvgid(cred, gid);
662 rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
663 /* oh this is silly. and by "this" I mean kauth_cred_setgroups() */
664 assert(rv == 0);
665
666 return cred;
667 }
668
669 void
670 rump_cred_put(kauth_cred_t cred)
671 {
672
673 kauth_cred_free(cred);
674 }
675
676 static int compcounter[RUMP_COMPONENT_MAX];
677 static int compinited[RUMP_COMPONENT_MAX];
678
679 /*
680 * Yea, this is O(n^2), but we're only looking at a handful of components.
681 * Components are always initialized from the thread that called rump_init().
682 * Could also free these when done with them, but prolly not worth it.
683 */
684 struct compstore {
685 const struct rump_component *cs_rc;
686 LIST_ENTRY(compstore) cs_entries;
687 };
688 static LIST_HEAD(, compstore) cshead = LIST_HEAD_INITIALIZER(cshead);
689
690 /*
691 * add components which are visible from the current object.
692 */
693 static void
694 rump_component_addlocal(void)
695 {
696 __link_set_decl(rump_components, struct rump_component);
697 struct rump_component *const *rc;
698
699 __link_set_foreach(rc, rump_components) {
700 rump_component_load(*rc);
701 }
702 }
703
704 static void
705 rump_component_load(const struct rump_component *rc)
706 {
707 struct compstore *cs;
708
709 KASSERT(curlwp == bootlwp);
710
711 LIST_FOREACH(cs, &cshead, cs_entries) {
712 if (rc == cs->cs_rc)
713 return;
714 }
715
716 cs = kmem_alloc(sizeof(*cs), KM_SLEEP);
717 cs->cs_rc = rc;
718 LIST_INSERT_HEAD(&cshead, cs, cs_entries);
719 KASSERT(rc->rc_type < RUMP_COMPONENT_MAX);
720 compcounter[rc->rc_type]++;
721 }
722
723 int
724 rump_component_count(enum rump_component_type type)
725 {
726
727 KASSERT(curlwp == bootlwp);
728 KASSERT(type < RUMP_COMPONENT_MAX);
729 return compcounter[type];
730 }
731
732 void
733 rump_component_init(enum rump_component_type type)
734 {
735 struct compstore *cs;
736 const struct rump_component *rc;
737
738 KASSERT(curlwp == bootlwp);
739 KASSERT(!compinited[type]);
740 LIST_FOREACH(cs, &cshead, cs_entries) {
741 rc = cs->cs_rc;
742 if (rc->rc_type == type)
743 rc->rc_init();
744 }
745 compinited[type] = 1;
746 }
747
748 /*
749 * Initialize a module which has already been loaded and linked
750 * with dlopen(). This is fundamentally the same as a builtin module.
751 */
752 int
753 rump_module_init(const struct modinfo * const *mip, size_t nmodinfo)
754 {
755
756 return module_builtin_add(mip, nmodinfo, true);
757 }
758
759 /*
760 * Finish module (flawless victory, fatality!).
761 */
762 int
763 rump_module_fini(const struct modinfo *mi)
764 {
765
766 return module_builtin_remove(mi, true);
767 }
768
769 /*
770 * Add loaded and linked module to the builtin list. It will
771 * later be initialized with module_init_class().
772 */
773
774 static void
775 add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo)
776 {
777
778 module_builtin_add(mip, nmodinfo, false);
779 }
780
781 int
782 rump_kernelfsym_load(void *symtab, uint64_t symsize,
783 char *strtab, uint64_t strsize)
784 {
785 static int inited = 0;
786 Elf64_Ehdr ehdr;
787
788 if (inited)
789 return EBUSY;
790 inited = 1;
791
792 /*
793 * Use 64bit header since it's bigger. Shouldn't make a
794 * difference, since we're passing in all zeroes anyway.
795 */
796 memset(&ehdr, 0, sizeof(ehdr));
797 ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize);
798
799 return 0;
800 }
801
802 static int
803 rump_hyp_syscall(int num, void *arg, long *retval)
804 {
805 register_t regrv[2] = {0, 0};
806 struct lwp *l;
807 struct sysent *callp;
808 int rv;
809
810 if (__predict_false(num >= SYS_NSYSENT))
811 return ENOSYS;
812
813 callp = rump_sysent + num;
814 l = curlwp;
815 rv = sy_call(callp, l, (void *)arg, regrv);
816 retval[0] = regrv[0];
817 retval[1] = regrv[1];
818
819 return rv;
820 }
821
822 static int
823 rump_hyp_rfork(void *priv, int flags, const char *comm)
824 {
825 struct vmspace *newspace;
826 struct proc *p;
827 int error;
828
829 if ((error = rump_lwproc_rfork(flags)) != 0)
830 return error;
831
832 /*
833 * Since it's a proxy proc, adjust the vmspace.
834 * Refcount will eternally be 1.
835 */
836 p = curproc;
837 newspace = kmem_zalloc(sizeof(*newspace), KM_SLEEP);
838 newspace->vm_refcnt = 1;
839 newspace->vm_map.pmap = priv;
840 KASSERT(p->p_vmspace == vmspace_kernel());
841 p->p_vmspace = newspace;
842 if (comm)
843 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
844
845 return 0;
846 }
847
848 /*
849 * Order all lwps in a process to exit. does *not* wait for them to drain.
850 */
851 static void
852 rump_hyp_lwpexit(void)
853 {
854 struct proc *p = curproc;
855 uint64_t where;
856 struct lwp *l;
857
858 mutex_enter(p->p_lock);
859 /*
860 * First pass: mark all lwps in the process with LW_RUMP_QEXIT
861 * so that they know they should exit.
862 */
863 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
864 if (l == curlwp)
865 continue;
866 l->l_flag |= LW_RUMP_QEXIT;
867 }
868 mutex_exit(p->p_lock);
869
870 /*
871 * Next, make sure everyone on all CPUs sees our status
872 * update. This keeps threads inside cv_wait() and makes
873 * sure we don't access a stale cv pointer later when
874 * we wake up the threads.
875 */
876
877 where = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
878 xc_wait(where);
879
880 /*
881 * Ok, all lwps are either:
882 * 1) not in the cv code
883 * 2) sleeping on l->l_private
884 * 3) sleeping on p->p_waitcv
885 *
886 * Either way, l_private is stable until we set PS_RUMP_LWPEXIT
887 * in p->p_sflag.
888 */
889
890 mutex_enter(p->p_lock);
891 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
892 if (l->l_private)
893 cv_broadcast(l->l_private);
894 }
895 p->p_sflag |= PS_RUMP_LWPEXIT;
896 cv_broadcast(&p->p_waitcv);
897 mutex_exit(p->p_lock);
898 }
899
900 /*
901 * Notify process that all threads have been drained and exec is complete.
902 */
903 static void
904 rump_hyp_execnotify(const char *comm)
905 {
906 struct proc *p = curproc;
907
908 fd_closeexec();
909 mutex_enter(p->p_lock);
910 KASSERT(p->p_nlwps == 1 && p->p_sflag & PS_RUMP_LWPEXIT);
911 p->p_sflag &= ~PS_RUMP_LWPEXIT;
912 mutex_exit(p->p_lock);
913 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
914 }
915
916 int
917 rump_boot_gethowto()
918 {
919
920 return boothowto;
921 }
922
923 void
924 rump_boot_sethowto(int howto)
925 {
926
927 boothowto = howto;
928 }
929
930 int
931 rump_getversion(void)
932 {
933
934 return __NetBSD_Version__;
935 }
936
937 /*
938 * Note: may be called unscheduled. Not fully safe since no locking
939 * of allevents (currently that's not even available).
940 */
941 void
942 rump_printevcnts()
943 {
944 struct evcnt *ev;
945
946 TAILQ_FOREACH(ev, &allevents, ev_list)
947 rumpuser_dprintf("%s / %s: %" PRIu64 "\n",
948 ev->ev_group, ev->ev_name, ev->ev_count);
949 }
950
951 /*
952 * If you use this interface ... well ... all bets are off.
953 * The original purpose is for the p2k fs server library to be
954 * able to use the same pid/lid for VOPs as the host kernel.
955 */
956 void
957 rump_allbetsareoff_setid(pid_t pid, int lid)
958 {
959 struct lwp *l = curlwp;
960 struct proc *p = l->l_proc;
961
962 l->l_lid = lid;
963 p->p_pid = pid;
964 }
965
966 #include <sys/pserialize.h>
967
968 static void
969 ipiemu(void *a1, void *a2)
970 {
971
972 xc__highpri_intr(NULL);
973 pserialize_switchpoint();
974 }
975
976 void
977 rump_xc_highpri(struct cpu_info *ci)
978 {
979
980 if (ci)
981 xc_unicast(0, ipiemu, NULL, NULL, ci);
982 else
983 xc_broadcast(0, ipiemu, NULL, NULL);
984 }
985