diff --git a/Kernel/arch/x86_64/proc.asm b/Kernel/arch/x86_64/proc.asm index 4771e1b963dc0a30dd94eea09673e5f53aba8dd5..90eb3dca0f357a23a1dff0586b2de5fb834b7cca 100644 --- a/Kernel/arch/x86_64/proc.asm +++ b/Kernel/arch/x86_64/proc.asm @@ -98,21 +98,28 @@ SaveState: 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 diff --git a/Kernel/arch/x86_64/proc.c b/Kernel/arch/x86_64/proc.c index 6ed99eead86f18138473133c09fef20631bdf320..2d07e1ac7f0b8e9e8902a8d95cca36b1c7d2b07a 100644 --- a/Kernel/arch/x86_64/proc.c +++ b/Kernel/arch/x86_64/proc.c @@ -16,7 +16,7 @@ #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 === @@ -457,7 +457,7 @@ int Proc_NewKThread(void (*Fcn)(void*), void *Data) 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); @@ -495,7 +495,7 @@ int Proc_Clone(Uint Flags) 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)); @@ -543,7 +543,7 @@ int Proc_SpawnWorker(void (*Fcn)(void*), void *Data) 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;