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