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