Kernel/x86_64 - Fixed Kernel not using COW, cleanup
[tpg/acess2.git] / Kernel / arch / x86 / vm8086.c
index ef449d4..589d9a7 100644 (file)
@@ -6,6 +6,8 @@
 #include <acess.h>
 #include <vm8086.h>
 #include <modules.h>
+#include <hal_proc.h>
+#include <semaphore.h>
 
 // === CONSTANTS ===
 #define VM8086_MAGIC_CS        0xFFFF
@@ -28,9 +30,6 @@ enum eVM8086_Opcodes
 #define VM8086_BLOCKSIZE       128
 #define VM8086_BLOCKCOUNT      ((0x9F000-0x10000)/VM8086_BLOCKSIZE)
 
-// === IMPORTS ===
- int   Proc_Clone(Uint *Err, Uint Flags);
-
 // === TYPES ===
 struct sVM8086_InternalData
 {
@@ -49,6 +48,8 @@ void  VM8086_GPF(tRegs *Regs);
 // === GLOBALS ===
 MODULE_DEFINE(0, 0x100, VM8086, VM8086_Install, NULL, NULL);
 tMutex glVM8086_Process;
+tSemaphore     gVM8086_TaskComplete;
+tSemaphore     gVM8086_TasksToDo;
 tPID   gVM8086_WorkerPID;
 tTID   gVM8086_CallingThread;
 tVM8086        volatile * volatile gpVM8086_State = (void*)-1; // Set to -1 to avoid race conditions
@@ -63,7 +64,7 @@ int VM8086_Install(char **Arguments)
        Mutex_Acquire( &glVM8086_Process );
        
        // Create BIOS Call process
-       pid = Proc_Clone(NULL, CLONE_VM);
+       pid = Proc_Clone(CLONE_VM);
        if(pid == -1)
        {
                Log_Error("VM8086", "Unable to clone kernel into VM8086 worker");
@@ -74,22 +75,25 @@ int VM8086_Install(char **Arguments)
                Uint    * volatile stacksetup;  // Initialising Stack
                Uint16  * volatile rmstack;     // Real Mode Stack
                 int    i;
-                
+               
                // Set Image Name
                Threads_SetName("VM8086");
-               
+
+               Log_Debug("VM8086", "Mapping memory");  
+       
                // Map ROM Area
                for(i=0xA0;i<0x100;i++) {
                        MM_Map( i * 0x1000, i * 0x1000 );
-                       //MM_SetFlags( i * 0x1000, MM_PFLAG_RO, MM_PFLAG_RO );  // Set Read Only
                }
+               Log_Debug("VM8086", "ROM area mapped");
                MM_Map( 0, 0 ); // IVT / BDA
                // Map (but allow allocation) of 0x1000 - 0x9F000
                // - So much hack, it isn't funny
                for(i=1;i<0x9F;i++) {
                        MM_Map( i * 0x1000, i * 0x1000 );
                        MM_DerefPhys( i * 0x1000 );     // Above
-                       MM_DerefPhys( i * 0x1000 );     // Phys setup
+                       while(MM_GetRefCount(i*0x1000))
+                               MM_DerefPhys( i * 0x1000 );     // Phys setup
                }
                MM_Map( 0x9F000, 0x9F000 );     // Stack / EBDA
                // System Stack / Stub
@@ -98,6 +102,7 @@ int VM8086_Install(char **Arguments)
                        gVM8086_WorkerPID = 0;
                        Threads_Exit(0, 1);
                }
+               Log_Debug("VM8086", "Mapped low memory");
                
                *(Uint8*)(0x100000) = VM8086_OP_IRET;
                *(Uint8*)(0x100001) = 0x07;     // POP ES
@@ -179,16 +184,12 @@ void VM8086_GPF(tRegs *Regs)
                        gpVM8086_State->DS = Regs->ds;  gpVM8086_State->ES = Regs->es;
                        gpVM8086_State = NULL;
                        // Wake the caller
-                       Threads_WakeTID(gVM8086_CallingThread);
+                       Semaphore_Signal(&gVM8086_TaskComplete, 1);
                }
                
                //Log_Log("VM8086", "Waiting for something to do");
                __asm__ __volatile__ ("sti");
-               // Wait for a new task
-               while(!gpVM8086_State) {
-                       Threads_Sleep();
-                       //Log_Log("VM8086", "gpVM8086_State = %p", gpVM8086_State);
-               }
+               Semaphore_Wait(&gVM8086_TasksToDo, 1);
                
                //Log_Log("VM8086", "We have a task (%p)", gpVM8086_State);
                Regs->esp -= 2; *(Uint16*volatile)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = VM8086_MAGIC_CS;
@@ -419,14 +420,16 @@ void VM8086_Int(tVM8086 *State, Uint8 Interrupt)
 {
        State->IP = *(Uint16*)(KERNEL_BASE+4*Interrupt);
        State->CS = *(Uint16*)(KERNEL_BASE+4*Interrupt+2);
+
+//     Log_Debug("VM8086", "Software interrupt %i to %04x:%04x", Interrupt, State->CS, State->IP);
        
        Mutex_Acquire( &glVM8086_Process );
        
        gpVM8086_State = State;
        gVM8086_CallingThread = Threads_GetTID();
-       Threads_WakeTID( gVM8086_WorkerPID );
-       Threads_Sleep();
-       while( gpVM8086_State != NULL ) Threads_Sleep();
+       Semaphore_Signal(&gVM8086_TasksToDo, 1);
+
+       Semaphore_Wait(&gVM8086_TaskComplete, 1);
        
        Mutex_Release( &glVM8086_Process );
 }

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