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