/*
* Copyright (c) 2010, 2011 Antti Kantee. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#if defined(__i386__) || defined(__x86_64__)
/*
* This file abuses the pmap abstraction to create its own statically
* allocated struct pmap object, even though it can't do anything
* useful with such a thing from userland. On x86 the struct pmap
* definition is private, so we have to go to extra effort to abuse it
* there. This should be fixed -- all of the struct pmap definitions
* should be private, and then rump can furnish its own fake struct
* pmap without clashing with anything.
*/
#include <machine/pmap_private.h>
#endif
static int
hyp_rfork(void *priv, int flags, const char *comm)
{
struct rump_spctl *spctl;
struct vmspace *vm;
struct proc *p;
struct lwp *l;
int error;
bool initfds;
/*
* If we are forking off of pid 1, initialize file descriptors.
*/
l = curlwp;
if (l->l_proc->p_pid == 1) {
KASSERT(flags == RUMP_RFFD_CLEAR);
initfds = true;
} else {
initfds = false;
}
/*
* Since it's a proxy proc, we create a vmspace for it.
*/
spctl = kmem_zalloc(sizeof(*spctl), KM_SLEEP);
vm = &spctl->spctl_vm;
uvmspace_init(vm, &remotepmap, 0, 0, false);
spctl->spctl = priv;
/*
* We forked in this routine, so cannot use curlwp (const)
*/
l = rump_lwproc_curlwp();
p = l->l_proc;
if (comm)
strlcpy(p->p_comm, comm, sizeof(p->p_comm));
if (initfds)
rump_consdev_init();
return 0;
}
/*
* Order all lwps in a process to exit. does *not* wait for them to drain.
*/
static void
hyp_lwpexit(void)
{
struct proc *p = curproc;
struct lwp *l;
mutex_enter(p->p_lock);
/*
* First pass: mark all lwps in the process with LW_RUMP_QEXIT
* so that they know they should exit.
*/
LIST_FOREACH(l, &p->p_lwps, l_sibling) {
if (l == curlwp)
continue;
l->l_flag |= LW_RUMP_QEXIT;
}
mutex_exit(p->p_lock);
/*
* Next, make sure everyone on all CPUs sees our status
* update. This keeps threads inside cv_wait() and makes
* sure we don't access a stale cv pointer later when
* we wake up the threads.
*/
xc_barrier(0);
/*
* Ok, all lwps are either:
* 1) not in the cv code
* 2) sleeping on l->l_sched.info
* 3) sleeping on p->p_waitcv
*
* Either way, l_sched.info is stable until we set
* PS_RUMP_LWPEXIT in p->p_sflag.
*/
/*
* Notify process that all threads have been drained and exec is complete.
*/
static void
hyp_execnotify(const char *comm)
{
struct proc *p = curproc;