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