1 /* $NetBSD: sysproxy.c,v 1.7 2019/05/17 03:34:26 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.7 2019/05/17 03:34:26 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 #include <sys/psref.h> 40 41 #define _RUMP_SYSPROXY 42 #include <rump/rumpuser.h> 43 44 #include <rump-sys/kern.h> 45 46 int 47 rump_init_server(const char *url) 48 { 49 50 return rumpuser_sp_init(url, ostype, osrelease, MACHINE); 51 } 52 53 static pid_t 54 hyp_getpid(void) 55 { 56 57 return curproc->p_pid; 58 } 59 60 static int 61 hyp_syscall(int num, void *arg, long *retval) 62 { 63 register_t regrv[2] = {0, 0}; 64 struct lwp *l; 65 struct sysent *callp; 66 int rv; 67 68 if (__predict_false(num >= SYS_NSYSENT)) 69 return ENOSYS; 70 71 /* XXX: always uses native syscall vector */ 72 callp = rump_sysent + num; 73 l = curlwp; 74 rv = sy_invoke(callp, l, (void *)arg, regrv, num); 75 retval[0] = regrv[0]; 76 retval[1] = regrv[1]; 77 78 /* Sanity checks (from mi_userret) */ 79 LOCKDEBUG_BARRIER(NULL, 0); 80 KASSERT(l->l_nopreempt == 0); 81 PSREF_DEBUG_BARRIER(); 82 KASSERT(l->l_psrefs == 0); 83 84 return rv; 85 } 86 87 static struct pmap remotepmap; 88 89 static int 90 hyp_rfork(void *priv, int flags, const char *comm) 91 { 92 struct rump_spctl *spctl; 93 struct vmspace *vm; 94 struct proc *p; 95 struct lwp *l; 96 int error; 97 bool initfds; 98 99 /* 100 * If we are forking off of pid 1, initialize file descriptors. 101 */ 102 l = curlwp; 103 if (l->l_proc->p_pid == 1) { 104 KASSERT(flags == RUMP_RFFD_CLEAR); 105 initfds = true; 106 } else { 107 initfds = false; 108 } 109 110 /* 111 * Since it's a proxy proc, we create a vmspace for it. 112 */ 113 spctl = kmem_zalloc(sizeof(*spctl), KM_SLEEP); 114 vm = &spctl->spctl_vm; 115 uvmspace_init(vm, &remotepmap, 0, 0, false); 116 spctl->spctl = priv; 117 118 if ((error = rump_lwproc_rfork_vmspace(vm, flags)) != 0) { 119 kmem_free(vm, sizeof(*vm)); 120 return error; 121 } 122 123 /* 124 * We forked in this routine, so cannot use curlwp (const) 125 */ 126 l = rump_lwproc_curlwp(); 127 p = l->l_proc; 128 129 if (comm) 130 strlcpy(p->p_comm, comm, sizeof(p->p_comm)); 131 if (initfds) 132 rump_consdev_init(); 133 134 return 0; 135 } 136 137 /* 138 * Order all lwps in a process to exit. does *not* wait for them to drain. 139 */ 140 static void 141 hyp_lwpexit(void) 142 { 143 struct proc *p = curproc; 144 uint64_t where; 145 struct lwp *l; 146 147 mutex_enter(p->p_lock); 148 /* 149 * First pass: mark all lwps in the process with LW_RUMP_QEXIT 150 * so that they know they should exit. 151 */ 152 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 153 if (l == curlwp) 154 continue; 155 l->l_flag |= LW_RUMP_QEXIT; 156 } 157 mutex_exit(p->p_lock); 158 159 /* 160 * Next, make sure everyone on all CPUs sees our status 161 * update. This keeps threads inside cv_wait() and makes 162 * sure we don't access a stale cv pointer later when 163 * we wake up the threads. 164 */ 165 166 where = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL); 167 xc_wait(where); 168 169 /* 170 * Ok, all lwps are either: 171 * 1) not in the cv code 172 * 2) sleeping on l->l_private 173 * 3) sleeping on p->p_waitcv 174 * 175 * Either way, l_private is stable until we set PS_RUMP_LWPEXIT 176 * in p->p_sflag. 177 */ 178 179 mutex_enter(p->p_lock); 180 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 181 if (l->l_private) 182 cv_broadcast(l->l_private); 183 } 184 p->p_sflag |= PS_RUMP_LWPEXIT; 185 cv_broadcast(&p->p_waitcv); 186 mutex_exit(p->p_lock); 187 } 188 189 /* 190 * Notify process that all threads have been drained and exec is complete. 191 */ 192 static void 193 hyp_execnotify(const char *comm) 194 { 195 struct proc *p = curproc; 196 197 fd_closeexec(); 198 mutex_enter(p->p_lock); 199 KASSERT(p->p_nlwps == 1 && p->p_sflag & PS_RUMP_LWPEXIT); 200 p->p_sflag &= ~PS_RUMP_LWPEXIT; 201 mutex_exit(p->p_lock); 202 strlcpy(p->p_comm, comm, sizeof(p->p_comm)); 203 } 204 205 /* 206 * Initialize interface pointers since component is present. 207 */ 208 RUMP_COMPONENT(RUMP_COMPONENT_KERN) 209 { 210 211 rump_sysproxy_ops.rspo_copyin = rumpuser_sp_copyin; 212 rump_sysproxy_ops.rspo_copyinstr = rumpuser_sp_copyinstr; 213 rump_sysproxy_ops.rspo_copyout = rumpuser_sp_copyout; 214 rump_sysproxy_ops.rspo_copyoutstr = rumpuser_sp_copyoutstr; 215 rump_sysproxy_ops.rspo_anonmmap = rumpuser_sp_anonmmap; 216 rump_sysproxy_ops.rspo_raise = rumpuser_sp_raise; 217 rump_sysproxy_ops.rspo_fini = rumpuser_sp_fini; 218 219 rump_sysproxy_ops.rspo_hyp_getpid = hyp_getpid; 220 rump_sysproxy_ops.rspo_hyp_syscall = hyp_syscall; 221 rump_sysproxy_ops.rspo_hyp_rfork = hyp_rfork; 222 rump_sysproxy_ops.rspo_hyp_lwpexit = hyp_lwpexit; 223 rump_sysproxy_ops.rspo_hyp_execnotify = hyp_execnotify; 224 } 225