rump.c revision 1.289 1 /* $NetBSD: rump.c,v 1.289 2014/03/10 22:44:11 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.289 2014/03/10 22:44:11 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/cprng.h>
74 #include <sys/ktrace.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 /* hw.pagesize */
171 sysctl_createv(NULL, 0, NULL, NULL,
172 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
173 CTLTYPE_INT, "pagesize",
174 SYSCTL_DESCR("Software page size"),
175 NULL, PAGE_SIZE, NULL, 0,
176 CTL_HW, HW_PAGESIZE, 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 __link_set_decl(rump_components, struct rump_component);
223
224 /*
225 * Trick compiler into generating references so that statically
226 * linked rump kernels are generated with the link set symbols.
227 */
228 asm("" :: "r"(__start_link_set_rump_components));
229 asm("" :: "r"(__stop_link_set_rump_components));
230 }
231
232 int
233 rump_init(void)
234 {
235 char buf[256];
236 struct timespec ts;
237 int64_t sec;
238 long nsec;
239 struct lwp *l, *initlwp;
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 /* init minimal lwp/cpu context */
257 l = &lwp0;
258 l->l_cpu = l->l_target_cpu = rump_cpu;
259
260 /* lwp0 isn't created like other threads, so notify hypervisor here */
261 rumpuser_curlwpop(RUMPUSER_LWP_CREATE, l);
262 rumpuser_curlwpop(RUMPUSER_LWP_SET, l);
263
264 /* retrieve env vars which affect the early stage of bootstrap */
265 if (rumpuser_getparam("RUMP_THREADS", buf, sizeof(buf)) == 0) {
266 rump_threads = *buf != '0';
267 }
268 if (rumpuser_getparam("RUMP_VERBOSE", buf, sizeof(buf)) == 0) {
269 if (*buf != '0')
270 boothowto = AB_VERBOSE;
271 }
272
273 if (rumpuser_getparam(RUMPUSER_PARAM_NCPU, buf, sizeof(buf)) != 0)
274 panic("mandatory hypervisor configuration (NCPU) missing");
275 numcpu = strtoll(buf, NULL, 10);
276 if (numcpu < 1) {
277 panic("rump kernels are not lightweight enough for \"%d\" CPUs",
278 numcpu);
279 }
280
281 rump_thread_init();
282 rump_cpus_bootstrap(&numcpu);
283
284 rumpuser_clock_gettime(RUMPUSER_CLOCK_RELWALL, &sec, &nsec);
285 boottime.tv_sec = sec;
286 boottime.tv_nsec = nsec;
287
288 initmsgbuf(rump_msgbuf, sizeof(rump_msgbuf));
289 aprint_verbose("%s%s", copyright, version);
290
291 rump_intr_init(numcpu);
292
293 rump_tsleep_init();
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 sysctl_init();
322
323 rnd_init();
324 cprng_init();
325 kern_cprng = cprng_strong_create("kernel", IPL_VM,
326 CPRNG_INIT_ANY|CPRNG_REKEY_ANY);
327 rump_hyperentropy_init();
328
329 procinit();
330 proc0_init();
331 uid_init();
332 chgproccnt(0, 1);
333
334 l->l_proc = &proc0;
335 lwp_update_creds(l);
336
337 lwpinit_specificdata();
338 lwp_initspecific(&lwp0);
339
340 rump_biglock_init();
341
342 rump_scheduler_init(numcpu);
343 /* revert temporary context and schedule a semireal context */
344 rumpuser_curlwpop(RUMPUSER_LWP_CLEAR, l);
345 initproc = &proc0; /* borrow proc0 before we get initproc started */
346 rump_schedule();
347 bootlwp = curlwp;
348
349 percpu_init();
350 inittimecounter();
351 ntp_init();
352
353 #ifdef KTRACE
354 ktrinit();
355 #endif
356
357 ts = boottime;
358 tc_setclock(&ts);
359
360 /* we are mostly go. do per-cpu subsystem init */
361 for (i = 0; i < numcpu; i++) {
362 struct cpu_info *ci = cpu_lookup(i);
363
364 /* attach non-bootstrap CPUs */
365 if (i > 0) {
366 rump_cpu_attach(ci);
367 ncpu++;
368 }
369
370 callout_init_cpu(ci);
371 softint_init(ci);
372 xc_init_cpu(ci);
373 pool_cache_cpu_init(ci);
374 selsysinit(ci);
375 percpu_init_cpu(ci);
376
377 TAILQ_INIT(&ci->ci_data.cpu_ld_locks);
378 __cpu_simple_lock_init(&ci->ci_data.cpu_ld_lock);
379
380 aprint_verbose("cpu%d at thinair0: rump virtual cpu\n", i);
381 }
382
383 /* CPUs are up. allow kernel threads to run */
384 rump_thread_allow();
385
386 rnd_init_softint();
387
388 mksysctls();
389 kqueue_init();
390 iostat_init();
391 fd_sys_init();
392 module_init();
393 devsw_init();
394 pipe_init();
395 resource_init();
396 procinit_sysctl();
397
398 /* start page baroness */
399 if (rump_threads) {
400 if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL,
401 uvm_pageout, NULL, &uvm.pagedaemon_lwp, "pdaemon") != 0)
402 panic("pagedaemon create failed");
403 } else
404 uvm.pagedaemon_lwp = NULL; /* doesn't match curlwp */
405
406 /* process dso's */
407 rumpuser_dl_bootstrap(add_linkedin_modules,
408 rump_kernelfsym_load, rump_component_load);
409
410 rump_component_addlocal();
411 rump_component_init(RUMP_COMPONENT_KERN);
412
413 /* initialize factions, if present */
414 rump_component_init(RUMP__FACTION_VFS);
415 /* pnbuf_cache is used even without vfs */
416 if (rump_component_count(RUMP__FACTION_VFS) == 0) {
417 pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
418 NULL, IPL_NONE, NULL, NULL, NULL);
419 }
420 rump_component_init(RUMP__FACTION_NET);
421 rump_component_init(RUMP__FACTION_DEV);
422 KASSERT(rump_component_count(RUMP__FACTION_VFS) <= 1
423 && rump_component_count(RUMP__FACTION_NET) <= 1
424 && rump_component_count(RUMP__FACTION_DEV) <= 1);
425
426 rump_component_init(RUMP_COMPONENT_KERN_VFS);
427
428 /*
429 * if we initialized the tty component above, the tyttymtx is
430 * now initialized. otherwise, we need to initialize it.
431 */
432 if (!rump_ttycomponent)
433 mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_VM);
434
435 cold = 0;
436
437 /* aieeeedondest */
438 if (rump_threads) {
439 if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
440 rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE))
441 panic("aiodoned");
442 }
443
444 sysctl_finalize();
445
446 module_init_class(MODULE_CLASS_ANY);
447
448 if (rumpuser_getparam(RUMPUSER_PARAM_HOSTNAME,
449 hostname, MAXHOSTNAMELEN) != 0) {
450 panic("mandatory hypervisor configuration (HOSTNAME) missing");
451 }
452 hostnamelen = strlen(hostname);
453
454 sigemptyset(&sigcantmask);
455
456 if (rump_threads)
457 vmem_rehash_start();
458
459 /*
460 * Create init (proc 1), used to attach implicit threads in rump.
461 * (note: must be done after vfsinit to get cwdi)
462 */
463 initlwp = rump__lwproc_alloclwp(NULL);
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 /* component inits done */
532 bootlwp = NULL;
533
534 /* open 0/1/2 for init */
535 KASSERT(rump_lwproc_curlwp() == NULL);
536 rump_lwproc_switch(initlwp);
537 rump_consdev_init();
538 rump_lwproc_switch(NULL);
539
540 /* release cpu */
541 rump_unschedule();
542
543 return 0;
544 }
545 /* historic compat */
546 __strong_alias(rump__init,rump_init);
547
548 int
549 rump_init_server(const char *url)
550 {
551
552 return rumpuser_sp_init(url, ostype, osrelease, MACHINE);
553 }
554
555 void
556 cpu_reboot(int howto, char *bootstr)
557 {
558 int ruhow = 0;
559 void *finiarg;
560
561 printf("rump kernel halting...\n");
562
563 if (!RUMP_LOCALPROC_P(curproc))
564 finiarg = curproc->p_vmspace->vm_map.pmap;
565 else
566 finiarg = NULL;
567
568 /* dump means we really take the dive here */
569 if ((howto & RB_DUMP) || panicstr) {
570 ruhow = RUMPUSER_PANIC;
571 goto out;
572 }
573
574 /* try to sync */
575 if (!((howto & RB_NOSYNC) || panicstr)) {
576 if (rump_vfs_fini)
577 rump_vfs_fini();
578 }
579
580 doshutdownhooks();
581
582 /* your wish is my command */
583 if (howto & RB_HALT) {
584 printf("rump kernel halted\n");
585 rumpuser_sp_fini(finiarg);
586 for (;;) {
587 rumpuser_clock_sleep(RUMPUSER_CLOCK_RELWALL, 10, 0);
588 }
589 }
590
591 /* this function is __dead, we must exit */
592 out:
593 printf("halted\n");
594 rumpuser_sp_fini(finiarg);
595 rumpuser_exit(ruhow);
596 }
597
598 struct uio *
599 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
600 {
601 struct uio *uio;
602 enum uio_rw uiorw;
603
604 switch (rw) {
605 case RUMPUIO_READ:
606 uiorw = UIO_READ;
607 break;
608 case RUMPUIO_WRITE:
609 uiorw = UIO_WRITE;
610 break;
611 default:
612 panic("%s: invalid rw %d", __func__, rw);
613 }
614
615 uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
616 uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
617
618 uio->uio_iov->iov_base = buf;
619 uio->uio_iov->iov_len = bufsize;
620
621 uio->uio_iovcnt = 1;
622 uio->uio_offset = offset;
623 uio->uio_resid = bufsize;
624 uio->uio_rw = uiorw;
625 UIO_SETUP_SYSSPACE(uio);
626
627 return uio;
628 }
629
630 size_t
631 rump_uio_getresid(struct uio *uio)
632 {
633
634 return uio->uio_resid;
635 }
636
637 off_t
638 rump_uio_getoff(struct uio *uio)
639 {
640
641 return uio->uio_offset;
642 }
643
644 size_t
645 rump_uio_free(struct uio *uio)
646 {
647 size_t resid;
648
649 resid = uio->uio_resid;
650 kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
651 kmem_free(uio, sizeof(*uio));
652
653 return resid;
654 }
655
656 kauth_cred_t
657 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
658 {
659 kauth_cred_t cred;
660 int rv;
661
662 cred = kauth_cred_alloc();
663 kauth_cred_setuid(cred, uid);
664 kauth_cred_seteuid(cred, uid);
665 kauth_cred_setsvuid(cred, uid);
666 kauth_cred_setgid(cred, gid);
667 kauth_cred_setgid(cred, gid);
668 kauth_cred_setegid(cred, gid);
669 kauth_cred_setsvgid(cred, gid);
670 rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
671 /* oh this is silly. and by "this" I mean kauth_cred_setgroups() */
672 assert(rv == 0);
673
674 return cred;
675 }
676
677 void
678 rump_cred_put(kauth_cred_t cred)
679 {
680
681 kauth_cred_free(cred);
682 }
683
684 static int compcounter[RUMP_COMPONENT_MAX];
685 static int compinited[RUMP_COMPONENT_MAX];
686
687 /*
688 * Yea, this is O(n^2), but we're only looking at a handful of components.
689 * Components are always initialized from the thread that called rump_init().
690 * Could also free these when done with them, but prolly not worth it.
691 */
692 struct compstore {
693 const struct rump_component *cs_rc;
694 LIST_ENTRY(compstore) cs_entries;
695 };
696 static LIST_HEAD(, compstore) cshead = LIST_HEAD_INITIALIZER(cshead);
697
698 /*
699 * add components which are visible from the current object.
700 */
701 static void
702 rump_component_addlocal(void)
703 {
704 __link_set_decl(rump_components, struct rump_component);
705 struct rump_component *const *rc;
706
707 __link_set_foreach(rc, rump_components) {
708 rump_component_load(*rc);
709 }
710 }
711
712 static void
713 rump_component_load(const struct rump_component *rc)
714 {
715 struct compstore *cs;
716
717 KASSERT(curlwp == bootlwp);
718
719 LIST_FOREACH(cs, &cshead, cs_entries) {
720 if (rc == cs->cs_rc)
721 return;
722 }
723
724 cs = kmem_alloc(sizeof(*cs), KM_SLEEP);
725 cs->cs_rc = rc;
726 LIST_INSERT_HEAD(&cshead, cs, cs_entries);
727 KASSERT(rc->rc_type < RUMP_COMPONENT_MAX);
728 compcounter[rc->rc_type]++;
729 }
730
731 int
732 rump_component_count(enum rump_component_type type)
733 {
734
735 KASSERT(curlwp == bootlwp);
736 KASSERT(type < RUMP_COMPONENT_MAX);
737 return compcounter[type];
738 }
739
740 void
741 rump_component_init(enum rump_component_type type)
742 {
743 struct compstore *cs;
744 const struct rump_component *rc;
745
746 KASSERT(curlwp == bootlwp);
747 KASSERT(!compinited[type]);
748 LIST_FOREACH(cs, &cshead, cs_entries) {
749 rc = cs->cs_rc;
750 if (rc->rc_type == type)
751 rc->rc_init();
752 }
753 compinited[type] = 1;
754 }
755
756 /*
757 * Initialize a module which has already been loaded and linked
758 * with dlopen(). This is fundamentally the same as a builtin module.
759 */
760 int
761 rump_module_init(const struct modinfo * const *mip, size_t nmodinfo)
762 {
763
764 return module_builtin_add(mip, nmodinfo, true);
765 }
766
767 /*
768 * Finish module (flawless victory, fatality!).
769 */
770 int
771 rump_module_fini(const struct modinfo *mi)
772 {
773
774 return module_builtin_remove(mi, true);
775 }
776
777 /*
778 * Add loaded and linked module to the builtin list. It will
779 * later be initialized with module_init_class().
780 */
781
782 static void
783 add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo)
784 {
785
786 module_builtin_add(mip, nmodinfo, false);
787 }
788
789 int
790 rump_kernelfsym_load(void *symtab, uint64_t symsize,
791 char *strtab, uint64_t strsize)
792 {
793 static int inited = 0;
794 Elf64_Ehdr ehdr;
795
796 if (inited)
797 return EBUSY;
798 inited = 1;
799
800 /*
801 * Use 64bit header since it's bigger. Shouldn't make a
802 * difference, since we're passing in all zeroes anyway.
803 */
804 memset(&ehdr, 0, sizeof(ehdr));
805 ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize);
806
807 return 0;
808 }
809
810 static int
811 rump_hyp_syscall(int num, void *arg, long *retval)
812 {
813 register_t regrv[2] = {0, 0};
814 struct lwp *l;
815 struct sysent *callp;
816 int rv;
817
818 if (__predict_false(num >= SYS_NSYSENT))
819 return ENOSYS;
820
821 /* XXX: always uses native syscall vector */
822 callp = rump_sysent + num;
823 l = curlwp;
824 rv = sy_invoke(callp, l, (void *)arg, regrv, num);
825 retval[0] = regrv[0];
826 retval[1] = regrv[1];
827
828 return rv;
829 }
830
831 static int
832 rump_hyp_rfork(void *priv, int flags, const char *comm)
833 {
834 struct vmspace *newspace;
835 struct proc *p;
836 int error;
837
838 if ((error = rump_lwproc_rfork(flags)) != 0)
839 return error;
840
841 /*
842 * Since it's a proxy proc, adjust the vmspace.
843 * Refcount will eternally be 1.
844 */
845 p = curproc;
846 newspace = kmem_zalloc(sizeof(*newspace), KM_SLEEP);
847 newspace->vm_refcnt = 1;
848 newspace->vm_map.pmap = priv;
849 KASSERT(p->p_vmspace == vmspace_kernel());
850 p->p_vmspace = newspace;
851 if (comm)
852 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
853
854 return 0;
855 }
856
857 /*
858 * Order all lwps in a process to exit. does *not* wait for them to drain.
859 */
860 static void
861 rump_hyp_lwpexit(void)
862 {
863 struct proc *p = curproc;
864 uint64_t where;
865 struct lwp *l;
866
867 mutex_enter(p->p_lock);
868 /*
869 * First pass: mark all lwps in the process with LW_RUMP_QEXIT
870 * so that they know they should exit.
871 */
872 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
873 if (l == curlwp)
874 continue;
875 l->l_flag |= LW_RUMP_QEXIT;
876 }
877 mutex_exit(p->p_lock);
878
879 /*
880 * Next, make sure everyone on all CPUs sees our status
881 * update. This keeps threads inside cv_wait() and makes
882 * sure we don't access a stale cv pointer later when
883 * we wake up the threads.
884 */
885
886 where = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
887 xc_wait(where);
888
889 /*
890 * Ok, all lwps are either:
891 * 1) not in the cv code
892 * 2) sleeping on l->l_private
893 * 3) sleeping on p->p_waitcv
894 *
895 * Either way, l_private is stable until we set PS_RUMP_LWPEXIT
896 * in p->p_sflag.
897 */
898
899 mutex_enter(p->p_lock);
900 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
901 if (l->l_private)
902 cv_broadcast(l->l_private);
903 }
904 p->p_sflag |= PS_RUMP_LWPEXIT;
905 cv_broadcast(&p->p_waitcv);
906 mutex_exit(p->p_lock);
907 }
908
909 /*
910 * Notify process that all threads have been drained and exec is complete.
911 */
912 static void
913 rump_hyp_execnotify(const char *comm)
914 {
915 struct proc *p = curproc;
916
917 fd_closeexec();
918 mutex_enter(p->p_lock);
919 KASSERT(p->p_nlwps == 1 && p->p_sflag & PS_RUMP_LWPEXIT);
920 p->p_sflag &= ~PS_RUMP_LWPEXIT;
921 mutex_exit(p->p_lock);
922 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
923 }
924
925 int
926 rump_boot_gethowto()
927 {
928
929 return boothowto;
930 }
931
932 void
933 rump_boot_sethowto(int howto)
934 {
935
936 boothowto = howto;
937 }
938
939 int
940 rump_getversion(void)
941 {
942
943 return __NetBSD_Version__;
944 }
945 /* compat */
946 __strong_alias(rump_pub_getversion,rump_getversion);
947
948 int
949 rump_nativeabi_p(void)
950 {
951
952 #ifdef _RUMP_NATIVE_ABI
953 return 1;
954 #else
955 return 0;
956 #endif
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 const int *etrans = NULL;
1015 int rv;
1016
1017 rump_schedule();
1018 p = curproc;
1019 e = p->p_emul;
1020 #ifndef __HAVE_MINIMAL_EMUL
1021 KASSERT(num > 0 && num < e->e_nsysent);
1022 #endif
1023 callp = e->e_sysent + num;
1024
1025 rv = sy_invoke(callp, curlwp, data, retval, num);
1026
1027 /*
1028 * I hope that (!__HAVE_MINIMAL_EMUL || __HAVE_SYSCALL_INTERN) is
1029 * an invariant ...
1030 */
1031 #if !defined(__HAVE_MINIMAL_EMUL)
1032 etrans = e->e_errno;
1033 #elif defined(__HAVE_SYSCALL_INTERN)
1034 etrans = p->p_emuldata;
1035 #endif
1036
1037 if (etrans) {
1038 rv = etrans[rv];
1039 /*
1040 * XXX: small hack since Linux etrans vectors on some
1041 * archs contain negative errnos, but rump_syscalls
1042 * uses the -1 + errno ABI. Note that these
1043 * negative values are always the result of translation,
1044 * otherwise the above translation method would not
1045 * work very well.
1046 */
1047 if (rv < 0)
1048 rv = -rv;
1049 }
1050 rump_unschedule();
1051
1052 return rv;
1053 }
1054