lwproc.c revision 1.52 1 /* $NetBSD: lwproc.c,v 1.52 2022/11/02 09:01:42 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 #define RUMP__CURLWP_PRIVATE
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: lwproc.c,v 1.52 2022/11/02 09:01:42 ozaki-r 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 membar_exit();
338 kmem_free(l, sizeof(*l));
339
340 if (p->p_stat == SDEAD)
341 lwproc_proc_free(p);
342 }
343
344 extern kmutex_t unruntime_lock;
345
346 static struct lwp *
347 lwproc_makelwp(struct proc *p, bool doswitch, bool procmake)
348 {
349 struct lwp *l = kmem_zalloc(sizeof(*l), KM_SLEEP);
350
351 l->l_refcnt = 1;
352 l->l_proc = p;
353 l->l_stat = LSIDL;
354 l->l_mutex = &unruntime_lock;
355
356 proc_alloc_lwpid(p, l);
357
358 mutex_enter(p->p_lock);
359 /*
360 * Account the new lwp to the owner of the process.
361 * For some reason, NetBSD doesn't count the first lwp
362 * in a process as a lwp, so skip that.
363 */
364 if (p->p_nlwps++) {
365 chglwpcnt(kauth_cred_getuid(p->p_cred), 1);
366 }
367
368 KASSERT((p->p_sflag & PS_RUMP_LWPEXIT) == 0);
369 LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling);
370
371 l->l_fd = p->p_fd;
372 l->l_cpu = &rump_bootcpu;
373 l->l_target_cpu = &rump_bootcpu; /* Initial target CPU always same */
374 l->l_stat = LSRUN;
375 TAILQ_INIT(&l->l_ld_locks);
376 mutex_exit(p->p_lock);
377
378 lwp_update_creds(l);
379 lwp_initspecific(l);
380 PSREF_DEBUG_INIT_LWP(l);
381
382 membar_enter();
383 lwproc_curlwpop(RUMPUSER_LWP_CREATE, l);
384 if (doswitch) {
385 rump_lwproc_switch(l);
386 }
387
388 /* filedesc already has refcount 1 when process is created */
389 if (!procmake) {
390 fd_hold(l);
391 }
392
393 mutex_enter(&proc_lock);
394 LIST_INSERT_HEAD(&alllwp, l, l_list);
395 mutex_exit(&proc_lock);
396
397 return l;
398 }
399
400 struct lwp *
401 rump__lwproc_alloclwp(struct proc *p)
402 {
403 bool newproc = false;
404
405 if (p == NULL) {
406 p = lwproc_newproc(&proc0, rump_vmspace_local, RUMP_RFCFDG);
407 newproc = true;
408 }
409
410 return lwproc_makelwp(p, false, newproc);
411 }
412
413 int
414 rump_lwproc_newlwp(pid_t pid)
415 {
416 struct proc *p;
417 struct lwp *l;
418
419 l = kmem_zalloc(sizeof(*l), KM_SLEEP);
420 mutex_enter(&proc_lock);
421 p = proc_find_raw(pid);
422 if (p == NULL) {
423 mutex_exit(&proc_lock);
424 kmem_free(l, sizeof(*l));
425 return ESRCH;
426 }
427 mutex_enter(p->p_lock);
428 if (p->p_sflag & PS_RUMP_LWPEXIT) {
429 mutex_exit(&proc_lock);
430 mutex_exit(p->p_lock);
431 kmem_free(l, sizeof(*l));
432 return EBUSY;
433 }
434 mutex_exit(p->p_lock);
435 mutex_exit(&proc_lock);
436
437 /* XXX what holds proc? */
438
439 lwproc_makelwp(p, true, false);
440
441 return 0;
442 }
443
444 int
445 rump_lwproc_rfork_vmspace(struct vmspace *vm, int flags)
446 {
447 struct proc *p;
448
449 if (flags & ~(RUMP_RFFDG|RUMP_RFCFDG) ||
450 (~flags & (RUMP_RFFDG|RUMP_RFCFDG)) == 0)
451 return EINVAL;
452
453 p = lwproc_newproc(curproc, vm, flags);
454 lwproc_makelwp(p, true, true);
455
456 return 0;
457 }
458
459 int
460 rump_lwproc_rfork(int flags)
461 {
462
463 return rump_lwproc_rfork_vmspace(rump_vmspace_local, flags);
464 }
465
466 /*
467 * Switch to a new process/thread. Release previous one if
468 * deemed to be exiting. This is considered a slow path for
469 * rump kernel entry.
470 */
471 void
472 rump_lwproc_switch(struct lwp *newlwp)
473 {
474 struct lwp *l = curlwp;
475
476 KASSERT(!(l->l_flag & LW_WEXIT) || newlwp);
477
478 if (__predict_false(newlwp && (newlwp->l_pflag & LP_RUNNING)))
479 panic("lwp %p (%d:%d) already running",
480 newlwp, newlwp->l_proc->p_pid, newlwp->l_lid);
481
482 if (newlwp == NULL) {
483 l->l_pflag &= ~LP_RUNNING;
484 l->l_flag |= LW_RUMP_CLEAR;
485 return;
486 }
487
488 /* fd_free() must be called from curlwp context. talk about ugh */
489 if (l->l_flag & LW_WEXIT) {
490 fd_free();
491 }
492
493 KERNEL_UNLOCK_ALL(NULL, &l->l_biglocks);
494 lwproc_curlwpop(RUMPUSER_LWP_CLEAR, l);
495
496 newlwp->l_cpu = newlwp->l_target_cpu = l->l_cpu;
497 newlwp->l_mutex = l->l_mutex;
498 newlwp->l_pflag |= LP_RUNNING;
499
500 lwproc_curlwpop(RUMPUSER_LWP_SET, newlwp);
501 curcpu()->ci_curlwp = newlwp;
502 KERNEL_LOCK(newlwp->l_biglocks, NULL);
503
504 /*
505 * Check if the thread should get a signal. This is
506 * mostly to satisfy the "record" rump sigmodel.
507 */
508 mutex_enter(newlwp->l_proc->p_lock);
509 if (sigispending(newlwp, 0)) {
510 newlwp->l_flag |= LW_PENDSIG;
511 }
512 mutex_exit(newlwp->l_proc->p_lock);
513
514 l->l_mutex = &unruntime_lock;
515 l->l_pflag &= ~LP_RUNNING;
516 l->l_flag &= ~LW_PENDSIG;
517 l->l_stat = LSRUN;
518
519 if (l->l_flag & LW_WEXIT) {
520 l->l_stat = LSIDL;
521 lwproc_freelwp(l);
522 }
523 }
524
525 /*
526 * Mark the current thread to be released upon return from
527 * kernel.
528 */
529 void
530 rump_lwproc_releaselwp(void)
531 {
532 struct lwp *l = curlwp;
533
534 if (l->l_refcnt == 0 || l->l_flag & LW_WEXIT)
535 panic("releasing non-pertinent lwp");
536
537 rump__lwproc_lwprele();
538 KASSERT(l->l_refcnt == 0 && (l->l_flag & LW_WEXIT));
539 }
540
541 /*
542 * In-kernel routines used to add and remove references for the
543 * current thread. The main purpose is to make it possible for
544 * implicit threads to persist over scheduling operations in
545 * rump kernel drivers. Note that we don't need p_lock in a
546 * rump kernel, since we do refcounting only for curlwp.
547 */
548 void
549 rump__lwproc_lwphold(void)
550 {
551 struct lwp *l = curlwp;
552
553 l->l_refcnt++;
554 l->l_flag &= ~LW_WEXIT;
555 }
556
557 void
558 rump__lwproc_lwprele(void)
559 {
560 struct lwp *l = curlwp;
561
562 l->l_refcnt--;
563 if (l->l_refcnt == 0)
564 l->l_flag |= LW_WEXIT;
565 }
566
567 struct lwp *
568 rump_lwproc_curlwp(void)
569 {
570 struct lwp *l = curlwp;
571
572 if (l->l_flag & LW_WEXIT)
573 return NULL;
574 return l;
575 }
576
577 /* this interface is under construction (like the proverbial 90's web page) */
578 int rump_i_know_what_i_am_doing_with_sysents = 0;
579 void
580 rump_lwproc_sysent_usenative()
581 {
582
583 if (!rump_i_know_what_i_am_doing_with_sysents)
584 panic("don't use rump_lwproc_sysent_usenative()");
585 curproc->p_emul = &emul_netbsd;
586 }
587