Lecture 7: Contexts

Contexts, invariants, and process exit

Context

Interruptibility

Privilege

Permanence

Suspension

WeensyOS contexts

Chickadee contexts

Chickadee contexts

Current context

Current context lookup

%gs and current context

// this_cpu
//    Return a pointer to the current CPU. Requires disabled interrupts.
inline cpustate* this_cpu() {
    assert(is_cli());
    cpustate* result;
    asm volatile ("movq %%gs:(0), %0" : "=r" (result));
    return result;
}

// current
//    Return a pointer to the current `struct proc`.
inline proc* current() {
    proc* result;
    asm volatile ("movq %%gs:(8), %0" : "=r" (result));
    return result;
}

struct __attribute__((aligned(4096))) cpustate {
    // These three members must come first: they are referenced in assembly
    cpustate* self_;
    proc* current_ = nullptr;
    uint64_t syscall_scratch_;
    ...   

Context switching

How are context switches performed?

Privileged voluntary context switches 1

Privileged voluntary context switches 2

Privileged voluntary context switches 3

Unprivileged voluntary context switches 1

Unprivileged voluntary context switches 2

cpustate::schedule

Requirements and invariants