Kernel/x86 - Removed PAE support
[tpg/acess2.git] / Kernel / arch / x86 / mm_virt.c
index 4c2455e..69b6719 100644 (file)
 #include <mm_phys.h>
 #include <proc.h>
 
-#if USE_PAE
-# define TAB   21
-# define DIR   30
-#else
-# define TAB   22
-#endif
+#define TAB    22
 
 #define KERNEL_STACKS          0xF0000000
 #define        KERNEL_STACK_SIZE       0x00008000
 #define        PF_PRESENT      0x1
 #define        PF_WRITE        0x2
 #define        PF_USER         0x4
+#define PF_GLOBAL      0x80
 #define        PF_COW          0x200
-#define        PF_PAGED        0x400
+#define        PF_NOPAGE       0x400
 
 #define INVLPG(addr)   __asm__ __volatile__ ("invlpg (%0)"::"r"(addr))
 
-#if USE_PAE
-typedef Uint64 tTabEnt;
-#else
 typedef Uint32 tTabEnt;
-#endif
 
 // === IMPORTS ===
 extern void    _UsertextEnd, _UsertextBase;
@@ -78,6 +70,7 @@ void  MM_PreinitVirtual(void);
 void   MM_InstallVirtual(void);
 void   MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs);
 void   MM_DumpTables(tVAddr Start, tVAddr End);
+tVAddr MM_ClearUser(void);
 tPAddr MM_DuplicatePage(tVAddr VAddr);
 
 // === GLOBALS ===
@@ -95,10 +88,17 @@ tPAddr      MM_DuplicatePage(tVAddr VAddr);
 #define gaPAE_TmpDir   ((tTabEnt*)PAE_TMP_DIR_ADDR)
 #define gaPAE_TmpPDPT  ((tTabEnt*)PAE_TMP_PDPT_ADDR)
  int   gbUsePAE = 0;
- int   gilTempMappings = 0;
- int   gilTempFractal = 0;
+tMutex glTempMappings;
+tMutex glTempFractal;
 Uint32 gWorkerStacks[(NUM_WORKER_STACKS+31)/32];
  int   giLastUsedWorker = 0;
+struct sPageInfo {
+       void    *Node;
+       tVAddr  Base;
+       Uint64  Offset;
+        int    Length;
+        int    Flags;
+}      *gaMappedRegions;       // sizeof = 24 bytes
 
 // === CODE ===
 /**
@@ -107,11 +107,7 @@ Uint32     gWorkerStacks[(NUM_WORKER_STACKS+31)/32];
  */
 void MM_PreinitVirtual(void)
 {
-       #if USE_PAE
-       gaInitPageDir[ ((PAGE_TABLE_ADDR >> TAB)-3*512+3)*2 ] = ((tTabEnt)&gaInitPageDir - KERNEL_BASE) | 3;
-       #else
        gaInitPageDir[ PAGE_TABLE_ADDR >> 22 ] = ((tTabEnt)&gaInitPageDir - KERNEL_BASE) | 3;
-       #endif
        INVLPG( PAGE_TABLE_ADDR );
 }
 
@@ -123,23 +119,6 @@ void MM_InstallVirtual(void)
 {
         int    i;
        
-       #if USE_PAE
-       // --- Pre-Allocate kernel tables
-       for( i = KERNEL_BASE >> TAB; i < 1024*4; i ++ )
-       {
-               if( gaPAE_PageDir[ i ] )        continue;
-               
-               // Skip stack tables, they are process unique
-               if( i > KERNEL_STACKS >> TAB && i < KERNEL_STACKS_END >> TAB) {
-                       gaPAE_PageDir[ i ] = 0;
-                       continue;
-               }
-               // Preallocate table
-               gaPAE_PageDir[ i ] = MM_AllocPhys() | 3;
-               INVLPG( &gaPAE_PageTable[i*512] );
-               memset( &gaPAE_PageTable[i*512], 0, 0x1000 );
-       }
-       #else
        // --- Pre-Allocate kernel tables
        for( i = KERNEL_BASE>>22; i < 1024; i ++ )
        {
@@ -154,7 +133,6 @@ void MM_InstallVirtual(void)
                INVLPG( &gaPageTable[i*1024] );
                memset( &gaPageTable[i*1024], 0, 0x1000 );
        }
-       #endif
        
        // Unset kernel on the User Text pages
        for( i = ((tVAddr)&_UsertextEnd-(tVAddr)&_UsertextBase+0xFFF)/4096; i--; ) {
@@ -167,11 +145,7 @@ void MM_InstallVirtual(void)
  */
 void MM_FinishVirtualInit(void)
 {
-       #if USE_PAE
-       gaInitPDPT[ 0 ] = 0;
-       #else
        gaInitPageDir[ 0 ] = 0;
-       #endif
 }
 
 /**
@@ -183,8 +157,7 @@ void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
        //ENTER("xAddr bErrorCode", Addr, ErrorCode);
        
        // -- Check for COW --
-       if( gaPageDir  [Addr>>22] & PF_PRESENT
-        && gaPageTable[Addr>>12] & PF_PRESENT
+       if( gaPageDir  [Addr>>22] & PF_PRESENT  && gaPageTable[Addr>>12] & PF_PRESENT
         && gaPageTable[Addr>>12] & PF_COW )
        {
                tPAddr  paddr;
@@ -203,7 +176,6 @@ void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
                }
                
                INVLPG( Addr & ~0xFFF );
-               //LEAVE('-')
                return;
        }
        
@@ -217,6 +189,9 @@ void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
                        );
                Warning("User Pagefault: Instruction at %04x:%08x accessed %p", Regs->cs, Regs->eip, Addr);
                __asm__ __volatile__ ("sti");   // Restart IRQs
+               #if 1
+               Error_Backtrace(Regs->eip, Regs->ebp);
+               #endif
                Threads_SegFault(Addr);
                return ;
        }
@@ -252,6 +227,12 @@ void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
        //Log("SS:ESP %04x:%08x", Regs->ss, Regs->esp);
        Log("CS:EIP %04x:%08x", Regs->cs, Regs->eip);
        Log("DS %04x ES %04x FS %04x GS %04x", Regs->ds, Regs->es, Regs->fs, Regs->gs);
+       {
+               Uint    dr0, dr1;
+               __ASM__ ("mov %%dr0, %0":"=r"(dr0):);
+               __ASM__ ("mov %%dr1, %0":"=r"(dr1):);
+               Log("DR0 %08x DR1 %08x", dr0, dr1);
+       }
        
        Panic("Page Fault at 0x%x (Accessed 0x%x)", Regs->eip, Addr);
 }
@@ -266,7 +247,7 @@ void MM_DumpTables(tVAddr Start, tVAddr End)
        tPAddr  expected = 0;
        tVAddr  curPos;
        Uint    page;
-       const tPAddr    MASK = ~0xF98;
+       const tPAddr    MASK = ~0xF78;
        
        Start >>= 12;   End >>= 12;
        
@@ -296,14 +277,16 @@ void MM_DumpTables(tVAddr Start, tVAddr End)
                ||  (gaPageTable[page] & MASK) != expected)
                {
                        if(expected) {
-                               Log(" 0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
-                                       rangeStart, curPos - 1,
+                               Log(" 0x%08x => 0x%08x - 0x%08x (%s%s%s%s%s)",
+                                       rangeStart,
                                        gaPageTable[rangeStart>>12] & ~0xFFF,
-                                       (expected & ~0xFFF) - 1,
-                                       (expected & PF_PAGED ? "p" : "-"),
+                                       curPos - rangeStart,
+                                       (expected & PF_NOPAGE ? "P" : "-"),
                                        (expected & PF_COW ? "C" : "-"),
+                                       (expected & PF_GLOBAL ? "G" : "-"),
                                        (expected & PF_USER ? "U" : "-"),
-                                       (expected & PF_WRITE ? "W" : "-")
+                                       (expected & PF_WRITE ? "W" : "-"),
+                                       gaPageTable[page] & MASK, expected
                                        );
                                expected = 0;
                        }
@@ -317,11 +300,11 @@ void MM_DumpTables(tVAddr Start, tVAddr End)
        }
        
        if(expected) {
-               Log("0x%08x-0x%08x => 0x%08x-0x%08x (%s%s%s%s)",
-                       rangeStart, curPos - 1,
+               Log("0x%08x => 0x%08x - 0x%08x (%s%s%s%s)",
+                       rangeStart,
                        gaPageTable[rangeStart>>12] & ~0xFFF,
-                       (expected & ~0xFFF) - 1,
-                       (expected & PF_PAGED ? "p" : "-"),
+                       curPos - rangeStart,
+                       (expected & PF_NOPAGE ? "p" : "-"),
                        (expected & PF_COW ? "C" : "-"),
                        (expected & PF_USER ? "U" : "-"),
                        (expected & PF_WRITE ? "W" : "-")
@@ -343,19 +326,16 @@ tPAddr MM_Allocate(tVAddr VAddr)
        {
                // Allocate directory
                paddr = MM_AllocPhys();
-               //LOG("paddr = 0x%llx (new table)", paddr);
                if( paddr == 0 ) {
                        Warning("MM_Allocate - Out of Memory (Called by %p)", __builtin_return_address(0));
                        //LEAVE('i',0);
                        return 0;
                }
-               // Map
+               // Map and mark as user (if needed)
                gaPageDir[ VAddr >> 22 ] = paddr | 3;
-               // Mark as user
                if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
                
                INVLPG( &gaPageDir[ VAddr >> 22 ] );
-               //LOG("Clearing new table");
                memsetd( &gaPageTable[ (VAddr >> 12) & ~0x3FF ], 0, 1024 );
        }
        // Check if the page is already allocated
@@ -489,7 +469,6 @@ tVAddr MM_ClearUser(void)
 {
        Uint    i, j;
        
-       // Copy Directories
        for( i = 0; i < (MM_USER_MAX>>22); i ++ )
        {
                // Check if directory is not allocated
@@ -498,7 +477,7 @@ tVAddr MM_ClearUser(void)
                        continue;
                }
                
-               
+               // Deallocate tables
                for( j = 0; j < 1024; j ++ )
                {
                        if( gaPageTable[i*1024+j] & 1 )
@@ -506,6 +485,7 @@ tVAddr MM_ClearUser(void)
                        gaPageTable[i*1024+j] = 0;
                }
                
+               // Deallocate directory
                MM_DerefPhys( gaPageDir[i] & ~0xFFF );
                gaPageDir[i] = 0;
                INVLPG( &gaPageTable[i*1024] );
@@ -527,7 +507,7 @@ tPAddr MM_Clone(void)
        tVAddr  kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
        void    *tmp;
        
-       LOCK( &gilTempFractal );
+       Mutex_Acquire( &glTempFractal );
        
        // Create Directory Table
        *gpTmpCR3 = MM_AllocPhys() | 3;
@@ -535,37 +515,40 @@ tPAddr MM_Clone(void)
        //LOG("Allocated Directory (%x)", *gpTmpCR3);
        memsetd( gaTmpDir, 0, 1024 );
        
-       // Copy Tables
-       for( i = 0; i < 768; i ++)
-       {
-               // Check if table is allocated
-               if( !(gaPageDir[i] & PF_PRESENT) ) {
-                       gaTmpDir[i] = 0;
-                       page += 1024;
-                       continue;
-               }
-               
-               // Allocate new table
-               gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
-               INVLPG( &gaTmpTable[page] );
-               // Fill
-               for( j = 0; j < 1024; j ++, page++ )
+       if( Threads_GetPID() != 0 )
+       {       
+               // Copy Tables
+               for( i = 0; i < 768; i ++)
                {
-                       if( !(gaPageTable[page] & PF_PRESENT) ) {
-                               gaTmpTable[page] = 0;
+                       // Check if table is allocated
+                       if( !(gaPageDir[i] & PF_PRESENT) ) {
+                               gaTmpDir[i] = 0;
+                               page += 1024;
                                continue;
                        }
                        
-                       // Refrence old page
-                       MM_RefPhys( gaPageTable[page] & ~0xFFF );
-                       // Add to new table
-                       if(gaPageTable[page] & PF_WRITE) {
-                               gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
-                               gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
-                               INVLPG( page << 12 );
+                       // Allocate new table
+                       gaTmpDir[i] = MM_AllocPhys() | (gaPageDir[i] & 7);
+                       INVLPG( &gaTmpTable[page] );
+                       // Fill
+                       for( j = 0; j < 1024; j ++, page++ )
+                       {
+                               if( !(gaPageTable[page] & PF_PRESENT) ) {
+                                       gaTmpTable[page] = 0;
+                                       continue;
+                               }
+                               
+                               // Refrence old page
+                               MM_RefPhys( gaPageTable[page] & ~0xFFF );
+                               // Add to new table
+                               if(gaPageTable[page] & PF_WRITE) {
+                                       gaTmpTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
+                                       gaPageTable[page] = (gaPageTable[page] & ~PF_WRITE) | PF_COW;
+                                       INVLPG( page << 12 );
+                               }
+                               else
+                                       gaTmpTable[page] = gaPageTable[page];
                        }
-                       else
-                               gaTmpTable[page] = gaPageTable[page];
                }
        }
        
@@ -635,7 +618,7 @@ tPAddr MM_Clone(void)
        }
        
        ret = *gpTmpCR3 & ~0xFFF;
-       RELEASE( &gilTempFractal );
+       Mutex_Release( &glTempFractal );
        
        //LEAVE('x', ret);
        return ret;
@@ -647,17 +630,32 @@ tPAddr MM_Clone(void)
  */
 tVAddr MM_NewKStack(void)
 {
-       tVAddr  base = KERNEL_STACKS;
+       tVAddr  base;
        Uint    i;
-       for(;base<KERNEL_STACKS_END;base+=KERNEL_STACK_SIZE)
+       for(base = KERNEL_STACKS; base < KERNEL_STACKS_END; base += KERNEL_STACK_SIZE)
        {
+               // Check if space is free
                if(MM_GetPhysAddr(base) != 0)   continue;
-               for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
-                       MM_Allocate(base+i);
+               // Allocate
+               //for(i = KERNEL_STACK_SIZE; i -= 0x1000 ; )
+               for(i = 0; i < KERNEL_STACK_SIZE; i += 0x1000 )
+               {
+                       if( MM_Allocate(base+i) == 0 )
+                       {
+                               // On error, print a warning and return error
+                               Warning("MM_NewKStack - Out of memory");
+                               // - Clean up
+                               //for( i += 0x1000 ; i < KERNEL_STACK_SIZE; i += 0x1000 )
+                               //      MM_Deallocate(base+i);
+                               return 0;
+                       }
                }
+               // Success
+               Log("MM_NewKStack - Allocated %p", base + KERNEL_STACK_SIZE);
                return base+KERNEL_STACK_SIZE;
        }
-       Warning("MM_NewKStack - No address space left\n");
+       // No stacks left
+       Warning("MM_NewKStack - No address space left");
        return 0;
 }
 
@@ -678,6 +676,7 @@ tVAddr MM_NewWorkerStack()
        __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp));
        __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp));
        
+       // TODO: Thread safety
        // Find a free worker stack address
        for(base = giLastUsedWorker; base < NUM_WORKER_STACKS; base++)
        {
@@ -707,7 +706,7 @@ tVAddr MM_NewWorkerStack()
        //Log(" MM_NewWorkerStack: base = 0x%x", base);
        
        // Acquire the lock for the temp fractal mappings
-       LOCK(&gilTempFractal);
+       Mutex_Acquire(&glTempFractal);
        
        // Set the temp fractals to TID0's address space
        *gpTmpCR3 = ((Uint)gaInitPageDir - KERNEL_BASE) | 3;
@@ -725,13 +724,14 @@ tVAddr MM_NewWorkerStack()
        
        // Mapping Time!
        for( addr = 0; addr < WORKER_STACK_SIZE; addr += 0x1000 )
+       //for( addr = WORKER_STACK_SIZE; addr; addr -= 0x1000 )
        {
                pages[ addr >> 12 ] = MM_AllocPhys();
                gaTmpTable[ (base + addr) >> 12 ] = pages[addr>>12] | 3;
        }
        *gpTmpCR3 = 0;
        // Release the temp mapping lock
-       RELEASE(&gilTempFractal);
+       Mutex_Release(&glTempFractal);
        
        // Copy the old stack
        oldstack = (esp + KERNEL_STACK_SIZE-1) & ~(KERNEL_STACK_SIZE-1);
@@ -894,11 +894,11 @@ tVAddr MM_MapTemp(tPAddr PAddr)
        
        PAddr &= ~0xFFF;
        
-       //LOG("gilTempMappings = %i", gilTempMappings);
+       //LOG("glTempMappings = %i", glTempMappings);
        
        for(;;)
        {
-               LOCK( &gilTempMappings );
+               Mutex_Acquire( &glTempMappings );
                
                for( i = 0; i < NUM_TEMP_PAGES; i ++ )
                {
@@ -908,11 +908,11 @@ tVAddr MM_MapTemp(tPAddr PAddr)
                        gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3;
                        INVLPG( TEMP_MAP_ADDR + (i << 12) );
                        //LEAVE('p', TEMP_MAP_ADDR + (i << 12));
-                       RELEASE( &gilTempMappings );
+                       Mutex_Release( &glTempMappings );
                        return TEMP_MAP_ADDR + (i << 12);
                }
-               RELEASE( &gilTempMappings );
-               Threads_Yield();
+               Mutex_Release( &glTempMappings );
+               Threads_Yield();        // TODO: Use a sleep queue here instead
        }
 }
 
@@ -1047,8 +1047,7 @@ void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
        
        i = VAddr >> 12;
        
-       LOCK( &gilTempMappings );       // Temp and HW share a directory, so they share a lock
-       
+       Mutex_Acquire( &glTempMappings );       // Temp and HW share a directory, so they share a lock
        
        for( j = 0; j < Number; j++ )
        {
@@ -1056,13 +1055,6 @@ void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
                gaPageTable[ i + j ] = 0;
        }
        
-       RELEASE( &gilTempMappings );
+       Mutex_Release( &glTempMappings );
 }
 
-// --- EXPORTS ---
-EXPORT(MM_GetPhysAddr);
-EXPORT(MM_Map);
-//EXPORT(MM_Unmap);
-EXPORT(MM_MapHWPages);
-EXPORT(MM_AllocDMA);
-EXPORT(MM_UnmapHWPages);

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