-
- // 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;
-
+
+ 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;
+
+ // 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;
+
+ // TODO: Make this more stable somehow
+ regs = (tRegs*)(rbp+(2+1)*8); // RBP,Ret + CurThread
+ thread->SavedState.UserCS = regs->CS;
+ thread->SavedState.UserRIP = regs->RIP;
+ }
+
+ #if BREAK_ON_SWITCH
+ {
+ tThread *oldthread = thread;
+ #endif
+