rump.c revision 1.270 1 /* $NetBSD: rump.c,v 1.270 2013/05/31 16:16:40 pooka 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.270 2013/05/31 16:16:40 pooka 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 rump_tsleep_init();
286
287 /* init minimal lwp/cpu context */
288 l = &lwp0;
289 l->l_lid = 1;
290 l->l_cpu = l->l_target_cpu = rump_cpu;
291 l->l_fd = &filedesc0;
292
293 /* lwp0 isn't created like other threads, so notify hypervisor here */
294 rumpuser_curlwpop(RUMPUSER_LWP_CREATE, l);
295 rumpuser_curlwpop(RUMPUSER_LWP_SET, l);
296
297 rumpuser_mutex_init(&rump_giantlock, RUMPUSER_MTX_SPIN);
298 ksyms_init();
299 uvm_init();
300 evcnt_init();
301
302 kcpuset_sysinit();
303 once_init();
304 kernconfig_lock_init();
305 prop_kern_init();
306
307 kmem_init();
308 kmeminit();
309
310 uvm_ra_init();
311 uao_init();
312
313 mutex_obj_init();
314 callout_startup();
315
316 kprintf_init();
317 pserialize_init();
318 loginit();
319
320 kauth_init();
321
322 secmodel_init();
323
324 rnd_init();
325
326 /*
327 * Create the kernel cprng. Yes, it's currently stubbed out
328 * to arc4random() for RUMP, but this won't always be so.
329 */
330 kern_cprng = cprng_strong_create("kernel", IPL_VM,
331 CPRNG_INIT_ANY|CPRNG_REKEY_ANY);
332
333 procinit();
334 proc0_init();
335 sysctl_init();
336 uid_init();
337 chgproccnt(0, 1);
338
339 l->l_proc = &proc0;
340 lwp_update_creds(l);
341
342 lwpinit_specificdata();
343 lwp_initspecific(&lwp0);
344
345 rump_biglock_init();
346
347 rump_scheduler_init(numcpu);
348 /* revert temporary context and schedule a semireal context */
349 rumpuser_curlwpop(RUMPUSER_LWP_CLEAR, l);
350 initproc = &proc0; /* borrow proc0 before we get initproc started */
351 rump_schedule();
352 bootlwp = curlwp;
353
354 percpu_init();
355 inittimecounter();
356 ntp_init();
357
358 ts = boottime;
359 tc_setclock(&ts);
360
361 /* we are mostly go. do per-cpu subsystem init */
362 for (i = 0; i < numcpu; i++) {
363 struct cpu_info *ci = cpu_lookup(i);
364
365 /* attach non-bootstrap CPUs */
366 if (i > 0) {
367 rump_cpu_attach(ci);
368 ncpu++;
369 }
370
371 callout_init_cpu(ci);
372 softint_init(ci);
373 xc_init_cpu(ci);
374 pool_cache_cpu_init(ci);
375 selsysinit(ci);
376 percpu_init_cpu(ci);
377
378 TAILQ_INIT(&ci->ci_data.cpu_ld_locks);
379 __cpu_simple_lock_init(&ci->ci_data.cpu_ld_lock);
380
381 aprint_verbose("cpu%d at thinair0: rump virtual cpu\n", i);
382 }
383
384 /* CPUs are up. allow kernel threads to run */
385 rump_thread_allow();
386
387 mksysctls();
388 kqueue_init();
389 iostat_init();
390 fd_sys_init();
391 module_init();
392 devsw_init();
393 pipe_init();
394 resource_init();
395 procinit_sysctl();
396
397 /* start page baroness */
398 if (rump_threads) {
399 if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL,
400 uvm_pageout, NULL, &uvm.pagedaemon_lwp, "pdaemon") != 0)
401 panic("pagedaemon create failed");
402 } else
403 uvm.pagedaemon_lwp = NULL; /* doesn't match curlwp */
404
405 /* process dso's */
406 rumpuser_dl_bootstrap(add_linkedin_modules,
407 rump_kernelfsym_load, rump_component_load);
408
409 rump_component_addlocal();
410 rump_component_init(RUMP_COMPONENT_KERN);
411
412 /* initialize factions, if present */
413 rump_component_init(RUMP__FACTION_VFS);
414 /* pnbuf_cache is used even without vfs */
415 if (rump_component_count(RUMP__FACTION_VFS) == 0) {
416 pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
417 NULL, IPL_NONE, NULL, NULL, NULL);
418 }
419 rump_component_init(RUMP__FACTION_NET);
420 rump_component_init(RUMP__FACTION_DEV);
421 KASSERT(rump_component_count(RUMP__FACTION_VFS) <= 1
422 && rump_component_count(RUMP__FACTION_NET) <= 1
423 && rump_component_count(RUMP__FACTION_DEV) <= 1);
424
425 rump_component_init(RUMP_COMPONENT_KERN_VFS);
426
427 /*
428 * if we initialized the tty component above, the tyttymtx is
429 * now initialized. otherwise, we need to initialize it.
430 */
431 if (!rump_ttycomponent)
432 mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_VM);
433
434 cold = 0;
435
436 /* aieeeedondest */
437 if (rump_threads) {
438 if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
439 rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE))
440 panic("aiodoned");
441 }
442
443 sysctl_finalize();
444
445 module_init_class(MODULE_CLASS_ANY);
446
447 if (rumpuser_getparam(RUMPUSER_PARAM_HOSTNAME,
448 hostname, MAXHOSTNAMELEN) != 0) {
449 panic("mandatory hypervisor configuration (HOSTNAME) missing");
450 }
451 hostnamelen = strlen(hostname);
452
453 sigemptyset(&sigcantmask);
454
455 if (rump_threads)
456 vmem_rehash_start();
457
458 /*
459 * Create init, used to attach implicit threads in rump.
460 * (note: must be done after vfsinit to get cwdi)
461 */
462 (void)rump__lwproc_alloclwp(NULL); /* dummy thread for initproc */
463 mutex_enter(proc_lock);
464 initproc = proc_find_raw(1);
465 mutex_exit(proc_lock);
466 if (initproc == NULL)
467 panic("where in the world is initproc?");
468
469 /*
470 * Adjust syscall vector in case factions were dlopen()'d
471 * before calling rump_init().
472 * (modules will handle dynamic syscalls the usual way)
473 *
474 * Note: this will adjust the function vectors of
475 * syscalls which use a funcalias (getpid etc.), but
476 * it makes no difference.
477 */
478 for (i = 0; i < SYS_NSYSENT; i++) {
479 void *sym;
480
481 if (rump_sysent[i].sy_flags & SYCALL_NOSYS ||
482 *syscallnames[i] == '#' ||
483 rump_sysent[i].sy_call == sys_nomodule)
484 continue;
485
486 /*
487 * deal with compat wrappers. makesyscalls.sh should
488 * generate the necessary info instead of this hack,
489 * though. ugly, fix it later.
490 */
491 #define CPFX "compat_"
492 #define CPFXLEN (sizeof(CPFX)-1)
493 if (strncmp(syscallnames[i], CPFX, CPFXLEN) == 0) {
494 const char *p = syscallnames[i] + CPFXLEN;
495 size_t namelen;
496
497 /* skip version number */
498 while (*p >= '0' && *p <= '9')
499 p++;
500 if (p == syscallnames[i] + CPFXLEN || *p != '_')
501 panic("invalid syscall name %s\n",
502 syscallnames[i]);
503
504 /* skip over the next underscore */
505 p++;
506 namelen = p + (sizeof("rumpns_")-1) - syscallnames[i];
507
508 strcpy(buf, "rumpns_");
509 strcat(buf, syscallnames[i]);
510 /* XXX: no strncat in the kernel */
511 strcpy(buf+namelen, "sys_");
512 strcat(buf, p);
513 #undef CPFX
514 #undef CPFXLEN
515 } else {
516 sprintf(buf, "rumpns_sys_%s", syscallnames[i]);
517 }
518 if ((sym = rumpuser_dl_globalsym(buf)) != NULL
519 && sym != rump_sysent[i].sy_call) {
520 #if 0
521 rumpuser_dprintf("adjusting %s: %p (old %p)\n",
522 syscallnames[i], sym, rump_sysent[i].sy_call);
523 #endif
524 rump_sysent[i].sy_call = sym;
525 }
526 }
527
528 rump_component_init(RUMP_COMPONENT_POSTINIT);
529
530 /* release cpu */
531 bootlwp = NULL;
532 rump_unschedule();
533
534 return 0;
535 }
536 /* historic compat */
537 __strong_alias(rump__init,rump_init);
538
539 int
540 rump_init_server(const char *url)
541 {
542
543 return rumpuser_sp_init(url, ostype, osrelease, MACHINE);
544 }
545
546 void
547 cpu_reboot(int howto, char *bootstr)
548 {
549 int ruhow = 0;
550 void *finiarg;
551
552 printf("rump kernel halting...\n");
553
554 if (!RUMP_LOCALPROC_P(curproc))
555 finiarg = curproc->p_vmspace->vm_map.pmap;
556 else
557 finiarg = NULL;
558
559 /* dump means we really take the dive here */
560 if ((howto & RB_DUMP) || panicstr) {
561 ruhow = RUMPUSER_PANIC;
562 goto out;
563 }
564
565 /* try to sync */
566 if (!((howto & RB_NOSYNC) || panicstr)) {
567 if (rump_vfs_fini)
568 rump_vfs_fini();
569 }
570
571 doshutdownhooks();
572
573 /* your wish is my command */
574 if (howto & RB_HALT) {
575 printf("rump kernel halted\n");
576 rumpuser_sp_fini(finiarg);
577 for (;;) {
578 rumpuser_clock_sleep(RUMPUSER_CLOCK_RELWALL, 10, 0);
579 }
580 }
581
582 /* this function is __dead, we must exit */
583 out:
584 printf("halted\n");
585 rumpuser_sp_fini(finiarg);
586 rumpuser_exit(ruhow);
587 }
588
589 struct uio *
590 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
591 {
592 struct uio *uio;
593 enum uio_rw uiorw;
594
595 switch (rw) {
596 case RUMPUIO_READ:
597 uiorw = UIO_READ;
598 break;
599 case RUMPUIO_WRITE:
600 uiorw = UIO_WRITE;
601 break;
602 default:
603 panic("%s: invalid rw %d", __func__, rw);
604 }
605
606 uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
607 uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
608
609 uio->uio_iov->iov_base = buf;
610 uio->uio_iov->iov_len = bufsize;
611
612 uio->uio_iovcnt = 1;
613 uio->uio_offset = offset;
614 uio->uio_resid = bufsize;
615 uio->uio_rw = uiorw;
616 UIO_SETUP_SYSSPACE(uio);
617
618 return uio;
619 }
620
621 size_t
622 rump_uio_getresid(struct uio *uio)
623 {
624
625 return uio->uio_resid;
626 }
627
628 off_t
629 rump_uio_getoff(struct uio *uio)
630 {
631
632 return uio->uio_offset;
633 }
634
635 size_t
636 rump_uio_free(struct uio *uio)
637 {
638 size_t resid;
639
640 resid = uio->uio_resid;
641 kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
642 kmem_free(uio, sizeof(*uio));
643
644 return resid;
645 }
646
647 kauth_cred_t
648 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
649 {
650 kauth_cred_t cred;
651 int rv;
652
653 cred = kauth_cred_alloc();
654 kauth_cred_setuid(cred, uid);
655 kauth_cred_seteuid(cred, uid);
656 kauth_cred_setsvuid(cred, uid);
657 kauth_cred_setgid(cred, gid);
658 kauth_cred_setgid(cred, gid);
659 kauth_cred_setegid(cred, gid);
660 kauth_cred_setsvgid(cred, gid);
661 rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
662 /* oh this is silly. and by "this" I mean kauth_cred_setgroups() */
663 assert(rv == 0);
664
665 return cred;
666 }
667
668 void
669 rump_cred_put(kauth_cred_t cred)
670 {
671
672 kauth_cred_free(cred);
673 }
674
675 static int compcounter[RUMP_COMPONENT_MAX];
676 static int compinited[RUMP_COMPONENT_MAX];
677
678 /*
679 * Yea, this is O(n^2), but we're only looking at a handful of components.
680 * Components are always initialized from the thread that called rump_init().
681 * Could also free these when done with them, but prolly not worth it.
682 */
683 struct compstore {
684 const struct rump_component *cs_rc;
685 LIST_ENTRY(compstore) cs_entries;
686 };
687 static LIST_HEAD(, compstore) cshead = LIST_HEAD_INITIALIZER(cshead);
688
689 /*
690 * add components which are visible from the current object.
691 */
692 static void
693 rump_component_addlocal(void)
694 {
695 __link_set_decl(rump_components, struct rump_component);
696 struct rump_component *const *rc;
697
698 __link_set_foreach(rc, rump_components) {
699 rump_component_load(*rc);
700 }
701 }
702
703 static void
704 rump_component_load(const struct rump_component *rc)
705 {
706 struct compstore *cs;
707
708 KASSERT(curlwp == bootlwp);
709
710 LIST_FOREACH(cs, &cshead, cs_entries) {
711 if (rc == cs->cs_rc)
712 return;
713 }
714
715 cs = kmem_alloc(sizeof(*cs), KM_SLEEP);
716 cs->cs_rc = rc;
717 LIST_INSERT_HEAD(&cshead, cs, cs_entries);
718 KASSERT(rc->rc_type < RUMP_COMPONENT_MAX);
719 compcounter[rc->rc_type]++;
720 }
721
722 int
723 rump_component_count(enum rump_component_type type)
724 {
725
726 KASSERT(curlwp == bootlwp);
727 KASSERT(type < RUMP_COMPONENT_MAX);
728 return compcounter[type];
729 }
730
731 void
732 rump_component_init(enum rump_component_type type)
733 {
734 struct compstore *cs;
735 const struct rump_component *rc;
736
737 KASSERT(curlwp == bootlwp);
738 KASSERT(!compinited[type]);
739 LIST_FOREACH(cs, &cshead, cs_entries) {
740 rc = cs->cs_rc;
741 if (rc->rc_type == type)
742 rc->rc_init();
743 }
744 compinited[type] = 1;
745 }
746
747 /*
748 * Initialize a module which has already been loaded and linked
749 * with dlopen(). This is fundamentally the same as a builtin module.
750 */
751 int
752 rump_module_init(const struct modinfo * const *mip, size_t nmodinfo)
753 {
754
755 return module_builtin_add(mip, nmodinfo, true);
756 }
757
758 /*
759 * Finish module (flawless victory, fatality!).
760 */
761 int
762 rump_module_fini(const struct modinfo *mi)
763 {
764
765 return module_builtin_remove(mi, true);
766 }
767
768 /*
769 * Add loaded and linked module to the builtin list. It will
770 * later be initialized with module_init_class().
771 */
772
773 static void
774 add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo)
775 {
776
777 module_builtin_add(mip, nmodinfo, false);
778 }
779
780 int
781 rump_kernelfsym_load(void *symtab, uint64_t symsize,
782 char *strtab, uint64_t strsize)
783 {
784 static int inited = 0;
785 Elf64_Ehdr ehdr;
786
787 if (inited)
788 return EBUSY;
789 inited = 1;
790
791 /*
792 * Use 64bit header since it's bigger. Shouldn't make a
793 * difference, since we're passing in all zeroes anyway.
794 */
795 memset(&ehdr, 0, sizeof(ehdr));
796 ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize);
797
798 return 0;
799 }
800
801 static int
802 rump_hyp_syscall(int num, void *arg, long *retval)
803 {
804 register_t regrv[2] = {0, 0};
805 struct lwp *l;
806 struct sysent *callp;
807 int rv;
808
809 if (__predict_false(num >= SYS_NSYSENT))
810 return ENOSYS;
811
812 callp = rump_sysent + num;
813 l = curlwp;
814 rv = sy_call(callp, l, (void *)arg, regrv);
815 retval[0] = regrv[0];
816 retval[1] = regrv[1];
817
818 return rv;
819 }
820
821 static int
822 rump_hyp_rfork(void *priv, int flags, const char *comm)
823 {
824 struct vmspace *newspace;
825 struct proc *p;
826 int error;
827
828 if ((error = rump_lwproc_rfork(flags)) != 0)
829 return error;
830
831 /*
832 * Since it's a proxy proc, adjust the vmspace.
833 * Refcount will eternally be 1.
834 */
835 p = curproc;
836 newspace = kmem_zalloc(sizeof(*newspace), KM_SLEEP);
837 newspace->vm_refcnt = 1;
838 newspace->vm_map.pmap = priv;
839 KASSERT(p->p_vmspace == vmspace_kernel());
840 p->p_vmspace = newspace;
841 if (comm)
842 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
843
844 return 0;
845 }
846
847 /*
848 * Order all lwps in a process to exit. does *not* wait for them to drain.
849 */
850 static void
851 rump_hyp_lwpexit(void)
852 {
853 struct proc *p = curproc;
854 uint64_t where;
855 struct lwp *l;
856
857 mutex_enter(p->p_lock);
858 /*
859 * First pass: mark all lwps in the process with LW_RUMP_QEXIT
860 * so that they know they should exit.
861 */
862 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
863 if (l == curlwp)
864 continue;
865 l->l_flag |= LW_RUMP_QEXIT;
866 }
867 mutex_exit(p->p_lock);
868
869 /*
870 * Next, make sure everyone on all CPUs sees our status
871 * update. This keeps threads inside cv_wait() and makes
872 * sure we don't access a stale cv pointer later when
873 * we wake up the threads.
874 */
875
876 where = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
877 xc_wait(where);
878
879 /*
880 * Ok, all lwps are either:
881 * 1) not in the cv code
882 * 2) sleeping on l->l_private
883 * 3) sleeping on p->p_waitcv
884 *
885 * Either way, l_private is stable until we set PS_RUMP_LWPEXIT
886 * in p->p_sflag.
887 */
888
889 mutex_enter(p->p_lock);
890 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
891 if (l->l_private)
892 cv_broadcast(l->l_private);
893 }
894 p->p_sflag |= PS_RUMP_LWPEXIT;
895 cv_broadcast(&p->p_waitcv);
896 mutex_exit(p->p_lock);
897 }
898
899 /*
900 * Notify process that all threads have been drained and exec is complete.
901 */
902 static void
903 rump_hyp_execnotify(const char *comm)
904 {
905 struct proc *p = curproc;
906
907 fd_closeexec();
908 mutex_enter(p->p_lock);
909 KASSERT(p->p_nlwps == 1 && p->p_sflag & PS_RUMP_LWPEXIT);
910 p->p_sflag &= ~PS_RUMP_LWPEXIT;
911 mutex_exit(p->p_lock);
912 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
913 }
914
915 int
916 rump_boot_gethowto()
917 {
918
919 return boothowto;
920 }
921
922 void
923 rump_boot_sethowto(int howto)
924 {
925
926 boothowto = howto;
927 }
928
929 int
930 rump_getversion(void)
931 {
932
933 return __NetBSD_Version__;
934 }
935
936 /*
937 * Note: may be called unscheduled. Not fully safe since no locking
938 * of allevents (currently that's not even available).
939 */
940 void
941 rump_printevcnts()
942 {
943 struct evcnt *ev;
944
945 TAILQ_FOREACH(ev, &allevents, ev_list)
946 rumpuser_dprintf("%s / %s: %" PRIu64 "\n",
947 ev->ev_group, ev->ev_name, ev->ev_count);
948 }
949
950 /*
951 * If you use this interface ... well ... all bets are off.
952 * The original purpose is for the p2k fs server library to be
953 * able to use the same pid/lid for VOPs as the host kernel.
954 */
955 void
956 rump_allbetsareoff_setid(pid_t pid, int lid)
957 {
958 struct lwp *l = curlwp;
959 struct proc *p = l->l_proc;
960
961 l->l_lid = lid;
962 p->p_pid = pid;
963 }
964
965 #include <sys/pserialize.h>
966
967 static void
968 ipiemu(void *a1, void *a2)
969 {
970
971 xc__highpri_intr(NULL);
972 pserialize_switchpoint();
973 }
974
975 void
976 rump_xc_highpri(struct cpu_info *ci)
977 {
978
979 if (ci)
980 xc_unicast(0, ipiemu, NULL, NULL, ci);
981 else
982 xc_broadcast(0, ipiemu, NULL, NULL);
983 }
984