SwitchTasks:
PUSH_GPR
+ ; Save state RIP and RSP
lea rax, [rel .restore]
- mov QWORD [rcx], rax
+ mov [rcx], rax
mov [rsi], rsp
+ ; Change CR3 if requested
test r8, r8
jz .setState
mov cr3, r8
+
+ ; Make sure the stack is valid before jumping
invlpg [rdi]
invlpg [rdi+0x1000]
+
+ ; Go to new state
.setState:
mov rsp, rdi
jmp rdx
+ ; Restore point for saved state
.restore:
POP_GPR
- xor eax, eax
+ xor eax, eax ; Return zero
ret
#include <hal_proc.h>
// === FLAGS ===
-#define DEBUG_TRACE_SWITCH 1
+#define DEBUG_TRACE_SWITCH 0
#define BREAK_ON_SWITCH 0 // Break into bochs debugger on a task switch
// === CONSTANTS ===
newThread->SavedState.RSP = rsp;
newThread->SavedState.RIP = (Uint)&NewTaskHeader;
- Log("New (KThread) %p, rsp = %p\n", newThread->SavedState.RIP, newThread->SavedState.RSP);
+// Log("New (KThread) %p, rsp = %p\n", newThread->SavedState.RIP, newThread->SavedState.RSP);
// MAGIC_BREAK();
Threads_AddActive(newThread);
newThread->SavedState.RIP = rip;
// DEBUG
- Log("New (Clone) %p, rsp = %p, cr3 = %p", rip, newThread->SavedState.RSP, newThread->MemState.CR3);
+// Log("New (Clone) %p, rsp = %p, cr3 = %p", rip, newThread->SavedState.RSP, newThread->MemState.CR3);
{
Uint cr3;
__asm__ __volatile__ ("mov %%cr3, %0" : "=r" (cr3));
new->SavedState.RSP = new->KernelStack - sizeof(stack_contents);
new->SavedState.RIP = (Uint)&NewTaskHeader;
- Log("New (Worker) %p, rsp = %p\n", new->SavedState.RIP, new->SavedState.RSP);
+// Log("New (Worker) %p, rsp = %p\n", new->SavedState.RIP, new->SavedState.RSP);
// Mark as active
new->Status = THREAD_STAT_PREINIT;