Sync with trunk r63502.
[reactos.git] / lib / fast486 / common.inl
index 175fb01..a558ed4 100644 (file)
@@ -281,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,
@@ -296,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,
@@ -319,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 */
@@ -329,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)
@@ -350,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;
@@ -358,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)
@@ -379,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;
@@ -542,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));
             }
         }
 
@@ -572,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;