8 #include <threads_int.h>
15 #include <arch_config.h>
19 #define DEBUG_TRACE_SWITCH 0
20 #define BREAK_ON_SWITCH 0 // Break into bochs debugger on a task switch
28 Uint8 State; // 0: Unavaliable, 1: Idle, 2: Active
36 extern void APStartup(void); // 16-bit AP startup code
38 extern Uint GetRIP(void); // start.asm
39 extern Uint SaveState(Uint *RSP, Uint *Regs);
40 extern Uint Proc_CloneInt(Uint *RSP, Uint *CR3);
41 extern void NewTaskHeader(void); // Actually takes cdecl args
42 extern void Proc_InitialiseSSE(void);
43 extern void Proc_SaveSSE(Uint DestPtr);
44 extern void Proc_DisableSSE(void);
46 extern Uint64 gInitialPML4[512]; // start.asm
49 extern int giTotalTickets;
50 extern int giNumActiveThreads;
51 extern tThread gThreadZero;
52 extern tProcess gProcessZero;
53 extern void Threads_Dump(void);
54 extern void Proc_ReturnToUser(tVAddr Handler, tVAddr KStackTop, int Argument);
55 extern void Time_UpdateTimestamp(void);
56 extern void SwitchTasks(Uint NewSP, Uint *OldSP, Uint NewIP, Uint *OldIO, Uint CR3);
59 //void ArchThreads_Init(void);
61 void MP_StartAP(int CPU);
62 void MP_SendIPI(Uint8 APICID, int Vector, int DeliveryMode);
64 void Proc_IdleTask(void *unused);
65 //void Proc_Start(void);
66 //tThread *Proc_GetCurThread(void);
67 // int Proc_NewKThread(void (*Fcn)(void*), void *Data);
68 // int Proc_Clone(Uint *Err, Uint Flags);
69 // int Proc_SpawnWorker(void);
70 Uint Proc_MakeUserStack(void);
71 //void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize);
72 void Proc_StartProcess(Uint16 SS, Uint Stack, Uint Flags, Uint16 CS, Uint IP) NORETURN;
73 int Proc_Demote(Uint *Err, int Dest, tRegs *Regs);
74 //void Proc_CallFaultHandler(tThread *Thread);
75 //void Proc_DumpThreadCPUState(tThread *Thread);
76 //void Proc_Reschedule(void);
77 void Proc_Scheduler(int CPU, Uint RSP, Uint RIP);
80 //!\brief Used by desctab.asm in SyscallStub
81 const int ci_offsetof_tThread_KernelStack = offsetof(tThread, KernelStack);
82 // --- Multiprocessing ---
84 volatile int giNumInitingCPUs = 0;
85 tMPInfo *gMPFloatPtr = NULL;
86 tAPIC *gpMP_LocalAPIC = NULL;
87 Uint8 gaAPIC_to_CPU[256] = {0};
89 tCPU gaCPUs[MAX_CPUS];
92 // --- Error Recovery ---
93 Uint32 gaDoubleFaultStack[1024];
97 * \fn void ArchThreads_Init(void)
98 * \brief Starts the process scheduler
100 void ArchThreads_Init(void)
107 // Mark BSP as active
110 // -- Initialise Multiprocessing
111 // Find MP Floating Table
112 // - EBDA/Last 1Kib (640KiB)
113 for(pos = KERNEL_BASE|0x9F000; pos < (KERNEL_BASE|0xA0000); pos += 16) {
114 if( *(Uint*)(pos) == MPPTR_IDENT ) {
115 Log("Possible %p", pos);
116 if( ByteSum((void*)pos, sizeof(tMPInfo)) != 0 ) continue;
117 gMPFloatPtr = (void*)pos;
121 // - Last KiB (512KiB base mem)
123 for(pos = KERNEL_BASE|0x7F000; pos < (KERNEL_BASE|0x80000); pos += 16) {
124 if( *(Uint*)(pos) == MPPTR_IDENT ) {
125 Log("Possible %p", pos);
126 if( ByteSum((void*)pos, sizeof(tMPInfo)) != 0 ) continue;
127 gMPFloatPtr = (void*)pos;
134 for(pos = KERNEL_BASE|0xE0000; pos < (KERNEL_BASE|0x100000); pos += 16) {
135 if( *(Uint*)(pos) == MPPTR_IDENT ) {
136 Log("Possible %p", pos);
137 if( ByteSum((void*)pos, sizeof(tMPInfo)) != 0 ) continue;
138 gMPFloatPtr = (void*)pos;
144 // If the MP Table Exists, parse it
149 Log("gMPFloatPtr = %p", gMPFloatPtr);
150 Log("*gMPFloatPtr = {");
151 Log("\t.Sig = 0x%08x", gMPFloatPtr->Sig);
152 Log("\t.MPConfig = 0x%08x", gMPFloatPtr->MPConfig);
153 Log("\t.Length = 0x%02x", gMPFloatPtr->Length);
154 Log("\t.Version = 0x%02x", gMPFloatPtr->Version);
155 Log("\t.Checksum = 0x%02x", gMPFloatPtr->Checksum);
156 Log("\t.Features = [0x%02x,0x%02x,0x%02x,0x%02x,0x%02x]",
157 gMPFloatPtr->Features[0], gMPFloatPtr->Features[1],
158 gMPFloatPtr->Features[2], gMPFloatPtr->Features[3],
159 gMPFloatPtr->Features[4]
163 mptable = (void*)( KERNEL_BASE|gMPFloatPtr->MPConfig );
164 Log("mptable = %p", mptable);
166 Log("\t.Sig = 0x%08x", mptable->Sig);
167 Log("\t.BaseTableLength = 0x%04x", mptable->BaseTableLength);
168 Log("\t.SpecRev = 0x%02x", mptable->SpecRev);
169 Log("\t.Checksum = 0x%02x", mptable->Checksum);
170 Log("\t.OEMID = '%8c'", mptable->OemID);
171 Log("\t.ProductID = '%8c'", mptable->ProductID);
172 Log("\t.OEMTablePtr = %p'", mptable->OEMTablePtr);
173 Log("\t.OEMTableSize = 0x%04x", mptable->OEMTableSize);
174 Log("\t.EntryCount = 0x%04x", mptable->EntryCount);
175 Log("\t.LocalAPICMemMap = 0x%08x", mptable->LocalAPICMemMap);
176 Log("\t.ExtendedTableLen = 0x%04x", mptable->ExtendedTableLen);
177 Log("\t.ExtendedTableChecksum = 0x%02x", mptable->ExtendedTableChecksum);
180 gpMP_LocalAPIC = (void*)MM_MapHWPage(mptable->LocalAPICMemMap, 1);
182 ents = mptable->Entries;
185 for( i = 0; i < mptable->EntryCount; i ++ )
192 Log("%i: Processor", i);
193 Log("\t.APICID = %i", ents->Proc.APICID);
194 Log("\t.APICVer = 0x%02x", ents->Proc.APICVer);
195 Log("\t.CPUFlags = 0x%02x", ents->Proc.CPUFlags);
196 Log("\t.CPUSignature = 0x%08x", ents->Proc.CPUSignature);
197 Log("\t.FeatureFlags = 0x%08x", ents->Proc.FeatureFlags);
200 if( !(ents->Proc.CPUFlags & 1) ) {
205 // Check if there is too many processors
206 if(giNumCPUs >= MAX_CPUS) {
207 giNumCPUs ++; // If `giNumCPUs` > MAX_CPUS later, it will be clipped
211 // Initialise CPU Info
212 gaAPIC_to_CPU[ents->Proc.APICID] = giNumCPUs;
213 gaCPUs[giNumCPUs].APICID = ents->Proc.APICID;
214 gaCPUs[giNumCPUs].State = 0;
218 if( !(ents->Proc.CPUFlags & 2) )
220 MP_StartAP( giNumCPUs-1 );
227 Log("\t.ID = %i", ents->Bus.ID);
228 Log("\t.TypeString = '%6c'", ents->Bus.TypeString);
232 Log("%i: I/O APIC", i);
233 Log("\t.ID = %i", ents->IOAPIC.ID);
234 Log("\t.Version = 0x%02x", ents->IOAPIC.Version);
235 Log("\t.Flags = 0x%02x", ents->IOAPIC.Flags);
236 Log("\t.Addr = 0x%08x", ents->IOAPIC.Addr);
238 case 3: // I/O Interrupt Assignment
240 Log("%i: I/O Interrupt Assignment", i);
241 Log("\t.IntType = %i", ents->IOInt.IntType);
242 Log("\t.Flags = 0x%04x", ents->IOInt.Flags);
243 Log("\t.SourceBusID = 0x%02x", ents->IOInt.SourceBusID);
244 Log("\t.SourceBusIRQ = 0x%02x", ents->IOInt.SourceBusIRQ);
245 Log("\t.DestAPICID = 0x%02x", ents->IOInt.DestAPICID);
246 Log("\t.DestAPICIRQ = 0x%02x", ents->IOInt.DestAPICIRQ);
248 case 4: // Local Interrupt Assignment
250 Log("%i: Local Interrupt Assignment", i);
251 Log("\t.IntType = %i", ents->LocalInt.IntType);
252 Log("\t.Flags = 0x%04x", ents->LocalInt.Flags);
253 Log("\t.SourceBusID = 0x%02x", ents->LocalInt.SourceBusID);
254 Log("\t.SourceBusIRQ = 0x%02x", ents->LocalInt.SourceBusIRQ);
255 Log("\t.DestLocalAPICID = 0x%02x", ents->LocalInt.DestLocalAPICID);
256 Log("\t.DestLocalAPICIRQ = 0x%02x", ents->LocalInt.DestLocalAPICIRQ);
259 Log("%i: Unknown (%i)", i, ents->Type);
262 ents = (void*)( (Uint)ents + entSize );
265 if( giNumCPUs > MAX_CPUS ) {
266 Warning("Too many CPUs detected (%i), only using %i of them", giNumCPUs, MAX_CPUS);
267 giNumCPUs = MAX_CPUS;
270 while( giNumInitingCPUs )
271 MM_FinishVirtualInit();
273 Panic("Uh oh... MP Table Parsing is unimplemented\n");
276 Log("No MP Table was found, assuming uniprocessor\n");
283 MM_FinishVirtualInit();
287 // Initialise Normal TSS(s)
288 for(pos=0;pos<giNumCPUs;pos++)
293 gTSSs[pos].RSP0 = 0; // Set properly by scheduler
294 gGDT[7+pos*2].LimitLow = sizeof(tTSS) & 0xFFFF;
295 gGDT[7+pos*2].BaseLow = ((Uint)(&gTSSs[pos])) & 0xFFFF;
296 gGDT[7+pos*2].BaseMid = ((Uint)(&gTSSs[pos])) >> 16;
297 gGDT[7+pos*2].BaseHi = ((Uint)(&gTSSs[pos])) >> 24;
298 gGDT[7+pos*2+1].DWord[0] = ((Uint)(&gTSSs[pos])) >> 32;
301 for(pos=0;pos<giNumCPUs;pos++) {
302 __asm__ __volatile__ ("ltr %%ax"::"a"(0x38+pos*16));
305 __asm__ __volatile__ ("ltr %%ax"::"a"(0x38));
308 // Set Debug registers
309 __asm__ __volatile__ ("mov %0, %%db0" : : "r"(&gThreadZero));
310 __asm__ __volatile__ ("mov %%rax, %%db1" : : "a"(0));
312 gaCPUs[0].Current = &gThreadZero;
314 gProcessZero.MemState.CR3 = (Uint)gInitialPML4 - KERNEL_BASE;
315 gThreadZero.CurCPU = 0;
316 gThreadZero.KernelStack = 0xFFFFA00000000000 + KERNEL_STACK_SIZE;
318 // Set timer frequency
319 outb(0x43, 0x34); // Set Channel 0, Low/High, Rate Generator
320 outb(0x40, PIT_TIMER_DIVISOR&0xFF); // Low Byte of Divisor
321 outb(0x40, (PIT_TIMER_DIVISOR>>8)&0xFF); // High Byte
323 // Create Per-Process Data Block
324 if( !MM_Allocate(MM_PPD_CFG) )
326 Warning("Oh, hell, Unable to allocate PPD for Thread#0");
329 Proc_InitialiseSSE();
331 Log_Log("Proc", "Multithreading initialised");
335 void MP_StartAP(int CPU)
337 Log("Starting AP %i (APIC %i)", CPU, gaCPUs[CPU].APICID);
338 // Set location of AP startup code and mark for a warm restart
339 *(Uint16*)(KERNEL_BASE|0x467) = (Uint)&APStartup - (KERNEL_BASE|0xFFFF0);
340 *(Uint16*)(KERNEL_BASE|0x469) = 0xFFFF;
341 outb(0x70, 0x0F); outb(0x71, 0x0A); // Warm Reset
342 MP_SendIPI(gaCPUs[CPU].APICID, 0, 5);
346 void MP_SendIPI(Uint8 APICID, int Vector, int DeliveryMode)
348 Uint32 addr = (Uint)gpMP_LocalAPIC + 0x300;
352 val = (Uint)APICID << 24;
353 Log("*%p = 0x%08x", addr+0x10, val);
354 *(Uint32*)(addr+0x10) = val;
356 val = ((DeliveryMode & 7) << 8) | (Vector & 0xFF);
357 Log("*%p = 0x%08x", addr, val);
358 *(Uint32*)addr = val;
365 void Proc_IdleTask(void *ptr)
368 cpu->IdleThread = Proc_GetCurThread();
369 cpu->IdleThread->ThreadName = (char*)"Idle Thread";
370 Threads_SetPriority( cpu->IdleThread, -1 ); // Never called randomly
371 cpu->IdleThread->Quantum = 1; // 1 slice quantum
373 HALT(); // Just yeilds
379 * \fn void Proc_Start(void)
380 * \brief Start process scheduler
382 void Proc_Start(void)
390 for( i = 0; i < giNumCPUs; i ++ )
393 if(i) gaCPUs[i].Current = NULL;
395 Proc_NewKThread(Proc_IdleTask, &gaCPUs[i]);
398 gaCPUs[i].IdleThread = Threads_GetThread(tid);
402 if( i != giProc_BootProcessorID ) {
407 // BSP still should run the current task
408 gaCPUs[0].Current = &gThreadZero;
409 __asm__ __volatile__ ("mov %0, %%db0" : : "r"(&gThreadZero));
411 // Start interrupts and wait for APs to come up
412 Log("Waiting for APs to come up\n");
413 __asm__ __volatile__ ("sti");
414 while( giNumInitingCPUs ) __asm__ __volatile__ ("hlt");
416 Proc_NewKThread(Proc_IdleTask, &gaCPUs[0]);
418 // Start Interrupts (and hence scheduler)
419 __asm__ __volatile__("sti");
421 MM_FinishVirtualInit();
422 Log_Log("Proc", "Multithreading started");
426 * \fn tThread *Proc_GetCurThread(void)
427 * \brief Gets the current thread
429 tThread *Proc_GetCurThread(void)
433 __asm__ __volatile__ ("mov %%db0, %0" : "=r"(thread));
434 return ret; // gaCPUs[ GetCPUNum() ].Current;
436 return gaCPUs[ 0 ].Current;
440 void Proc_ClearProcess(tProcess *Process)
442 Log_Warning("Proc", "TODO: Nuke address space etc");
448 void Proc_ClearThread(tThread *Thread)
453 * \brief Create a new kernel thread
455 tTID Proc_NewKThread(void (*Fcn)(void*), void *Data)
458 tThread *newThread, *cur;
460 cur = Proc_GetCurThread();
461 newThread = Threads_CloneTCB(0);
462 if(!newThread) return -1;
465 newThread->KernelStack = MM_NewKStack();
467 if(newThread->KernelStack == 0) {
472 rsp = newThread->KernelStack;
473 *(Uint*)(rsp-=8) = (Uint)Data; // Data (shadowed)
474 *(Uint*)(rsp-=8) = (Uint)Fcn; // Function to call
475 *(Uint*)(rsp-=8) = (Uint)newThread; // Thread ID
477 newThread->SavedState.RSP = rsp;
478 newThread->SavedState.RIP = (Uint)&NewTaskHeader;
479 newThread->SavedState.SSE = NULL;
480 // Log("New (KThread) %p, rsp = %p\n", newThread->SavedState.RIP, newThread->SavedState.RSP);
483 Threads_AddActive(newThread);
485 return newThread->TID;
489 * \fn int Proc_Clone(Uint Flags)
490 * \brief Clone the current process
492 tTID Proc_Clone(Uint Flags)
494 tThread *newThread, *cur = Proc_GetCurThread();
498 if( !(Flags & CLONE_VM) ) {
499 Log_Error("Proc", "Proc_Clone: Don't leave CLONE_VM unset, use Proc_NewKThread instead");
504 newThread = Threads_CloneTCB(Flags);
505 if(!newThread) return -1;
507 // Save core machine state
508 rip = Proc_CloneInt(&newThread->SavedState.RSP, &newThread->Process->MemState.CR3);
509 if(rip == 0) return 0; // Child
510 newThread->KernelStack = cur->KernelStack;
511 newThread->SavedState.RIP = rip;
512 newThread->SavedState.SSE = NULL;
516 Log("New (Clone) %p, rsp = %p, cr3 = %p", rip, newThread->SavedState.RSP, newThread->MemState.CR3);
519 __asm__ __volatile__ ("mov %%cr3, %0" : "=r" (cr3));
520 Log("Current CR3 = 0x%x, PADDR(RSP) = 0x%x", cr3, MM_GetPhysAddr(newThread->SavedState.RSP));
525 // Lock list and add to active
526 Threads_AddActive(newThread);
528 return newThread->TID;
532 * \fn int Proc_SpawnWorker(void)
533 * \brief Spawns a new worker thread
535 int Proc_SpawnWorker(void (*Fcn)(void*), void *Data)
538 Uint stack_contents[3];
540 cur = Proc_GetCurThread();
543 new = Threads_CloneThreadZero();
545 Warning("Proc_SpawnWorker - Out of heap space!\n");
549 // Create the stack contents
550 stack_contents[2] = (Uint)Data;
551 stack_contents[1] = (Uint)Fcn;
552 stack_contents[0] = (Uint)new;
554 // Create a new worker stack (in PID0's address space)
555 // - The stack is built by this code using stack_contents
556 new->KernelStack = MM_NewWorkerStack(stack_contents, sizeof(stack_contents));
558 new->SavedState.RSP = new->KernelStack - sizeof(stack_contents);
559 new->SavedState.RIP = (Uint)&NewTaskHeader;
560 new->SavedState.SSE = NULL;
562 // Log("New (Worker) %p, rsp = %p\n", new->SavedState.RIP, new->SavedState.RSP);
565 new->Status = THREAD_STAT_PREINIT;
566 Threads_AddActive( new );
572 * \brief Creates a new user stack
574 Uint Proc_MakeUserStack(void)
577 Uint base = USER_STACK_TOP - USER_STACK_SZ;
579 // Check Prospective Space
580 for( i = USER_STACK_SZ >> 12; i--; )
582 if( MM_GetPhysAddr( base + (i<<12) ) != 0 )
586 if(i != -1) return 0;
588 // Allocate Stack - Allocate incrementally to clean up MM_Dump output
589 // - Most of the user stack is the zero page
590 for( i = 0; i < (USER_STACK_SZ-USER_STACK_PREALLOC)/0x1000; i++ )
592 MM_AllocateZero( base + (i<<12) );
594 // - but the top USER_STACK_PREALLOC pages are actually allocated
595 for( ; i < USER_STACK_SZ/0x1000; i++ )
597 tPAddr alloc = MM_Allocate( base + (i<<12) );
601 Log_Error("Proc", "Unable to allocate user stack (%i pages requested)", USER_STACK_SZ/0x1000);
603 MM_Deallocate( base + (i<<12) );
608 return base + USER_STACK_SZ;
611 void Proc_StartUser(Uint Entrypoint, Uint Base, int ArgC, const char **ArgV, int DataSize)
615 const char **envp = NULL;
620 stack = (void*)Proc_MakeUserStack();
622 Log_Error("Proc", "Unable to create user stack!");
625 stack -= (DataSize+7)/8;
626 memcpy( stack, ArgV, DataSize );
629 // Adjust Arguments and environment
632 Uint delta = (Uint)stack - (Uint)ArgV;
633 ArgV = (const char**)stack;
634 for( i = 0; ArgV[i]; i++ ) ArgV[i] += delta;
636 for( i = 0; envp[i]; i++ ) envp[i] += delta;
639 // User Mode Segments
641 ss = 0x23; cs = 0x2B;
644 *--stack = (Uint)envp;
645 *--stack = (Uint)ArgV;
646 *--stack = (Uint)ArgC;
649 Proc_StartProcess(ss, (Uint)stack, 0x202, cs, Entrypoint);
652 void Proc_StartProcess(Uint16 SS, Uint Stack, Uint Flags, Uint16 CS, Uint IP)
654 if( !(CS == 0x1B || CS == 0x2B) || SS != 0x23 ) {
655 Log_Error("Proc", "Proc_StartProcess: CS / SS are not valid (%x, %x)",
659 // Log("Proc_StartProcess: (SS=%x, Stack=%p, Flags=%x, CS=%x, IP=%p)", SS, Stack, Flags, CS, IP);
660 // MM_DumpTables(0, USER_MAX);
664 __asm__ __volatile__ (
665 "mov %0, %%rsp;\n\t" // Set stack pointer
666 "mov %2, %%r11;\n\t" // Set RFLAGS
668 : : "r" (Stack), "c" (IP), "r" (Flags)
674 __asm__ __volatile__ (
675 "mov %0, %%rsp;\n\t" // Set stack pointer
676 "mov %2, %%r11;\n\t" // Set RFLAGS
678 : : "r" (Stack), "c" (IP), "r" (Flags)
685 * \fn int Proc_Demote(Uint *Err, int Dest, tRegs *Regs)
686 * \brief Demotes a process to a lower permission level
687 * \param Err Pointer to user's errno
688 * \param Dest New Permission Level
689 * \param Regs Pointer to user's register structure
691 int Proc_Demote(Uint *Err, int Dest, tRegs *Regs)
693 int cpl = Regs->CS & 3;
695 if(Dest > 3 || Dest < 0) {
706 // Change the Segment Registers
707 Regs->CS = (((Dest+1)<<4) | Dest) - 8;
708 Regs->SS = ((Dest+1)<<4) | Dest;
714 * \brief Calls a signal handler in user mode
715 * \note Used for signals
717 void Proc_CallFaultHandler(tThread *Thread)
720 Proc_ReturnToUser(Thread->FaultHandler, Thread->KernelStack, Thread->CurFaultNum);
724 void Proc_DumpThreadCPUState(tThread *Thread)
726 Log(" At %04x:%016llx", Thread->SavedState.UserCS, Thread->SavedState.UserRIP);
729 void Proc_Reschedule(void)
731 tThread *nextthread, *curthread;
732 int cpu = GetCPUNum();
734 // TODO: Wait for it?
735 if(IS_LOCKED(&glThreadListLock)) return;
737 curthread = gaCPUs[cpu].Current;
739 nextthread = Threads_GetNextToRun(cpu, curthread);
741 if(nextthread == curthread) return ;
743 nextthread = gaCPUs[cpu].IdleThread;
747 #if DEBUG_TRACE_SWITCH
748 LogF("\nSwitching to task CR3 = 0x%x, RIP = %p, RSP = %p - %i (%s)\n",
749 nextthread->Process->MemState.CR3,
750 nextthread->SavedState.RIP,
751 nextthread->SavedState.RSP,
753 nextthread->ThreadName
758 gaCPUs[cpu].Current = nextthread;
759 gTSSs[cpu].RSP0 = nextthread->KernelStack-4;
760 __asm__ __volatile__ ("mov %0, %%db0" : : "r" (nextthread));
764 // Save FPU/MMX/XMM/SSE state
765 if( curthread->SavedState.SSE )
767 Proc_SaveSSE( ((Uint)curthread->SavedState.SSE + 0xF) & ~0xF );
768 curthread->SavedState.bSSEModified = 0;
772 nextthread->SavedState.RSP, &curthread->SavedState.RSP,
773 nextthread->SavedState.RIP, &curthread->SavedState.RIP,
774 nextthread->Process->MemState.CR3
781 nextthread->SavedState.RSP, &tmp,
782 nextthread->SavedState.RIP, &tmp,
783 nextthread->Process->MemState.CR3
790 * \fn void Proc_Scheduler(int CPU)
791 * \brief Swap current thread and clears dead threads
793 void Proc_Scheduler(int CPU, Uint RSP, Uint RIP)
798 // If the spinlock is set, let it complete
799 if(IS_LOCKED(&glThreadListLock)) return;
801 // Get current thread
802 thread = gaCPUs[CPU].Current;
807 // Reduce remaining quantum and continue timeslice if non-zero
808 if(thread->Remaining--) return;
809 // Reset quantum for next call
810 thread->Remaining = thread->Quantum;
812 // TODO: Make this more stable somehow
814 regs = (tRegs*)(RSP+(1)*8); // CurThread
815 thread->SavedState.UserCS = regs->CS;
816 thread->SavedState.UserRIP = regs->RIP;
827 EXPORT(Proc_SpawnWorker);