Home | History | Annotate | Line # | Download | only in libsysproxy
sysproxy.c revision 1.4.20.1
      1  1.4.20.1  christos /*	$NetBSD: sysproxy.c,v 1.4.20.1 2019/06/10 22:09:53 christos Exp $	*/
      2       1.1     pooka 
      3       1.1     pooka /*
      4       1.1     pooka  * Copyright (c) 2010, 2011 Antti Kantee.  All Rights Reserved.
      5       1.1     pooka  *
      6       1.1     pooka  * Redistribution and use in source and binary forms, with or without
      7       1.1     pooka  * modification, are permitted provided that the following conditions
      8       1.1     pooka  * are met:
      9       1.1     pooka  * 1. Redistributions of source code must retain the above copyright
     10       1.1     pooka  *    notice, this list of conditions and the following disclaimer.
     11       1.1     pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1     pooka  *    notice, this list of conditions and the following disclaimer in the
     13       1.1     pooka  *    documentation and/or other materials provided with the distribution.
     14       1.1     pooka  *
     15       1.1     pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16       1.1     pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17       1.1     pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18       1.1     pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19       1.1     pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20       1.1     pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21       1.1     pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22       1.1     pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23       1.1     pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24       1.1     pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25       1.1     pooka  * SUCH DAMAGE.
     26       1.1     pooka  */
     27       1.1     pooka 
     28       1.1     pooka #include <sys/cdefs.h>
     29  1.4.20.1  christos __KERNEL_RCSID(0, "$NetBSD: sysproxy.c,v 1.4.20.1 2019/06/10 22:09:53 christos Exp $");
     30       1.1     pooka 
     31       1.1     pooka #include <sys/param.h>
     32       1.1     pooka #include <sys/filedesc.h>
     33       1.1     pooka #include <sys/kmem.h>
     34       1.1     pooka #include <sys/syscall.h>
     35       1.1     pooka #include <sys/syscallvar.h>
     36       1.1     pooka #include <sys/systm.h>
     37       1.1     pooka #include <sys/xcall.h>
     38  1.4.20.1  christos #include <sys/lockdebug.h>
     39  1.4.20.1  christos #include <sys/psref.h>
     40       1.1     pooka 
     41       1.1     pooka #define _RUMP_SYSPROXY
     42       1.1     pooka #include <rump/rumpuser.h>
     43       1.1     pooka 
     44       1.4     pooka #include <rump-sys/kern.h>
     45       1.1     pooka 
     46       1.1     pooka int
     47       1.1     pooka rump_init_server(const char *url)
     48       1.1     pooka {
     49       1.1     pooka 
     50       1.1     pooka 	return rumpuser_sp_init(url, ostype, osrelease, MACHINE);
     51       1.1     pooka }
     52       1.1     pooka 
     53       1.1     pooka static pid_t
     54       1.1     pooka hyp_getpid(void)
     55       1.1     pooka {
     56       1.1     pooka 
     57       1.1     pooka 	return curproc->p_pid;
     58       1.1     pooka }
     59       1.1     pooka 
     60       1.1     pooka static int
     61       1.1     pooka hyp_syscall(int num, void *arg, long *retval)
     62       1.1     pooka {
     63       1.1     pooka 	register_t regrv[2] = {0, 0};
     64       1.1     pooka 	struct lwp *l;
     65       1.1     pooka 	struct sysent *callp;
     66       1.1     pooka 	int rv;
     67       1.1     pooka 
     68       1.1     pooka 	if (__predict_false(num >= SYS_NSYSENT))
     69       1.1     pooka 		return ENOSYS;
     70       1.1     pooka 
     71       1.1     pooka 	/* XXX: always uses native syscall vector */
     72       1.1     pooka 	callp = rump_sysent + num;
     73       1.1     pooka 	l = curlwp;
     74       1.1     pooka 	rv = sy_invoke(callp, l, (void *)arg, regrv, num);
     75       1.1     pooka 	retval[0] = regrv[0];
     76       1.1     pooka 	retval[1] = regrv[1];
     77       1.1     pooka 
     78  1.4.20.1  christos 	/* Sanity checks (from mi_userret) */
     79  1.4.20.1  christos 	LOCKDEBUG_BARRIER(NULL, 0);
     80  1.4.20.1  christos 	KASSERT(l->l_nopreempt == 0);
     81  1.4.20.1  christos 	PSREF_DEBUG_BARRIER();
     82  1.4.20.1  christos 	KASSERT(l->l_psrefs == 0);
     83  1.4.20.1  christos 
     84       1.1     pooka 	return rv;
     85       1.1     pooka }
     86       1.1     pooka 
     87       1.3     pooka static struct pmap remotepmap;
     88       1.3     pooka 
     89       1.1     pooka static int
     90       1.1     pooka hyp_rfork(void *priv, int flags, const char *comm)
     91       1.1     pooka {
     92       1.3     pooka 	struct rump_spctl *spctl;
     93       1.2     pooka 	struct vmspace *vm;
     94       1.1     pooka 	struct proc *p;
     95       1.1     pooka 	struct lwp *l;
     96       1.1     pooka 	int error;
     97       1.1     pooka 	bool initfds;
     98       1.1     pooka 
     99       1.1     pooka 	/*
    100       1.1     pooka 	 * If we are forking off of pid 1, initialize file descriptors.
    101       1.1     pooka 	 */
    102       1.1     pooka 	l = curlwp;
    103       1.1     pooka 	if (l->l_proc->p_pid == 1) {
    104       1.1     pooka 		KASSERT(flags == RUMP_RFFD_CLEAR);
    105       1.1     pooka 		initfds = true;
    106       1.1     pooka 	} else {
    107       1.1     pooka 		initfds = false;
    108       1.1     pooka 	}
    109       1.1     pooka 
    110       1.2     pooka 	/*
    111       1.3     pooka 	 * Since it's a proxy proc, we create a vmspace for it.
    112       1.2     pooka 	 */
    113       1.3     pooka 	spctl = kmem_zalloc(sizeof(*spctl), KM_SLEEP);
    114       1.3     pooka 	vm = &spctl->spctl_vm;
    115       1.3     pooka 	uvmspace_init(vm, &remotepmap, 0, 0, false);
    116       1.3     pooka 	spctl->spctl = priv;
    117       1.2     pooka 
    118       1.2     pooka 	if ((error = rump_lwproc_rfork_vmspace(vm, flags)) != 0) {
    119       1.2     pooka 		kmem_free(vm, sizeof(*vm));
    120       1.1     pooka 		return error;
    121       1.2     pooka 	}
    122       1.1     pooka 
    123       1.1     pooka 	/*
    124       1.1     pooka 	 * We forked in this routine, so cannot use curlwp (const)
    125       1.1     pooka 	 */
    126       1.1     pooka 	l = rump_lwproc_curlwp();
    127       1.1     pooka 	p = l->l_proc;
    128       1.1     pooka 
    129       1.1     pooka 	if (comm)
    130       1.1     pooka 		strlcpy(p->p_comm, comm, sizeof(p->p_comm));
    131       1.1     pooka 	if (initfds)
    132       1.1     pooka 		rump_consdev_init();
    133       1.1     pooka 
    134       1.1     pooka 	return 0;
    135       1.1     pooka }
    136       1.1     pooka 
    137       1.1     pooka /*
    138       1.1     pooka  * Order all lwps in a process to exit.  does *not* wait for them to drain.
    139       1.1     pooka  */
    140       1.1     pooka static void
    141       1.1     pooka hyp_lwpexit(void)
    142       1.1     pooka {
    143       1.1     pooka 	struct proc *p = curproc;
    144       1.1     pooka 	uint64_t where;
    145       1.1     pooka 	struct lwp *l;
    146       1.1     pooka 
    147       1.1     pooka 	mutex_enter(p->p_lock);
    148       1.1     pooka 	/*
    149       1.1     pooka 	 * First pass: mark all lwps in the process with LW_RUMP_QEXIT
    150       1.1     pooka 	 * so that they know they should exit.
    151       1.1     pooka 	 */
    152       1.1     pooka 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    153       1.1     pooka 		if (l == curlwp)
    154       1.1     pooka 			continue;
    155       1.1     pooka 		l->l_flag |= LW_RUMP_QEXIT;
    156       1.1     pooka 	}
    157       1.1     pooka 	mutex_exit(p->p_lock);
    158       1.1     pooka 
    159       1.1     pooka 	/*
    160       1.1     pooka 	 * Next, make sure everyone on all CPUs sees our status
    161       1.1     pooka 	 * update.  This keeps threads inside cv_wait() and makes
    162       1.1     pooka 	 * sure we don't access a stale cv pointer later when
    163       1.1     pooka 	 * we wake up the threads.
    164       1.1     pooka 	 */
    165       1.1     pooka 
    166       1.1     pooka 	where = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
    167       1.1     pooka 	xc_wait(where);
    168       1.1     pooka 
    169       1.1     pooka 	/*
    170       1.1     pooka 	 * Ok, all lwps are either:
    171       1.1     pooka 	 *  1) not in the cv code
    172       1.1     pooka 	 *  2) sleeping on l->l_private
    173       1.1     pooka 	 *  3) sleeping on p->p_waitcv
    174       1.1     pooka 	 *
    175       1.1     pooka 	 * Either way, l_private is stable until we set PS_RUMP_LWPEXIT
    176       1.1     pooka 	 * in p->p_sflag.
    177       1.1     pooka 	 */
    178       1.1     pooka 
    179       1.1     pooka 	mutex_enter(p->p_lock);
    180       1.1     pooka 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    181       1.1     pooka 		if (l->l_private)
    182       1.1     pooka 			cv_broadcast(l->l_private);
    183       1.1     pooka 	}
    184       1.1     pooka 	p->p_sflag |= PS_RUMP_LWPEXIT;
    185       1.1     pooka 	cv_broadcast(&p->p_waitcv);
    186       1.1     pooka 	mutex_exit(p->p_lock);
    187       1.1     pooka }
    188       1.1     pooka 
    189       1.1     pooka /*
    190       1.1     pooka  * Notify process that all threads have been drained and exec is complete.
    191       1.1     pooka  */
    192       1.1     pooka static void
    193       1.1     pooka hyp_execnotify(const char *comm)
    194       1.1     pooka {
    195       1.1     pooka 	struct proc *p = curproc;
    196       1.1     pooka 
    197       1.1     pooka 	fd_closeexec();
    198       1.1     pooka 	mutex_enter(p->p_lock);
    199       1.1     pooka 	KASSERT(p->p_nlwps == 1 && p->p_sflag & PS_RUMP_LWPEXIT);
    200       1.1     pooka 	p->p_sflag &= ~PS_RUMP_LWPEXIT;
    201       1.1     pooka 	mutex_exit(p->p_lock);
    202       1.1     pooka 	strlcpy(p->p_comm, comm, sizeof(p->p_comm));
    203       1.1     pooka }
    204       1.1     pooka 
    205       1.1     pooka /*
    206       1.1     pooka  * Initialize interface pointers since component is present.
    207       1.1     pooka  */
    208       1.1     pooka RUMP_COMPONENT(RUMP_COMPONENT_KERN)
    209       1.1     pooka {
    210       1.1     pooka 
    211       1.1     pooka 	rump_sysproxy_ops.rspo_copyin		= rumpuser_sp_copyin;
    212       1.1     pooka 	rump_sysproxy_ops.rspo_copyinstr	= rumpuser_sp_copyinstr;
    213       1.1     pooka 	rump_sysproxy_ops.rspo_copyout		= rumpuser_sp_copyout;
    214       1.1     pooka 	rump_sysproxy_ops.rspo_copyoutstr	= rumpuser_sp_copyoutstr;
    215       1.1     pooka 	rump_sysproxy_ops.rspo_anonmmap		= rumpuser_sp_anonmmap;
    216       1.1     pooka 	rump_sysproxy_ops.rspo_raise		= rumpuser_sp_raise;
    217       1.1     pooka 	rump_sysproxy_ops.rspo_fini		= rumpuser_sp_fini;
    218       1.1     pooka 
    219       1.1     pooka 	rump_sysproxy_ops.rspo_hyp_getpid	= hyp_getpid;
    220       1.1     pooka 	rump_sysproxy_ops.rspo_hyp_syscall	= hyp_syscall;
    221       1.1     pooka 	rump_sysproxy_ops.rspo_hyp_rfork	= hyp_rfork;
    222       1.1     pooka 	rump_sysproxy_ops.rspo_hyp_lwpexit	= hyp_lwpexit;
    223       1.1     pooka 	rump_sysproxy_ops.rspo_hyp_execnotify	= hyp_execnotify;
    224       1.1     pooka }
    225