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