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