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