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