Fiddling with threading bugs (both Qemu and Bochs hate atm)
authorJohn Hodge <[email protected]>
Tue, 31 Aug 2010 02:59:41 +0000 (10:59 +0800)
committerJohn Hodge <[email protected]>
Tue, 31 Aug 2010 02:59:41 +0000 (10:59 +0800)
- How the **** did I stuff up those header guards?

Kernel/arch/x86/include/arch.h
Kernel/arch/x86/mm_phys.c
Kernel/include/acess.h
Kernel/messages.c
Kernel/threads.c
Modules/Storage/FDD/fdd.c

index eff85fc..464023e 100644 (file)
@@ -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)
index 3abe413..ef289ac 100644 (file)
@@ -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;
        }
index b8ba67a..9549ede 100644 (file)
@@ -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 <stdint.h>
 #include <arch.h>
 #include <stdarg.h>
 #include "errno.h"
index 4765d65..03e225b 100644 (file)
@@ -6,6 +6,9 @@
 #include <threads.h>
 #include <errno.h>
 
+// === 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;
 }
index 9fd052b..ab31e12 100644 (file)
@@ -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!
index 974e529..4a9ec2d 100644 (file)
@@ -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;

UCC git Repository :: git.ucc.asn.au