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