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