2 * AcessOS Microkernel Version
14 #define DEBUG_TRACE_SWITCH 0
17 #define SWITCH_MAGIC 0xFFFACE55 // There is no code in this area
18 #define TIMER_DIVISOR 11931 //~100Hz
22 extern Uint GetEIP(); // start.asm
23 extern Uint32 gaInitPageDir[1024]; // start.asm
24 extern void Kernel_Stack_Top;
25 extern volatile int giThreadListLock;
28 extern int giTotalTickets;
29 extern int giNumActiveThreads;
30 extern tThread gThreadZero;
31 extern tThread *gActiveThreads;
32 extern tThread *gSleepingThreads;
33 extern tThread *gDeleteThreads;
34 extern tThread *Threads_GetNextToRun(int CPU);
35 extern void Threads_Dump();
36 extern tThread *Threads_CloneTCB(Uint *Err, Uint Flags);
40 void ArchThreads_Init();
41 tThread *Proc_GetCurThread();
42 void Proc_ChangeStack();
43 int Proc_Clone(Uint *Err, Uint Flags);
44 void Proc_Scheduler();
47 // --- Current State ---
49 tThread *gCurrentThread[MAX_CPUS] = {NULL};
51 tThread *gCurrentThread = NULL;
53 // --- Multiprocessing ---
55 tMPInfo *gMPTable = NULL;
58 Uint32 *gPML4s[4] = NULL;
64 // --- Error Recovery ---
65 char gaDoubleFaultStack[1024];
66 tTSS gDoubleFault_TSS = {
67 .ESP0 = (Uint)&gaDoubleFaultStack[1023],
74 * \fn void ArchThreads_Init()
75 * \brief Starts the process scheduler
77 void ArchThreads_Init()
81 // -- Initialise Multiprocessing
82 // Find MP Floating Table
84 for(pos = KERNEL_BASE|0x9FC00; pos < (KERNEL_BASE|0xA0000); pos += 16) {
85 if( *(Uint*)(pos) == MPTABLE_IDENT ) {
86 if(ByteSum( (void*)pos, sizeof(tMPInfo) ) != 0) continue;
87 gMPTable = (void*)pos;
97 for(pos = KERNEL_BASE|0xF0000; pos < (KERNEL_BASE|0x100000); pos += 16) {
98 if( *(Uint*)(pos) == MPTABLE_IDENT ) {
99 if(ByteSum( (void*)pos, sizeof(tMPInfo) ) != 0) continue;
100 gMPTable = (void*)pos;
106 // If the MP Table Exists, parse it
109 Panic("Uh oh... MP Table Parsing is unimplemented\n");
117 // Initialise Double Fault TSS
118 gGDT[5].LimitLow = sizeof(tTSS);
120 gGDT[5].Access = 0x89; // Type
122 gGDT[5].BaseLow = (Uint)&gDoubleFault_TSS & 0xFFFF;
123 gGDT[5].BaseMid = (Uint)&gDoubleFault_TSS >> 16;
124 gGDT[5].BaseHi = (Uint)&gDoubleFault_TSS >> 24;
127 for(pos=0;pos<giNumCPUs;pos++)
132 gTSSs[pos].SS0 = 0x10;
133 gTSSs[pos].ESP0 = 0; // Set properly by scheduler
134 gGDT[6+pos].LimitLow = sizeof(tTSS);
135 gGDT[6+pos].LimitHi = 0;
136 gGDT[6+pos].Access = 0x89; // Type
137 gGDT[6+pos].Flags = 0x4;
138 gGDT[6+pos].BaseLow = (Uint)&gTSSs[pos] & 0xFFFF;
139 gGDT[6+pos].BaseMid = (Uint)&gTSSs[pos] >> 16;
140 gGDT[6+pos].BaseHi = (Uint)&gTSSs[pos] >> 24;
143 for(pos=0;pos<giNumCPUs;pos++) {
145 __asm__ __volatile__ ("ltr %%ax"::"a"(0x30+pos*8));
151 gCurrentThread[0] = &gThreadZero;
153 gCurrentThread = &gThreadZero;
157 gThreadZero.MemState.PDP[0] = 0;
158 gThreadZero.MemState.PDP[1] = 0;
159 gThreadZero.MemState.PDP[2] = 0;
161 gThreadZero.MemState.CR3 = (Uint)gaInitPageDir - KERNEL_BASE;
164 // Set timer frequency
165 outb(0x43, 0x34); // Set Channel 0, Low/High, Rate Generator
166 outb(0x40, TIMER_DIVISOR&0xFF); // Low Byte of Divisor
167 outb(0x40, (TIMER_DIVISOR>>8)&0xFF); // High Byte
169 // Create Per-Process Data Block
170 MM_Allocate(MM_PPD_CFG);
177 * \fn void Proc_Start()
178 * \brief Start process scheduler
182 // Start Interrupts (and hence scheduler)
183 __asm__ __volatile__("sti");
187 * \fn tThread *Proc_GetCurThread()
188 * \brief Gets the current thread
190 tThread *Proc_GetCurThread()
195 return gCurrentThread;
200 * \fn void Proc_ChangeStack()
201 * \brief Swaps the current stack for a new one (in the proper stack reigon)
203 void Proc_ChangeStack()
207 Uint curBase, newBase;
209 __asm__ __volatile__ ("mov %%esp, %0":"=r"(esp));
210 __asm__ __volatile__ ("mov %%ebp, %0":"=r"(ebp));
215 newBase = MM_NewKStack();
218 Panic("What the?? Unable to allocate space for initial kernel stack");
222 curBase = (Uint)&Kernel_Stack_Top;
224 LOG("curBase = 0x%x, newBase = 0x%x", curBase, newBase);
226 // Get ESP as a used size
228 LOG("memcpy( %p, %p, 0x%x )", (void*)(newBase - esp), (void*)(curBase - esp), esp );
230 memcpy( (void*)(newBase - esp), (void*)(curBase - esp), esp );
231 // Get ESP as an offset in the new stack
234 ebp = newBase - (curBase - ebp);
236 // Repair EBPs & Stack Addresses
237 // Catches arguments also, but may trash stack-address-like values
238 for(tmpEbp = esp; tmpEbp < newBase; tmpEbp += 4)
240 if(oldEsp < *(Uint*)tmpEbp && *(Uint*)tmpEbp < curBase)
241 *(Uint*)tmpEbp += newBase - curBase;
244 gCurrentThread->KernelStack = newBase;
246 __asm__ __volatile__ ("mov %0, %%esp"::"r"(esp));
247 __asm__ __volatile__ ("mov %0, %%ebp"::"r"(ebp));
251 * \fn int Proc_Clone(Uint *Err, Uint Flags)
252 * \brief Clone the current process
254 int Proc_Clone(Uint *Err, Uint Flags)
257 tThread *cur = Proc_GetCurThread();
260 __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp));
261 __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp));
263 newThread = Threads_CloneTCB(Err, Flags);
264 if(!newThread) return -1;
266 // Initialise Memory Space (New Addr space or kernel stack)
267 if(Flags & CLONE_VM) {
268 newThread->MemState.CR3 = MM_Clone();
269 newThread->KernelStack = cur->KernelStack;
271 Uint tmpEbp, oldEsp = esp;
274 newThread->MemState.CR3 = cur->MemState.CR3;
277 newThread->KernelStack = MM_NewKStack();
279 if(newThread->KernelStack == 0) {
284 // Get ESP as a used size
285 esp = cur->KernelStack - esp;
287 memcpy( (void*)(newThread->KernelStack - esp), (void*)(cur->KernelStack - esp), esp );
288 // Get ESP as an offset in the new stack
289 esp = newThread->KernelStack - esp;
291 ebp = newThread->KernelStack - (cur->KernelStack - ebp);
293 // Repair EBPs & Stack Addresses
294 // Catches arguments also, but may trash stack-address-like values
295 for(tmpEbp = esp; tmpEbp < newThread->KernelStack; tmpEbp += 4)
297 if(oldEsp < *(Uint*)tmpEbp && *(Uint*)tmpEbp < cur->KernelStack)
298 *(Uint*)tmpEbp += newThread->KernelStack - cur->KernelStack;
302 // Save core machine state
303 newThread->SavedState.ESP = esp;
304 newThread->SavedState.EBP = ebp;
306 if(eip == SWITCH_MAGIC) {
307 outb(0x20, 0x20); // ACK Timer and return as child
312 newThread->SavedState.EIP = eip;
314 // Lock list and add to active
315 Threads_AddActive(newThread);
317 return newThread->TID;
321 * \fn int Proc_SpawnWorker()
322 * \brief Spawns a new worker thread
324 int Proc_SpawnWorker()
329 cur = Proc_GetCurThread();
332 new = malloc( sizeof(tThread) );
334 Warning("Proc_SpawnWorker - Out of heap space!\n");
337 memcpy(new, &gThreadZero, sizeof(tThread));
339 new->TID = giNextTID++;
341 new->KernelStack = MM_NewWorkerStack();
343 // Get ESP and EBP based in the new stack
344 __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp));
345 __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp));
346 esp = cur->KernelStack - (new->KernelStack - esp);
347 ebp = new->KernelStack - (cur->KernelStack - ebp);
349 // Save core machine state
350 new->SavedState.ESP = esp;
351 new->SavedState.EBP = ebp;
353 if(eip == SWITCH_MAGIC) {
354 outb(0x20, 0x20); // ACK Timer and return as child
359 new->SavedState.EIP = eip;
365 * \fn Uint Proc_MakeUserStack()
366 * \brief Creates a new user stack
368 Uint Proc_MakeUserStack()
371 Uint base = USER_STACK_TOP - USER_STACK_SZ;
373 // Check Prospective Space
374 for( i = USER_STACK_SZ >> 12; i--; )
375 if( MM_GetPhysAddr( base + (i<<12) ) != 0 )
378 if(i != -1) return 0;
380 // Allocate Stack - Allocate incrementally to clean up MM_Dump output
381 for( i = 0; i < USER_STACK_SZ/4069; i++ )
382 MM_Allocate( base + (i<<12) );
384 return base + USER_STACK_SZ;
389 * \fn void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize)
390 * \brief Starts a user task
392 void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize)
394 Uint *stack = (void*)Proc_MakeUserStack();
399 LOG("stack = 0x%x", stack);
402 stack = (void*)( (Uint)stack - DataSize );
403 memcpy( stack, ArgV, DataSize );
405 // Adjust Arguments and environment
406 delta = (Uint)stack - (Uint)ArgV;
407 ArgV = (char**)stack;
408 for( i = 0; ArgV[i]; i++ ) ArgV[i] += delta;
411 for( i = 0; EnvP[i]; i++ ) EnvP[i] += delta;
413 // User Mode Segments
414 ss = 0x23; cs = 0x1B;
417 *--stack = (Uint)EnvP;
418 *--stack = (Uint)ArgV;
419 *--stack = (Uint)ArgC;
422 *--stack = 0; // Return Address
423 delta = (Uint)stack; // Reuse delta to save SP
425 *--stack = ss; //Stack Segment
426 *--stack = delta; //Stack Pointer
427 *--stack = 0x0202; //EFLAGS (Resvd (0x2) and IF (0x20))
428 *--stack = cs; //Code Segment
429 *--stack = Entrypoint; //EIP
431 *--stack = 0xAAAAAAAA; // eax
432 *--stack = 0xCCCCCCCC; // ecx
433 *--stack = 0xDDDDDDDD; // edx
434 *--stack = 0xBBBBBBBB; // ebx
435 *--stack = 0xD1D1D1D1; // edi
436 *--stack = 0x54545454; // esp - NOT POPED
437 *--stack = 0x51515151; // esi
438 *--stack = 0xB4B4B4B4; // ebp
445 __asm__ __volatile__ (
446 "mov %%eax,%%esp;\n\t" // Set stack pointer
452 "iret;\n\t" : : "a" (stack));
457 * \fn int Proc_Demote(Uint *Err, int Dest, tRegs *Regs)
458 * \brief Demotes a process to a lower permission level
459 * \param Err Pointer to user's errno
460 * \param Dest New Permission Level
461 * \param Regs Pointer to user's register structure
463 int Proc_Demote(Uint *Err, int Dest, tRegs *Regs)
465 int cpl = Regs->cs & 3;
467 if(Dest > 3 || Dest < 0) {
478 // Change the Segment Registers
479 Regs->cs = (((Dest+1)<<4) | Dest) - 8;
480 Regs->ss = ((Dest+1)<<4) | Dest;
481 // Check if the GP Segs are GDT, then change them
482 if(!(Regs->ds & 4)) Regs->ds = ((Dest+1)<<4) | Dest;
483 if(!(Regs->es & 4)) Regs->es = ((Dest+1)<<4) | Dest;
484 if(!(Regs->fs & 4)) Regs->fs = ((Dest+1)<<4) | Dest;
485 if(!(Regs->gs & 4)) Regs->gs = ((Dest+1)<<4) | Dest;
491 * \fn void Proc_Scheduler(int CPU)
492 * \brief Swap current thread and clears dead threads
494 void Proc_Scheduler(int CPU)
499 // If the spinlock is set, let it complete
500 if(giThreadListLock) return;
502 // Clear Delete Queue
503 while(gDeleteThreads)
505 thread = gDeleteThreads->Next;
506 if(gDeleteThreads->IsLocked) { // Only free if structure is unused
507 gDeleteThreads->Status = THREAD_STAT_NULL;
508 free( gDeleteThreads );
510 gDeleteThreads = thread;
513 // Check if there is any tasks running
514 if(giNumActiveThreads == 0) {
515 Log("No Active threads, sleeping");
516 __asm__ __volatile__ ("hlt");
520 // Reduce remaining quantum and continue timeslice if non-zero
521 if(gCurrentThread->Remaining--) return;
522 // Reset quantum for next call
523 gCurrentThread->Remaining = gCurrentThread->Quantum;
526 __asm__ __volatile__ ("mov %%esp, %0":"=r"(esp));
527 __asm__ __volatile__ ("mov %%ebp, %0":"=r"(ebp));
529 if(eip == SWITCH_MAGIC) return; // Check if a switch happened
531 // Save machine state
532 gCurrentThread->SavedState.ESP = esp;
533 gCurrentThread->SavedState.EBP = ebp;
534 gCurrentThread->SavedState.EIP = eip;
537 thread = Threads_GetNextToRun(CPU);
541 Warning("Hmm... Threads_GetNextToRun returned NULL, I don't think this should happen.\n");
545 #if DEBUG_TRACE_SWITCH
546 Log("Switching to task %i, CR3 = 0x%x, EIP = %p",
548 thread->MemState.CR3,
549 thread->SavedState.EIP
553 // Set current thread
554 gCurrentThread = thread;
556 // Update Kernel Stack pointer
557 gTSSs[CPU].ESP0 = thread->KernelStack;
560 __asm__ __volatile__ ("mov %0, %%cr3"::"a"(gCurrentThread->MemState.CR3));
562 __asm__ __volatile__ (
566 "a"(SWITCH_MAGIC), "b"(gCurrentThread->SavedState.ESP),
567 "d"(gCurrentThread->SavedState.EBP), "c"(gCurrentThread->SavedState.EIP));
568 for(;;); // Shouldn't reach here