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