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