Skip to content

Instantly share code, notes, and snippets.

@mopp
Last active August 29, 2015 14:10
Show Gist options
  • Save mopp/cee2d321eb16f221ebd5 to your computer and use it in GitHub Desktop.
Save mopp/cee2d321eb16f221ebd5 to your computer and use it in GitHub Desktop.
context switch core
void __fastcall change_context(Process* current, Process* next) {
(void)current;
run_proc= next;
}
void switch_context(Interrupt_frame* current_iframe) {
Process* current_p = running_proc();
Process* next_p = next_proc();
if (current_p == next_p) {
return;
}
Thread* current_t = &current_p->thread;
Thread* next_t = &next_p->thread;
current_t->iframe = current_iframe;
if (next_p->pdt != NULL && pdt_process != next_p) {
/*
* Next Memory space is user space.
* So, change pdt.
*/
set_cpu_pdt(get_page_phys_addr(&next_p->pdt_page));
pdt_process = next_p;
axel_s.tss->esp0 = ECAST_UINT32(next_p->km_stack);
}
__asm__ volatile(
"pushfl \n" // Store eflags
"movl $next_turn, %[current_ip] \n" // Store ip
"movl %%esp, %[current_sp] \n" // Store sp
"movl %[current_proc], %%ecx \n" // Set second argument
"movl %[next_proc], %%edx \n" // Set first argument
"movl %[next_sp], %%esp \n" // Restore sp
"pushl %[next_ip] \n" // Restore ip (set return address)
"jmp change_context \n" // Change context
".globl next_turn \n"
"next_turn: \n"
"popfl \n" // Restore eflags
: [current_ip] "=m"(current_t->ip),
[current_sp] "=m"(current_t->sp)
: [next_ip] "r"(next_t->ip),
[next_sp] "r"(next_t->sp),
[current_proc] "m"(current_p),
[next_proc] "m"(next_p)
: "memory", "%ecx", "%edx"
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment