lwproc.c revision 1.11 1 /* $NetBSD: lwproc.c,v 1.11 2011/01/28 16:34:31 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.11 2011/01/28 16:34:31 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 strcpy(p->p_comm, "rumproc");
119
120 if ((flags & RUMP_RFCFDG) == 0)
121 KASSERT(parent == curproc);
122 if (flags & RUMP_RFFDG)
123 p->p_fd = fd_copy();
124 else if (flags & RUMP_RFCFDG)
125 p->p_fd = fd_init(NULL);
126 else
127 fd_share(p);
128
129 lim_addref(parent->p_limit);
130 p->p_limit = parent->p_limit;
131
132 LIST_INIT(&p->p_lwps);
133 LIST_INIT(&p->p_children);
134
135 p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
136 mutex_init(&p->p_stmutex, MUTEX_DEFAULT, IPL_NONE);
137 mutex_init(&p->p_auxlock, MUTEX_DEFAULT, IPL_NONE);
138 rw_init(&p->p_reflock);
139 cv_init(&p->p_waitcv, "pwait");
140 cv_init(&p->p_lwpcv, "plwp");
141
142 p->p_pptr = parent;
143 p->p_ppid = parent->p_pid;
144
145 kauth_proc_fork(parent, p);
146
147 /* initialize cwd in rump kernels with vfs */
148 if (rump_proc_vfs_init)
149 rump_proc_vfs_init(p);
150
151 chgproccnt(uid, 1); /* not enforced */
152
153 /* publish proc various proc lists */
154 mutex_enter(proc_lock);
155 LIST_INSERT_HEAD(&allproc, p, p_list);
156 LIST_INSERT_HEAD(&parent->p_children, p, p_sibling);
157 LIST_INSERT_AFTER(parent, p, p_pglist);
158 mutex_exit(proc_lock);
159
160 return p;
161 }
162
163 static void
164 lwproc_freelwp(struct lwp *l)
165 {
166 struct proc *p;
167 bool freeproc;
168
169 p = l->l_proc;
170 mutex_enter(p->p_lock);
171
172 /* XXX: l_refcnt */
173 KASSERT(l->l_flag & LW_WEXIT);
174 KASSERT(l->l_refcnt == 0);
175
176 /* ok, zero references, continue with nuke */
177 LIST_REMOVE(l, l_sibling);
178 KASSERT(p->p_nlwps >= 1);
179 if (--p->p_nlwps == 0) {
180 KASSERT(p != &proc0);
181 p->p_stat = SDEAD;
182 }
183 freeproc = p->p_nlwps == 0;
184 cv_broadcast(&p->p_lwpcv); /* nobody sleeps on this in rump? */
185 kauth_cred_free(l->l_cred);
186 mutex_exit(p->p_lock);
187
188 mutex_enter(proc_lock);
189 LIST_REMOVE(l, l_list);
190 mutex_exit(proc_lock);
191
192 if (l->l_name)
193 kmem_free(l->l_name, MAXCOMLEN);
194 lwp_finispecific(l);
195
196 kmem_free(l, sizeof(*l));
197
198 if (p->p_stat == SDEAD)
199 lwproc_proc_free(p);
200 }
201
202 /*
203 * called with p_lock held, releases lock before return
204 */
205 static void
206 lwproc_makelwp(struct proc *p, struct lwp *l, bool doswitch, bool procmake)
207 {
208
209 p->p_nlwps++;
210 l->l_refcnt = 1;
211 l->l_proc = p;
212
213 l->l_lid = p->p_nlwpid++;
214 LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling);
215 mutex_exit(p->p_lock);
216
217 lwp_update_creds(l);
218
219 l->l_fd = p->p_fd;
220 l->l_cpu = NULL;
221 l->l_target_cpu = rump_cpu; /* Initial target CPU always the same */
222 l->l_stat = LSRUN;
223 TAILQ_INIT(&l->l_ld_locks);
224
225 lwp_initspecific(l);
226
227 if (doswitch) {
228 rump_lwproc_switch(l);
229 }
230
231 /* filedesc already has refcount 1 when process is created */
232 if (!procmake) {
233 fd_hold(l);
234 }
235
236 mutex_enter(proc_lock);
237 LIST_INSERT_HEAD(&alllwp, l, l_list);
238 mutex_exit(proc_lock);
239 }
240
241 struct lwp *
242 rump__lwproc_alloclwp(struct proc *p)
243 {
244 struct lwp *l;
245 bool newproc = false;
246
247 if (p == NULL) {
248 p = lwproc_newproc(&proc0, 0);
249 newproc = true;
250 }
251
252 l = kmem_zalloc(sizeof(*l), KM_SLEEP);
253
254 mutex_enter(p->p_lock);
255 lwproc_makelwp(p, l, false, newproc);
256
257 return l;
258 }
259
260 int
261 rump_lwproc_newlwp(pid_t pid)
262 {
263 struct proc *p;
264 struct lwp *l;
265
266 l = kmem_zalloc(sizeof(*l), KM_SLEEP);
267 mutex_enter(proc_lock);
268 p = proc_find_raw(pid);
269 if (p == NULL) {
270 mutex_exit(proc_lock);
271 kmem_free(l, sizeof(*l));
272 return ESRCH;
273 }
274 mutex_enter(p->p_lock);
275 mutex_exit(proc_lock);
276 lwproc_makelwp(p, l, true, false);
277
278 return 0;
279 }
280
281 int
282 rump_lwproc_rfork(int flags)
283 {
284 struct proc *p;
285 struct lwp *l;
286
287 if (flags & ~(RUMP_RFFDG|RUMP_RFCFDG) ||
288 (~flags & (RUMP_RFFDG|RUMP_RFCFDG)) == 0)
289 return EINVAL;
290
291 p = lwproc_newproc(curproc, flags);
292 l = kmem_zalloc(sizeof(*l), KM_SLEEP);
293 mutex_enter(p->p_lock);
294 lwproc_makelwp(p, l, true, true);
295
296 return 0;
297 }
298
299 /*
300 * Switch to a new process/thread. Release previous one if
301 * deemed to be exiting. This is considered a slow path for
302 * rump kernel entry.
303 */
304 void
305 rump_lwproc_switch(struct lwp *newlwp)
306 {
307 struct lwp *l = curlwp;
308
309 KASSERT(!(l->l_flag & LW_WEXIT) || newlwp);
310
311 if (__predict_false(newlwp && (newlwp->l_pflag & LP_RUNNING)))
312 panic("lwp %p (%d:%d) already running",
313 newlwp, newlwp->l_proc->p_pid, newlwp->l_lid);
314
315 if (newlwp == NULL) {
316 l->l_pflag &= ~LP_RUNNING;
317 l->l_flag |= LW_RUMP_CLEAR;
318 return;
319 }
320
321 /* fd_free() must be called from curlwp context. talk about ugh */
322 if (l->l_flag & LW_WEXIT) {
323 fd_free();
324 }
325
326 rumpuser_set_curlwp(NULL);
327
328 newlwp->l_cpu = newlwp->l_target_cpu = l->l_cpu;
329 newlwp->l_mutex = l->l_mutex;
330 newlwp->l_pflag |= LP_RUNNING;
331
332 rumpuser_set_curlwp(newlwp);
333
334 /*
335 * Check if the thread should get a signal. This is
336 * mostly to satisfy the "record" rump sigmodel.
337 */
338 mutex_enter(newlwp->l_proc->p_lock);
339 if (sigispending(newlwp, 0)) {
340 newlwp->l_flag |= LW_PENDSIG;
341 }
342 mutex_exit(newlwp->l_proc->p_lock);
343
344 l->l_mutex = NULL;
345 l->l_cpu = NULL;
346 l->l_pflag &= ~LP_RUNNING;
347 l->l_flag &= ~LW_PENDSIG;
348
349 if (l->l_flag & LW_WEXIT) {
350 lwproc_freelwp(l);
351 }
352 }
353
354 void
355 rump_lwproc_releaselwp(void)
356 {
357 struct proc *p;
358 struct lwp *l = curlwp;
359
360 if (l->l_refcnt == 0 && l->l_flag & LW_WEXIT)
361 panic("releasing non-pertinent lwp");
362
363 p = l->l_proc;
364 mutex_enter(p->p_lock);
365 KASSERT(l->l_refcnt != 0);
366 l->l_refcnt--;
367 mutex_exit(p->p_lock);
368 l->l_flag |= LW_WEXIT; /* will be released when unscheduled */
369 }
370
371 struct lwp *
372 rump_lwproc_curlwp(void)
373 {
374 struct lwp *l = curlwp;
375
376 if (l->l_flag & LW_WEXIT)
377 return NULL;
378 return l;
379 }
380