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