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