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