rump.c revision 1.253 1 /* $NetBSD: rump.c,v 1.253 2013/03/07 18:33:27 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.253 2013/03/07 18:33:27 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 RUMP_COMPONENT(RUMP_COMPONENT_POSTINIT)
212 {
213 extern void *__start_link_set_rump_components;
214 extern void *__stop_link_set_rump_components;
215
216 /*
217 * Trick compiler into generating references so that statically
218 * linked rump kernels are generated with the link set symbols.
219 */
220 asm("" :: "r"(__start_link_set_rump_components));
221 asm("" :: "r"(__stop_link_set_rump_components));
222 }
223
224 int
225 rump__init(int rump_version)
226 {
227 char buf[256];
228 struct timespec ts;
229 uint64_t sec, nsec;
230 struct lwp *l;
231 int i, numcpu;
232 int error;
233
234 /* not reentrant */
235 if (rump_inited)
236 return 0;
237 else if (rump_inited == -1)
238 panic("rump_init: host process restart required");
239 else
240 rump_inited = 1;
241
242 if (rumpuser_getversion() != RUMPUSER_VERSION) {
243 /* let's hope the ABI of rumpuser_dprintf is the same ;) */
244 rumpuser_dprintf("rumpuser version mismatch: %d vs. %d\n",
245 rumpuser_getversion(), RUMPUSER_VERSION);
246 return EPROGMISMATCH;
247 }
248
249 if (rumpuser_getenv("RUMP_VERBOSE", buf, sizeof(buf), &error) == 0) {
250 if (*buf != '0')
251 boothowto = AB_VERBOSE;
252 }
253
254 if (rumpuser_getenv("RUMP_NCPU", buf, sizeof(buf), &error) == 0)
255 error = 0;
256 if (error == 0) {
257 numcpu = strtoll(buf, NULL, 10);
258 if (numcpu < 1)
259 numcpu = 1;
260 } else {
261 numcpu = rumpuser_getnhostcpu();
262 }
263 rump_cpus_bootstrap(&numcpu);
264
265 rumpuser_gettime(&sec, &nsec, &error);
266 boottime.tv_sec = sec;
267 boottime.tv_nsec = nsec;
268
269 initmsgbuf(rump_msgbuf, sizeof(rump_msgbuf));
270 aprint_verbose("%s%s", copyright, version);
271
272 if (rump_version != RUMP_VERSION) {
273 printf("rump version mismatch, %d vs. %d\n",
274 rump_version, RUMP_VERSION);
275 return EPROGMISMATCH;
276 }
277
278 if (rumpuser_getenv("RUMP_THREADS", buf, sizeof(buf), &error) == 0) {
279 rump_threads = *buf != '0';
280 }
281 rumpuser_thrinit(rump_user_schedule, rump_user_unschedule,
282 rump_threads);
283 rump_intr_init(numcpu);
284 rump_tsleep_init();
285
286 /* init minimal lwp/cpu context */
287 l = &lwp0;
288 l->l_lid = 1;
289 l->l_cpu = l->l_target_cpu = rump_cpu;
290 l->l_fd = &filedesc0;
291 rumpuser_set_curlwp(l);
292
293 rumpuser_mutex_init(&rump_giantlock);
294 ksyms_init();
295 uvm_init();
296 evcnt_init();
297
298 kcpuset_sysinit();
299 once_init();
300 kernconfig_lock_init();
301 prop_kern_init();
302
303 kmem_init();
304
305 uvm_ra_init();
306 uao_init();
307
308 mutex_obj_init();
309 callout_startup();
310
311 kprintf_init();
312 pserialize_init();
313 loginit();
314
315 kauth_init();
316
317 secmodel_init();
318
319 rnd_init();
320
321 /*
322 * Create the kernel cprng. Yes, it's currently stubbed out
323 * to arc4random() for RUMP, but this won't always be so.
324 */
325 kern_cprng = cprng_strong_create("kernel", IPL_VM,
326 CPRNG_INIT_ANY|CPRNG_REKEY_ANY);
327
328 procinit();
329 proc0_init();
330 sysctl_init();
331 uid_init();
332 chgproccnt(0, 1);
333
334 l->l_proc = &proc0;
335 lwp_update_creds(l);
336
337 lwpinit_specificdata();
338 lwp_initspecific(&lwp0);
339
340 rump_biglock_init();
341
342 rump_scheduler_init(numcpu);
343 /* revert temporary context and schedule a semireal context */
344 rumpuser_set_curlwp(NULL);
345 initproc = &proc0; /* borrow proc0 before we get initproc started */
346 rump_schedule();
347
348 percpu_init();
349 inittimecounter();
350 ntp_init();
351
352 rumpuser_gettime(&sec, &nsec, &error);
353 ts.tv_sec = sec;
354 ts.tv_nsec = nsec;
355 tc_setclock(&ts);
356
357 /* we are mostly go. do per-cpu subsystem init */
358 for (i = 0; i < numcpu; i++) {
359 struct cpu_info *ci = cpu_lookup(i);
360
361 /* attach non-bootstrap CPUs */
362 if (i > 0) {
363 rump_cpu_attach(ci);
364 ncpu++;
365 }
366
367 callout_init_cpu(ci);
368 softint_init(ci);
369 xc_init_cpu(ci);
370 pool_cache_cpu_init(ci);
371 selsysinit(ci);
372 percpu_init_cpu(ci);
373
374 TAILQ_INIT(&ci->ci_data.cpu_ld_locks);
375 __cpu_simple_lock_init(&ci->ci_data.cpu_ld_lock);
376
377 aprint_verbose("cpu%d at thinair0: rump virtual cpu\n", i);
378 }
379
380 mksysctls();
381 kqueue_init();
382 iostat_init();
383 fd_sys_init();
384 module_init();
385 devsw_init();
386 pipe_init();
387 resource_init();
388 procinit_sysctl();
389
390 /* start page baroness */
391 if (rump_threads) {
392 if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL,
393 uvm_pageout, NULL, &uvm.pagedaemon_lwp, "pdaemon") != 0)
394 panic("pagedaemon create failed");
395 } else
396 uvm.pagedaemon_lwp = NULL; /* doesn't match curlwp */
397
398 /* process dso's */
399 rumpuser_dl_bootstrap(add_linkedin_modules, rump_kernelfsym_load);
400
401 rump_component_init(RUMP_COMPONENT_KERN);
402
403 /* initialize factions, if present */
404 rump_component_init(RUMP__FACTION_VFS);
405 /* pnbuf_cache is used even without vfs */
406 if (rump_component_count(RUMP__FACTION_VFS) == 0) {
407 pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
408 NULL, IPL_NONE, NULL, NULL, NULL);
409 }
410 rump_component_init(RUMP__FACTION_NET);
411 rump_component_init(RUMP__FACTION_DEV);
412 KASSERT(rump_component_count(RUMP__FACTION_VFS) <= 1
413 && rump_component_count(RUMP__FACTION_NET) <= 1
414 && rump_component_count(RUMP__FACTION_DEV) <= 1);
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 rump_component_init(RUMP_COMPONENT_POSTINIT);
517
518 /* release cpu */
519 rump_unschedule();
520
521 return 0;
522 }
523
524 int
525 rump_init_server(const char *url)
526 {
527
528 return rumpuser_sp_init(url, &spops, ostype, osrelease, MACHINE);
529 }
530
531 void
532 cpu_reboot(int howto, char *bootstr)
533 {
534 int ruhow = 0;
535 void *finiarg;
536
537 printf("rump kernel halting...\n");
538
539 if (!RUMP_LOCALPROC_P(curproc))
540 finiarg = curproc->p_vmspace->vm_map.pmap;
541 else
542 finiarg = NULL;
543
544 /* dump means we really take the dive here */
545 if ((howto & RB_DUMP) || panicstr) {
546 ruhow = RUMPUSER_PANIC;
547 goto out;
548 }
549
550 /* try to sync */
551 if (!((howto & RB_NOSYNC) || panicstr)) {
552 if (rump_vfs_fini)
553 rump_vfs_fini();
554 }
555
556 /* your wish is my command */
557 if (howto & RB_HALT) {
558 printf("rump kernel halted\n");
559 rumpuser_sp_fini(finiarg);
560 for (;;) {
561 uint64_t sec = 5, nsec = 0;
562 int error;
563
564 rumpuser_nanosleep(&sec, &nsec, &error);
565 }
566 }
567
568 /* this function is __dead, we must exit */
569 out:
570 printf("halted\n");
571 rumpuser_sp_fini(finiarg);
572 rumpuser_exit(ruhow);
573 }
574
575 struct uio *
576 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
577 {
578 struct uio *uio;
579 enum uio_rw uiorw;
580
581 switch (rw) {
582 case RUMPUIO_READ:
583 uiorw = UIO_READ;
584 break;
585 case RUMPUIO_WRITE:
586 uiorw = UIO_WRITE;
587 break;
588 default:
589 panic("%s: invalid rw %d", __func__, rw);
590 }
591
592 uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
593 uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
594
595 uio->uio_iov->iov_base = buf;
596 uio->uio_iov->iov_len = bufsize;
597
598 uio->uio_iovcnt = 1;
599 uio->uio_offset = offset;
600 uio->uio_resid = bufsize;
601 uio->uio_rw = uiorw;
602 UIO_SETUP_SYSSPACE(uio);
603
604 return uio;
605 }
606
607 size_t
608 rump_uio_getresid(struct uio *uio)
609 {
610
611 return uio->uio_resid;
612 }
613
614 off_t
615 rump_uio_getoff(struct uio *uio)
616 {
617
618 return uio->uio_offset;
619 }
620
621 size_t
622 rump_uio_free(struct uio *uio)
623 {
624 size_t resid;
625
626 resid = uio->uio_resid;
627 kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
628 kmem_free(uio, sizeof(*uio));
629
630 return resid;
631 }
632
633 kauth_cred_t
634 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
635 {
636 kauth_cred_t cred;
637 int rv;
638
639 cred = kauth_cred_alloc();
640 kauth_cred_setuid(cred, uid);
641 kauth_cred_seteuid(cred, uid);
642 kauth_cred_setsvuid(cred, uid);
643 kauth_cred_setgid(cred, gid);
644 kauth_cred_setgid(cred, gid);
645 kauth_cred_setegid(cred, gid);
646 kauth_cred_setsvgid(cred, gid);
647 rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
648 /* oh this is silly. and by "this" I mean kauth_cred_setgroups() */
649 assert(rv == 0);
650
651 return cred;
652 }
653
654 void
655 rump_cred_put(kauth_cred_t cred)
656 {
657
658 kauth_cred_free(cred);
659 }
660
661 static int compcounter[RUMP_COMPONENT_MAX];
662 static int compinited[RUMP_COMPONENT_MAX];
663
664 static void
665 rump_component_init_cb(struct rump_component *rc, int type)
666 {
667
668 KASSERT(type < RUMP_COMPONENT_MAX);
669
670 if (rc->rc_type == type) {
671 rc->rc_init();
672 compcounter[type]++;
673 }
674 }
675
676 int
677 rump_component_count(enum rump_component_type type)
678 {
679
680 KASSERT(type <= RUMP_COMPONENT_MAX);
681 KASSERT(compinited[type]);
682 return compcounter[type];
683 }
684
685 void
686 rump_component_init(enum rump_component_type type)
687 {
688
689 KASSERT(!compinited[type]);
690 rumpuser_dl_component_init(type, rump_component_init_cb);
691 compinited[type] = 1;
692 }
693
694 /*
695 * Initialize a module which has already been loaded and linked
696 * with dlopen(). This is fundamentally the same as a builtin module.
697 */
698 int
699 rump_module_init(const struct modinfo * const *mip, size_t nmodinfo)
700 {
701
702 return module_builtin_add(mip, nmodinfo, true);
703 }
704
705 /*
706 * Finish module (flawless victory, fatality!).
707 */
708 int
709 rump_module_fini(const struct modinfo *mi)
710 {
711
712 return module_builtin_remove(mi, true);
713 }
714
715 /*
716 * Add loaded and linked module to the builtin list. It will
717 * later be initialized with module_init_class().
718 */
719
720 static void
721 add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo)
722 {
723
724 module_builtin_add(mip, nmodinfo, false);
725 }
726
727 int
728 rump_kernelfsym_load(void *symtab, uint64_t symsize,
729 char *strtab, uint64_t strsize)
730 {
731 static int inited = 0;
732 Elf64_Ehdr ehdr;
733
734 if (inited)
735 return EBUSY;
736 inited = 1;
737
738 /*
739 * Use 64bit header since it's bigger. Shouldn't make a
740 * difference, since we're passing in all zeroes anyway.
741 */
742 memset(&ehdr, 0, sizeof(ehdr));
743 ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize);
744
745 return 0;
746 }
747
748 static int
749 rump_proxy_syscall(int num, void *arg, register_t *retval)
750 {
751 struct lwp *l;
752 struct sysent *callp;
753 int rv;
754
755 if (__predict_false(num >= SYS_NSYSENT))
756 return ENOSYS;
757
758 callp = rump_sysent + num;
759 l = curlwp;
760 rv = sy_call(callp, l, (void *)arg, retval);
761
762 return rv;
763 }
764
765 static int
766 rump_proxy_rfork(void *priv, int flags, const char *comm)
767 {
768 struct vmspace *newspace;
769 struct proc *p;
770 int error;
771
772 if ((error = rump_lwproc_rfork(flags)) != 0)
773 return error;
774
775 /*
776 * Since it's a proxy proc, adjust the vmspace.
777 * Refcount will eternally be 1.
778 */
779 p = curproc;
780 newspace = kmem_zalloc(sizeof(*newspace), KM_SLEEP);
781 newspace->vm_refcnt = 1;
782 newspace->vm_map.pmap = priv;
783 KASSERT(p->p_vmspace == vmspace_kernel());
784 p->p_vmspace = newspace;
785 if (comm)
786 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
787
788 return 0;
789 }
790
791 /*
792 * Order all lwps in a process to exit. does *not* wait for them to drain.
793 */
794 static void
795 rump_proxy_lwpexit(void)
796 {
797 struct proc *p = curproc;
798 uint64_t where;
799 struct lwp *l;
800
801 mutex_enter(p->p_lock);
802 /*
803 * First pass: mark all lwps in the process with LW_RUMP_QEXIT
804 * so that they know they should exit.
805 */
806 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
807 if (l == curlwp)
808 continue;
809 l->l_flag |= LW_RUMP_QEXIT;
810 }
811 mutex_exit(p->p_lock);
812
813 /*
814 * Next, make sure everyone on all CPUs sees our status
815 * update. This keeps threads inside cv_wait() and makes
816 * sure we don't access a stale cv pointer later when
817 * we wake up the threads.
818 */
819
820 where = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
821 xc_wait(where);
822
823 /*
824 * Ok, all lwps are either:
825 * 1) not in the cv code
826 * 2) sleeping on l->l_private
827 * 3) sleeping on p->p_waitcv
828 *
829 * Either way, l_private is stable until we set PS_RUMP_LWPEXIT
830 * in p->p_sflag.
831 */
832
833 mutex_enter(p->p_lock);
834 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
835 if (l->l_private)
836 cv_broadcast(l->l_private);
837 }
838 p->p_sflag |= PS_RUMP_LWPEXIT;
839 cv_broadcast(&p->p_waitcv);
840 mutex_exit(p->p_lock);
841 }
842
843 /*
844 * Notify process that all threads have been drained and exec is complete.
845 */
846 static void
847 rump_proxy_execnotify(const char *comm)
848 {
849 struct proc *p = curproc;
850
851 fd_closeexec();
852 mutex_enter(p->p_lock);
853 KASSERT(p->p_nlwps == 1 && p->p_sflag & PS_RUMP_LWPEXIT);
854 p->p_sflag &= ~PS_RUMP_LWPEXIT;
855 mutex_exit(p->p_lock);
856 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
857 }
858
859 int
860 rump_boot_gethowto()
861 {
862
863 return boothowto;
864 }
865
866 void
867 rump_boot_sethowto(int howto)
868 {
869
870 boothowto = howto;
871 }
872
873 int
874 rump_getversion(void)
875 {
876
877 return __NetBSD_Version__;
878 }
879
880 /*
881 * Note: may be called unscheduled. Not fully safe since no locking
882 * of allevents (currently that's not even available).
883 */
884 void
885 rump_printevcnts()
886 {
887 struct evcnt *ev;
888
889 TAILQ_FOREACH(ev, &allevents, ev_list)
890 rumpuser_dprintf("%s / %s: %" PRIu64 "\n",
891 ev->ev_group, ev->ev_name, ev->ev_count);
892 }
893
894 /*
895 * If you use this interface ... well ... all bets are off.
896 * The original purpose is for the p2k fs server library to be
897 * able to use the same pid/lid for VOPs as the host kernel.
898 */
899 void
900 rump_allbetsareoff_setid(pid_t pid, int lid)
901 {
902 struct lwp *l = curlwp;
903 struct proc *p = l->l_proc;
904
905 l->l_lid = lid;
906 p->p_pid = pid;
907 }
908
909 #include <sys/pserialize.h>
910
911 static void
912 ipiemu(void *a1, void *a2)
913 {
914
915 xc__highpri_intr(NULL);
916 pserialize_switchpoint();
917 }
918
919 void
920 rump_xc_highpri(struct cpu_info *ci)
921 {
922
923 if (ci)
924 xc_unicast(0, ipiemu, NULL, NULL, ci);
925 else
926 xc_broadcast(0, ipiemu, NULL, NULL);
927 }
928