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