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