2 * AcessOS Microkernel Version
19 #define DEBUG_TRACE_SWITCH 0
20 #define DEBUG_DISABLE_DOUBLEFAULT 1
21 #define DEBUG_VERY_SLOW_PERIOD 0
22 #define DEBUG_NOPREEMPT 1
26 #define TIMER_BASE 1193182
27 #if DEBUG_VERY_SLOW_PERIOD
28 # define TIMER_DIVISOR 1193 //~10Hz switch, with 10 quantum = 1s per thread
30 # define TIMER_DIVISOR 11932 //~100Hz
38 extern void APWait(void); // 16-bit AP pause code
39 extern void APStartup(void); // 16-bit AP startup code
40 extern void NewTaskHeader(tThread *Thread, void *Fcn, int nArgs, ...); // Actually takes cdecl args
41 extern Uint Proc_CloneInt(Uint *ESP, Uint32 *CR3, int bNoUserClone);
42 extern Uint32 gaInitPageDir[1024]; // start.asm
43 extern char Kernel_Stack_Top[];
45 extern tThread gThreadZero;
46 extern tProcess gProcessZero;
47 extern void Isr8(void); // Double Fault
48 extern void Proc_ReturnToUser(tVAddr Handler, Uint Argument, tVAddr KernelStack);
49 extern char scheduler_return[]; // Return address in SchedulerBase
50 extern char IRQCommon[]; // Common IRQ handler code
51 extern char IRQCommon_handled[]; // IRQCommon call return location
52 extern char GetEIP_Sched_ret[]; // GetEIP call return location
53 extern void Timer_CallTimers(void);
56 //void ArchThreads_Init(void);
58 void MP_StartAP(int CPU);
59 void MP_SendIPIVector(int CPU, Uint8 Vector);
60 void MP_SendIPI(Uint8 APICID, int Vector, int DeliveryMode);
62 void Proc_IdleThread(void *Ptr);
63 //void Proc_Start(void);
64 //tThread *Proc_GetCurThread(void);
65 void Proc_ChangeStack(void);
66 // int Proc_NewKThread(void (*Fcn)(void*), void *Data);
67 // int Proc_Clone(Uint *Err, Uint Flags);
68 Uint Proc_MakeUserStack(void);
69 //void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize);
70 void Proc_StartProcess(Uint16 SS, Uint Stack, Uint Flags, Uint16 CS, Uint IP) NORETURN;
71 void Proc_CallUser(Uint32 UserIP, Uint32 UserSP, const void *StackData, size_t StackDataLen);
72 //void Proc_CallFaultHandler(tThread *Thread);
73 //void Proc_DumpThreadCPUState(tThread *Thread);
74 void Proc_Scheduler(int CPU);
77 // --- Multiprocessing ---
79 volatile int giNumInitingCPUs = 0;
80 volatile Uint32 giMP_TimerCount; // Start Count for Local APIC Timer
81 tAPIC *gpMP_LocalAPIC = NULL;
82 Uint8 gaAPIC_to_CPU[256] = {0};
83 int giProc_BootProcessorID = 0;
84 tTSS gaTSSs[MAX_CPUS]; // TSS Array
86 tCPU gaCPUs[MAX_CPUS] = {
87 {.Current = &gThreadZero}
89 tTSS *gTSSs = NULL; // Pointer to TSS array
91 // --- Error Recovery ---
92 char gaDoubleFaultStack[1024] __attribute__ ((section(".padata")));
93 tTSS gDoubleFault_TSS = {
94 .ESP0 = (Uint)&gaDoubleFaultStack[1024],
96 .CR3 = (Uint)gaInitPageDir - KERNEL_BASE,
98 .ESP = (Uint)&gaDoubleFaultStack[1024],
99 .CS = 0x08, .SS = 0x10,
100 .DS = 0x10, .ES = 0x10,
101 .FS = 0x10, .GS = 0x10,
106 * \fn void ArchThreads_Init(void)
107 * \brief Starts the process scheduler
109 void ArchThreads_Init(void)
111 // Mark BSP as active
115 // -- Initialise Multiprocessing
116 const void *mpfloatptr = MPTable_LocateFloatPtr();
119 giNumCPUs = MPTable_FillCPUs(mpfloatptr, gaCPUs, MAX_CPUS, &giProc_BootProcessorID);
120 for( int i = 0; i < giNumCPUs; i ++ )
122 // TODO: Determine if there's an overlap
123 gaAPIC_to_CPU[gaCPUs[i].APICID] = i;
129 Log("No MP Table was found, assuming uniprocessor");
138 #if !DEBUG_DISABLE_DOUBLEFAULT
139 // Initialise Double Fault TSS
140 gGDT[5].BaseLow = (Uint)&gDoubleFault_TSS & 0xFFFF;
141 gGDT[5].BaseMid = (Uint)&gDoubleFault_TSS >> 16;
142 gGDT[5].BaseHi = (Uint)&gDoubleFault_TSS >> 24;
144 // Set double fault IDT to use the new TSS
145 gIDT[8].OffsetLo = 0;
147 gIDT[8].Flags = 0x8500;
148 gIDT[8].OffsetHi = 0;
151 // Set timer frequency
152 outb(0x43, 0x34); // Set Channel 0, Low/High, Rate Generator
153 outb(0x40, TIMER_DIVISOR&0xFF); // Low Byte of Divisor
154 outb(0x40, (TIMER_DIVISOR>>8)&0xFF); // High Byte
156 Log_Debug("Proc", "PIT Frequency %i.%03i Hz",
157 TIMER_BASE/TIMER_DIVISOR,
158 ((Uint64)TIMER_BASE*1000/TIMER_DIVISOR)%1000
162 // Get the count setting for APIC timer
163 Log("Determining APIC Count");
164 __asm__ __volatile__ ("sti");
165 while( giMP_TimerCount == 0 ) __asm__ __volatile__ ("hlt");
166 __asm__ __volatile__ ("cli");
167 Log("APIC Count %i", giMP_TimerCount);
169 Uint64 freq = giMP_TimerCount;
171 freq /= TIMER_DIVISOR;
172 if( (freq /= 1000) < 2*1000)
173 Log("Bus Frequency %i KHz", freq);
174 else if( (freq /= 1000) < 2*1000)
175 Log("Bus Frequency %i MHz", freq);
176 else if( (freq /= 1000) < 2*1000)
177 Log("Bus Frequency %i GHz", freq);
179 Log("Bus Frequency %i THz", freq);
182 // Initialise Normal TSS(s)
183 for(int pos=0;pos<giNumCPUs;pos++)
188 gTSSs[pos].SS0 = 0x10;
189 gTSSs[pos].ESP0 = 0; // Set properly by scheduler
190 gGDT[6+pos].BaseLow = ((Uint)(&gTSSs[pos])) & 0xFFFF;
191 gGDT[6+pos].BaseMid = ((Uint)(&gTSSs[pos]) >> 16) & 0xFFFF;
192 gGDT[6+pos].BaseHi = ((Uint)(&gTSSs[pos])) >> 24;
197 // Load the BSP's TSS
198 __asm__ __volatile__ ("ltr %%ax"::"a"(0x30));
199 // Set Current Thread and CPU Number in DR0 and DR1
200 __asm__ __volatile__ ("mov %0, %%db0"::"r"(&gThreadZero));
201 __asm__ __volatile__ ("mov %0, %%db1"::"r"(0));
203 gaCPUs[0].Current = &gThreadZero;
204 gThreadZero.CurCPU = 0;
206 gProcessZero.MemState.CR3 = (Uint)gaInitPageDir - KERNEL_BASE;
208 // Create Per-Process Data Block
209 if( !MM_Allocate(MM_PPD_CFG) )
211 Panic("OOM - No space for initial Per-Process Config");
214 // Initialise SSE support
215 Proc_InitialiseSSE();
225 void MP_StartAP(int CPU)
227 Log_Log("Proc", "Starting AP %i (APIC %i)", CPU, gaCPUs[CPU].APICID);
229 // Set location of AP startup code and mark for a warm restart
230 *(Uint16*)(KERNEL_BASE|0x467) = (Uint)&APWait - (KERNEL_BASE|0xFFFF0);
231 *(Uint16*)(KERNEL_BASE|0x469) = 0xFFFF;
232 outb(0x70, 0x0F); outb(0x71, 0x0A); // Set warm reset flag
233 MP_SendIPI(gaCPUs[CPU].APICID, 0, 5); // Init IPI
235 // Take a quick nap (20ms)
238 // TODO: Use a better address, preferably registered with the MM
239 // - MM_AllocDMA mabye?
241 *(Uint8*)(KERNEL_BASE|0x11000) = 0xEA; // Far JMP
242 *(Uint16*)(KERNEL_BASE|0x11001) = (Uint16)(tVAddr)&APStartup + 0x10; // IP
243 *(Uint16*)(KERNEL_BASE|0x11003) = 0xFFFF; // CS
247 // Send a Startup-IPI to make the CPU execute at 0x11000 (which we
249 MP_SendIPI(gaCPUs[CPU].APICID, 0x11, 6); // StartupIPI
251 tTime timeout = now() + 2;
252 while( giNumInitingCPUs && now() > timeout )
255 if( giNumInitingCPUs == 0 )
258 // First S-IPI failed, send again
259 MP_SendIPI(gaCPUs[CPU].APICID, 0x11, 6);
261 while( giNumInitingCPUs && now() > timeout )
263 if( giNumInitingCPUs == 0 )
266 Log_Notice("Proc", "CPU %i (APIC %x) didn't come up", CPU, gaCPUs[CPU].APICID);
269 giNumInitingCPUs = 0;
272 void MP_SendIPIVector(int CPU, Uint8 Vector)
274 MP_SendIPI(gaCPUs[CPU].APICID, Vector, 0);
278 * \brief Send an Inter-Processor Interrupt
279 * \param APICID Processor's Local APIC ID
280 * \param Vector Argument of some kind
281 * \param DeliveryMode Type of signal
282 * \note 3A 10.5 "APIC/Handling Local Interrupts"
284 void MP_SendIPI(Uint8 APICID, int Vector, int DeliveryMode)
289 val = (Uint)APICID << 24;
290 gpMP_LocalAPIC->ICR[1].Val = val;
292 val = ((DeliveryMode & 7) << 8) | (Vector & 0xFF);
293 gpMP_LocalAPIC->ICR[0].Val = val;
297 void Proc_IdleThread(void *Ptr)
299 tCPU *cpu = &gaCPUs[GetCPUNum()];
300 cpu->Current->ThreadName = strdup("Idle Thread");
301 Threads_SetPriority( cpu->Current, -1 ); // Never called randomly
302 cpu->Current->Quantum = 1; // 1 slice quantum
304 __asm__ __volatile__ ("sti"); // Make sure interrupts are enabled
305 __asm__ __volatile__ ("hlt");
311 * \fn void Proc_Start(void)
312 * \brief Start process scheduler
314 void Proc_Start(void)
317 // BSP still should run the current task
318 gaCPUs[giProc_BootProcessorID].Current = &gThreadZero;
320 __asm__ __volatile__ ("sti");
323 for( int i = 0; i < giNumCPUs; i ++ )
325 if(i != giProc_BootProcessorID)
326 gaCPUs[i].Current = NULL;
329 Proc_NewKThread(Proc_IdleThread, &gaCPUs[i]);
332 if( i != giProc_BootProcessorID ) {
338 Proc_NewKThread(Proc_IdleThread, &gaCPUs[0]);
341 gaCPUs[0].Current = &gThreadZero;
343 // Start Interrupts (and hence scheduler)
344 __asm__ __volatile__("sti");
346 MM_FinishVirtualInit();
350 * \fn tThread *Proc_GetCurThread(void)
351 * \brief Gets the current thread
353 tThread *Proc_GetCurThread(void)
356 return gaCPUs[ GetCPUNum() ].Current;
358 return gaCPUs[ 0 ].Current;
363 * \fn void Proc_ChangeStack(void)
364 * \brief Swaps the current stack for a new one (in the proper stack reigon)
366 void Proc_ChangeStack(void)
370 Uint curBase, newBase;
372 __asm__ __volatile__ ("mov %%esp, %0":"=r"(esp));
373 __asm__ __volatile__ ("mov %%ebp, %0":"=r"(ebp));
378 newBase = MM_NewKStack();
381 Panic("What the?? Unable to allocate space for initial kernel stack");
385 curBase = (Uint)&Kernel_Stack_Top;
387 LOG("curBase = 0x%x, newBase = 0x%x", curBase, newBase);
389 // Get ESP as a used size
391 LOG("memcpy( %p, %p, 0x%x )", (void*)(newBase - esp), (void*)(curBase - esp), esp );
393 memcpy( (void*)(newBase - esp), (void*)(curBase - esp), esp );
394 // Get ESP as an offset in the new stack
397 ebp = newBase - (curBase - ebp);
399 // Repair EBPs & Stack Addresses
400 // Catches arguments also, but may trash stack-address-like values
401 for(tmpEbp = esp; tmpEbp < newBase; tmpEbp += 4)
403 if(oldEsp < *(Uint*)tmpEbp && *(Uint*)tmpEbp < curBase)
404 *(Uint*)tmpEbp += newBase - curBase;
407 Proc_GetCurThread()->KernelStack = newBase;
409 __asm__ __volatile__ ("mov %0, %%esp"::"r"(esp));
410 __asm__ __volatile__ ("mov %0, %%ebp"::"r"(ebp));
413 void Proc_ClearProcess(tProcess *Process)
415 MM_ClearSpace(Process->MemState.CR3);
418 void Proc_ClearThread(tThread *Thread)
420 if(Thread->SavedState.SSE) {
421 free(Thread->SavedState.SSE);
422 Thread->SavedState.SSE = NULL;
426 tTID Proc_NewKThread(void (*Fcn)(void*), void *Data)
431 newThread = Threads_CloneTCB(0);
432 if(!newThread) return -1;
435 newThread->KernelStack = MM_NewKStack();
437 if(newThread->KernelStack == 0) {
442 esp = newThread->KernelStack;
443 *(Uint*)(esp-=4) = (Uint)Data; // Data (shadowed)
444 *(Uint*)(esp-=4) = 1; // Number of params
445 *(Uint*)(esp-=4) = (Uint)Fcn; // Function to call
446 *(Uint*)(esp-=4) = (Uint)newThread; // Thread ID
448 newThread->SavedState.ESP = esp;
449 newThread->SavedState.EIP = (Uint)&NewTaskHeader;
450 newThread->SavedState.SSE = NULL;
451 // Log("New (KThread) %p, esp = %p", newThread->SavedState.EIP, newThread->SavedState.ESP);
454 Threads_AddActive(newThread);
456 return newThread->TID;
460 * \fn int Proc_Clone(Uint *Err, Uint Flags)
461 * \brief Clone the current process
463 tPID Proc_Clone(Uint Flags)
466 tThread *cur = Proc_GetCurThread();
470 if( !(Flags & CLONE_VM) ) {
471 Log_Error("Proc", "Proc_Clone: Don't leave CLONE_VM unset, use Proc_NewKThread instead");
476 newThread = Threads_CloneTCB(Flags);
477 if(!newThread) return -1;
479 newThread->KernelStack = cur->KernelStack;
482 eip = Proc_CloneInt(&newThread->SavedState.ESP, &newThread->Process->MemState.CR3, Flags & CLONE_NOUSER);
486 newThread->SavedState.EIP = eip;
487 newThread->SavedState.SSE = NULL;
488 newThread->SavedState.bSSEModified = 0;
491 if( newThread->Process->MemState.CR3 == 0 ) {
492 Log_Error("Proc", "Proc_Clone: MM_Clone failed");
493 Threads_Delete(newThread);
497 // Add the new thread to the run queue
498 Threads_AddActive(newThread);
499 return newThread->TID;
503 * \fn int Proc_SpawnWorker(void)
504 * \brief Spawns a new worker thread
506 tThread *Proc_SpawnWorker(void (*Fcn)(void*), void *Data)
509 Uint stack_contents[4];
512 new = Threads_CloneThreadZero();
514 Warning("Proc_SpawnWorker - Out of heap space!\n");
518 // Create the stack contents
519 stack_contents[3] = (Uint)Data;
520 stack_contents[2] = 1;
521 stack_contents[1] = (Uint)Fcn;
522 stack_contents[0] = (Uint)new;
524 // Create a new worker stack (in PID0's address space)
525 new->KernelStack = MM_NewWorkerStack(stack_contents, sizeof(stack_contents));
527 // Save core machine state
528 new->SavedState.ESP = new->KernelStack - sizeof(stack_contents);
529 new->SavedState.EIP = (Uint)NewTaskHeader;
530 new->SavedState.SSE = NULL;
531 new->SavedState.bSSEModified = 0;
534 new->Status = THREAD_STAT_PREINIT;
535 Threads_AddActive( new );
541 * \fn Uint Proc_MakeUserStack(void)
542 * \brief Creates a new user stack
544 Uint Proc_MakeUserStack(void)
547 Uint base = USER_STACK_TOP - USER_STACK_SZ;
549 // Check Prospective Space
550 for( i = USER_STACK_SZ >> 12; i--; )
551 if( MM_GetPhysAddr( (void*)(base + (i<<12)) ) != 0 )
554 if(i != -1) return 0;
556 // Allocate Stack - Allocate incrementally to clean up MM_Dump output
557 for( i = 0; i < USER_STACK_SZ/0x1000; i++ )
559 if( !MM_Allocate( base + (i<<12) ) )
561 Warning("OOM: Proc_MakeUserStack");
566 return base + USER_STACK_SZ;
569 void Proc_StartUser(Uint Entrypoint, Uint Base, int ArgC, const char **ArgV, int DataSize)
573 const char **envp = NULL;
576 // Copy data to the user stack and free original buffer
577 stack = (void*)Proc_MakeUserStack();
578 stack -= (DataSize+sizeof(*stack)-1)/sizeof(*stack);
579 memcpy( stack, ArgV, DataSize );
582 // Adjust Arguments and environment
585 Uint delta = (Uint)stack - (Uint)ArgV;
586 ArgV = (const char**)stack;
587 for( i = 0; ArgV[i]; i++ ) ArgV[i] += delta;
589 for( i = 0; envp[i]; i++ ) envp[i] += delta;
592 // User Mode Segments
593 ss = 0x23; cs = 0x1B;
596 *--stack = (Uint)envp;
597 *--stack = (Uint)ArgV;
598 *--stack = (Uint)ArgC;
601 Proc_StartProcess(ss, (Uint)stack, 0x202, cs, Entrypoint);
604 void Proc_StartProcess(Uint16 SS, Uint Stack, Uint Flags, Uint16 CS, Uint IP)
606 Uint *stack = (void*)Stack;
607 *--stack = SS; //Stack Segment
608 *--stack = Stack; //Stack Pointer
609 *--stack = Flags; //EFLAGS (Resvd (0x2) and IF (0x20))
610 *--stack = CS; //Code Segment
613 *--stack = 0xAAAAAAAA; // eax
614 *--stack = 0xCCCCCCCC; // ecx
615 *--stack = 0xDDDDDDDD; // edx
616 *--stack = 0xBBBBBBBB; // ebx
617 *--stack = 0xD1D1D1D1; // edi
618 *--stack = 0x54545454; // esp - NOT POPED
619 *--stack = 0x51515151; // esi
620 *--stack = 0xB4B4B4B4; // ebp
627 __asm__ __volatile__ (
628 "mov %%eax,%%esp;\n\t" // Set stack pointer
634 "iret;\n\t" : : "a" (stack));
638 void Proc_CallUser(Uint32 UserIP, Uint32 UserSP, const void *StackData, size_t StackDataLen)
640 if( UserSP < StackDataLen )
642 if( !CheckMem( (void*)(UserSP - StackDataLen), StackDataLen ) )
644 memcpy( (void*)(UserSP - StackDataLen), StackData, StackDataLen );
646 __asm__ __volatile__ (
647 "mov $0x23,%%ax;\n\t"
648 "mov %%ax, %%ds;\n\t"
649 "mov %%ax, %%es;\n\t"
650 "mov %%ax, %%fs;\n\t"
651 "mov %%ax, %%gs;\n\t"
659 : "r" (UserIP), "r" (UserSP - StackDataLen)
667 * \brief Calls a signal handler in user mode
668 * \note Used for signals
670 void Proc_CallFaultHandler(tThread *Thread)
672 // Rewinds the stack and calls the user function
674 Proc_ReturnToUser( Thread->FaultHandler, Thread->CurFaultNum, Thread->KernelStack );
678 void Proc_DumpThreadCPUState(tThread *Thread)
680 if( Thread->CurCPU > -1 )
682 int maxBacktraceDistance = 6;
686 if( Thread->CurCPU != GetCPUNum() ) {
687 Log(" Currently running");
691 // Backtrace to find the IRQ entrypoint
692 // - This will usually only be called by an IRQ, so this should
694 __asm__ __volatile__ ("mov %%ebp, %0" : "=r" (stack));
695 while( maxBacktraceDistance -- )
697 if( !CheckMem(stack, 8) ) {
704 if( stack[1] == (tVAddr)&IRQCommon_handled ) {
705 regs = (void*)stack[2];
709 stack = (void*)stack[0];
713 Log(" Unable to find IRQ Entry");
717 Log(" at %04x:%08x [EAX:%x]", regs->cs, regs->eip, regs->eax);
718 Error_Backtrace(regs->eip, regs->ebp);
722 tVAddr diffFromScheduler = Thread->SavedState.EIP - (tVAddr)SwitchTasks;
723 tVAddr diffFromClone = Thread->SavedState.EIP - (tVAddr)Proc_CloneInt;
724 tVAddr diffFromSpawn = Thread->SavedState.EIP - (tVAddr)NewTaskHeader;
726 if( diffFromClone > 0 && diffFromClone < 40 ) // When I last checked, .newTask was at .+27
728 Log(" Creating process");
732 if( diffFromSpawn == 0 )
734 Log(" Creating thread");
738 if( diffFromScheduler > 0 && diffFromScheduler < 128 ) // When I last checked, GetEIP was at .+0x30
741 Log(" At %04x:%08x", Thread->SavedState.UserCS, Thread->SavedState.UserEIP);
745 Log(" Just created (unknown %p)", Thread->SavedState.EIP);
748 void Proc_Reschedule(void)
750 tThread *nextthread, *curthread;
751 int cpu = GetCPUNum();
753 // TODO: Wait for the lock?
754 if(IS_LOCKED(&glThreadListLock)) return;
756 curthread = Proc_GetCurThread();
758 nextthread = Threads_GetNextToRun(cpu, curthread);
760 if(!nextthread || nextthread == curthread)
763 #if DEBUG_TRACE_SWITCH
764 // HACK: Ignores switches to the idle threads
765 if( nextthread->TID == 0 || nextthread->TID > giNumCPUs )
767 LogF("\nSwitching CPU %i to %p (%i %s) - CR3 = 0x%x, EIP = %p, ESP = %p\n",
769 nextthread, nextthread->TID, nextthread->ThreadName,
770 nextthread->Process->MemState.CR3,
771 nextthread->SavedState.EIP,
772 nextthread->SavedState.ESP
774 LogF("OldCR3 = %P\n", curthread->Process->MemState.CR3);
779 gaCPUs[cpu].Current = nextthread;
780 gaCPUs[cpu].LastTimerThread = NULL;
781 gTSSs[cpu].ESP0 = nextthread->KernelStack-4;
782 __asm__ __volatile__("mov %0, %%db0\n\t" : : "r"(nextthread) );
784 // Save FPU/MMX/XMM/SSE state
785 if( curthread && curthread->SavedState.SSE )
787 Proc_SaveSSE( ((Uint)curthread->SavedState.SSE + 0xF) & ~0xF );
788 curthread->SavedState.bSSEModified = 0;
795 nextthread->SavedState.ESP, &curthread->SavedState.ESP,
796 nextthread->SavedState.EIP, &curthread->SavedState.EIP,
797 nextthread->Process->MemState.CR3
803 nextthread->SavedState.ESP, 0,
804 nextthread->SavedState.EIP, 0,
805 nextthread->Process->MemState.CR3
813 * \fn void Proc_Scheduler(int CPU)
814 * \brief Swap current thread and clears dead threads
816 void Proc_Scheduler(int CPU)
820 gpMP_LocalAPIC->EOI.Val = 0;
824 __asm__ __volatile__ ("sti");
826 // Call the timer update code
830 // If two ticks happen within the same task, and it's not an idle task, swap
831 if( gaCPUs[CPU].Current->TID > giNumCPUs && gaCPUs[CPU].Current == gaCPUs[CPU].LastTimerThread )
836 gaCPUs[CPU].LastTimerThread = gaCPUs[CPU].Current;
841 EXPORT(Proc_SpawnWorker);