X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Farch%2Fx86%2Fproc.c;h=2f59472a1a0e3411cece31446239d21d0e3bdfd3;hb=4b80e9762374558077e28e321d75029645529a45;hp=62980674215919ebd538b0ee2ce2f0403e2c1795;hpb=7d2f8503550796a3cd42d01f944c2baa49a2fcfa;p=tpg%2Facess2.git diff --git a/Kernel/arch/x86/proc.c b/Kernel/arch/x86/proc.c index 62980674..2f59472a 100644 --- a/Kernel/arch/x86/proc.c +++ b/Kernel/arch/x86/proc.c @@ -22,11 +22,12 @@ // === IMPORTS === extern tGDT gGDT[]; extern tIDT gIDT[]; -extern void APStartup(); // 16-bit AP startup code -extern Uint GetEIP(); // start.asm +extern void APStartup(void); // 16-bit AP startup code +extern Uint GetEIP(void); // start.asm +extern int GetCPUNum(void); // start.asm extern Uint32 gaInitPageDir[1024]; // start.asm extern void Kernel_Stack_Top; -extern volatile int giThreadListLock; +extern tSpinlock glThreadListLock; extern int giNumCPUs; extern int giNextTID; extern int giTotalTickets; @@ -36,22 +37,24 @@ extern tThread *gActiveThreads; extern tThread *gSleepingThreads; extern tThread *gDeleteThreads; extern tThread *Threads_GetNextToRun(int CPU); -extern void Threads_Dump(); +extern void Threads_Dump(void); extern tThread *Threads_CloneTCB(Uint *Err, Uint Flags); -extern void Isr8(); // Double Fault +extern void Isr8(void); // Double Fault +extern void Proc_ReturnToUser(void); // === PROTOTYPES === -void ArchThreads_Init(); +void ArchThreads_Init(void); #if USE_MP void MP_StartAP(int CPU); void MP_SendIPI(Uint8 APICID, int Vector, int DeliveryMode); #endif -void Proc_Start(); -tThread *Proc_GetCurThread(); -void Proc_ChangeStack(); +void Proc_Start(void); +tThread *Proc_GetCurThread(void); +void Proc_ChangeStack(void); int Proc_Clone(Uint *Err, Uint Flags); void Proc_StartProcess(Uint16 SS, Uint Stack, Uint Flags, Uint16 CS, Uint IP); -void Proc_Scheduler(); +void Proc_CallFaultHandler(tThread *Thread); +void Proc_Scheduler(int CPU); // === GLOBALS === // --- Multiprocessing --- @@ -84,10 +87,10 @@ tTSS gDoubleFault_TSS = { // === CODE === /** - * \fn void ArchThreads_Init() + * \fn void ArchThreads_Init(void) * \brief Starts the process scheduler */ -void ArchThreads_Init() +void ArchThreads_Init(void) { Uint pos = 0; @@ -371,33 +374,34 @@ void MP_SendIPI(Uint8 APICID, int Vector, int DeliveryMode) #endif /** - * \fn void Proc_Start() + * \fn void Proc_Start(void) * \brief Start process scheduler */ -void Proc_Start() +void Proc_Start(void) { // Start Interrupts (and hence scheduler) __asm__ __volatile__("sti"); } /** - * \fn tThread *Proc_GetCurThread() + * \fn tThread *Proc_GetCurThread(void) * \brief Gets the current thread */ -tThread *Proc_GetCurThread() +tThread *Proc_GetCurThread(void) { #if USE_MP - return gaCPUs[ gaAPIC_to_CPU[gpMP_LocalAPIC->ID.Val&0xFF] ].Current; + //return gaCPUs[ gaAPIC_to_CPU[gpMP_LocalAPIC->ID.Val&0xFF] ].Current; + return gaCPUs[ GetCPUNum() ].Current; #else return gCurrentThread; #endif } /** - * \fn void Proc_ChangeStack() + * \fn void Proc_ChangeStack(void) * \brief Swaps the current stack for a new one (in the proper stack reigon) */ -void Proc_ChangeStack() +void Proc_ChangeStack(void) { Uint esp, ebp; Uint tmpEbp, oldEsp; @@ -515,10 +519,10 @@ int Proc_Clone(Uint *Err, Uint Flags) } /** - * \fn int Proc_SpawnWorker() + * \fn int Proc_SpawnWorker(void) * \brief Spawns a new worker thread */ -int Proc_SpawnWorker() +int Proc_SpawnWorker(void) { tThread *new, *cur; Uint eip, esp, ebp; @@ -563,10 +567,10 @@ int Proc_SpawnWorker() } /** - * \fn Uint Proc_MakeUserStack() + * \fn Uint Proc_MakeUserStack(void) * \brief Creates a new user stack */ -Uint Proc_MakeUserStack() +Uint Proc_MakeUserStack(void) { int i; Uint base = USER_STACK_TOP - USER_STACK_SZ; @@ -585,7 +589,6 @@ Uint Proc_MakeUserStack() return base + USER_STACK_SZ; } - /** * \fn void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize) * \brief Starts a user task @@ -597,19 +600,30 @@ void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char ** Uint delta; Uint16 ss, cs; - LOG("stack = 0x%x", stack); + //Log("stack = %p", stack); // Copy Arguments - stack = (void*)( (Uint)stack - DataSize ); + stack -= DataSize/sizeof(*stack); memcpy( stack, ArgV, DataSize ); - // Adjust Arguments and environment - delta = (Uint)stack - (Uint)ArgV; - ArgV = (char**)stack; - for( i = 0; ArgV[i]; i++ ) ArgV[i] += delta; - i ++; - EnvP = &ArgV[i]; - for( i = 0; EnvP[i]; i++ ) EnvP[i] += delta; + //Log("stack = %p", stack); + + if( DataSize ) + { + // Adjust Arguments and environment + delta = (Uint)stack - (Uint)ArgV; + ArgV = (char**)stack; + for( i = 0; ArgV[i]; i++ ) + ArgV[i] += delta; + i ++; + + // Do we care about EnvP? + if( EnvP ) { + EnvP = &ArgV[i]; + for( i = 0; EnvP[i]; i++ ) + EnvP[i] += delta; + } + } // User Mode Segments ss = 0x23; cs = 0x1B; @@ -693,6 +707,18 @@ int Proc_Demote(Uint *Err, int Dest, tRegs *Regs) return 0; } +/** + * \brief Calls a signal handler in user mode + * \note Used for signals + */ +void Proc_CallFaultHandler(tThread *Thread) +{ + // Rewinds the stack and calls the user function + // Never returns + __asm__ __volatile__ ("mov %0, %%ebp;\n\tcall Proc_ReturnToUser" :: "r"(Thread->FaultHandler)); + for(;;); +} + /** * \fn void Proc_Scheduler(int CPU) * \brief Swap current thread and clears dead threads @@ -703,7 +729,7 @@ void Proc_Scheduler(int CPU) tThread *thread; // If the spinlock is set, let it complete - if(giThreadListLock) return; + if(IS_LOCKED(&glThreadListLock)) return; // Clear Delete Queue while(gDeleteThreads)