Sync with trunk r63502.
[reactos.git] / lib / fast486 / common.inl
index 61e69a6..a558ed4 100644 (file)
@@ -128,6 +128,7 @@ Fast486ReadLinearMemory(PFAST486_STATE State,
         ULONG Page;
         FAST486_PAGE_TABLE TableEntry;
         INT Cpl = Fast486GetCurrentPrivLevel(State);
+        ULONG BufferOffset = 0;
 
         for (Page = PAGE_ALIGN(LinearAddress);
              Page <= PAGE_ALIGN(LinearAddress + Size - 1);
@@ -152,20 +153,23 @@ Fast486ReadLinearMemory(PFAST486_STATE State,
             {
                 /* Start reading from the offset from the beginning of the page */
                 PageOffset = PAGE_OFFSET(LinearAddress);
+                PageLength -= PageOffset;
             }
 
             /* Check if this is the last page */
             if (Page == PAGE_ALIGN(LinearAddress + Size - 1))
             {
                 /* Read only a part of the page */
-                PageLength = PAGE_OFFSET(LinearAddress) + Size - PageOffset;
+                PageLength = PAGE_OFFSET(LinearAddress + Size - 1) - PageOffset + 1;
             }
 
             /* Read the memory */
             State->MemReadCallback(State,
                                    (TableEntry.Address << 12) | PageOffset,
-                                   Buffer,
+                                   (PVOID)((ULONG_PTR)Buffer + BufferOffset),
                                    PageLength);
+
+            BufferOffset += PageLength;
         }
     }
     else
@@ -190,6 +194,7 @@ Fast486WriteLinearMemory(PFAST486_STATE State,
         ULONG Page;
         FAST486_PAGE_TABLE TableEntry;
         INT Cpl = Fast486GetCurrentPrivLevel(State);
+        ULONG BufferOffset = 0;
 
         for (Page = PAGE_ALIGN(LinearAddress);
              Page <= PAGE_ALIGN(LinearAddress + Size - 1);
@@ -216,20 +221,23 @@ Fast486WriteLinearMemory(PFAST486_STATE State,
             {
                 /* Start writing from the offset from the beginning of the page */
                 PageOffset = PAGE_OFFSET(LinearAddress);
+                PageLength -= PageOffset;
             }
 
             /* Check if this is the last page */
             if (Page == PAGE_ALIGN(LinearAddress + Size - 1))
             {
                 /* Write only a part of the page */
-                PageLength = PAGE_OFFSET(LinearAddress) + Size - PageOffset;
+                PageLength = PAGE_OFFSET(LinearAddress + Size - 1) - PageOffset + 1;
             }
 
             /* Write the memory */
             State->MemWriteCallback(State,
                                     (TableEntry.Address << 12) | PageOffset,
-                                    Buffer,
+                                    (PVOID)((ULONG_PTR)Buffer + BufferOffset),
                                     PageLength);
+
+            BufferOffset += PageLength;
         }
     }
     else
@@ -273,7 +281,7 @@ Fast486StackPush(PFAST486_STATE State,
         }
 
         /* Subtract ESP by 4 */
-        State->GeneralRegs[FAST486_REG_ESP].Long -= 4;
+        State->GeneralRegs[FAST486_REG_ESP].Long -= sizeof(ULONG);
 
         /* Store the value in SS:ESP */
         return Fast486WriteMemory(State,
@@ -288,14 +296,14 @@ Fast486StackPush(PFAST486_STATE State,
         USHORT ShortValue = LOWORD(Value);
 
         /* Check if SP is 1 */
-        if (State->GeneralRegs[FAST486_REG_ESP].Long == 1)
+        if (State->GeneralRegs[FAST486_REG_ESP].LowWord == 1)
         {
             Fast486Exception(State, FAST486_EXCEPTION_SS);
             return FALSE;
         }
 
         /* Subtract SP by 2 */
-        State->GeneralRegs[FAST486_REG_ESP].LowWord -= 2;
+        State->GeneralRegs[FAST486_REG_ESP].LowWord -= sizeof(USHORT);
 
         /* Store the value in SS:SP */
         return Fast486WriteMemory(State,
@@ -311,8 +319,6 @@ BOOLEAN
 Fast486StackPop(PFAST486_STATE State,
                 PULONG Value)
 {
-    ULONG LongValue;
-    USHORT ShortValue;
     BOOLEAN Size = State->SegmentRegs[FAST486_REG_CS].Size;
 
     /* The OPSIZE prefix toggles the size */
@@ -321,6 +327,7 @@ Fast486StackPop(PFAST486_STATE State,
     if (Size)
     {
         /* 32-bit size */
+        ULONG LongValue;
 
         /* Check if ESP is 0xFFFFFFFF */
         if (State->GeneralRegs[FAST486_REG_ESP].Long == 0xFFFFFFFF)
@@ -342,7 +349,7 @@ Fast486StackPop(PFAST486_STATE State,
         }
 
         /* Increment ESP by 4 */
-        State->GeneralRegs[FAST486_REG_ESP].Long += 4;
+        State->GeneralRegs[FAST486_REG_ESP].Long += sizeof(ULONG);
 
         /* Store the value in the result */
         *Value = LongValue;
@@ -350,6 +357,7 @@ Fast486StackPop(PFAST486_STATE State,
     else
     {
         /* 16-bit size */
+        USHORT ShortValue;
 
         /* Check if SP is 0xFFFF */
         if (State->GeneralRegs[FAST486_REG_ESP].LowWord == 0xFFFF)
@@ -371,7 +379,7 @@ Fast486StackPop(PFAST486_STATE State,
         }
 
         /* Increment SP by 2 */
-        State->GeneralRegs[FAST486_REG_ESP].LowWord += 2;
+        State->GeneralRegs[FAST486_REG_ESP].LowWord += sizeof(USHORT);
 
         /* Store the value in the result */
         *Value = ShortValue;
@@ -534,24 +542,32 @@ Fast486LoadSegment(PFAST486_STATE State,
         {
             /* Loading a data segment */
 
-            if (!GdtEntry.SystemType)
+            if (GET_SEGMENT_INDEX(Selector) != 0)
             {
-                /* This is a special descriptor */
-                Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
-                return FALSE;
-            }
+                if (!GdtEntry.SystemType)
+                {
+                    /* This is a special descriptor */
+                    Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
+                    return FALSE;
+                }
 
-            if ((GET_SEGMENT_RPL(Selector) > GdtEntry.Dpl)
-                || (Fast486GetCurrentPrivLevel(State) > GdtEntry.Dpl))
-            {
-                Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
-                return FALSE;
-            }
+                if ((GET_SEGMENT_RPL(Selector) > GdtEntry.Dpl)
+                    || (Fast486GetCurrentPrivLevel(State) > GdtEntry.Dpl))
+                {
+                    Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
+                    return FALSE;
+                }
 
-            if (!GdtEntry.Present)
+                if (!GdtEntry.Present)
+                {
+                    Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_NP, Selector);
+                    return FALSE;
+                }
+            }
+            else
             {
-                Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_NP, Selector);
-                return FALSE;
+                /* This is a NULL selector */
+                RtlZeroMemory(&GdtEntry, sizeof(GdtEntry));
             }
         }
 
@@ -564,6 +580,7 @@ Fast486LoadSegment(PFAST486_STATE State,
         CachedDescriptor->DirConf = GdtEntry.DirConf;
         CachedDescriptor->Executable = GdtEntry.Executable;
         CachedDescriptor->SystemType = GdtEntry.SystemType;
+        CachedDescriptor->Rpl = GET_SEGMENT_RPL(Selector);
         CachedDescriptor->Dpl = GdtEntry.Dpl;
         CachedDescriptor->Present = GdtEntry.Present;
         CachedDescriptor->Size = GdtEntry.Size;