X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Farch%2Fx86%2Fproc.c;h=badeaec91a7969146f58ca69b1cef49dab3b8229;hb=17aac974ab83a3521f2b49b8de33ae05a00fbe07;hp=b677dfdb388b26e2c97ad9504a425690fa8eaf71;hpb=9c61cf12758c0977ee1dc5791cba638fd3437ba6;p=tpg%2Facess2.git diff --git a/Kernel/arch/x86/proc.c b/Kernel/arch/x86/proc.c index b677dfdb..badeaec9 100644 --- a/Kernel/arch/x86/proc.c +++ b/Kernel/arch/x86/proc.c @@ -11,16 +11,21 @@ #if USE_MP # include #endif +#include // === FLAGS === -#define DEBUG_TRACE_SWITCH 1 +#define DEBUG_TRACE_SWITCH 0 #define DEBUG_DISABLE_DOUBLEFAULT 1 +#define DEBUG_VERY_SLOW_SWITCH 0 // === CONSTANTS === -#define SWITCH_MAGIC 0xFF5317C8 // FF SWITCH - There is no code in this area // Base is 1193182 #define TIMER_BASE 1193182 -#define TIMER_DIVISOR 11932 //~100Hz +#if DEBUG_VERY_SLOW_PERIOD +# define TIMER_DIVISOR 1193 //~10Hz switch, with 10 quantum = 1s per thread +#else +# define TIMER_DIVISOR 11932 //~100Hz +#endif // === TYPES === #if USE_MP @@ -41,7 +46,7 @@ extern void APWait(void); // 16-bit AP pause code extern void APStartup(void); // 16-bit AP startup code extern Uint GetEIP(void); // start.asm extern Uint GetEIP_Sched(void); // proc.asm -extern int GetCPUNum(void); // start.asm +extern void NewTaskHeader(tThread *Thread, void *Fcn, int nArgs, ...); // Actually takes cdecl args extern Uint32 gaInitPageDir[1024]; // start.asm extern char Kernel_Stack_Top[]; extern tShortSpinlock glThreadListLock; @@ -56,7 +61,7 @@ extern void IRQCommon_handled; // IRQCommon call return location extern void GetEIP_Sched_ret; // GetEIP call return location // === PROTOTYPES === -void ArchThreads_Init(void); +//void ArchThreads_Init(void); #if USE_MP void MP_StartAP(int CPU); void MP_SendIPI(Uint8 APICID, int Vector, int DeliveryMode); @@ -66,11 +71,11 @@ void MP_SendIPI(Uint8 APICID, int Vector, int DeliveryMode); void Proc_ChangeStack(void); // int Proc_Clone(Uint *Err, Uint Flags); Uint Proc_MakeUserStack(void); -void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize); +//void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize); void Proc_StartProcess(Uint16 SS, Uint Stack, Uint Flags, Uint16 CS, Uint IP); int Proc_Demote(Uint *Err, int Dest, tRegs *Regs); -void Proc_CallFaultHandler(tThread *Thread); -void Proc_DumpThreadCPUState(tThread *Thread); +//void Proc_CallFaultHandler(tThread *Thread); +//void Proc_DumpThreadCPUState(tThread *Thread); void Proc_Scheduler(int CPU); // === GLOBALS === @@ -88,9 +93,6 @@ tTSS gaTSSs[MAX_CPUS]; // TSS Array tThread *gCurrentThread = NULL; tThread *gpIdleThread = NULL; #endif -#if USE_PAE -Uint32 *gPML4s[4] = NULL; -#endif tTSS *gTSSs = NULL; // Pointer to TSS array tTSS gTSS0 = {0}; // --- Error Recovery --- @@ -366,18 +368,12 @@ void ArchThreads_Init(void) #endif gThreadZero.CurCPU = 0; - #if USE_PAE - gThreadZero.MemState.PDP[0] = 0; - gThreadZero.MemState.PDP[1] = 0; - gThreadZero.MemState.PDP[2] = 0; - #else gThreadZero.MemState.CR3 = (Uint)gaInitPageDir - KERNEL_BASE; - #endif // Create Per-Process Data Block if( !MM_Allocate(MM_PPD_CFG) ) { - Panic("OOM - No space for initiali Per-Process Config"); + Panic("OOM - No space for initial Per-Process Config"); } // Change Stacks @@ -475,7 +471,7 @@ void Proc_Start(void) while( giNumInitingCPUs ) __asm__ __volatile__ ("hlt"); #else // Create Idle Task - if(Proc_Clone(0, 0) == 0) + if(Proc_Clone(0) == 0) { gpIdleThread = Proc_GetCurThread(); gpIdleThread->ThreadName = strdup("Idle Thread"); @@ -561,7 +557,7 @@ void Proc_ChangeStack(void) * \fn int Proc_Clone(Uint *Err, Uint Flags) * \brief Clone the current process */ -int Proc_Clone(Uint *Err, Uint Flags) +int Proc_Clone(Uint Flags) { tThread *newThread; tThread *cur = Proc_GetCurThread(); @@ -570,7 +566,7 @@ int Proc_Clone(Uint *Err, Uint Flags) __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp)); __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp)); - newThread = Threads_CloneTCB(Err, Flags); + newThread = Threads_CloneTCB(NULL, Flags); if(!newThread) return -1; // Initialise Memory Space (New Addr space or kernel stack) @@ -586,11 +582,7 @@ int Proc_Clone(Uint *Err, Uint Flags) Uint tmpEbp, oldEsp = esp; // Set CR3 - #if USE_PAE - # warning "PAE Unimplemented" - #else newThread->MemState.CR3 = cur->MemState.CR3; - #endif // Create new KStack newThread->KernelStack = MM_NewKStack(); @@ -622,7 +614,7 @@ int Proc_Clone(Uint *Err, Uint Flags) newThread->SavedState.ESP = esp; newThread->SavedState.EBP = ebp; eip = GetEIP(); - if(eip == SWITCH_MAGIC) { + if(eip == 0) { //__asm__ __volatile__ ("mov %0, %%db0" : : "r" (newThread) ); #if USE_MP // ACK the interrupt @@ -648,12 +640,10 @@ int Proc_Clone(Uint *Err, Uint Flags) * \fn int Proc_SpawnWorker(void) * \brief Spawns a new worker thread */ -int Proc_SpawnWorker(void) +int Proc_SpawnWorker(void (*Fcn)(void*), void *Data) { - tThread *new, *cur; - Uint eip, esp, ebp; - - cur = Proc_GetCurThread(); + tThread *new; + Uint stack_contents[4]; // Create new thread new = Threads_CloneThreadZero(); @@ -661,36 +651,23 @@ int Proc_SpawnWorker(void) Warning("Proc_SpawnWorker - Out of heap space!\n"); return -1; } - // Create a new worker stack (in PID0's address space) - // - The stack is relocated by this function - new->KernelStack = MM_NewWorkerStack(); - // Get ESP and EBP based in the new stack - __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp)); - __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp)); - esp = new->KernelStack - (cur->KernelStack - esp); - ebp = new->KernelStack - (cur->KernelStack - ebp); + // Create the stack contents + stack_contents[3] = (Uint)Data; + stack_contents[2] = 1; + stack_contents[1] = (Uint)Fcn; + stack_contents[0] = (Uint)new; + // Create a new worker stack (in PID0's address space) + new->KernelStack = MM_NewWorkerStack(stack_contents, sizeof(stack_contents)); + // Save core machine state - new->SavedState.ESP = esp; - new->SavedState.EBP = ebp; - eip = GetEIP(); - if(eip == SWITCH_MAGIC) { - //__asm__ __volatile__ ("mov %0, %%db0" : : "r"(new)); - #if USE_MP - // ACK the interrupt - if(GetCPUNum()) - gpMP_LocalAPIC->EOI.Val = 0; - else - #endif - outb(0x20, 0x20); // ACK Timer and return as child - __asm__ __volatile__ ("sti"); // Restart interrupts - return 0; - } + new->SavedState.ESP = new->KernelStack - sizeof(stack_contents); + new->SavedState.EBP = 0; + new->SavedState.EIP = (Uint)NewTaskHeader; - // Set EIP as parent - new->SavedState.EIP = eip; // Mark as active + new->Status = THREAD_STAT_PREINIT; Threads_AddActive( new ); return new->TID; @@ -958,10 +935,6 @@ void Proc_Scheduler(int CPU) thread = gCurrentThread; #endif - // NOTE: - // 2011-04-05 - // Bug may be caused by DR0 not being maintained somewhere, hence - // login is getting loaded with the idle state. if( thread ) { tRegs *regs; @@ -975,7 +948,24 @@ void Proc_Scheduler(int CPU) __asm__ __volatile__ ( "mov %%esp, %0" : "=r" (esp) ); __asm__ __volatile__ ( "mov %%ebp, %0" : "=r" (ebp) ); eip = GetEIP(); - if(eip == SWITCH_MAGIC) return; // Check if a switch happened + if(eip == 0) { + __asm__ __volatile__ ( "nop" : : : "eax","ebx","ecx","edx","edi","esi"); + regs = (tRegs*)(ebp+(2+2)*4); // EBP,Ret + CPU,CurThread + #if USE_MP + thread = gaCPUs[CPU].Current; + #else + thread = gCurrentThread; + #endif + if(thread->bInstrTrace) { + regs->eflags |= 0x100; // Set TF +// LogF("Tracing enabled\n"); + Log("%p Scheduled (EIP = %p)", thread, thread->SavedState.EIP); + } + else + regs->eflags &= ~0x100; + + return; // Check if a switch happened + } // Save machine state thread->SavedState.ESP = esp; @@ -999,16 +989,6 @@ void Proc_Scheduler(int CPU) thread = Threads_GetNextToRun(CPU, thread); - #if DEBUG_TRACE_SWITCH - if(thread) { - Log("Switching to task %i, CR3 = 0x%x, EIP = %p", - thread->TID, - thread->MemState.CR3, - thread->SavedState.EIP - ); - } - #endif - // No avaliable tasks, just go into low power mode (idle thread) if(thread == NULL) { #if USE_MP @@ -1019,6 +999,17 @@ void Proc_Scheduler(int CPU) #endif } + #if DEBUG_TRACE_SWITCH + if(thread && thread != Proc_GetCurThread() ) { + Log("Switching to task %i(%s), CR3 = 0x%x, EIP = %p", + thread->TID, + thread->ThreadName, + thread->MemState.CR3, + thread->SavedState.EIP + ); + } + #endif + // Set current thread #if USE_MP gaCPUs[CPU].Current = thread; @@ -1041,27 +1032,28 @@ void Proc_Scheduler(int CPU) #endif if( thread->bInstrTrace ) { - Log("%p Scheduled", thread); + Log("%p Scheduled (EIP = %p)", thread, thread->SavedState.EIP); } - #if USE_PAE - # error "Todo: Implement PAE Address space switching" - #else // Set thread pointer __asm__ __volatile__("mov %0, %%db0\n\t" : : "r"(thread) ); // Switch threads __asm__ __volatile__ ( - "mov %4, %%cr3\n\t" // Set address space - "mov %1, %%esp\n\t" // Restore ESP - "mov %2, %%ebp\n\t" // and EBP - "or %5, 72(%%ebp)\n\t" // or trace flag to eflags (2+2+4+8+2)*4 - "jmp *%3" : : // And return to where we saved state (Proc_Clone or Proc_Scheduler) - "a"(SWITCH_MAGIC), "b"(thread->SavedState.ESP), - "d"(thread->SavedState.EBP), "c"(thread->SavedState.EIP), + "mov %3, %%cr3\n\t" // Set address space + "mov %0, %%esp\n\t" // Restore ESP + "mov %1, %%ebp\n\t" // and EBP + "test %4, %4\n\t" + "jz 1f\n\t" + "or %4, 72(%%ebp)\n\t" // or trace flag to eflags (2+2+4+8+2)*4 + "1:" + "xor %%eax, %%eax\n\t" + "jmp *%2" : : // And return to where we saved state (Proc_Clone or Proc_Scheduler) + "r"(thread->SavedState.ESP), + "r"(thread->SavedState.EBP), + "r"(thread->SavedState.EIP), "r"(thread->MemState.CR3), "r"(thread->bInstrTrace&&thread->SavedState.EIP==(Uint)&GetEIP_Sched_ret?0x100:0) ); - #endif for(;;); // Shouldn't reach here }