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