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