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