rump.c revision 1.94 1 /* $NetBSD: rump.c,v 1.94 2009/02/12 14:46:58 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.94 2009/02/12 14:46:58 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 void rump__unavailable_vfs_panic(void);
113 void rump__unavailable_vfs_panic() {panic("vfs component not available");}
114 __weak_alias(vn_open,rump__unavailable_vfs_panic);
115 __weak_alias(vn_rdwr,rump__unavailable_vfs_panic);
116 __weak_alias(vn_close,rump__unavailable_vfs_panic);
117
118 static void
119 pvfsinit_nop(struct proc *p)
120 {
121
122 return;
123 }
124
125 static void
126 pvfsrele_nop(struct proc *p)
127 {
128
129 return;
130 }
131
132 rump_proc_vfs_init_fn rump_proc_vfs_init = pvfsinit_nop;
133 rump_proc_vfs_release_fn rump_proc_vfs_release = pvfsrele_nop;
134
135 int
136 rump__init(int rump_version)
137 {
138 char buf[256];
139 struct proc *p;
140 struct lwp *l;
141 int error;
142
143 /* XXX */
144 if (rump_inited)
145 return 0;
146 rump_inited = 1;
147
148 if (rump_version != RUMP_VERSION) {
149 printf("rump version mismatch, %d vs. %d\n",
150 rump_version, RUMP_VERSION);
151 return EPROGMISMATCH;
152 }
153
154 if (rumpuser_getenv("RUMP_THREADS", buf, sizeof(buf), &error) == 0) {
155 rump_threads = *buf != '0';
156 }
157 rumpuser_thrinit(_kernel_lock, _kernel_unlock, rump_threads);
158
159 mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_NONE);
160 rumpuser_mutex_recursive_init(&rump_giantlock);
161 ksyms_init();
162 rumpvm_init();
163
164 once_init();
165
166 rump_sleepers_init();
167 #ifdef RUMP_USE_REAL_ALLOCATORS
168 pool_subsystem_init();
169 kmem_init();
170 #endif
171 kprintf_init();
172 loginit();
173
174 kauth_init();
175 rump_susercred = rump_cred_create(0, 0, 0, NULL);
176
177 l = &lwp0;
178 p = &proc0;
179 p->p_stats = &rump_stats;
180 p->p_limit = &rump_limits;
181 p->p_pgrp = &rump_pgrp;
182 p->p_pid = 0;
183 p->p_fd = &rump_filedesc0;
184 p->p_vmspace = &rump_vmspace;
185 p->p_emul = &emul_rump;
186 p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
187 l->l_cred = rump_cred_suserget();
188 l->l_proc = p;
189 l->l_lid = 1;
190 LIST_INSERT_HEAD(&allproc, p, p_list);
191 proc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
192
193 rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
194 rump_limits.pl_rlimit[RLIMIT_NOFILE].rlim_cur = RLIM_INFINITY;
195 rump_limits.pl_rlimit[RLIMIT_SBSIZE].rlim_cur = RLIM_INFINITY;
196
197 callout_startup();
198 callout_init_cpu(&rump_cpu);
199
200 iostat_init();
201 uid_init();
202 percpu_init();
203 fd_sys_init();
204 module_init();
205 sysctl_init();
206 softint_init(&rump_cpu);
207 cold = 0;
208 devsw_init();
209
210 /* these do nothing if not present */
211 rump_vfs_init();
212 rump_net_init();
213
214 /* aieeeedondest */
215 if (rump_threads) {
216 if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
217 rump_aiodone_worker, NULL, 0, 0, 0))
218 panic("aiodoned");
219 }
220
221 rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
222 hostnamelen = strlen(hostname);
223
224 sigemptyset(&sigcantmask);
225
226 lwp0.l_fd = proc0.p_fd = fd_init(&rump_filedesc0);
227
228 #ifdef RUMP_USE_REAL_ALLOCATORS
229 if (rump_threads)
230 vmem_rehash_start();
231 #endif
232
233 return 0;
234 }
235
236 struct uio *
237 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
238 {
239 struct uio *uio;
240 enum uio_rw uiorw;
241
242 switch (rw) {
243 case RUMPUIO_READ:
244 uiorw = UIO_READ;
245 break;
246 case RUMPUIO_WRITE:
247 uiorw = UIO_WRITE;
248 break;
249 default:
250 panic("%s: invalid rw %d", __func__, rw);
251 }
252
253 uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
254 uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
255
256 uio->uio_iov->iov_base = buf;
257 uio->uio_iov->iov_len = bufsize;
258
259 uio->uio_iovcnt = 1;
260 uio->uio_offset = offset;
261 uio->uio_resid = bufsize;
262 uio->uio_rw = uiorw;
263 uio->uio_vmspace = UIO_VMSPACE_SYS;
264
265 return uio;
266 }
267
268 size_t
269 rump_uio_getresid(struct uio *uio)
270 {
271
272 return uio->uio_resid;
273 }
274
275 off_t
276 rump_uio_getoff(struct uio *uio)
277 {
278
279 return uio->uio_offset;
280 }
281
282 size_t
283 rump_uio_free(struct uio *uio)
284 {
285 size_t resid;
286
287 resid = uio->uio_resid;
288 kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
289 kmem_free(uio, sizeof(*uio));
290
291 return resid;
292 }
293
294 struct lwp *
295 rump_setup_curlwp(pid_t pid, lwpid_t lid, int set)
296 {
297 struct lwp *l;
298 struct proc *p;
299
300 l = kmem_zalloc(sizeof(struct lwp), KM_SLEEP);
301 if (pid != 0) {
302 p = kmem_zalloc(sizeof(struct proc), KM_SLEEP);
303 rump_proc_vfs_init(p);
304 p->p_stats = &rump_stats;
305 p->p_limit = &rump_limits;
306 p->p_pid = pid;
307 p->p_vmspace = &rump_vmspace;
308 p->p_fd = fd_init(NULL);
309 } else {
310 p = &proc0;
311 }
312
313 l->l_cred = rump_cred_suserget();
314 l->l_proc = p;
315 l->l_lid = lid;
316 l->l_fd = p->p_fd;
317 l->l_mutex = RUMP_LMUTEX_MAGIC;
318 l->l_cpu = &rump_cpu;
319
320 if (set)
321 rumpuser_set_curlwp(l);
322
323 return l;
324 }
325
326 void
327 rump_clear_curlwp()
328 {
329 struct lwp *l;
330
331 l = rumpuser_get_curlwp();
332 if (l->l_proc->p_pid != 0) {
333 fd_free();
334 rump_proc_vfs_release(l->l_proc);
335 rump_cred_destroy(l->l_cred);
336 kmem_free(l->l_proc, sizeof(*l->l_proc));
337 }
338 kmem_free(l, sizeof(*l));
339 rumpuser_set_curlwp(NULL);
340 }
341
342 struct lwp *
343 rump_get_curlwp()
344 {
345 struct lwp *l;
346
347 l = rumpuser_get_curlwp();
348 if (l == NULL)
349 l = &lwp0;
350
351 return l;
352 }
353
354 kauth_cred_t
355 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
356 {
357 kauth_cred_t cred;
358 int rv;
359
360 cred = kauth_cred_alloc();
361 kauth_cred_setuid(cred, uid);
362 kauth_cred_seteuid(cred, uid);
363 kauth_cred_setsvuid(cred, uid);
364 kauth_cred_setgid(cred, gid);
365 kauth_cred_setgid(cred, gid);
366 kauth_cred_setegid(cred, gid);
367 kauth_cred_setsvgid(cred, gid);
368 rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
369 /* oh this is silly. and by "this" I mean kauth_cred_setgroups() */
370 assert(rv == 0);
371
372 return cred;
373 }
374
375 void
376 rump_cred_destroy(kauth_cred_t cred)
377 {
378
379 kauth_cred_free(cred);
380 }
381
382 kauth_cred_t
383 rump_cred_suserget()
384 {
385
386 kauth_cred_hold(rump_susercred);
387 return rump_susercred;
388 }
389
390 /*
391 * Return the next system lwpid
392 */
393 lwpid_t
394 rump_nextlid()
395 {
396 lwpid_t retid;
397
398 mutex_enter(proc0.p_lock);
399 /*
400 * Take next one, don't return 0
401 * XXX: most likely we'll have collisions in case this
402 * wraps around.
403 */
404 if (++proc0.p_nlwpid == 0)
405 ++proc0.p_nlwpid;
406 retid = proc0.p_nlwpid;
407 mutex_exit(proc0.p_lock);
408
409 return retid;
410 }
411
412 int
413 rump_module_load(struct modinfo **mi)
414 {
415
416 if (!module_compatible((*mi)->mi_version, __NetBSD_Version__))
417 return EPROGMISMATCH;
418
419 return (*mi)->mi_modcmd(MODULE_CMD_INIT, NULL);
420 }
421
422 int _syspuffs_stub(int, int *);
423 int
424 _syspuffs_stub(int fd, int *newfd)
425 {
426
427 return ENODEV;
428 }
429 __weak_alias(rump_syspuffs_glueinit,_syspuffs_stub);
430