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