rump.c revision 1.260 1 /* $NetBSD: rump.c,v 1.260 2013/04/27 16:02:56 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.260 2013/04/27 16:02:56 pooka Exp $");
30
31 #include <sys/systm.h>
32 #define ELFSIZE ARCH_ELFSIZE
33
34 #include <sys/param.h>
35 #include <sys/atomic.h>
36 #include <sys/buf.h>
37 #include <sys/callout.h>
38 #include <sys/conf.h>
39 #include <sys/cpu.h>
40 #include <sys/device.h>
41 #include <sys/evcnt.h>
42 #include <sys/event.h>
43 #include <sys/exec_elf.h>
44 #include <sys/filedesc.h>
45 #include <sys/iostat.h>
46 #include <sys/kauth.h>
47 #include <sys/kcpuset.h>
48 #include <sys/kernel.h>
49 #include <sys/kmem.h>
50 #include <sys/kprintf.h>
51 #include <sys/kthread.h>
52 #include <sys/ksyms.h>
53 #include <sys/msgbuf.h>
54 #include <sys/module.h>
55 #include <sys/namei.h>
56 #include <sys/once.h>
57 #include <sys/percpu.h>
58 #include <sys/pipe.h>
59 #include <sys/pool.h>
60 #include <sys/pserialize.h>
61 #include <sys/queue.h>
62 #include <sys/reboot.h>
63 #include <sys/resourcevar.h>
64 #include <sys/select.h>
65 #include <sys/sysctl.h>
66 #include <sys/syscall.h>
67 #include <sys/syscallvar.h>
68 #include <sys/timetc.h>
69 #include <sys/tty.h>
70 #include <sys/uidinfo.h>
71 #include <sys/vmem.h>
72 #include <sys/xcall.h>
73 #include <sys/simplelock.h>
74 #include <sys/cprng.h>
75
76 #include <rump/rumpuser.h>
77
78 #include <secmodel/suser/suser.h>
79
80 #include <prop/proplib.h>
81
82 #include <uvm/uvm_extern.h>
83 #include <uvm/uvm_readahead.h>
84
85 #include "rump_private.h"
86 #include "rump_net_private.h"
87 #include "rump_vfs_private.h"
88 #include "rump_dev_private.h"
89
90 char machine[] = MACHINE;
91
92 struct proc *initproc;
93
94 struct device rump_rootdev = {
95 .dv_class = DV_VIRTUAL
96 };
97
98 #ifdef RUMP_WITHOUT_THREADS
99 int rump_threads = 0;
100 #else
101 int rump_threads = 1;
102 #endif
103
104 static int rump_proxy_syscall(int, void *, long *);
105 static int rump_proxy_rfork(void *, int, const char *);
106 static void rump_proxy_lwpexit(void);
107 static void rump_proxy_execnotify(const char *);
108
109 static void rump_component_load(const struct rump_component *);
110 static struct lwp *bootlwp;
111
112 static char rump_msgbuf[16*1024]; /* 16k should be enough for std rump needs */
113
114 #ifdef LOCKDEBUG
115 const int rump_lockdebug = 1;
116 #else
117 const int rump_lockdebug = 0;
118 #endif
119 bool rump_ttycomponent = false;
120
121 static void
122 rump_aiodone_worker(struct work *wk, void *dummy)
123 {
124 struct buf *bp = (struct buf *)wk;
125
126 KASSERT(&bp->b_work == wk);
127 bp->b_iodone(bp);
128 }
129
130 static int rump_inited;
131
132 void (*rump_vfs_drainbufs)(int);
133 void (*rump_vfs_fini)(void);
134 int (*rump_vfs_makeonedevnode)(dev_t, const char *,
135 devmajor_t, devminor_t) = (void *)nullop;
136 int (*rump_vfs_makedevnodes)(dev_t, const char *, char,
137 devmajor_t, devminor_t, int) = (void *)nullop;
138
139 int rump__unavailable(void);
140 int rump__unavailable() {return EOPNOTSUPP;}
141
142 __weak_alias(biodone,rump__unavailable);
143 __weak_alias(sopoll,rump__unavailable);
144
145 void rump__unavailable_vfs_panic(void);
146 void rump__unavailable_vfs_panic() {panic("vfs component not available");}
147 __weak_alias(usermount_common_policy,rump__unavailable_vfs_panic);
148
149 /* easier to write vfs-less clients */
150 __weak_alias(rump_pub_etfs_register,rump__unavailable);
151 __weak_alias(rump_pub_etfs_register_withsize,rump__unavailable);
152 __weak_alias(rump_pub_etfs_remove,rump__unavailable);
153
154 rump_proc_vfs_init_fn rump_proc_vfs_init;
155 rump_proc_vfs_release_fn rump_proc_vfs_release;
156
157 static void add_linkedin_modules(const struct modinfo *const *, size_t);
158
159 /*
160 * Create kern.hostname. why only this you ask. well, init_sysctl
161 * is a kitchen sink in need of some gardening. but i want to use
162 * kern.hostname today.
163 */
164 static void
165 mksysctls(void)
166 {
167
168 sysctl_createv(NULL, 0, NULL, NULL,
169 CTLFLAG_PERMANENT, CTLTYPE_NODE, "kern", NULL,
170 NULL, 0, NULL, 0, CTL_KERN, CTL_EOL);
171
172 /* XXX: setting hostnamelen is missing */
173 sysctl_createv(NULL, 0, NULL, NULL,
174 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_STRING, "hostname",
175 SYSCTL_DESCR("System hostname"), NULL, 0,
176 hostname, MAXHOSTNAMELEN, CTL_KERN, KERN_HOSTNAME, CTL_EOL);
177 }
178
179 /* there's no convenient kernel entry point for this, so just craft out own */
180 static pid_t
181 spgetpid(void)
182 {
183
184 return curproc->p_pid;
185 }
186
187 static const struct rumpuser_sp_ops spops = {
188 .spop_schedule = rump_schedule,
189 .spop_unschedule = rump_unschedule,
190 .spop_lwproc_switch = rump_lwproc_switch,
191 .spop_lwproc_release = rump_lwproc_releaselwp,
192 .spop_lwproc_rfork = rump_proxy_rfork,
193 .spop_lwproc_newlwp = rump_lwproc_newlwp,
194 .spop_lwproc_curlwp = rump_lwproc_curlwp,
195 .spop_lwpexit = rump_proxy_lwpexit,
196 .spop_syscall = rump_proxy_syscall,
197 .spop_execnotify = rump_proxy_execnotify,
198 .spop_getpid = spgetpid,
199 };
200
201 int
202 rump_daemonize_begin(void)
203 {
204
205 if (rump_inited)
206 return EALREADY;
207
208 return rumpuser_daemonize_begin();
209 }
210
211 int
212 rump_daemonize_done(int error)
213 {
214
215 return rumpuser_daemonize_done(error);
216 }
217
218 RUMP_COMPONENT(RUMP_COMPONENT_POSTINIT)
219 {
220 extern void *__start_link_set_rump_components;
221 extern void *__stop_link_set_rump_components;
222
223 /*
224 * Trick compiler into generating references so that statically
225 * linked rump kernels are generated with the link set symbols.
226 */
227 asm("" :: "r"(__start_link_set_rump_components));
228 asm("" :: "r"(__stop_link_set_rump_components));
229 }
230
231 int
232 rump_init(void)
233 {
234 char buf[256];
235 struct timespec ts;
236 uint64_t sec, nsec;
237 struct lwp *l;
238 int i, numcpu;
239 int error;
240
241 /* not reentrant */
242 if (rump_inited)
243 return 0;
244 else if (rump_inited == -1)
245 panic("rump_init: host process restart required");
246 else
247 rump_inited = 1;
248
249 /* initialize hypervisor */
250 if (rumpuser_init(RUMPUSER_VERSION,
251 rump_user_schedule, rump_user_unschedule) != 0) {
252 rumpuser_dprintf("rumpuser init failed\n");
253 return EINVAL;
254 }
255
256 /* retrieve env vars which affect the early stage of bootstrap */
257 if (rumpuser_getenv("RUMP_THREADS", buf, sizeof(buf), &error) == 0) {
258 rump_threads = *buf != '0';
259 }
260 if (rumpuser_getenv("RUMP_VERBOSE", buf, sizeof(buf), &error) == 0) {
261 if (*buf != '0')
262 boothowto = AB_VERBOSE;
263 }
264 if (rumpuser_getenv("RUMP_NCPU", buf, sizeof(buf), &error) == 0)
265 error = 0;
266 if (error == 0) {
267 numcpu = strtoll(buf, NULL, 10);
268 if (numcpu < 1)
269 numcpu = 1;
270 } else {
271 numcpu = rumpuser_getnhostcpu();
272 }
273
274 rump_thread_init();
275 rump_cpus_bootstrap(&numcpu);
276
277 rumpuser_gettime(&sec, &nsec, &error);
278 boottime.tv_sec = sec;
279 boottime.tv_nsec = nsec;
280
281 initmsgbuf(rump_msgbuf, sizeof(rump_msgbuf));
282 aprint_verbose("%s%s", copyright, version);
283
284 rump_intr_init(numcpu);
285 rump_tsleep_init();
286
287 /* init minimal lwp/cpu context */
288 l = &lwp0;
289 l->l_lid = 1;
290 l->l_cpu = l->l_target_cpu = rump_cpu;
291 l->l_fd = &filedesc0;
292 rumpuser_set_curlwp(l);
293
294 rumpuser_mutex_init(&rump_giantlock);
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
321 rnd_init();
322
323 /*
324 * Create the kernel cprng. Yes, it's currently stubbed out
325 * to arc4random() for RUMP, but this won't always be so.
326 */
327 kern_cprng = cprng_strong_create("kernel", IPL_VM,
328 CPRNG_INIT_ANY|CPRNG_REKEY_ANY);
329
330 procinit();
331 proc0_init();
332 sysctl_init();
333 uid_init();
334 chgproccnt(0, 1);
335
336 l->l_proc = &proc0;
337 lwp_update_creds(l);
338
339 lwpinit_specificdata();
340 lwp_initspecific(&lwp0);
341
342 rump_biglock_init();
343
344 rump_scheduler_init(numcpu);
345 /* revert temporary context and schedule a semireal context */
346 rumpuser_set_curlwp(NULL);
347 initproc = &proc0; /* borrow proc0 before we get initproc started */
348 rump_schedule();
349 bootlwp = curlwp;
350
351 percpu_init();
352 inittimecounter();
353 ntp_init();
354
355 rumpuser_gettime(&sec, &nsec, &error);
356 ts.tv_sec = sec;
357 ts.tv_nsec = nsec;
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 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_init(RUMP_COMPONENT_KERN);
409
410 /* initialize factions, if present */
411 rump_component_init(RUMP__FACTION_VFS);
412 /* pnbuf_cache is used even without vfs */
413 if (rump_component_count(RUMP__FACTION_VFS) == 0) {
414 pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
415 NULL, IPL_NONE, NULL, NULL, NULL);
416 }
417 rump_component_init(RUMP__FACTION_NET);
418 rump_component_init(RUMP__FACTION_DEV);
419 KASSERT(rump_component_count(RUMP__FACTION_VFS) <= 1
420 && rump_component_count(RUMP__FACTION_NET) <= 1
421 && rump_component_count(RUMP__FACTION_DEV) <= 1);
422
423 rump_component_init(RUMP_COMPONENT_KERN_VFS);
424
425 /*
426 * if we initialized the tty component above, the tyttymtx is
427 * now initialized. otherwise, we need to initialize it.
428 */
429 if (!rump_ttycomponent)
430 mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_VM);
431
432 cold = 0;
433
434 /* aieeeedondest */
435 if (rump_threads) {
436 if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
437 rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE))
438 panic("aiodoned");
439 }
440
441 sysctl_finalize();
442
443 module_init_class(MODULE_CLASS_ANY);
444
445 rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
446 hostnamelen = strlen(hostname);
447
448 sigemptyset(&sigcantmask);
449
450 if (rump_threads)
451 vmem_rehash_start();
452
453 /*
454 * Create init, used to attach implicit threads in rump.
455 * (note: must be done after vfsinit to get cwdi)
456 */
457 (void)rump__lwproc_alloclwp(NULL); /* dummy thread for initproc */
458 mutex_enter(proc_lock);
459 initproc = proc_find_raw(1);
460 mutex_exit(proc_lock);
461 if (initproc == NULL)
462 panic("where in the world is initproc?");
463
464 /*
465 * Adjust syscall vector in case factions were dlopen()'d
466 * before calling rump_init().
467 * (modules will handle dynamic syscalls the usual way)
468 *
469 * Note: this will adjust the function vectors of
470 * syscalls which use a funcalias (getpid etc.), but
471 * it makes no difference.
472 */
473 for (i = 0; i < SYS_NSYSENT; i++) {
474 void *sym;
475
476 if (rump_sysent[i].sy_flags & SYCALL_NOSYS ||
477 *syscallnames[i] == '#' ||
478 rump_sysent[i].sy_call == sys_nomodule)
479 continue;
480
481 /*
482 * deal with compat wrappers. makesyscalls.sh should
483 * generate the necessary info instead of this hack,
484 * though. ugly, fix it later.
485 */
486 #define CPFX "compat_"
487 #define CPFXLEN (sizeof(CPFX)-1)
488 if (strncmp(syscallnames[i], CPFX, CPFXLEN) == 0) {
489 const char *p = syscallnames[i] + CPFXLEN;
490 size_t namelen;
491
492 /* skip version number */
493 while (*p >= '0' && *p <= '9')
494 p++;
495 if (p == syscallnames[i] + CPFXLEN || *p != '_')
496 panic("invalid syscall name %s\n",
497 syscallnames[i]);
498
499 /* skip over the next underscore */
500 p++;
501 namelen = p + (sizeof("rumpns_")-1) - syscallnames[i];
502
503 strcpy(buf, "rumpns_");
504 strcat(buf, syscallnames[i]);
505 /* XXX: no strncat in the kernel */
506 strcpy(buf+namelen, "sys_");
507 strcat(buf, p);
508 #undef CPFX
509 #undef CPFXLEN
510 } else {
511 sprintf(buf, "rumpns_sys_%s", syscallnames[i]);
512 }
513 if ((sym = rumpuser_dl_globalsym(buf)) != NULL
514 && sym != rump_sysent[i].sy_call) {
515 #if 0
516 rumpuser_dprintf("adjusting %s: %p (old %p)\n",
517 syscallnames[i], sym, rump_sysent[i].sy_call);
518 #endif
519 rump_sysent[i].sy_call = sym;
520 }
521 }
522
523 rump_component_init(RUMP_COMPONENT_POSTINIT);
524
525 /* release cpu */
526 bootlwp = NULL;
527 rump_unschedule();
528
529 return 0;
530 }
531 /* historic compat */
532 __strong_alias(rump__init,rump_init);
533
534 int
535 rump_init_server(const char *url)
536 {
537
538 return rumpuser_sp_init(url, &spops, ostype, osrelease, MACHINE);
539 }
540
541 void
542 cpu_reboot(int howto, char *bootstr)
543 {
544 int ruhow = 0;
545 void *finiarg;
546
547 printf("rump kernel halting...\n");
548
549 if (!RUMP_LOCALPROC_P(curproc))
550 finiarg = curproc->p_vmspace->vm_map.pmap;
551 else
552 finiarg = NULL;
553
554 /* dump means we really take the dive here */
555 if ((howto & RB_DUMP) || panicstr) {
556 ruhow = RUMPUSER_PANIC;
557 goto out;
558 }
559
560 /* try to sync */
561 if (!((howto & RB_NOSYNC) || panicstr)) {
562 if (rump_vfs_fini)
563 rump_vfs_fini();
564 }
565
566 /* your wish is my command */
567 if (howto & RB_HALT) {
568 printf("rump kernel halted\n");
569 rumpuser_sp_fini(finiarg);
570 for (;;) {
571 uint64_t sec = 5, nsec = 0;
572 int error;
573
574 rumpuser_nanosleep(&sec, &nsec, &error);
575 }
576 }
577
578 /* this function is __dead, we must exit */
579 out:
580 printf("halted\n");
581 rumpuser_sp_fini(finiarg);
582 rumpuser_exit(ruhow);
583 }
584
585 struct uio *
586 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
587 {
588 struct uio *uio;
589 enum uio_rw uiorw;
590
591 switch (rw) {
592 case RUMPUIO_READ:
593 uiorw = UIO_READ;
594 break;
595 case RUMPUIO_WRITE:
596 uiorw = UIO_WRITE;
597 break;
598 default:
599 panic("%s: invalid rw %d", __func__, rw);
600 }
601
602 uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
603 uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
604
605 uio->uio_iov->iov_base = buf;
606 uio->uio_iov->iov_len = bufsize;
607
608 uio->uio_iovcnt = 1;
609 uio->uio_offset = offset;
610 uio->uio_resid = bufsize;
611 uio->uio_rw = uiorw;
612 UIO_SETUP_SYSSPACE(uio);
613
614 return uio;
615 }
616
617 size_t
618 rump_uio_getresid(struct uio *uio)
619 {
620
621 return uio->uio_resid;
622 }
623
624 off_t
625 rump_uio_getoff(struct uio *uio)
626 {
627
628 return uio->uio_offset;
629 }
630
631 size_t
632 rump_uio_free(struct uio *uio)
633 {
634 size_t resid;
635
636 resid = uio->uio_resid;
637 kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
638 kmem_free(uio, sizeof(*uio));
639
640 return resid;
641 }
642
643 kauth_cred_t
644 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
645 {
646 kauth_cred_t cred;
647 int rv;
648
649 cred = kauth_cred_alloc();
650 kauth_cred_setuid(cred, uid);
651 kauth_cred_seteuid(cred, uid);
652 kauth_cred_setsvuid(cred, uid);
653 kauth_cred_setgid(cred, gid);
654 kauth_cred_setgid(cred, gid);
655 kauth_cred_setegid(cred, gid);
656 kauth_cred_setsvgid(cred, gid);
657 rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
658 /* oh this is silly. and by "this" I mean kauth_cred_setgroups() */
659 assert(rv == 0);
660
661 return cred;
662 }
663
664 void
665 rump_cred_put(kauth_cred_t cred)
666 {
667
668 kauth_cred_free(cred);
669 }
670
671 static int compcounter[RUMP_COMPONENT_MAX];
672 static int compinited[RUMP_COMPONENT_MAX];
673
674 /*
675 * Yea, this is O(n^2), but we're only looking at a handful of components.
676 * Components are always initialized from the thread that called rump_init().
677 * Could also free these when done with them, but prolly not worth it.
678 */
679 struct compstore {
680 const struct rump_component *cs_rc;
681 LIST_ENTRY(compstore) cs_entries;
682 };
683 static LIST_HEAD(, compstore) cshead = LIST_HEAD_INITIALIZER(cshead);
684
685 static void
686 rump_component_load(const struct rump_component *rc)
687 {
688 struct compstore *cs;
689
690 KASSERT(curlwp == bootlwp);
691
692 LIST_FOREACH(cs, &cshead, cs_entries) {
693 if (rc == cs->cs_rc)
694 return;
695 }
696
697 cs = kmem_alloc(sizeof(*cs), KM_SLEEP);
698 cs->cs_rc = rc;
699 LIST_INSERT_HEAD(&cshead, cs, cs_entries);
700 KASSERT(rc->rc_type < RUMP_COMPONENT_MAX);
701 compcounter[rc->rc_type]++;
702 }
703
704 int
705 rump_component_count(enum rump_component_type type)
706 {
707
708 KASSERT(curlwp == bootlwp);
709 KASSERT(type < RUMP_COMPONENT_MAX);
710 return compcounter[type];
711 }
712
713 void
714 rump_component_init(enum rump_component_type type)
715 {
716 struct compstore *cs;
717 const struct rump_component *rc;
718
719 KASSERT(curlwp == bootlwp);
720 KASSERT(!compinited[type]);
721 LIST_FOREACH(cs, &cshead, cs_entries) {
722 rc = cs->cs_rc;
723 if (rc->rc_type == type)
724 rc->rc_init();
725 }
726 compinited[type] = 1;
727 }
728
729 /*
730 * Initialize a module which has already been loaded and linked
731 * with dlopen(). This is fundamentally the same as a builtin module.
732 */
733 int
734 rump_module_init(const struct modinfo * const *mip, size_t nmodinfo)
735 {
736
737 return module_builtin_add(mip, nmodinfo, true);
738 }
739
740 /*
741 * Finish module (flawless victory, fatality!).
742 */
743 int
744 rump_module_fini(const struct modinfo *mi)
745 {
746
747 return module_builtin_remove(mi, true);
748 }
749
750 /*
751 * Add loaded and linked module to the builtin list. It will
752 * later be initialized with module_init_class().
753 */
754
755 static void
756 add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo)
757 {
758
759 module_builtin_add(mip, nmodinfo, false);
760 }
761
762 int
763 rump_kernelfsym_load(void *symtab, uint64_t symsize,
764 char *strtab, uint64_t strsize)
765 {
766 static int inited = 0;
767 Elf64_Ehdr ehdr;
768
769 if (inited)
770 return EBUSY;
771 inited = 1;
772
773 /*
774 * Use 64bit header since it's bigger. Shouldn't make a
775 * difference, since we're passing in all zeroes anyway.
776 */
777 memset(&ehdr, 0, sizeof(ehdr));
778 ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize);
779
780 return 0;
781 }
782
783 static int
784 rump_proxy_syscall(int num, void *arg, long *retval)
785 {
786 register_t regrv[2] = {0, 0};
787 struct lwp *l;
788 struct sysent *callp;
789 int rv;
790
791 if (__predict_false(num >= SYS_NSYSENT))
792 return ENOSYS;
793
794 callp = rump_sysent + num;
795 l = curlwp;
796 rv = sy_call(callp, l, (void *)arg, regrv);
797 retval[0] = regrv[0];
798 retval[1] = regrv[1];
799
800 return rv;
801 }
802
803 static int
804 rump_proxy_rfork(void *priv, int flags, const char *comm)
805 {
806 struct vmspace *newspace;
807 struct proc *p;
808 int error;
809
810 if ((error = rump_lwproc_rfork(flags)) != 0)
811 return error;
812
813 /*
814 * Since it's a proxy proc, adjust the vmspace.
815 * Refcount will eternally be 1.
816 */
817 p = curproc;
818 newspace = kmem_zalloc(sizeof(*newspace), KM_SLEEP);
819 newspace->vm_refcnt = 1;
820 newspace->vm_map.pmap = priv;
821 KASSERT(p->p_vmspace == vmspace_kernel());
822 p->p_vmspace = newspace;
823 if (comm)
824 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
825
826 return 0;
827 }
828
829 /*
830 * Order all lwps in a process to exit. does *not* wait for them to drain.
831 */
832 static void
833 rump_proxy_lwpexit(void)
834 {
835 struct proc *p = curproc;
836 uint64_t where;
837 struct lwp *l;
838
839 mutex_enter(p->p_lock);
840 /*
841 * First pass: mark all lwps in the process with LW_RUMP_QEXIT
842 * so that they know they should exit.
843 */
844 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
845 if (l == curlwp)
846 continue;
847 l->l_flag |= LW_RUMP_QEXIT;
848 }
849 mutex_exit(p->p_lock);
850
851 /*
852 * Next, make sure everyone on all CPUs sees our status
853 * update. This keeps threads inside cv_wait() and makes
854 * sure we don't access a stale cv pointer later when
855 * we wake up the threads.
856 */
857
858 where = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
859 xc_wait(where);
860
861 /*
862 * Ok, all lwps are either:
863 * 1) not in the cv code
864 * 2) sleeping on l->l_private
865 * 3) sleeping on p->p_waitcv
866 *
867 * Either way, l_private is stable until we set PS_RUMP_LWPEXIT
868 * in p->p_sflag.
869 */
870
871 mutex_enter(p->p_lock);
872 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
873 if (l->l_private)
874 cv_broadcast(l->l_private);
875 }
876 p->p_sflag |= PS_RUMP_LWPEXIT;
877 cv_broadcast(&p->p_waitcv);
878 mutex_exit(p->p_lock);
879 }
880
881 /*
882 * Notify process that all threads have been drained and exec is complete.
883 */
884 static void
885 rump_proxy_execnotify(const char *comm)
886 {
887 struct proc *p = curproc;
888
889 fd_closeexec();
890 mutex_enter(p->p_lock);
891 KASSERT(p->p_nlwps == 1 && p->p_sflag & PS_RUMP_LWPEXIT);
892 p->p_sflag &= ~PS_RUMP_LWPEXIT;
893 mutex_exit(p->p_lock);
894 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
895 }
896
897 int
898 rump_boot_gethowto()
899 {
900
901 return boothowto;
902 }
903
904 void
905 rump_boot_sethowto(int howto)
906 {
907
908 boothowto = howto;
909 }
910
911 int
912 rump_getversion(void)
913 {
914
915 return __NetBSD_Version__;
916 }
917
918 /*
919 * Note: may be called unscheduled. Not fully safe since no locking
920 * of allevents (currently that's not even available).
921 */
922 void
923 rump_printevcnts()
924 {
925 struct evcnt *ev;
926
927 TAILQ_FOREACH(ev, &allevents, ev_list)
928 rumpuser_dprintf("%s / %s: %" PRIu64 "\n",
929 ev->ev_group, ev->ev_name, ev->ev_count);
930 }
931
932 /*
933 * If you use this interface ... well ... all bets are off.
934 * The original purpose is for the p2k fs server library to be
935 * able to use the same pid/lid for VOPs as the host kernel.
936 */
937 void
938 rump_allbetsareoff_setid(pid_t pid, int lid)
939 {
940 struct lwp *l = curlwp;
941 struct proc *p = l->l_proc;
942
943 l->l_lid = lid;
944 p->p_pid = pid;
945 }
946
947 #include <sys/pserialize.h>
948
949 static void
950 ipiemu(void *a1, void *a2)
951 {
952
953 xc__highpri_intr(NULL);
954 pserialize_switchpoint();
955 }
956
957 void
958 rump_xc_highpri(struct cpu_info *ci)
959 {
960
961 if (ci)
962 xc_unicast(0, ipiemu, NULL, NULL, ci);
963 else
964 xc_broadcast(0, ipiemu, NULL, NULL);
965 }
966