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