[FAST486]
[reactos.git] / reactos / lib / fast486 / common.c
index 52f2fc7..4f78e01 100644 (file)
@@ -2,7 +2,7 @@
  * Fast486 386/486 CPU Emulation Library
  * common.c
  *
- * Copyright (C) 2014 Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
+ * Copyright (C) 2015 Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -47,11 +47,23 @@ Fast486ReadMemory(PFAST486_STATE State,
     /* Get the cached descriptor */
     CachedDescriptor = &State->SegmentRegs[SegmentReg];
 
-    if ((Offset + Size - 1) > CachedDescriptor->Limit)
+    if (InstFetch || !CachedDescriptor->DirConf)
     {
-        /* Read beyond limit */
-        Fast486Exception(State, FAST486_EXCEPTION_GP);
-        return FALSE;
+        if ((Offset + Size - 1) > CachedDescriptor->Limit)
+        {
+            /* Read beyond limit */
+            Fast486Exception(State, FAST486_EXCEPTION_GP);
+            return FALSE;
+        }
+    }
+    else
+    {
+        if (Offset < CachedDescriptor->Limit)
+        {
+            /* Read beyond limit */
+            Fast486Exception(State, FAST486_EXCEPTION_GP);
+            return FALSE;
+        }
     }
 
     /* Check for protected mode */
@@ -157,11 +169,23 @@ Fast486WriteMemory(PFAST486_STATE State,
     /* Get the cached descriptor */
     CachedDescriptor = &State->SegmentRegs[SegmentReg];
 
-    if ((Offset + Size - 1) > CachedDescriptor->Limit)
+    if (!CachedDescriptor->DirConf)
     {
-        /* Write beyond limit */
-        Fast486Exception(State, FAST486_EXCEPTION_GP);
-        return FALSE;
+        if ((Offset + Size - 1) > CachedDescriptor->Limit)
+        {
+            /* Write beyond limit */
+            Fast486Exception(State, FAST486_EXCEPTION_GP);
+            return FALSE;
+        }
+    }
+    else
+    {
+        if (Offset < CachedDescriptor->Limit)
+        {
+            /* Read beyond limit */
+            Fast486Exception(State, FAST486_EXCEPTION_GP);
+            return FALSE;
+        }
     }
 
     /* Check for protected mode */
@@ -264,7 +288,9 @@ Fast486GetIntVector(PFAST486_STATE State,
 static inline BOOLEAN
 FASTCALL
 Fast486InterruptInternal(PFAST486_STATE State,
-                         PFAST486_IDT_ENTRY IdtEntry)
+                         PFAST486_IDT_ENTRY IdtEntry,
+                         BOOLEAN PushErrorCode,
+                         ULONG ErrorCode)
 {
     USHORT SegmentSelector = IdtEntry->Selector;
     ULONG  Offset          = MAKELONG(IdtEntry->Offset, IdtEntry->OffsetHigh);
@@ -287,7 +313,7 @@ Fast486InterruptInternal(PFAST486_STATE State,
             /* Task call */
             return Fast486TaskSwitch(State, FAST486_TASK_CALL, IdtEntry->Selector);
         }
-        
+
         if (GateSize != (State->SegmentRegs[FAST486_REG_CS].Size))
         {
             /* The gate size doesn't match the current operand size, so set the OPSIZE flag. */
@@ -400,6 +426,16 @@ Fast486InterruptInternal(PFAST486_STATE State,
     /* Push the instruction pointer */
     if (!Fast486StackPush(State, State->InstPtr.Long)) goto Cleanup;
 
+    if (PushErrorCode)
+    {
+        /* Push the error code */
+        if (!Fast486StackPush(State, ErrorCode))
+        {
+            /* An exception occurred */
+            goto Cleanup;
+        }
+    }
+
     if ((GateType == FAST486_IDT_INT_GATE) || (GateType == FAST486_IDT_INT_GATE_32))
     {
         /* Disable interrupts after a jump to an interrupt gate handler */
@@ -448,7 +484,7 @@ Fast486PerformInterrupt(PFAST486_STATE State,
     }
 
     /* Perform the interrupt */
-    if (!Fast486InterruptInternal(State, &IdtEntry))
+    if (!Fast486InterruptInternal(State, &IdtEntry, FALSE, 0))
     {
         /* Exception occurred */
         return FALSE;
@@ -463,6 +499,8 @@ Fast486ExceptionWithErrorCode(PFAST486_STATE State,
                               FAST486_EXCEPTIONS ExceptionCode,
                               ULONG ErrorCode)
 {
+    FAST486_IDT_ENTRY IdtEntry;
+
     /* Increment the exception count */
     State->ExceptionCount++;
 
@@ -491,8 +529,8 @@ Fast486ExceptionWithErrorCode(PFAST486_STATE State,
     /* Restore the IP to the saved IP */
     State->InstPtr = State->SavedInstPtr;
 
-    /* Perform the interrupt */
-    if (!Fast486PerformInterrupt(State, ExceptionCode))
+    /* Get the interrupt vector */
+    if (!Fast486GetIntVector(State, ExceptionCode, &IdtEntry))
     {
         /*
          * If this function failed, that means Fast486Exception
@@ -501,18 +539,18 @@ Fast486ExceptionWithErrorCode(PFAST486_STATE State,
         return;
     }
 
-    if (EXCEPTION_HAS_ERROR_CODE(ExceptionCode)
-        && (State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PE))
+    /* Perform the interrupt */
+    if (!Fast486InterruptInternal(State,
+                                  &IdtEntry,
+                                  EXCEPTION_HAS_ERROR_CODE(ExceptionCode)
+                                  && (State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PE),
+                                  ErrorCode))
     {
-        /* Push the error code */
-        if (!Fast486StackPush(State, ErrorCode))
-        {
-            /*
-             * If this function failed, that means Fast486Exception
-             * was called again, so just return in this case.
-             */
-            return;
-        }
+        /*
+         * If this function failed, that means Fast486Exception
+         * was called again, so just return in this case.
+         */
+        return;
     }
 
     /* Reset the exception count */
@@ -545,7 +583,7 @@ Fast486TaskSwitch(PFAST486_STATE State, FAST486_TASK_SWITCH_TYPE Type, USHORT Se
     /* Make sure the entry exists in the GDT (not LDT!) */
     if ((GET_SEGMENT_INDEX(Selector) == 0)
         || (Selector & SEGMENT_TABLE_INDICATOR)
-        || GET_SEGMENT_INDEX(Selector) >= (State->Gdtr.Size + 1))
+        || GET_SEGMENT_INDEX(Selector) >= (State->Gdtr.Size + 1u))
     {
         Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_TS, Selector);
         return FALSE;
@@ -598,7 +636,7 @@ Fast486TaskSwitch(PFAST486_STATE State, FAST486_TASK_SWITCH_TYPE Type, USHORT Se
         && ((NewTssDescriptor.Signature != FAST486_BUSY_TSS_SIGNATURE)
         || (Type != FAST486_TASK_RETURN)))
     {
-        Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector); 
+        Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
         return FALSE;
     }
 
@@ -702,11 +740,17 @@ Fast486TaskSwitch(PFAST486_STATE State, FAST486_TASK_SWITCH_TYPE Type, USHORT Se
     /* Flush the TLB */
     if (State->Tlb) RtlZeroMemory(State->Tlb, NUM_TLB_ENTRIES * sizeof(ULONG));
 
+    /* Update the CPL */
+    State->Cpl = GET_SEGMENT_RPL(NewTss.Cs);
+
 #ifndef FAST486_NO_PREFETCH
     /* Context switching invalidates the prefetch */
     State->PrefetchValid = FALSE;
 #endif
 
+    /* Update the CPL */
+    State->Cpl = GET_SEGMENT_RPL(NewTss.Cs);
+
     /* Load the registers */
     State->InstPtr.Long = State->SavedInstPtr.Long = NewTss.Eip;
     State->Flags.Long = NewTss.Eflags;