refactor: namespace to arch_context_variant

main
Sean McBride 4 years ago
parent 8b199f702e
commit 3aba285862

@ -18,9 +18,9 @@ arch_context_init(struct arch_context *actx, reg_t ip, reg_t sp)
assert(actx != NULL); assert(actx != NULL);
if (ip == 0 && sp == 0) { if (ip == 0 && sp == 0) {
actx->variant = arch_context_unused; actx->variant = arch_context_variant_unused;
} else { } else {
actx->variant = arch_context_fast; actx->variant = arch_context_variant_fast;
} }
actx->regs[ureg_rsp] = sp; actx->regs[ureg_rsp] = sp;
@ -47,8 +47,8 @@ arch_context_restore(mcontext_t *active_context, struct arch_context *sandbox_co
assert(sandbox_context->pc); assert(sandbox_context->pc);
/* Transitioning from Fast -> Running */ /* Transitioning from Fast -> Running */
assert(sandbox_context->variant == arch_context_fast); assert(sandbox_context->variant == arch_context_variant_fast);
sandbox_context->variant = arch_context_running; sandbox_context->variant = arch_context_variant_running;
active_context->sp = sandbox_context->regs[ureg_rsp]; active_context->sp = sandbox_context->regs[ureg_rsp];
active_context->pc = sandbox_context->regs[ureg_rip] + ARCH_SIG_JMP_OFF; active_context->pc = sandbox_context->regs[ureg_rip] + ARCH_SIG_JMP_OFF;
@ -79,13 +79,13 @@ arch_context_switch(struct arch_context *a, struct arch_context *b)
if (b == NULL) b = &worker_thread_base_context; if (b == NULL) b = &worker_thread_base_context;
/* A Transition {Unused, Running} -> Fast */ /* A Transition {Unused, Running} -> Fast */
assert(a->variant == arch_context_unused || a->variant == arch_context_running); assert(a->variant == arch_context_variant_unused || a->variant == arch_context_variant_running);
/* B Transition {Fast, Slow} -> Running */ /* B Transition {Fast, Slow} -> Running */
assert(b->variant == arch_context_fast || b->variant == arch_context_slow); assert(b->variant == arch_context_variant_fast || b->variant == arch_context_variant_slow);
/* Assumption: Fastpath state is well formed */ /* Assumption: Fastpath state is well formed */
if (b->variant == arch_context_fast) { if (b->variant == arch_context_variant_fast) {
assert(b->regs[ureg_rip] != 0); assert(b->regs[ureg_rip] != 0);
assert(b->regs[ureg_rsp] != 0); assert(b->regs[ureg_rsp] != 0);
} }

@ -30,23 +30,23 @@ typedef enum
/* The enum is compared directly in assembly, so maintain integral values! */ /* The enum is compared directly in assembly, so maintain integral values! */
typedef enum typedef enum
{ {
arch_context_unused = 0, /* Has not have saved a context */ arch_context_variant_unused = 0, /* Has not have saved a context */
arch_context_fast = 1, /* Saved a fastpath context */ arch_context_variant_fast = 1, /* Saved a fastpath context */
arch_context_slow = 2, /* Saved a slowpath context */ arch_context_variant_slow = 2, /* Saved a slowpath context */
arch_context_running = 3 /* Context is executing and content is out of date */ arch_context_variant_running = 3 /* Context is executing and content is out of date */
} arch_context_t; } arch_context_variant_t;
static inline char * static inline char *
arch_context_print(arch_context_t context) arch_context_variant_print(arch_context_variant_t context)
{ {
switch (context) { switch (context) {
case arch_context_unused: case arch_context_variant_unused:
return "Unused"; return "Unused";
case arch_context_fast: case arch_context_variant_fast:
return "Fast"; return "Fast";
case arch_context_slow: case arch_context_variant_slow:
return "Slow"; return "Slow";
case arch_context_running: case arch_context_variant_running:
return "Running"; return "Running";
default: default:
panic("Encountered unexpected arch_context variant\n"); panic("Encountered unexpected arch_context variant\n");
@ -55,9 +55,9 @@ arch_context_print(arch_context_t context)
struct arch_context { struct arch_context {
arch_context_t variant; arch_context_variant_t variant;
reg_t regs[ureg_count]; reg_t regs[ureg_count];
mcontext_t mctx; mcontext_t mctx;
}; };
/* /*

@ -48,8 +48,8 @@ arch_mcontext_restore(mcontext_t *active_context, struct arch_context *sandbox_c
assert(sandbox_context != &worker_thread_base_context); assert(sandbox_context != &worker_thread_base_context);
/* Transitioning from Slow -> Running */ /* Transitioning from Slow -> Running */
assert(sandbox_context->variant == arch_context_slow); assert(sandbox_context->variant == arch_context_variant_slow);
sandbox_context->variant = arch_context_running; sandbox_context->variant = arch_context_variant_running;
/* Restore mcontext */ /* Restore mcontext */
memcpy(active_context, &sandbox_context->mctx, sizeof(mcontext_t)); memcpy(active_context, &sandbox_context->mctx, sizeof(mcontext_t));
@ -74,8 +74,9 @@ arch_mcontext_save(struct arch_context *sandbox_context, const mcontext_t *activ
assert(sandbox_context != &worker_thread_base_context); assert(sandbox_context != &worker_thread_base_context);
/* Transitioning from {Unused, Running} -> Slow */ /* Transitioning from {Unused, Running} -> Slow */
assert(sandbox_context->variant == arch_context_unused || sandbox_context->variant == arch_context_running); assert(sandbox_context->variant == arch_context_variant_unused
sandbox_context->variant = arch_context_slow; || sandbox_context->variant == arch_context_variant_running);
sandbox_context->variant = arch_context_variant_slow;
/* Copy mcontext */ /* Copy mcontext */
memcpy(&sandbox_context->mctx, active_context, sizeof(mcontext_t)); memcpy(&sandbox_context->mctx, active_context, sizeof(mcontext_t));

@ -16,9 +16,9 @@ static void __attribute__((noinline)) arch_context_init(struct arch_context *act
assert(actx != NULL); assert(actx != NULL);
if (ip == 0 && sp == 0) { if (ip == 0 && sp == 0) {
actx->variant = arch_context_unused; actx->variant = arch_context_variant_unused;
} else { } else {
actx->variant = arch_context_fast; actx->variant = arch_context_variant_fast;
} }
if (sp) { if (sp) {
@ -64,8 +64,8 @@ arch_context_restore(mcontext_t *active_context, struct arch_context *sandbox_co
assert(sandbox_context->regs[ureg_rip]); assert(sandbox_context->regs[ureg_rip]);
/* Transitioning from Fast -> Running */ /* Transitioning from Fast -> Running */
assert(sandbox_context->variant == arch_context_fast); assert(sandbox_context->variant == arch_context_variant_fast);
sandbox_context->variant = arch_context_running; sandbox_context->variant = arch_context_variant_running;
active_context->gregs[REG_RSP] = sandbox_context->regs[ureg_rsp]; active_context->gregs[REG_RSP] = sandbox_context->regs[ureg_rsp];
active_context->gregs[REG_RIP] = sandbox_context->regs[ureg_rip] + ARCH_SIG_JMP_OFF; active_context->gregs[REG_RIP] = sandbox_context->regs[ureg_rip] + ARCH_SIG_JMP_OFF;
@ -96,13 +96,13 @@ arch_context_switch(struct arch_context *a, struct arch_context *b)
if (b == NULL) b = &worker_thread_base_context; if (b == NULL) b = &worker_thread_base_context;
/* A Transition {Unused, Running} -> Fast */ /* A Transition {Unused, Running} -> Fast */
assert(a->variant == arch_context_unused || a->variant == arch_context_running); assert(a->variant == arch_context_variant_unused || a->variant == arch_context_variant_running);
/* B Transition {Fast, Slow} -> Running */ /* B Transition {Fast, Slow} -> Running */
assert(b->variant == arch_context_fast || b->variant == arch_context_slow); assert(b->variant == arch_context_variant_fast || b->variant == arch_context_variant_slow);
/* Assumption: Fastpath state is well formed */ /* Assumption: Fastpath state is well formed */
if (b->variant == arch_context_fast) { if (b->variant == arch_context_variant_fast) {
assert(b->regs[ureg_rip] != 0); assert(b->regs[ureg_rip] != 0);
assert(b->regs[ureg_rsp] != 0); assert(b->regs[ureg_rsp] != 0);
} }
@ -122,7 +122,7 @@ arch_context_switch(struct arch_context *a, struct arch_context *b)
*/ */
"movq $2f, 8(%%rax)\n\t" /* Write the address of label 2 to context a's IP. */ "movq $2f, 8(%%rax)\n\t" /* Write the address of label 2 to context a's IP. */
"movq %%rsp, (%%rax)\n\t" /* a_registers[0] (stack_pointer) = stack_pointer */ "movq %%rsp, (%%rax)\n\t" /* a_registers[0] (stack_pointer) = stack_pointer */
"movq $1, (%%rcx)\n\t" /* a->variant = arch_context_fast; */ "movq $1, (%%rcx)\n\t" /* a->variant = arch_context_variant_fast; */
/* /*
* Execute a fastpath or slowpath context switch based on context B's variant * Execute a fastpath or slowpath context switch based on context B's variant
@ -130,7 +130,7 @@ arch_context_switch(struct arch_context *a, struct arch_context *b)
* If slow (mcontext-based), jump to label 1 to restore via a signal handler * If slow (mcontext-based), jump to label 1 to restore via a signal handler
* Otherwise, fall through and execute fast path. * Otherwise, fall through and execute fast path.
*/ */
"cmpq $2, (%%rdx)\n\t" /* if (b->variant == arch_context_slow); */ "cmpq $2, (%%rdx)\n\t" /* if (b->variant == arch_context_variant_slow); */
"je 1f\n\t" /* goto 1; restore the existing sandbox using mcontext */ "je 1f\n\t" /* goto 1; restore the existing sandbox using mcontext */
/* /*
@ -147,7 +147,7 @@ arch_context_switch(struct arch_context *a, struct arch_context *b)
/* /*
* Slow Path * Slow Path
* If the variant is arch_context_slow, that means the sandbox was preempted and we need to * If the variant is arch_context_variant_slow, that means the sandbox was preempted and we need to
* fallback to a full mcontext-based context switch. We do this by invoking * fallback to a full mcontext-based context switch. We do this by invoking
* arch_context_restore_preempted, which fires a SIGUSR1 signal. The SIGUSR1 signal handler * arch_context_restore_preempted, which fires a SIGUSR1 signal. The SIGUSR1 signal handler
* executes the mcontext-based context switch. * executes the mcontext-based context switch.
@ -163,7 +163,7 @@ arch_context_switch(struct arch_context *a, struct arch_context *b)
* The sandbox either resumes at label 2 or 3 depending on if an offset of 8 is used. * The sandbox either resumes at label 2 or 3 depending on if an offset of 8 is used.
*/ */
"2:\n\t" "2:\n\t"
"movq $3, (%%rdx)\n\t" /* b->variant = arch_context_running; */ "movq $3, (%%rdx)\n\t" /* b->variant = arch_context_variant_running; */
".align 8\n\t" ".align 8\n\t"
/* This label is used in conjunction with a static offset */ /* This label is used in conjunction with a static offset */

@ -121,7 +121,7 @@ software_interrupt_handle_signals(int signal_type, siginfo_t *signal_info, void
assert(current_sandbox); assert(current_sandbox);
/* Extra checks to verify that preemption properly set context state */ /* Extra checks to verify that preemption properly set context state */
assert(current_sandbox->ctxt.variant == arch_context_slow); assert(current_sandbox->ctxt.variant == arch_context_variant_slow);
software_interrupt_SIGUSR_count++; software_interrupt_SIGUSR_count++;
debuglog("usr1:%d\n", software_interrupt_SIGUSR_count); debuglog("usr1:%d\n", software_interrupt_SIGUSR_count);

@ -55,8 +55,8 @@ worker_thread_switch_to_sandbox(struct sandbox *next_sandbox)
/* Switching from "Base Context" */ /* Switching from "Base Context" */
debuglog("Base Context (%s) > Sandbox %lu (%s)\n", debuglog("Base Context (%s) > Sandbox %lu (%s)\n",
arch_context_print(worker_thread_base_context.variant), arch_context_variant_print(worker_thread_base_context.variant),
next_sandbox->request_arrival_timestamp, arch_context_print(next_context->variant)); next_sandbox->request_arrival_timestamp, arch_context_variant_print(next_context->variant));
current_sandbox_set(next_sandbox); current_sandbox_set(next_sandbox);
@ -97,8 +97,8 @@ worker_thread_switch_to_base_context()
current_sandbox_set(NULL); current_sandbox_set(NULL);
debuglog("Sandbox %lu (%s) > Base Context (%s)\n", current_sandbox->request_arrival_timestamp, debuglog("Sandbox %lu (%s) > Base Context (%s)\n", current_sandbox->request_arrival_timestamp,
arch_context_print(current_sandbox->ctxt.variant), arch_context_variant_print(current_sandbox->ctxt.variant),
arch_context_print(worker_thread_base_context.variant)); arch_context_variant_print(worker_thread_base_context.variant));
arch_context_switch(&current_sandbox->ctxt, &worker_thread_base_context); arch_context_switch(&current_sandbox->ctxt, &worker_thread_base_context);

Loading…
Cancel
Save