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
43 extern Uint64 gInitialPML4[512]; // start.asm
44 extern tShortSpinlock glThreadListLock;
47 extern int giTotalTickets;
48 extern int giNumActiveThreads;
49 extern tThread gThreadZero;
50 extern void Threads_Dump(void);
51 extern void Proc_ReturnToUser(tVAddr Handler, tVAddr KStackTop, int Argument);
52 extern void Time_UpdateTimestamp(void);
53 extern void SwitchTasks(Uint NewSP, Uint *OldSP, Uint NewIP, Uint *OldIO, Uint CR3);
56 //void ArchThreads_Init(void);
58 void MP_StartAP(int CPU);
59 void MP_SendIPI(Uint8 APICID, int Vector, int DeliveryMode);
61 void Proc_IdleTask(void *unused);
62 //void Proc_Start(void);
63 //tThread *Proc_GetCurThread(void);
64 int Proc_NewKThread(void (*Fcn)(void*), void *Data);
65 // int Proc_Clone(Uint *Err, Uint Flags);
66 // int Proc_SpawnWorker(void);
67 Uint Proc_MakeUserStack(void);
68 //void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize);
69 void Proc_StartProcess(Uint16 SS, Uint Stack, Uint Flags, Uint16 CS, Uint IP) NORETURN;
70 int Proc_Demote(Uint *Err, int Dest, tRegs *Regs);
71 //void Proc_CallFaultHandler(tThread *Thread);
72 //void Proc_DumpThreadCPUState(tThread *Thread);
73 //void Proc_Reschedule(void);
74 void Proc_Scheduler(int CPU, Uint RSP, Uint RIP);
77 //!\brief Used by desctab.asm in SyscallStub
78 const int ci_offsetof_tThread_KernelStack = offsetof(tThread, KernelStack);
79 // --- Multiprocessing ---
81 volatile int giNumInitingCPUs = 0;
82 tMPInfo *gMPFloatPtr = NULL;
83 tAPIC *gpMP_LocalAPIC = NULL;
84 Uint8 gaAPIC_to_CPU[256] = {0};
86 tCPU gaCPUs[MAX_CPUS];
89 // --- Error Recovery ---
90 Uint32 gaDoubleFaultStack[1024];
94 * \fn void ArchThreads_Init(void)
95 * \brief Starts the process scheduler
97 void ArchThreads_Init(void)
104 // Mark BSP as active
107 // -- Initialise Multiprocessing
108 // Find MP Floating Table
109 // - EBDA/Last 1Kib (640KiB)
110 for(pos = KERNEL_BASE|0x9F000; pos < (KERNEL_BASE|0xA0000); pos += 16) {
111 if( *(Uint*)(pos) == MPPTR_IDENT ) {
112 Log("Possible %p", pos);
113 if( ByteSum((void*)pos, sizeof(tMPInfo)) != 0 ) continue;
114 gMPFloatPtr = (void*)pos;
118 // - Last KiB (512KiB base mem)
120 for(pos = KERNEL_BASE|0x7F000; pos < (KERNEL_BASE|0x80000); pos += 16) {
121 if( *(Uint*)(pos) == MPPTR_IDENT ) {
122 Log("Possible %p", pos);
123 if( ByteSum((void*)pos, sizeof(tMPInfo)) != 0 ) continue;
124 gMPFloatPtr = (void*)pos;
131 for(pos = KERNEL_BASE|0xE0000; pos < (KERNEL_BASE|0x100000); pos += 16) {
132 if( *(Uint*)(pos) == MPPTR_IDENT ) {
133 Log("Possible %p", pos);
134 if( ByteSum((void*)pos, sizeof(tMPInfo)) != 0 ) continue;
135 gMPFloatPtr = (void*)pos;
141 // If the MP Table Exists, parse it
146 Log("gMPFloatPtr = %p", gMPFloatPtr);
147 Log("*gMPFloatPtr = {");
148 Log("\t.Sig = 0x%08x", gMPFloatPtr->Sig);
149 Log("\t.MPConfig = 0x%08x", gMPFloatPtr->MPConfig);
150 Log("\t.Length = 0x%02x", gMPFloatPtr->Length);
151 Log("\t.Version = 0x%02x", gMPFloatPtr->Version);
152 Log("\t.Checksum = 0x%02x", gMPFloatPtr->Checksum);
153 Log("\t.Features = [0x%02x,0x%02x,0x%02x,0x%02x,0x%02x]",
154 gMPFloatPtr->Features[0], gMPFloatPtr->Features[1],
155 gMPFloatPtr->Features[2], gMPFloatPtr->Features[3],
156 gMPFloatPtr->Features[4]
160 mptable = (void*)( KERNEL_BASE|gMPFloatPtr->MPConfig );
161 Log("mptable = %p", mptable);
163 Log("\t.Sig = 0x%08x", mptable->Sig);
164 Log("\t.BaseTableLength = 0x%04x", mptable->BaseTableLength);
165 Log("\t.SpecRev = 0x%02x", mptable->SpecRev);
166 Log("\t.Checksum = 0x%02x", mptable->Checksum);
167 Log("\t.OEMID = '%8c'", mptable->OemID);
168 Log("\t.ProductID = '%8c'", mptable->ProductID);
169 Log("\t.OEMTablePtr = %p'", mptable->OEMTablePtr);
170 Log("\t.OEMTableSize = 0x%04x", mptable->OEMTableSize);
171 Log("\t.EntryCount = 0x%04x", mptable->EntryCount);
172 Log("\t.LocalAPICMemMap = 0x%08x", mptable->LocalAPICMemMap);
173 Log("\t.ExtendedTableLen = 0x%04x", mptable->ExtendedTableLen);
174 Log("\t.ExtendedTableChecksum = 0x%02x", mptable->ExtendedTableChecksum);
177 gpMP_LocalAPIC = (void*)MM_MapHWPage(mptable->LocalAPICMemMap, 1);
179 ents = mptable->Entries;
182 for( i = 0; i < mptable->EntryCount; i ++ )
189 Log("%i: Processor", i);
190 Log("\t.APICID = %i", ents->Proc.APICID);
191 Log("\t.APICVer = 0x%02x", ents->Proc.APICVer);
192 Log("\t.CPUFlags = 0x%02x", ents->Proc.CPUFlags);
193 Log("\t.CPUSignature = 0x%08x", ents->Proc.CPUSignature);
194 Log("\t.FeatureFlags = 0x%08x", ents->Proc.FeatureFlags);
197 if( !(ents->Proc.CPUFlags & 1) ) {
202 // Check if there is too many processors
203 if(giNumCPUs >= MAX_CPUS) {
204 giNumCPUs ++; // If `giNumCPUs` > MAX_CPUS later, it will be clipped
208 // Initialise CPU Info
209 gaAPIC_to_CPU[ents->Proc.APICID] = giNumCPUs;
210 gaCPUs[giNumCPUs].APICID = ents->Proc.APICID;
211 gaCPUs[giNumCPUs].State = 0;
215 if( !(ents->Proc.CPUFlags & 2) )
217 MP_StartAP( giNumCPUs-1 );
224 Log("\t.ID = %i", ents->Bus.ID);
225 Log("\t.TypeString = '%6c'", ents->Bus.TypeString);
229 Log("%i: I/O APIC", i);
230 Log("\t.ID = %i", ents->IOAPIC.ID);
231 Log("\t.Version = 0x%02x", ents->IOAPIC.Version);
232 Log("\t.Flags = 0x%02x", ents->IOAPIC.Flags);
233 Log("\t.Addr = 0x%08x", ents->IOAPIC.Addr);
235 case 3: // I/O Interrupt Assignment
237 Log("%i: I/O Interrupt Assignment", i);
238 Log("\t.IntType = %i", ents->IOInt.IntType);
239 Log("\t.Flags = 0x%04x", ents->IOInt.Flags);
240 Log("\t.SourceBusID = 0x%02x", ents->IOInt.SourceBusID);
241 Log("\t.SourceBusIRQ = 0x%02x", ents->IOInt.SourceBusIRQ);
242 Log("\t.DestAPICID = 0x%02x", ents->IOInt.DestAPICID);
243 Log("\t.DestAPICIRQ = 0x%02x", ents->IOInt.DestAPICIRQ);
245 case 4: // Local Interrupt Assignment
247 Log("%i: Local Interrupt Assignment", i);
248 Log("\t.IntType = %i", ents->LocalInt.IntType);
249 Log("\t.Flags = 0x%04x", ents->LocalInt.Flags);
250 Log("\t.SourceBusID = 0x%02x", ents->LocalInt.SourceBusID);
251 Log("\t.SourceBusIRQ = 0x%02x", ents->LocalInt.SourceBusIRQ);
252 Log("\t.DestLocalAPICID = 0x%02x", ents->LocalInt.DestLocalAPICID);
253 Log("\t.DestLocalAPICIRQ = 0x%02x", ents->LocalInt.DestLocalAPICIRQ);
256 Log("%i: Unknown (%i)", i, ents->Type);
259 ents = (void*)( (Uint)ents + entSize );
262 if( giNumCPUs > MAX_CPUS ) {
263 Warning("Too many CPUs detected (%i), only using %i of them", giNumCPUs, MAX_CPUS);
264 giNumCPUs = MAX_CPUS;
267 while( giNumInitingCPUs )
268 MM_FinishVirtualInit();
270 Panic("Uh oh... MP Table Parsing is unimplemented\n");
273 Log("No MP Table was found, assuming uniprocessor\n");
280 MM_FinishVirtualInit();
284 // Initialise Normal TSS(s)
285 for(pos=0;pos<giNumCPUs;pos++)
290 gTSSs[pos].RSP0 = 0; // Set properly by scheduler
291 gGDT[7+pos*2].LimitLow = sizeof(tTSS) & 0xFFFF;
292 gGDT[7+pos*2].BaseLow = ((Uint)(&gTSSs[pos])) & 0xFFFF;
293 gGDT[7+pos*2].BaseMid = ((Uint)(&gTSSs[pos])) >> 16;
294 gGDT[7+pos*2].BaseHi = ((Uint)(&gTSSs[pos])) >> 24;
295 gGDT[7+pos*2+1].DWord[0] = ((Uint)(&gTSSs[pos])) >> 32;
298 for(pos=0;pos<giNumCPUs;pos++) {
299 __asm__ __volatile__ ("ltr %%ax"::"a"(0x38+pos*16));
302 __asm__ __volatile__ ("ltr %%ax"::"a"(0x38));
305 // Set Debug registers
306 __asm__ __volatile__ ("mov %0, %%db0" : : "r"(&gThreadZero));
307 __asm__ __volatile__ ("mov %%rax, %%db1" : : "a"(0));
309 gaCPUs[0].Current = &gThreadZero;
311 gThreadZero.MemState.CR3 = (Uint)gInitialPML4 - KERNEL_BASE;
312 gThreadZero.CurCPU = 0;
313 gThreadZero.KernelStack = 0xFFFFA00000000000 + KERNEL_STACK_SIZE;
315 // Set timer frequency
316 outb(0x43, 0x34); // Set Channel 0, Low/High, Rate Generator
317 outb(0x40, PIT_TIMER_DIVISOR&0xFF); // Low Byte of Divisor
318 outb(0x40, (PIT_TIMER_DIVISOR>>8)&0xFF); // High Byte
320 // Create Per-Process Data Block
321 if( !MM_Allocate(MM_PPD_CFG) )
323 Warning("Oh, hell, Unable to allocate PPD for Thread#0");
326 Log_Log("Proc", "Multithreading initialised");
330 void MP_StartAP(int CPU)
332 Log("Starting AP %i (APIC %i)", CPU, gaCPUs[CPU].APICID);
333 // Set location of AP startup code and mark for a warm restart
334 *(Uint16*)(KERNEL_BASE|0x467) = (Uint)&APStartup - (KERNEL_BASE|0xFFFF0);
335 *(Uint16*)(KERNEL_BASE|0x469) = 0xFFFF;
336 outb(0x70, 0x0F); outb(0x71, 0x0A); // Warm Reset
337 MP_SendIPI(gaCPUs[CPU].APICID, 0, 5);
341 void MP_SendIPI(Uint8 APICID, int Vector, int DeliveryMode)
343 Uint32 addr = (Uint)gpMP_LocalAPIC + 0x300;
347 val = (Uint)APICID << 24;
348 Log("*%p = 0x%08x", addr+0x10, val);
349 *(Uint32*)(addr+0x10) = val;
351 val = ((DeliveryMode & 7) << 8) | (Vector & 0xFF);
352 Log("*%p = 0x%08x", addr, val);
353 *(Uint32*)addr = val;
360 void Proc_IdleTask(void *ptr)
363 cpu->IdleThread = Proc_GetCurThread();
364 cpu->IdleThread->ThreadName = (char*)"Idle Thread";
365 Threads_SetPriority( cpu->IdleThread, -1 ); // Never called randomly
366 cpu->IdleThread->Quantum = 1; // 1 slice quantum
368 HALT(); // Just yeilds
374 * \fn void Proc_Start(void)
375 * \brief Start process scheduler
377 void Proc_Start(void)
385 for( i = 0; i < giNumCPUs; i ++ )
388 if(i) gaCPUs[i].Current = NULL;
390 Proc_NewKThread(Proc_IdleTask, &gaCPUs[i]);
393 gaCPUs[i].IdleThread = Threads_GetThread(tid);
397 if( i != giProc_BootProcessorID ) {
402 // BSP still should run the current task
403 gaCPUs[0].Current = &gThreadZero;
405 // Start interrupts and wait for APs to come up
406 Log("Waiting for APs to come up\n");
407 __asm__ __volatile__ ("sti");
408 while( giNumInitingCPUs ) __asm__ __volatile__ ("hlt");
410 Proc_NewKThread(Proc_IdleTask, &gaCPUs[0]);
412 // Start Interrupts (and hence scheduler)
413 __asm__ __volatile__("sti");
415 MM_FinishVirtualInit();
416 Log_Log("Proc", "Multithreading started");
420 * \fn tThread *Proc_GetCurThread(void)
421 * \brief Gets the current thread
423 tThread *Proc_GetCurThread(void)
426 return gaCPUs[ GetCPUNum() ].Current;
428 return gaCPUs[ 0 ].Current;
432 int Proc_NewKThread(void (*Fcn)(void*), void *Data)
435 tThread *newThread, *cur;
437 cur = Proc_GetCurThread();
438 newThread = Threads_CloneTCB(NULL, 0);
439 if(!newThread) return -1;
442 newThread->MemState.CR3 = cur->MemState.CR3;
445 newThread->KernelStack = MM_NewKStack();
447 if(newThread->KernelStack == 0) {
452 rsp = newThread->KernelStack;
453 *(Uint*)(rsp-=8) = (Uint)Data; // Data (shadowed)
454 *(Uint*)(rsp-=8) = 1; // Number of params
455 *(Uint*)(rsp-=8) = (Uint)Fcn; // Function to call
456 *(Uint*)(rsp-=8) = (Uint)newThread; // Thread ID
458 newThread->SavedState.RSP = rsp;
459 newThread->SavedState.RIP = (Uint)&NewTaskHeader;
460 // Log("New (KThread) %p, rsp = %p\n", newThread->SavedState.RIP, newThread->SavedState.RSP);
463 Threads_AddActive(newThread);
465 return newThread->TID;
469 * \fn int Proc_Clone(Uint Flags)
470 * \brief Clone the current process
472 int Proc_Clone(Uint Flags)
474 tThread *newThread, *cur = Proc_GetCurThread();
478 if( !(Flags & CLONE_VM) ) {
479 Log_Error("Proc", "Proc_Clone: Don't leave CLONE_VM unset, use Proc_NewKThread instead");
484 newThread = Threads_CloneTCB(NULL, Flags);
485 if(!newThread) return -1;
487 // Save core machine state
488 rip = Proc_CloneInt(&newThread->SavedState.RSP, &newThread->MemState.CR3);
489 if(rip == 0) return 0; // Child
490 newThread->KernelStack = cur->KernelStack;
491 newThread->SavedState.RIP = rip;
495 Log("New (Clone) %p, rsp = %p, cr3 = %p", rip, newThread->SavedState.RSP, newThread->MemState.CR3);
498 __asm__ __volatile__ ("mov %%cr3, %0" : "=r" (cr3));
499 Log("Current CR3 = 0x%x, PADDR(RSP) = 0x%x", cr3, MM_GetPhysAddr(newThread->SavedState.RSP));
504 // Lock list and add to active
505 Threads_AddActive(newThread);
507 return newThread->TID;
511 * \fn int Proc_SpawnWorker(void)
512 * \brief Spawns a new worker thread
514 int Proc_SpawnWorker(void (*Fcn)(void*), void *Data)
517 Uint stack_contents[4];
519 cur = Proc_GetCurThread();
522 new = malloc( sizeof(tThread) );
524 Warning("Proc_SpawnWorker - Out of heap space!\n");
527 memcpy(new, &gThreadZero, sizeof(tThread));
529 new->TID = giNextTID++;
531 // Create the stack contents
532 stack_contents[3] = (Uint)Data;
533 stack_contents[2] = 1;
534 stack_contents[1] = (Uint)Fcn;
535 stack_contents[0] = (Uint)new;
537 // Create a new worker stack (in PID0's address space)
538 // The stack is relocated by this code
539 new->KernelStack = MM_NewWorkerStack(stack_contents, sizeof(stack_contents));
541 new->SavedState.RSP = new->KernelStack - sizeof(stack_contents);
542 new->SavedState.RIP = (Uint)&NewTaskHeader;
544 // Log("New (Worker) %p, rsp = %p\n", new->SavedState.RIP, new->SavedState.RSP);
547 new->Status = THREAD_STAT_PREINIT;
548 Threads_AddActive( new );
554 * \brief Creates a new user stack
556 Uint Proc_MakeUserStack(void)
559 Uint base = USER_STACK_TOP - USER_STACK_SZ;
561 // Check Prospective Space
562 for( i = USER_STACK_SZ >> 12; i--; )
564 if( MM_GetPhysAddr( base + (i<<12) ) != 0 )
568 if(i != -1) return 0;
570 // Allocate Stack - Allocate incrementally to clean up MM_Dump output
571 for( i = 0; i < (USER_STACK_SZ-USER_STACK_PREALLOC)/0x1000; i++ )
573 MM_AllocateZero( base + (i<<12) );
575 for( ; i < USER_STACK_SZ/0x1000; i++ )
577 tPAddr alloc = MM_Allocate( base + (i<<12) );
581 Log_Error("Proc", "Unable to allocate user stack (%i pages requested)", USER_STACK_SZ/0x1000);
583 MM_Deallocate( base + (i<<12) );
588 return base + USER_STACK_SZ;
593 * \fn void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize)
594 * \brief Starts a user task
596 void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize)
598 Uint *stack = (void*)Proc_MakeUserStack();
603 LOG("stack = 0x%x", stack);
606 stack = (void*)( (Uint)stack - DataSize );
607 memcpy( stack, ArgV, DataSize );
609 // Adjust Arguments and environment
610 delta = (Uint)stack - (Uint)ArgV;
611 ArgV = (char**)stack;
612 for( i = 0; ArgV[i]; i++ ) ArgV[i] += delta;
615 for( i = 0; EnvP[i]; i++ ) EnvP[i] += delta;
617 // User Mode Segments
619 ss = 0x23; cs = 0x2B;
622 *--stack = (Uint)EnvP;
623 *--stack = (Uint)ArgV;
624 *--stack = (Uint)ArgC;
628 Proc_StartProcess(ss, (Uint)stack, 0x202, cs, Entrypoint);
631 void Proc_StartProcess(Uint16 SS, Uint Stack, Uint Flags, Uint16 CS, Uint IP)
633 if( !(CS == 0x1B || CS == 0x2B) || SS != 0x23 ) {
634 Log_Error("Proc", "Proc_StartProcess: CS / SS are not valid (%x, %x)",
638 // Log("Proc_StartProcess: (SS=%x, Stack=%p, Flags=%x, CS=%x, IP=%p)", SS, Stack, Flags, CS, IP);
639 // MM_DumpTables(0, USER_MAX);
643 __asm__ __volatile__ (
644 "mov %0, %%rsp;\n\t" // Set stack pointer
645 "mov %2, %%r11;\n\t" // Set RFLAGS
647 : : "r" (Stack), "c" (IP), "r" (Flags)
653 __asm__ __volatile__ (
654 "mov %0, %%rsp;\n\t" // Set stack pointer
655 "mov %2, %%r11;\n\t" // Set RFLAGS
657 : : "r" (Stack), "c" (IP), "r" (Flags)
664 * \fn int Proc_Demote(Uint *Err, int Dest, tRegs *Regs)
665 * \brief Demotes a process to a lower permission level
666 * \param Err Pointer to user's errno
667 * \param Dest New Permission Level
668 * \param Regs Pointer to user's register structure
670 int Proc_Demote(Uint *Err, int Dest, tRegs *Regs)
672 int cpl = Regs->CS & 3;
674 if(Dest > 3 || Dest < 0) {
685 // Change the Segment Registers
686 Regs->CS = (((Dest+1)<<4) | Dest) - 8;
687 Regs->SS = ((Dest+1)<<4) | Dest;
693 * \brief Calls a signal handler in user mode
694 * \note Used for signals
696 void Proc_CallFaultHandler(tThread *Thread)
699 Proc_ReturnToUser(Thread->FaultHandler, Thread->KernelStack, Thread->CurFaultNum);
703 void Proc_DumpThreadCPUState(tThread *Thread)
705 Log(" At %04x:%016llx", Thread->SavedState.UserCS, Thread->SavedState.UserRIP);
708 void Proc_Reschedule(void)
710 tThread *nextthread, *curthread;
711 int cpu = GetCPUNum();
713 // TODO: Wait for it?
714 if(IS_LOCKED(&glThreadListLock)) return;
716 curthread = gaCPUs[cpu].Current;
718 nextthread = Threads_GetNextToRun(cpu, curthread);
720 if(nextthread == curthread) return ;
722 nextthread = gaCPUs[cpu].IdleThread;
726 #if DEBUG_TRACE_SWITCH
727 LogF("\nSwitching to task CR3 = 0x%x, RIP = %p, RSP = %p - %i (%s)\n",
728 nextthread->MemState.CR3,
729 nextthread->SavedState.RIP,
730 nextthread->SavedState.RSP,
732 nextthread->ThreadName
737 gaCPUs[cpu].Current = nextthread;
738 gTSSs[cpu].RSP0 = nextthread->KernelStack-4;
739 __asm__ __volatile__ ("mov %0, %%db0" : : "r" (nextthread));
742 nextthread->SavedState.RSP, &curthread->SavedState.RSP,
743 nextthread->SavedState.RIP, &curthread->SavedState.RIP,
744 nextthread->MemState.CR3
750 * \fn void Proc_Scheduler(int CPU)
751 * \brief Swap current thread and clears dead threads
753 void Proc_Scheduler(int CPU, Uint RSP, Uint RIP)
759 // If the spinlock is set, let it complete
760 if(IS_LOCKED(&glThreadListLock)) return;
762 // Get current thread
763 thread = gaCPUs[CPU].Current;
768 // Reduce remaining quantum and continue timeslice if non-zero
769 if(thread->Remaining--) return;
770 // Reset quantum for next call
771 thread->Remaining = thread->Quantum;
773 // TODO: Make this more stable somehow
775 regs = (tRegs*)(RSP+(1)*8); // CurThread
776 thread->SavedState.UserCS = regs->CS;
777 thread->SavedState.UserRIP = regs->RIP;
789 EXPORT(Proc_SpawnWorker);