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