lwproc.c revision 1.28
1/* $NetBSD: lwproc.c,v 1.28 2014/03/16 15:30:05 pooka 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#define RUMP__CURLWP_PRIVATE 29 30#include <sys/cdefs.h> 31__KERNEL_RCSID(0, "$NetBSD: lwproc.c,v 1.28 2014/03/16 15:30:05 pooka Exp $"); 32 33#include <sys/param.h> 34#include <sys/atomic.h> 35#include <sys/filedesc.h> 36#include <sys/kauth.h> 37#include <sys/kmem.h> 38#include <sys/lwp.h> 39#include <sys/ktrace.h> 40#include <sys/pool.h> 41#include <sys/proc.h> 42#include <sys/queue.h> 43#include <sys/resourcevar.h> 44#include <sys/uidinfo.h> 45 46#include <rump/rumpuser.h> 47#include "rump_private.h" 48#include "rump_curlwp.h" 49 50struct emul *emul_default = &emul_netbsd; 51 52void 53rump_lwproc_init(void) 54{ 55 56 lwproc_curlwpop(RUMPUSER_LWP_CREATE, &lwp0); 57} 58 59struct lwp * 60rump_lwproc_curlwp_hypercall(void) 61{ 62 63 return rumpuser_curlwp(); 64} 65 66void 67rump_lwproc_curlwp_set(struct lwp *l) 68{ 69 70 KASSERT(curlwp == NULL); 71 lwproc_curlwpop(RUMPUSER_LWP_SET, l); 72} 73 74void 75rump_lwproc_curlwp_clear(struct lwp *l) 76{ 77 78 KASSERT(l == curlwp); 79 lwproc_curlwpop(RUMPUSER_LWP_CLEAR, l); 80} 81 82static void 83lwproc_proc_free(struct proc *p) 84{ 85 kauth_cred_t cred; 86 87 KASSERT(p->p_stat == SDYING || p->p_stat == SDEAD); 88 89#ifdef KTRACE 90 if (p->p_tracep) { 91 mutex_enter(&ktrace_lock); 92 ktrderef(p); 93 mutex_exit(&ktrace_lock); 94 } 95#endif 96 97 mutex_enter(proc_lock); 98 99 KASSERT(p->p_nlwps == 0); 100 KASSERT(LIST_EMPTY(&p->p_lwps)); 101 102 LIST_REMOVE(p, p_list); 103 LIST_REMOVE(p, p_sibling); 104 proc_free_pid(p->p_pid); /* decrements nprocs */ 105 proc_leavepgrp(p); /* releases proc_lock */ 106 107 cred = p->p_cred; 108 chgproccnt(kauth_cred_getuid(cred), -1); 109 if (rump_proc_vfs_release) 110 rump_proc_vfs_release(p); 111 112 lim_free(p->p_limit); 113 pstatsfree(p->p_stats); 114 kauth_cred_free(p->p_cred); 115 proc_finispecific(p); 116 117 mutex_obj_free(p->p_lock); 118 mutex_destroy(&p->p_stmutex); 119 mutex_destroy(&p->p_auxlock); 120 rw_destroy(&p->p_reflock); 121 cv_destroy(&p->p_waitcv); 122 cv_destroy(&p->p_lwpcv); 123 124 /* non-kernel vmspaces are not shared */ 125 if (!RUMP_LOCALPROC_P(p)) { 126 KASSERT(p->p_vmspace->vm_refcnt == 1); 127 kmem_free(p->p_vmspace, sizeof(*p->p_vmspace)); 128 } 129 130 proc_free_mem(p); 131} 132 133/* 134 * Allocate a new process. Mostly mimic fork by 135 * copying the properties of the parent. However, there are some 136 * differences. 137 * 138 * Switch to the new lwp and return a pointer to it. 139 */ 140static struct proc * 141lwproc_newproc(struct proc *parent, int flags) 142{ 143 uid_t uid = kauth_cred_getuid(parent->p_cred); 144 struct proc *p; 145 146 /* maxproc not enforced */ 147 atomic_inc_uint(&nprocs); 148 149 /* allocate process */ 150 p = proc_alloc(); 151 memset(&p->p_startzero, 0, 152 offsetof(struct proc, p_endzero) 153 - offsetof(struct proc, p_startzero)); 154 memcpy(&p->p_startcopy, &parent->p_startcopy, 155 offsetof(struct proc, p_endcopy) 156 - offsetof(struct proc, p_startcopy)); 157 158 /* some other garbage we need to zero */ 159 p->p_sigacts = NULL; 160 p->p_aio = NULL; 161 p->p_dtrace = NULL; 162 p->p_mqueue_cnt = p->p_exitsig = 0; 163 p->p_flag = p->p_sflag = p->p_slflag = p->p_lflag = p->p_stflag = 0; 164 p->p_trace_enabled = 0; 165 p->p_xstat = p->p_acflag = 0; 166 p->p_stackbase = 0; 167 168 p->p_stats = pstatscopy(parent->p_stats); 169 170 p->p_vmspace = vmspace_kernel(); 171 p->p_emul = emul_default; 172#ifdef __HAVE_SYSCALL_INTERN 173 p->p_emul->e_syscall_intern(p); 174#endif 175 if (*parent->p_comm) 176 strcpy(p->p_comm, parent->p_comm); 177 else 178 strcpy(p->p_comm, "rumproc"); 179 180 if ((flags & RUMP_RFCFDG) == 0) 181 KASSERT(parent == curproc); 182 if (flags & RUMP_RFFDG) 183 p->p_fd = fd_copy(); 184 else if (flags & RUMP_RFCFDG) 185 p->p_fd = fd_init(NULL); 186 else 187 fd_share(p); 188 189 lim_addref(parent->p_limit); 190 p->p_limit = parent->p_limit; 191 192 LIST_INIT(&p->p_lwps); 193 LIST_INIT(&p->p_children); 194 195 p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); 196 mutex_init(&p->p_stmutex, MUTEX_DEFAULT, IPL_HIGH); 197 mutex_init(&p->p_auxlock, MUTEX_DEFAULT, IPL_NONE); 198 rw_init(&p->p_reflock); 199 cv_init(&p->p_waitcv, "pwait"); 200 cv_init(&p->p_lwpcv, "plwp"); 201 202 p->p_pptr = parent; 203 p->p_ppid = parent->p_pid; 204 p->p_stat = SACTIVE; 205 206 kauth_proc_fork(parent, p); 207 208 /* initialize cwd in rump kernels with vfs */ 209 if (rump_proc_vfs_init) 210 rump_proc_vfs_init(p); 211 212 chgproccnt(uid, 1); /* not enforced */ 213 214 /* publish proc various proc lists */ 215 mutex_enter(proc_lock); 216 LIST_INSERT_HEAD(&allproc, p, p_list); 217 LIST_INSERT_HEAD(&parent->p_children, p, p_sibling); 218 LIST_INSERT_AFTER(parent, p, p_pglist); 219 mutex_exit(proc_lock); 220 221 return p; 222} 223 224static void 225lwproc_freelwp(struct lwp *l) 226{ 227 struct proc *p; 228 229 p = l->l_proc; 230 mutex_enter(p->p_lock); 231 232 KASSERT(l->l_flag & LW_WEXIT); 233 KASSERT(l->l_refcnt == 0); 234 235 /* ok, zero references, continue with nuke */ 236 LIST_REMOVE(l, l_sibling); 237 KASSERT(p->p_nlwps >= 1); 238 if (--p->p_nlwps == 0) { 239 KASSERT(p != &proc0); 240 p->p_stat = SDEAD; 241 } 242 cv_broadcast(&p->p_lwpcv); /* nobody sleeps on this in a rump kernel? */ 243 kauth_cred_free(l->l_cred); 244 mutex_exit(p->p_lock); 245 246 mutex_enter(proc_lock); 247 LIST_REMOVE(l, l_list); 248 mutex_exit(proc_lock); 249 250 if (l->l_name) 251 kmem_free(l->l_name, MAXCOMLEN); 252 lwp_finispecific(l); 253 254 lwproc_curlwpop(RUMPUSER_LWP_DESTROY, l); 255 membar_exit(); 256 kmem_free(l, sizeof(*l)); 257 258 if (p->p_stat == SDEAD) 259 lwproc_proc_free(p); 260} 261 262extern kmutex_t unruntime_lock; 263 264/* 265 * called with p_lock held, releases lock before return 266 */ 267static void 268lwproc_makelwp(struct proc *p, struct lwp *l, bool doswitch, bool procmake) 269{ 270 271 p->p_nlwps++; 272 l->l_refcnt = 1; 273 l->l_proc = p; 274 275 l->l_lid = p->p_nlwpid++; 276 LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling); 277 278 l->l_fd = p->p_fd; 279 l->l_cpu = rump_cpu; 280 l->l_target_cpu = rump_cpu; /* Initial target CPU always the same */ 281 l->l_stat = LSRUN; 282 l->l_mutex = &unruntime_lock; 283 TAILQ_INIT(&l->l_ld_locks); 284 mutex_exit(p->p_lock); 285 286 lwp_update_creds(l); 287 lwp_initspecific(l); 288 289 membar_enter(); 290 lwproc_curlwpop(RUMPUSER_LWP_CREATE, l); 291 if (doswitch) { 292 rump_lwproc_switch(l); 293 } 294 295 /* filedesc already has refcount 1 when process is created */ 296 if (!procmake) { 297 fd_hold(l); 298 } 299 300 mutex_enter(proc_lock); 301 LIST_INSERT_HEAD(&alllwp, l, l_list); 302 mutex_exit(proc_lock); 303} 304 305struct lwp * 306rump__lwproc_alloclwp(struct proc *p) 307{ 308 struct lwp *l; 309 bool newproc = false; 310 311 if (p == NULL) { 312 p = lwproc_newproc(&proc0, 0); 313 newproc = true; 314 } 315 316 l = kmem_zalloc(sizeof(*l), KM_SLEEP); 317 318 mutex_enter(p->p_lock); 319 KASSERT((p->p_sflag & PS_RUMP_LWPEXIT) == 0); 320 lwproc_makelwp(p, l, false, newproc); 321 322 return l; 323} 324 325int 326rump_lwproc_newlwp(pid_t pid) 327{ 328 struct proc *p; 329 struct lwp *l; 330 331 l = kmem_zalloc(sizeof(*l), KM_SLEEP); 332 mutex_enter(proc_lock); 333 p = proc_find_raw(pid); 334 if (p == NULL) { 335 mutex_exit(proc_lock); 336 kmem_free(l, sizeof(*l)); 337 return ESRCH; 338 } 339 mutex_enter(p->p_lock); 340 if (p->p_sflag & PS_RUMP_LWPEXIT) { 341 mutex_exit(proc_lock); 342 mutex_exit(p->p_lock); 343 kmem_free(l, sizeof(*l)); 344 return EBUSY; 345 } 346 mutex_exit(proc_lock); 347 lwproc_makelwp(p, l, true, false); 348 349 return 0; 350} 351 352int 353rump_lwproc_rfork(int flags) 354{ 355 struct proc *p; 356 struct lwp *l; 357 358 if (flags & ~(RUMP_RFFDG|RUMP_RFCFDG) || 359 (~flags & (RUMP_RFFDG|RUMP_RFCFDG)) == 0) 360 return EINVAL; 361 362 p = lwproc_newproc(curproc, flags); 363 l = kmem_zalloc(sizeof(*l), KM_SLEEP); 364 mutex_enter(p->p_lock); 365 KASSERT((p->p_sflag & PS_RUMP_LWPEXIT) == 0); 366 lwproc_makelwp(p, l, true, true); 367 368 return 0; 369} 370 371/* 372 * Switch to a new process/thread. Release previous one if 373 * deemed to be exiting. This is considered a slow path for 374 * rump kernel entry. 375 */ 376void 377rump_lwproc_switch(struct lwp *newlwp) 378{ 379 struct lwp *l = curlwp; 380 381 KASSERT(!(l->l_flag & LW_WEXIT) || newlwp); 382 383 if (__predict_false(newlwp && (newlwp->l_pflag & LP_RUNNING))) 384 panic("lwp %p (%d:%d) already running", 385 newlwp, newlwp->l_proc->p_pid, newlwp->l_lid); 386 387 if (newlwp == NULL) { 388 l->l_pflag &= ~LP_RUNNING; 389 l->l_flag |= LW_RUMP_CLEAR; 390 return; 391 } 392 393 /* fd_free() must be called from curlwp context. talk about ugh */ 394 if (l->l_flag & LW_WEXIT) { 395 fd_free(); 396 } 397 398 KERNEL_UNLOCK_ALL(NULL, &l->l_biglocks); 399 lwproc_curlwpop(RUMPUSER_LWP_CLEAR, l); 400 401 newlwp->l_cpu = newlwp->l_target_cpu = l->l_cpu; 402 newlwp->l_mutex = l->l_mutex; 403 newlwp->l_pflag |= LP_RUNNING; 404 405 lwproc_curlwpop(RUMPUSER_LWP_SET, newlwp); 406 curcpu()->ci_curlwp = newlwp; 407 KERNEL_LOCK(newlwp->l_biglocks, NULL); 408 409 /* 410 * Check if the thread should get a signal. This is 411 * mostly to satisfy the "record" rump sigmodel. 412 */ 413 mutex_enter(newlwp->l_proc->p_lock); 414 if (sigispending(newlwp, 0)) { 415 newlwp->l_flag |= LW_PENDSIG; 416 } 417 mutex_exit(newlwp->l_proc->p_lock); 418 419 l->l_mutex = &unruntime_lock; 420 l->l_pflag &= ~LP_RUNNING; 421 l->l_flag &= ~LW_PENDSIG; 422 l->l_stat = LSRUN; 423 424 if (l->l_flag & LW_WEXIT) { 425 lwproc_freelwp(l); 426 } 427} 428 429/* 430 * Mark the current thread to be released upon return from 431 * kernel. 432 */ 433void 434rump_lwproc_releaselwp(void) 435{ 436 struct lwp *l = curlwp; 437 438 if (l->l_refcnt == 0 || l->l_flag & LW_WEXIT) 439 panic("releasing non-pertinent lwp"); 440 441 rump__lwproc_lwprele(); 442 KASSERT(l->l_refcnt == 0 && (l->l_flag & LW_WEXIT)); 443} 444 445/* 446 * In-kernel routines used to add and remove references for the 447 * current thread. The main purpose is to make it possible for 448 * implicit threads to persist over scheduling operations in 449 * rump kernel drivers. Note that we don't need p_lock in a 450 * rump kernel, since we do refcounting only for curlwp. 451 */ 452void 453rump__lwproc_lwphold(void) 454{ 455 struct lwp *l = curlwp; 456 457 l->l_refcnt++; 458 l->l_flag &= ~LW_WEXIT; 459} 460 461void 462rump__lwproc_lwprele(void) 463{ 464 struct lwp *l = curlwp; 465 466 l->l_refcnt--; 467 if (l->l_refcnt == 0) 468 l->l_flag |= LW_WEXIT; 469} 470 471struct lwp * 472rump_lwproc_curlwp(void) 473{ 474 struct lwp *l = curlwp; 475 476 if (l->l_flag & LW_WEXIT) 477 return NULL; 478 return l; 479} 480 481/* this interface is under construction (like the proverbial 90's web page) */ 482int rump_i_know_what_i_am_doing_with_sysents = 0; 483void 484rump_lwproc_sysent_usenative() 485{ 486 487 if (!rump_i_know_what_i_am_doing_with_sysents) 488 panic("don't use rump_lwproc_sysent_usenative()"); 489 curproc->p_emul = &emul_netbsd; 490} 491