Home | History | Annotate | Line # | Download | only in rumpkern
rump.c revision 1.167
      1  1.167   pooka /*	$NetBSD: rump.c,v 1.167 2010/04/28 11:34:18 pooka Exp $	*/
      2    1.1   pooka 
      3    1.1   pooka /*
      4    1.1   pooka  * Copyright (c) 2007 Antti Kantee.  All Rights Reserved.
      5    1.1   pooka  *
      6    1.1   pooka  * Development of this software was supported by Google Summer of Code.
      7    1.1   pooka  *
      8    1.1   pooka  * Redistribution and use in source and binary forms, with or without
      9    1.1   pooka  * modification, are permitted provided that the following conditions
     10    1.1   pooka  * are met:
     11    1.1   pooka  * 1. Redistributions of source code must retain the above copyright
     12    1.1   pooka  *    notice, this list of conditions and the following disclaimer.
     13    1.1   pooka  * 2. Redistributions in binary form must reproduce the above copyright
     14    1.1   pooka  *    notice, this list of conditions and the following disclaimer in the
     15    1.1   pooka  *    documentation and/or other materials provided with the distribution.
     16    1.1   pooka  *
     17    1.1   pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     18    1.1   pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19    1.1   pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20    1.1   pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21    1.1   pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22    1.1   pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     23    1.1   pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24    1.1   pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25    1.1   pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26    1.1   pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27    1.1   pooka  * SUCH DAMAGE.
     28    1.1   pooka  */
     29    1.1   pooka 
     30   1.78   pooka #include <sys/cdefs.h>
     31  1.167   pooka __KERNEL_RCSID(0, "$NetBSD: rump.c,v 1.167 2010/04/28 11:34:18 pooka Exp $");
     32  1.165   pooka 
     33  1.165   pooka #include <sys/systm.h>
     34  1.165   pooka #define ELFSIZE ARCH_ELFSIZE
     35   1.78   pooka 
     36    1.1   pooka #include <sys/param.h>
     37   1.64   pooka #include <sys/atomic.h>
     38   1.75   pooka #include <sys/buf.h>
     39   1.66   pooka #include <sys/callout.h>
     40   1.79   pooka #include <sys/conf.h>
     41   1.27   pooka #include <sys/cpu.h>
     42  1.144   pooka #include <sys/device.h>
     43  1.100   pooka #include <sys/evcnt.h>
     44   1.98   pooka #include <sys/event.h>
     45  1.137   pooka #include <sys/exec_elf.h>
     46    1.1   pooka #include <sys/filedesc.h>
     47   1.72   pooka #include <sys/iostat.h>
     48    1.1   pooka #include <sys/kauth.h>
     49   1.80   pooka #include <sys/kernel.h>
     50   1.14   pooka #include <sys/kmem.h>
     51   1.81   pooka #include <sys/kprintf.h>
     52   1.82   pooka #include <sys/ksyms.h>
     53   1.81   pooka #include <sys/msgbuf.h>
     54   1.49  simonb #include <sys/module.h>
     55   1.71   pooka #include <sys/once.h>
     56   1.65   pooka #include <sys/percpu.h>
     57  1.139   pooka #include <sys/pipe.h>
     58  1.162   pooka #include <sys/pool.h>
     59    1.1   pooka #include <sys/queue.h>
     60  1.121   pooka #include <sys/reboot.h>
     61    1.1   pooka #include <sys/resourcevar.h>
     62   1.27   pooka #include <sys/select.h>
     63   1.87   pooka #include <sys/sysctl.h>
     64   1.96   pooka #include <sys/syscall.h>
     65  1.160   pooka #include <sys/timetc.h>
     66   1.87   pooka #include <sys/tty.h>
     67   1.68   pooka #include <sys/uidinfo.h>
     68   1.89   pooka #include <sys/vmem.h>
     69  1.143   pooka #include <sys/xcall.h>
     70    1.1   pooka 
     71   1.48   pooka #include <rump/rumpuser.h>
     72   1.48   pooka 
     73  1.117    elad #include <secmodel/suser/suser.h>
     74  1.101   pooka 
     75  1.114   pooka #include <prop/proplib.h>
     76  1.114   pooka 
     77  1.136   pooka #include <uvm/uvm_readahead.h>
     78  1.136   pooka 
     79    1.8   pooka #include "rump_private.h"
     80   1.71   pooka #include "rump_net_private.h"
     81   1.75   pooka #include "rump_vfs_private.h"
     82  1.112   pooka #include "rump_dev_private.h"
     83    1.1   pooka 
     84   1.40      ad struct proc proc0;
     85   1.81   pooka struct session rump_session = {
     86   1.81   pooka 	.s_count = 1,
     87   1.81   pooka 	.s_flags = 0,
     88   1.81   pooka 	.s_leader = &proc0,
     89   1.81   pooka 	.s_login = "rumphobo",
     90   1.81   pooka 	.s_sid = 0,
     91   1.81   pooka };
     92   1.81   pooka struct pgrp rump_pgrp = {
     93   1.81   pooka 	.pg_members = LIST_HEAD_INITIALIZER(pg_members),
     94   1.81   pooka 	.pg_session = &rump_session,
     95   1.81   pooka 	.pg_jobc = 1,
     96   1.81   pooka };
     97    1.1   pooka struct pstats rump_stats;
     98    1.1   pooka struct plimit rump_limits;
     99   1.40      ad struct filedesc rump_filedesc0;
    100   1.36   pooka struct proclist allproc;
    101  1.164   pooka char machine[] = MACHINE;
    102   1.59   pooka static kauth_cred_t rump_susercred;
    103    1.1   pooka 
    104  1.101   pooka /* pretend the master rump proc is init */
    105  1.101   pooka struct proc *initproc = &proc0;
    106  1.101   pooka 
    107   1.77   pooka struct rumpuser_mtx *rump_giantlock;
    108   1.19   pooka 
    109  1.144   pooka struct device rump_rootdev = {
    110  1.144   pooka 	.dv_class = DV_VIRTUAL
    111  1.144   pooka };
    112  1.144   pooka 
    113   1.54   pooka #ifdef RUMP_WITHOUT_THREADS
    114   1.54   pooka int rump_threads = 0;
    115   1.54   pooka #else
    116   1.54   pooka int rump_threads = 1;
    117   1.54   pooka #endif
    118   1.54   pooka 
    119  1.156   pooka static char rump_msgbuf[16*1024]; /* 16k should be enough for std rump needs */
    120  1.156   pooka 
    121   1.14   pooka static void
    122   1.14   pooka rump_aiodone_worker(struct work *wk, void *dummy)
    123   1.14   pooka {
    124   1.14   pooka 	struct buf *bp = (struct buf *)wk;
    125   1.14   pooka 
    126   1.14   pooka 	KASSERT(&bp->b_work == wk);
    127   1.14   pooka 	bp->b_iodone(bp);
    128   1.14   pooka }
    129   1.14   pooka 
    130   1.51   pooka static int rump_inited;
    131   1.27   pooka 
    132  1.162   pooka /*
    133  1.162   pooka  * Make sure pnbuf_cache is available even without vfs
    134  1.162   pooka  */
    135  1.162   pooka struct pool_cache *pnbuf_cache;
    136  1.162   pooka int rump_initpnbufpool(void);
    137  1.162   pooka int rump_initpnbufpool(void)
    138  1.162   pooka {
    139  1.162   pooka 
    140  1.162   pooka         pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
    141  1.162   pooka 	    NULL, IPL_NONE, NULL, NULL, NULL);
    142  1.162   pooka 	return EOPNOTSUPP;
    143  1.162   pooka }
    144  1.162   pooka 
    145  1.126   pooka int rump__unavailable(void);
    146  1.126   pooka int rump__unavailable() {return EOPNOTSUPP;}
    147   1.86   pooka __weak_alias(rump_net_init,rump__unavailable);
    148  1.162   pooka __weak_alias(rump_vfs_init,rump_initpnbufpool);
    149  1.112   pooka __weak_alias(rump_dev_init,rump__unavailable);
    150   1.75   pooka 
    151  1.121   pooka __weak_alias(rump_vfs_fini,rump__unavailable);
    152  1.121   pooka 
    153  1.113   pooka __weak_alias(biodone,rump__unavailable);
    154  1.126   pooka __weak_alias(sopoll,rump__unavailable);
    155  1.113   pooka 
    156   1.92   pooka void rump__unavailable_vfs_panic(void);
    157   1.92   pooka void rump__unavailable_vfs_panic() {panic("vfs component not available");}
    158  1.132   pooka __weak_alias(usermount_common_policy,rump__unavailable_vfs_panic);
    159   1.92   pooka 
    160  1.147   pooka rump_proc_vfs_init_fn rump_proc_vfs_init;
    161  1.147   pooka rump_proc_vfs_release_fn rump_proc_vfs_release;
    162   1.71   pooka 
    163  1.155   pooka static void add_linkedin_modules(const struct modinfo *const *, size_t);
    164  1.155   pooka 
    165  1.151   pooka static void __noinline
    166  1.105   pooka messthestack(void)
    167  1.105   pooka {
    168  1.151   pooka 	volatile uint32_t mess[64];
    169  1.105   pooka 	uint64_t d1, d2;
    170  1.105   pooka 	int i, error;
    171  1.105   pooka 
    172  1.105   pooka 	for (i = 0; i < 64; i++) {
    173  1.105   pooka 		rumpuser_gettime(&d1, &d2, &error);
    174  1.105   pooka 		mess[i] = d2;
    175  1.105   pooka 	}
    176  1.105   pooka }
    177  1.105   pooka 
    178  1.158   pooka /*
    179  1.158   pooka  * Create kern.hostname.  why only this you ask.  well, init_sysctl
    180  1.158   pooka  * is a kitchen sink in need of some gardening.  but i want to use
    181  1.158   pooka  * kern.hostname today.
    182  1.158   pooka  */
    183  1.158   pooka static void
    184  1.158   pooka mksysctls(void)
    185  1.158   pooka {
    186  1.158   pooka 
    187  1.158   pooka 	sysctl_createv(NULL, 0, NULL, NULL,
    188  1.158   pooka 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "kern", NULL,
    189  1.158   pooka 	    NULL, 0, NULL, 0, CTL_KERN, CTL_EOL);
    190  1.158   pooka 
    191  1.158   pooka 	/* XXX: setting hostnamelen is missing */
    192  1.158   pooka 	sysctl_createv(NULL, 0, NULL, NULL,
    193  1.158   pooka 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_STRING, "hostname",
    194  1.158   pooka 	    SYSCTL_DESCR("System hostname"), NULL, 0,
    195  1.158   pooka 	    &hostname, MAXHOSTNAMELEN, CTL_KERN, KERN_HOSTNAME, CTL_EOL);
    196  1.158   pooka }
    197  1.158   pooka 
    198   1.61   pooka int
    199   1.83   pooka rump__init(int rump_version)
    200    1.1   pooka {
    201   1.36   pooka 	char buf[256];
    202  1.160   pooka 	struct timespec ts;
    203  1.157   pooka 	uint64_t sec, nsec;
    204   1.14   pooka 	struct proc *p;
    205   1.14   pooka 	struct lwp *l;
    206  1.166   pooka 	int i, numcpu;
    207    1.1   pooka 	int error;
    208    1.1   pooka 
    209  1.121   pooka 	/* not reentrant */
    210   1.27   pooka 	if (rump_inited)
    211   1.61   pooka 		return 0;
    212  1.121   pooka 	else if (rump_inited == -1)
    213  1.121   pooka 		panic("rump_init: host process restart required");
    214  1.121   pooka 	else
    215  1.121   pooka 		rump_inited = 1;
    216   1.27   pooka 
    217  1.149   pooka 	if (rumpuser_getenv("RUMP_VERBOSE", buf, sizeof(buf), &error) == 0) {
    218  1.149   pooka 		if (*buf != '0')
    219  1.149   pooka 			boothowto = AB_VERBOSE;
    220  1.149   pooka 	}
    221  1.149   pooka 
    222  1.167   pooka 	if (rumpuser_getenv("RUMP_NCPU", buf, sizeof(buf), &error) == 0)
    223  1.167   pooka 		error = 0;
    224  1.166   pooka 	/* non-x86 is missing CPU_INFO_FOREACH() support */
    225  1.166   pooka #if defined(__i386__) || defined(__x86_64__)
    226  1.167   pooka 	if (error == 0) {
    227  1.167   pooka 		numcpu = strtoll(buf, NULL, 10);
    228  1.167   pooka 		if (numcpu < 1)
    229  1.167   pooka 			numcpu = 1;
    230  1.167   pooka 	} else {
    231  1.167   pooka 		numcpu = rumpuser_getnhostcpu();
    232  1.167   pooka 	}
    233  1.166   pooka #else
    234  1.167   pooka 	if (error == 0)
    235  1.167   pooka 		printf("NCPU limited to 1 on this host\n");
    236  1.166   pooka 	numcpu = 1;
    237  1.166   pooka #endif
    238  1.166   pooka 	rump_cpus_bootstrap(numcpu);
    239  1.165   pooka 
    240  1.157   pooka 	rumpuser_gettime(&sec, &nsec, &error);
    241  1.157   pooka 	boottime.tv_sec = sec;
    242  1.157   pooka 	boottime.tv_nsec = nsec;
    243  1.157   pooka 
    244  1.156   pooka 	initmsgbuf(rump_msgbuf, sizeof(rump_msgbuf));
    245  1.156   pooka 	aprint_verbose("%s%s", copyright, version);
    246  1.148   pooka 
    247  1.105   pooka 	/*
    248  1.105   pooka 	 * Seed arc4random() with a "reasonable" amount of randomness.
    249  1.105   pooka 	 * Yes, this is a quick kludge which depends on the arc4random
    250  1.105   pooka 	 * implementation.
    251  1.105   pooka 	 */
    252  1.105   pooka 	messthestack();
    253  1.105   pooka 	arc4random();
    254  1.105   pooka 
    255   1.61   pooka 	if (rump_version != RUMP_VERSION) {
    256   1.61   pooka 		printf("rump version mismatch, %d vs. %d\n",
    257   1.61   pooka 		    rump_version, RUMP_VERSION);
    258   1.61   pooka 		return EPROGMISMATCH;
    259   1.61   pooka 	}
    260   1.61   pooka 
    261   1.54   pooka 	if (rumpuser_getenv("RUMP_THREADS", buf, sizeof(buf), &error) == 0) {
    262   1.54   pooka 		rump_threads = *buf != '0';
    263   1.54   pooka 	}
    264  1.124   pooka 	rumpuser_thrinit(rump_user_schedule, rump_user_unschedule,
    265  1.124   pooka 	    rump_threads);
    266  1.143   pooka 	rump_intr_init();
    267   1.36   pooka 
    268  1.131   pooka 	/* init minimal lwp/cpu context */
    269  1.131   pooka 	l = &lwp0;
    270  1.131   pooka 	l->l_lid = 1;
    271  1.131   pooka 	l->l_cpu = rump_cpu;
    272  1.131   pooka 	rumpuser_set_curlwp(l);
    273  1.131   pooka 
    274   1.87   pooka 	mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_NONE);
    275   1.77   pooka 	rumpuser_mutex_recursive_init(&rump_giantlock);
    276   1.82   pooka 	ksyms_init();
    277   1.89   pooka 	rumpvm_init();
    278  1.100   pooka 	evcnt_init();
    279   1.89   pooka 
    280   1.89   pooka 	once_init();
    281  1.114   pooka 	prop_kern_init();
    282   1.63   pooka 
    283   1.90   pooka 	pool_subsystem_init();
    284   1.52   pooka 	kmem_init();
    285  1.102   pooka 
    286  1.136   pooka 	uvm_ra_init();
    287  1.136   pooka 
    288  1.133   pooka 	mutex_obj_init();
    289  1.143   pooka 	callout_startup();
    290  1.133   pooka 
    291   1.81   pooka 	kprintf_init();
    292   1.81   pooka 	loginit();
    293   1.52   pooka 
    294   1.59   pooka 	kauth_init();
    295  1.123   pooka 	rump_susercred = rump_cred_create(0, 0, 0, NULL);
    296   1.59   pooka 
    297  1.131   pooka 	/* init proc0 and rest of lwp0 now that we can allocate memory */
    298   1.40      ad 	p = &proc0;
    299   1.14   pooka 	p->p_stats = &rump_stats;
    300   1.14   pooka 	p->p_limit = &rump_limits;
    301   1.81   pooka 	p->p_pgrp = &rump_pgrp;
    302   1.14   pooka 	p->p_pid = 0;
    303   1.40      ad 	p->p_fd = &rump_filedesc0;
    304   1.27   pooka 	p->p_vmspace = &rump_vmspace;
    305  1.161   pooka 	p->p_emul = &emul_netbsd;
    306   1.94   pooka 	p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    307  1.123   pooka 	l->l_cred = rump_cred_suserget();
    308   1.14   pooka 	l->l_proc = p;
    309   1.99   pooka 	LIST_INIT(&allproc);
    310  1.103   pooka 	LIST_INSERT_HEAD(&allproc, &proc0, p_list);
    311   1.91   pooka 	proc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    312  1.159   pooka 	lwpinit_specificdata();
    313   1.36   pooka 
    314  1.162   pooka 	mutex_init(&rump_limits.pl_lock, MUTEX_DEFAULT, IPL_NONE);
    315    1.1   pooka 	rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
    316   1.27   pooka 	rump_limits.pl_rlimit[RLIMIT_NOFILE].rlim_cur = RLIM_INFINITY;
    317   1.70   pooka 	rump_limits.pl_rlimit[RLIMIT_SBSIZE].rlim_cur = RLIM_INFINITY;
    318  1.162   pooka 	rump_limits.pl_corename = defcorename;
    319    1.1   pooka 
    320  1.124   pooka 	rump_scheduler_init();
    321  1.131   pooka 	/* revert temporary context and schedule a real context */
    322  1.131   pooka 	l->l_cpu = NULL;
    323  1.131   pooka 	rumpuser_set_curlwp(NULL);
    324  1.124   pooka 	rump_schedule();
    325  1.124   pooka 
    326  1.150   pooka 	percpu_init();
    327  1.160   pooka 	inittimecounter();
    328  1.160   pooka 	ntp_init();
    329  1.160   pooka 
    330  1.160   pooka 	rumpuser_gettime(&sec, &nsec, &error);
    331  1.160   pooka 	ts.tv_sec = sec;
    332  1.160   pooka 	ts.tv_nsec = nsec;
    333  1.160   pooka 	tc_setclock(&ts);
    334  1.150   pooka 
    335  1.143   pooka 	/* we are mostly go.  do per-cpu subsystem init */
    336  1.143   pooka 	for (i = 0; i < ncpu; i++) {
    337  1.143   pooka 		struct cpu_info *ci = cpu_lookup(i);
    338  1.143   pooka 
    339  1.143   pooka 		callout_init_cpu(ci);
    340  1.143   pooka 		softint_init(ci);
    341  1.143   pooka 		xc_init_cpu(ci);
    342  1.143   pooka 		pool_cache_cpu_init(ci);
    343  1.143   pooka 		selsysinit(ci);
    344  1.150   pooka 		percpu_init_cpu(ci);
    345  1.143   pooka 	}
    346   1.43   pooka 
    347  1.115   pooka 	sysctl_init();
    348   1.98   pooka 	kqueue_init();
    349   1.72   pooka 	iostat_init();
    350   1.68   pooka 	uid_init();
    351   1.43   pooka 	fd_sys_init();
    352   1.44      ad 	module_init();
    353   1.79   pooka 	devsw_init();
    354  1.139   pooka 	pipe_init();
    355  1.162   pooka 	resource_init();
    356    1.1   pooka 
    357  1.155   pooka 	rumpuser_dl_bootstrap(add_linkedin_modules, rump_kernelfsym_load);
    358  1.155   pooka 
    359   1.75   pooka 	/* these do nothing if not present */
    360   1.75   pooka 	rump_vfs_init();
    361   1.75   pooka 	rump_net_init();
    362  1.112   pooka 	rump_dev_init();
    363  1.112   pooka 	cold = 0;
    364   1.31   pooka 
    365   1.14   pooka 	/* aieeeedondest */
    366   1.54   pooka 	if (rump_threads) {
    367   1.54   pooka 		if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
    368  1.110   pooka 		    rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE))
    369   1.54   pooka 			panic("aiodoned");
    370   1.54   pooka 	}
    371   1.14   pooka 
    372  1.158   pooka 	mksysctls();
    373  1.115   pooka 	sysctl_finalize();
    374  1.115   pooka 
    375  1.155   pooka 	module_init_class(MODULE_CLASS_ANY);
    376  1.140   pooka 
    377    1.1   pooka 	rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
    378    1.1   pooka 	hostnamelen = strlen(hostname);
    379   1.24   pooka 
    380   1.24   pooka 	sigemptyset(&sigcantmask);
    381   1.27   pooka 
    382   1.56   pooka 	lwp0.l_fd = proc0.p_fd = fd_init(&rump_filedesc0);
    383    1.2   pooka 
    384   1.89   pooka 	if (rump_threads)
    385   1.89   pooka 		vmem_rehash_start();
    386   1.89   pooka 
    387  1.124   pooka 	rump_unschedule();
    388  1.116   pooka 
    389    1.2   pooka 	return 0;
    390    1.2   pooka }
    391    1.2   pooka 
    392  1.121   pooka /* maybe support sys_reboot some day for remote shutdown */
    393  1.121   pooka void
    394  1.123   pooka rump_reboot(int howto)
    395  1.121   pooka {
    396  1.121   pooka 
    397  1.121   pooka 	/* dump means we really take the dive here */
    398  1.121   pooka 	if ((howto & RB_DUMP) || panicstr) {
    399  1.121   pooka 		rumpuser_exit(RUMPUSER_PANIC);
    400  1.121   pooka 		/*NOTREACHED*/
    401  1.121   pooka 	}
    402  1.121   pooka 
    403  1.121   pooka 	/* try to sync */
    404  1.121   pooka 	if (!((howto & RB_NOSYNC) || panicstr)) {
    405  1.121   pooka 		rump_vfs_fini();
    406  1.121   pooka 	}
    407  1.121   pooka 
    408  1.121   pooka 	/* your wish is my command */
    409  1.121   pooka 	if (howto & RB_HALT) {
    410  1.121   pooka 		for (;;) {
    411  1.121   pooka 			uint64_t sec = 5, nsec = 0;
    412  1.121   pooka 			int error;
    413  1.121   pooka 
    414  1.121   pooka 			rumpuser_nanosleep(&sec, &nsec, &error);
    415  1.121   pooka 		}
    416  1.121   pooka 	}
    417  1.121   pooka 	rump_inited = -1;
    418  1.121   pooka }
    419  1.121   pooka 
    420    1.8   pooka struct uio *
    421  1.123   pooka rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
    422    1.8   pooka {
    423    1.8   pooka 	struct uio *uio;
    424    1.8   pooka 	enum uio_rw uiorw;
    425    1.8   pooka 
    426    1.8   pooka 	switch (rw) {
    427    1.8   pooka 	case RUMPUIO_READ:
    428    1.8   pooka 		uiorw = UIO_READ;
    429    1.8   pooka 		break;
    430    1.8   pooka 	case RUMPUIO_WRITE:
    431    1.8   pooka 		uiorw = UIO_WRITE;
    432    1.8   pooka 		break;
    433   1.11   pooka 	default:
    434   1.11   pooka 		panic("%s: invalid rw %d", __func__, rw);
    435    1.8   pooka 	}
    436    1.8   pooka 
    437   1.28   pooka 	uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
    438   1.28   pooka 	uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
    439    1.8   pooka 
    440    1.8   pooka 	uio->uio_iov->iov_base = buf;
    441    1.8   pooka 	uio->uio_iov->iov_len = bufsize;
    442    1.8   pooka 
    443    1.8   pooka 	uio->uio_iovcnt = 1;
    444    1.8   pooka 	uio->uio_offset = offset;
    445    1.8   pooka 	uio->uio_resid = bufsize;
    446    1.8   pooka 	uio->uio_rw = uiorw;
    447    1.8   pooka 	uio->uio_vmspace = UIO_VMSPACE_SYS;
    448    1.8   pooka 
    449    1.8   pooka 	return uio;
    450    1.8   pooka }
    451    1.8   pooka 
    452    1.8   pooka size_t
    453  1.123   pooka rump_uio_getresid(struct uio *uio)
    454    1.8   pooka {
    455    1.8   pooka 
    456    1.8   pooka 	return uio->uio_resid;
    457    1.8   pooka }
    458    1.8   pooka 
    459    1.8   pooka off_t
    460  1.123   pooka rump_uio_getoff(struct uio *uio)
    461    1.8   pooka {
    462    1.8   pooka 
    463    1.8   pooka 	return uio->uio_offset;
    464    1.8   pooka }
    465    1.8   pooka 
    466    1.8   pooka size_t
    467  1.123   pooka rump_uio_free(struct uio *uio)
    468    1.8   pooka {
    469    1.8   pooka 	size_t resid;
    470    1.8   pooka 
    471    1.8   pooka 	resid = uio->uio_resid;
    472   1.28   pooka 	kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
    473   1.28   pooka 	kmem_free(uio, sizeof(*uio));
    474    1.8   pooka 
    475    1.8   pooka 	return resid;
    476    1.8   pooka }
    477    1.8   pooka 
    478  1.104   pooka static pid_t nextpid = 1;
    479  1.104   pooka struct lwp *
    480  1.123   pooka rump_newproc_switch()
    481  1.104   pooka {
    482  1.125   pooka 	struct lwp *l;
    483  1.104   pooka 	pid_t mypid;
    484  1.104   pooka 
    485  1.104   pooka 	mypid = atomic_inc_uint_nv(&nextpid);
    486  1.104   pooka 	if (__predict_false(mypid == 0))
    487  1.104   pooka 		mypid = atomic_inc_uint_nv(&nextpid);
    488  1.104   pooka 
    489  1.125   pooka 	l = rump_lwp_alloc(mypid, 0);
    490  1.125   pooka 	rump_lwp_switch(l);
    491  1.125   pooka 
    492  1.125   pooka 	return l;
    493  1.125   pooka }
    494  1.125   pooka 
    495  1.125   pooka struct lwp *
    496  1.125   pooka rump_lwp_alloc_and_switch(pid_t pid, lwpid_t lid)
    497  1.125   pooka {
    498  1.125   pooka 	struct lwp *l;
    499  1.125   pooka 
    500  1.125   pooka 	l = rump_lwp_alloc(pid, lid);
    501  1.125   pooka 	rump_lwp_switch(l);
    502  1.104   pooka 
    503  1.125   pooka 	return l;
    504  1.104   pooka }
    505  1.104   pooka 
    506   1.14   pooka struct lwp *
    507  1.125   pooka rump_lwp_alloc(pid_t pid, lwpid_t lid)
    508   1.14   pooka {
    509   1.14   pooka 	struct lwp *l;
    510   1.14   pooka 	struct proc *p;
    511   1.14   pooka 
    512  1.125   pooka 	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
    513   1.53   pooka 	if (pid != 0) {
    514  1.125   pooka 		p = kmem_zalloc(sizeof(*p), KM_SLEEP);
    515  1.147   pooka 		if (rump_proc_vfs_init)
    516  1.147   pooka 			rump_proc_vfs_init(p);
    517   1.53   pooka 		p->p_stats = &rump_stats;
    518  1.162   pooka 		p->p_limit = lim_copy(&rump_limits);
    519   1.53   pooka 		p->p_pid = pid;
    520   1.53   pooka 		p->p_vmspace = &rump_vmspace;
    521  1.161   pooka 		p->p_emul = &emul_netbsd;
    522   1.53   pooka 		p->p_fd = fd_init(NULL);
    523  1.111   pooka 		p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    524  1.135   pooka 		l->l_cred = rump_cred_suserget();
    525   1.53   pooka 	} else {
    526   1.53   pooka 		p = &proc0;
    527  1.135   pooka 		l->l_cred = rump_susercred;
    528   1.53   pooka 	}
    529   1.37   pooka 
    530   1.14   pooka 	l->l_proc = p;
    531   1.53   pooka 	l->l_lid = lid;
    532   1.53   pooka 	l->l_fd = p->p_fd;
    533  1.124   pooka 	l->l_cpu = NULL;
    534  1.159   pooka 	lwp_initspecific(l);
    535  1.160   pooka 	LIST_INSERT_HEAD(&alllwp, l, l_list);
    536   1.38   pooka 
    537   1.14   pooka 	return l;
    538   1.14   pooka }
    539   1.14   pooka 
    540  1.104   pooka void
    541  1.125   pooka rump_lwp_switch(struct lwp *newlwp)
    542  1.104   pooka {
    543  1.125   pooka 	struct lwp *l = curlwp;
    544  1.104   pooka 
    545  1.125   pooka 	rumpuser_set_curlwp(NULL);
    546  1.125   pooka 	newlwp->l_cpu = l->l_cpu;
    547  1.126   pooka 	newlwp->l_mutex = l->l_mutex;
    548  1.126   pooka 	l->l_mutex = NULL;
    549  1.126   pooka 	l->l_cpu = NULL;
    550  1.125   pooka 	rumpuser_set_curlwp(newlwp);
    551  1.125   pooka 	if (l->l_flag & LW_WEXIT)
    552  1.125   pooka 		rump_lwp_free(l);
    553  1.104   pooka }
    554  1.104   pooka 
    555  1.125   pooka /* XXX: this has effect only on non-pid0 lwps */
    556   1.14   pooka void
    557  1.125   pooka rump_lwp_release(struct lwp *l)
    558   1.14   pooka {
    559  1.104   pooka 	struct proc *p;
    560   1.14   pooka 
    561  1.104   pooka 	p = l->l_proc;
    562  1.104   pooka 	if (p->p_pid != 0) {
    563  1.111   pooka 		mutex_obj_free(p->p_lock);
    564   1.53   pooka 		fd_free();
    565  1.147   pooka 		if (rump_proc_vfs_release)
    566  1.147   pooka 			rump_proc_vfs_release(p);
    567  1.123   pooka 		rump_cred_put(l->l_cred);
    568  1.162   pooka 		limfree(p->p_limit);
    569  1.104   pooka 		kmem_free(p, sizeof(*p));
    570   1.53   pooka 	}
    571  1.125   pooka 	KASSERT((l->l_flag & LW_WEXIT) == 0);
    572  1.125   pooka 	l->l_flag |= LW_WEXIT;
    573  1.125   pooka }
    574  1.125   pooka 
    575  1.125   pooka void
    576  1.125   pooka rump_lwp_free(struct lwp *l)
    577  1.125   pooka {
    578  1.125   pooka 
    579  1.125   pooka 	KASSERT(l->l_flag & LW_WEXIT);
    580  1.126   pooka 	KASSERT(l->l_mutex == NULL);
    581  1.152   pooka 	if (l->l_name)
    582  1.152   pooka 		kmem_free(l->l_name, MAXCOMLEN);
    583  1.159   pooka 	lwp_finispecific(l);
    584  1.160   pooka 	LIST_REMOVE(l, l_list);
    585   1.37   pooka 	kmem_free(l, sizeof(*l));
    586   1.14   pooka }
    587   1.14   pooka 
    588   1.14   pooka struct lwp *
    589  1.125   pooka rump_lwp_curlwp(void)
    590   1.14   pooka {
    591  1.125   pooka 	struct lwp *l = curlwp;
    592   1.14   pooka 
    593  1.125   pooka 	if (l->l_flag & LW_WEXIT)
    594  1.125   pooka 		return NULL;
    595   1.14   pooka 	return l;
    596   1.14   pooka }
    597   1.15   pooka 
    598  1.125   pooka /* rump private.  NEEDS WORK! */
    599  1.108   pooka void
    600  1.125   pooka rump_set_vmspace(struct vmspace *vm)
    601  1.108   pooka {
    602  1.125   pooka 	struct proc *p = curproc;
    603  1.108   pooka 
    604  1.125   pooka 	p->p_vmspace = vm;
    605  1.108   pooka }
    606  1.108   pooka 
    607   1.59   pooka kauth_cred_t
    608  1.123   pooka rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
    609   1.59   pooka {
    610   1.59   pooka 	kauth_cred_t cred;
    611   1.59   pooka 	int rv;
    612   1.59   pooka 
    613   1.59   pooka 	cred = kauth_cred_alloc();
    614   1.59   pooka 	kauth_cred_setuid(cred, uid);
    615   1.59   pooka 	kauth_cred_seteuid(cred, uid);
    616   1.59   pooka 	kauth_cred_setsvuid(cred, uid);
    617   1.59   pooka 	kauth_cred_setgid(cred, gid);
    618   1.59   pooka 	kauth_cred_setgid(cred, gid);
    619   1.59   pooka 	kauth_cred_setegid(cred, gid);
    620   1.59   pooka 	kauth_cred_setsvgid(cred, gid);
    621   1.59   pooka 	rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
    622   1.59   pooka 	/* oh this is silly.  and by "this" I mean kauth_cred_setgroups() */
    623   1.59   pooka 	assert(rv == 0);
    624   1.59   pooka 
    625   1.59   pooka 	return cred;
    626   1.59   pooka }
    627   1.59   pooka 
    628   1.59   pooka void
    629  1.123   pooka rump_cred_put(kauth_cred_t cred)
    630   1.59   pooka {
    631   1.59   pooka 
    632   1.59   pooka 	kauth_cred_free(cred);
    633   1.59   pooka }
    634   1.59   pooka 
    635   1.59   pooka kauth_cred_t
    636  1.123   pooka rump_cred_suserget(void)
    637   1.59   pooka {
    638   1.59   pooka 
    639   1.59   pooka 	kauth_cred_hold(rump_susercred);
    640   1.59   pooka 	return rump_susercred;
    641   1.59   pooka }
    642   1.59   pooka 
    643   1.94   pooka /*
    644   1.94   pooka  * Return the next system lwpid
    645   1.94   pooka  */
    646   1.64   pooka lwpid_t
    647   1.97  cegger rump_nextlid(void)
    648   1.64   pooka {
    649   1.94   pooka 	lwpid_t retid;
    650   1.64   pooka 
    651   1.94   pooka 	mutex_enter(proc0.p_lock);
    652   1.94   pooka 	/*
    653   1.94   pooka 	 * Take next one, don't return 0
    654   1.94   pooka 	 * XXX: most likely we'll have collisions in case this
    655   1.94   pooka 	 * wraps around.
    656   1.94   pooka 	 */
    657   1.94   pooka 	if (++proc0.p_nlwpid == 0)
    658   1.94   pooka 		++proc0.p_nlwpid;
    659   1.94   pooka 	retid = proc0.p_nlwpid;
    660   1.94   pooka 	mutex_exit(proc0.p_lock);
    661   1.64   pooka 
    662   1.94   pooka 	return retid;
    663   1.64   pooka }
    664   1.64   pooka 
    665  1.154   pooka static int compcounter[RUMP_COMPONENT_MAX];
    666  1.154   pooka 
    667  1.154   pooka static void
    668  1.154   pooka rump_component_init_cb(struct rump_component *rc, int type)
    669  1.154   pooka {
    670  1.154   pooka 
    671  1.154   pooka 	KASSERT(type < RUMP_COMPONENT_MAX);
    672  1.154   pooka 	if (rc->rc_type == type) {
    673  1.154   pooka 		rc->rc_init();
    674  1.154   pooka 		compcounter[type]++;
    675  1.154   pooka 	}
    676  1.154   pooka }
    677  1.154   pooka 
    678  1.154   pooka int
    679  1.154   pooka rump_component_count(enum rump_component_type type)
    680  1.154   pooka {
    681  1.154   pooka 
    682  1.154   pooka 	KASSERT(type <= RUMP_COMPONENT_MAX);
    683  1.154   pooka 	return compcounter[type];
    684  1.154   pooka }
    685  1.154   pooka 
    686  1.154   pooka void
    687  1.154   pooka rump_component_init(enum rump_component_type type)
    688  1.154   pooka {
    689  1.154   pooka 
    690  1.154   pooka 	rumpuser_dl_component_init(type, rump_component_init_cb);
    691  1.154   pooka }
    692  1.154   pooka 
    693  1.155   pooka /*
    694  1.155   pooka  * Initialize a module which has already been loaded and linked
    695  1.155   pooka  * with dlopen(). This is fundamentally the same as a builtin module.
    696  1.155   pooka  */
    697   1.76   pooka int
    698  1.155   pooka rump_module_init(const struct modinfo * const *mip, size_t nmodinfo)
    699   1.76   pooka {
    700   1.76   pooka 
    701  1.155   pooka 	return module_builtin_add(mip, nmodinfo, true);
    702  1.106   pooka }
    703  1.106   pooka 
    704  1.155   pooka /*
    705  1.155   pooka  * Finish module (flawless victory, fatality!).
    706  1.155   pooka  */
    707  1.106   pooka int
    708  1.155   pooka rump_module_fini(const struct modinfo *mi)
    709  1.106   pooka {
    710  1.120   pooka 
    711  1.155   pooka 	return module_builtin_remove(mi, true);
    712  1.155   pooka }
    713  1.155   pooka 
    714  1.155   pooka /*
    715  1.155   pooka  * Add loaded and linked module to the builtin list.  It will
    716  1.155   pooka  * later be initialized with module_init_class().
    717  1.155   pooka  */
    718  1.155   pooka 
    719  1.155   pooka static void
    720  1.155   pooka add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo)
    721  1.155   pooka {
    722  1.106   pooka 
    723  1.155   pooka 	module_builtin_add(mip, nmodinfo, false);
    724   1.76   pooka }
    725   1.76   pooka 
    726  1.137   pooka int
    727  1.137   pooka rump_kernelfsym_load(void *symtab, uint64_t symsize,
    728  1.137   pooka 	char *strtab, uint64_t strsize)
    729  1.137   pooka {
    730  1.137   pooka 	static int inited = 0;
    731  1.137   pooka 	Elf64_Ehdr ehdr;
    732  1.137   pooka 
    733  1.137   pooka 	if (inited)
    734  1.137   pooka 		return EBUSY;
    735  1.137   pooka 	inited = 1;
    736  1.137   pooka 
    737  1.137   pooka 	/*
    738  1.137   pooka 	 * Use 64bit header since it's bigger.  Shouldn't make a
    739  1.137   pooka 	 * difference, since we're passing in all zeroes anyway.
    740  1.137   pooka 	 */
    741  1.137   pooka 	memset(&ehdr, 0, sizeof(ehdr));
    742  1.137   pooka 	ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize);
    743  1.137   pooka 
    744  1.137   pooka 	return 0;
    745  1.137   pooka }
    746  1.137   pooka 
    747   1.95   pooka static int
    748  1.123   pooka rump_sysproxy_local(int num, void *arg, uint8_t *data, size_t dlen,
    749   1.95   pooka 	register_t *retval)
    750   1.95   pooka {
    751   1.95   pooka 	struct lwp *l;
    752   1.95   pooka 	struct sysent *callp;
    753  1.124   pooka 	int rv;
    754   1.95   pooka 
    755   1.95   pooka 	if (__predict_false(num >= SYS_NSYSENT))
    756   1.95   pooka 		return ENOSYS;
    757   1.95   pooka 
    758   1.95   pooka 	callp = rump_sysent + num;
    759  1.124   pooka 	rump_schedule();
    760  1.125   pooka 	l = curlwp;
    761  1.124   pooka 	rv = callp->sy_call(l, (void *)data, retval);
    762  1.124   pooka 	rump_unschedule();
    763  1.124   pooka 
    764  1.124   pooka 	return rv;
    765   1.95   pooka }
    766   1.95   pooka 
    767  1.130   pooka int
    768  1.130   pooka rump_boot_gethowto()
    769  1.130   pooka {
    770  1.130   pooka 
    771  1.130   pooka 	return boothowto;
    772  1.130   pooka }
    773  1.130   pooka 
    774  1.130   pooka void
    775  1.130   pooka rump_boot_sethowto(int howto)
    776  1.130   pooka {
    777  1.130   pooka 
    778  1.130   pooka 	boothowto = howto;
    779  1.130   pooka }
    780  1.130   pooka 
    781  1.123   pooka rump_sysproxy_t rump_sysproxy = rump_sysproxy_local;
    782   1.95   pooka void *rump_sysproxy_arg;
    783   1.95   pooka 
    784   1.95   pooka /*
    785   1.95   pooka  * This whole syscall-via-rpc is still taking form.  For example, it
    786   1.95   pooka  * may be necessary to set syscalls individually instead of lobbing
    787   1.95   pooka  * them all to the same place.  So don't think this interface is
    788   1.95   pooka  * set in stone.
    789   1.95   pooka  */
    790   1.95   pooka int
    791  1.123   pooka rump_sysproxy_set(rump_sysproxy_t proxy, void *arg)
    792   1.95   pooka {
    793   1.95   pooka 
    794   1.95   pooka 	if (rump_sysproxy_arg)
    795   1.95   pooka 		return EBUSY;
    796   1.95   pooka 
    797   1.95   pooka 	rump_sysproxy_arg = arg;
    798   1.95   pooka 	rump_sysproxy = proxy;
    799   1.95   pooka 
    800   1.95   pooka 	return 0;
    801   1.95   pooka }
    802  1.109   pooka 
    803  1.109   pooka int
    804  1.124   pooka rump_getversion(void)
    805  1.109   pooka {
    806  1.109   pooka 
    807  1.109   pooka 	return __NetBSD_Version__;
    808  1.109   pooka }
    809