[FAST486]
[reactos.git] / lib / fast486 / opgroups.c
index 27950cd..264fa66 100644 (file)
@@ -85,7 +85,7 @@ Fast486ArithmeticOperation(PFAST486_STATE State,
                               || ((Result < FirstValue) && (Result < (SecondValue + Carry)));
             State->Flags.Of = ((FirstValue & SignFlag) == (SecondValue & SignFlag))
                               && ((FirstValue & SignFlag) != (Result & SignFlag));
-            State->Flags.Af = ((((FirstValue & 0x0F) + ((SecondValue + Carry) & 0x0F)) & 0x10) != 0);
+            State->Flags.Af = ((FirstValue ^ SecondValue ^ Result) & 0x10) != 0;
 
             break;
         }
@@ -98,10 +98,12 @@ Fast486ArithmeticOperation(PFAST486_STATE State,
             Result = (FirstValue - SecondValue - Carry) & MaxValue;
 
             /* Update CF, OF and AF */
-            State->Flags.Cf = FirstValue < (SecondValue + Carry);
+            State->Flags.Cf = Carry
+                              ? (FirstValue <= SecondValue)
+                              : (FirstValue < SecondValue);
             State->Flags.Of = ((FirstValue & SignFlag) != (SecondValue & SignFlag))
                               && ((FirstValue & SignFlag) != (Result & SignFlag));
-            State->Flags.Af = (FirstValue & 0x0F) < ((SecondValue + Carry) & 0x0F);
+            State->Flags.Af = ((FirstValue ^ SecondValue ^ Result) & 0x10) != 0;
 
             break;
         }
@@ -167,8 +169,7 @@ Fast486RotateOperation(PFAST486_STATE State,
     /* Normalize the count */
     Count &= 0x1F;
 
-    if (Operation <= 1) Count %= Bits;
-    else if (Operation <= 3) Count %= Bits + 1;
+    if ((Operation == 2) || (Operation == 3)) Count %= Bits + 1;
 
     /* If the count is zero, do nothing */
     if (Count == 0) return Value;
@@ -179,6 +180,7 @@ Fast486RotateOperation(PFAST486_STATE State,
         /* ROL */
         case 0:
         {
+            Count %= Bits;
             Result = (Value << Count) | (Value >> (Bits - Count));
 
             /* Update CF and OF */
@@ -192,6 +194,7 @@ Fast486RotateOperation(PFAST486_STATE State,
         /* ROR */
         case 1:
         {
+            Count %= Bits;
             Result = (Value >> Count) | (Value << (Bits - Count));
 
             /* Update CF and OF */
@@ -292,7 +295,7 @@ Fast486RotateOperation(PFAST486_STATE State,
 
 FAST486_OPCODE_HANDLER(Fast486OpcodeGroup8082)
 {
-    UCHAR Immediate, Dummy, Value;
+    UCHAR Immediate, Value;
     FAST486_MOD_REG_RM ModRegRm;
     BOOLEAN AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
 
@@ -312,7 +315,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroup8082)
     }
 
     /* Read the operands */
-    if (!Fast486ReadModrmByteOperands(State, &ModRegRm, &Dummy, &Value))
+    if (!Fast486ReadModrmByteOperands(State, &ModRegRm, NULL, &Value))
     {
         /* Exception occurred */
         return FALSE;
@@ -348,7 +351,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroup81)
 
     if (OperandSize)
     {
-        ULONG Immediate, Value, Dummy;
+        ULONG Immediate, Value;
 
         /* Fetch the immediate operand */
         if (!Fast486FetchDword(State, &Immediate))
@@ -358,7 +361,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroup81)
         }
 
         /* Read the operands */
-        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, &Dummy, &Value))
+        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, NULL, &Value))
         {
             /* Exception occurred */
             return FALSE;
@@ -375,7 +378,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroup81)
     }
     else
     {
-        USHORT Immediate, Value, Dummy;
+        USHORT Immediate, Value;
 
         /* Fetch the immediate operand */
         if (!Fast486FetchWord(State, &Immediate))
@@ -385,7 +388,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroup81)
         }
 
         /* Read the operands */
-        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, &Dummy, &Value))
+        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, NULL, &Value))
         {
             /* Exception occurred */
             return FALSE;
@@ -431,10 +434,10 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroup83)
     if (OperandSize)
     {
         ULONG Immediate = (ULONG)((LONG)ImmByte); // Sign extend
-        ULONG Value, Dummy;
+        ULONG Value;
 
         /* Read the operands */
-        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, &Dummy, &Value))
+        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, NULL, &Value))
         {
             /* Exception occurred */
             return FALSE;
@@ -452,10 +455,10 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroup83)
     else
     {
         USHORT Immediate = (USHORT)((SHORT)ImmByte); // Sign extend
-        USHORT Value, Dummy;
+        USHORT Value;
 
         /* Read the operands */
-        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, &Dummy, &Value))
+        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, NULL, &Value))
         {
             /* Exception occurred */
             return FALSE;
@@ -485,23 +488,26 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroup8F)
     TOGGLE_OPSIZE(OperandSize);
     TOGGLE_ADSIZE(AddressSize);
 
-    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    /* Pop a value from the stack - this must be done first */
+    if (!Fast486StackPop(State, &Value))
     {
         /* Exception occurred */
         return FALSE;
     }
 
-    if (ModRegRm.Register != 0)
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
     {
-        /* Invalid */
-        Fast486Exception(State, FAST486_EXCEPTION_UD);
+        /* Exception occurred - restore SP */
+        if (OperandSize) State->GeneralRegs[FAST486_REG_ESP].Long -= sizeof(ULONG);
+        else State->GeneralRegs[FAST486_REG_ESP].LowWord -= sizeof(USHORT);
+
         return FALSE;
     }
 
-    /* Pop a value from the stack */
-    if (!Fast486StackPop(State, &Value))
+    if (ModRegRm.Register != 0)
     {
-        /* Exception occurred */
+        /* Invalid */
+        Fast486Exception(State, FAST486_EXCEPTION_UD);
         return FALSE;
     }
 
@@ -523,7 +529,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroup8F)
 
 FAST486_OPCODE_HANDLER(Fast486OpcodeGroupC0)
 {
-    UCHAR Dummy, Value, Count;
+    UCHAR Value, Count;
     FAST486_MOD_REG_RM ModRegRm;
     BOOLEAN AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
 
@@ -543,7 +549,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupC0)
     }
 
     /* Read the operands */
-    if (!Fast486ReadModrmByteOperands(State, &ModRegRm, &Dummy, &Value))
+    if (!Fast486ReadModrmByteOperands(State, &ModRegRm, NULL, &Value))
     {
         /* Exception occurred */
         return FALSE;
@@ -589,10 +595,10 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupC1)
 
     if (OperandSize)
     {
-        ULONG Dummy, Value;
+        ULONG Value;
 
         /* Read the operands */
-        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, &Dummy, &Value))
+        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, NULL, &Value))
         {
             /* Exception occurred */
             return FALSE;
@@ -610,10 +616,10 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupC1)
     }
     else
     {
-        USHORT Dummy, Value;
+        USHORT Value;
 
         /* Read the operands */
-        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, &Dummy, &Value))
+        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, NULL, &Value))
         {
             /* Exception occurred */
             return FALSE;
@@ -724,7 +730,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupC7)
 
 FAST486_OPCODE_HANDLER(Fast486OpcodeGroupD0)
 {
-    UCHAR Dummy, Value;
+    UCHAR Value;
     FAST486_MOD_REG_RM ModRegRm;
     BOOLEAN AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
 
@@ -737,7 +743,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupD0)
     }
 
     /* Read the operands */
-    if (!Fast486ReadModrmByteOperands(State, &ModRegRm, &Dummy, &Value))
+    if (!Fast486ReadModrmByteOperands(State, &ModRegRm, NULL, &Value))
     {
         /* Exception occurred */
         return FALSE;
@@ -772,10 +778,10 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupD1)
 
     if (OperandSize)
     {
-        ULONG Dummy, Value;
+        ULONG Value;
 
         /* Read the operands */
-        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, &Dummy, &Value))
+        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, NULL, &Value))
         {
             /* Exception occurred */
             return FALSE;
@@ -789,10 +795,10 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupD1)
     }
     else
     {
-        USHORT Dummy, Value;
+        USHORT Value;
 
         /* Read the operands */
-        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, &Dummy, &Value))
+        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, NULL, &Value))
         {
             /* Exception occurred */
             return FALSE;
@@ -808,7 +814,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupD1)
 
 FAST486_OPCODE_HANDLER(Fast486OpcodeGroupD2)
 {
-    UCHAR Dummy, Value;
+    UCHAR Value;
     FAST486_MOD_REG_RM ModRegRm;
     BOOLEAN AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
 
@@ -821,7 +827,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupD2)
     }
 
     /* Read the operands */
-    if (!Fast486ReadModrmByteOperands(State, &ModRegRm, &Dummy, &Value))
+    if (!Fast486ReadModrmByteOperands(State, &ModRegRm, NULL, &Value))
     {
         /* Exception occurred */
         return FALSE;
@@ -859,10 +865,10 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupD3)
 
     if (OperandSize)
     {
-        ULONG Dummy, Value;
+        ULONG Value;
 
         /* Read the operands */
-        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, &Dummy, &Value))
+        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, NULL, &Value))
         {
             /* Exception occurred */
             return FALSE;
@@ -880,10 +886,10 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupD3)
     }
     else
     {
-        USHORT Dummy, Value;
+        USHORT Value;
 
         /* Read the operands */
-        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, &Dummy, &Value))
+        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, NULL, &Value))
         {
             /* Exception occurred */
             return FALSE;
@@ -903,7 +909,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupD3)
 
 FAST486_OPCODE_HANDLER(Fast486OpcodeGroupF6)
 {
-    UCHAR Dummy, Value = 0;
+    UCHAR Value = 0;
     FAST486_MOD_REG_RM ModRegRm;
     BOOLEAN AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
 
@@ -916,7 +922,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupF6)
     }
 
     /* Read the operands */
-    if (!Fast486ReadModrmByteOperands(State, &ModRegRm, &Dummy, &Value))
+    if (!Fast486ReadModrmByteOperands(State, &ModRegRm, NULL, &Value))
     {
         /* Exception occurred */
         return FALSE;
@@ -1006,8 +1012,17 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupF6)
         /* DIV */
         case 6:
         {
-            UCHAR Quotient = State->GeneralRegs[FAST486_REG_EAX].LowWord / Value;
-            UCHAR Remainder = State->GeneralRegs[FAST486_REG_EAX].LowWord % Value;
+            UCHAR Quotient, Remainder;
+
+            if (Value == 0)
+            {
+                /* Divide error */
+                Fast486Exception(State, FAST486_EXCEPTION_DE);
+                return FALSE;
+            }
+
+            Quotient = State->GeneralRegs[FAST486_REG_EAX].LowWord / Value;
+            Remainder = State->GeneralRegs[FAST486_REG_EAX].LowWord % Value;
 
             /* Write back the results */
             State->GeneralRegs[FAST486_REG_EAX].LowByte = Quotient;
@@ -1019,8 +1034,17 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupF6)
         /* IDIV */
         case 7:
         {
-            CHAR Quotient = (SHORT)State->GeneralRegs[FAST486_REG_EAX].LowWord / (CHAR)Value;
-            CHAR Remainder = (SHORT)State->GeneralRegs[FAST486_REG_EAX].LowWord % (CHAR)Value;
+            CHAR Quotient, Remainder;
+
+            if (Value == 0)
+            {
+                /* Divide error */
+                Fast486Exception(State, FAST486_EXCEPTION_DE);
+                return FALSE;
+            }
+
+            Quotient = (SHORT)State->GeneralRegs[FAST486_REG_EAX].LowWord / (CHAR)Value;
+            Remainder = (SHORT)State->GeneralRegs[FAST486_REG_EAX].LowWord % (CHAR)Value;
 
             /* Write back the results */
             State->GeneralRegs[FAST486_REG_EAX].LowByte = (UCHAR)Quotient;
@@ -1035,7 +1059,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupF6)
 
 FAST486_OPCODE_HANDLER(Fast486OpcodeGroupF7)
 {
-    ULONG Dummy, Value = 0, SignFlag;
+    ULONG Value = 0, SignFlag;
     FAST486_MOD_REG_RM ModRegRm;
     BOOLEAN OperandSize, AddressSize;
 
@@ -1058,7 +1082,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupF7)
     if (OperandSize)
     {
         /* 32-bit */
-        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, &Dummy, &Value))
+        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, NULL, &Value))
         {
             /* Exception occurred */
             return FALSE;
@@ -1067,7 +1091,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupF7)
     else
     {
         /* 16-bit */
-        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, (PUSHORT)&Dummy, (PUSHORT)&Value))
+        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, NULL, (PUSHORT)&Value))
         {
             /* Exception occurred */
             return FALSE;
@@ -1219,6 +1243,13 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupF7)
         /* DIV */
         case 6:
         {
+            if (Value == 0)
+            {
+                /* Divide error */
+                Fast486Exception(State, FAST486_EXCEPTION_DE);
+                return FALSE;
+            }
+
             if (OperandSize)
             {
                 ULONGLONG Dividend = (ULONGLONG)State->GeneralRegs[FAST486_REG_EAX].Long
@@ -1248,6 +1279,13 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupF7)
         /* IDIV */
         case 7:
         {
+            if (Value == 0)
+            {
+                /* Divide error */
+                Fast486Exception(State, FAST486_EXCEPTION_DE);
+                return FALSE;
+            }
+
             if (OperandSize)
             {
                 LONGLONG Dividend = (LONGLONG)State->GeneralRegs[FAST486_REG_EAX].Long
@@ -1280,7 +1318,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupF7)
 
 FAST486_OPCODE_HANDLER(Fast486OpcodeGroupFE)
 {
-    UCHAR Dummy, Value;
+    UCHAR Value;
     FAST486_MOD_REG_RM ModRegRm;
     BOOLEAN AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
 
@@ -1300,7 +1338,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupFE)
     }
 
     /* Read the operands */
-    if (!Fast486ReadModrmByteOperands(State, &ModRegRm, &Dummy, &Value))
+    if (!Fast486ReadModrmByteOperands(State, &ModRegRm, NULL, &Value))
     {
         /* Exception occurred */
         return FALSE;
@@ -1359,9 +1397,9 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupFF)
     /* Read the operands */
     if (OperandSize)
     {
-        ULONG Dummy, Value;
+        ULONG Value;
 
-        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, &Dummy, &Value))
+        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, NULL, &Value))
         {
             /* Exception occurred */
             return FALSE;
@@ -1396,7 +1434,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupFF)
         else if (ModRegRm.Register == 3)
         {
             USHORT Selector;
-            INT Segment = FAST486_REG_DS;
+            FAST486_SEG_REGS Segment = FAST486_REG_DS;
 
             /* Check for the segment override */
             if (State->PrefixFlags & FAST486_PREFIX_SEG)
@@ -1449,7 +1487,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupFF)
         else if (ModRegRm.Register == 5)
         {
             USHORT Selector;
-            INT Segment = FAST486_REG_DS;
+            FAST486_SEG_REGS Segment = FAST486_REG_DS;
 
             /* Check for the segment override */
             if (State->PrefixFlags & FAST486_PREFIX_SEG)
@@ -1502,9 +1540,9 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupFF)
     }
     else
     {
-        USHORT Dummy, Value;
+        USHORT Value;
 
-        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, &Dummy, &Value))
+        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, NULL, &Value))
         {
             /* Exception occurred */
             return FALSE;
@@ -1535,11 +1573,14 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupFF)
 
             /* Set the IP to the address */
             State->InstPtr.LowWord = Value;
+
+            /* Clear the top half of EIP */
+            State->InstPtr.Long &= 0xFFFF;
         }
         else if (ModRegRm.Register == 3)
         {
             USHORT Selector;
-            INT Segment = FAST486_REG_DS;
+            FAST486_SEG_REGS Segment = FAST486_REG_DS;
 
             /* Check for the segment override */
             if (State->PrefixFlags & FAST486_PREFIX_SEG)
@@ -1584,16 +1625,21 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupFF)
             /* Set the IP to the address */
             State->InstPtr.LowWord = Value;
 
+            /* Clear the top half of EIP */
+            State->InstPtr.Long &= 0xFFFF;
         }
         else if (ModRegRm.Register == 4)
         {
             /* Set the IP to the address */
             State->InstPtr.LowWord = Value;
+
+            /* Clear the top half of EIP */
+            State->InstPtr.Long &= 0xFFFF;
         }
         else if (ModRegRm.Register == 5)
         {
             USHORT Selector;
-            INT Segment = FAST486_REG_DS;
+            FAST486_SEG_REGS Segment = FAST486_REG_DS;
 
             /* Check for the segment override */
             if (State->PrefixFlags & FAST486_PREFIX_SEG)
@@ -1623,6 +1669,9 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupFF)
 
             /* Set the IP to the address */
             State->InstPtr.LowWord = Value;
+
+            /* Clear the top half of EIP */
+            State->InstPtr.Long &= 0xFFFF;
         }
         else if (ModRegRm.Register == 6)
         {
@@ -1654,6 +1703,317 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroupFF)
     return TRUE;
 }
 
+FAST486_OPCODE_HANDLER(Fast486OpcodeGroup0F00)
+{
+    FAST486_MOD_REG_RM ModRegRm;
+    BOOLEAN AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+
+    NO_LOCK_PREFIX();
+    TOGGLE_ADSIZE(AddressSize);
+
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* Check which operation this is */
+    switch (ModRegRm.Register)
+    {
+        /* SLDT */
+        case 0:
+        {
+            /* Not recognized in real mode or virtual 8086 mode */
+            if (!(State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PE)
+                || State->Flags.Vm)
+            {
+                Fast486Exception(State, FAST486_EXCEPTION_UD);
+                return FALSE;
+            }
+
+            return Fast486WriteModrmWordOperands(State,
+                                                 &ModRegRm,
+                                                 FALSE,
+                                                 State->Ldtr.Selector);
+        }
+
+        /* STR */
+        case 1:
+        {
+            /* Not recognized in real mode or virtual 8086 mode */
+            if (!(State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PE)
+                || State->Flags.Vm)
+            {
+                Fast486Exception(State, FAST486_EXCEPTION_UD);
+                return FALSE;
+            }
+
+            return Fast486WriteModrmWordOperands(State,
+                                                 &ModRegRm,
+                                                 FALSE,
+                                                 State->TaskReg.Selector);
+        }
+
+        /* LLDT */
+        case 2:
+        {
+            USHORT Selector;
+            FAST486_SYSTEM_DESCRIPTOR GdtEntry;
+
+            /* Not recognized in real mode or virtual 8086 mode */
+            if (!(State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PE)
+                || State->Flags.Vm)
+            {
+                Fast486Exception(State, FAST486_EXCEPTION_UD);
+                return FALSE;
+            }
+
+            /* This is a privileged instruction */
+            if (Fast486GetCurrentPrivLevel(State) != 0)
+            {
+                Fast486Exception(State, FAST486_EXCEPTION_GP);
+                return FALSE;
+            }
+
+            if (!Fast486ReadModrmWordOperands(State,
+                                              &ModRegRm,
+                                              NULL,
+                                              &Selector))
+            {
+                /* Exception occurred */
+                return FALSE;
+            }
+
+            /* Make sure the GDT contains the entry */
+            if (GET_SEGMENT_INDEX(Selector) >= (State->Gdtr.Size + 1))
+            {
+                Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
+                return FALSE;
+            }
+
+            /* Read the GDT */
+            if (!Fast486ReadLinearMemory(State,
+                                         State->Gdtr.Address
+                                         + GET_SEGMENT_INDEX(Selector),
+                                         &GdtEntry,
+                                         sizeof(GdtEntry)))
+            {
+                /* Exception occurred */
+                return FALSE;
+            }
+
+            if (GET_SEGMENT_INDEX(Selector) == 0)
+            {
+                RtlZeroMemory(&State->Ldtr, sizeof(State->Ldtr));
+                return TRUE;
+            }
+
+            if (!GdtEntry.Present)
+            {
+                Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_NP, Selector);
+                return FALSE;
+            }
+
+            if (GdtEntry.Signature != FAST486_LDT_SIGNATURE)
+            {
+                /* This is not a LDT descriptor */
+                Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
+                return FALSE;
+            }
+
+            /* Update the LDTR */
+            State->Ldtr.Selector = Selector;
+            State->Ldtr.Base = GdtEntry.Base | (GdtEntry.BaseMid << 16) | (GdtEntry.BaseHigh << 24);
+            State->Ldtr.Limit = GdtEntry.Limit | (GdtEntry.LimitHigh << 16);
+            if (GdtEntry.Granularity) State->Ldtr.Limit <<= 12;
+
+            return TRUE;
+        }
+
+        /* LTR */
+        case 3:
+        {
+            USHORT Selector;
+            FAST486_SYSTEM_DESCRIPTOR GdtEntry;
+
+            /* Not recognized in real mode or virtual 8086 mode */
+            if (!(State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PE)
+                || State->Flags.Vm)
+            {
+                Fast486Exception(State, FAST486_EXCEPTION_UD);
+                return FALSE;
+            }
+
+            /* This is a privileged instruction */
+            if (Fast486GetCurrentPrivLevel(State) != 0)
+            {
+                Fast486Exception(State, FAST486_EXCEPTION_GP);
+                return FALSE;
+            }
+
+            if (!Fast486ReadModrmWordOperands(State,
+                                              &ModRegRm,
+                                              NULL,
+                                              &Selector))
+            {
+                /* Exception occurred */
+                return FALSE;
+            }
+
+            /* Make sure the GDT contains the entry */
+            if (GET_SEGMENT_INDEX(Selector) >= (State->Gdtr.Size + 1))
+            {
+                Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
+                return FALSE;
+            }
+
+            /* Read the GDT */
+            if (!Fast486ReadLinearMemory(State,
+                                         State->Gdtr.Address
+                                         + GET_SEGMENT_INDEX(Selector),
+                                         &GdtEntry,
+                                         sizeof(GdtEntry)))
+            {
+                /* Exception occurred */
+                return FALSE;
+            }
+
+            if (GET_SEGMENT_INDEX(Selector) == 0)
+            {
+                Fast486Exception(State, FAST486_EXCEPTION_GP);
+                return FALSE;
+            }
+
+            if (!GdtEntry.Present)
+            {
+                Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_NP, Selector);
+                return FALSE;
+            }
+
+            if (GdtEntry.Signature != FAST486_TSS_SIGNATURE)
+            {
+                /* This is not a TSS descriptor */
+                Fast486ExceptionWithErrorCode(State, FAST486_EXCEPTION_GP, Selector);
+                return FALSE;
+            }
+
+            /* Update the TR */
+            State->TaskReg.Selector = Selector;
+            State->TaskReg.Base = GdtEntry.Base | (GdtEntry.BaseMid << 16) | (GdtEntry.BaseHigh << 24);
+            State->TaskReg.Limit = GdtEntry.Limit | (GdtEntry.LimitHigh << 16);
+            if (GdtEntry.Granularity) State->TaskReg.Limit <<= 12;
+            State->TaskReg.Busy = TRUE;
+
+            return TRUE;
+        }
+
+        /* VERR/VERW */
+        case 4:
+        case 5:
+        {
+            USHORT Selector;
+            FAST486_GDT_ENTRY GdtEntry;
+
+            /* Not recognized in real mode or virtual 8086 mode */
+            if (!(State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PE)
+                || State->Flags.Vm)
+            {
+                Fast486Exception(State, FAST486_EXCEPTION_UD);
+                return FALSE;
+            }
+
+            /* This is a privileged instruction */
+            if (Fast486GetCurrentPrivLevel(State) != 0)
+            {
+                Fast486Exception(State, FAST486_EXCEPTION_GP);
+                return FALSE;
+            }
+
+            if (!Fast486ReadModrmWordOperands(State,
+                                              &ModRegRm,
+                                              NULL,
+                                              &Selector))
+            {
+                /* Exception occurred */
+                return FALSE;
+            }
+
+            if (!(Selector & SEGMENT_TABLE_INDICATOR))
+            {
+                /* Make sure the GDT contains the entry */
+                if (GET_SEGMENT_INDEX(Selector) >= (State->Gdtr.Size + 1))
+                {
+                    /* Clear ZF */
+                    State->Flags.Zf = FALSE;
+                    return TRUE;
+                }
+
+                /* Read the GDT */
+                if (!Fast486ReadLinearMemory(State,
+                                             State->Gdtr.Address
+                                             + GET_SEGMENT_INDEX(Selector),
+                                             &GdtEntry,
+                                             sizeof(GdtEntry)))
+                {
+                    /* Exception occurred */
+                    return FALSE;
+                }
+            }
+            else
+            {
+                /* Make sure the LDT contains the entry */
+                if (GET_SEGMENT_INDEX(Selector) >= (State->Ldtr.Limit + 1))
+                {
+                    /* Clear ZF */
+                    State->Flags.Zf = FALSE;
+                    return TRUE;
+                }
+
+                /* Read the LDT */
+                if (!Fast486ReadLinearMemory(State,
+                                             State->Ldtr.Base
+                                             + GET_SEGMENT_INDEX(Selector),
+                                             &GdtEntry,
+                                             sizeof(GdtEntry)))
+                {
+                    /* Exception occurred */
+                    return FALSE;
+                }
+            }
+
+            /* Set ZF if it is valid and accessible */
+            State->Flags.Zf = GdtEntry.Present // must be present
+                               && GdtEntry.SystemType // must be a segment
+                               && (((ModRegRm.Register == 4)
+                               /* code segments are only readable if the RW bit is set */
+                               && (!GdtEntry.Executable || GdtEntry.ReadWrite))
+                               || ((ModRegRm.Register == 5)
+                               /* code segments are never writable, data segments are writable when RW is set */
+                               && (!GdtEntry.Executable && GdtEntry.ReadWrite)))
+                               /*
+                                * for segments other than conforming code segments,
+                                * both RPL and CPL must be less than or equal to DPL
+                                */
+                               && ((!GdtEntry.Executable || !GdtEntry.DirConf)
+                               && ((GET_SEGMENT_RPL(Selector) <= GdtEntry.Dpl)
+                               && (Fast486GetCurrentPrivLevel(State) <= GdtEntry.Dpl)))
+                               /* for conforming code segments, DPL must be less than or equal to CPL */
+                               && ((GdtEntry.Executable && GdtEntry.DirConf)
+                               && (GdtEntry.Dpl <= Fast486GetCurrentPrivLevel(State)));
+
+
+            return TRUE;
+        }
+
+        /* Invalid */
+        default:
+        {
+            Fast486Exception(State, FAST486_EXCEPTION_UD);
+            return FALSE;
+        }
+    }
+}
+
 FAST486_OPCODE_HANDLER(Fast486OpcodeGroup0F01)
 {
     UCHAR TableReg[6];
@@ -1664,6 +2024,12 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroup0F01)
     NO_LOCK_PREFIX();
     TOGGLE_ADSIZE(AddressSize);
 
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
     /* Check for the segment override */
     if (State->PrefixFlags & FAST486_PREFIX_SEG)
     {
@@ -1671,12 +2037,6 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroup0F01)
         Segment = State->SegmentOverride;
     }
 
-    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
-    {
-        /* Exception occurred */
-        return FALSE;
-    }
-
     /* Check which operation this is */
     switch (ModRegRm.Register)
     {
@@ -1799,7 +2159,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroup0F01)
         /* SMSW */
         case 4:
         {
-            /* Store the lower 16 bits of CR0 */
+            /* Store the lower 16 bits (Machine Status Word) of CR0 */
             return Fast486WriteModrmWordOperands(State,
                                                  &ModRegRm,
                                                  FALSE,
@@ -1809,7 +2169,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroup0F01)
         /* LMSW */
         case 6:
         {
-            USHORT MasterStatusWord, Dummy;
+            USHORT MachineStatusWord;
 
             /* This is a privileged instruction */
             if (Fast486GetCurrentPrivLevel(State) != 0)
@@ -1818,8 +2178,8 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroup0F01)
                 return FALSE;
             }
 
-            /* Read the new master status word */
-            if (!Fast486ReadModrmWordOperands(State, &ModRegRm, &Dummy, &MasterStatusWord))
+            /* Read the new Machine Status Word */
+            if (!Fast486ReadModrmWordOperands(State, &ModRegRm, NULL, &MachineStatusWord))
             {
                 /* Exception occurred */
                 return FALSE;
@@ -1827,7 +2187,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroup0F01)
 
             /* This instruction cannot be used to return to real mode */
             if ((State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PE)
-                && !(MasterStatusWord & FAST486_CR0_PE))
+                && !(MachineStatusWord & FAST486_CR0_PE))
             {
                 Fast486Exception(State, FAST486_EXCEPTION_GP);
                 return FALSE;
@@ -1835,7 +2195,7 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroup0F01)
 
             /* Set the lowest 4 bits */
             State->ControlRegisters[FAST486_REG_CR0] &= 0xFFFFFFF0;
-            State->ControlRegisters[FAST486_REG_CR0] |= MasterStatusWord & 0x0F;
+            State->ControlRegisters[FAST486_REG_CR0] |= MachineStatusWord & 0x0F;
 
             return TRUE;
         }
@@ -1920,14 +2280,14 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroup0FBA)
     }
 
     /* Normalize the bit number */
-    BitNumber &= (1 << DataSize) - 1;
+    BitNumber %= DataSize;
 
     if (OperandSize)
     {
-        ULONG Dummy, Value;
+        ULONG Value;
 
         /* Read the value */
-        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, &Dummy, &Value))
+        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, NULL, &Value))
         {
             /* Exception occurred */
             return FALSE;
@@ -1964,10 +2324,10 @@ FAST486_OPCODE_HANDLER(Fast486OpcodeGroup0FBA)
     }
     else
     {
-        USHORT Dummy, Value;
+        USHORT Value;
 
         /* Read the value */
-        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, &Dummy, &Value))
+        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, NULL, &Value))
         {
             /* Exception occurred */
             return FALSE;