2 * AcessOS Microkernel Version
16 #define DEBUG_TRACE_SWITCH 0
17 #define DEBUG_DISABLE_DOUBLEFAULT 1
20 #define SWITCH_MAGIC 0xFF5317C8 // FF SWITCH - There is no code in this area
22 #define TIMER_BASE 1193182
23 #define TIMER_DIVISOR 11932 //~100Hz
30 Uint8 State; // 0: Unavaliable, 1: Idle, 2: Active
40 extern void APWait(void); // 16-bit AP pause code
41 extern void APStartup(void); // 16-bit AP startup code
42 extern Uint GetEIP(void); // start.asm
43 extern int GetCPUNum(void); // start.asm
44 extern Uint32 gaInitPageDir[1024]; // start.asm
45 extern char Kernel_Stack_Top[];
46 extern tShortSpinlock glThreadListLock;
49 extern tThread gThreadZero;
50 extern void Isr8(void); // Double Fault
51 extern void Proc_ReturnToUser(tVAddr Handler, Uint Argument, tVAddr KernelStack);
54 void ArchThreads_Init(void);
56 void MP_StartAP(int CPU);
57 void MP_SendIPI(Uint8 APICID, int Vector, int DeliveryMode);
59 //void Proc_Start(void);
60 //tThread *Proc_GetCurThread(void);
61 void Proc_ChangeStack(void);
62 // int Proc_Clone(Uint *Err, Uint Flags);
63 Uint Proc_MakeUserStack(void);
64 void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize);
65 void Proc_StartProcess(Uint16 SS, Uint Stack, Uint Flags, Uint16 CS, Uint IP);
66 int Proc_Demote(Uint *Err, int Dest, tRegs *Regs);
67 void Proc_CallFaultHandler(tThread *Thread);
68 void Proc_Scheduler(int CPU);
71 // --- Multiprocessing ---
73 volatile int giNumInitingCPUs = 0;
74 tMPInfo *gMPFloatPtr = NULL;
75 volatile Uint32 giMP_TimerCount; // Start Count for Local APIC Timer
76 tAPIC *gpMP_LocalAPIC = NULL;
77 Uint8 gaAPIC_to_CPU[256] = {0};
78 tCPU gaCPUs[MAX_CPUS];
79 tTSS gaTSSs[MAX_CPUS]; // TSS Array
80 int giProc_BootProcessorID = 0;
82 tThread *gCurrentThread = NULL;
83 tThread *gpIdleThread = NULL;
86 Uint32 *gPML4s[4] = NULL;
88 tTSS *gTSSs = NULL; // Pointer to TSS array
90 // --- Error Recovery ---
91 char gaDoubleFaultStack[1024] __attribute__ ((section(".padata")));
92 tTSS gDoubleFault_TSS = {
93 .ESP0 = (Uint)&gaDoubleFaultStack[1024],
95 .CR3 = (Uint)gaInitPageDir - KERNEL_BASE,
97 .ESP = (Uint)&gaDoubleFaultStack[1024],
98 .CS = 0x08, .SS = 0x10,
99 .DS = 0x10, .ES = 0x10,
100 .FS = 0x10, .GS = 0x10,
105 * \fn void ArchThreads_Init(void)
106 * \brief Starts the process scheduler
108 void ArchThreads_Init(void)
115 // Mark BSP as active
118 // -- Initialise Multiprocessing
119 // Find MP Floating Table
120 // - EBDA/Last 1Kib (640KiB)
121 for(pos = KERNEL_BASE|0x9F000; pos < (KERNEL_BASE|0xA0000); pos += 16) {
122 if( *(Uint*)(pos) == MPPTR_IDENT ) {
123 Log("Possible %p", pos);
124 if( ByteSum((void*)pos, sizeof(tMPInfo)) != 0 ) continue;
125 gMPFloatPtr = (void*)pos;
129 // - Last KiB (512KiB base mem)
131 for(pos = KERNEL_BASE|0x7F000; pos < (KERNEL_BASE|0x80000); 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;
142 for(pos = KERNEL_BASE|0xE0000; pos < (KERNEL_BASE|0x100000); pos += 16) {
143 if( *(Uint*)(pos) == MPPTR_IDENT ) {
144 Log("Possible %p", pos);
145 if( ByteSum((void*)pos, sizeof(tMPInfo)) != 0 ) continue;
146 gMPFloatPtr = (void*)pos;
152 // If the MP Table Exists, parse it
157 Log("gMPFloatPtr = %p", gMPFloatPtr);
158 Log("*gMPFloatPtr = {");
159 Log("\t.Sig = 0x%08x", gMPFloatPtr->Sig);
160 Log("\t.MPConfig = 0x%08x", gMPFloatPtr->MPConfig);
161 Log("\t.Length = 0x%02x", gMPFloatPtr->Length);
162 Log("\t.Version = 0x%02x", gMPFloatPtr->Version);
163 Log("\t.Checksum = 0x%02x", gMPFloatPtr->Checksum);
164 Log("\t.Features = [0x%02x,0x%02x,0x%02x,0x%02x,0x%02x]",
165 gMPFloatPtr->Features[0], gMPFloatPtr->Features[1],
166 gMPFloatPtr->Features[2], gMPFloatPtr->Features[3],
167 gMPFloatPtr->Features[4]
171 mptable = (void*)( KERNEL_BASE|gMPFloatPtr->MPConfig );
172 Log("mptable = %p", mptable);
174 Log("\t.Sig = 0x%08x", mptable->Sig);
175 Log("\t.BaseTableLength = 0x%04x", mptable->BaseTableLength);
176 Log("\t.SpecRev = 0x%02x", mptable->SpecRev);
177 Log("\t.Checksum = 0x%02x", mptable->Checksum);
178 Log("\t.OEMID = '%8c'", mptable->OemID);
179 Log("\t.ProductID = '%8c'", mptable->ProductID);
180 Log("\t.OEMTablePtr = %p'", mptable->OEMTablePtr);
181 Log("\t.OEMTableSize = 0x%04x", mptable->OEMTableSize);
182 Log("\t.EntryCount = 0x%04x", mptable->EntryCount);
183 Log("\t.LocalAPICMemMap = 0x%08x", mptable->LocalAPICMemMap);
184 Log("\t.ExtendedTableLen = 0x%04x", mptable->ExtendedTableLen);
185 Log("\t.ExtendedTableChecksum = 0x%02x", mptable->ExtendedTableChecksum);
188 gpMP_LocalAPIC = (void*)MM_MapHWPages(mptable->LocalAPICMemMap, 1);
190 ents = mptable->Entries;
193 for( i = 0; i < mptable->EntryCount; i ++ )
200 Log("%i: Processor", i);
201 Log("\t.APICID = %i", ents->Proc.APICID);
202 Log("\t.APICVer = 0x%02x", ents->Proc.APICVer);
203 Log("\t.CPUFlags = 0x%02x", ents->Proc.CPUFlags);
204 Log("\t.CPUSignature = 0x%08x", ents->Proc.CPUSignature);
205 Log("\t.FeatureFlags = 0x%08x", ents->Proc.FeatureFlags);
208 if( !(ents->Proc.CPUFlags & 1) ) {
213 // Check if there is too many processors
214 if(giNumCPUs >= MAX_CPUS) {
215 giNumCPUs ++; // If `giNumCPUs` > MAX_CPUS later, it will be clipped
219 // Initialise CPU Info
220 gaAPIC_to_CPU[ents->Proc.APICID] = giNumCPUs;
221 gaCPUs[giNumCPUs].APICID = ents->Proc.APICID;
222 gaCPUs[giNumCPUs].State = 0;
226 if( ents->Proc.CPUFlags & 2 ) {
227 giProc_BootProcessorID = giNumCPUs-1;
236 Log("\t.ID = %i", ents->Bus.ID);
237 Log("\t.TypeString = '%6C'", ents->Bus.TypeString);
241 Log("%i: I/O APIC", i);
242 Log("\t.ID = %i", ents->IOAPIC.ID);
243 Log("\t.Version = 0x%02x", ents->IOAPIC.Version);
244 Log("\t.Flags = 0x%02x", ents->IOAPIC.Flags);
245 Log("\t.Addr = 0x%08x", ents->IOAPIC.Addr);
247 case 3: // I/O Interrupt Assignment
249 Log("%i: I/O Interrupt Assignment", i);
250 Log("\t.IntType = %i", ents->IOInt.IntType);
251 Log("\t.Flags = 0x%04x", ents->IOInt.Flags);
252 Log("\t.SourceBusID = 0x%02x", ents->IOInt.SourceBusID);
253 Log("\t.SourceBusIRQ = 0x%02x", ents->IOInt.SourceBusIRQ);
254 Log("\t.DestAPICID = 0x%02x", ents->IOInt.DestAPICID);
255 Log("\t.DestAPICIRQ = 0x%02x", ents->IOInt.DestAPICIRQ);
257 case 4: // Local Interrupt Assignment
259 Log("%i: Local Interrupt Assignment", i);
260 Log("\t.IntType = %i", ents->LocalInt.IntType);
261 Log("\t.Flags = 0x%04x", ents->LocalInt.Flags);
262 Log("\t.SourceBusID = 0x%02x", ents->LocalInt.SourceBusID);
263 Log("\t.SourceBusIRQ = 0x%02x", ents->LocalInt.SourceBusIRQ);
264 Log("\t.DestLocalAPICID = 0x%02x", ents->LocalInt.DestLocalAPICID);
265 Log("\t.DestLocalAPICIRQ = 0x%02x", ents->LocalInt.DestLocalAPICIRQ);
268 Log("%i: Unknown (%i)", i, ents->Type);
272 ents = (void*)( (Uint)ents + entSize );
275 if( giNumCPUs > MAX_CPUS ) {
276 Warning("Too many CPUs detected (%i), only using %i of them", giNumCPUs, MAX_CPUS);
277 giNumCPUs = MAX_CPUS;
282 Log("No MP Table was found, assuming uniprocessor\n");
289 MM_FinishVirtualInit();
292 #if !DEBUG_DISABLE_DOUBLEFAULT
293 // Initialise Double Fault TSS
294 gGDT[5].BaseLow = (Uint)&gDoubleFault_TSS & 0xFFFF;
295 gGDT[5].BaseMid = (Uint)&gDoubleFault_TSS >> 16;
296 gGDT[5].BaseHi = (Uint)&gDoubleFault_TSS >> 24;
298 // Set double fault IDT to use the new TSS
299 gIDT[8].OffsetLo = 0;
301 gIDT[8].Flags = 0x8500;
302 gIDT[8].OffsetHi = 0;
305 // Set timer frequency
306 outb(0x43, 0x34); // Set Channel 0, Low/High, Rate Generator
307 outb(0x40, TIMER_DIVISOR&0xFF); // Low Byte of Divisor
308 outb(0x40, (TIMER_DIVISOR>>8)&0xFF); // High Byte
310 Log("Timer Frequency %i.%03i Hz",
311 TIMER_BASE/TIMER_DIVISOR,
312 ((Uint64)TIMER_BASE*1000/TIMER_DIVISOR)%1000
316 // Get the count setting for APIC timer
317 Log("Determining APIC Count");
318 __asm__ __volatile__ ("sti");
319 while( giMP_TimerCount == 0 ) __asm__ __volatile__ ("hlt");
320 __asm__ __volatile__ ("cli");
321 Log("APIC Count %i", giMP_TimerCount);
323 Uint64 freq = giMP_TimerCount;
324 freq /= TIMER_DIVISOR;
326 if( (freq /= 1000) < 2*1000)
327 Log("Bus Frequency %i KHz", freq);
328 else if( (freq /= 1000) < 2*1000)
329 Log("Bus Frequency %i MHz", freq);
330 else if( (freq /= 1000) < 2*1000)
331 Log("Bus Frequency %i GHz", freq);
333 Log("Bus Frequency %i THz", freq);
336 // Initialise Normal TSS(s)
337 for(pos=0;pos<giNumCPUs;pos++)
342 gTSSs[pos].SS0 = 0x10;
343 gTSSs[pos].ESP0 = 0; // Set properly by scheduler
344 gGDT[6+pos].BaseLow = ((Uint)(&gTSSs[pos])) & 0xFFFF;
345 gGDT[6+pos].BaseMid = ((Uint)(&gTSSs[pos]) >> 16) & 0xFFFF;
346 gGDT[6+pos].BaseHi = ((Uint)(&gTSSs[pos])) >> 24;
351 // Load the BSP's TSS
352 __asm__ __volatile__ ("ltr %%ax"::"a"(0x30));
353 // Set Current Thread and CPU Number in DR0 and DR1
354 __asm__ __volatile__ ("mov %0, %%db0"::"r"(&gThreadZero));
355 __asm__ __volatile__ ("mov %0, %%db1"::"r"(0));
358 gaCPUs[0].Current = &gThreadZero;
360 gCurrentThread = &gThreadZero;
362 gThreadZero.CurCPU = 0;
365 gThreadZero.MemState.PDP[0] = 0;
366 gThreadZero.MemState.PDP[1] = 0;
367 gThreadZero.MemState.PDP[2] = 0;
369 gThreadZero.MemState.CR3 = (Uint)gaInitPageDir - KERNEL_BASE;
372 // Create Per-Process Data Block
373 if( !MM_Allocate(MM_PPD_CFG) )
375 Panic("OOM - No space for initiali Per-Process Config");
383 void MP_StartAP(int CPU)
385 Log("Starting AP %i (APIC %i)", CPU, gaCPUs[CPU].APICID);
387 // Set location of AP startup code and mark for a warm restart
388 *(Uint16*)(KERNEL_BASE|0x467) = (Uint)&APWait - (KERNEL_BASE|0xFFFF0);
389 *(Uint16*)(KERNEL_BASE|0x469) = 0xFFFF;
390 outb(0x70, 0x0F); outb(0x71, 0x0A); // Set warm reset flag
391 MP_SendIPI(gaCPUs[CPU].APICID, 0, 5); // Init IPI
394 inb(0x80); inb(0x80); inb(0x80); inb(0x80);
396 // TODO: Use a better address, preferably registered with the MM
397 // - MM_AllocDMA mabye?
399 *(Uint8*)(KERNEL_BASE|0x11000) = 0xEA; // Far JMP
400 *(Uint16*)(KERNEL_BASE|0x11001) = (Uint)&APStartup - (KERNEL_BASE|0xFFFF0); // IP
401 *(Uint16*)(KERNEL_BASE|0x11003) = 0xFFFF; // CS
402 // Send a Startup-IPI to make the CPU execute at 0x11000 (which we
404 MP_SendIPI(gaCPUs[CPU].APICID, 0x11, 6); // StartupIPI
410 * \brief Send an Inter-Processor Interrupt
411 * \param APICID Processor's Local APIC ID
412 * \param Vector Argument of some kind
413 * \param DeliveryMode Type of signal?
415 void MP_SendIPI(Uint8 APICID, int Vector, int DeliveryMode)
420 val = (Uint)APICID << 24;
421 Log("*%p = 0x%08x", &gpMP_LocalAPIC->ICR[1], val);
422 gpMP_LocalAPIC->ICR[1].Val = val;
424 val = ((DeliveryMode & 7) << 8) | (Vector & 0xFF);
425 Log("*%p = 0x%08x", &gpMP_LocalAPIC->ICR[0], val);
426 gpMP_LocalAPIC->ICR[0].Val = val;
431 * \fn void Proc_Start(void)
432 * \brief Start process scheduler
434 void Proc_Start(void)
442 for( i = 0; i < giNumCPUs; i ++ )
445 if(i) gaCPUs[i].Current = NULL;
448 if( (tid = Proc_Clone(0, 0)) == 0)
450 for(;;) HALT(); // Just yeilds
452 gaCPUs[i].IdleThread = Threads_GetThread(tid);
453 gaCPUs[i].IdleThread->ThreadName = "Idle Thread";
454 Threads_SetPriority( gaCPUs[i].IdleThread, -1 ); // Never called randomly
455 gaCPUs[i].IdleThread->Quantum = 1; // 1 slice quantum
459 if( i != giProc_BootProcessorID ) {
464 // BSP still should run the current task
465 gaCPUs[0].Current = &gThreadZero;
467 // Start interrupts and wait for APs to come up
468 Log("Waiting for APs to come up\n");
469 __asm__ __volatile__ ("sti");
470 while( giNumInitingCPUs ) __asm__ __volatile__ ("hlt");
473 if(Proc_Clone(0, 0) == 0)
475 gpIdleThread = Proc_GetCurThread();
476 gpIdleThread->ThreadName = strdup("Idle Thread");
477 Threads_SetPriority( gpIdleThread, -1 ); // Never called randomly
478 gpIdleThread->Quantum = 1; // 1 slice quantum
479 for(;;) HALT(); // Just yeilds
483 gCurrentThread = &gThreadZero;
485 // Start Interrupts (and hence scheduler)
486 __asm__ __volatile__("sti");
488 MM_FinishVirtualInit();
492 * \fn tThread *Proc_GetCurThread(void)
493 * \brief Gets the current thread
495 tThread *Proc_GetCurThread(void)
498 return gaCPUs[ GetCPUNum() ].Current;
500 return gCurrentThread;
505 * \fn void Proc_ChangeStack(void)
506 * \brief Swaps the current stack for a new one (in the proper stack reigon)
508 void Proc_ChangeStack(void)
512 Uint curBase, newBase;
514 __asm__ __volatile__ ("mov %%esp, %0":"=r"(esp));
515 __asm__ __volatile__ ("mov %%ebp, %0":"=r"(ebp));
520 newBase = MM_NewKStack();
523 Panic("What the?? Unable to allocate space for initial kernel stack");
527 curBase = (Uint)&Kernel_Stack_Top;
529 LOG("curBase = 0x%x, newBase = 0x%x", curBase, newBase);
531 // Get ESP as a used size
533 LOG("memcpy( %p, %p, 0x%x )", (void*)(newBase - esp), (void*)(curBase - esp), esp );
535 memcpy( (void*)(newBase - esp), (void*)(curBase - esp), esp );
536 // Get ESP as an offset in the new stack
539 ebp = newBase - (curBase - ebp);
541 // Repair EBPs & Stack Addresses
542 // Catches arguments also, but may trash stack-address-like values
543 for(tmpEbp = esp; tmpEbp < newBase; tmpEbp += 4)
545 if(oldEsp < *(Uint*)tmpEbp && *(Uint*)tmpEbp < curBase)
546 *(Uint*)tmpEbp += newBase - curBase;
549 Proc_GetCurThread()->KernelStack = newBase;
551 __asm__ __volatile__ ("mov %0, %%esp"::"r"(esp));
552 __asm__ __volatile__ ("mov %0, %%ebp"::"r"(ebp));
556 * \fn int Proc_Clone(Uint *Err, Uint Flags)
557 * \brief Clone the current process
559 int Proc_Clone(Uint *Err, Uint Flags)
562 tThread *cur = Proc_GetCurThread();
565 __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp));
566 __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp));
568 newThread = Threads_CloneTCB(Err, Flags);
569 if(!newThread) return -1;
571 // Initialise Memory Space (New Addr space or kernel stack)
572 if(Flags & CLONE_VM) {
573 newThread->MemState.CR3 = MM_Clone();
575 if(newThread->MemState.CR3 == 0) {
576 Threads_Kill(newThread, -2);
579 newThread->KernelStack = cur->KernelStack;
581 Uint tmpEbp, oldEsp = esp;
585 # warning "PAE Unimplemented"
587 newThread->MemState.CR3 = cur->MemState.CR3;
591 newThread->KernelStack = MM_NewKStack();
593 if(newThread->KernelStack == 0) {
594 Threads_Kill(newThread, -2);
598 // Get ESP as a used size
599 esp = cur->KernelStack - esp;
601 memcpy( (void*)(newThread->KernelStack - esp), (void*)(cur->KernelStack - esp), esp );
602 // Get ESP as an offset in the new stack
603 esp = newThread->KernelStack - esp;
605 ebp = newThread->KernelStack - (cur->KernelStack - ebp);
607 // Repair EBPs & Stack Addresses
608 // Catches arguments also, but may trash stack-address-like values
609 for(tmpEbp = esp; tmpEbp < newThread->KernelStack; tmpEbp += 4)
611 if(oldEsp < *(Uint*)tmpEbp && *(Uint*)tmpEbp < cur->KernelStack)
612 *(Uint*)tmpEbp += newThread->KernelStack - cur->KernelStack;
616 // Save core machine state
617 newThread->SavedState.ESP = esp;
618 newThread->SavedState.EBP = ebp;
620 if(eip == SWITCH_MAGIC) {
621 __asm__ __volatile__ ("mov %0, %%db0" : : "r" (newThread) );
625 gpMP_LocalAPIC->EOI.Val = 0;
628 outb(0x20, 0x20); // ACK Timer and return as child
629 __asm__ __volatile__ ("sti"); // Restart interrupts
634 newThread->SavedState.EIP = eip;
636 // Lock list and add to active
637 Threads_AddActive(newThread);
639 return newThread->TID;
643 * \fn int Proc_SpawnWorker(void)
644 * \brief Spawns a new worker thread
646 int Proc_SpawnWorker(void)
651 cur = Proc_GetCurThread();
654 new = Threads_CloneThreadZero();
656 Warning("Proc_SpawnWorker - Out of heap space!\n");
659 // Create a new worker stack (in PID0's address space)
660 // - The stack is relocated by this function
661 new->KernelStack = MM_NewWorkerStack();
663 // Get ESP and EBP based in the new stack
664 __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp));
665 __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp));
666 esp = new->KernelStack - (cur->KernelStack - esp);
667 ebp = new->KernelStack - (cur->KernelStack - ebp);
669 // Save core machine state
670 new->SavedState.ESP = esp;
671 new->SavedState.EBP = ebp;
673 if(eip == SWITCH_MAGIC) {
674 __asm__ __volatile__ ("mov %0, %%db0" : : "r"(new));
678 gpMP_LocalAPIC->EOI.Val = 0;
681 outb(0x20, 0x20); // ACK Timer and return as child
682 __asm__ __volatile__ ("sti"); // Restart interrupts
687 new->SavedState.EIP = eip;
689 Threads_AddActive( new );
695 * \fn Uint Proc_MakeUserStack(void)
696 * \brief Creates a new user stack
698 Uint Proc_MakeUserStack(void)
701 Uint base = USER_STACK_TOP - USER_STACK_SZ;
703 // Check Prospective Space
704 for( i = USER_STACK_SZ >> 12; i--; )
705 if( MM_GetPhysAddr( base + (i<<12) ) != 0 )
708 if(i != -1) return 0;
710 // Allocate Stack - Allocate incrementally to clean up MM_Dump output
711 for( i = 0; i < USER_STACK_SZ/0x1000; i++ )
713 if( !MM_Allocate( base + (i<<12) ) )
715 Warning("OOM: Proc_MakeUserStack");
720 return base + USER_STACK_SZ;
724 * \fn void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize)
725 * \brief Starts a user task
727 void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize)
729 Uint *stack = (void*)Proc_MakeUserStack();
734 //Log("stack = %p", stack);
737 stack -= DataSize/sizeof(*stack);
738 memcpy( stack, ArgV, DataSize );
740 //Log("stack = %p", stack);
744 // Adjust Arguments and environment
745 delta = (Uint)stack - (Uint)ArgV;
746 ArgV = (char**)stack;
747 for( i = 0; ArgV[i]; i++ )
751 // Do we care about EnvP?
754 for( i = 0; EnvP[i]; i++ )
759 // User Mode Segments
760 ss = 0x23; cs = 0x1B;
763 *--stack = (Uint)EnvP;
764 *--stack = (Uint)ArgV;
765 *--stack = (Uint)ArgC;
768 *--stack = 0; // Return Address
770 Proc_StartProcess(ss, (Uint)stack, 0x202, cs, Entrypoint);
773 void Proc_StartProcess(Uint16 SS, Uint Stack, Uint Flags, Uint16 CS, Uint IP)
775 Uint *stack = (void*)Stack;
776 *--stack = SS; //Stack Segment
777 *--stack = Stack; //Stack Pointer
778 *--stack = Flags; //EFLAGS (Resvd (0x2) and IF (0x20))
779 *--stack = CS; //Code Segment
782 *--stack = 0xAAAAAAAA; // eax
783 *--stack = 0xCCCCCCCC; // ecx
784 *--stack = 0xDDDDDDDD; // edx
785 *--stack = 0xBBBBBBBB; // ebx
786 *--stack = 0xD1D1D1D1; // edi
787 *--stack = 0x54545454; // esp - NOT POPED
788 *--stack = 0x51515151; // esi
789 *--stack = 0xB4B4B4B4; // ebp
796 __asm__ __volatile__ (
797 "mov %%eax,%%esp;\n\t" // Set stack pointer
803 "iret;\n\t" : : "a" (stack));
808 * \fn int Proc_Demote(Uint *Err, int Dest, tRegs *Regs)
809 * \brief Demotes a process to a lower permission level
810 * \param Err Pointer to user's errno
811 * \param Dest New Permission Level
812 * \param Regs Pointer to user's register structure
814 int Proc_Demote(Uint *Err, int Dest, tRegs *Regs)
816 int cpl = Regs->cs & 3;
818 if(Dest > 3 || Dest < 0) {
829 // Change the Segment Registers
830 Regs->cs = (((Dest+1)<<4) | Dest) - 8;
831 Regs->ss = ((Dest+1)<<4) | Dest;
832 // Check if the GP Segs are GDT, then change them
833 if(!(Regs->ds & 4)) Regs->ds = ((Dest+1)<<4) | Dest;
834 if(!(Regs->es & 4)) Regs->es = ((Dest+1)<<4) | Dest;
835 if(!(Regs->fs & 4)) Regs->fs = ((Dest+1)<<4) | Dest;
836 if(!(Regs->gs & 4)) Regs->gs = ((Dest+1)<<4) | Dest;
842 * \brief Calls a signal handler in user mode
843 * \note Used for signals
845 void Proc_CallFaultHandler(tThread *Thread)
847 // Rewinds the stack and calls the user function
849 Proc_ReturnToUser( Thread->FaultHandler, Thread->CurFaultNum, Thread->KernelStack );
854 * \fn void Proc_Scheduler(int CPU)
855 * \brief Swap current thread and clears dead threads
857 void Proc_Scheduler(int CPU)
862 // If the spinlock is set, let it complete
863 if(IS_LOCKED(&glThreadListLock)) return;
865 // Get current thread
867 thread = gaCPUs[CPU].Current;
869 thread = gCurrentThread;
874 // Reduce remaining quantum and continue timeslice if non-zero
875 if( thread->Remaining-- )
877 // Reset quantum for next call
878 thread->Remaining = thread->Quantum;
881 __asm__ __volatile__ ( "mov %%esp, %0" : "=r" (esp) );
882 __asm__ __volatile__ ( "mov %%ebp, %0" : "=r" (ebp) );
884 if(eip == SWITCH_MAGIC) return; // Check if a switch happened
886 // Save machine state
887 thread->SavedState.ESP = esp;
888 thread->SavedState.EBP = ebp;
889 thread->SavedState.EIP = eip;
892 // Get next thread to run
893 thread = Threads_GetNextToRun(CPU, thread);
895 // No avaliable tasks, just go into low power mode (idle thread)
898 thread = gaCPUs[CPU].IdleThread;
899 Log("CPU %i Running Idle Thread", CPU);
901 thread = gpIdleThread;
905 // Set current thread
907 gaCPUs[CPU].Current = thread;
909 gCurrentThread = thread;
912 #if DEBUG_TRACE_SWITCH
913 Log("Switching to task %i, CR3 = 0x%x, EIP = %p",
915 thread->MemState.CR3,
916 thread->SavedState.EIP
920 #if USE_MP // MP Debug
921 Log("CPU = %i, Thread %p", CPU, thread);
924 // Update Kernel Stack pointer
925 gTSSs[CPU].ESP0 = thread->KernelStack-4;
928 if(thread->SavedState.ESP > 0xC0000000
929 && thread->SavedState.ESP < thread->KernelStack-0x2000) {
930 Log_Warning("Proc", "Possible bad ESP %p (PID %i)", thread->SavedState.ESP);
935 # error "Todo: Implement PAE Address space switching"
938 __asm__ __volatile__ (
939 "mov %4, %%cr3\n\t" // Set address space
940 "mov %1, %%esp\n\t" // Restore ESP
941 "mov %2, %%ebp\n\t" // and EBP
942 "jmp *%3" : : // And return to where we saved state (Proc_Clone or Proc_Scheduler)
943 "a"(SWITCH_MAGIC), "b"(thread->SavedState.ESP),
944 "d"(thread->SavedState.EBP), "c"(thread->SavedState.EIP),
945 "r"(thread->MemState.CR3)
948 for(;;); // Shouldn't reach here
952 EXPORT(Proc_SpawnWorker);