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