X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Farch%2Fx86_64%2Fproc.c;h=31dd80e373e4b321ff44ccf2a6823455be2ec393;hb=5255c9f07cb2e0e43cf283e256c964eaa7970c8e;hp=f2abb289e206a23e3c491c847a8ad4d8aee3c35c;hpb=19930d5a055b1804fbdc8188973b52092392bf8f;p=tpg%2Facess2.git diff --git a/Kernel/arch/x86_64/proc.c b/Kernel/arch/x86_64/proc.c index f2abb289..31dd80e3 100644 --- a/Kernel/arch/x86_64/proc.c +++ b/Kernel/arch/x86_64/proc.c @@ -12,14 +12,15 @@ #if USE_MP # include #endif +#include +#include // === FLAGS === #define DEBUG_TRACE_SWITCH 0 +//#define BREAK_ON_SWITCH 1 // Break into bochs debugger on a task switch // === CONSTANTS === #define SWITCH_MAGIC 0x55ECAFFF##FFFACE55 // There is no code in this area -// Base is 1193182 -#define TIMER_DIVISOR 11931 //~100Hz // === TYPES === typedef struct sCPU @@ -45,10 +46,10 @@ extern int giNumActiveThreads; extern tThread gThreadZero; extern void Threads_Dump(void); extern void Proc_ReturnToUser(void); -extern int GetCPUNum(void); +extern void Time_UpdateTimestamp(void); // === 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); @@ -59,11 +60,11 @@ void Proc_ChangeStack(void); // int Proc_Clone(Uint *Err, Uint Flags); // int Proc_SpawnWorker(void); 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 === @@ -287,10 +288,10 @@ void ArchThreads_Init(void) #if USE_MP } for(pos=0;pos>8)&0xFF); // High Byte + outb(0x40, PIT_TIMER_DIVISOR&0xFF); // Low Byte of Divisor + outb(0x40, (PIT_TIMER_DIVISOR>>8)&0xFF); // High Byte // Create Per-Process Data Block if( !MM_Allocate(MM_PPD_CFG) ) @@ -390,7 +391,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) { gaCPUs[0].IdleThread = Proc_GetCurThread(); gaCPUs[0].IdleThread->ThreadName = (char*)"Idle Thread"; @@ -476,10 +477,10 @@ void Proc_ChangeStack(void) } /** - * \fn int Proc_Clone(Uint *Err, Uint Flags) + * \fn int Proc_Clone(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(); @@ -488,7 +489,7 @@ int Proc_Clone(Uint *Err, Uint Flags) __asm__ __volatile__ ("mov %%rsp, %0": "=r"(rsp)); __asm__ __volatile__ ("mov %%rbp, %0": "=r"(rbp)); - newThread = Threads_CloneTCB(Err, Flags); + newThread = Threads_CloneTCB(NULL, Flags); if(!newThread) return -1; Log("Proc_Clone: newThread = %p", newThread); @@ -498,6 +499,7 @@ int Proc_Clone(Uint *Err, Uint Flags) Log("Proc_Clone: Cloning VM"); newThread->MemState.CR3 = MM_Clone(); newThread->KernelStack = cur->KernelStack; +// MAGIC_BREAK(); } else { Uint tmp_rbp, old_rsp = rsp; @@ -541,6 +543,8 @@ int Proc_Clone(Uint *Err, Uint Flags) rip = GetRIP(); if(rip == SWITCH_MAGIC) { outb(0x20, 0x20); // ACK Timer and return as child + __asm__ __volatile__ ("sti"); +// MAGIC_BREAK(); return 0; } @@ -589,13 +593,14 @@ int Proc_SpawnWorker(void) rip = GetRIP(); if(rip == SWITCH_MAGIC) { outb(0x20, 0x20); // ACK Timer and return as child + __asm__ __volatile__ ("sti"); return 0; } // Set EIP as parent new->SavedState.RIP = rip; // Mark as active - new->Status = THREAD_STAT_ACTIVE; + new->Status = THREAD_STAT_PREINIT; Threads_AddActive( new ); return new->TID; @@ -753,6 +758,9 @@ void Proc_Scheduler(int CPU) { Uint rsp, rbp, rip; tThread *thread; + + if( CPU == 0 ) + Time_UpdateTimestamp(); // If the spinlock is set, let it complete if(IS_LOCKED(&glThreadListLock)) return; @@ -784,7 +792,12 @@ void Proc_Scheduler(int CPU) thread->SavedState.UserCS = regs->CS; thread->SavedState.UserRIP = regs->RIP; } - + + #if BREAK_ON_SWITCH + { + tThread *oldthread = thread; + #endif + // Get next thread thread = Threads_GetNextToRun(CPU, thread); @@ -792,9 +805,18 @@ void Proc_Scheduler(int CPU) if(thread == NULL) { thread = gaCPUs[CPU].IdleThread; //Warning("Hmm... Threads_GetNextToRun returned NULL, I don't think this should happen.\n"); - //LogF("Zzzzz.\n"); - return; +// LogF("Zzzzz.\n"); + //return; + } + if(thread == NULL ) { + return ; + } + #if BREAK_ON_SWITCH + if( thread != oldthread ) { + MAGIC_BREAK(); } + } + #endif #if DEBUG_TRACE_SWITCH LogF("Switching to task %i, CR3 = 0x%x, RIP = %p", @@ -813,16 +835,15 @@ void Proc_Scheduler(int CPU) // Update Kernel Stack pointer gTSSs[CPU].RSP0 = thread->KernelStack-4; - // Set address space - __asm__ __volatile__ ("mov %0, %%cr3"::"a"(thread->MemState.CR3)); - // Switch threads __asm__ __volatile__ ( + "mov %4, %%cr3\n\t" "mov %1, %%rsp\n\t" // Restore RSP "mov %2, %%rbp\n\t" // and RBP "jmp *%3" : : // And return to where we saved state (Proc_Clone or Proc_Scheduler) - "a"(SWITCH_MAGIC), "b"(thread->SavedState.RSP), - "d"(thread->SavedState.RBP), "c"(thread->SavedState.RIP) + "a"(SWITCH_MAGIC), "r"(thread->SavedState.RSP), + "r"(thread->SavedState.RBP), "r"(thread->SavedState.RIP), + "r"(thread->MemState.CR3) ); for(;;); // Shouldn't reach here }