lwproc.c revision 1.1 1 /* $NetBSD: lwproc.c,v 1.1 2010/09/01 19:37:58 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2010 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.1 2010/09/01 19:37:58 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 static void
48 lwproc_proc_free(struct proc *p)
49 {
50 kauth_cred_t cred;
51
52 mutex_enter(proc_lock);
53
54 KASSERT(p->p_nlwps == 0);
55 KASSERT(LIST_EMPTY(&p->p_lwps));
56 KASSERT(p->p_stat == SIDL || p->p_stat == SDEAD);
57
58 LIST_REMOVE(p, p_list);
59 LIST_REMOVE(p, p_sibling);
60 proc_free_pid(p->p_pid); /* decrements nprocs */
61 proc_leavepgrp(p); /* releases proc_lock */
62
63 cred = p->p_cred;
64 chgproccnt(kauth_cred_getuid(cred), -1);
65 if (rump_proc_vfs_release)
66 rump_proc_vfs_release(p);
67
68 limfree(p->p_limit);
69 pstatsfree(p->p_stats);
70 kauth_cred_free(p->p_cred);
71 proc_finispecific(p);
72
73 mutex_obj_free(p->p_lock);
74 mutex_destroy(&p->p_stmutex);
75 mutex_destroy(&p->p_auxlock);
76 rw_destroy(&p->p_reflock);
77 cv_destroy(&p->p_waitcv);
78 cv_destroy(&p->p_lwpcv);
79
80 proc_free_mem(p);
81 }
82
83 /*
84 * Allocate a new process. Mostly mimic fork by
85 * copying the properties of the parent. However, there are some
86 * differences. For example, we never share the fd table.
87 *
88 * Switch to the new lwp and return a pointer to it.
89 */
90 static struct proc *
91 lwproc_newproc(struct proc *parent)
92 {
93 uid_t uid = kauth_cred_getuid(parent->p_cred);
94 struct proc *p;
95
96 /* maxproc not enforced */
97 atomic_inc_uint(&nprocs);
98
99 /* allocate process */
100 p = proc_alloc();
101 memset(&p->p_startzero, 0,
102 offsetof(struct proc, p_endzero)
103 - offsetof(struct proc, p_startzero));
104 memcpy(&p->p_startcopy, &parent->p_startcopy,
105 offsetof(struct proc, p_endcopy)
106 - offsetof(struct proc, p_startcopy));
107
108 p->p_stats = pstatscopy(parent->p_stats);
109
110 /* not based on parent */
111 p->p_vmspace = &vmspace0;
112 p->p_emul = &emul_netbsd;
113 p->p_fd = fd_init(NULL);
114 lim_addref(parent->p_limit);
115 p->p_limit = parent->p_limit;
116
117 LIST_INIT(&p->p_lwps);
118 LIST_INIT(&p->p_children);
119
120 p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
121 mutex_init(&p->p_stmutex, MUTEX_DEFAULT, IPL_NONE);
122 mutex_init(&p->p_auxlock, MUTEX_DEFAULT, IPL_NONE);
123 rw_init(&p->p_reflock);
124 cv_init(&p->p_waitcv, "pwait");
125 cv_init(&p->p_lwpcv, "plwp");
126
127 p->p_pptr = parent;
128 p->p_ppid = parent->p_pid;
129
130 kauth_proc_fork(parent, p);
131
132 /* initialize cwd in rump kernels with vfs */
133 if (rump_proc_vfs_init)
134 rump_proc_vfs_init(p);
135
136 chgproccnt(uid, 1); /* not enforced */
137
138 /* publish proc various proc lists */
139 mutex_enter(proc_lock);
140 LIST_INSERT_HEAD(&allproc, p, p_list);
141 LIST_INSERT_HEAD(&parent->p_children, p, p_sibling);
142 LIST_INSERT_AFTER(parent, p, p_pglist);
143 mutex_exit(proc_lock);
144
145 return p;
146 }
147
148 static void
149 lwproc_freelwp(struct lwp *l)
150 {
151 struct proc *p;
152 bool freeproc;
153
154 p = l->l_proc;
155 mutex_enter(p->p_lock);
156
157 /* XXX: l_refcnt */
158 KASSERT(l->l_flag & LW_WEXIT);
159 KASSERT(l->l_refcnt == 0);
160
161 /* ok, zero references, continue with nuke */
162 LIST_REMOVE(l, l_sibling);
163 KASSERT(p->p_nlwps >= 1);
164 if (--p->p_nlwps == 0) {
165 KASSERT(p != &proc0);
166 p->p_stat = SDEAD;
167 }
168 freeproc = p->p_nlwps == 0;
169 cv_broadcast(&p->p_lwpcv); /* nobody sleeps on this in rump? */
170 kauth_cred_free(l->l_cred);
171 mutex_exit(p->p_lock);
172
173 mutex_enter(proc_lock);
174 LIST_REMOVE(l, l_list);
175 mutex_exit(proc_lock);
176
177 if (l->l_name)
178 kmem_free(l->l_name, MAXCOMLEN);
179 lwp_finispecific(l);
180
181 kmem_free(l, sizeof(*l));
182
183 if (p->p_stat == SDEAD)
184 lwproc_proc_free(p);
185 }
186
187 /*
188 * called with p_lock held, releases lock before return
189 */
190 static void
191 lwproc_makelwp(struct proc *p, struct lwp *l, bool doswitch, bool procmake)
192 {
193
194 p->p_nlwps++;
195 l->l_refcnt = 1;
196 l->l_proc = p;
197
198 l->l_lid = p->p_nlwpid++;
199 LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling);
200 mutex_exit(p->p_lock);
201
202 lwp_update_creds(l);
203
204 l->l_fd = p->p_fd;
205 l->l_cpu = NULL;
206 l->l_target_cpu = rump_cpu; /* Initial target CPU always the same */
207
208 lwp_initspecific(l);
209
210 if (doswitch) {
211 rump_lwproc_switch(l);
212 }
213
214 /* filedesc already has refcount 1 when process is created */
215 if (!procmake) {
216 fd_hold(l);
217 }
218
219 mutex_enter(proc_lock);
220 LIST_INSERT_HEAD(&alllwp, l, l_list);
221 mutex_exit(proc_lock);
222 }
223
224 struct lwp *
225 rump__lwproc_allockernlwp(void)
226 {
227 struct proc *p;
228 struct lwp *l;
229
230 l = kmem_zalloc(sizeof(*l), KM_SLEEP);
231
232 p = &proc0;
233 mutex_enter(p->p_lock);
234 lwproc_makelwp(p, l, false, false);
235
236 return l;
237 }
238
239 int
240 rump_lwproc_newlwp(pid_t pid)
241 {
242 struct proc *p;
243 struct lwp *l;
244
245 l = kmem_zalloc(sizeof(*l), KM_SLEEP);
246 mutex_enter(proc_lock);
247 p = proc_find_raw(pid);
248 if (p == NULL) {
249 mutex_exit(proc_lock);
250 kmem_free(l, sizeof(*l));
251 return ESRCH;
252 }
253 mutex_enter(p->p_lock);
254 mutex_exit(proc_lock);
255 lwproc_makelwp(p, l, true, false);
256
257 return 0;
258 }
259
260 int
261 rump_lwproc_newproc(void)
262 {
263 struct proc *p;
264 struct lwp *l;
265
266 p = lwproc_newproc(curproc);
267 l = kmem_zalloc(sizeof(*l), KM_SLEEP);
268 mutex_enter(p->p_lock);
269 lwproc_makelwp(p, l, true, true);
270
271 return 0;
272 }
273
274 /*
275 * Switch to a new process/thread. Release previous one if
276 * deemed to be exiting.
277 */
278 void
279 rump_lwproc_switch(struct lwp *newlwp)
280 {
281 struct lwp *l = curlwp;
282
283 KASSERT(!(l->l_flag & LW_WEXIT) || newlwp);
284
285 if (__predict_false(newlwp && (newlwp->l_pflag & LP_RUNNING)))
286 panic("lwp %p (%d:%d) already running",
287 newlwp, newlwp->l_proc->p_pid, newlwp->l_lid);
288
289 if (newlwp == NULL) {
290 l->l_pflag &= ~LP_RUNNING;
291 l->l_flag |= LW_RUMP_CLEAR;
292 return;
293 }
294
295 /* fd_free() must be called from curlwp context. talk about ugh */
296 if (l->l_flag & LW_WEXIT) {
297 fd_free();
298 }
299
300 rumpuser_set_curlwp(NULL);
301
302 newlwp->l_cpu = newlwp->l_target_cpu = l->l_cpu;
303 newlwp->l_mutex = l->l_mutex;
304 newlwp->l_pflag |= LP_RUNNING;
305
306 rumpuser_set_curlwp(newlwp);
307
308 l->l_mutex = NULL;
309 l->l_cpu = NULL;
310 l->l_pflag &= ~LP_RUNNING;
311
312 if (l->l_flag & LW_WEXIT) {
313 lwproc_freelwp(l);
314 }
315 }
316
317 void
318 rump_lwproc_releaselwp(void)
319 {
320 struct proc *p;
321 struct lwp *l = curlwp;
322
323 p = l->l_proc;
324 mutex_enter(p->p_lock);
325 KASSERT(l->l_refcnt != 0);
326 l->l_refcnt--;
327 mutex_exit(p->p_lock);
328 l->l_flag |= LW_WEXIT; /* will be released when unscheduled */
329 }
330
331 struct lwp *
332 rump_lwproc_curlwp(void)
333 {
334 struct lwp *l = curlwp;
335
336 if (l->l_flag & LW_WEXIT)
337 return NULL;
338 return l;
339 }
340