rump.c revision 1.340 1 /* $NetBSD: rump.c,v 1.340 2020/02/10 03:23:29 riastradh 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.340 2020/02/10 03:23:29 riastradh 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/module_hook.h>
56 #include <sys/namei.h>
57 #include <sys/once.h>
58 #include <sys/percpu.h>
59 #include <sys/pipe.h>
60 #include <sys/pool.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/threadpool.h>
69 #include <sys/timetc.h>
70 #include <sys/tty.h>
71 #include <sys/uidinfo.h>
72 #include <sys/vmem.h>
73 #include <sys/xcall.h>
74 #include <sys/cprng.h>
75 #include <sys/rnd.h>
76 #include <sys/ktrace.h>
77 #include <sys/pserialize.h>
78 #include <sys/psref.h>
79
80 #include <rump-sys/kern.h>
81 #include <rump-sys/dev.h>
82 #include <rump-sys/net.h>
83 #include <rump-sys/vfs.h>
84
85 #include <rump/rumpuser.h>
86
87 #include <secmodel/suser/suser.h>
88
89 #include <prop/proplib.h>
90
91 #include <uvm/uvm_extern.h>
92 #include <uvm/uvm_readahead.h>
93
94 char machine[] = MACHINE;
95 char machine_arch[] = MACHINE_ARCH;
96
97 struct proc *initproc;
98
99 struct device rump_rootdev = {
100 .dv_class = DV_VIRTUAL
101 };
102
103 #ifdef RUMP_WITHOUT_THREADS
104 int rump_threads = 0;
105 #else
106 int rump_threads = 1;
107 #endif
108
109 static void rump_component_addlocal(void);
110 static struct lwp *bootlwp;
111
112 /* 16k should be enough for std rump needs */
113 static char rump_msgbuf[16*1024] __aligned(256);
114
115 bool rump_ttycomponent = false;
116
117 pool_cache_t pnbuf_cache;
118
119 static void
120 rump_aiodone_worker(struct work *wk, void *dummy)
121 {
122 struct buf *bp = (struct buf *)wk;
123
124 KASSERT(&bp->b_work == wk);
125 bp->b_iodone(bp);
126 }
127
128 static int rump_inited;
129
130 void (*rump_vfs_drainbufs)(int) = (void *)nullop;
131 int (*rump_vfs_makeonedevnode)(dev_t, const char *,
132 devmajor_t, devminor_t) = (void *)nullop;
133 int (*rump_vfs_makedevnodes)(dev_t, const char *, char,
134 devmajor_t, devminor_t, int) = (void *)nullop;
135 int (*rump_vfs_makesymlink)(const char *, const char *) = (void *)nullop;
136
137 rump_proc_vfs_init_fn rump_proc_vfs_init = (void *)nullop;
138 rump_proc_vfs_release_fn rump_proc_vfs_release = (void *)nullop;
139
140 static void add_linkedin_modules(const struct modinfo *const *, size_t);
141
142 static pid_t rspo_wrap_getpid(void) {
143 return rump_sysproxy_hyp_getpid();
144 }
145 static int rspo_wrap_syscall(int num, void *arg, long *retval) {
146 return rump_sysproxy_hyp_syscall(num, arg, retval);
147 }
148 static int rspo_wrap_rfork(void *priv, int flag, const char *comm) {
149 return rump_sysproxy_hyp_rfork(priv, flag, comm);
150 }
151 static void rspo_wrap_lwpexit(void) {
152 rump_sysproxy_hyp_lwpexit();
153 }
154 static void rspo_wrap_execnotify(const char *comm) {
155 rump_sysproxy_hyp_execnotify(comm);
156 }
157 static const struct rumpuser_hyperup hyp = {
158 .hyp_schedule = rump_schedule,
159 .hyp_unschedule = rump_unschedule,
160 .hyp_backend_unschedule = rump_user_unschedule,
161 .hyp_backend_schedule = rump_user_schedule,
162 .hyp_lwproc_switch = rump_lwproc_switch,
163 .hyp_lwproc_release = rump_lwproc_releaselwp,
164 .hyp_lwproc_newlwp = rump_lwproc_newlwp,
165 .hyp_lwproc_curlwp = rump_lwproc_curlwp,
166
167 .hyp_getpid = rspo_wrap_getpid,
168 .hyp_syscall = rspo_wrap_syscall,
169 .hyp_lwproc_rfork = rspo_wrap_rfork,
170 .hyp_lwpexit = rspo_wrap_lwpexit,
171 .hyp_execnotify = rspo_wrap_execnotify,
172 };
173 struct rump_sysproxy_ops rump_sysproxy_ops = {
174 .rspo_copyin = (void *)enxio,
175 .rspo_copyinstr = (void *)enxio,
176 .rspo_copyout = (void *)enxio,
177 .rspo_copyoutstr = (void *)enxio,
178 .rspo_anonmmap = (void *)enxio,
179 .rspo_raise = (void *)enxio,
180 .rspo_fini = (void *)enxio,
181 .rspo_hyp_getpid = (void *)enxio,
182 .rspo_hyp_syscall = (void *)enxio,
183 .rspo_hyp_rfork = (void *)enxio,
184 .rspo_hyp_lwpexit = (void *)enxio,
185 .rspo_hyp_execnotify = (void *)enxio,
186 };
187
188 int
189 rump_daemonize_begin(void)
190 {
191
192 if (rump_inited)
193 return EALREADY;
194
195 return rumpuser_daemonize_begin();
196 }
197
198 int
199 rump_daemonize_done(int error)
200 {
201
202 return rumpuser_daemonize_done(error);
203 }
204
205 #ifdef RUMP_USE_CTOR
206
207 /* sysctl bootstrap handling */
208 struct sysctl_boot_chain sysctl_boot_chain \
209 = LIST_HEAD_INITIALIZER(sysctl_boot_chain);
210 __link_set_add_text(sysctl_funcs,voidop); /* ensure linkset is non-empty */
211
212 #else /* RUMP_USE_CTOR */
213
214 RUMP_COMPONENT(RUMP_COMPONENT_POSTINIT)
215 {
216 __link_set_decl(rump_components, struct rump_component);
217
218 /*
219 * Trick compiler into generating references so that statically
220 * linked rump kernels are generated with the link set symbols.
221 */
222 asm("" :: "r"(__start_link_set_rump_components));
223 asm("" :: "r"(__stop_link_set_rump_components));
224 }
225
226 #endif /* RUMP_USE_CTOR */
227
228 int
229 rump_init(void)
230 {
231 char buf[256];
232 struct timespec bts;
233 int64_t sec;
234 long nsec;
235 struct lwp *l, *initlwp;
236 int i, numcpu;
237
238 /* not reentrant */
239 if (rump_inited)
240 return 0;
241 else if (rump_inited == -1)
242 panic("rump_init: host process restart required");
243 else
244 rump_inited = 1;
245
246 /* initialize hypervisor */
247 if (rumpuser_init(RUMPUSER_VERSION, &hyp) != 0) {
248 rumpuser_dprintf("rumpuser init failed\n");
249 return EINVAL;
250 }
251
252 /* init minimal lwp/cpu context */
253 rump_lwproc_init();
254 l = &lwp0;
255 l->l_cpu = l->l_target_cpu = &rump_bootcpu;
256 rump_lwproc_curlwp_set(l);
257
258 /* retrieve env vars which affect the early stage of bootstrap */
259 if (rumpuser_getparam("RUMP_THREADS", buf, sizeof(buf)) == 0) {
260 rump_threads = *buf != '0';
261 }
262 if (rumpuser_getparam("RUMP_VERBOSE", buf, sizeof(buf)) == 0) {
263 if (*buf != '0')
264 boothowto = AB_VERBOSE;
265 }
266
267 if (rumpuser_getparam(RUMPUSER_PARAM_NCPU, buf, sizeof(buf)) != 0)
268 panic("mandatory hypervisor configuration (NCPU) missing");
269 numcpu = strtoll(buf, NULL, 10);
270 if (numcpu < 1) {
271 panic("rump kernels are not lightweight enough for \"%d\" CPUs",
272 numcpu);
273 }
274
275 rump_thread_init();
276 rump_cpus_bootstrap(&numcpu);
277
278 rumpuser_clock_gettime(RUMPUSER_CLOCK_RELWALL, &sec, &nsec);
279 bts.tv_sec = sec;
280 bts.tv_nsec = nsec;
281
282 initmsgbuf(rump_msgbuf, sizeof(rump_msgbuf));
283 aprint_verbose("%s%s", copyright, version);
284
285 rump_intr_init(numcpu);
286
287 rump_tsleep_init();
288
289 rumpuser_mutex_init(&rump_giantlock, RUMPUSER_MTX_SPIN);
290 ksyms_init();
291 uvm_init();
292 evcnt_init();
293
294 kcpuset_sysinit();
295 once_init();
296 kernconfig_lock_init();
297 prop_kern_init();
298
299 kmem_init();
300
301 uvm_ra_init();
302 uao_init();
303
304 mutex_obj_init();
305 rw_obj_init();
306 callout_startup();
307
308 kprintf_init();
309 percpu_init();
310 pserialize_init();
311
312 kauth_init();
313
314 secmodel_init();
315 sysctl_init();
316 /*
317 * The above call to sysctl_init() only initializes sysctl nodes
318 * from link sets. Initialize sysctls in case we used ctors.
319 */
320 #ifdef RUMP_USE_CTOR
321 {
322 struct sysctl_setup_chain *ssc;
323
324 while ((ssc = LIST_FIRST(&sysctl_boot_chain)) != NULL) {
325 LIST_REMOVE(ssc, ssc_entries);
326 ssc->ssc_func(NULL);
327 }
328 }
329 #endif /* RUMP_USE_CTOR */
330
331 rnd_init();
332 cprng_init();
333 kern_cprng = cprng_strong_create("kernel", IPL_VM,
334 CPRNG_INIT_ANY|CPRNG_REKEY_ANY);
335
336 rump_hyperentropy_init();
337
338 procinit();
339 proc0_init();
340 uid_init();
341 chgproccnt(0, 1);
342
343 l->l_proc = &proc0;
344 lwp_update_creds(l);
345
346 lwpinit_specificdata();
347 lwp_initspecific(&lwp0);
348
349 /* Must be called after lwpinit_specificdata */
350 psref_init();
351
352 threadpools_init();
353
354 loginit();
355
356 rump_biglock_init();
357
358 rump_scheduler_init(numcpu);
359 /* revert temporary context and schedule a semireal context */
360 rump_lwproc_curlwp_clear(l);
361 initproc = &proc0; /* borrow proc0 before we get initproc started */
362 rump_schedule();
363 bootlwp = curlwp;
364
365 inittimecounter();
366 ntp_init();
367
368 #ifdef KTRACE
369 ktrinit();
370 #endif
371
372 tc_setclock(&bts);
373
374 extern krwlock_t exec_lock;
375 rw_init(&exec_lock);
376
377 /* we are mostly go. do per-cpu subsystem init */
378 for (i = 0; i < numcpu; i++) {
379 struct cpu_info *ci = cpu_lookup(i);
380
381 /* attach non-bootstrap CPUs */
382 if (i > 0) {
383 rump_cpu_attach(ci);
384 ncpu++;
385 }
386 snprintf(ci->ci_cpuname, sizeof ci->ci_cpuname, "cpu%d", i);
387
388 callout_init_cpu(ci);
389 softint_init(ci);
390 xc_init_cpu(ci);
391 pool_cache_cpu_init(ci);
392 selsysinit(ci);
393 percpu_init_cpu(ci);
394
395 TAILQ_INIT(&ci->ci_data.cpu_ld_locks);
396 __cpu_simple_lock_init(&ci->ci_data.cpu_ld_lock);
397
398 aprint_verbose("cpu%d at thinair0: rump virtual cpu\n", i);
399 }
400 ncpuonline = ncpu;
401
402 /* Once all CPUs are detected, initialize the per-CPU cprng_fast. */
403 cprng_fast_init();
404
405 mp_online = true;
406
407 /* CPUs are up. allow kernel threads to run */
408 rump_thread_allow(NULL);
409
410 rnd_init_softint();
411
412 kqueue_init();
413 iostat_init();
414 fd_sys_init();
415 module_init();
416 module_hook_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 strlcpy(initproc->p_comm, "rumplocal", sizeof(initproc->p_comm));
496
497 rump_component_init(RUMP_COMPONENT_POSTINIT);
498
499 /* load syscalls */
500 rump_component_init(RUMP_COMPONENT_SYSCALL);
501
502 /* component inits done */
503 bootlwp = NULL;
504
505 /* open 0/1/2 for init */
506 KASSERT(rump_lwproc_curlwp() == NULL);
507 rump_lwproc_switch(initlwp);
508 rump_consdev_init();
509 rump_lwproc_switch(NULL);
510
511 /* release cpu */
512 rump_unschedule();
513
514 return 0;
515 }
516 /* historic compat */
517 __strong_alias(rump__init,rump_init);
518
519 static int compcounter[RUMP_COMPONENT_MAX];
520 static int compinited[RUMP_COMPONENT_MAX];
521
522 /*
523 * Yea, this is O(n^2), but we're only looking at a handful of components.
524 * Components are always initialized from the thread that called rump_init().
525 */
526 static LIST_HEAD(, rump_component) rchead = LIST_HEAD_INITIALIZER(rchead);
527
528 #ifdef RUMP_USE_CTOR
529 struct modinfo_boot_chain modinfo_boot_chain \
530 = LIST_HEAD_INITIALIZER(modinfo_boot_chain);
531
532 static void
533 rump_component_addlocal(void)
534 {
535 struct modinfo_chain *mc;
536
537 while ((mc = LIST_FIRST(&modinfo_boot_chain)) != NULL) {
538 LIST_REMOVE(mc, mc_entries);
539 module_builtin_add(&mc->mc_info, 1, false);
540 }
541 }
542
543 #else /* RUMP_USE_CTOR */
544
545 static void
546 rump_component_addlocal(void)
547 {
548 __link_set_decl(rump_components, struct rump_component);
549 struct rump_component *const *rc;
550
551 __link_set_foreach(rc, rump_components) {
552 rump_component_load(*rc);
553 }
554 }
555 #endif /* RUMP_USE_CTOR */
556
557 void
558 rump_component_load(const struct rump_component *rc_const)
559 {
560 struct rump_component *rc, *rc_iter;
561
562 /* time for rump component loading and unloading has passed */
563 if (!cold)
564 return;
565
566 /*
567 * XXX: this is ok since the "const" was removed from the
568 * definition of RUMP_COMPONENT().
569 *
570 * However, to preserve the hypercall interface, the const
571 * remains here. This can be fixed in the next hypercall revision.
572 */
573 rc = __UNCONST(rc_const);
574
575 KASSERT(!rump_inited || curlwp == bootlwp);
576
577 LIST_FOREACH(rc_iter, &rchead, rc_entries) {
578 if (rc_iter == rc)
579 return;
580 }
581
582 LIST_INSERT_HEAD(&rchead, rc, rc_entries);
583 KASSERT(rc->rc_type < RUMP_COMPONENT_MAX);
584 compcounter[rc->rc_type]++;
585 }
586
587 void
588 rump_component_unload(struct rump_component *rc)
589 {
590
591 /*
592 * Checking for cold is enough because rump_init() both
593 * flips it and handles component loading.
594 */
595 if (!cold)
596 return;
597
598 LIST_REMOVE(rc, rc_entries);
599 }
600
601 int
602 rump_component_count(enum rump_component_type type)
603 {
604
605 KASSERT(curlwp == bootlwp);
606 KASSERT(type < RUMP_COMPONENT_MAX);
607 return compcounter[type];
608 }
609
610 void
611 rump_component_init(enum rump_component_type type)
612 {
613 const struct rump_component *rc, *rc_safe;
614
615 KASSERT(curlwp == bootlwp);
616 KASSERT(!compinited[type]);
617 LIST_FOREACH_SAFE(rc, &rchead, rc_entries, rc_safe) {
618 if (rc->rc_type == type) {
619 rc->rc_init();
620 LIST_REMOVE(rc, rc_entries);
621 }
622 }
623 compinited[type] = 1;
624 }
625
626 /*
627 * Initialize a module which has already been loaded and linked
628 * with dlopen(). This is fundamentally the same as a builtin module.
629 *
630 * XXX: this interface does not really work in the RUMP_USE_CTOR case,
631 * but I'm not sure it's anything to cry about. In feeling blue,
632 * things could somehow be handled via modinfo_boot_chain.
633 */
634 int
635 rump_module_init(const struct modinfo * const *mip, size_t nmodinfo)
636 {
637
638 return module_builtin_add(mip, nmodinfo, true);
639 }
640
641 /*
642 * Finish module (flawless victory, fatality!).
643 */
644 int
645 rump_module_fini(const struct modinfo *mi)
646 {
647
648 return module_builtin_remove(mi, true);
649 }
650
651 /*
652 * Add loaded and linked module to the builtin list. It will
653 * later be initialized with module_init_class().
654 */
655
656 static void
657 add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo)
658 {
659
660 module_builtin_add(mip, nmodinfo, false);
661 }
662
663 int
664 rump_kernelfsym_load(void *symtab, uint64_t symsize,
665 char *strtab, uint64_t strsize)
666 {
667 static int inited = 0;
668 Elf64_Ehdr ehdr;
669
670 if (inited)
671 return EBUSY;
672 inited = 1;
673
674 /*
675 * Use 64bit header since it's bigger. Shouldn't make a
676 * difference, since we're passing in all zeroes anyway.
677 */
678 memset(&ehdr, 0, sizeof(ehdr));
679 ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize);
680
681 return 0;
682 }
683
684 int
685 rump_boot_gethowto()
686 {
687
688 return boothowto;
689 }
690
691 void
692 rump_boot_sethowto(int howto)
693 {
694
695 boothowto = howto;
696 }
697
698 int
699 rump_getversion(void)
700 {
701
702 return __NetBSD_Version__;
703 }
704 /* compat */
705 __strong_alias(rump_pub_getversion,rump_getversion);
706
707 /*
708 * Note: may be called unscheduled. Not fully safe since no locking
709 * of allevents (currently that's not even available).
710 */
711 void
712 rump_printevcnts()
713 {
714 struct evcnt *ev;
715
716 TAILQ_FOREACH(ev, &allevents, ev_list)
717 rumpuser_dprintf("%s / %s: %" PRIu64 "\n",
718 ev->ev_group, ev->ev_name, ev->ev_count);
719 }
720
721 /*
722 * If you use this interface ... well ... all bets are off.
723 * The original purpose is for the p2k fs server library to be
724 * able to use the same pid/lid for VOPs as the host kernel.
725 */
726 void
727 rump_allbetsareoff_setid(pid_t pid, int lid)
728 {
729 struct lwp *l = curlwp;
730 struct proc *p = l->l_proc;
731
732 l->l_lid = lid;
733 p->p_pid = pid;
734 }
735
736 static void
737 ipiemu(void *a1, void *a2)
738 {
739
740 xc__highpri_intr(NULL);
741 }
742
743 void
744 rump_xc_highpri(struct cpu_info *ci)
745 {
746
747 if (ci)
748 xc_unicast(0, ipiemu, NULL, NULL, ci);
749 else
750 xc_broadcast(0, ipiemu, NULL, NULL);
751 }
752
753 int
754 rump_syscall(int num, void *data, size_t dlen, register_t *retval)
755 {
756 struct proc *p;
757 struct emul *e;
758 struct sysent *callp;
759 const int *etrans = NULL;
760 int rv;
761
762 rump_schedule();
763 p = curproc;
764 e = p->p_emul;
765 #ifndef __HAVE_MINIMAL_EMUL
766 KASSERT(num > 0 && num < e->e_nsysent);
767 #endif
768 callp = e->e_sysent + num;
769
770 rv = sy_invoke(callp, curlwp, data, retval, num);
771
772 /*
773 * I hope that (!__HAVE_MINIMAL_EMUL || __HAVE_SYSCALL_INTERN) is
774 * an invariant ...
775 */
776 #if !defined(__HAVE_MINIMAL_EMUL)
777 etrans = e->e_errno;
778 #elif defined(__HAVE_SYSCALL_INTERN)
779 etrans = p->p_emuldata;
780 #endif
781
782 if (etrans) {
783 rv = etrans[rv];
784 /*
785 * XXX: small hack since Linux etrans vectors on some
786 * archs contain negative errnos, but rump_syscalls
787 * uses the -1 + errno ABI. Note that these
788 * negative values are always the result of translation,
789 * otherwise the above translation method would not
790 * work very well.
791 */
792 if (rv < 0)
793 rv = -rv;
794 }
795 rump_unschedule();
796
797 return rv;
798 }
799
800 void
801 rump_syscall_boot_establish(const struct rump_onesyscall *calls, size_t ncall)
802 {
803 struct sysent *callp;
804 size_t i;
805
806 for (i = 0; i < ncall; i++) {
807 callp = rump_sysent + calls[i].ros_num;
808 KASSERT(bootlwp != NULL
809 && callp->sy_call == (sy_call_t *)(void *)enosys);
810 callp->sy_call = calls[i].ros_handler;
811 }
812 }
813
814 struct rump_boot_etfs *ebstart;
815 void
816 rump_boot_etfs_register(struct rump_boot_etfs *eb)
817 {
818
819 /*
820 * Could use atomics, but, since caller would need to synchronize
821 * against calling rump_init() anyway, easier to just specify the
822 * interface as "caller serializes". This solve-by-specification
823 * approach avoids the grey area of using atomics before rump_init()
824 * runs.
825 */
826 eb->_eb_next = ebstart;
827 eb->eb_status = -1;
828 ebstart = eb;
829 }
830