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