rump.c revision 1.103 1 1.103 pooka /* $NetBSD: rump.c,v 1.103 2009/04/29 15:49:28 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Development of this software was supported by Google Summer of Code.
7 1.1 pooka *
8 1.1 pooka * Redistribution and use in source and binary forms, with or without
9 1.1 pooka * modification, are permitted provided that the following conditions
10 1.1 pooka * are met:
11 1.1 pooka * 1. Redistributions of source code must retain the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer.
13 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 pooka * notice, this list of conditions and the following disclaimer in the
15 1.1 pooka * documentation and/or other materials provided with the distribution.
16 1.1 pooka *
17 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 pooka * SUCH DAMAGE.
28 1.1 pooka */
29 1.1 pooka
30 1.78 pooka #include <sys/cdefs.h>
31 1.103 pooka __KERNEL_RCSID(0, "$NetBSD: rump.c,v 1.103 2009/04/29 15:49:28 pooka Exp $");
32 1.78 pooka
33 1.1 pooka #include <sys/param.h>
34 1.64 pooka #include <sys/atomic.h>
35 1.75 pooka #include <sys/buf.h>
36 1.66 pooka #include <sys/callout.h>
37 1.79 pooka #include <sys/conf.h>
38 1.27 pooka #include <sys/cpu.h>
39 1.100 pooka #include <sys/evcnt.h>
40 1.98 pooka #include <sys/event.h>
41 1.1 pooka #include <sys/filedesc.h>
42 1.72 pooka #include <sys/iostat.h>
43 1.1 pooka #include <sys/kauth.h>
44 1.80 pooka #include <sys/kernel.h>
45 1.14 pooka #include <sys/kmem.h>
46 1.81 pooka #include <sys/kprintf.h>
47 1.82 pooka #include <sys/ksyms.h>
48 1.81 pooka #include <sys/msgbuf.h>
49 1.49 simonb #include <sys/module.h>
50 1.71 pooka #include <sys/once.h>
51 1.65 pooka #include <sys/percpu.h>
52 1.1 pooka #include <sys/queue.h>
53 1.1 pooka #include <sys/resourcevar.h>
54 1.27 pooka #include <sys/select.h>
55 1.87 pooka #include <sys/sysctl.h>
56 1.96 pooka #include <sys/syscall.h>
57 1.87 pooka #include <sys/tty.h>
58 1.68 pooka #include <sys/uidinfo.h>
59 1.89 pooka #include <sys/vmem.h>
60 1.1 pooka
61 1.48 pooka #include <rump/rumpuser.h>
62 1.48 pooka
63 1.101 pooka #include <secmodel/secmodel.h>
64 1.101 pooka
65 1.8 pooka #include "rump_private.h"
66 1.71 pooka #include "rump_net_private.h"
67 1.75 pooka #include "rump_vfs_private.h"
68 1.1 pooka
69 1.40 ad struct proc proc0;
70 1.81 pooka struct session rump_session = {
71 1.81 pooka .s_count = 1,
72 1.81 pooka .s_flags = 0,
73 1.81 pooka .s_leader = &proc0,
74 1.81 pooka .s_login = "rumphobo",
75 1.81 pooka .s_sid = 0,
76 1.81 pooka };
77 1.81 pooka struct pgrp rump_pgrp = {
78 1.81 pooka .pg_members = LIST_HEAD_INITIALIZER(pg_members),
79 1.81 pooka .pg_session = &rump_session,
80 1.81 pooka .pg_jobc = 1,
81 1.81 pooka };
82 1.1 pooka struct pstats rump_stats;
83 1.1 pooka struct plimit rump_limits;
84 1.1 pooka struct cpu_info rump_cpu;
85 1.40 ad struct filedesc rump_filedesc0;
86 1.36 pooka struct proclist allproc;
87 1.46 ad char machine[] = "rump";
88 1.59 pooka static kauth_cred_t rump_susercred;
89 1.1 pooka
90 1.101 pooka /* pretend the master rump proc is init */
91 1.101 pooka struct proc *initproc = &proc0;
92 1.101 pooka
93 1.77 pooka struct rumpuser_mtx *rump_giantlock;
94 1.19 pooka
95 1.24 pooka sigset_t sigcantmask;
96 1.24 pooka
97 1.54 pooka #ifdef RUMP_WITHOUT_THREADS
98 1.54 pooka int rump_threads = 0;
99 1.54 pooka #else
100 1.54 pooka int rump_threads = 1;
101 1.54 pooka #endif
102 1.54 pooka
103 1.14 pooka static void
104 1.14 pooka rump_aiodone_worker(struct work *wk, void *dummy)
105 1.14 pooka {
106 1.14 pooka struct buf *bp = (struct buf *)wk;
107 1.14 pooka
108 1.14 pooka KASSERT(&bp->b_work == wk);
109 1.14 pooka bp->b_iodone(bp);
110 1.14 pooka }
111 1.14 pooka
112 1.51 pooka static int rump_inited;
113 1.51 pooka static struct emul emul_rump;
114 1.27 pooka
115 1.86 pooka void rump__unavailable(void);
116 1.86 pooka void rump__unavailable() {}
117 1.86 pooka __weak_alias(rump_net_init,rump__unavailable);
118 1.86 pooka __weak_alias(rump_vfs_init,rump__unavailable);
119 1.75 pooka
120 1.92 pooka void rump__unavailable_vfs_panic(void);
121 1.92 pooka void rump__unavailable_vfs_panic() {panic("vfs component not available");}
122 1.92 pooka __weak_alias(vn_open,rump__unavailable_vfs_panic);
123 1.92 pooka __weak_alias(vn_rdwr,rump__unavailable_vfs_panic);
124 1.92 pooka __weak_alias(vn_close,rump__unavailable_vfs_panic);
125 1.92 pooka
126 1.75 pooka static void
127 1.75 pooka pvfsinit_nop(struct proc *p)
128 1.75 pooka {
129 1.75 pooka
130 1.75 pooka return;
131 1.75 pooka }
132 1.75 pooka
133 1.75 pooka static void
134 1.75 pooka pvfsrele_nop(struct proc *p)
135 1.75 pooka {
136 1.75 pooka
137 1.75 pooka return;
138 1.75 pooka }
139 1.75 pooka
140 1.75 pooka rump_proc_vfs_init_fn rump_proc_vfs_init = pvfsinit_nop;
141 1.75 pooka rump_proc_vfs_release_fn rump_proc_vfs_release = pvfsrele_nop;
142 1.71 pooka
143 1.61 pooka int
144 1.83 pooka rump__init(int rump_version)
145 1.1 pooka {
146 1.36 pooka char buf[256];
147 1.14 pooka struct proc *p;
148 1.14 pooka struct lwp *l;
149 1.1 pooka int error;
150 1.1 pooka
151 1.27 pooka /* XXX */
152 1.27 pooka if (rump_inited)
153 1.61 pooka return 0;
154 1.27 pooka rump_inited = 1;
155 1.27 pooka
156 1.61 pooka if (rump_version != RUMP_VERSION) {
157 1.61 pooka printf("rump version mismatch, %d vs. %d\n",
158 1.61 pooka rump_version, RUMP_VERSION);
159 1.61 pooka return EPROGMISMATCH;
160 1.61 pooka }
161 1.61 pooka
162 1.54 pooka if (rumpuser_getenv("RUMP_THREADS", buf, sizeof(buf), &error) == 0) {
163 1.54 pooka rump_threads = *buf != '0';
164 1.54 pooka }
165 1.84 pooka rumpuser_thrinit(_kernel_lock, _kernel_unlock, rump_threads);
166 1.36 pooka
167 1.87 pooka mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_NONE);
168 1.77 pooka rumpuser_mutex_recursive_init(&rump_giantlock);
169 1.82 pooka ksyms_init();
170 1.89 pooka rumpvm_init();
171 1.100 pooka evcnt_init();
172 1.89 pooka
173 1.89 pooka once_init();
174 1.63 pooka
175 1.52 pooka rump_sleepers_init();
176 1.102 pooka
177 1.90 pooka pool_subsystem_init();
178 1.52 pooka kmem_init();
179 1.102 pooka
180 1.81 pooka kprintf_init();
181 1.81 pooka loginit();
182 1.52 pooka
183 1.59 pooka kauth_init();
184 1.59 pooka rump_susercred = rump_cred_create(0, 0, 0, NULL);
185 1.59 pooka
186 1.14 pooka l = &lwp0;
187 1.40 ad p = &proc0;
188 1.14 pooka p->p_stats = &rump_stats;
189 1.14 pooka p->p_limit = &rump_limits;
190 1.81 pooka p->p_pgrp = &rump_pgrp;
191 1.14 pooka p->p_pid = 0;
192 1.40 ad p->p_fd = &rump_filedesc0;
193 1.27 pooka p->p_vmspace = &rump_vmspace;
194 1.51 pooka p->p_emul = &emul_rump;
195 1.94 pooka p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
196 1.59 pooka l->l_cred = rump_cred_suserget();
197 1.14 pooka l->l_proc = p;
198 1.14 pooka l->l_lid = 1;
199 1.99 pooka LIST_INIT(&allproc);
200 1.103 pooka LIST_INSERT_HEAD(&allproc, &proc0, p_list);
201 1.91 pooka proc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
202 1.36 pooka
203 1.1 pooka rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
204 1.27 pooka rump_limits.pl_rlimit[RLIMIT_NOFILE].rlim_cur = RLIM_INFINITY;
205 1.70 pooka rump_limits.pl_rlimit[RLIMIT_SBSIZE].rlim_cur = RLIM_INFINITY;
206 1.1 pooka
207 1.66 pooka callout_startup();
208 1.66 pooka callout_init_cpu(&rump_cpu);
209 1.43 pooka
210 1.98 pooka kqueue_init();
211 1.72 pooka iostat_init();
212 1.68 pooka uid_init();
213 1.65 pooka percpu_init();
214 1.43 pooka fd_sys_init();
215 1.44 ad module_init();
216 1.51 pooka sysctl_init();
217 1.63 pooka softint_init(&rump_cpu);
218 1.80 pooka cold = 0;
219 1.79 pooka devsw_init();
220 1.101 pooka secmodel_start();
221 1.1 pooka
222 1.75 pooka /* these do nothing if not present */
223 1.75 pooka rump_vfs_init();
224 1.75 pooka rump_net_init();
225 1.31 pooka
226 1.14 pooka /* aieeeedondest */
227 1.54 pooka if (rump_threads) {
228 1.54 pooka if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
229 1.54 pooka rump_aiodone_worker, NULL, 0, 0, 0))
230 1.54 pooka panic("aiodoned");
231 1.54 pooka }
232 1.14 pooka
233 1.1 pooka rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
234 1.1 pooka hostnamelen = strlen(hostname);
235 1.24 pooka
236 1.24 pooka sigemptyset(&sigcantmask);
237 1.27 pooka
238 1.56 pooka lwp0.l_fd = proc0.p_fd = fd_init(&rump_filedesc0);
239 1.2 pooka
240 1.89 pooka #ifdef RUMP_USE_REAL_ALLOCATORS
241 1.89 pooka if (rump_threads)
242 1.89 pooka vmem_rehash_start();
243 1.89 pooka #endif
244 1.89 pooka
245 1.2 pooka return 0;
246 1.2 pooka }
247 1.2 pooka
248 1.8 pooka struct uio *
249 1.8 pooka rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
250 1.8 pooka {
251 1.8 pooka struct uio *uio;
252 1.8 pooka enum uio_rw uiorw;
253 1.8 pooka
254 1.8 pooka switch (rw) {
255 1.8 pooka case RUMPUIO_READ:
256 1.8 pooka uiorw = UIO_READ;
257 1.8 pooka break;
258 1.8 pooka case RUMPUIO_WRITE:
259 1.8 pooka uiorw = UIO_WRITE;
260 1.8 pooka break;
261 1.11 pooka default:
262 1.11 pooka panic("%s: invalid rw %d", __func__, rw);
263 1.8 pooka }
264 1.8 pooka
265 1.28 pooka uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
266 1.28 pooka uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
267 1.8 pooka
268 1.8 pooka uio->uio_iov->iov_base = buf;
269 1.8 pooka uio->uio_iov->iov_len = bufsize;
270 1.8 pooka
271 1.8 pooka uio->uio_iovcnt = 1;
272 1.8 pooka uio->uio_offset = offset;
273 1.8 pooka uio->uio_resid = bufsize;
274 1.8 pooka uio->uio_rw = uiorw;
275 1.8 pooka uio->uio_vmspace = UIO_VMSPACE_SYS;
276 1.8 pooka
277 1.8 pooka return uio;
278 1.8 pooka }
279 1.8 pooka
280 1.8 pooka size_t
281 1.8 pooka rump_uio_getresid(struct uio *uio)
282 1.8 pooka {
283 1.8 pooka
284 1.8 pooka return uio->uio_resid;
285 1.8 pooka }
286 1.8 pooka
287 1.8 pooka off_t
288 1.8 pooka rump_uio_getoff(struct uio *uio)
289 1.8 pooka {
290 1.8 pooka
291 1.8 pooka return uio->uio_offset;
292 1.8 pooka }
293 1.8 pooka
294 1.8 pooka size_t
295 1.8 pooka rump_uio_free(struct uio *uio)
296 1.8 pooka {
297 1.8 pooka size_t resid;
298 1.8 pooka
299 1.8 pooka resid = uio->uio_resid;
300 1.28 pooka kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
301 1.28 pooka kmem_free(uio, sizeof(*uio));
302 1.8 pooka
303 1.8 pooka return resid;
304 1.8 pooka }
305 1.8 pooka
306 1.14 pooka struct lwp *
307 1.14 pooka rump_setup_curlwp(pid_t pid, lwpid_t lid, int set)
308 1.14 pooka {
309 1.14 pooka struct lwp *l;
310 1.14 pooka struct proc *p;
311 1.14 pooka
312 1.37 pooka l = kmem_zalloc(sizeof(struct lwp), KM_SLEEP);
313 1.53 pooka if (pid != 0) {
314 1.53 pooka p = kmem_zalloc(sizeof(struct proc), KM_SLEEP);
315 1.75 pooka rump_proc_vfs_init(p);
316 1.53 pooka p->p_stats = &rump_stats;
317 1.53 pooka p->p_limit = &rump_limits;
318 1.53 pooka p->p_pid = pid;
319 1.53 pooka p->p_vmspace = &rump_vmspace;
320 1.53 pooka p->p_fd = fd_init(NULL);
321 1.53 pooka } else {
322 1.53 pooka p = &proc0;
323 1.53 pooka }
324 1.37 pooka
325 1.59 pooka l->l_cred = rump_cred_suserget();
326 1.14 pooka l->l_proc = p;
327 1.53 pooka l->l_lid = lid;
328 1.53 pooka l->l_fd = p->p_fd;
329 1.64 pooka l->l_mutex = RUMP_LMUTEX_MAGIC;
330 1.71 pooka l->l_cpu = &rump_cpu;
331 1.38 pooka
332 1.14 pooka if (set)
333 1.14 pooka rumpuser_set_curlwp(l);
334 1.14 pooka
335 1.14 pooka return l;
336 1.14 pooka }
337 1.14 pooka
338 1.14 pooka void
339 1.97 cegger rump_clear_curlwp(void)
340 1.14 pooka {
341 1.14 pooka struct lwp *l;
342 1.14 pooka
343 1.14 pooka l = rumpuser_get_curlwp();
344 1.53 pooka if (l->l_proc->p_pid != 0) {
345 1.53 pooka fd_free();
346 1.75 pooka rump_proc_vfs_release(l->l_proc);
347 1.59 pooka rump_cred_destroy(l->l_cred);
348 1.53 pooka kmem_free(l->l_proc, sizeof(*l->l_proc));
349 1.53 pooka }
350 1.37 pooka kmem_free(l, sizeof(*l));
351 1.14 pooka rumpuser_set_curlwp(NULL);
352 1.14 pooka }
353 1.14 pooka
354 1.14 pooka struct lwp *
355 1.97 cegger rump_get_curlwp(void)
356 1.14 pooka {
357 1.14 pooka struct lwp *l;
358 1.14 pooka
359 1.14 pooka l = rumpuser_get_curlwp();
360 1.14 pooka if (l == NULL)
361 1.14 pooka l = &lwp0;
362 1.14 pooka
363 1.14 pooka return l;
364 1.14 pooka }
365 1.15 pooka
366 1.59 pooka kauth_cred_t
367 1.59 pooka rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
368 1.59 pooka {
369 1.59 pooka kauth_cred_t cred;
370 1.59 pooka int rv;
371 1.59 pooka
372 1.59 pooka cred = kauth_cred_alloc();
373 1.59 pooka kauth_cred_setuid(cred, uid);
374 1.59 pooka kauth_cred_seteuid(cred, uid);
375 1.59 pooka kauth_cred_setsvuid(cred, uid);
376 1.59 pooka kauth_cred_setgid(cred, gid);
377 1.59 pooka kauth_cred_setgid(cred, gid);
378 1.59 pooka kauth_cred_setegid(cred, gid);
379 1.59 pooka kauth_cred_setsvgid(cred, gid);
380 1.59 pooka rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
381 1.59 pooka /* oh this is silly. and by "this" I mean kauth_cred_setgroups() */
382 1.59 pooka assert(rv == 0);
383 1.59 pooka
384 1.59 pooka return cred;
385 1.59 pooka }
386 1.59 pooka
387 1.59 pooka void
388 1.59 pooka rump_cred_destroy(kauth_cred_t cred)
389 1.59 pooka {
390 1.59 pooka
391 1.59 pooka kauth_cred_free(cred);
392 1.59 pooka }
393 1.59 pooka
394 1.59 pooka kauth_cred_t
395 1.97 cegger rump_cred_suserget(void)
396 1.59 pooka {
397 1.59 pooka
398 1.59 pooka kauth_cred_hold(rump_susercred);
399 1.59 pooka return rump_susercred;
400 1.59 pooka }
401 1.59 pooka
402 1.94 pooka /*
403 1.94 pooka * Return the next system lwpid
404 1.94 pooka */
405 1.64 pooka lwpid_t
406 1.97 cegger rump_nextlid(void)
407 1.64 pooka {
408 1.94 pooka lwpid_t retid;
409 1.64 pooka
410 1.94 pooka mutex_enter(proc0.p_lock);
411 1.94 pooka /*
412 1.94 pooka * Take next one, don't return 0
413 1.94 pooka * XXX: most likely we'll have collisions in case this
414 1.94 pooka * wraps around.
415 1.94 pooka */
416 1.94 pooka if (++proc0.p_nlwpid == 0)
417 1.94 pooka ++proc0.p_nlwpid;
418 1.94 pooka retid = proc0.p_nlwpid;
419 1.94 pooka mutex_exit(proc0.p_lock);
420 1.64 pooka
421 1.94 pooka return retid;
422 1.64 pooka }
423 1.64 pooka
424 1.76 pooka int
425 1.76 pooka rump_module_load(struct modinfo **mi)
426 1.76 pooka {
427 1.76 pooka
428 1.76 pooka if (!module_compatible((*mi)->mi_version, __NetBSD_Version__))
429 1.76 pooka return EPROGMISMATCH;
430 1.76 pooka
431 1.76 pooka return (*mi)->mi_modcmd(MODULE_CMD_INIT, NULL);
432 1.76 pooka }
433 1.76 pooka
434 1.55 pooka int _syspuffs_stub(int, int *);
435 1.55 pooka int
436 1.55 pooka _syspuffs_stub(int fd, int *newfd)
437 1.55 pooka {
438 1.55 pooka
439 1.55 pooka return ENODEV;
440 1.55 pooka }
441 1.85 pooka __weak_alias(rump_syspuffs_glueinit,_syspuffs_stub);
442 1.95 pooka
443 1.95 pooka static int
444 1.95 pooka rump_sysproxy_local(int num, void *arg, uint8_t *data, size_t dlen,
445 1.95 pooka register_t *retval)
446 1.95 pooka {
447 1.95 pooka struct lwp *l;
448 1.95 pooka struct sysent *callp;
449 1.95 pooka
450 1.95 pooka if (__predict_false(num >= SYS_NSYSENT))
451 1.95 pooka return ENOSYS;
452 1.95 pooka
453 1.95 pooka l = curlwp;
454 1.95 pooka callp = rump_sysent + num;
455 1.95 pooka return callp->sy_call(l, (void *)data, retval);
456 1.95 pooka }
457 1.95 pooka
458 1.95 pooka rump_sysproxy_t rump_sysproxy = rump_sysproxy_local;
459 1.95 pooka void *rump_sysproxy_arg;
460 1.95 pooka
461 1.95 pooka /*
462 1.95 pooka * This whole syscall-via-rpc is still taking form. For example, it
463 1.95 pooka * may be necessary to set syscalls individually instead of lobbing
464 1.95 pooka * them all to the same place. So don't think this interface is
465 1.95 pooka * set in stone.
466 1.95 pooka */
467 1.95 pooka int
468 1.95 pooka rump_sysproxy_set(rump_sysproxy_t proxy, void *arg)
469 1.95 pooka {
470 1.95 pooka
471 1.95 pooka if (rump_sysproxy_arg)
472 1.95 pooka return EBUSY;
473 1.95 pooka
474 1.95 pooka rump_sysproxy_arg = arg;
475 1.95 pooka rump_sysproxy = proxy;
476 1.95 pooka
477 1.95 pooka return 0;
478 1.95 pooka }
479