lwproc.c revision 1.32
1/*      $NetBSD: lwproc.c,v 1.32 2015/01/21 14:39:37 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.32 2015/01/21 14:39:37 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
50struct emul *emul_default = &emul_netbsd;
51
52void
53rump_lwproc_init(void)
54{
55
56	lwproc_curlwpop(RUMPUSER_LWP_CREATE, &lwp0);
57}
58
59struct lwp *
60rump_lwproc_curlwp_hypercall(void)
61{
62
63	return rumpuser_curlwp();
64}
65
66void
67rump_lwproc_curlwp_set(struct lwp *l)
68{
69
70	KASSERT(curlwp == NULL);
71	lwproc_curlwpop(RUMPUSER_LWP_SET, l);
72}
73
74void
75rump_lwproc_curlwp_clear(struct lwp *l)
76{
77
78	KASSERT(l == curlwp);
79	lwproc_curlwpop(RUMPUSER_LWP_CLEAR, l);
80}
81
82static void
83lwproc_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 */
149static struct proc *
150lwproc_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
232static void
233lwproc_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	} else {
250		chglwpcnt(kauth_cred_getuid(p->p_cred), -1);
251	}
252	cv_broadcast(&p->p_lwpcv); /* nobody sleeps on this in a rump kernel? */
253	kauth_cred_free(l->l_cred);
254	mutex_exit(p->p_lock);
255
256	mutex_enter(proc_lock);
257	LIST_REMOVE(l, l_list);
258	mutex_exit(proc_lock);
259
260	if (l->l_name)
261		kmem_free(l->l_name, MAXCOMLEN);
262	lwp_finispecific(l);
263
264	lwproc_curlwpop(RUMPUSER_LWP_DESTROY, l);
265	membar_exit();
266	kmem_free(l, sizeof(*l));
267
268	if (p->p_stat == SDEAD)
269		lwproc_proc_free(p);
270}
271
272extern kmutex_t unruntime_lock;
273
274/*
275 * called with p_lock held, releases lock before return
276 */
277static void
278lwproc_makelwp(struct proc *p, struct lwp *l, bool doswitch, bool procmake)
279{
280
281	/*
282	 * Account the new lwp to the owner of the process.
283	 * For some reason, NetBSD doesn't count the first lwp
284	 * in a process as a lwp, so skip that.
285	 */
286	if (p->p_nlwps++) {
287		chglwpcnt(kauth_cred_getuid(p->p_cred), 1);
288	}
289
290	l->l_refcnt = 1;
291	l->l_proc = p;
292
293	l->l_lid = p->p_nlwpid++;
294	LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling);
295
296	l->l_fd = p->p_fd;
297	l->l_cpu = rump_cpu;
298	l->l_target_cpu = rump_cpu; /* Initial target CPU always the same */
299	l->l_stat = LSRUN;
300	l->l_mutex = &unruntime_lock;
301	TAILQ_INIT(&l->l_ld_locks);
302	mutex_exit(p->p_lock);
303
304	lwp_update_creds(l);
305	lwp_initspecific(l);
306
307	membar_enter();
308	lwproc_curlwpop(RUMPUSER_LWP_CREATE, l);
309	if (doswitch) {
310		rump_lwproc_switch(l);
311	}
312
313	/* filedesc already has refcount 1 when process is created */
314	if (!procmake) {
315		fd_hold(l);
316	}
317
318	mutex_enter(proc_lock);
319	LIST_INSERT_HEAD(&alllwp, l, l_list);
320	mutex_exit(proc_lock);
321}
322
323struct lwp *
324rump__lwproc_alloclwp(struct proc *p)
325{
326	struct lwp *l;
327	bool newproc = false;
328
329	if (p == NULL) {
330		p = lwproc_newproc(&proc0, 0);
331		newproc = true;
332	}
333
334	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
335
336	mutex_enter(p->p_lock);
337	KASSERT((p->p_sflag & PS_RUMP_LWPEXIT) == 0);
338	lwproc_makelwp(p, l, false, newproc);
339
340	return l;
341}
342
343int
344rump_lwproc_newlwp(pid_t pid)
345{
346	struct proc *p;
347	struct lwp *l;
348
349	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
350	mutex_enter(proc_lock);
351	p = proc_find_raw(pid);
352	if (p == NULL) {
353		mutex_exit(proc_lock);
354		kmem_free(l, sizeof(*l));
355		return ESRCH;
356	}
357	mutex_enter(p->p_lock);
358	if (p->p_sflag & PS_RUMP_LWPEXIT) {
359		mutex_exit(proc_lock);
360		mutex_exit(p->p_lock);
361		kmem_free(l, sizeof(*l));
362		return EBUSY;
363	}
364	mutex_exit(proc_lock);
365	lwproc_makelwp(p, l, true, false);
366
367	return 0;
368}
369
370int
371rump_lwproc_rfork(int flags)
372{
373	struct proc *p;
374	struct lwp *l;
375
376	if (flags & ~(RUMP_RFFDG|RUMP_RFCFDG) ||
377	    (~flags & (RUMP_RFFDG|RUMP_RFCFDG)) == 0)
378		return EINVAL;
379
380	p = lwproc_newproc(curproc, flags);
381	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
382	mutex_enter(p->p_lock);
383	KASSERT((p->p_sflag & PS_RUMP_LWPEXIT) == 0);
384	lwproc_makelwp(p, l, true, true);
385
386	return 0;
387}
388
389/*
390 * Switch to a new process/thread.  Release previous one if
391 * deemed to be exiting.  This is considered a slow path for
392 * rump kernel entry.
393 */
394void
395rump_lwproc_switch(struct lwp *newlwp)
396{
397	struct lwp *l = curlwp;
398
399	KASSERT(!(l->l_flag & LW_WEXIT) || newlwp);
400
401	if (__predict_false(newlwp && (newlwp->l_pflag & LP_RUNNING)))
402		panic("lwp %p (%d:%d) already running",
403		    newlwp, newlwp->l_proc->p_pid, newlwp->l_lid);
404
405	if (newlwp == NULL) {
406		l->l_pflag &= ~LP_RUNNING;
407		l->l_flag |= LW_RUMP_CLEAR;
408		return;
409	}
410
411	/* fd_free() must be called from curlwp context.  talk about ugh */
412	if (l->l_flag & LW_WEXIT) {
413		fd_free();
414	}
415
416	KERNEL_UNLOCK_ALL(NULL, &l->l_biglocks);
417	lwproc_curlwpop(RUMPUSER_LWP_CLEAR, l);
418
419	newlwp->l_cpu = newlwp->l_target_cpu = l->l_cpu;
420	newlwp->l_mutex = l->l_mutex;
421	newlwp->l_pflag |= LP_RUNNING;
422
423	lwproc_curlwpop(RUMPUSER_LWP_SET, newlwp);
424	curcpu()->ci_curlwp = newlwp;
425	KERNEL_LOCK(newlwp->l_biglocks, NULL);
426
427	/*
428	 * Check if the thread should get a signal.  This is
429	 * mostly to satisfy the "record" rump sigmodel.
430	 */
431	mutex_enter(newlwp->l_proc->p_lock);
432	if (sigispending(newlwp, 0)) {
433		newlwp->l_flag |= LW_PENDSIG;
434	}
435	mutex_exit(newlwp->l_proc->p_lock);
436
437	l->l_mutex = &unruntime_lock;
438	l->l_pflag &= ~LP_RUNNING;
439	l->l_flag &= ~LW_PENDSIG;
440	l->l_stat = LSRUN;
441
442	if (l->l_flag & LW_WEXIT) {
443		lwproc_freelwp(l);
444	}
445}
446
447/*
448 * Mark the current thread to be released upon return from
449 * kernel.
450 */
451void
452rump_lwproc_releaselwp(void)
453{
454	struct lwp *l = curlwp;
455
456	if (l->l_refcnt == 0 || l->l_flag & LW_WEXIT)
457		panic("releasing non-pertinent lwp");
458
459	rump__lwproc_lwprele();
460	KASSERT(l->l_refcnt == 0 && (l->l_flag & LW_WEXIT));
461}
462
463/*
464 * In-kernel routines used to add and remove references for the
465 * current thread.  The main purpose is to make it possible for
466 * implicit threads to persist over scheduling operations in
467 * rump kernel drivers.  Note that we don't need p_lock in a
468 * rump kernel, since we do refcounting only for curlwp.
469 */
470void
471rump__lwproc_lwphold(void)
472{
473	struct lwp *l = curlwp;
474
475	l->l_refcnt++;
476	l->l_flag &= ~LW_WEXIT;
477}
478
479void
480rump__lwproc_lwprele(void)
481{
482	struct lwp *l = curlwp;
483
484	l->l_refcnt--;
485	if (l->l_refcnt == 0)
486		l->l_flag |= LW_WEXIT;
487}
488
489struct lwp *
490rump_lwproc_curlwp(void)
491{
492	struct lwp *l = curlwp;
493
494	if (l->l_flag & LW_WEXIT)
495		return NULL;
496	return l;
497}
498
499/* this interface is under construction (like the proverbial 90's web page) */
500int rump_i_know_what_i_am_doing_with_sysents = 0;
501void
502rump_lwproc_sysent_usenative()
503{
504
505	if (!rump_i_know_what_i_am_doing_with_sysents)
506		panic("don't use rump_lwproc_sysent_usenative()");
507	curproc->p_emul = &emul_netbsd;
508}
509