- #else
- thread = gCurrentThread;
- #endif
-
- // Reduce remaining quantum and continue timeslice if non-zero
- if(thread->Remaining--) return;
- // Reset quantum for next call
- thread->Remaining = thread->Quantum;
-
- // Get machine state
- __asm__ __volatile__ ("mov %%rsp, %0":"=r"(rsp));
- __asm__ __volatile__ ("mov %%rbp, %0":"=r"(rbp));
- rip = GetRIP();
- if(rip == SWITCH_MAGIC) return; // Check if a switch happened
-
- // Save machine state
- thread->SavedState.RSP = rsp;
- thread->SavedState.RBP = rbp;
- thread->SavedState.RIP = rip;
-
- // Get next thread
- thread = Threads_GetNextToRun(CPU);
-
- // Error Check
- if(thread == NULL) {
- Warning("Hmm... Threads_GetNextToRun returned NULL, I don't think this should happen.\n");
- return;
- }
-
- #if DEBUG_TRACE_SWITCH
- Log("Switching to task %i, CR3 = 0x%x, RIP = %p",
- thread->TID,
- thread->MemState.CR3,
- thread->SavedState.RIP
- );
- #endif
-
- // Set current thread
- #if USE_MP
- gaCPUs[CPU].Current = thread;
- #else
- gCurrentThread = thread;
- #endif
-
- // Update Kernel Stack pointer
- gTSSs[CPU].RSP0 = thread->KernelStack-4;
-
- // Set address space
- #if USE_PAE
- # error "Todo: Implement PAE Address space switching"
- #else
- __asm__ __volatile__ ("mov %0, %%cr3"::"a"(thread->MemState.CR3));
- #endif
-
- #if 0
- if(thread->SavedState.RSP > 0xC0000000
- && thread->SavedState.RSP < thread->KernelStack-0x2000) {
- Log_Warning("Proc", "Possible bad ESP %p (PID %i)", thread->SavedState.ESP);
+
+ if( thread )
+ {
+ tRegs *regs;
+ // Reduce remaining quantum and continue timeslice if non-zero
+ if(thread->Remaining--) return;
+ // Reset quantum for next call
+ thread->Remaining = thread->Quantum;
+
+ // TODO: Make this more stable somehow
+ {
+ regs = (tRegs*)(RSP+(1)*8); // CurThread
+ thread->SavedState.UserCS = regs->CS;
+ thread->SavedState.UserRIP = regs->RIP;
+ }