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