From aaf21fdf21db89849c419315e3d9ff956ed60d5d Mon Sep 17 00:00:00 2001 From: John Hodge Date: Tue, 31 Aug 2010 10:59:41 +0800 Subject: [PATCH] Fiddling with threading bugs (both Qemu and Bochs hate atm) - How the **** did I stuff up those header guards? --- Kernel/arch/x86/include/arch.h | 4 ++-- Kernel/arch/x86/mm_phys.c | 7 +++++-- Kernel/include/acess.h | 7 +++---- Kernel/messages.c | 5 +++++ Kernel/threads.c | 26 ++++++++++++++++---------- Modules/Storage/FDD/fdd.c | 5 +++-- 6 files changed, 34 insertions(+), 20 deletions(-) diff --git a/Kernel/arch/x86/include/arch.h b/Kernel/arch/x86/include/arch.h index eff85fc2..464023e7 100644 --- a/Kernel/arch/x86/include/arch.h +++ b/Kernel/arch/x86/include/arch.h @@ -61,8 +61,8 @@ static inline void SHORTLOCK(struct sShortSpinlock *Lock) { // int cpu = GetCPUNum() + 1; // Save interrupt state and clear interrupts - __ASM__ ("pushf;\n\tcli;\n\tpop %%eax" : "=a"(IF)); - IF &= 0x200; + __ASM__ ("pushf;\n\tpop %%eax\n\tcli" : "=a"(IF)); + IF &= 0x200; // AND out all but the interrupt flag // Wait for another CPU to release while(v) diff --git a/Kernel/arch/x86/mm_phys.c b/Kernel/arch/x86/mm_phys.c index 3abe4133..ef289acd 100644 --- a/Kernel/arch/x86/mm_phys.c +++ b/Kernel/arch/x86/mm_phys.c @@ -166,7 +166,8 @@ tPAddr MM_AllocPhys(void) for( ; gaSuperBitmap[a] == -1 && a >= 0; a-- ); if(a < 0) { Mutex_Release( &glPhysAlloc ); - Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p)", __builtin_return_address(0)); + Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p) - %lli/%lli used", + __builtin_return_address(0), giPhysAlloc, giPageCount); LEAVE('i', 0); return 0; } @@ -178,7 +179,9 @@ tPAddr MM_AllocPhys(void) if( indx < 0 ) { Mutex_Release( &glPhysAlloc ); - Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p)", __builtin_return_address(0)); + Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p) - %lli/%lli used (indx = %x)", + __builtin_return_address(0), giPhysAlloc, giPageCount, indx); + Log_Debug("PMem", "giLastPossibleFree = %lli", giLastPossibleFree); LEAVE('i', 0); return 0; } diff --git a/Kernel/include/acess.h b/Kernel/include/acess.h index b8ba67a5..9549ede3 100644 --- a/Kernel/include/acess.h +++ b/Kernel/include/acess.h @@ -1,16 +1,15 @@ /* * AcessOS Microkernel Version - * common.h + * acess.h */ -#ifndef _COMMON_H -#define _COMMON_H +#ifndef _ACESS_H +#define _ACESS_H #define NULL ((void*)0) #define PACKED __attribute__((packed)) #define UNUSED(x) UNUSED_##x __attribute__((unused)) #define offsetof(st, m) ((Uint)((char *)&((st *)(0))->m - (char *)0 )) -//#include #include #include #include "errno.h" diff --git a/Kernel/messages.c b/Kernel/messages.c index 4765d65e..03e225b8 100644 --- a/Kernel/messages.c +++ b/Kernel/messages.c @@ -6,6 +6,9 @@ #include #include +// === IMPORTS === +extern tShortSpinlock glThreadListLock; + // === CODE === /** * \fn int Proc_SendMessage(Uint *Err, Uint Dest, int Length, void *Data) @@ -60,7 +63,9 @@ int Proc_SendMessage(Uint *Err, Uint Dest, int Length, void *Data) SHORTREL(&thread->IsLocked); + SHORTLOCK(&glThreadListLock); Threads_Wake( thread ); + SHORTREL(&glThreadListLock); return 0; } diff --git a/Kernel/threads.c b/Kernel/threads.c index 9fd052b6..ab31e12e 100644 --- a/Kernel/threads.c +++ b/Kernel/threads.c @@ -91,7 +91,7 @@ void Threads_Init(void) // Create Initial Task gActiveThreads = &gThreadZero; gAllThreads = &gThreadZero; - //giFreeTickets = gThreadZero.NumTickets; + //giFreeTickets = gThreadZero.NumTickets; // Not needed, as ThreadZero is running giNumActiveThreads = 1; Proc_Start(); @@ -385,7 +385,7 @@ void Threads_Kill(tThread *Thread, int Status) tThread *prev; tMsg *msg; - // Kill all children + // TODO: Kill all children #if 0 { tThread *child; @@ -411,8 +411,8 @@ void Threads_Kill(tThread *Thread, int Status) prev = Threads_int_GetPrev( &gActiveThreads, Thread ); if(!prev) { Warning("Proc_Exit - Current thread is not on the active queue"); - SHORTREL( &Thread->IsLocked ); SHORTREL( &glThreadListLock ); + SHORTREL( &Thread->IsLocked ); return; } @@ -510,7 +510,7 @@ void Threads_Sleep(void) * \brief Wakes a sleeping/waiting thread up * \param Thread Thread to wake * \return Boolean Failure (Returns ERRNO) - * \note Should be called with the scheduler lock held + * \warning This should ONLY be called with task switches disabled */ int Threads_Wake(tThread *Thread) { @@ -525,19 +525,22 @@ int Threads_Wake(tThread *Thread) Log("Thread_Wake: Waking awake thread (%i)", Thread->TID); return -EALREADY; - case THREAD_STAT_SLEEPING: // TODO: Comment better + case THREAD_STAT_SLEEPING: // Remove from sleeping queue prev = Threads_int_GetPrev(&gSleepingThreads, Thread); prev->Next = Thread->Next; + // Add to active queue Thread->Next = gActiveThreads; gActiveThreads = Thread; + // Update bookkeeping giNumActiveThreads ++; giFreeTickets += Thread->NumTickets; #if DEBUG_TRACE_TICKETS Log("Threads_Wake: new giFreeTickets = %i", giFreeTickets); #endif + Thread->CurCPU = -1; Thread->Status = THREAD_STAT_ACTIVE; #if DEBUG_TRACE_STATE @@ -573,6 +576,7 @@ int Threads_WakeTID(tTID TID) SHORTLOCK( &glThreadListLock ); ret = Threads_Wake( thread ); SHORTREL( &glThreadListLock ); + //Log_Debug("Threads", "TID %i woke %i (%p)", Threads_GetTID(), TID, thread); return ret; } @@ -714,7 +718,8 @@ void Threads_Dump(void) { Log(" %i (%i) - %s (CPU %i)", thread->TID, thread->TGID, thread->ThreadName, thread->CurCPU); - Log(" State: %i", thread->Status); + if(thread->Status != THREAD_STAT_ACTIVE) + Log(" ERROR State (%i) != THREAD_STAT_ACTIVE (%i)", thread->Status, THREAD_STAT_ACTIVE); Log(" %i Tickets, Quantum %i", thread->NumTickets, thread->Quantum); Log(" KStack 0x%x", thread->KernelStack); } @@ -724,7 +729,7 @@ void Threads_Dump(void) { Log(" %i (%i) - %s (CPU %i)", thread->TID, thread->TGID, thread->ThreadName, thread->CurCPU); - Log(" State: %i", thread->Status); + Log(" State %i", thread->Status); Log(" %i Tickets, Quantum %i", thread->NumTickets, thread->Quantum); Log(" KStack 0x%x", thread->KernelStack); } @@ -746,6 +751,7 @@ tThread *Threads_GetNextToRun(int CPU, tThread *Last) SHORTLOCK( &glThreadListLock ); // Clear Delete Queue + // - I should probably put this in a worker thread to avoid calling free() in the scheduler while(gDeleteThreads) { thread = gDeleteThreads->Next; @@ -795,14 +801,14 @@ tThread *Threads_GetNextToRun(int CPU, tThread *Last) } #if DEBUG_TRACE_TICKETS else - LogF(" %p (%s)->Status = %i\n", Last, Last->ThreadName, Last->Status); + LogF(" %p (%s)->Status = %i (Released)\n", Last, Last->ThreadName, Last->Status); #endif Last->CurCPU = -1; } #if 1 number = 0; - for(thread=gActiveThreads;thread;thread=thread->Next) { + for(thread = gActiveThreads; thread; thread = thread->Next) { if(thread->CurCPU >= 0) continue; number += thread->NumTickets; } @@ -907,7 +913,7 @@ void Mutex_Acquire(tMutex *Mutex) } SHORTREL( &glThreadListLock ); SHORTREL( &Mutex->Protector ); - while(us->Status == THREAD_STAT_OFFSLEEP) HALT(); + while(us->Status == THREAD_STAT_OFFSLEEP) Threads_Yield(); // We're only woken when we get the lock } // Ooh, let's take it! diff --git a/Modules/Storage/FDD/fdd.c b/Modules/Storage/FDD/fdd.c index 974e5295..4a9ec2d4 100644 --- a/Modules/Storage/FDD/fdd.c +++ b/Modules/Storage/FDD/fdd.c @@ -398,7 +398,7 @@ int FDD_int_ReadWriteSector(Uint32 Disk, Uint64 SectorAddr, int Write, void *Buf // Read Data from DMA LOG("Setting DMA for read"); - DMA_SetChannel(2, 512, !Write); // Read 512 Bytes from channel 2 + DMA_SetChannel(2, 512, !Write); // Read/Write 512 Bytes from channel 2 LOG("Sending command"); @@ -441,7 +441,7 @@ int FDD_int_ReadWriteSector(Uint32 Disk, Uint64 SectorAddr, int Write, void *Buf st1 = FDD_int_GetByte(base); st2 = FDD_int_GetByte(base); - // Cylinder, Head and Sector (mutilated in some way + // Cylinder, Head and Sector (mutilated in some way) rcy = FDD_int_GetByte(base); rhe = FDD_int_GetByte(base); rse = FDD_int_GetByte(base); @@ -499,6 +499,7 @@ int FDD_int_ReadWriteSector(Uint32 Disk, Uint64 SectorAddr, int Write, void *Buf // Don't turn the motor off now, wait for a while gFDD_Devices[Disk].timer = Time_CreateTimer(MOTOR_OFF_DELAY, FDD_int_StopMotor, (void*)(tVAddr)Disk); + // Error check if( i < FDD_MAX_READWRITE_ATTEMPTS ) { LEAVE('i', 0); return 0; -- 2.20.1