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