Kernel/armv7 - Fixing bugs, Proc_Clone works now
[tpg/acess2.git] / Kernel / arch / x86_64 / mm_phys.c
index 55fc68a..c2c215b 100644 (file)
@@ -8,6 +8,8 @@
 #include <mboot.h>
 #include <mm_virt.h>
 
+#define TRACE_REF      0
+
 enum eMMPhys_Ranges
 {
        MM_PHYS_16BIT,  // Does anything need this?
@@ -24,18 +26,27 @@ extern char gKernelEnd[];
 
 // === PROTOTYPES ===
 void   MM_InitPhys_Multiboot(tMBoot_Info *MBoot);
-tPAddr MM_AllocPhysRange(int Num, int Bits);
-tPAddr MM_AllocPhys(void);
-void   MM_RefPhys(tPAddr PAddr);
-void   MM_DerefPhys(tPAddr PAddr);
+//tPAddr       MM_AllocPhysRange(int Num, int Bits);
+//tPAddr       MM_AllocPhys(void);
+//void MM_RefPhys(tPAddr PAddr);
+//void MM_DerefPhys(tPAddr PAddr);
  int   MM_int_GetRangeID( tPAddr Addr );
 
+// === MACROS ===
+#define PAGE_ALLOC_TEST(__page)        (gaMainBitmap[(__page)>>6] & (1ULL << ((__page)&63)))
+#define PAGE_ALLOC_SET(__page)         do{gaMainBitmap[(__page)>>6] |= (1ULL << ((__page)&63));}while(0)
+#define PAGE_ALLOC_CLEAR(__page)       do{gaMainBitmap[(__page)>>6] &= ~(1ULL << ((__page)&63));}while(0)
+//#define PAGE_MULTIREF_TEST(__page)   (gaMultiBitmap[(__page)>>6] & (1ULL << ((__page)&63)))
+//#define PAGE_MULTIREF_SET(__page)    do{gaMultiBitmap[(__page)>>6] |= 1ULL << ((__page)&63);}while(0)
+//#define PAGE_MULTIREF_CLEAR(__page)  do{gaMultiBitmap[(__page)>>6] &= ~(1ULL << ((__page)&63));}while(0)
+
 // === GLOBALS ===
-tSpinlock      glPhysicalPages;
-Uint64 *gaSuperBitmap = (void*)MM_PAGE_SUPBMP; // 1 bit = 64 Pages, 16 MiB Per Word
+tMutex glPhysicalPages;
+Uint64 *gaSuperBitmap = (void*)MM_PAGE_SUPBMP; // 1 bit = 64 Pages, 16 MiB per Word
 Uint64 *gaMainBitmap = (void*)MM_PAGE_BITMAP;  // 1 bit = 1 Page, 256 KiB per Word
 Uint64 *gaMultiBitmap = (void*)MM_PAGE_DBLBMP; // Each bit means that the page is being used multiple times
 Uint32 *gaiPageReferences = (void*)MM_PAGE_COUNTS;     // Reference Counts
+void   **gapPageNodes = (void*)MM_PAGE_NODES;  // Reference Counts
 tPAddr giFirstFreePage;        // First possibly free page
 Uint64 giPhysRangeFree[NUM_MM_PHYS_RANGES];    // Number of free pages in each range
 Uint64 giPhysRangeFirst[NUM_MM_PHYS_RANGES];   // First free page in each range
@@ -172,7 +183,7 @@ void MM_InitPhys_Multiboot(tMBoot_Info *MBoot)
                                paddr = ent->Base;
                        }
                        
-                       Log(" MM_InitPhys_Multiboot: paddr=0x%x, avail=%i", paddr, avail);
+                       Log("MM_InitPhys_Multiboot: paddr=0x%x, avail=0x%x pg", paddr, avail);
                        
                        // Map
                        while( todo && avail --)
@@ -249,8 +260,8 @@ void MM_InitPhys_Multiboot(tMBoot_Info *MBoot)
                if(base & 63) {
                        Uint64  val = -1LL << (base & 63);
                        gaSuperBitmap[base / 64] &= ~val;
-                       size -= (base & 63);
-                       base += 64 - (base & 63);
+//                     size -= (base & 63);
+//                     base += 64 - (base & 63);
                }
        }
        
@@ -307,28 +318,28 @@ void MM_InitPhys_Multiboot(tMBoot_Info *MBoot)
 
 /**
  * \brief Allocate a contiguous range of physical pages with a maximum
- *        bit size of \a Bits
- * \param Num  Number of pages to allocate
- * \param Bits Maximum size of the physical address
- * \note If \a Bits is <= 0, any sized address is used (with preference
+ *        bit size of \a MaxBits
+ * \param Pages        Number of pages to allocate
+ * \param MaxBits      Maximum size of the physical address
+ * \note If \a MaxBits is <= 0, any sized address is used (with preference
  *       to higher addresses)
  */
-tPAddr MM_AllocPhysRange(int Num, int Bits)
+tPAddr MM_AllocPhysRange(int Pages, int MaxBits)
 {
        tPAddr  addr, ret;
         int    rangeID;
         int    nFree = 0, i;
        
-       ENTER("iNum iBits", Num, Bits);
+       ENTER("iPages iBits", Pages, MaxBits);
        
-       if( Bits <= 0 || Bits >= 64 )   // Speedup for the common case
+       if( MaxBits <= 0 || MaxBits >= 64 )     // Speedup for the common case
                rangeID = MM_PHYS_MAX;
        else
-               rangeID = MM_int_GetRangeID( (1LL << Bits) - 1 );
+               rangeID = MM_int_GetRangeID( (1LL << MaxBits) - 1 );
        
        LOG("rangeID = %i", rangeID);
        
-       LOCK(&glPhysicalPages);
+       Mutex_Acquire(&glPhysicalPages);
        
        // Check if the range actually has any free pages
        while(giPhysRangeFree[rangeID] == 0 && rangeID)
@@ -338,20 +349,20 @@ tPAddr MM_AllocPhysRange(int Num, int Bits)
        
        // What the? Oh, man. No free pages
        if(giPhysRangeFree[rangeID] == 0) {
-               RELEASE(&glPhysicalPages);
+               Mutex_Release(&glPhysicalPages);
                // TODO: Page out
                // ATM. Just Warning
                Warning(" MM_AllocPhysRange: Out of free pages");
                Log_Warning("Arch",
                        "Out of memory (unable to fulfil request for %i pages), zero remaining",
-                       Num
+                       Pages
                        );
                LEAVE('i', 0);
                return 0;
        }
        
        // Check if there is enough in the range
-       if(giPhysRangeFree[rangeID] >= Num)
+       if(giPhysRangeFree[rangeID] >= Pages)
        {
                LOG("{%i,0x%x -> 0x%x}",
                        giPhysRangeFree[rangeID],
@@ -389,30 +400,30 @@ tPAddr MM_AllocPhysRange(int Num, int Bits)
                        }
                        nFree ++;
                        addr ++;
-                       LOG("nFree(%i) == %i (0x%x)", nFree, Num, addr);
-                       if(nFree == Num)
+                       LOG("nFree(%i) == %i (0x%x)", nFree, Pages, addr);
+                       if(nFree == Pages)
                                break;
                }
                LOG("nFree = %i", nFree);
                // If we don't find a contiguous block, nFree will not be equal
                // to Num, so we set it to zero and do the expensive lookup.
-               if(nFree != Num)        nFree = 0;
+               if(nFree != Pages)      nFree = 0;
        }
        
        if( !nFree )
        {
                // Oops. ok, let's do an expensive check (scan down the list
                // until a free range is found)
-               nFree = 1;
-               addr = giPhysRangeLast[ rangeID ];
-               // TODO
-               RELEASE(&glPhysicalPages);
+//             nFree = 1;
+//             addr = giPhysRangeLast[ rangeID ];
+               // TODO: Expensive Check
+               Mutex_Release(&glPhysicalPages);
                // TODO: Page out
                // ATM. Just Warning
-               Warning(" MM_AllocPhysRange: Out of memory (unable to fulfil request for %i pages)", Num);
+               Warning(" MM_AllocPhysRange: Out of memory (unable to fulfil request for %i pages)", Pages);
                Log_Warning("Arch",
                        "Out of memory (unable to fulfil request for %i pages)",
-                       Num
+                       Pages   
                        );
                LEAVE('i', 0);
                return 0;
@@ -420,29 +431,36 @@ tPAddr MM_AllocPhysRange(int Num, int Bits)
        LOG("nFree = %i, addr = 0x%08x", nFree, addr);
        
        // Mark pages as allocated
-       addr -= Num;
-       for( i = 0; i < Num; i++, addr++ )
+       addr -= Pages;
+       for( i = 0; i < Pages; i++, addr++ )
        {
                gaMainBitmap[addr >> 6] |= 1LL << (addr & 63);
+               if( MM_GetPhysAddr( (tVAddr)&gaiPageReferences[addr] ) )
+                       gaiPageReferences[addr] = 1;
+//             Log("page %P refcount = %i", MM_GetRefCount(addr<<12)); 
                rangeID = MM_int_GetRangeID(addr << 12);
                giPhysRangeFree[ rangeID ] --;
                LOG("%x == %x", addr, giPhysRangeFirst[ rangeID ]);
                if(addr == giPhysRangeFirst[ rangeID ])
                        giPhysRangeFirst[ rangeID ] += 1;
        }
+       addr -= Pages;
        ret = addr;     // Save the return address
        
        // Update super bitmap
-       Num += addr & (64-1);
+       Pages += addr & (64-1);
        addr &= ~(64-1);
-       Num = (Num + (64-1)) & ~(64-1);
-       for( i = 0; i < Num/64; i++ )
+       Pages = (Pages + (64-1)) & ~(64-1);
+       for( i = 0; i < Pages/64; i++ )
        {
                if( gaMainBitmap[ addr >> 6 ] + 1 == 0 )
                        gaSuperBitmap[addr>>12] |= 1LL << ((addr >> 6) & 63);
        }
        
-       RELEASE(&glPhysicalPages);
+       Mutex_Release(&glPhysicalPages);
+       #if TRACE_REF
+       Log("MM_AllocPhysRange: ret = %P (Ref %i)", ret << 12, MM_GetRefCount(ret<<12));
+       #endif
        LEAVE('x', ret << 12);
        return ret << 12;
 }
@@ -460,7 +478,7 @@ tPAddr MM_AllocPhys(void)
                if( gaiStaticAllocPages[i] ) {
                        tPAddr  ret = gaiStaticAllocPages[i];
                        gaiStaticAllocPages[i] = 0;
-                       Log("MM_AllocPhys: Return %x, static alloc %i", ret, i);
+                       Log("MM_AllocPhys: Return %P, static alloc %i", ret, i);
                        return ret;
                }
        }
@@ -475,21 +493,43 @@ void MM_RefPhys(tPAddr PAddr)
 {
        Uint64  page = PAddr >> 12;
        
-       if( PAddr >> 12 > giMaxPhysPage )       return ;
+       if( page > giMaxPhysPage )      return ;
        
-       if( gaMainBitmap[ page >> 6 ] & (1LL << (page&63)) )
+       if( PAGE_ALLOC_TEST(page) )
        {
-               // Reference again
-               gaMultiBitmap[ page >> 6 ] |= 1LL << (page&63);
-               gaiPageReferences[ page ] ++;
+               tVAddr  ref_base = ((tVAddr)&gaiPageReferences[ page ]) & ~0xFFF;
+               // Allocate reference page
+               if( !MM_GetPhysAddr(ref_base) )
+               {
+                       const int       pages_per_refpage = PAGE_SIZE/sizeof(gaiPageReferences[0]);
+                        int    i;
+                        int    page_base = page / pages_per_refpage * pages_per_refpage;
+                       if( !MM_Allocate( ref_base ) ) {
+                               Log_Error("Arch", "Out of memory when allocating reference count page");
+                               return ;
+                       }
+                       // Fill block
+                       Log("Allocated references for %P-%P", page_base << 12, (page_base+pages_per_refpage)<<12);
+                       for( i = 0; i < pages_per_refpage; i ++ ) {
+                                int    pg = page_base + i;
+                               gaiPageReferences[pg] = !!PAGE_ALLOC_TEST(pg);
+                       }
+               }
+               gaiPageReferences[page] ++;
        }
        else
        {
                // Allocate
-               gaMainBitmap[page >> 6] |= 1LL << (page&63);
-               if( gaMainBitmap[page >> 6 ] + 1 == 0 )
+               PAGE_ALLOC_SET(page);
+               if( gaMainBitmap[page >> 6] + 1 == 0 )
                        gaSuperBitmap[page>> 12] |= 1LL << ((page >> 6) & 63);
+               if( MM_GetPhysAddr( (tVAddr)&gaiPageReferences[page] ) )
+                       gaiPageReferences[page] = 1;
        }
+
+       #if TRACE_REF
+       Log("MM_RefPhys: %P referenced (%i)", page << 12, MM_GetRefCount(page << 12));
+       #endif
 }
 
 /**
@@ -501,18 +541,17 @@ void MM_DerefPhys(tPAddr PAddr)
        
        if( PAddr >> 12 > giMaxPhysPage )       return ;
        
-       if( gaMultiBitmap[ page >> 6 ] & (1LL << (page&63)) ) {
+       if( MM_GetPhysAddr( (tVAddr) &gaiPageReferences[page] ) )
+       {
                gaiPageReferences[ page ] --;
-               if( gaiPageReferences[ page ] == 1 )
-                       gaMultiBitmap[ page >> 6 ] &= ~(1LL << (page&63));
                if( gaiPageReferences[ page ] == 0 )
-                       gaMainBitmap[ page >> 6 ] &= ~(1LL << (page&63));
+                       PAGE_ALLOC_CLEAR(page);
        }
        else
-               gaMainBitmap[ page >> 6 ] &= ~(1LL << (page&63));
+               PAGE_ALLOC_CLEAR(page);
        
        // Update the free counts if the page was freed
-       if( !(gaMainBitmap[ page >> 6 ] & (1LL << (page&63))) )
+       if( !PAGE_ALLOC_TEST(page) )
        {
                 int    rangeID;
                rangeID = MM_int_GetRangeID( PAddr );
@@ -527,6 +566,27 @@ void MM_DerefPhys(tPAddr PAddr)
        if(gaMainBitmap[ page >> 6 ] + 1 != 0 ) {
                gaSuperBitmap[page >> 12] &= ~(1LL << ((page >> 6) & 63));
        }
+       
+       #if TRACE_REF
+       Log("Page %P dereferenced (%i)", page << 12, MM_GetRefCount(page << 12));
+       #endif
+}
+
+int MM_GetRefCount( tPAddr PAddr )
+{
+       PAddr >>= 12;
+       
+       if( PAddr > giMaxPhysPage )     return 0;
+
+       if( MM_GetPhysAddr( (tVAddr)&gaiPageReferences[PAddr] ) ) {
+               return gaiPageReferences[PAddr];
+       }
+
+       if( PAGE_ALLOC_TEST(PAddr) )
+       {
+               return 1;
+       }
+       return 0;
 }
 
 /**
@@ -547,3 +607,35 @@ int MM_int_GetRangeID( tPAddr Addr )
        else
                return MM_PHYS_16BIT;
 }
+
+int MM_SetPageNode(tPAddr PAddr, void *Node)
+{
+       tPAddr  page = PAddr >> 12;
+       tVAddr  node_page = ((tVAddr)&gapPageNodes[page]) & ~(PAGE_SIZE-1);
+
+//     if( !MM_GetRefCount(PAddr) )    return 1;
+       
+       if( !MM_GetPhysAddr(node_page) ) {
+               if( !MM_Allocate(node_page) )
+                       return -1;
+               memset( (void*)node_page, 0, PAGE_SIZE );
+       }
+
+       gapPageNodes[page] = Node;
+       return 0;
+}
+
+int MM_GetPageNode(tPAddr PAddr, void **Node)
+{
+//     if( !MM_GetRefCount(PAddr) )    return 1;
+       PAddr >>= 12;
+       
+       if( !MM_GetPhysAddr( (tVAddr)&gapPageNodes[PAddr] ) ) {
+               *Node = NULL;
+               return 0;
+       }
+
+       *Node = gapPageNodes[PAddr];
+       return 0;
+}
+

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