xref: /netbsd-src/sys/rump/librump/rumpkern/rump.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: rump.c,v 1.281 2013/12/16 15:36:30 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.281 2013/12/16 15:36:30 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 #include <sys/ktrace.h>
76 
77 #include <rump/rumpuser.h>
78 
79 #include <secmodel/suser/suser.h>
80 
81 #include <prop/proplib.h>
82 
83 #include <uvm/uvm_extern.h>
84 #include <uvm/uvm_readahead.h>
85 
86 #include "rump_private.h"
87 #include "rump_net_private.h"
88 #include "rump_vfs_private.h"
89 #include "rump_dev_private.h"
90 
91 char machine[] = MACHINE;
92 
93 struct proc *initproc;
94 
95 struct device rump_rootdev = {
96 	.dv_class = DV_VIRTUAL
97 };
98 
99 #ifdef RUMP_WITHOUT_THREADS
100 int rump_threads = 0;
101 #else
102 int rump_threads = 1;
103 #endif
104 
105 static int rump_hyp_syscall(int, void *, long *);
106 static int rump_hyp_rfork(void *, int, const char *);
107 static void rump_hyp_lwpexit(void);
108 static void rump_hyp_execnotify(const char *);
109 
110 static void rump_component_addlocal(void);
111 static void rump_component_load(const struct rump_component *);
112 static struct lwp *bootlwp;
113 
114 static char rump_msgbuf[16*1024]; /* 16k should be enough for std rump needs */
115 
116 #ifdef LOCKDEBUG
117 const int rump_lockdebug = 1;
118 #else
119 const int rump_lockdebug = 0;
120 #endif
121 bool rump_ttycomponent = false;
122 
123 static void
124 rump_aiodone_worker(struct work *wk, void *dummy)
125 {
126 	struct buf *bp = (struct buf *)wk;
127 
128 	KASSERT(&bp->b_work == wk);
129 	bp->b_iodone(bp);
130 }
131 
132 static int rump_inited;
133 
134 void (*rump_vfs_drainbufs)(int);
135 void (*rump_vfs_fini)(void);
136 int  (*rump_vfs_makeonedevnode)(dev_t, const char *,
137 				devmajor_t, devminor_t) = (void *)nullop;
138 int  (*rump_vfs_makedevnodes)(dev_t, const char *, char,
139 			      devmajor_t, devminor_t, int) = (void *)nullop;
140 
141 int rump__unavailable(void);
142 int rump__unavailable() {return EOPNOTSUPP;}
143 
144 __weak_alias(biodone,rump__unavailable);
145 __weak_alias(sopoll,rump__unavailable);
146 
147 void rump__unavailable_vfs_panic(void);
148 void rump__unavailable_vfs_panic() {panic("vfs component not available");}
149 __weak_alias(usermount_common_policy,rump__unavailable_vfs_panic);
150 
151 /* easier to write vfs-less clients */
152 __weak_alias(rump_pub_etfs_register,rump__unavailable);
153 __weak_alias(rump_pub_etfs_register_withsize,rump__unavailable);
154 __weak_alias(rump_pub_etfs_remove,rump__unavailable);
155 
156 rump_proc_vfs_init_fn rump_proc_vfs_init;
157 rump_proc_vfs_release_fn rump_proc_vfs_release;
158 
159 static void add_linkedin_modules(const struct modinfo *const *, size_t);
160 
161 /*
162  * Create some sysctl nodes.  why only this you ask.  well, init_sysctl
163  * is a kitchen sink in need of some gardening.  but i want to use
164  * others today.  Furthermore, creating a whole kitchen sink full of
165  * sysctl nodes is a waste of cycles for rump kernel bootstrap.
166  */
167 static void
168 mksysctls(void)
169 {
170 
171 	/* kern.hostname */
172 	sysctl_createv(NULL, 0, NULL, NULL,
173 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "kern", NULL,
174 	    NULL, 0, NULL, 0, CTL_KERN, CTL_EOL);
175 	/* XXX: setting hostnamelen is missing */
176 	sysctl_createv(NULL, 0, NULL, NULL,
177 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_STRING, "hostname",
178 	    SYSCTL_DESCR("System hostname"), NULL, 0,
179 	    hostname, MAXHOSTNAMELEN, CTL_KERN, KERN_HOSTNAME, CTL_EOL);
180 
181 	/* hw.pagesize */
182 	sysctl_createv(NULL, 0, NULL, NULL,
183 	    CTLFLAG_PERMANENT,
184 	    CTLTYPE_NODE, "hw", NULL,
185 	    NULL, 0, NULL, 0,
186 	    CTL_HW, CTL_EOL);
187 	sysctl_createv(NULL, 0, NULL, NULL,
188 	    CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
189 	    CTLTYPE_INT, "pagesize",
190 	    SYSCTL_DESCR("Software page size"),
191 	    NULL, PAGE_SIZE, NULL, 0,
192 	    CTL_HW, HW_PAGESIZE, CTL_EOL);
193 }
194 
195 /* there's no convenient kernel entry point for this, so just craft out own */
196 static pid_t
197 spgetpid(void)
198 {
199 
200 	return curproc->p_pid;
201 }
202 
203 static const struct rumpuser_hyperup hyp = {
204 	.hyp_schedule		= rump_schedule,
205 	.hyp_unschedule		= rump_unschedule,
206 	.hyp_backend_unschedule	= rump_user_unschedule,
207 	.hyp_backend_schedule	= rump_user_schedule,
208 	.hyp_lwproc_switch	= rump_lwproc_switch,
209 	.hyp_lwproc_release	= rump_lwproc_releaselwp,
210 	.hyp_lwproc_rfork	= rump_hyp_rfork,
211 	.hyp_lwproc_newlwp	= rump_lwproc_newlwp,
212 	.hyp_lwproc_curlwp	= rump_lwproc_curlwp,
213 	.hyp_lwpexit		= rump_hyp_lwpexit,
214 	.hyp_syscall		= rump_hyp_syscall,
215 	.hyp_execnotify		= rump_hyp_execnotify,
216 	.hyp_getpid		= spgetpid,
217 };
218 
219 int
220 rump_daemonize_begin(void)
221 {
222 
223 	if (rump_inited)
224 		return EALREADY;
225 
226 	return rumpuser_daemonize_begin();
227 }
228 
229 int
230 rump_daemonize_done(int error)
231 {
232 
233 	return rumpuser_daemonize_done(error);
234 }
235 
236 RUMP_COMPONENT(RUMP_COMPONENT_POSTINIT)
237 {
238 	__link_set_decl(rump_components, struct rump_component);
239 
240 	/*
241 	 * Trick compiler into generating references so that statically
242 	 * linked rump kernels are generated with the link set symbols.
243 	 */
244 	asm("" :: "r"(__start_link_set_rump_components));
245 	asm("" :: "r"(__stop_link_set_rump_components));
246 }
247 
248 int
249 rump_init(void)
250 {
251 	char buf[256];
252 	struct timespec ts;
253 	int64_t sec;
254 	long nsec;
255 	struct lwp *l, *initlwp;
256 	int i, numcpu;
257 
258 	/* not reentrant */
259 	if (rump_inited)
260 		return 0;
261 	else if (rump_inited == -1)
262 		panic("rump_init: host process restart required");
263 	else
264 		rump_inited = 1;
265 
266 	/* initialize hypervisor */
267 	if (rumpuser_init(RUMPUSER_VERSION, &hyp) != 0) {
268 		rumpuser_dprintf("rumpuser init failed\n");
269 		return EINVAL;
270 	}
271 
272 	/* init minimal lwp/cpu context */
273 	l = &lwp0;
274 	l->l_lid = 1;
275 	l->l_cpu = l->l_target_cpu = rump_cpu;
276 	l->l_fd = &filedesc0;
277 
278 	/* lwp0 isn't created like other threads, so notify hypervisor here */
279 	rumpuser_curlwpop(RUMPUSER_LWP_CREATE, l);
280 	rumpuser_curlwpop(RUMPUSER_LWP_SET, l);
281 
282 	/* retrieve env vars which affect the early stage of bootstrap */
283 	if (rumpuser_getparam("RUMP_THREADS", buf, sizeof(buf)) == 0) {
284 		rump_threads = *buf != '0';
285 	}
286 	if (rumpuser_getparam("RUMP_VERBOSE", buf, sizeof(buf)) == 0) {
287 		if (*buf != '0')
288 			boothowto = AB_VERBOSE;
289 	}
290 
291 	if (rumpuser_getparam(RUMPUSER_PARAM_NCPU, buf, sizeof(buf)) != 0)
292 		panic("mandatory hypervisor configuration (NCPU) missing");
293 	numcpu = strtoll(buf, NULL, 10);
294 	if (numcpu < 1) {
295 		panic("rump kernels are not lightweight enough for \"%d\" CPUs",
296 		    numcpu);
297 	}
298 
299 	rump_thread_init();
300 	rump_cpus_bootstrap(&numcpu);
301 
302 	rumpuser_clock_gettime(RUMPUSER_CLOCK_RELWALL, &sec, &nsec);
303 	boottime.tv_sec = sec;
304 	boottime.tv_nsec = nsec;
305 
306 	initmsgbuf(rump_msgbuf, sizeof(rump_msgbuf));
307 	aprint_verbose("%s%s", copyright, version);
308 
309 	rump_intr_init(numcpu);
310 
311 	rump_tsleep_init();
312 
313 	rumpuser_mutex_init(&rump_giantlock, RUMPUSER_MTX_SPIN);
314 	ksyms_init();
315 	uvm_init();
316 	evcnt_init();
317 
318 	kcpuset_sysinit();
319 	once_init();
320 	kernconfig_lock_init();
321 	prop_kern_init();
322 
323 	kmem_init();
324 	kmeminit();
325 
326 	uvm_ra_init();
327 	uao_init();
328 
329 	mutex_obj_init();
330 	callout_startup();
331 
332 	kprintf_init();
333 	pserialize_init();
334 	loginit();
335 
336 	kauth_init();
337 
338 	secmodel_init();
339 
340 	rnd_init();
341 
342 	/*
343 	 * Create the kernel cprng.  Yes, it's currently stubbed out
344 	 * to arc4random() for RUMP, but this won't always be so.
345 	 */
346 	kern_cprng = cprng_strong_create("kernel", IPL_VM,
347 					 CPRNG_INIT_ANY|CPRNG_REKEY_ANY);
348 
349 	procinit();
350 	proc0_init();
351 	sysctl_init();
352 	uid_init();
353 	chgproccnt(0, 1);
354 
355 	l->l_proc = &proc0;
356 	lwp_update_creds(l);
357 
358 	lwpinit_specificdata();
359 	lwp_initspecific(&lwp0);
360 
361 	rump_biglock_init();
362 
363 	rump_scheduler_init(numcpu);
364 	/* revert temporary context and schedule a semireal context */
365 	rumpuser_curlwpop(RUMPUSER_LWP_CLEAR, l);
366 	initproc = &proc0; /* borrow proc0 before we get initproc started */
367 	rump_schedule();
368 	bootlwp = curlwp;
369 
370 	percpu_init();
371 	inittimecounter();
372 	ntp_init();
373 
374 #ifdef KTRACE
375 	ktrinit();
376 #endif
377 
378 	ts = boottime;
379 	tc_setclock(&ts);
380 
381 	/* we are mostly go.  do per-cpu subsystem init */
382 	for (i = 0; i < numcpu; i++) {
383 		struct cpu_info *ci = cpu_lookup(i);
384 
385 		/* attach non-bootstrap CPUs */
386 		if (i > 0) {
387 			rump_cpu_attach(ci);
388 			ncpu++;
389 		}
390 
391 		callout_init_cpu(ci);
392 		softint_init(ci);
393 		xc_init_cpu(ci);
394 		pool_cache_cpu_init(ci);
395 		selsysinit(ci);
396 		percpu_init_cpu(ci);
397 
398 		TAILQ_INIT(&ci->ci_data.cpu_ld_locks);
399 		__cpu_simple_lock_init(&ci->ci_data.cpu_ld_lock);
400 
401 		aprint_verbose("cpu%d at thinair0: rump virtual cpu\n", i);
402 	}
403 
404 	/* CPUs are up.  allow kernel threads to run */
405 	rump_thread_allow();
406 
407 	mksysctls();
408 	kqueue_init();
409 	iostat_init();
410 	fd_sys_init();
411 	module_init();
412 	devsw_init();
413 	pipe_init();
414 	resource_init();
415 	procinit_sysctl();
416 
417 	/* start page baroness */
418 	if (rump_threads) {
419 		if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL,
420 		    uvm_pageout, NULL, &uvm.pagedaemon_lwp, "pdaemon") != 0)
421 			panic("pagedaemon create failed");
422 	} else
423 		uvm.pagedaemon_lwp = NULL; /* doesn't match curlwp */
424 
425 	/* process dso's */
426 	rumpuser_dl_bootstrap(add_linkedin_modules,
427 	    rump_kernelfsym_load, rump_component_load);
428 
429 	rump_component_addlocal();
430 	rump_component_init(RUMP_COMPONENT_KERN);
431 
432 	/* initialize factions, if present */
433 	rump_component_init(RUMP__FACTION_VFS);
434 	/* pnbuf_cache is used even without vfs */
435 	if (rump_component_count(RUMP__FACTION_VFS) == 0) {
436 		pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
437 		    NULL, IPL_NONE, NULL, NULL, NULL);
438 	}
439 	rump_component_init(RUMP__FACTION_NET);
440 	rump_component_init(RUMP__FACTION_DEV);
441 	KASSERT(rump_component_count(RUMP__FACTION_VFS) <= 1
442 	    && rump_component_count(RUMP__FACTION_NET) <= 1
443 	    && rump_component_count(RUMP__FACTION_DEV) <= 1);
444 
445 	rump_component_init(RUMP_COMPONENT_KERN_VFS);
446 
447 	/*
448 	 * if we initialized the tty component above, the tyttymtx is
449 	 * now initialized.  otherwise, we need to initialize it.
450 	 */
451 	if (!rump_ttycomponent)
452 		mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_VM);
453 
454 	cold = 0;
455 
456 	/* aieeeedondest */
457 	if (rump_threads) {
458 		if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
459 		    rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE))
460 			panic("aiodoned");
461 	}
462 
463 	sysctl_finalize();
464 
465 	module_init_class(MODULE_CLASS_ANY);
466 
467 	if (rumpuser_getparam(RUMPUSER_PARAM_HOSTNAME,
468 	    hostname, MAXHOSTNAMELEN) != 0) {
469 		panic("mandatory hypervisor configuration (HOSTNAME) missing");
470 	}
471 	hostnamelen = strlen(hostname);
472 
473 	sigemptyset(&sigcantmask);
474 
475 	if (rump_threads)
476 		vmem_rehash_start();
477 
478 	/*
479 	 * Create init (proc 1), used to attach implicit threads in rump.
480 	 * (note: must be done after vfsinit to get cwdi)
481 	 */
482 	initlwp = rump__lwproc_alloclwp(NULL);
483 	mutex_enter(proc_lock);
484 	initproc = proc_find_raw(1);
485 	mutex_exit(proc_lock);
486 	if (initproc == NULL)
487 		panic("where in the world is initproc?");
488 
489 	/*
490 	 * Adjust syscall vector in case factions were dlopen()'d
491 	 * before calling rump_init().
492 	 * (modules will handle dynamic syscalls the usual way)
493 	 *
494 	 * Note: this will adjust the function vectors of
495 	 * syscalls which use a funcalias (getpid etc.), but
496 	 * it makes no difference.
497 	 */
498 	for (i = 0; i < SYS_NSYSENT; i++) {
499 		void *sym;
500 
501 		if (rump_sysent[i].sy_flags & SYCALL_NOSYS ||
502 		    *syscallnames[i] == '#' ||
503 		    rump_sysent[i].sy_call == sys_nomodule)
504 			continue;
505 
506 		/*
507 		 * deal with compat wrappers.  makesyscalls.sh should
508 		 * generate the necessary info instead of this hack,
509 		 * though.  ugly, fix it later.
510 		 */
511 #define CPFX "compat_"
512 #define CPFXLEN (sizeof(CPFX)-1)
513 		if (strncmp(syscallnames[i], CPFX, CPFXLEN) == 0) {
514 			const char *p = syscallnames[i] + CPFXLEN;
515 			size_t namelen;
516 
517 			/* skip version number */
518 			while (*p >= '0' && *p <= '9')
519 				p++;
520 			if (p == syscallnames[i] + CPFXLEN || *p != '_')
521 				panic("invalid syscall name %s\n",
522 				    syscallnames[i]);
523 
524 			/* skip over the next underscore */
525 			p++;
526 			namelen = p + (sizeof("rumpns_")-1) - syscallnames[i];
527 
528 			strcpy(buf, "rumpns_");
529 			strcat(buf, syscallnames[i]);
530 			/* XXX: no strncat in the kernel */
531 			strcpy(buf+namelen, "sys_");
532 			strcat(buf, p);
533 #undef CPFX
534 #undef CPFXLEN
535 		} else {
536 			sprintf(buf, "rumpns_sys_%s", syscallnames[i]);
537 		}
538 		if ((sym = rumpuser_dl_globalsym(buf)) != NULL
539 		    && sym != rump_sysent[i].sy_call) {
540 #if 0
541 			rumpuser_dprintf("adjusting %s: %p (old %p)\n",
542 			    syscallnames[i], sym, rump_sysent[i].sy_call);
543 #endif
544 			rump_sysent[i].sy_call = sym;
545 		}
546 	}
547 
548 	rump_component_init(RUMP_COMPONENT_POSTINIT);
549 
550 	/* component inits done */
551 	bootlwp = NULL;
552 
553 	/* open 0/1/2 for init */
554 	KASSERT(rump_lwproc_curlwp() == NULL);
555 	rump_lwproc_switch(initlwp);
556 	rump_consdev_init();
557 	rump_lwproc_switch(NULL);
558 
559 	/* release cpu */
560 	rump_unschedule();
561 
562 	return 0;
563 }
564 /* historic compat */
565 __strong_alias(rump__init,rump_init);
566 
567 int
568 rump_init_server(const char *url)
569 {
570 
571 	return rumpuser_sp_init(url, ostype, osrelease, MACHINE);
572 }
573 
574 void
575 cpu_reboot(int howto, char *bootstr)
576 {
577 	int ruhow = 0;
578 	void *finiarg;
579 
580 	printf("rump kernel halting...\n");
581 
582 	if (!RUMP_LOCALPROC_P(curproc))
583 		finiarg = curproc->p_vmspace->vm_map.pmap;
584 	else
585 		finiarg = NULL;
586 
587 	/* dump means we really take the dive here */
588 	if ((howto & RB_DUMP) || panicstr) {
589 		ruhow = RUMPUSER_PANIC;
590 		goto out;
591 	}
592 
593 	/* try to sync */
594 	if (!((howto & RB_NOSYNC) || panicstr)) {
595 		if (rump_vfs_fini)
596 			rump_vfs_fini();
597 	}
598 
599 	doshutdownhooks();
600 
601 	/* your wish is my command */
602 	if (howto & RB_HALT) {
603 		printf("rump kernel halted\n");
604 		rumpuser_sp_fini(finiarg);
605 		for (;;) {
606 			rumpuser_clock_sleep(RUMPUSER_CLOCK_RELWALL, 10, 0);
607 		}
608 	}
609 
610 	/* this function is __dead, we must exit */
611  out:
612 	printf("halted\n");
613 	rumpuser_sp_fini(finiarg);
614 	rumpuser_exit(ruhow);
615 }
616 
617 struct uio *
618 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
619 {
620 	struct uio *uio;
621 	enum uio_rw uiorw;
622 
623 	switch (rw) {
624 	case RUMPUIO_READ:
625 		uiorw = UIO_READ;
626 		break;
627 	case RUMPUIO_WRITE:
628 		uiorw = UIO_WRITE;
629 		break;
630 	default:
631 		panic("%s: invalid rw %d", __func__, rw);
632 	}
633 
634 	uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
635 	uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
636 
637 	uio->uio_iov->iov_base = buf;
638 	uio->uio_iov->iov_len = bufsize;
639 
640 	uio->uio_iovcnt = 1;
641 	uio->uio_offset = offset;
642 	uio->uio_resid = bufsize;
643 	uio->uio_rw = uiorw;
644 	UIO_SETUP_SYSSPACE(uio);
645 
646 	return uio;
647 }
648 
649 size_t
650 rump_uio_getresid(struct uio *uio)
651 {
652 
653 	return uio->uio_resid;
654 }
655 
656 off_t
657 rump_uio_getoff(struct uio *uio)
658 {
659 
660 	return uio->uio_offset;
661 }
662 
663 size_t
664 rump_uio_free(struct uio *uio)
665 {
666 	size_t resid;
667 
668 	resid = uio->uio_resid;
669 	kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
670 	kmem_free(uio, sizeof(*uio));
671 
672 	return resid;
673 }
674 
675 kauth_cred_t
676 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
677 {
678 	kauth_cred_t cred;
679 	int rv;
680 
681 	cred = kauth_cred_alloc();
682 	kauth_cred_setuid(cred, uid);
683 	kauth_cred_seteuid(cred, uid);
684 	kauth_cred_setsvuid(cred, uid);
685 	kauth_cred_setgid(cred, gid);
686 	kauth_cred_setgid(cred, gid);
687 	kauth_cred_setegid(cred, gid);
688 	kauth_cred_setsvgid(cred, gid);
689 	rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
690 	/* oh this is silly.  and by "this" I mean kauth_cred_setgroups() */
691 	assert(rv == 0);
692 
693 	return cred;
694 }
695 
696 void
697 rump_cred_put(kauth_cred_t cred)
698 {
699 
700 	kauth_cred_free(cred);
701 }
702 
703 static int compcounter[RUMP_COMPONENT_MAX];
704 static int compinited[RUMP_COMPONENT_MAX];
705 
706 /*
707  * Yea, this is O(n^2), but we're only looking at a handful of components.
708  * Components are always initialized from the thread that called rump_init().
709  * Could also free these when done with them, but prolly not worth it.
710  */
711 struct compstore {
712 	const struct rump_component *cs_rc;
713 	LIST_ENTRY(compstore) cs_entries;
714 };
715 static LIST_HEAD(, compstore) cshead = LIST_HEAD_INITIALIZER(cshead);
716 
717 /*
718  * add components which are visible from the current object.
719  */
720 static void
721 rump_component_addlocal(void)
722 {
723 	__link_set_decl(rump_components, struct rump_component);
724 	struct rump_component *const *rc;
725 
726 	__link_set_foreach(rc, rump_components) {
727 		rump_component_load(*rc);
728 	}
729 }
730 
731 static void
732 rump_component_load(const struct rump_component *rc)
733 {
734 	struct compstore *cs;
735 
736 	KASSERT(curlwp == bootlwp);
737 
738 	LIST_FOREACH(cs, &cshead, cs_entries) {
739 		if (rc == cs->cs_rc)
740 			return;
741 	}
742 
743 	cs = kmem_alloc(sizeof(*cs), KM_SLEEP);
744 	cs->cs_rc = rc;
745 	LIST_INSERT_HEAD(&cshead, cs, cs_entries);
746 	KASSERT(rc->rc_type < RUMP_COMPONENT_MAX);
747 	compcounter[rc->rc_type]++;
748 }
749 
750 int
751 rump_component_count(enum rump_component_type type)
752 {
753 
754 	KASSERT(curlwp == bootlwp);
755 	KASSERT(type < RUMP_COMPONENT_MAX);
756 	return compcounter[type];
757 }
758 
759 void
760 rump_component_init(enum rump_component_type type)
761 {
762 	struct compstore *cs;
763 	const struct rump_component *rc;
764 
765 	KASSERT(curlwp == bootlwp);
766 	KASSERT(!compinited[type]);
767 	LIST_FOREACH(cs, &cshead, cs_entries) {
768 		rc = cs->cs_rc;
769 		if (rc->rc_type == type)
770 			rc->rc_init();
771 	}
772 	compinited[type] = 1;
773 }
774 
775 /*
776  * Initialize a module which has already been loaded and linked
777  * with dlopen(). This is fundamentally the same as a builtin module.
778  */
779 int
780 rump_module_init(const struct modinfo * const *mip, size_t nmodinfo)
781 {
782 
783 	return module_builtin_add(mip, nmodinfo, true);
784 }
785 
786 /*
787  * Finish module (flawless victory, fatality!).
788  */
789 int
790 rump_module_fini(const struct modinfo *mi)
791 {
792 
793 	return module_builtin_remove(mi, true);
794 }
795 
796 /*
797  * Add loaded and linked module to the builtin list.  It will
798  * later be initialized with module_init_class().
799  */
800 
801 static void
802 add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo)
803 {
804 
805 	module_builtin_add(mip, nmodinfo, false);
806 }
807 
808 int
809 rump_kernelfsym_load(void *symtab, uint64_t symsize,
810 	char *strtab, uint64_t strsize)
811 {
812 	static int inited = 0;
813 	Elf64_Ehdr ehdr;
814 
815 	if (inited)
816 		return EBUSY;
817 	inited = 1;
818 
819 	/*
820 	 * Use 64bit header since it's bigger.  Shouldn't make a
821 	 * difference, since we're passing in all zeroes anyway.
822 	 */
823 	memset(&ehdr, 0, sizeof(ehdr));
824 	ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize);
825 
826 	return 0;
827 }
828 
829 static int
830 rump_hyp_syscall(int num, void *arg, long *retval)
831 {
832 	register_t regrv[2] = {0, 0};
833 	struct lwp *l;
834 	struct sysent *callp;
835 	int rv;
836 
837 	if (__predict_false(num >= SYS_NSYSENT))
838 		return ENOSYS;
839 
840 	/* XXX: always uses native syscall vector */
841 	callp = rump_sysent + num;
842 	l = curlwp;
843 	rv = sy_invoke(callp, l, (void *)arg, regrv, num);
844 	retval[0] = regrv[0];
845 	retval[1] = regrv[1];
846 
847 	return rv;
848 }
849 
850 static int
851 rump_hyp_rfork(void *priv, int flags, const char *comm)
852 {
853 	struct vmspace *newspace;
854 	struct proc *p;
855 	int error;
856 
857 	if ((error = rump_lwproc_rfork(flags)) != 0)
858 		return error;
859 
860 	/*
861 	 * Since it's a proxy proc, adjust the vmspace.
862 	 * Refcount will eternally be 1.
863 	 */
864 	p = curproc;
865 	newspace = kmem_zalloc(sizeof(*newspace), KM_SLEEP);
866 	newspace->vm_refcnt = 1;
867 	newspace->vm_map.pmap = priv;
868 	KASSERT(p->p_vmspace == vmspace_kernel());
869 	p->p_vmspace = newspace;
870 	if (comm)
871 		strlcpy(p->p_comm, comm, sizeof(p->p_comm));
872 
873 	return 0;
874 }
875 
876 /*
877  * Order all lwps in a process to exit.  does *not* wait for them to drain.
878  */
879 static void
880 rump_hyp_lwpexit(void)
881 {
882 	struct proc *p = curproc;
883 	uint64_t where;
884 	struct lwp *l;
885 
886 	mutex_enter(p->p_lock);
887 	/*
888 	 * First pass: mark all lwps in the process with LW_RUMP_QEXIT
889 	 * so that they know they should exit.
890 	 */
891 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
892 		if (l == curlwp)
893 			continue;
894 		l->l_flag |= LW_RUMP_QEXIT;
895 	}
896 	mutex_exit(p->p_lock);
897 
898 	/*
899 	 * Next, make sure everyone on all CPUs sees our status
900 	 * update.  This keeps threads inside cv_wait() and makes
901 	 * sure we don't access a stale cv pointer later when
902 	 * we wake up the threads.
903 	 */
904 
905 	where = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
906 	xc_wait(where);
907 
908 	/*
909 	 * Ok, all lwps are either:
910 	 *  1) not in the cv code
911 	 *  2) sleeping on l->l_private
912 	 *  3) sleeping on p->p_waitcv
913 	 *
914 	 * Either way, l_private is stable until we set PS_RUMP_LWPEXIT
915 	 * in p->p_sflag.
916 	 */
917 
918 	mutex_enter(p->p_lock);
919 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
920 		if (l->l_private)
921 			cv_broadcast(l->l_private);
922 	}
923 	p->p_sflag |= PS_RUMP_LWPEXIT;
924 	cv_broadcast(&p->p_waitcv);
925 	mutex_exit(p->p_lock);
926 }
927 
928 /*
929  * Notify process that all threads have been drained and exec is complete.
930  */
931 static void
932 rump_hyp_execnotify(const char *comm)
933 {
934 	struct proc *p = curproc;
935 
936 	fd_closeexec();
937 	mutex_enter(p->p_lock);
938 	KASSERT(p->p_nlwps == 1 && p->p_sflag & PS_RUMP_LWPEXIT);
939 	p->p_sflag &= ~PS_RUMP_LWPEXIT;
940 	mutex_exit(p->p_lock);
941 	strlcpy(p->p_comm, comm, sizeof(p->p_comm));
942 }
943 
944 int
945 rump_boot_gethowto()
946 {
947 
948 	return boothowto;
949 }
950 
951 void
952 rump_boot_sethowto(int howto)
953 {
954 
955 	boothowto = howto;
956 }
957 
958 int
959 rump_getversion(void)
960 {
961 
962 	return __NetBSD_Version__;
963 }
964 
965 /*
966  * Note: may be called unscheduled.  Not fully safe since no locking
967  * of allevents (currently that's not even available).
968  */
969 void
970 rump_printevcnts()
971 {
972 	struct evcnt *ev;
973 
974 	TAILQ_FOREACH(ev, &allevents, ev_list)
975 		rumpuser_dprintf("%s / %s: %" PRIu64 "\n",
976 		    ev->ev_group, ev->ev_name, ev->ev_count);
977 }
978 
979 /*
980  * If you use this interface ... well ... all bets are off.
981  * The original purpose is for the p2k fs server library to be
982  * able to use the same pid/lid for VOPs as the host kernel.
983  */
984 void
985 rump_allbetsareoff_setid(pid_t pid, int lid)
986 {
987 	struct lwp *l = curlwp;
988 	struct proc *p = l->l_proc;
989 
990 	l->l_lid = lid;
991 	p->p_pid = pid;
992 }
993 
994 #include <sys/pserialize.h>
995 
996 static void
997 ipiemu(void *a1, void *a2)
998 {
999 
1000 	xc__highpri_intr(NULL);
1001 	pserialize_switchpoint();
1002 }
1003 
1004 void
1005 rump_xc_highpri(struct cpu_info *ci)
1006 {
1007 
1008 	if (ci)
1009 		xc_unicast(0, ipiemu, NULL, NULL, ci);
1010 	else
1011 		xc_broadcast(0, ipiemu, NULL, NULL);
1012 }
1013 
1014 int
1015 rump_syscall(int num, void *data, size_t dlen, register_t *retval)
1016 {
1017 	struct proc *p;
1018 	struct emul *e;
1019 	struct sysent *callp;
1020 	const int *etrans = NULL;
1021 	int rv;
1022 
1023 	rump_schedule();
1024 	p = curproc;
1025 	e = p->p_emul;
1026 #ifndef __HAVE_MINIMAL_EMUL
1027 	KASSERT(num > 0 && num < e->e_nsysent);
1028 #endif
1029 	callp = e->e_sysent + num;
1030 
1031 	rv = sy_invoke(callp, curlwp, data, retval, num);
1032 
1033 	/*
1034 	 * I hope that (!__HAVE_MINIMAL_EMUL || __HAVE_SYSCALL_INTERN) is
1035 	 * an invariant ...
1036 	 */
1037 #if !defined(__HAVE_MINIMAL_EMUL)
1038 	etrans = e->e_errno;
1039 #elif defined(__HAVE_SYSCALL_INTERN)
1040 	etrans = p->p_emuldata;
1041 #endif
1042 
1043 	if (etrans) {
1044 		rv = etrans[rv];
1045 		/*
1046 		 * XXX: small hack since Linux etrans vectors on some
1047 		 * archs contain negative errnos, but rump_syscalls
1048 		 * uses the -1 + errno ABI.  Note that these
1049 		 * negative values are always the result of translation,
1050 		 * otherwise the above translation method would not
1051 		 * work very well.
1052 		 */
1053 		if (rv < 0)
1054 			rv = -rv;
1055 	}
1056 	rump_unschedule();
1057 
1058 	return rv;
1059 }
1060