Cleaned up some more debug
[tpg/acess2.git] / Kernel / arch / x86 / mm_phys.c
index a6656e5..c7de9be 100644 (file)
@@ -1,7 +1,8 @@
 /*
- AcessOS Microkernel Version
- mm_phys.c
-*/
+ * Acess2
+ * - Physical memory manager
+ */
+#define DEBUG  1
 #include <common.h>
 #include <mboot.h>
 #include <mm_virt.h>
 extern void    gKernelEnd;
 
 // === PROTOTYPES ===
-Uint32 MM_AllocPhys();
-void   MM_RefPhys(Uint32 Addr);
-void   MM_DerefPhys(Uint32 Addr);
+tPAddr MM_AllocPhys();
+tPAddr MM_AllocPhysRange(int Pages);
+void   MM_RefPhys(tPAddr Addr);
+void   MM_DerefPhys(tPAddr Addr);
 
 // === GLOBALS ===
  int   giPhysAlloc = 0;
@@ -32,7 +34,7 @@ void MM_Install(tMBoot_Info *MBoot)
        
        // Initialise globals
        giPageCount = (MBoot->HighMem >> 2) + 256;      // HighMem is a kByte value
-       Log("giPageCount = %i", giPageCount);
+       //LOG("giPageCount = %i", giPageCount);
        
        // Get used page count
        kernelPages = (Uint)&gKernelEnd - KERNEL_BASE;
@@ -65,12 +67,13 @@ void MM_Install(tMBoot_Info *MBoot)
        }
        
        // Allocate References
-       Log("Reference Pages %i", (giPageCount*4+0xFFF)>>12);
+       //LOG("Reference Pages %i", (giPageCount*4+0xFFF)>>12);
        for(num = 0; num < (giPageCount*4+0xFFF)>>12; num++)
        {
                MM_Allocate( REFERENCE_BASE + (num<<12) );
        }
        
+       //LOG("Filling");
        // Fill references
        gaPageReferences = (void*)REFERENCE_BASE;
        memsetd(gaPageReferences, 1, kernelPages);
@@ -88,7 +91,7 @@ tPAddr MM_AllocPhys()
 {
         int    num = giPageCount / 32 / 32;
         int    a, b, c;
-       Uint32  ret;
+       tPAddr  ret;
        
        LOCK( &giPhysAlloc );
        
@@ -101,7 +104,6 @@ tPAddr MM_AllocPhys()
        }
        for(b=0;gaSuperBitmap[a]&(1<<b);b++);
        for(c=0;gaPageBitmap[a*32+b]&(1<<c);c++);
-       //for(c=0;gaPageReferences[a*32*32+b*32+c]>0;c++);
        
        // Mark page used
        if(gaPageReferences)
@@ -116,7 +118,47 @@ tPAddr MM_AllocPhys()
 
        // Release Spinlock
        RELEASE( &giPhysAlloc );
-       //LOG("ret = %x", ret);
+       
+       return ret;
+}
+
+/**
+ * \fn tPAddr MM_AllocPhysRange(int Pages)
+ * \brief Allocate a range of physical pages
+ * \param Pages        Number of pages to allocate
+ */
+tPAddr MM_AllocPhysRange(int Pages)
+{
+        int    num = giPageCount / 32 / 32;
+        int    a, b, c;
+       tPAddr  ret;
+       
+       LOCK( &giPhysAlloc );
+       
+       // Find free page
+       for(a=0;gaSuperBitmap[a]==-1&&a<num;a++);
+       if(a == num) {
+               RELEASE( &giPhysAlloc );
+               Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p)", __builtin_return_address(0));
+               return 0;
+       }
+       for(b=0;gaSuperBitmap[a]&(1<<b);b++);
+       for(c=0;gaPageBitmap[a*32+b]&(1<<c);c++);
+       
+       // Mark page used
+       if(gaPageReferences)
+               gaPageReferences[a*32*32+b*32+c] = 1;
+       gaPageBitmap[ a*32+b ] |= 1 << c;
+       
+       // Get address
+       ret = (a << 22) + (b << 17) + (c << 12);
+       
+       // Mark used block
+       if(gaPageBitmap[ a*32+b ] == -1)        gaSuperBitmap[a] |= 1 << b;
+
+       // Release Spinlock
+       RELEASE( &giPhysAlloc );
+       
        return ret;
 }
 
@@ -174,6 +216,7 @@ void MM_DerefPhys(tPAddr Addr)
        // Mark as free in bitmaps
        if( gaPageReferences[ Addr ] == 0 )
        {
+               //LOG("Freed 0x%x by %p\n", Addr<<12, __builtin_return_address(0));
                gaPageBitmap[ Addr / 32 ] &= ~(1 << (Addr&31));
                if(gaPageReferences[ Addr ] == 0)
                        gaSuperBitmap[ Addr >> 10 ] &= ~(1 << ((Addr >> 5)&31));

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