#include <mdb/mdb_context_impl.h>
#include <mdb/mdb_modapi.h>
#include <mdb/mdb_debug.h>
#include <mdb/mdb_err.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <ucontext.h>
#include <unistd.h>
#include <setjmp.h>
#include <fcntl.h>
#include <errno.h>
static void
context_init(mdb_context_t *volatile c)
{
c->ctx_status = c->ctx_func();
ASSERT(c->ctx_resumes > 0);
longjmp(c->ctx_pcb, 1);
}
mdb_context_t *
mdb_context_create(int (*func)(void))
{
mdb_context_t *c = mdb_zalloc(sizeof (mdb_context_t), UM_NOSLEEP);
size_t pagesize = sysconf(_SC_PAGESIZE);
int prot = sysconf(_SC_STACK_PROT);
static int zfd = -1;
if (c == NULL)
return (NULL);
if (prot == -1)
prot = PROT_READ | PROT_WRITE | PROT_EXEC;
c->ctx_func = func;
c->ctx_stacksize = pagesize * 4;
c->ctx_stack = mmap(NULL, c->ctx_stacksize, prot,
MAP_PRIVATE | MAP_ANON, -1, 0);
if (c->ctx_stack == MAP_FAILED && errno == EBADF) {
if (zfd == -1 && (zfd = open("/dev/zero", O_RDWR)) >= 0)
(void) fcntl(zfd, F_SETFD, FD_CLOEXEC);
if (zfd >= 0) {
c->ctx_stack = mmap(NULL, c->ctx_stacksize, prot,
MAP_PRIVATE, zfd, 0);
}
}
c->ctx_uc.uc_flags = UC_ALL;
if (c->ctx_stack == MAP_FAILED || getcontext(&c->ctx_uc) != 0) {
mdb_free(c, sizeof (mdb_context_t));
return (NULL);
}
c->ctx_uc.uc_stack.ss_sp = c->ctx_stack;
c->ctx_uc.uc_stack.ss_size = c->ctx_stacksize;
c->ctx_uc.uc_stack.ss_flags = 0;
c->ctx_uc.uc_link = NULL;
makecontext(&c->ctx_uc, context_init, 1, c);
return (c);
}
void
mdb_context_destroy(mdb_context_t *c)
{
if (munmap(c->ctx_stack, c->ctx_stacksize) == -1)
fail("failed to unmap stack %p", c->ctx_stack);
mdb_free(c, sizeof (mdb_context_t));
}
void
mdb_context_switch(mdb_context_t *c)
{
if (setjmp(c->ctx_pcb) == 0 && setcontext(&c->ctx_uc) == -1)
fail("failed to change context to %p", (void *)c);
else
fail("unexpectedly returned from context %p", (void *)c);
}
jmp_buf *
mdb_context_getpcb(mdb_context_t *c)
{
c->ctx_resumes++;
return (&c->ctx_pcb);
}