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