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