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