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
55 //void ArchThreads_Init(void);
57 void MP_StartAP(int CPU);
58 void MP_SendIPIVector(int CPU, Uint8 Vector);
59 void MP_SendIPI(Uint8 APICID, int Vector, int DeliveryMode);
61 void Proc_IdleThread(void *Ptr);
62 //void Proc_Start(void);
63 //tThread *Proc_GetCurThread(void);
64 void Proc_ChangeStack(void);
65 // int Proc_NewKThread(void (*Fcn)(void*), void *Data);
66 // int Proc_Clone(Uint *Err, Uint Flags);
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 //void Proc_CallFaultHandler(tThread *Thread);
71 //void Proc_DumpThreadCPUState(tThread *Thread);
72 void Proc_Scheduler(int CPU);
75 // --- Multiprocessing ---
77 volatile int giNumInitingCPUs = 0;
78 volatile Uint32 giMP_TimerCount; // Start Count for Local APIC Timer
79 tAPIC *gpMP_LocalAPIC = NULL;
80 Uint8 gaAPIC_to_CPU[256] = {0};
81 int giProc_BootProcessorID = 0;
82 tTSS gaTSSs[MAX_CPUS]; // TSS Array
84 tCPU gaCPUs[MAX_CPUS] = {
85 {.Current = &gThreadZero}
87 tTSS *gTSSs = NULL; // Pointer to TSS array
89 // --- Error Recovery ---
90 char gaDoubleFaultStack[1024] __attribute__ ((section(".padata")));
91 tTSS gDoubleFault_TSS = {
92 .ESP0 = (Uint)&gaDoubleFaultStack[1024],
94 .CR3 = (Uint)gaInitPageDir - KERNEL_BASE,
96 .ESP = (Uint)&gaDoubleFaultStack[1024],
97 .CS = 0x08, .SS = 0x10,
98 .DS = 0x10, .ES = 0x10,
99 .FS = 0x10, .GS = 0x10,
104 * \fn void ArchThreads_Init(void)
105 * \brief Starts the process scheduler
107 void ArchThreads_Init(void)
109 // Mark BSP as active
113 // -- Initialise Multiprocessing
114 const void *mpfloatptr = MPTable_LocateFloatPtr();
117 giNumCPUs = MPTable_FillCPUs(mpfloatptr, gaCPUs, MAX_CPUS, &giProc_BootProcessorID);
118 for( int i = 0; i < giNumCPUs; i ++ )
120 // TODO: Determine if there's an overlap
121 gaAPIC_to_CPU[gaCPUs[i].APICID] = i;
127 Log("No MP Table was found, assuming uniprocessor");
136 #if !DEBUG_DISABLE_DOUBLEFAULT
137 // Initialise Double Fault TSS
138 gGDT[5].BaseLow = (Uint)&gDoubleFault_TSS & 0xFFFF;
139 gGDT[5].BaseMid = (Uint)&gDoubleFault_TSS >> 16;
140 gGDT[5].BaseHi = (Uint)&gDoubleFault_TSS >> 24;
142 // Set double fault IDT to use the new TSS
143 gIDT[8].OffsetLo = 0;
145 gIDT[8].Flags = 0x8500;
146 gIDT[8].OffsetHi = 0;
149 // Set timer frequency
150 outb(0x43, 0x34); // Set Channel 0, Low/High, Rate Generator
151 outb(0x40, TIMER_DIVISOR&0xFF); // Low Byte of Divisor
152 outb(0x40, (TIMER_DIVISOR>>8)&0xFF); // High Byte
154 Log_Debug("Proc", "PIT Frequency %i.%03i Hz",
155 TIMER_BASE/TIMER_DIVISOR,
156 ((Uint64)TIMER_BASE*1000/TIMER_DIVISOR)%1000
160 // Get the count setting for APIC timer
161 Log("Determining APIC Count");
162 __asm__ __volatile__ ("sti");
163 while( giMP_TimerCount == 0 ) __asm__ __volatile__ ("hlt");
164 __asm__ __volatile__ ("cli");
165 Log("APIC Count %i", giMP_TimerCount);
167 Uint64 freq = giMP_TimerCount;
169 freq /= TIMER_DIVISOR;
170 if( (freq /= 1000) < 2*1000)
171 Log("Bus Frequency %i KHz", freq);
172 else if( (freq /= 1000) < 2*1000)
173 Log("Bus Frequency %i MHz", freq);
174 else if( (freq /= 1000) < 2*1000)
175 Log("Bus Frequency %i GHz", freq);
177 Log("Bus Frequency %i THz", freq);
180 // Initialise Normal TSS(s)
181 for(int pos=0;pos<giNumCPUs;pos++)
186 gTSSs[pos].SS0 = 0x10;
187 gTSSs[pos].ESP0 = 0; // Set properly by scheduler
188 gGDT[6+pos].BaseLow = ((Uint)(&gTSSs[pos])) & 0xFFFF;
189 gGDT[6+pos].BaseMid = ((Uint)(&gTSSs[pos]) >> 16) & 0xFFFF;
190 gGDT[6+pos].BaseHi = ((Uint)(&gTSSs[pos])) >> 24;
195 // Load the BSP's TSS
196 __asm__ __volatile__ ("ltr %%ax"::"a"(0x30));
197 // Set Current Thread and CPU Number in DR0 and DR1
198 __asm__ __volatile__ ("mov %0, %%db0"::"r"(&gThreadZero));
199 __asm__ __volatile__ ("mov %0, %%db1"::"r"(0));
201 gaCPUs[0].Current = &gThreadZero;
202 gThreadZero.CurCPU = 0;
204 gProcessZero.MemState.CR3 = (Uint)gaInitPageDir - KERNEL_BASE;
206 // Create Per-Process Data Block
207 if( !MM_Allocate(MM_PPD_CFG) )
209 Panic("OOM - No space for initial Per-Process Config");
212 // Initialise SSE support
213 Proc_InitialiseSSE();
223 void MP_StartAP(int CPU)
225 Log_Log("Proc", "Starting AP %i (APIC %i)", CPU, gaCPUs[CPU].APICID);
227 // Set location of AP startup code and mark for a warm restart
228 *(Uint16*)(KERNEL_BASE|0x467) = (Uint)&APWait - (KERNEL_BASE|0xFFFF0);
229 *(Uint16*)(KERNEL_BASE|0x469) = 0xFFFF;
230 outb(0x70, 0x0F); outb(0x71, 0x0A); // Set warm reset flag
231 MP_SendIPI(gaCPUs[CPU].APICID, 0, 5); // Init IPI
233 // Take a quick nap (20ms)
236 // TODO: Use a better address, preferably registered with the MM
237 // - MM_AllocDMA mabye?
239 *(Uint8*)(KERNEL_BASE|0x11000) = 0xEA; // Far JMP
240 *(Uint16*)(KERNEL_BASE|0x11001) = (Uint16)(tVAddr)&APStartup + 0x10; // IP
241 *(Uint16*)(KERNEL_BASE|0x11003) = 0xFFFF; // CS
245 // Send a Startup-IPI to make the CPU execute at 0x11000 (which we
247 MP_SendIPI(gaCPUs[CPU].APICID, 0x11, 6); // StartupIPI
249 tTime timeout = now() + 2;
250 while( giNumInitingCPUs && now() > timeout )
253 if( giNumInitingCPUs == 0 )
256 // First S-IPI failed, send again
257 MP_SendIPI(gaCPUs[CPU].APICID, 0x11, 6);
259 while( giNumInitingCPUs && now() > timeout )
261 if( giNumInitingCPUs == 0 )
264 Log_Notice("Proc", "CPU %i (APIC %x) didn't come up", CPU, gaCPUs[CPU].APICID);
267 giNumInitingCPUs = 0;
270 void MP_SendIPIVector(int CPU, Uint8 Vector)
272 MP_SendIPI(gaCPUs[CPU].APICID, Vector, 0);
276 * \brief Send an Inter-Processor Interrupt
277 * \param APICID Processor's Local APIC ID
278 * \param Vector Argument of some kind
279 * \param DeliveryMode Type of signal
280 * \note 3A 10.5 "APIC/Handling Local Interrupts"
282 void MP_SendIPI(Uint8 APICID, int Vector, int DeliveryMode)
287 val = (Uint)APICID << 24;
288 gpMP_LocalAPIC->ICR[1].Val = val;
290 val = ((DeliveryMode & 7) << 8) | (Vector & 0xFF);
291 gpMP_LocalAPIC->ICR[0].Val = val;
295 void Proc_IdleThread(void *Ptr)
297 tCPU *cpu = &gaCPUs[GetCPUNum()];
298 cpu->Current->ThreadName = strdup("Idle Thread");
299 Threads_SetPriority( cpu->Current, -1 ); // Never called randomly
300 cpu->Current->Quantum = 1; // 1 slice quantum
302 __asm__ __volatile__ ("sti"); // Make sure interrupts are enabled
303 __asm__ __volatile__ ("hlt");
309 * \fn void Proc_Start(void)
310 * \brief Start process scheduler
312 void Proc_Start(void)
315 // BSP still should run the current task
316 gaCPUs[giProc_BootProcessorID].Current = &gThreadZero;
318 __asm__ __volatile__ ("sti");
321 for( int i = 0; i < giNumCPUs; i ++ )
323 if(i != giProc_BootProcessorID)
324 gaCPUs[i].Current = NULL;
327 Proc_NewKThread(Proc_IdleThread, &gaCPUs[i]);
330 if( i != giProc_BootProcessorID ) {
336 Proc_NewKThread(Proc_IdleThread, &gaCPUs[0]);
339 gaCPUs[0].Current = &gThreadZero;
341 // Start Interrupts (and hence scheduler)
342 __asm__ __volatile__("sti");
344 MM_FinishVirtualInit();
348 * \fn tThread *Proc_GetCurThread(void)
349 * \brief Gets the current thread
351 tThread *Proc_GetCurThread(void)
354 return gaCPUs[ GetCPUNum() ].Current;
356 return gaCPUs[ 0 ].Current;
361 * \fn void Proc_ChangeStack(void)
362 * \brief Swaps the current stack for a new one (in the proper stack reigon)
364 void Proc_ChangeStack(void)
368 Uint curBase, newBase;
370 __asm__ __volatile__ ("mov %%esp, %0":"=r"(esp));
371 __asm__ __volatile__ ("mov %%ebp, %0":"=r"(ebp));
376 newBase = MM_NewKStack();
379 Panic("What the?? Unable to allocate space for initial kernel stack");
383 curBase = (Uint)&Kernel_Stack_Top;
385 LOG("curBase = 0x%x, newBase = 0x%x", curBase, newBase);
387 // Get ESP as a used size
389 LOG("memcpy( %p, %p, 0x%x )", (void*)(newBase - esp), (void*)(curBase - esp), esp );
391 memcpy( (void*)(newBase - esp), (void*)(curBase - esp), esp );
392 // Get ESP as an offset in the new stack
395 ebp = newBase - (curBase - ebp);
397 // Repair EBPs & Stack Addresses
398 // Catches arguments also, but may trash stack-address-like values
399 for(tmpEbp = esp; tmpEbp < newBase; tmpEbp += 4)
401 if(oldEsp < *(Uint*)tmpEbp && *(Uint*)tmpEbp < curBase)
402 *(Uint*)tmpEbp += newBase - curBase;
405 Proc_GetCurThread()->KernelStack = newBase;
407 __asm__ __volatile__ ("mov %0, %%esp"::"r"(esp));
408 __asm__ __volatile__ ("mov %0, %%ebp"::"r"(ebp));
411 void Proc_ClearProcess(tProcess *Process)
413 MM_ClearSpace(Process->MemState.CR3);
416 void Proc_ClearThread(tThread *Thread)
418 if(Thread->SavedState.SSE) {
419 free(Thread->SavedState.SSE);
420 Thread->SavedState.SSE = NULL;
424 tTID Proc_NewKThread(void (*Fcn)(void*), void *Data)
429 newThread = Threads_CloneTCB(0);
430 if(!newThread) return -1;
433 newThread->KernelStack = MM_NewKStack();
435 if(newThread->KernelStack == 0) {
440 esp = newThread->KernelStack;
441 *(Uint*)(esp-=4) = (Uint)Data; // Data (shadowed)
442 *(Uint*)(esp-=4) = 1; // Number of params
443 *(Uint*)(esp-=4) = (Uint)Fcn; // Function to call
444 *(Uint*)(esp-=4) = (Uint)newThread; // Thread ID
446 newThread->SavedState.ESP = esp;
447 newThread->SavedState.EIP = (Uint)&NewTaskHeader;
448 newThread->SavedState.SSE = NULL;
449 // Log("New (KThread) %p, esp = %p", newThread->SavedState.EIP, newThread->SavedState.ESP);
452 Threads_AddActive(newThread);
454 return newThread->TID;
458 * \fn int Proc_Clone(Uint *Err, Uint Flags)
459 * \brief Clone the current process
461 tPID Proc_Clone(Uint Flags)
464 tThread *cur = Proc_GetCurThread();
468 if( !(Flags & CLONE_VM) ) {
469 Log_Error("Proc", "Proc_Clone: Don't leave CLONE_VM unset, use Proc_NewKThread instead");
474 newThread = Threads_CloneTCB(Flags);
475 if(!newThread) return -1;
477 newThread->KernelStack = cur->KernelStack;
480 eip = Proc_CloneInt(&newThread->SavedState.ESP, &newThread->Process->MemState.CR3, Flags & CLONE_NOUSER);
484 newThread->SavedState.EIP = eip;
485 newThread->SavedState.SSE = NULL;
486 newThread->SavedState.bSSEModified = 0;
489 if( newThread->Process->MemState.CR3 == 0 ) {
490 Log_Error("Proc", "Proc_Clone: MM_Clone failed");
491 Threads_Delete(newThread);
495 // Add the new thread to the run queue
496 Threads_AddActive(newThread);
497 return newThread->TID;
501 * \fn int Proc_SpawnWorker(void)
502 * \brief Spawns a new worker thread
504 tThread *Proc_SpawnWorker(void (*Fcn)(void*), void *Data)
507 Uint stack_contents[4];
510 new = Threads_CloneThreadZero();
512 Warning("Proc_SpawnWorker - Out of heap space!\n");
516 // Create the stack contents
517 stack_contents[3] = (Uint)Data;
518 stack_contents[2] = 1;
519 stack_contents[1] = (Uint)Fcn;
520 stack_contents[0] = (Uint)new;
522 // Create a new worker stack (in PID0's address space)
523 new->KernelStack = MM_NewWorkerStack(stack_contents, sizeof(stack_contents));
525 // Save core machine state
526 new->SavedState.ESP = new->KernelStack - sizeof(stack_contents);
527 new->SavedState.EIP = (Uint)NewTaskHeader;
528 new->SavedState.SSE = NULL;
529 new->SavedState.bSSEModified = 0;
532 new->Status = THREAD_STAT_PREINIT;
533 Threads_AddActive( new );
539 * \fn Uint Proc_MakeUserStack(void)
540 * \brief Creates a new user stack
542 Uint Proc_MakeUserStack(void)
545 Uint base = USER_STACK_TOP - USER_STACK_SZ;
547 // Check Prospective Space
548 for( i = USER_STACK_SZ >> 12; i--; )
549 if( MM_GetPhysAddr( (void*)(base + (i<<12)) ) != 0 )
552 if(i != -1) return 0;
554 // Allocate Stack - Allocate incrementally to clean up MM_Dump output
555 for( i = 0; i < USER_STACK_SZ/0x1000; i++ )
557 if( !MM_Allocate( base + (i<<12) ) )
559 Warning("OOM: Proc_MakeUserStack");
564 return base + USER_STACK_SZ;
567 void Proc_StartUser(Uint Entrypoint, Uint Base, int ArgC, const char **ArgV, int DataSize)
571 const char **envp = NULL;
574 // Copy data to the user stack and free original buffer
575 stack = (void*)Proc_MakeUserStack();
576 stack -= (DataSize+sizeof(*stack)-1)/sizeof(*stack);
577 memcpy( stack, ArgV, DataSize );
580 // Adjust Arguments and environment
583 Uint delta = (Uint)stack - (Uint)ArgV;
584 ArgV = (const char**)stack;
585 for( i = 0; ArgV[i]; i++ ) ArgV[i] += delta;
587 for( i = 0; envp[i]; i++ ) envp[i] += delta;
590 // User Mode Segments
591 ss = 0x23; cs = 0x1B;
594 *--stack = (Uint)envp;
595 *--stack = (Uint)ArgV;
596 *--stack = (Uint)ArgC;
599 Proc_StartProcess(ss, (Uint)stack, 0x202, cs, Entrypoint);
602 void Proc_StartProcess(Uint16 SS, Uint Stack, Uint Flags, Uint16 CS, Uint IP)
604 Uint *stack = (void*)Stack;
605 *--stack = SS; //Stack Segment
606 *--stack = Stack; //Stack Pointer
607 *--stack = Flags; //EFLAGS (Resvd (0x2) and IF (0x20))
608 *--stack = CS; //Code Segment
611 *--stack = 0xAAAAAAAA; // eax
612 *--stack = 0xCCCCCCCC; // ecx
613 *--stack = 0xDDDDDDDD; // edx
614 *--stack = 0xBBBBBBBB; // ebx
615 *--stack = 0xD1D1D1D1; // edi
616 *--stack = 0x54545454; // esp - NOT POPED
617 *--stack = 0x51515151; // esi
618 *--stack = 0xB4B4B4B4; // ebp
625 __asm__ __volatile__ (
626 "mov %%eax,%%esp;\n\t" // Set stack pointer
632 "iret;\n\t" : : "a" (stack));
637 * \brief Calls a signal handler in user mode
638 * \note Used for signals
640 void Proc_CallFaultHandler(tThread *Thread)
642 // Rewinds the stack and calls the user function
644 Proc_ReturnToUser( Thread->FaultHandler, Thread->CurFaultNum, Thread->KernelStack );
648 void Proc_DumpThreadCPUState(tThread *Thread)
650 if( Thread->CurCPU > -1 )
652 int maxBacktraceDistance = 6;
656 if( Thread->CurCPU != GetCPUNum() ) {
657 Log(" Currently running");
661 // Backtrace to find the IRQ entrypoint
662 // - This will usually only be called by an IRQ, so this should
664 __asm__ __volatile__ ("mov %%ebp, %0" : "=r" (stack));
665 while( maxBacktraceDistance -- )
667 if( !CheckMem(stack, 8) ) {
674 if( stack[1] == (tVAddr)&IRQCommon_handled ) {
675 regs = (void*)stack[2];
679 stack = (void*)stack[0];
683 Log(" Unable to find IRQ Entry");
687 Log(" at %04x:%08x", regs->cs, regs->eip);
691 tVAddr diffFromScheduler = Thread->SavedState.EIP - (tVAddr)SwitchTasks;
692 tVAddr diffFromClone = Thread->SavedState.EIP - (tVAddr)Proc_CloneInt;
693 tVAddr diffFromSpawn = Thread->SavedState.EIP - (tVAddr)NewTaskHeader;
695 if( diffFromClone > 0 && diffFromClone < 40 ) // When I last checked, .newTask was at .+27
697 Log(" Creating process");
701 if( diffFromSpawn == 0 )
703 Log(" Creating thread");
707 if( diffFromScheduler > 0 && diffFromScheduler < 128 ) // When I last checked, GetEIP was at .+0x30
710 Log(" At %04x:%08x", Thread->SavedState.UserCS, Thread->SavedState.UserEIP);
714 Log(" Just created (unknown %p)", Thread->SavedState.EIP);
717 void Proc_Reschedule(void)
719 tThread *nextthread, *curthread;
720 int cpu = GetCPUNum();
722 // TODO: Wait for the lock?
723 if(IS_LOCKED(&glThreadListLock)) return;
725 curthread = Proc_GetCurThread();
727 nextthread = Threads_GetNextToRun(cpu, curthread);
729 if(!nextthread || nextthread == curthread)
732 #if DEBUG_TRACE_SWITCH
733 // HACK: Ignores switches to the idle threads
734 if( nextthread->TID == 0 || nextthread->TID > giNumCPUs )
736 LogF("\nSwitching CPU %i to %p (%i %s) - CR3 = 0x%x, EIP = %p, ESP = %p\n",
738 nextthread, nextthread->TID, nextthread->ThreadName,
739 nextthread->Process->MemState.CR3,
740 nextthread->SavedState.EIP,
741 nextthread->SavedState.ESP
743 LogF("OldCR3 = %P\n", curthread->Process->MemState.CR3);
748 gaCPUs[cpu].Current = nextthread;
749 gaCPUs[cpu].LastTimerThread = NULL;
750 gTSSs[cpu].ESP0 = nextthread->KernelStack-4;
751 __asm__ __volatile__("mov %0, %%db0\n\t" : : "r"(nextthread) );
753 // Save FPU/MMX/XMM/SSE state
754 if( curthread && curthread->SavedState.SSE )
756 Proc_SaveSSE( ((Uint)curthread->SavedState.SSE + 0xF) & ~0xF );
757 curthread->SavedState.bSSEModified = 0;
764 nextthread->SavedState.ESP, &curthread->SavedState.ESP,
765 nextthread->SavedState.EIP, &curthread->SavedState.EIP,
766 nextthread->Process->MemState.CR3
772 nextthread->SavedState.ESP, 0,
773 nextthread->SavedState.EIP, 0,
774 nextthread->Process->MemState.CR3
782 * \fn void Proc_Scheduler(int CPU)
783 * \brief Swap current thread and clears dead threads
785 void Proc_Scheduler(int CPU)
789 gpMP_LocalAPIC->EOI.Val = 0;
793 __asm__ __volatile__ ("sti");
795 // Call the timer update code
799 // If two ticks happen within the same task, and it's not an idle task, swap
800 if( gaCPUs[CPU].Current->TID > giNumCPUs && gaCPUs[CPU].Current == gaCPUs[CPU].LastTimerThread )
805 gaCPUs[CPU].LastTimerThread = gaCPUs[CPU].Current;
810 EXPORT(Proc_SpawnWorker);