Home | History | Annotate | Line # | Download | only in rumpkern
rump.c revision 1.107
      1  1.107   pooka /*	$NetBSD: rump.c,v 1.107 2009/05/03 17:09:49 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.107   pooka __KERNEL_RCSID(0, "$NetBSD: rump.c,v 1.107 2009/05/03 17:09:49 pooka Exp $");
     32   1.78   pooka 
     33    1.1   pooka #include <sys/param.h>
     34   1.64   pooka #include <sys/atomic.h>
     35   1.75   pooka #include <sys/buf.h>
     36   1.66   pooka #include <sys/callout.h>
     37   1.79   pooka #include <sys/conf.h>
     38   1.27   pooka #include <sys/cpu.h>
     39  1.100   pooka #include <sys/evcnt.h>
     40   1.98   pooka #include <sys/event.h>
     41    1.1   pooka #include <sys/filedesc.h>
     42   1.72   pooka #include <sys/iostat.h>
     43    1.1   pooka #include <sys/kauth.h>
     44   1.80   pooka #include <sys/kernel.h>
     45   1.14   pooka #include <sys/kmem.h>
     46   1.81   pooka #include <sys/kprintf.h>
     47   1.82   pooka #include <sys/ksyms.h>
     48   1.81   pooka #include <sys/msgbuf.h>
     49   1.49  simonb #include <sys/module.h>
     50   1.71   pooka #include <sys/once.h>
     51   1.65   pooka #include <sys/percpu.h>
     52    1.1   pooka #include <sys/queue.h>
     53    1.1   pooka #include <sys/resourcevar.h>
     54   1.27   pooka #include <sys/select.h>
     55   1.87   pooka #include <sys/sysctl.h>
     56   1.96   pooka #include <sys/syscall.h>
     57   1.87   pooka #include <sys/tty.h>
     58   1.68   pooka #include <sys/uidinfo.h>
     59   1.89   pooka #include <sys/vmem.h>
     60    1.1   pooka 
     61   1.48   pooka #include <rump/rumpuser.h>
     62   1.48   pooka 
     63  1.101   pooka #include <secmodel/secmodel.h>
     64  1.101   pooka 
     65    1.8   pooka #include "rump_private.h"
     66   1.71   pooka #include "rump_net_private.h"
     67   1.75   pooka #include "rump_vfs_private.h"
     68    1.1   pooka 
     69   1.40      ad struct proc proc0;
     70   1.81   pooka struct session rump_session = {
     71   1.81   pooka 	.s_count = 1,
     72   1.81   pooka 	.s_flags = 0,
     73   1.81   pooka 	.s_leader = &proc0,
     74   1.81   pooka 	.s_login = "rumphobo",
     75   1.81   pooka 	.s_sid = 0,
     76   1.81   pooka };
     77   1.81   pooka struct pgrp rump_pgrp = {
     78   1.81   pooka 	.pg_members = LIST_HEAD_INITIALIZER(pg_members),
     79   1.81   pooka 	.pg_session = &rump_session,
     80   1.81   pooka 	.pg_jobc = 1,
     81   1.81   pooka };
     82    1.1   pooka struct pstats rump_stats;
     83    1.1   pooka struct plimit rump_limits;
     84    1.1   pooka struct cpu_info rump_cpu;
     85   1.40      ad struct filedesc rump_filedesc0;
     86   1.36   pooka struct proclist allproc;
     87   1.46      ad char machine[] = "rump";
     88   1.59   pooka static kauth_cred_t rump_susercred;
     89    1.1   pooka 
     90  1.101   pooka /* pretend the master rump proc is init */
     91  1.101   pooka struct proc *initproc = &proc0;
     92  1.101   pooka 
     93   1.77   pooka struct rumpuser_mtx *rump_giantlock;
     94   1.19   pooka 
     95   1.24   pooka sigset_t sigcantmask;
     96   1.24   pooka 
     97   1.54   pooka #ifdef RUMP_WITHOUT_THREADS
     98   1.54   pooka int rump_threads = 0;
     99   1.54   pooka #else
    100   1.54   pooka int rump_threads = 1;
    101   1.54   pooka #endif
    102   1.54   pooka 
    103   1.14   pooka static void
    104   1.14   pooka rump_aiodone_worker(struct work *wk, void *dummy)
    105   1.14   pooka {
    106   1.14   pooka 	struct buf *bp = (struct buf *)wk;
    107   1.14   pooka 
    108   1.14   pooka 	KASSERT(&bp->b_work == wk);
    109   1.14   pooka 	bp->b_iodone(bp);
    110   1.14   pooka }
    111   1.14   pooka 
    112   1.51   pooka static int rump_inited;
    113   1.51   pooka static struct emul emul_rump;
    114   1.27   pooka 
    115   1.86   pooka void rump__unavailable(void);
    116   1.86   pooka void rump__unavailable() {}
    117   1.86   pooka __weak_alias(rump_net_init,rump__unavailable);
    118   1.86   pooka __weak_alias(rump_vfs_init,rump__unavailable);
    119   1.75   pooka 
    120   1.92   pooka void rump__unavailable_vfs_panic(void);
    121   1.92   pooka void rump__unavailable_vfs_panic() {panic("vfs component not available");}
    122   1.92   pooka __weak_alias(vn_open,rump__unavailable_vfs_panic);
    123   1.92   pooka __weak_alias(vn_rdwr,rump__unavailable_vfs_panic);
    124   1.92   pooka __weak_alias(vn_close,rump__unavailable_vfs_panic);
    125   1.92   pooka 
    126   1.75   pooka static void
    127   1.75   pooka pvfsinit_nop(struct proc *p)
    128   1.75   pooka {
    129   1.75   pooka 
    130   1.75   pooka 	return;
    131   1.75   pooka }
    132   1.75   pooka 
    133   1.75   pooka static void
    134   1.75   pooka pvfsrele_nop(struct proc *p)
    135   1.75   pooka {
    136   1.75   pooka 
    137   1.75   pooka 	return;
    138   1.75   pooka }
    139   1.75   pooka 
    140   1.75   pooka rump_proc_vfs_init_fn rump_proc_vfs_init = pvfsinit_nop;
    141   1.75   pooka rump_proc_vfs_release_fn rump_proc_vfs_release = pvfsrele_nop;
    142   1.71   pooka 
    143  1.105   pooka /*
    144  1.105   pooka  * Stir up the stack a bit.  These are exported functions to help
    145  1.105   pooka  * convince the compiler that we don't want these routines completely
    146  1.105   pooka  * optimized out or inlined.  Is there an easier way to do this?
    147  1.105   pooka  */
    148  1.105   pooka void nullfn(uint32_t *);
    149  1.105   pooka void nullfn(uint32_t *arg){}
    150  1.105   pooka void messthestack(void);
    151  1.105   pooka void
    152  1.105   pooka messthestack(void)
    153  1.105   pooka {
    154  1.105   pooka 	uint32_t mess[64];
    155  1.105   pooka 	uint64_t d1, d2;
    156  1.105   pooka 	int i, error;
    157  1.105   pooka 
    158  1.105   pooka 	for (i = 0; i < 64; i++) {
    159  1.105   pooka 		rumpuser_gettime(&d1, &d2, &error);
    160  1.105   pooka 		mess[i] = d2;
    161  1.105   pooka 	}
    162  1.105   pooka 	nullfn(mess);
    163  1.105   pooka }
    164  1.105   pooka 
    165   1.61   pooka int
    166   1.83   pooka rump__init(int rump_version)
    167    1.1   pooka {
    168   1.36   pooka 	char buf[256];
    169   1.14   pooka 	struct proc *p;
    170   1.14   pooka 	struct lwp *l;
    171    1.1   pooka 	int error;
    172    1.1   pooka 
    173   1.27   pooka 	/* XXX */
    174   1.27   pooka 	if (rump_inited)
    175   1.61   pooka 		return 0;
    176   1.27   pooka 	rump_inited = 1;
    177   1.27   pooka 
    178  1.105   pooka 	/*
    179  1.105   pooka 	 * Seed arc4random() with a "reasonable" amount of randomness.
    180  1.105   pooka 	 * Yes, this is a quick kludge which depends on the arc4random
    181  1.105   pooka 	 * implementation.
    182  1.105   pooka 	 */
    183  1.105   pooka 	messthestack();
    184  1.105   pooka 	arc4random();
    185  1.105   pooka 
    186   1.61   pooka 	if (rump_version != RUMP_VERSION) {
    187   1.61   pooka 		printf("rump version mismatch, %d vs. %d\n",
    188   1.61   pooka 		    rump_version, RUMP_VERSION);
    189   1.61   pooka 		return EPROGMISMATCH;
    190   1.61   pooka 	}
    191   1.61   pooka 
    192   1.54   pooka 	if (rumpuser_getenv("RUMP_THREADS", buf, sizeof(buf), &error) == 0) {
    193   1.54   pooka 		rump_threads = *buf != '0';
    194   1.54   pooka 	}
    195   1.84   pooka 	rumpuser_thrinit(_kernel_lock, _kernel_unlock, rump_threads);
    196   1.36   pooka 
    197   1.87   pooka 	mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_NONE);
    198   1.77   pooka 	rumpuser_mutex_recursive_init(&rump_giantlock);
    199   1.82   pooka 	ksyms_init();
    200   1.89   pooka 	rumpvm_init();
    201  1.100   pooka 	evcnt_init();
    202   1.89   pooka 
    203   1.89   pooka 	once_init();
    204   1.63   pooka 
    205   1.52   pooka 	rump_sleepers_init();
    206  1.102   pooka 
    207   1.90   pooka 	pool_subsystem_init();
    208   1.52   pooka 	kmem_init();
    209  1.102   pooka 
    210   1.81   pooka 	kprintf_init();
    211   1.81   pooka 	loginit();
    212   1.52   pooka 
    213   1.59   pooka 	kauth_init();
    214   1.59   pooka 	rump_susercred = rump_cred_create(0, 0, 0, NULL);
    215   1.59   pooka 
    216   1.14   pooka 	l = &lwp0;
    217   1.40      ad 	p = &proc0;
    218   1.14   pooka 	p->p_stats = &rump_stats;
    219   1.14   pooka 	p->p_limit = &rump_limits;
    220   1.81   pooka 	p->p_pgrp = &rump_pgrp;
    221   1.14   pooka 	p->p_pid = 0;
    222   1.40      ad 	p->p_fd = &rump_filedesc0;
    223   1.27   pooka 	p->p_vmspace = &rump_vmspace;
    224   1.51   pooka 	p->p_emul = &emul_rump;
    225   1.94   pooka 	p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    226   1.59   pooka 	l->l_cred = rump_cred_suserget();
    227   1.14   pooka 	l->l_proc = p;
    228   1.14   pooka 	l->l_lid = 1;
    229   1.99   pooka 	LIST_INIT(&allproc);
    230  1.103   pooka 	LIST_INSERT_HEAD(&allproc, &proc0, p_list);
    231   1.91   pooka 	proc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    232   1.36   pooka 
    233    1.1   pooka 	rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
    234   1.27   pooka 	rump_limits.pl_rlimit[RLIMIT_NOFILE].rlim_cur = RLIM_INFINITY;
    235   1.70   pooka 	rump_limits.pl_rlimit[RLIMIT_SBSIZE].rlim_cur = RLIM_INFINITY;
    236    1.1   pooka 
    237   1.66   pooka 	callout_startup();
    238   1.66   pooka 	callout_init_cpu(&rump_cpu);
    239   1.43   pooka 
    240   1.98   pooka 	kqueue_init();
    241   1.72   pooka 	iostat_init();
    242   1.68   pooka 	uid_init();
    243   1.65   pooka 	percpu_init();
    244   1.43   pooka 	fd_sys_init();
    245   1.44      ad 	module_init();
    246   1.51   pooka 	sysctl_init();
    247   1.63   pooka 	softint_init(&rump_cpu);
    248   1.80   pooka 	cold = 0;
    249   1.79   pooka 	devsw_init();
    250  1.101   pooka 	secmodel_start();
    251    1.1   pooka 
    252   1.75   pooka 	/* these do nothing if not present */
    253   1.75   pooka 	rump_vfs_init();
    254   1.75   pooka 	rump_net_init();
    255   1.31   pooka 
    256   1.14   pooka 	/* aieeeedondest */
    257   1.54   pooka 	if (rump_threads) {
    258   1.54   pooka 		if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
    259   1.54   pooka 		    rump_aiodone_worker, NULL, 0, 0, 0))
    260   1.54   pooka 			panic("aiodoned");
    261   1.54   pooka 	}
    262   1.14   pooka 
    263    1.1   pooka 	rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
    264    1.1   pooka 	hostnamelen = strlen(hostname);
    265   1.24   pooka 
    266   1.24   pooka 	sigemptyset(&sigcantmask);
    267   1.27   pooka 
    268   1.56   pooka 	lwp0.l_fd = proc0.p_fd = fd_init(&rump_filedesc0);
    269    1.2   pooka 
    270   1.89   pooka #ifdef RUMP_USE_REAL_ALLOCATORS
    271   1.89   pooka 	if (rump_threads)
    272   1.89   pooka 		vmem_rehash_start();
    273   1.89   pooka #endif
    274   1.89   pooka 
    275    1.2   pooka 	return 0;
    276    1.2   pooka }
    277    1.2   pooka 
    278    1.8   pooka struct uio *
    279    1.8   pooka rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
    280    1.8   pooka {
    281    1.8   pooka 	struct uio *uio;
    282    1.8   pooka 	enum uio_rw uiorw;
    283    1.8   pooka 
    284    1.8   pooka 	switch (rw) {
    285    1.8   pooka 	case RUMPUIO_READ:
    286    1.8   pooka 		uiorw = UIO_READ;
    287    1.8   pooka 		break;
    288    1.8   pooka 	case RUMPUIO_WRITE:
    289    1.8   pooka 		uiorw = UIO_WRITE;
    290    1.8   pooka 		break;
    291   1.11   pooka 	default:
    292   1.11   pooka 		panic("%s: invalid rw %d", __func__, rw);
    293    1.8   pooka 	}
    294    1.8   pooka 
    295   1.28   pooka 	uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
    296   1.28   pooka 	uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
    297    1.8   pooka 
    298    1.8   pooka 	uio->uio_iov->iov_base = buf;
    299    1.8   pooka 	uio->uio_iov->iov_len = bufsize;
    300    1.8   pooka 
    301    1.8   pooka 	uio->uio_iovcnt = 1;
    302    1.8   pooka 	uio->uio_offset = offset;
    303    1.8   pooka 	uio->uio_resid = bufsize;
    304    1.8   pooka 	uio->uio_rw = uiorw;
    305    1.8   pooka 	uio->uio_vmspace = UIO_VMSPACE_SYS;
    306    1.8   pooka 
    307    1.8   pooka 	return uio;
    308    1.8   pooka }
    309    1.8   pooka 
    310    1.8   pooka size_t
    311    1.8   pooka rump_uio_getresid(struct uio *uio)
    312    1.8   pooka {
    313    1.8   pooka 
    314    1.8   pooka 	return uio->uio_resid;
    315    1.8   pooka }
    316    1.8   pooka 
    317    1.8   pooka off_t
    318    1.8   pooka rump_uio_getoff(struct uio *uio)
    319    1.8   pooka {
    320    1.8   pooka 
    321    1.8   pooka 	return uio->uio_offset;
    322    1.8   pooka }
    323    1.8   pooka 
    324    1.8   pooka size_t
    325    1.8   pooka rump_uio_free(struct uio *uio)
    326    1.8   pooka {
    327    1.8   pooka 	size_t resid;
    328    1.8   pooka 
    329    1.8   pooka 	resid = uio->uio_resid;
    330   1.28   pooka 	kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
    331   1.28   pooka 	kmem_free(uio, sizeof(*uio));
    332    1.8   pooka 
    333    1.8   pooka 	return resid;
    334    1.8   pooka }
    335    1.8   pooka 
    336  1.104   pooka /* public interface */
    337  1.104   pooka static pid_t nextpid = 1;
    338  1.104   pooka struct lwp *
    339  1.104   pooka rump_newproc_switch()
    340  1.104   pooka {
    341  1.104   pooka 	struct lwp *oldlwp = curlwp;
    342  1.104   pooka 	pid_t mypid;
    343  1.104   pooka 
    344  1.104   pooka 	mypid = atomic_inc_uint_nv(&nextpid);
    345  1.104   pooka 	if (__predict_false(mypid == 0))
    346  1.104   pooka 		mypid = atomic_inc_uint_nv(&nextpid);
    347  1.104   pooka 
    348  1.104   pooka 	rumpuser_set_curlwp(NULL);
    349  1.104   pooka 	rump_setup_curlwp(mypid, 0, 1);
    350  1.104   pooka 
    351  1.104   pooka 	return oldlwp;
    352  1.104   pooka }
    353  1.104   pooka 
    354  1.104   pooka /* rump private */
    355   1.14   pooka struct lwp *
    356   1.14   pooka rump_setup_curlwp(pid_t pid, lwpid_t lid, int set)
    357   1.14   pooka {
    358   1.14   pooka 	struct lwp *l;
    359   1.14   pooka 	struct proc *p;
    360   1.14   pooka 
    361   1.37   pooka 	l = kmem_zalloc(sizeof(struct lwp), KM_SLEEP);
    362   1.53   pooka 	if (pid != 0) {
    363   1.53   pooka 		p = kmem_zalloc(sizeof(struct proc), KM_SLEEP);
    364   1.75   pooka 		rump_proc_vfs_init(p);
    365   1.53   pooka 		p->p_stats = &rump_stats;
    366   1.53   pooka 		p->p_limit = &rump_limits;
    367   1.53   pooka 		p->p_pid = pid;
    368   1.53   pooka 		p->p_vmspace = &rump_vmspace;
    369   1.53   pooka 		p->p_fd = fd_init(NULL);
    370   1.53   pooka 	} else {
    371   1.53   pooka 		p = &proc0;
    372   1.53   pooka 	}
    373   1.37   pooka 
    374   1.59   pooka 	l->l_cred = rump_cred_suserget();
    375   1.14   pooka 	l->l_proc = p;
    376   1.53   pooka 	l->l_lid = lid;
    377   1.53   pooka 	l->l_fd = p->p_fd;
    378   1.64   pooka 	l->l_mutex = RUMP_LMUTEX_MAGIC;
    379   1.71   pooka 	l->l_cpu = &rump_cpu;
    380   1.38   pooka 
    381   1.14   pooka 	if (set)
    382   1.14   pooka 		rumpuser_set_curlwp(l);
    383   1.14   pooka 
    384   1.14   pooka 	return l;
    385   1.14   pooka }
    386   1.14   pooka 
    387  1.104   pooka /* rump private.  NEEDS WORK! */
    388  1.104   pooka void
    389  1.104   pooka rump_set_vmspace(struct vmspace *vm)
    390  1.104   pooka {
    391  1.104   pooka 	struct proc *p = curproc;
    392  1.104   pooka 
    393  1.104   pooka 	p->p_vmspace = vm;
    394  1.104   pooka }
    395  1.104   pooka 
    396   1.14   pooka void
    397   1.97  cegger rump_clear_curlwp(void)
    398   1.14   pooka {
    399   1.14   pooka 	struct lwp *l;
    400  1.104   pooka 	struct proc *p;
    401   1.14   pooka 
    402   1.14   pooka 	l = rumpuser_get_curlwp();
    403  1.104   pooka 	p = l->l_proc;
    404  1.104   pooka 	if (p->p_pid != 0) {
    405   1.53   pooka 		fd_free();
    406  1.104   pooka 		rump_proc_vfs_release(p);
    407  1.107   pooka 		rump_cred_put(l->l_cred);
    408  1.104   pooka 		kmem_free(p, sizeof(*p));
    409   1.53   pooka 	}
    410   1.37   pooka 	kmem_free(l, sizeof(*l));
    411   1.14   pooka 	rumpuser_set_curlwp(NULL);
    412   1.14   pooka }
    413   1.14   pooka 
    414   1.14   pooka struct lwp *
    415   1.97  cegger rump_get_curlwp(void)
    416   1.14   pooka {
    417   1.14   pooka 	struct lwp *l;
    418   1.14   pooka 
    419   1.14   pooka 	l = rumpuser_get_curlwp();
    420   1.14   pooka 	if (l == NULL)
    421   1.14   pooka 		l = &lwp0;
    422   1.14   pooka 
    423   1.14   pooka 	return l;
    424   1.14   pooka }
    425   1.15   pooka 
    426   1.59   pooka kauth_cred_t
    427   1.59   pooka rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
    428   1.59   pooka {
    429   1.59   pooka 	kauth_cred_t cred;
    430   1.59   pooka 	int rv;
    431   1.59   pooka 
    432   1.59   pooka 	cred = kauth_cred_alloc();
    433   1.59   pooka 	kauth_cred_setuid(cred, uid);
    434   1.59   pooka 	kauth_cred_seteuid(cred, uid);
    435   1.59   pooka 	kauth_cred_setsvuid(cred, uid);
    436   1.59   pooka 	kauth_cred_setgid(cred, gid);
    437   1.59   pooka 	kauth_cred_setgid(cred, gid);
    438   1.59   pooka 	kauth_cred_setegid(cred, gid);
    439   1.59   pooka 	kauth_cred_setsvgid(cred, gid);
    440   1.59   pooka 	rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
    441   1.59   pooka 	/* oh this is silly.  and by "this" I mean kauth_cred_setgroups() */
    442   1.59   pooka 	assert(rv == 0);
    443   1.59   pooka 
    444   1.59   pooka 	return cred;
    445   1.59   pooka }
    446   1.59   pooka 
    447   1.59   pooka void
    448  1.107   pooka rump_cred_put(kauth_cred_t cred)
    449   1.59   pooka {
    450   1.59   pooka 
    451   1.59   pooka 	kauth_cred_free(cred);
    452   1.59   pooka }
    453   1.59   pooka 
    454   1.59   pooka kauth_cred_t
    455   1.97  cegger rump_cred_suserget(void)
    456   1.59   pooka {
    457   1.59   pooka 
    458   1.59   pooka 	kauth_cred_hold(rump_susercred);
    459   1.59   pooka 	return rump_susercred;
    460   1.59   pooka }
    461   1.59   pooka 
    462   1.94   pooka /*
    463   1.94   pooka  * Return the next system lwpid
    464   1.94   pooka  */
    465   1.64   pooka lwpid_t
    466   1.97  cegger rump_nextlid(void)
    467   1.64   pooka {
    468   1.94   pooka 	lwpid_t retid;
    469   1.64   pooka 
    470   1.94   pooka 	mutex_enter(proc0.p_lock);
    471   1.94   pooka 	/*
    472   1.94   pooka 	 * Take next one, don't return 0
    473   1.94   pooka 	 * XXX: most likely we'll have collisions in case this
    474   1.94   pooka 	 * wraps around.
    475   1.94   pooka 	 */
    476   1.94   pooka 	if (++proc0.p_nlwpid == 0)
    477   1.94   pooka 		++proc0.p_nlwpid;
    478   1.94   pooka 	retid = proc0.p_nlwpid;
    479   1.94   pooka 	mutex_exit(proc0.p_lock);
    480   1.64   pooka 
    481   1.94   pooka 	return retid;
    482   1.64   pooka }
    483   1.64   pooka 
    484   1.76   pooka int
    485  1.106   pooka rump_module_init(struct modinfo *mi, prop_dictionary_t props)
    486   1.76   pooka {
    487   1.76   pooka 
    488  1.106   pooka 	if (!module_compatible(mi->mi_version, __NetBSD_Version__))
    489   1.76   pooka 		return EPROGMISMATCH;
    490   1.76   pooka 
    491  1.106   pooka 	return mi->mi_modcmd(MODULE_CMD_INIT, props);
    492  1.106   pooka }
    493  1.106   pooka 
    494  1.106   pooka int
    495  1.106   pooka rump_module_fini(struct modinfo *mi)
    496  1.106   pooka {
    497  1.106   pooka 
    498  1.106   pooka 	return mi->mi_modcmd(MODULE_CMD_FINI, NULL);
    499   1.76   pooka }
    500   1.76   pooka 
    501   1.55   pooka int _syspuffs_stub(int, int *);
    502   1.55   pooka int
    503   1.55   pooka _syspuffs_stub(int fd, int *newfd)
    504   1.55   pooka {
    505   1.55   pooka 
    506   1.55   pooka 	return ENODEV;
    507   1.55   pooka }
    508   1.85   pooka __weak_alias(rump_syspuffs_glueinit,_syspuffs_stub);
    509   1.95   pooka 
    510   1.95   pooka static int
    511   1.95   pooka rump_sysproxy_local(int num, void *arg, uint8_t *data, size_t dlen,
    512   1.95   pooka 	register_t *retval)
    513   1.95   pooka {
    514   1.95   pooka 	struct lwp *l;
    515   1.95   pooka 	struct sysent *callp;
    516   1.95   pooka 
    517   1.95   pooka 	if (__predict_false(num >= SYS_NSYSENT))
    518   1.95   pooka 		return ENOSYS;
    519   1.95   pooka 
    520   1.95   pooka 	l = curlwp;
    521   1.95   pooka 	callp = rump_sysent + num;
    522   1.95   pooka 	return callp->sy_call(l, (void *)data, retval);
    523   1.95   pooka }
    524   1.95   pooka 
    525   1.95   pooka rump_sysproxy_t rump_sysproxy = rump_sysproxy_local;
    526   1.95   pooka void *rump_sysproxy_arg;
    527   1.95   pooka 
    528   1.95   pooka /*
    529   1.95   pooka  * This whole syscall-via-rpc is still taking form.  For example, it
    530   1.95   pooka  * may be necessary to set syscalls individually instead of lobbing
    531   1.95   pooka  * them all to the same place.  So don't think this interface is
    532   1.95   pooka  * set in stone.
    533   1.95   pooka  */
    534   1.95   pooka int
    535   1.95   pooka rump_sysproxy_set(rump_sysproxy_t proxy, void *arg)
    536   1.95   pooka {
    537   1.95   pooka 
    538   1.95   pooka 	if (rump_sysproxy_arg)
    539   1.95   pooka 		return EBUSY;
    540   1.95   pooka 
    541   1.95   pooka 	rump_sysproxy_arg = arg;
    542   1.95   pooka 	rump_sysproxy = proxy;
    543   1.95   pooka 
    544   1.95   pooka 	return 0;
    545   1.95   pooka }
    546