rump.c revision 1.111 1 /* $NetBSD: rump.c,v 1.111 2009/09/04 12:27:09 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.111 2009/09/04 12:27:09 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 <secmodel/secmodel.h>
64
65 #include "rump_private.h"
66 #include "rump_net_private.h"
67 #include "rump_vfs_private.h"
68
69 struct proc proc0;
70 struct session rump_session = {
71 .s_count = 1,
72 .s_flags = 0,
73 .s_leader = &proc0,
74 .s_login = "rumphobo",
75 .s_sid = 0,
76 };
77 struct pgrp rump_pgrp = {
78 .pg_members = LIST_HEAD_INITIALIZER(pg_members),
79 .pg_session = &rump_session,
80 .pg_jobc = 1,
81 };
82 struct pstats rump_stats;
83 struct plimit rump_limits;
84 struct cpu_info rump_cpu;
85 struct filedesc rump_filedesc0;
86 struct proclist allproc;
87 char machine[] = "rump";
88 static kauth_cred_t rump_susercred;
89
90 /* pretend the master rump proc is init */
91 struct proc *initproc = &proc0;
92
93 struct rumpuser_mtx *rump_giantlock;
94
95 sigset_t sigcantmask;
96
97 #ifdef RUMP_WITHOUT_THREADS
98 int rump_threads = 0;
99 #else
100 int rump_threads = 1;
101 #endif
102
103 static void
104 rump_aiodone_worker(struct work *wk, void *dummy)
105 {
106 struct buf *bp = (struct buf *)wk;
107
108 KASSERT(&bp->b_work == wk);
109 bp->b_iodone(bp);
110 }
111
112 static int rump_inited;
113 static struct emul emul_rump;
114
115 void rump__unavailable(void);
116 void rump__unavailable() {}
117 __weak_alias(rump_net_init,rump__unavailable);
118 __weak_alias(rump_vfs_init,rump__unavailable);
119
120 void rump__unavailable_vfs_panic(void);
121 void rump__unavailable_vfs_panic() {panic("vfs component not available");}
122 __weak_alias(vn_open,rump__unavailable_vfs_panic);
123 __weak_alias(vn_rdwr,rump__unavailable_vfs_panic);
124 __weak_alias(vn_close,rump__unavailable_vfs_panic);
125
126 static void
127 pvfsinit_nop(struct proc *p)
128 {
129
130 return;
131 }
132
133 static void
134 pvfsrele_nop(struct proc *p)
135 {
136
137 return;
138 }
139
140 rump_proc_vfs_init_fn rump_proc_vfs_init = pvfsinit_nop;
141 rump_proc_vfs_release_fn rump_proc_vfs_release = pvfsrele_nop;
142
143 /*
144 * Stir up the stack a bit. These are exported functions to help
145 * convince the compiler that we don't want these routines completely
146 * optimized out or inlined. Is there an easier way to do this?
147 */
148 void nullfn(uint32_t *);
149 void nullfn(uint32_t *arg){}
150 void messthestack(void);
151 void
152 messthestack(void)
153 {
154 uint32_t mess[64];
155 uint64_t d1, d2;
156 int i, error;
157
158 for (i = 0; i < 64; i++) {
159 rumpuser_gettime(&d1, &d2, &error);
160 mess[i] = d2;
161 }
162 nullfn(mess);
163 }
164
165 int
166 rump__init(int rump_version)
167 {
168 char buf[256];
169 struct proc *p;
170 struct lwp *l;
171 int error;
172
173 /* XXX */
174 if (rump_inited)
175 return 0;
176 rump_inited = 1;
177
178 /*
179 * Seed arc4random() with a "reasonable" amount of randomness.
180 * Yes, this is a quick kludge which depends on the arc4random
181 * implementation.
182 */
183 messthestack();
184 arc4random();
185
186 if (rump_version != RUMP_VERSION) {
187 printf("rump version mismatch, %d vs. %d\n",
188 rump_version, RUMP_VERSION);
189 return EPROGMISMATCH;
190 }
191
192 if (rumpuser_getenv("RUMP_THREADS", buf, sizeof(buf), &error) == 0) {
193 rump_threads = *buf != '0';
194 }
195 rumpuser_thrinit(_kernel_lock, _kernel_unlock, rump_threads);
196
197 mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_NONE);
198 rumpuser_mutex_recursive_init(&rump_giantlock);
199 ksyms_init();
200 rumpvm_init();
201 evcnt_init();
202
203 once_init();
204
205 rump_sleepers_init();
206
207 pool_subsystem_init();
208 kmem_init();
209
210 kprintf_init();
211 loginit();
212
213 kauth_init();
214 rump_susercred = rump_cred_create(0, 0, 0, NULL);
215
216 l = &lwp0;
217 p = &proc0;
218 p->p_stats = &rump_stats;
219 p->p_limit = &rump_limits;
220 p->p_pgrp = &rump_pgrp;
221 p->p_pid = 0;
222 p->p_fd = &rump_filedesc0;
223 p->p_vmspace = &rump_vmspace;
224 p->p_emul = &emul_rump;
225 p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
226 l->l_cred = rump_cred_suserget();
227 l->l_proc = p;
228 l->l_lid = 1;
229 LIST_INIT(&allproc);
230 LIST_INSERT_HEAD(&allproc, &proc0, p_list);
231 proc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
232
233 rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
234 rump_limits.pl_rlimit[RLIMIT_NOFILE].rlim_cur = RLIM_INFINITY;
235 rump_limits.pl_rlimit[RLIMIT_SBSIZE].rlim_cur = RLIM_INFINITY;
236
237 callout_startup();
238 callout_init_cpu(&rump_cpu);
239
240 kqueue_init();
241 iostat_init();
242 uid_init();
243 percpu_init();
244 fd_sys_init();
245 module_init();
246 sysctl_init();
247 softint_init(&rump_cpu);
248 cold = 0;
249 devsw_init();
250 secmodel_start();
251
252 /* these do nothing if not present */
253 rump_vfs_init();
254 rump_net_init();
255
256 /* aieeeedondest */
257 if (rump_threads) {
258 if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
259 rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE))
260 panic("aiodoned");
261 }
262
263 rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
264 hostnamelen = strlen(hostname);
265
266 sigemptyset(&sigcantmask);
267
268 lwp0.l_fd = proc0.p_fd = fd_init(&rump_filedesc0);
269
270 #ifdef RUMP_USE_REAL_ALLOCATORS
271 if (rump_threads)
272 vmem_rehash_start();
273 #endif
274
275 return 0;
276 }
277
278 struct uio *
279 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
280 {
281 struct uio *uio;
282 enum uio_rw uiorw;
283
284 switch (rw) {
285 case RUMPUIO_READ:
286 uiorw = UIO_READ;
287 break;
288 case RUMPUIO_WRITE:
289 uiorw = UIO_WRITE;
290 break;
291 default:
292 panic("%s: invalid rw %d", __func__, rw);
293 }
294
295 uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
296 uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
297
298 uio->uio_iov->iov_base = buf;
299 uio->uio_iov->iov_len = bufsize;
300
301 uio->uio_iovcnt = 1;
302 uio->uio_offset = offset;
303 uio->uio_resid = bufsize;
304 uio->uio_rw = uiorw;
305 uio->uio_vmspace = UIO_VMSPACE_SYS;
306
307 return uio;
308 }
309
310 size_t
311 rump_uio_getresid(struct uio *uio)
312 {
313
314 return uio->uio_resid;
315 }
316
317 off_t
318 rump_uio_getoff(struct uio *uio)
319 {
320
321 return uio->uio_offset;
322 }
323
324 size_t
325 rump_uio_free(struct uio *uio)
326 {
327 size_t resid;
328
329 resid = uio->uio_resid;
330 kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
331 kmem_free(uio, sizeof(*uio));
332
333 return resid;
334 }
335
336 /* public interface */
337 static pid_t nextpid = 1;
338 struct lwp *
339 rump_newproc_switch()
340 {
341 struct lwp *oldlwp = curlwp;
342 pid_t mypid;
343
344 mypid = atomic_inc_uint_nv(&nextpid);
345 if (__predict_false(mypid == 0))
346 mypid = atomic_inc_uint_nv(&nextpid);
347
348 rumpuser_set_curlwp(NULL);
349 rump_setup_curlwp(mypid, 0, 1);
350
351 return oldlwp;
352 }
353
354 /* rump private */
355 struct lwp *
356 rump_setup_curlwp(pid_t pid, lwpid_t lid, int set)
357 {
358 struct lwp *l;
359 struct proc *p;
360
361 l = kmem_zalloc(sizeof(struct lwp), KM_SLEEP);
362 if (pid != 0) {
363 p = kmem_zalloc(sizeof(struct proc), KM_SLEEP);
364 rump_proc_vfs_init(p);
365 p->p_stats = &rump_stats;
366 p->p_limit = &rump_limits;
367 p->p_pid = pid;
368 p->p_vmspace = &rump_vmspace;
369 p->p_fd = fd_init(NULL);
370 p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
371 } else {
372 p = &proc0;
373 }
374
375 l->l_cred = rump_cred_suserget();
376 l->l_proc = p;
377 l->l_lid = lid;
378 l->l_fd = p->p_fd;
379 l->l_mutex = RUMP_LMUTEX_MAGIC;
380 l->l_cpu = &rump_cpu;
381
382 if (set)
383 rumpuser_set_curlwp(l);
384
385 return l;
386 }
387
388 /* rump private. NEEDS WORK! */
389 void
390 rump_set_vmspace(struct vmspace *vm)
391 {
392 struct proc *p = curproc;
393
394 p->p_vmspace = vm;
395 }
396
397 void
398 rump_clear_curlwp(void)
399 {
400 struct lwp *l;
401 struct proc *p;
402
403 l = rumpuser_get_curlwp();
404 p = l->l_proc;
405 if (p->p_pid != 0) {
406 mutex_obj_free(p->p_lock);
407 fd_free();
408 rump_proc_vfs_release(p);
409 rump_cred_put(l->l_cred);
410 kmem_free(p, sizeof(*p));
411 }
412 kmem_free(l, sizeof(*l));
413 rumpuser_set_curlwp(NULL);
414 }
415
416 struct lwp *
417 rump_get_curlwp(void)
418 {
419 struct lwp *l;
420
421 l = rumpuser_get_curlwp();
422 if (l == NULL)
423 l = &lwp0;
424
425 return l;
426 }
427
428 void
429 rump_set_curlwp(struct lwp *l)
430 {
431
432 /* clear current */
433 rumpuser_set_curlwp(NULL);
434 /* set new */
435 rumpuser_set_curlwp(l);
436 }
437
438 kauth_cred_t
439 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
440 {
441 kauth_cred_t cred;
442 int rv;
443
444 cred = kauth_cred_alloc();
445 kauth_cred_setuid(cred, uid);
446 kauth_cred_seteuid(cred, uid);
447 kauth_cred_setsvuid(cred, uid);
448 kauth_cred_setgid(cred, gid);
449 kauth_cred_setgid(cred, gid);
450 kauth_cred_setegid(cred, gid);
451 kauth_cred_setsvgid(cred, gid);
452 rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
453 /* oh this is silly. and by "this" I mean kauth_cred_setgroups() */
454 assert(rv == 0);
455
456 return cred;
457 }
458
459 void
460 rump_cred_put(kauth_cred_t cred)
461 {
462
463 kauth_cred_free(cred);
464 }
465
466 kauth_cred_t
467 rump_cred_suserget(void)
468 {
469
470 kauth_cred_hold(rump_susercred);
471 return rump_susercred;
472 }
473
474 /*
475 * Return the next system lwpid
476 */
477 lwpid_t
478 rump_nextlid(void)
479 {
480 lwpid_t retid;
481
482 mutex_enter(proc0.p_lock);
483 /*
484 * Take next one, don't return 0
485 * XXX: most likely we'll have collisions in case this
486 * wraps around.
487 */
488 if (++proc0.p_nlwpid == 0)
489 ++proc0.p_nlwpid;
490 retid = proc0.p_nlwpid;
491 mutex_exit(proc0.p_lock);
492
493 return retid;
494 }
495
496 int
497 rump_module_init(struct modinfo *mi, prop_dictionary_t props)
498 {
499
500 if (!module_compatible(mi->mi_version, __NetBSD_Version__))
501 return EPROGMISMATCH;
502
503 return mi->mi_modcmd(MODULE_CMD_INIT, props);
504 }
505
506 int
507 rump_module_fini(struct modinfo *mi)
508 {
509
510 return mi->mi_modcmd(MODULE_CMD_FINI, NULL);
511 }
512
513 int _syspuffs_stub(int, int *);
514 int
515 _syspuffs_stub(int fd, int *newfd)
516 {
517
518 return ENODEV;
519 }
520 __weak_alias(rump_syspuffs_glueinit,_syspuffs_stub);
521
522 static int
523 rump_sysproxy_local(int num, void *arg, uint8_t *data, size_t dlen,
524 register_t *retval)
525 {
526 struct lwp *l;
527 struct sysent *callp;
528
529 if (__predict_false(num >= SYS_NSYSENT))
530 return ENOSYS;
531
532 l = curlwp;
533 callp = rump_sysent + num;
534 return callp->sy_call(l, (void *)data, retval);
535 }
536
537 rump_sysproxy_t rump_sysproxy = rump_sysproxy_local;
538 void *rump_sysproxy_arg;
539
540 /*
541 * This whole syscall-via-rpc is still taking form. For example, it
542 * may be necessary to set syscalls individually instead of lobbing
543 * them all to the same place. So don't think this interface is
544 * set in stone.
545 */
546 int
547 rump_sysproxy_set(rump_sysproxy_t proxy, void *arg)
548 {
549
550 if (rump_sysproxy_arg)
551 return EBUSY;
552
553 rump_sysproxy_arg = arg;
554 rump_sysproxy = proxy;
555
556 return 0;
557 }
558
559 int
560 rump_getversion()
561 {
562
563 return __NetBSD_Version__;
564 }
565