lwproc.c revision 1.18
1/*      $NetBSD: lwproc.c,v 1.18 2011/05/01 02:52:42 pgoyette 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#include <sys/cdefs.h>
29__KERNEL_RCSID(0, "$NetBSD: lwproc.c,v 1.18 2011/05/01 02:52:42 pgoyette Exp $");
30
31#include <sys/param.h>
32#include <sys/atomic.h>
33#include <sys/filedesc.h>
34#include <sys/kauth.h>
35#include <sys/kmem.h>
36#include <sys/lwp.h>
37#include <sys/pool.h>
38#include <sys/proc.h>
39#include <sys/queue.h>
40#include <sys/resourcevar.h>
41#include <sys/uidinfo.h>
42
43#include <rump/rumpuser.h>
44
45#include "rump_private.h"
46
47static void
48lwproc_proc_free(struct proc *p)
49{
50	kauth_cred_t cred;
51
52	mutex_enter(proc_lock);
53
54	KASSERT(p->p_nlwps == 0);
55	KASSERT(LIST_EMPTY(&p->p_lwps));
56	KASSERT(p->p_stat == SACTIVE || p->p_stat == SDYING ||
57	    p->p_stat == SDEAD);
58
59	LIST_REMOVE(p, p_list);
60	LIST_REMOVE(p, p_sibling);
61	proc_free_pid(p->p_pid); /* decrements nprocs */
62	proc_leavepgrp(p); /* releases proc_lock */
63
64	cred = p->p_cred;
65	chgproccnt(kauth_cred_getuid(cred), -1);
66	if (rump_proc_vfs_release)
67		rump_proc_vfs_release(p);
68
69	lim_free(p->p_limit);
70	pstatsfree(p->p_stats);
71	kauth_cred_free(p->p_cred);
72	proc_finispecific(p);
73
74	mutex_obj_free(p->p_lock);
75	mutex_destroy(&p->p_stmutex);
76	mutex_destroy(&p->p_auxlock);
77	rw_destroy(&p->p_reflock);
78	cv_destroy(&p->p_waitcv);
79	cv_destroy(&p->p_lwpcv);
80
81	/* non-kernel vmspaces are not shared */
82	if (!RUMP_LOCALPROC_P(p)) {
83		KASSERT(p->p_vmspace->vm_refcnt == 1);
84		kmem_free(p->p_vmspace, sizeof(*p->p_vmspace));
85	}
86
87	proc_free_mem(p);
88}
89
90/*
91 * Allocate a new process.  Mostly mimic fork by
92 * copying the properties of the parent.  However, there are some
93 * differences.  For example, we never share the fd table.
94 *
95 * Switch to the new lwp and return a pointer to it.
96 */
97static struct proc *
98lwproc_newproc(struct proc *parent, int flags)
99{
100	uid_t uid = kauth_cred_getuid(parent->p_cred);
101	struct proc *p;
102
103	/* maxproc not enforced */
104	atomic_inc_uint(&nprocs);
105
106	/* allocate process */
107	p = proc_alloc();
108	memset(&p->p_startzero, 0,
109	    offsetof(struct proc, p_endzero)
110	      - offsetof(struct proc, p_startzero));
111	memcpy(&p->p_startcopy, &parent->p_startcopy,
112	    offsetof(struct proc, p_endcopy)
113	      - offsetof(struct proc, p_startcopy));
114
115	/* some other garbage we need to zero */
116	p->p_sigacts = NULL;
117	p->p_aio = NULL;
118	p->p_dtrace = NULL;
119	p->p_mqueue_cnt = p->p_exitsig = 0;
120	p->p_flag = p->p_sflag = p->p_slflag = p->p_lflag = p->p_stflag = 0;
121	p->p_trace_enabled = 0;
122	p->p_xstat = p->p_acflag = 0;
123	p->p_stackbase = 0;
124
125	p->p_stats = pstatscopy(parent->p_stats);
126
127	p->p_vmspace = vmspace_kernel();
128	p->p_emul = &emul_netbsd;
129	if (*parent->p_comm)
130		strcpy(p->p_comm, parent->p_comm);
131	else
132		strcpy(p->p_comm, "rumproc");
133
134	if ((flags & RUMP_RFCFDG) == 0)
135		KASSERT(parent == curproc);
136	if (flags & RUMP_RFFDG)
137		p->p_fd = fd_copy();
138	else if (flags & RUMP_RFCFDG)
139		p->p_fd = fd_init(NULL);
140	else
141		fd_share(p);
142
143	lim_addref(parent->p_limit);
144	p->p_limit = parent->p_limit;
145
146	LIST_INIT(&p->p_lwps);
147	LIST_INIT(&p->p_children);
148
149	p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
150	mutex_init(&p->p_stmutex, MUTEX_DEFAULT, IPL_NONE);
151	mutex_init(&p->p_auxlock, MUTEX_DEFAULT, IPL_NONE);
152	rw_init(&p->p_reflock);
153	cv_init(&p->p_waitcv, "pwait");
154	cv_init(&p->p_lwpcv, "plwp");
155
156	p->p_pptr = parent;
157	p->p_ppid = parent->p_pid;
158	p->p_stat = SACTIVE;
159
160	kauth_proc_fork(parent, p);
161
162	/* initialize cwd in rump kernels with vfs */
163	if (rump_proc_vfs_init)
164		rump_proc_vfs_init(p);
165
166	chgproccnt(uid, 1); /* not enforced */
167
168	/* publish proc various proc lists */
169	mutex_enter(proc_lock);
170	LIST_INSERT_HEAD(&allproc, p, p_list);
171	LIST_INSERT_HEAD(&parent->p_children, p, p_sibling);
172	LIST_INSERT_AFTER(parent, p, p_pglist);
173	mutex_exit(proc_lock);
174
175	return p;
176}
177
178static void
179lwproc_freelwp(struct lwp *l)
180{
181	struct proc *p;
182	bool freeproc;
183
184	p = l->l_proc;
185	mutex_enter(p->p_lock);
186
187	/* XXX: l_refcnt */
188	KASSERT(l->l_flag & LW_WEXIT);
189	KASSERT(l->l_refcnt == 0);
190
191	/* ok, zero references, continue with nuke */
192	LIST_REMOVE(l, l_sibling);
193	KASSERT(p->p_nlwps >= 1);
194	if (--p->p_nlwps == 0) {
195		KASSERT(p != &proc0);
196		p->p_stat = SDEAD;
197	}
198	freeproc = p->p_nlwps == 0;
199	cv_broadcast(&p->p_lwpcv); /* nobody sleeps on this in rump? */
200	kauth_cred_free(l->l_cred);
201	mutex_exit(p->p_lock);
202
203	mutex_enter(proc_lock);
204	LIST_REMOVE(l, l_list);
205	mutex_exit(proc_lock);
206
207	if (l->l_name)
208		kmem_free(l->l_name, MAXCOMLEN);
209	lwp_finispecific(l);
210
211	kmem_free(l, sizeof(*l));
212
213	if (p->p_stat == SDEAD)
214		lwproc_proc_free(p);
215}
216
217extern kmutex_t unruntime_lock;
218
219/*
220 * called with p_lock held, releases lock before return
221 */
222static void
223lwproc_makelwp(struct proc *p, struct lwp *l, bool doswitch, bool procmake)
224{
225
226	p->p_nlwps++;
227	l->l_refcnt = 1;
228	l->l_proc = p;
229
230	l->l_lid = p->p_nlwpid++;
231	LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling);
232
233	l->l_fd = p->p_fd;
234	l->l_cpu = rump_cpu;
235	l->l_target_cpu = rump_cpu; /* Initial target CPU always the same */
236	l->l_stat = LSRUN;
237	l->l_mutex = &unruntime_lock;
238	TAILQ_INIT(&l->l_ld_locks);
239	mutex_exit(p->p_lock);
240
241	lwp_update_creds(l);
242	lwp_initspecific(l);
243
244	if (doswitch) {
245		rump_lwproc_switch(l);
246	}
247
248	/* filedesc already has refcount 1 when process is created */
249	if (!procmake) {
250		fd_hold(l);
251	}
252
253	mutex_enter(proc_lock);
254	LIST_INSERT_HEAD(&alllwp, l, l_list);
255	mutex_exit(proc_lock);
256}
257
258struct lwp *
259rump__lwproc_alloclwp(struct proc *p)
260{
261	struct lwp *l;
262	bool newproc = false;
263
264	if (p == NULL) {
265		p = lwproc_newproc(&proc0, 0);
266		newproc = true;
267	}
268
269	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
270
271	mutex_enter(p->p_lock);
272	KASSERT((p->p_sflag & PS_RUMP_LWPEXIT) == 0);
273	lwproc_makelwp(p, l, false, newproc);
274
275	return l;
276}
277
278int
279rump_lwproc_newlwp(pid_t pid)
280{
281	struct proc *p;
282	struct lwp *l;
283
284	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
285	mutex_enter(proc_lock);
286	p = proc_find_raw(pid);
287	if (p == NULL) {
288		mutex_exit(proc_lock);
289		kmem_free(l, sizeof(*l));
290		return ESRCH;
291	}
292	mutex_enter(p->p_lock);
293	if (p->p_sflag & PS_RUMP_LWPEXIT) {
294		mutex_exit(proc_lock);
295		mutex_exit(p->p_lock);
296		kmem_free(l, sizeof(*l));
297		return EBUSY;
298	}
299	mutex_exit(proc_lock);
300	lwproc_makelwp(p, l, true, false);
301
302	return 0;
303}
304
305int
306rump_lwproc_rfork(int flags)
307{
308	struct proc *p;
309	struct lwp *l;
310
311	if (flags & ~(RUMP_RFFDG|RUMP_RFCFDG) ||
312	    (~flags & (RUMP_RFFDG|RUMP_RFCFDG)) == 0)
313		return EINVAL;
314
315	p = lwproc_newproc(curproc, flags);
316	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
317	mutex_enter(p->p_lock);
318	KASSERT((p->p_sflag & PS_RUMP_LWPEXIT) == 0);
319	lwproc_makelwp(p, l, true, true);
320
321	return 0;
322}
323
324/*
325 * Switch to a new process/thread.  Release previous one if
326 * deemed to be exiting.  This is considered a slow path for
327 * rump kernel entry.
328 */
329void
330rump_lwproc_switch(struct lwp *newlwp)
331{
332	struct lwp *l = curlwp;
333
334	KASSERT(!(l->l_flag & LW_WEXIT) || newlwp);
335
336	if (__predict_false(newlwp && (newlwp->l_pflag & LP_RUNNING)))
337		panic("lwp %p (%d:%d) already running",
338		    newlwp, newlwp->l_proc->p_pid, newlwp->l_lid);
339
340	if (newlwp == NULL) {
341		l->l_pflag &= ~LP_RUNNING;
342		l->l_flag |= LW_RUMP_CLEAR;
343		return;
344	}
345
346	/* fd_free() must be called from curlwp context.  talk about ugh */
347	if (l->l_flag & LW_WEXIT) {
348		fd_free();
349	}
350
351	rumpuser_set_curlwp(NULL);
352
353	newlwp->l_cpu = newlwp->l_target_cpu = l->l_cpu;
354	newlwp->l_mutex = l->l_mutex;
355	newlwp->l_pflag |= LP_RUNNING;
356
357	rumpuser_set_curlwp(newlwp);
358
359	/*
360	 * Check if the thread should get a signal.  This is
361	 * mostly to satisfy the "record" rump sigmodel.
362	 */
363	mutex_enter(newlwp->l_proc->p_lock);
364	if (sigispending(newlwp, 0)) {
365		newlwp->l_flag |= LW_PENDSIG;
366	}
367	mutex_exit(newlwp->l_proc->p_lock);
368
369	l->l_mutex = &unruntime_lock;
370	l->l_pflag &= ~LP_RUNNING;
371	l->l_flag &= ~LW_PENDSIG;
372	l->l_stat = LSRUN;
373
374	if (l->l_flag & LW_WEXIT) {
375		lwproc_freelwp(l);
376	}
377}
378
379void
380rump_lwproc_releaselwp(void)
381{
382	struct proc *p;
383	struct lwp *l = curlwp;
384
385	if (l->l_refcnt == 0 && l->l_flag & LW_WEXIT)
386		panic("releasing non-pertinent lwp");
387
388	p = l->l_proc;
389	mutex_enter(p->p_lock);
390	KASSERT(l->l_refcnt != 0);
391	l->l_refcnt--;
392	mutex_exit(p->p_lock);
393	l->l_flag |= LW_WEXIT; /* will be released when unscheduled */
394}
395
396struct lwp *
397rump_lwproc_curlwp(void)
398{
399	struct lwp *l = curlwp;
400
401	if (l->l_flag & LW_WEXIT)
402		return NULL;
403	return l;
404}
405