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