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