Trying to reduce the ability for a fork bomb to fault the kernel
[tpg/acess2.git] / Kernel / arch / x86 / mm_virt.c
index aaec06e..fdc19b1 100644 (file)
@@ -74,8 +74,8 @@ extern void   Threads_SegFault(tVAddr Addr);
 extern void    Error_Backtrace(Uint eip, Uint ebp);
 
 // === PROTOTYPES ===
-void   MM_PreinitVirtual();
-void   MM_InstallVirtual();
+void   MM_PreinitVirtual(void);
+void   MM_InstallVirtual(void);
 void   MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs);
 void   MM_DumpTables(tVAddr Start, tVAddr End);
 tPAddr MM_DuplicatePage(tVAddr VAddr);
@@ -95,17 +95,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;
 
 // === CODE ===
 /**
- * \fn void MM_PreinitVirtual()
+ * \fn void MM_PreinitVirtual(void)
  * \brief Maps the fractal mappings
  */
-void MM_PreinitVirtual()
+void MM_PreinitVirtual(void)
 {
        #if USE_PAE
        gaInitPageDir[ ((PAGE_TABLE_ADDR >> TAB)-3*512+3)*2 ] = ((tTabEnt)&gaInitPageDir - KERNEL_BASE) | 3;
@@ -116,10 +116,10 @@ void MM_PreinitVirtual()
 }
 
 /**
- * \fn void MM_InstallVirtual()
+ * \fn void MM_InstallVirtual(void)
  * \brief Sets up the constant page mappings
  */
-void MM_InstallVirtual()
+void MM_InstallVirtual(void)
 {
         int    i;
        
@@ -158,7 +158,6 @@ void MM_InstallVirtual()
        
        // Unset kernel on the User Text pages
        for( i = ((tVAddr)&_UsertextEnd-(tVAddr)&_UsertextBase+0xFFF)/4096; i--; ) {
-               Log("MM_SetFlags( 0x%08x, 0, MM_PFLAG_KERNEL)", (tVAddr)&_UsertextBase + i*4096);
                MM_SetFlags( (tVAddr)&_UsertextBase + i*4096, 0, MM_PFLAG_KERNEL );
        }
 }
@@ -166,7 +165,7 @@ void MM_InstallVirtual()
 /**
  * \brief Cleans up the SMP required mappings
  */
-void MM_FinishVirtualInit()
+void MM_FinishVirtualInit(void)
 {
        #if USE_PAE
        gaInitPDPT[ 0 ] = 0;
@@ -247,6 +246,19 @@ void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs)
        
        //MM_DumpTables(0, -1); 
        
+       // Register Dump
+       Log("EAX %08x ECX %08x EDX %08x EBX %08x", Regs->eax, Regs->ecx, Regs->edx, Regs->ebx);
+       Log("ESP %08x EBP %08x ESI %08x EDI %08x", Regs->esp, Regs->ebp, Regs->esi, Regs->edi);
+       //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);
 }
 
@@ -479,7 +491,7 @@ int MM_Map(tVAddr VAddr, tPAddr PAddr)
  * \fn tVAddr MM_ClearUser()
  * \brief Clear user's address space
  */
-tVAddr MM_ClearUser()
+tVAddr MM_ClearUser(void)
 {
        Uint    i, j;
        
@@ -510,10 +522,10 @@ tVAddr MM_ClearUser()
 }
 
 /**
- * \fn tPAddr MM_Clone()
+ * \fn tPAddr MM_Clone(void)
  * \brief Clone the current address space
  */
-tPAddr MM_Clone()
+tPAddr MM_Clone(void)
 {
        Uint    i, j;
        tVAddr  ret;
@@ -521,7 +533,7 @@ tPAddr MM_Clone()
        tVAddr  kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE;
        void    *tmp;
        
-       LOCK( &gilTempFractal );
+       Mutex_Acquire( &glTempFractal );
        
        // Create Directory Table
        *gpTmpCR3 = MM_AllocPhys() | 3;
@@ -629,26 +641,27 @@ tPAddr MM_Clone()
        }
        
        ret = *gpTmpCR3 & ~0xFFF;
-       RELEASE( &gilTempFractal );
+       Mutex_Release( &glTempFractal );
        
        //LEAVE('x', ret);
        return ret;
 }
 
 /**
- * \fn tVAddr MM_NewKStack()
+ * \fn tVAddr MM_NewKStack(void)
  * \brief Create a new kernel stack
  */
-tVAddr MM_NewKStack()
+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)
        {
                if(MM_GetPhysAddr(base) != 0)   continue;
-               for(i=0;i<KERNEL_STACK_SIZE;i+=0x1000) {
+               for(i = 0; i < KERNEL_STACK_SIZE; i += 0x1000) {
                        MM_Allocate(base+i);
                }
+               Log("MM_NewKStack - Allocated %p", base + KERNEL_STACK_SIZE);
                return base+KERNEL_STACK_SIZE;
        }
        Warning("MM_NewKStack - No address space left\n");
@@ -701,7 +714,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,7 +738,7 @@ tVAddr MM_NewWorkerStack()
        }
        *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);
@@ -888,11 +901,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 ++ )
                {
@@ -902,11 +915,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: Less expensive
        }
 }
 
@@ -1041,8 +1054,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++ )
        {
@@ -1050,7 +1062,7 @@ void MM_UnmapHWPages(tVAddr VAddr, Uint Number)
                gaPageTable[ i + j ] = 0;
        }
        
-       RELEASE( &gilTempMappings );
+       Mutex_Release( &glTempMappings );
 }
 
 // --- EXPORTS ---

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