Kernel - Fixing and breaking SMP related stuff
[tpg/acess2.git] / Kernel / arch / x86 / mm_virt.c
index 69b6719..73e19d3 100644 (file)
 #include <mm_virt.h>
 #include <mm_phys.h>
 #include <proc.h>
+#include <hal_proc.h>
 
 #define TAB    22
 
-#define KERNEL_STACKS          0xF0000000
-#define        KERNEL_STACK_SIZE       0x00008000
-#define KERNEL_STACKS_END      0xFC000000
 #define WORKER_STACKS          0x00100000      // Thread0 Only!
-#define        WORKER_STACK_SIZE       KERNEL_STACK_SIZE
+#define        WORKER_STACK_SIZE       MM_KERNEL_STACK_SIZE
 #define WORKER_STACKS_END      0xB0000000
 #define        NUM_WORKER_STACKS       ((WORKER_STACKS_END-WORKER_STACKS)/WORKER_STACK_SIZE)
 
@@ -59,7 +57,7 @@
 typedef Uint32 tTabEnt;
 
 // === IMPORTS ===
-extern void    _UsertextEnd, _UsertextBase;
+extern char    _UsertextEnd[], _UsertextBase[];
 extern Uint32  gaInitPageDir[1024];
 extern Uint32  gaInitPageTable[1024];
 extern void    Threads_SegFault(tVAddr Addr);
@@ -69,8 +67,8 @@ extern void   Error_Backtrace(Uint eip, Uint ebp);
 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);
+//void MM_DumpTables(tVAddr Start, tVAddr End);
+//void MM_ClearUser(void);
 tPAddr MM_DuplicatePage(tVAddr VAddr);
 
 // === GLOBALS ===
@@ -124,7 +122,7 @@ void MM_InstallVirtual(void)
        {
                if( gaPageDir[ i ] )    continue;
                // Skip stack tables, they are process unique
-               if( i > KERNEL_STACKS >> 22 && i < KERNEL_STACKS_END >> 22) {
+               if( i > MM_KERNEL_STACKS >> 22 && i < MM_KERNEL_STACKS_END >> 22) {
                        gaPageDir[ i ] = 0;
                        continue;
                }
@@ -175,19 +173,23 @@ void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
                        gaPageTable[Addr>>12] |= paddr|PF_PRESENT|PF_WRITE;
                }
                
+//             Log_Debug("MMVirt", "COW for %p (%P)", Addr, gaPageTable[Addr>>12]);
+               
                INVLPG( Addr & ~0xFFF );
                return;
        }
        
+       __asm__ __volatile__ ("pushf; andw $0xFEFF, 0(%esp); popf");
+       Proc_GetCurThread()->bInstrTrace = 0;
+
        // If it was a user, tell the thread handler
        if(ErrorCode & 4) {
-               Warning("%s %s %s memory%s",
-                       (ErrorCode&4?"User":"Kernel"),
+               Log_Warning("MMVirt", "User %s %s memory%s",
                        (ErrorCode&2?"write to":"read from"),
                        (ErrorCode&1?"bad/locked":"non-present"),
                        (ErrorCode&16?" (Instruction Fetch)":"")
                        );
-               Warning("User Pagefault: Instruction at %04x:%08x accessed %p", Regs->cs, Regs->eip, Addr);
+               Log_Warning("MMVirt", "Instruction %04x:%08x accessed %p", Regs->cs, Regs->eip, Addr);
                __asm__ __volatile__ ("sti");   // Restart IRQs
                #if 1
                Error_Backtrace(Regs->eip, Regs->ebp);
@@ -203,8 +205,7 @@ void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
                Warning("Reserved Bits Trashed!");
        else
        {
-               Warning("%s %s %s memory%s",
-                       (ErrorCode&4?"User":"Kernel"),
+               Warning("Kernel %s %s memory%s",
                        (ErrorCode&2?"write to":"read from"),
                        (ErrorCode&1?"bad/locked":"non-present"),
                        (ErrorCode&16?" (Instruction Fetch)":"")
@@ -245,6 +246,7 @@ void MM_DumpTables(tVAddr Start, tVAddr End)
 {
        tVAddr  rangeStart = 0;
        tPAddr  expected = 0;
+       void    *expected_node = NULL, *tmpnode = NULL;
        tVAddr  curPos;
        Uint    page;
        const tPAddr    MASK = ~0xF78;
@@ -274,19 +276,21 @@ void MM_DumpTables(tVAddr Start, tVAddr End)
        {
                if( !(gaPageDir[curPos>>22] & PF_PRESENT)
                ||  !(gaPageTable[page] & PF_PRESENT)
-               ||  (gaPageTable[page] & MASK) != expected)
+               ||  (gaPageTable[page] & MASK) != expected
+               ||  (tmpnode=NULL,MM_GetPageNode(expected, &tmpnode), tmpnode != expected_node))
                {
                        if(expected) {
-                               Log(" 0x%08x => 0x%08x - 0x%08x (%s%s%s%s%s)",
+                               tPAddr  orig = gaPageTable[rangeStart>>12];
+                               Log(" 0x%08x => 0x%08x - 0x%08x (%s%s%s%s%s) %p",
                                        rangeStart,
-                                       gaPageTable[rangeStart>>12] & ~0xFFF,
+                                       orig & ~0xFFF,
                                        curPos - rangeStart,
-                                       (expected & PF_NOPAGE ? "P" : "-"),
-                                       (expected & PF_COW ? "C" : "-"),
-                                       (expected & PF_GLOBAL ? "G" : "-"),
-                                       (expected & PF_USER ? "U" : "-"),
-                                       (expected & PF_WRITE ? "W" : "-"),
-                                       gaPageTable[page] & MASK, expected
+                                       (orig & PF_NOPAGE ? "P" : "-"),
+                                       (orig & PF_COW ? "C" : "-"),
+                                       (orig & PF_GLOBAL ? "G" : "-"),
+                                       (orig & PF_USER ? "U" : "-"),
+                                       (orig & PF_WRITE ? "W" : "-"),
+                                       expected_node
                                        );
                                expected = 0;
                        }
@@ -294,20 +298,24 @@ void MM_DumpTables(tVAddr Start, tVAddr End)
                        if( !(gaPageTable[curPos>>12] & PF_PRESENT) )   continue;
                        
                        expected = (gaPageTable[page] & MASK);
+                       MM_GetPageNode(expected, &expected_node);
                        rangeStart = curPos;
                }
                if(expected)    expected += 0x1000;
        }
        
        if(expected) {
-               Log("0x%08x => 0x%08x - 0x%08x (%s%s%s%s)",
+               tPAddr  orig = gaPageTable[rangeStart>>12];
+               Log("0x%08x => 0x%08x - 0x%08x (%s%s%s%s%s) %p",
                        rangeStart,
-                       gaPageTable[rangeStart>>12] & ~0xFFF,
+                       orig & ~0xFFF,
                        curPos - rangeStart,
-                       (expected & PF_NOPAGE ? "p" : "-"),
-                       (expected & PF_COW ? "C" : "-"),
-                       (expected & PF_USER ? "U" : "-"),
-                       (expected & PF_WRITE ? "W" : "-")
+                       (orig & PF_NOPAGE ? "p" : "-"),
+                       (orig & PF_COW ? "C" : "-"),
+                       (orig & PF_GLOBAL ? "G" : "-"),
+                       (orig & PF_USER ? "U" : "-"),
+                       (orig & PF_WRITE ? "W" : "-"),
+                       expected_node
                        );
                expected = 0;
        }
@@ -428,7 +436,10 @@ int MM_Map(tVAddr VAddr, tPAddr PAddr)
        // Check if the directory is mapped
        if( gaPageDir[ VAddr >> 22 ] == 0 )
        {
-               gaPageDir[ VAddr >> 22 ] = MM_AllocPhys() | 3;
+               tPAddr  tmp = MM_AllocPhys();
+               if( tmp == 0 )
+                       return 0;
+               gaPageDir[ VAddr >> 22 ] = tmp | 3;
                
                // Mark as user
                if(VAddr < MM_USER_MAX) gaPageDir[ VAddr >> 22 ] |= PF_USER;
@@ -462,10 +473,9 @@ int MM_Map(tVAddr VAddr, tPAddr PAddr)
 }
 
 /**
- * \fn tVAddr MM_ClearUser()
  * \brief Clear user's address space
  */
-tVAddr MM_ClearUser(void)
+void MM_ClearUser(void)
 {
        Uint    i, j;
        
@@ -491,8 +501,6 @@ tVAddr MM_ClearUser(void)
                INVLPG( &gaPageTable[i*1024] );
        }
        INVLPG( gaPageDir );
-       
-       return *gpPageCR3;
 }
 
 /**
@@ -504,13 +512,17 @@ tPAddr MM_Clone(void)
        Uint    i, j;
        tVAddr  ret;
        Uint    page = 0;
-       tVAddr  kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
+       tVAddr  kStackBase = Proc_GetCurThread()->KernelStack - MM_KERNEL_STACK_SIZE;
        void    *tmp;
        
        Mutex_Acquire( &glTempFractal );
        
        // Create Directory Table
        *gpTmpCR3 = MM_AllocPhys() | 3;
+       if( *gpTmpCR3 == 3 ) {
+               *gpTmpCR3 = 0;
+               return 0;
+       }
        INVLPG( gaTmpDir );
        //LOG("Allocated Directory (%x)", *gpTmpCR3);
        memsetd( gaTmpDir, 0, 1024 );
@@ -572,9 +584,7 @@ tPAddr MM_Clone(void)
        }
        
        // Allocate kernel stack
-       for(i = KERNEL_STACKS >> 22;
-               i < KERNEL_STACKS_END >> 22;
-               i ++ )
+       for(i = MM_KERNEL_STACKS >> 22; i < MM_KERNEL_STACKS_END >> 22; i ++ )
        {
                // Check if directory is allocated
                if( (gaPageDir[i] & 1) == 0 ) {
@@ -601,7 +611,7 @@ tPAddr MM_Clone(void)
                        }
                        
                        // We don't care about other kernel stacks
-                       if( ((i*1024+j)*4096 & ~(KERNEL_STACK_SIZE-1)) != kStackBase ) {
+                       if( ((i*1024+j)*4096 & ~(MM_KERNEL_STACK_SIZE-1)) != kStackBase ) {
                                gaTmpTable[i*1024+j] = 0;
                                continue;
                        }
@@ -632,30 +642,30 @@ tVAddr MM_NewKStack(void)
 {
        tVAddr  base;
        Uint    i;
-       for(base = KERNEL_STACKS; base < KERNEL_STACKS_END; base += KERNEL_STACK_SIZE)
+       for(base = MM_KERNEL_STACKS; base < MM_KERNEL_STACKS_END; base += MM_KERNEL_STACK_SIZE)
        {
                // Check if space is free
                if(MM_GetPhysAddr(base) != 0)   continue;
                // Allocate
-               //for(i = KERNEL_STACK_SIZE; i -= 0x1000 ; )
-               for(i = 0; i < KERNEL_STACK_SIZE; i += 0x1000 )
+               //for(i = MM_KERNEL_STACK_SIZE; i -= 0x1000 ; )
+               for(i = 0; i < MM_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 )
+                               //for( i += 0x1000 ; i < MM_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;
+//             Log("MM_NewKStack - Allocated %p", base + MM_KERNEL_STACK_SIZE);
+               return base+MM_KERNEL_STACK_SIZE;
        }
        // No stacks left
-       Warning("MM_NewKStack - No address space left");
+       Log_Warning("MMVirt", "MM_NewKStack - No address space left");
        return 0;
 }
 
@@ -663,18 +673,11 @@ tVAddr MM_NewKStack(void)
  * \fn tVAddr MM_NewWorkerStack()
  * \brief Creates a new worker stack
  */
-tVAddr MM_NewWorkerStack()
+tVAddr MM_NewWorkerStack(Uint *StackContents, size_t ContentsSize)
 {
-       Uint    esp, ebp;
-       Uint    oldstack;
        Uint    base, addr;
-        int    i, j;
-       Uint    *tmpPage;
-       tPAddr  pages[WORKER_STACK_SIZE>>12];
-       
-       // Get the old ESP and EBP
-       __asm__ __volatile__ ("mov %%esp, %0": "=r"(esp));
-       __asm__ __volatile__ ("mov %%ebp, %0": "=r"(ebp));
+       tVAddr  tmpPage;
+       tPAddr  page;
        
        // TODO: Thread safety
        // Find a free worker stack address
@@ -724,44 +727,22 @@ 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;
+               page = MM_AllocPhys();
+               gaTmpTable[ (base + addr) >> 12 ] = page | 3;
        }
        *gpTmpCR3 = 0;
        // Release the temp mapping lock
        Mutex_Release(&glTempFractal);
-       
-       // Copy the old stack
-       oldstack = (esp + KERNEL_STACK_SIZE-1) & ~(KERNEL_STACK_SIZE-1);
-       esp = oldstack - esp;   // ESP as an offset in the stack
-       
-       // Make `base` be the top of the stack
-       base += WORKER_STACK_SIZE;
-       
-       i = (WORKER_STACK_SIZE>>12) - 1;
-       // Copy the contents of the old stack to the new one, altering the addresses
-       // `addr` is refering to bytes from the stack base (mem downwards)
-       for(addr = 0; addr < esp; addr += 0x1000)
-       {
-               Uint    *stack = (Uint*)( oldstack-(addr+0x1000) );
-               tmpPage = (void*)MM_MapTemp( pages[i] );
-               // Copy old stack
-               for(j = 0; j < 1024; j++)
-               {
-                       // Possible Stack address?
-                       if(oldstack-esp < stack[j] && stack[j] < oldstack)
-                               tmpPage[j] = base - (oldstack - stack[j]);
-                       else    // Seems not, best leave it alone
-                               tmpPage[j] = stack[j];
-               }
-               MM_FreeTemp((tVAddr)tmpPage);
-               i --;
-       }
+
+       // NOTE: Max of 1 page
+       // `page` is the last allocated page from the previious for loop
+       tmpPage = MM_MapTemp( page );
+       memcpy( (void*)( tmpPage + (0x1000 - ContentsSize) ), StackContents, ContentsSize);
+       MM_FreeTemp(tmpPage);   
        
        //Log("MM_NewWorkerStack: RETURN 0x%x", base);
-       return base;
+       return base + WORKER_STACK_SIZE;
 }
 
 /**
@@ -841,6 +822,45 @@ Uint MM_GetFlags(tVAddr VAddr)
        return ret;
 }
 
+/**
+ * \brief Check if the provided buffer is valid
+ * \return Boolean valid
+ */
+int MM_IsValidBuffer(tVAddr Addr, size_t Size)
+{
+        int    bIsUser;
+        int    dir, tab;
+
+       Size += Addr & (PAGE_SIZE-1);
+       Addr &= ~(PAGE_SIZE-1);
+
+       dir = Addr >> 22;
+       tab = Addr >> 12;
+       
+//     Debug("Addr = %p, Size = 0x%x, dir = %i, tab = %i", Addr, Size, dir, tab);
+
+       if( !(gaPageDir[dir] & 1) )     return 0;
+       if( !(gaPageTable[tab] & 1) )   return 0;
+       
+       bIsUser = !!(gaPageTable[tab] & PF_USER);
+
+       while( Size >= PAGE_SIZE )
+       {
+               if( (tab & 1023) == 0 )
+               {
+                       dir ++;
+                       if( !(gaPageDir[dir] & 1) )     return 0;
+               }
+               
+               if( !(gaPageTable[tab] & 1) )   return 0;
+               if( bIsUser && !(gaPageTable[tab] & PF_USER) )  return 0;
+
+               tab ++;
+               Size -= PAGE_SIZE;
+       }
+       return 1;
+}
+
 /**
  * \fn tPAddr MM_DuplicatePage(tVAddr VAddr)
  * \brief Duplicates a virtual page to a physical one
@@ -862,6 +882,9 @@ tPAddr MM_DuplicatePage(tVAddr VAddr)
        
        // Allocate new page
        ret = MM_AllocPhys();
+       if( !ret ) {
+               return 0;
+       }
        
        // Write-lock the page (to keep data constistent), saving its R/W state
        wasRO = (gaPageTable[VAddr >> 12] & PF_WRITE ? 0 : 1);
@@ -998,6 +1021,10 @@ tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr)
        if(Pages == 1 && MaxBits >= PHYS_BITS)
        {
                phys = MM_AllocPhys();
+               if( !phys ) {
+                       *PhysAddr = 0;
+                       LEAVE_RET('i', 0);
+               }
                *PhysAddr = phys;
                ret = MM_MapHWPages(phys, 1);
                if(ret == 0) {

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