rump.c revision 1.89 1 /* $NetBSD: rump.c,v 1.89 2009/01/23 13:14:17 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.89 2009/01/23 13:14:17 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 pool_subsystem_init();
160
161 rump_sleepers_init();
162 #ifdef RUMP_USE_REAL_ALLOCATORS
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
185 rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
186 rump_limits.pl_rlimit[RLIMIT_NOFILE].rlim_cur = RLIM_INFINITY;
187 rump_limits.pl_rlimit[RLIMIT_SBSIZE].rlim_cur = RLIM_INFINITY;
188
189 callout_startup();
190 callout_init_cpu(&rump_cpu);
191
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 #ifdef RUMP_USE_REAL_ALLOCATORS
221 if (rump_threads)
222 vmem_rehash_start();
223 #endif
224
225 return 0;
226 }
227
228 struct uio *
229 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
230 {
231 struct uio *uio;
232 enum uio_rw uiorw;
233
234 switch (rw) {
235 case RUMPUIO_READ:
236 uiorw = UIO_READ;
237 break;
238 case RUMPUIO_WRITE:
239 uiorw = UIO_WRITE;
240 break;
241 default:
242 panic("%s: invalid rw %d", __func__, rw);
243 }
244
245 uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
246 uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
247
248 uio->uio_iov->iov_base = buf;
249 uio->uio_iov->iov_len = bufsize;
250
251 uio->uio_iovcnt = 1;
252 uio->uio_offset = offset;
253 uio->uio_resid = bufsize;
254 uio->uio_rw = uiorw;
255 uio->uio_vmspace = UIO_VMSPACE_SYS;
256
257 return uio;
258 }
259
260 size_t
261 rump_uio_getresid(struct uio *uio)
262 {
263
264 return uio->uio_resid;
265 }
266
267 off_t
268 rump_uio_getoff(struct uio *uio)
269 {
270
271 return uio->uio_offset;
272 }
273
274 size_t
275 rump_uio_free(struct uio *uio)
276 {
277 size_t resid;
278
279 resid = uio->uio_resid;
280 kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
281 kmem_free(uio, sizeof(*uio));
282
283 return resid;
284 }
285
286 struct lwp *
287 rump_setup_curlwp(pid_t pid, lwpid_t lid, int set)
288 {
289 struct lwp *l;
290 struct proc *p;
291
292 l = kmem_zalloc(sizeof(struct lwp), KM_SLEEP);
293 if (pid != 0) {
294 p = kmem_zalloc(sizeof(struct proc), KM_SLEEP);
295 rump_proc_vfs_init(p);
296 p->p_stats = &rump_stats;
297 p->p_limit = &rump_limits;
298 p->p_pid = pid;
299 p->p_vmspace = &rump_vmspace;
300 p->p_fd = fd_init(NULL);
301 } else {
302 p = &proc0;
303 }
304
305 l->l_cred = rump_cred_suserget();
306 l->l_proc = p;
307 l->l_lid = lid;
308 l->l_fd = p->p_fd;
309 l->l_mutex = RUMP_LMUTEX_MAGIC;
310 l->l_cpu = &rump_cpu;
311
312 if (set)
313 rumpuser_set_curlwp(l);
314
315 return l;
316 }
317
318 void
319 rump_clear_curlwp()
320 {
321 struct lwp *l;
322
323 l = rumpuser_get_curlwp();
324 if (l->l_proc->p_pid != 0) {
325 fd_free();
326 rump_proc_vfs_release(l->l_proc);
327 rump_cred_destroy(l->l_cred);
328 kmem_free(l->l_proc, sizeof(*l->l_proc));
329 }
330 kmem_free(l, sizeof(*l));
331 rumpuser_set_curlwp(NULL);
332 }
333
334 struct lwp *
335 rump_get_curlwp()
336 {
337 struct lwp *l;
338
339 l = rumpuser_get_curlwp();
340 if (l == NULL)
341 l = &lwp0;
342
343 return l;
344 }
345
346 int
347 rump_splfoo()
348 {
349
350 if (rumpuser_whatis_ipl() != RUMPUSER_IPL_INTR) {
351 rumpuser_rw_enter(&rumpspl, 0);
352 rumpuser_set_ipl(RUMPUSER_IPL_SPLFOO);
353 }
354
355 return 0;
356 }
357
358 void
359 rump_intr_enter(void)
360 {
361
362 rumpuser_set_ipl(RUMPUSER_IPL_INTR);
363 rumpuser_rw_enter(&rumpspl, 1);
364 }
365
366 void
367 rump_intr_exit(void)
368 {
369
370 rumpuser_rw_exit(&rumpspl);
371 rumpuser_clear_ipl(RUMPUSER_IPL_INTR);
372 }
373
374 void
375 rump_splx(int dummy)
376 {
377
378 if (rumpuser_whatis_ipl() != RUMPUSER_IPL_INTR) {
379 rumpuser_clear_ipl(RUMPUSER_IPL_SPLFOO);
380 rumpuser_rw_exit(&rumpspl);
381 }
382 }
383
384 kauth_cred_t
385 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
386 {
387 kauth_cred_t cred;
388 int rv;
389
390 cred = kauth_cred_alloc();
391 kauth_cred_setuid(cred, uid);
392 kauth_cred_seteuid(cred, uid);
393 kauth_cred_setsvuid(cred, uid);
394 kauth_cred_setgid(cred, gid);
395 kauth_cred_setgid(cred, gid);
396 kauth_cred_setegid(cred, gid);
397 kauth_cred_setsvgid(cred, gid);
398 rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
399 /* oh this is silly. and by "this" I mean kauth_cred_setgroups() */
400 assert(rv == 0);
401
402 return cred;
403 }
404
405 void
406 rump_cred_destroy(kauth_cred_t cred)
407 {
408
409 kauth_cred_free(cred);
410 }
411
412 kauth_cred_t
413 rump_cred_suserget()
414 {
415
416 kauth_cred_hold(rump_susercred);
417 return rump_susercred;
418 }
419
420 /* XXX: if they overflow, we're screwed */
421 lwpid_t
422 rump_nextlid()
423 {
424 static unsigned lwpid = 2;
425
426 do {
427 lwpid = atomic_inc_uint_nv(&lwpid);
428 } while (lwpid == 0);
429
430 return (lwpid_t)lwpid;
431 }
432
433 int
434 rump_module_load(struct modinfo **mi)
435 {
436
437 if (!module_compatible((*mi)->mi_version, __NetBSD_Version__))
438 return EPROGMISMATCH;
439
440 return (*mi)->mi_modcmd(MODULE_CMD_INIT, NULL);
441 }
442
443 int _syspuffs_stub(int, int *);
444 int
445 _syspuffs_stub(int fd, int *newfd)
446 {
447
448 return ENODEV;
449 }
450 __weak_alias(rump_syspuffs_glueinit,_syspuffs_stub);
451