rump.c revision 1.315 1 /* $NetBSD: rump.c,v 1.315 2015/01/07 22:24:04 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2007-2011 Antti Kantee. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: rump.c,v 1.315 2015/01/07 22:24:04 pooka Exp $");
30
31 #include <sys/systm.h>
32 #define ELFSIZE ARCH_ELFSIZE
33
34 #include <sys/param.h>
35 #include <sys/atomic.h>
36 #include <sys/buf.h>
37 #include <sys/callout.h>
38 #include <sys/conf.h>
39 #include <sys/cpu.h>
40 #include <sys/device.h>
41 #include <sys/evcnt.h>
42 #include <sys/event.h>
43 #include <sys/exec_elf.h>
44 #include <sys/filedesc.h>
45 #include <sys/iostat.h>
46 #include <sys/kauth.h>
47 #include <sys/kcpuset.h>
48 #include <sys/kernel.h>
49 #include <sys/kmem.h>
50 #include <sys/kprintf.h>
51 #include <sys/kthread.h>
52 #include <sys/ksyms.h>
53 #include <sys/msgbuf.h>
54 #include <sys/module.h>
55 #include <sys/namei.h>
56 #include <sys/once.h>
57 #include <sys/percpu.h>
58 #include <sys/pipe.h>
59 #include <sys/pool.h>
60 #include <sys/pserialize.h>
61 #include <sys/queue.h>
62 #include <sys/reboot.h>
63 #include <sys/resourcevar.h>
64 #include <sys/select.h>
65 #include <sys/sysctl.h>
66 #include <sys/syscall.h>
67 #include <sys/syscallvar.h>
68 #include <sys/timetc.h>
69 #include <sys/tty.h>
70 #include <sys/uidinfo.h>
71 #include <sys/vmem.h>
72 #include <sys/xcall.h>
73 #include <sys/cprng.h>
74 #include <sys/ktrace.h>
75
76 #include <rump/rumpuser.h>
77
78 #include <secmodel/suser/suser.h>
79
80 #include <prop/proplib.h>
81
82 #include <uvm/uvm_extern.h>
83 #include <uvm/uvm_readahead.h>
84
85 #include "rump_private.h"
86 #include "rump_net_private.h"
87 #include "rump_vfs_private.h"
88 #include "rump_dev_private.h"
89
90 char machine[] = MACHINE;
91
92 struct proc *initproc;
93
94 struct device rump_rootdev = {
95 .dv_class = DV_VIRTUAL
96 };
97
98 #ifdef RUMP_WITHOUT_THREADS
99 int rump_threads = 0;
100 #else
101 int rump_threads = 1;
102 #endif
103
104 static void rump_component_addlocal(void);
105 static struct lwp *bootlwp;
106
107 static char rump_msgbuf[16*1024]; /* 16k should be enough for std rump needs */
108
109 bool rump_ttycomponent = false;
110
111 static void
112 rump_aiodone_worker(struct work *wk, void *dummy)
113 {
114 struct buf *bp = (struct buf *)wk;
115
116 KASSERT(&bp->b_work == wk);
117 bp->b_iodone(bp);
118 }
119
120 static int rump_inited;
121
122 void (*rump_vfs_drainbufs)(int) = (void *)nullop;
123 int (*rump_vfs_makeonedevnode)(dev_t, const char *,
124 devmajor_t, devminor_t) = (void *)nullop;
125 int (*rump_vfs_makedevnodes)(dev_t, const char *, char,
126 devmajor_t, devminor_t, int) = (void *)nullop;
127
128 rump_proc_vfs_init_fn rump_proc_vfs_init = (void *)nullop;
129 rump_proc_vfs_release_fn rump_proc_vfs_release = (void *)nullop;
130
131 static void add_linkedin_modules(const struct modinfo *const *, size_t);
132
133 /*
134 * Create some sysctl nodes. why only this you ask. well, init_sysctl
135 * is a kitchen sink in need of some gardening. but i want to use
136 * others today. Furthermore, creating a whole kitchen sink full of
137 * sysctl nodes is a waste of cycles for rump kernel bootstrap.
138 */
139 static void
140 mksysctls(void)
141 {
142
143 /* hw.pagesize */
144 sysctl_createv(NULL, 0, NULL, NULL,
145 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
146 CTLTYPE_INT, "pagesize",
147 SYSCTL_DESCR("Software page size"),
148 NULL, PAGE_SIZE, NULL, 0,
149 CTL_HW, HW_PAGESIZE, CTL_EOL);
150 }
151
152 static pid_t rspo_wrap_getpid(void) {
153 return rump_sysproxy_hyp_getpid();
154 }
155 static int rspo_wrap_syscall(int num, void *arg, long *retval) {
156 return rump_sysproxy_hyp_syscall(num, arg, retval);
157 }
158 static int rspo_wrap_rfork(void *priv, int flag, const char *comm) {
159 return rump_sysproxy_hyp_rfork(priv, flag, comm);
160 }
161 static void rspo_wrap_lwpexit(void) {
162 rump_sysproxy_hyp_lwpexit();
163 }
164 static void rspo_wrap_execnotify(const char *comm) {
165 rump_sysproxy_hyp_execnotify(comm);
166 }
167 static const struct rumpuser_hyperup hyp = {
168 .hyp_schedule = rump_schedule,
169 .hyp_unschedule = rump_unschedule,
170 .hyp_backend_unschedule = rump_user_unschedule,
171 .hyp_backend_schedule = rump_user_schedule,
172 .hyp_lwproc_switch = rump_lwproc_switch,
173 .hyp_lwproc_release = rump_lwproc_releaselwp,
174 .hyp_lwproc_newlwp = rump_lwproc_newlwp,
175 .hyp_lwproc_curlwp = rump_lwproc_curlwp,
176
177 .hyp_getpid = rspo_wrap_getpid,
178 .hyp_syscall = rspo_wrap_syscall,
179 .hyp_lwproc_rfork = rspo_wrap_rfork,
180 .hyp_lwpexit = rspo_wrap_lwpexit,
181 .hyp_execnotify = rspo_wrap_execnotify,
182 };
183 struct rump_sysproxy_ops rump_sysproxy_ops = {
184 .rspo_copyin = (void *)enxio,
185 .rspo_copyinstr = (void *)enxio,
186 .rspo_copyout = (void *)enxio,
187 .rspo_copyoutstr = (void *)enxio,
188 .rspo_anonmmap = (void *)enxio,
189 .rspo_raise = (void *)enxio,
190 .rspo_fini = (void *)enxio,
191 .rspo_hyp_getpid = (void *)enxio,
192 .rspo_hyp_syscall = (void *)enxio,
193 .rspo_hyp_rfork = (void *)enxio,
194 .rspo_hyp_lwpexit = (void *)enxio,
195 .rspo_hyp_execnotify = (void *)enxio,
196 };
197
198 int
199 rump_daemonize_begin(void)
200 {
201
202 if (rump_inited)
203 return EALREADY;
204
205 return rumpuser_daemonize_begin();
206 }
207
208 int
209 rump_daemonize_done(int error)
210 {
211
212 return rumpuser_daemonize_done(error);
213 }
214
215 #ifdef RUMP_USE_CTOR
216
217 /* sysctl bootstrap handling */
218 struct sysctl_boot_chain sysctl_boot_chain \
219 = LIST_HEAD_INITIALIZER(sysctl_boot_chain);
220 __link_set_add_text(sysctl_funcs,voidop); /* ensure linkset is non-empty */
221
222 #else /* RUMP_USE_CTOR */
223
224 RUMP_COMPONENT(RUMP_COMPONENT_POSTINIT)
225 {
226 __link_set_decl(rump_components, struct rump_component);
227
228 /*
229 * Trick compiler into generating references so that statically
230 * linked rump kernels are generated with the link set symbols.
231 */
232 asm("" :: "r"(__start_link_set_rump_components));
233 asm("" :: "r"(__stop_link_set_rump_components));
234 }
235
236 #endif /* RUMP_USE_CTOR */
237
238 int
239 rump_init(void)
240 {
241 char buf[256];
242 struct timespec ts;
243 int64_t sec;
244 long nsec;
245 struct lwp *l, *initlwp;
246 int i, numcpu;
247
248 /* not reentrant */
249 if (rump_inited)
250 return 0;
251 else if (rump_inited == -1)
252 panic("rump_init: host process restart required");
253 else
254 rump_inited = 1;
255
256 /* initialize hypervisor */
257 if (rumpuser_init(RUMPUSER_VERSION, &hyp) != 0) {
258 rumpuser_dprintf("rumpuser init failed\n");
259 return EINVAL;
260 }
261
262 /* init minimal lwp/cpu context */
263 rump_lwproc_init();
264 l = &lwp0;
265 l->l_cpu = l->l_target_cpu = rump_cpu;
266 rump_lwproc_curlwp_set(l);
267
268 /* retrieve env vars which affect the early stage of bootstrap */
269 if (rumpuser_getparam("RUMP_THREADS", buf, sizeof(buf)) == 0) {
270 rump_threads = *buf != '0';
271 }
272 if (rumpuser_getparam("RUMP_VERBOSE", buf, sizeof(buf)) == 0) {
273 if (*buf != '0')
274 boothowto = AB_VERBOSE;
275 }
276
277 if (rumpuser_getparam(RUMPUSER_PARAM_NCPU, buf, sizeof(buf)) != 0)
278 panic("mandatory hypervisor configuration (NCPU) missing");
279 numcpu = strtoll(buf, NULL, 10);
280 if (numcpu < 1) {
281 panic("rump kernels are not lightweight enough for \"%d\" CPUs",
282 numcpu);
283 }
284
285 rump_thread_init();
286 rump_cpus_bootstrap(&numcpu);
287
288 rumpuser_clock_gettime(RUMPUSER_CLOCK_RELWALL, &sec, &nsec);
289 boottime.tv_sec = sec;
290 boottime.tv_nsec = nsec;
291
292 initmsgbuf(rump_msgbuf, sizeof(rump_msgbuf));
293 aprint_verbose("%s%s", copyright, version);
294
295 rump_intr_init(numcpu);
296
297 rump_tsleep_init();
298
299 rumpuser_mutex_init(&rump_giantlock, RUMPUSER_MTX_SPIN);
300 ksyms_init();
301 uvm_init();
302 evcnt_init();
303
304 kcpuset_sysinit();
305 once_init();
306 kernconfig_lock_init();
307 prop_kern_init();
308
309 kmem_init();
310 kmeminit();
311
312 uvm_ra_init();
313 uao_init();
314
315 mutex_obj_init();
316 callout_startup();
317
318 kprintf_init();
319 pserialize_init();
320 loginit();
321
322 kauth_init();
323
324 secmodel_init();
325 sysctl_init();
326 /*
327 * The above call to sysctl_init() only initializes sysctl nodes
328 * from link sets. Initialize sysctls in case we used ctors.
329 */
330 #ifdef RUMP_USE_CTOR
331 {
332 struct sysctl_setup_chain *ssc;
333
334 LIST_FOREACH(ssc, &sysctl_boot_chain, ssc_entries) {
335 ssc->ssc_func(NULL);
336 }
337 }
338 #endif /* RUMP_USE_CTOR */
339
340 rnd_init();
341 cprng_init();
342 kern_cprng = cprng_strong_create("kernel", IPL_VM,
343 CPRNG_INIT_ANY|CPRNG_REKEY_ANY);
344
345 rump_hyperentropy_init();
346
347 procinit();
348 proc0_init();
349 uid_init();
350 chgproccnt(0, 1);
351
352 l->l_proc = &proc0;
353 lwp_update_creds(l);
354
355 lwpinit_specificdata();
356 lwp_initspecific(&lwp0);
357
358 rump_biglock_init();
359
360 rump_scheduler_init(numcpu);
361 /* revert temporary context and schedule a semireal context */
362 rump_lwproc_curlwp_clear(l);
363 initproc = &proc0; /* borrow proc0 before we get initproc started */
364 rump_schedule();
365 bootlwp = curlwp;
366
367 percpu_init();
368 inittimecounter();
369 ntp_init();
370
371 #ifdef KTRACE
372 ktrinit();
373 #endif
374
375 ts = boottime;
376 tc_setclock(&ts);
377
378 extern krwlock_t exec_lock;
379 rw_init(&exec_lock);
380
381 /* we are mostly go. do per-cpu subsystem init */
382 for (i = 0; i < numcpu; i++) {
383 struct cpu_info *ci = cpu_lookup(i);
384
385 /* attach non-bootstrap CPUs */
386 if (i > 0) {
387 rump_cpu_attach(ci);
388 ncpu++;
389 }
390
391 callout_init_cpu(ci);
392 softint_init(ci);
393 xc_init_cpu(ci);
394 pool_cache_cpu_init(ci);
395 selsysinit(ci);
396 percpu_init_cpu(ci);
397
398 TAILQ_INIT(&ci->ci_data.cpu_ld_locks);
399 __cpu_simple_lock_init(&ci->ci_data.cpu_ld_lock);
400
401 aprint_verbose("cpu%d at thinair0: rump virtual cpu\n", i);
402 }
403
404 /* Once all CPUs are detected, initialize the per-CPU cprng_fast. */
405 cprng_fast_init();
406
407 /* CPUs are up. allow kernel threads to run */
408 rump_thread_allow(NULL);
409
410 rnd_init_softint();
411
412 mksysctls();
413 kqueue_init();
414 iostat_init();
415 fd_sys_init();
416 module_init();
417 devsw_init();
418 pipe_init();
419 resource_init();
420 procinit_sysctl();
421 time_init();
422 time_init2();
423
424 /* start page baroness */
425 if (rump_threads) {
426 if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL,
427 uvm_pageout, NULL, &uvm.pagedaemon_lwp, "pdaemon") != 0)
428 panic("pagedaemon create failed");
429 } else
430 uvm.pagedaemon_lwp = NULL; /* doesn't match curlwp */
431
432 /* process dso's */
433 rumpuser_dl_bootstrap(add_linkedin_modules,
434 rump_kernelfsym_load, rump_component_load);
435
436 rump_component_addlocal();
437 rump_component_init(RUMP_COMPONENT_KERN);
438
439 /* initialize factions, if present */
440 rump_component_init(RUMP__FACTION_VFS);
441 /* pnbuf_cache is used even without vfs */
442 if (rump_component_count(RUMP__FACTION_VFS) == 0) {
443 pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
444 NULL, IPL_NONE, NULL, NULL, NULL);
445 }
446 rump_component_init(RUMP__FACTION_NET);
447 rump_component_init(RUMP__FACTION_DEV);
448 KASSERT(rump_component_count(RUMP__FACTION_VFS) <= 1
449 && rump_component_count(RUMP__FACTION_NET) <= 1
450 && rump_component_count(RUMP__FACTION_DEV) <= 1);
451
452 rump_component_init(RUMP_COMPONENT_KERN_VFS);
453
454 /*
455 * if we initialized the tty component above, the tyttymtx is
456 * now initialized. otherwise, we need to initialize it.
457 */
458 if (!rump_ttycomponent)
459 mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_VM);
460
461 cold = 0;
462
463 /* aieeeedondest */
464 if (rump_threads) {
465 if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
466 rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE))
467 panic("aiodoned");
468 }
469
470 sysctl_finalize();
471
472 module_init_class(MODULE_CLASS_ANY);
473
474 if (rumpuser_getparam(RUMPUSER_PARAM_HOSTNAME,
475 hostname, MAXHOSTNAMELEN) != 0) {
476 panic("mandatory hypervisor configuration (HOSTNAME) missing");
477 }
478 hostnamelen = strlen(hostname);
479
480 sigemptyset(&sigcantmask);
481
482 if (rump_threads)
483 vmem_rehash_start();
484
485 /*
486 * Create init (proc 1), used to attach implicit threads in rump.
487 * (note: must be done after vfsinit to get cwdi)
488 */
489 initlwp = rump__lwproc_alloclwp(NULL);
490 mutex_enter(proc_lock);
491 initproc = proc_find_raw(1);
492 mutex_exit(proc_lock);
493 if (initproc == NULL)
494 panic("where in the world is initproc?");
495
496 rump_component_init(RUMP_COMPONENT_POSTINIT);
497
498 /* load syscalls */
499 rump_component_init(RUMP_COMPONENT_SYSCALL);
500
501 /* component inits done */
502 bootlwp = NULL;
503
504 /* open 0/1/2 for init */
505 KASSERT(rump_lwproc_curlwp() == NULL);
506 rump_lwproc_switch(initlwp);
507 rump_consdev_init();
508 rump_lwproc_switch(NULL);
509
510 /* release cpu */
511 rump_unschedule();
512
513 return 0;
514 }
515 /* historic compat */
516 __strong_alias(rump__init,rump_init);
517
518 static int compcounter[RUMP_COMPONENT_MAX];
519 static int compinited[RUMP_COMPONENT_MAX];
520
521 /*
522 * Yea, this is O(n^2), but we're only looking at a handful of components.
523 * Components are always initialized from the thread that called rump_init().
524 */
525 static LIST_HEAD(, rump_component) rchead = LIST_HEAD_INITIALIZER(rchead);
526
527 #ifdef RUMP_USE_CTOR
528 struct modinfo_boot_chain modinfo_boot_chain \
529 = LIST_HEAD_INITIALIZER(modinfo_boot_chain);
530
531 static void
532 rump_component_addlocal(void)
533 {
534 struct modinfo_chain *mc;
535
536 while ((mc = LIST_FIRST(&modinfo_boot_chain)) != NULL) {
537 LIST_REMOVE(mc, mc_entries);
538 module_builtin_add(&mc->mc_info, 1, false);
539 }
540 }
541
542 #else /* RUMP_USE_CTOR */
543
544 static void
545 rump_component_addlocal(void)
546 {
547 __link_set_decl(rump_components, struct rump_component);
548 struct rump_component *const *rc;
549
550 __link_set_foreach(rc, rump_components) {
551 rump_component_load(*rc);
552 }
553 }
554 #endif /* RUMP_USE_CTOR */
555
556 void
557 rump_component_load(const struct rump_component *rc_const)
558 {
559 struct rump_component *rc, *rc_iter;
560
561 /*
562 * XXX: this is ok since the "const" was removed from the
563 * definition of RUMP_COMPONENT().
564 *
565 * However, to preserve the hypercall interface, the const
566 * remains here. This can be fixed in the next hypercall revision.
567 */
568 rc = __UNCONST(rc_const);
569
570 KASSERT(!rump_inited || curlwp == bootlwp);
571
572 LIST_FOREACH(rc_iter, &rchead, rc_entries) {
573 if (rc_iter == rc)
574 return;
575 }
576
577 LIST_INSERT_HEAD(&rchead, rc, rc_entries);
578 KASSERT(rc->rc_type < RUMP_COMPONENT_MAX);
579 compcounter[rc->rc_type]++;
580 }
581
582 int
583 rump_component_count(enum rump_component_type type)
584 {
585
586 KASSERT(curlwp == bootlwp);
587 KASSERT(type < RUMP_COMPONENT_MAX);
588 return compcounter[type];
589 }
590
591 void
592 rump_component_init(enum rump_component_type type)
593 {
594 const struct rump_component *rc, *rc_safe;
595
596 KASSERT(curlwp == bootlwp);
597 KASSERT(!compinited[type]);
598 LIST_FOREACH_SAFE(rc, &rchead, rc_entries, rc_safe) {
599 if (rc->rc_type == type) {
600 rc->rc_init();
601 LIST_REMOVE(rc, rc_entries);
602 }
603 }
604 compinited[type] = 1;
605 }
606
607 /*
608 * Initialize a module which has already been loaded and linked
609 * with dlopen(). This is fundamentally the same as a builtin module.
610 *
611 * XXX: this interface does not really work in the RUMP_USE_CTOR case,
612 * but I'm not sure it's anything to cry about. In feeling blue,
613 * things could somehow be handled via modinfo_boot_chain.
614 */
615 int
616 rump_module_init(const struct modinfo * const *mip, size_t nmodinfo)
617 {
618
619 return module_builtin_add(mip, nmodinfo, true);
620 }
621
622 /*
623 * Finish module (flawless victory, fatality!).
624 */
625 int
626 rump_module_fini(const struct modinfo *mi)
627 {
628
629 return module_builtin_remove(mi, true);
630 }
631
632 /*
633 * Add loaded and linked module to the builtin list. It will
634 * later be initialized with module_init_class().
635 */
636
637 static void
638 add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo)
639 {
640
641 module_builtin_add(mip, nmodinfo, false);
642 }
643
644 int
645 rump_kernelfsym_load(void *symtab, uint64_t symsize,
646 char *strtab, uint64_t strsize)
647 {
648 static int inited = 0;
649 Elf64_Ehdr ehdr;
650
651 if (inited)
652 return EBUSY;
653 inited = 1;
654
655 /*
656 * Use 64bit header since it's bigger. Shouldn't make a
657 * difference, since we're passing in all zeroes anyway.
658 */
659 memset(&ehdr, 0, sizeof(ehdr));
660 ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize);
661
662 return 0;
663 }
664
665 int
666 rump_boot_gethowto()
667 {
668
669 return boothowto;
670 }
671
672 void
673 rump_boot_sethowto(int howto)
674 {
675
676 boothowto = howto;
677 }
678
679 int
680 rump_getversion(void)
681 {
682
683 return __NetBSD_Version__;
684 }
685 /* compat */
686 __strong_alias(rump_pub_getversion,rump_getversion);
687
688 int
689 rump_nativeabi_p(void)
690 {
691
692 #ifdef _RUMP_NATIVE_ABI
693 return 1;
694 #else
695 return 0;
696 #endif
697 }
698
699 /*
700 * Note: may be called unscheduled. Not fully safe since no locking
701 * of allevents (currently that's not even available).
702 */
703 void
704 rump_printevcnts()
705 {
706 struct evcnt *ev;
707
708 TAILQ_FOREACH(ev, &allevents, ev_list)
709 rumpuser_dprintf("%s / %s: %" PRIu64 "\n",
710 ev->ev_group, ev->ev_name, ev->ev_count);
711 }
712
713 /*
714 * If you use this interface ... well ... all bets are off.
715 * The original purpose is for the p2k fs server library to be
716 * able to use the same pid/lid for VOPs as the host kernel.
717 */
718 void
719 rump_allbetsareoff_setid(pid_t pid, int lid)
720 {
721 struct lwp *l = curlwp;
722 struct proc *p = l->l_proc;
723
724 l->l_lid = lid;
725 p->p_pid = pid;
726 }
727
728 #include <sys/pserialize.h>
729
730 static void
731 ipiemu(void *a1, void *a2)
732 {
733
734 xc__highpri_intr(NULL);
735 pserialize_switchpoint();
736 }
737
738 void
739 rump_xc_highpri(struct cpu_info *ci)
740 {
741
742 if (ci)
743 xc_unicast(0, ipiemu, NULL, NULL, ci);
744 else
745 xc_broadcast(0, ipiemu, NULL, NULL);
746 }
747
748 int
749 rump_syscall(int num, void *data, size_t dlen, register_t *retval)
750 {
751 struct proc *p;
752 struct emul *e;
753 struct sysent *callp;
754 const int *etrans = NULL;
755 int rv;
756
757 rump_schedule();
758 p = curproc;
759 e = p->p_emul;
760 #ifndef __HAVE_MINIMAL_EMUL
761 KASSERT(num > 0 && num < e->e_nsysent);
762 #endif
763 callp = e->e_sysent + num;
764
765 rv = sy_invoke(callp, curlwp, data, retval, num);
766
767 /*
768 * I hope that (!__HAVE_MINIMAL_EMUL || __HAVE_SYSCALL_INTERN) is
769 * an invariant ...
770 */
771 #if !defined(__HAVE_MINIMAL_EMUL)
772 etrans = e->e_errno;
773 #elif defined(__HAVE_SYSCALL_INTERN)
774 etrans = p->p_emuldata;
775 #endif
776
777 if (etrans) {
778 rv = etrans[rv];
779 /*
780 * XXX: small hack since Linux etrans vectors on some
781 * archs contain negative errnos, but rump_syscalls
782 * uses the -1 + errno ABI. Note that these
783 * negative values are always the result of translation,
784 * otherwise the above translation method would not
785 * work very well.
786 */
787 if (rv < 0)
788 rv = -rv;
789 }
790 rump_unschedule();
791
792 return rv;
793 }
794
795 void
796 rump_syscall_boot_establish(const struct rump_onesyscall *calls, size_t ncall)
797 {
798 struct sysent *callp;
799 size_t i;
800
801 for (i = 0; i < ncall; i++) {
802 callp = rump_sysent + calls[i].ros_num;
803 KASSERT(bootlwp != NULL
804 && callp->sy_call == (sy_call_t *)enosys);
805 callp->sy_call = calls[i].ros_handler;
806 }
807 }
808
809 struct rump_boot_etfs *ebstart;
810 void
811 rump_boot_etfs_register(struct rump_boot_etfs *eb)
812 {
813
814 /*
815 * Could use atomics, but, since caller would need to synchronize
816 * against calling rump_init() anyway, easier to just specify the
817 * interface as "caller serializes". This solve-by-specification
818 * approach avoids the grey area of using atomics before rump_init()
819 * runs.
820 */
821 eb->_eb_next = ebstart;
822 eb->eb_status = -1;
823 ebstart = eb;
824 }
825