Skip to contentSkip to conversation

x86emul: VEX.B is ignored in compatibility mode

PATCH In review Jan Beulich 2 participants 4 messages Jan 12, 2017 – Jan 16, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86emul: VEX.B is ignored in compatibility mode

While VEX.R and VEX.X are guaranteed to be 1 in compatibility mode, VEX.B can be encoded as zero, but would be ignored by the processor. Since we emulate instructions in 64-bit mode, we need to force the bit to 1 in order to not act on the wrong {X,Y,Z}MM register.

We must not, however, fiddle with the high bit of VEX.VVVV in the decode phase, as that would undermine the checking of instructions requiring the field to be all ones independent of mode. This is being enforced in copy_REX_VEX() instead.

xen/arch/x86/x86_emulate/x86_emulate.c +7 -1
@@ -331,7 +331,11 @@ union vex {
 
 #define copy_REX_VEX(ptr, rex, vex) do { \
     if ( (vex).opcx != vex_none ) \
+    { \
+        if ( !mode_64bit() ) \
+            vex.reg |= 8; \
         ptr[0] = 0xc4, ptr[1] = (vex).raw[0], ptr[2] = (vex).raw[1]; \
+    } \
     else if ( mode_64bit() ) \
         ptr[1] = rex | REX_PREFIX; \
 } while (0)
@@ -2217,6 +2221,8 @@ x86_decode(
                             op_bytes = 8;
                         }
                     }
+                    else
+                        vex.b = 1;
                     switch ( b )
                     {
                     case 0x62:
@@ -2235,7 +2241,7 @@ x86_decode(
                         break;
                     }
                 }
-                if ( mode_64bit() && !vex.r )
+                if ( !vex.r )
                     rex_prefix |= REX_R;
 
                 ext = vex.opcx;

x86emul: VEX.B is ignored in compatibility mode

While VEX.R and VEX.X are guaranteed to be 1 in compatibility mode, VEX.B can be encoded as zero, but would be ignored by the processor. Since we emulate instructions in 64-bit mode, we need to force the bit to 1 in order to not act on the wrong {X,Y,Z}MM register.

We must not, however, fiddle with the high bit of VEX.VVVV in the decode phase, as that would undermine the checking of instructions requiring the field to be all ones independent of mode. This is being enforced in copy_REX_VEX() instead.

xen/arch/x86/x86_emulate/x86_emulate.c +7 -1
@@ -331,7 +331,11 @@ union vex {
 
 #define copy_REX_VEX(ptr, rex, vex) do { \
     if ( (vex).opcx != vex_none ) \
+    { \
+        if ( !mode_64bit() ) \
+            vex.reg |= 8; \
         ptr[0] = 0xc4, ptr[1] = (vex).raw[0], ptr[2] = (vex).raw[1]; \
+    } \
     else if ( mode_64bit() ) \
         ptr[1] = rex | REX_PREFIX; \
 } while (0)
@@ -2217,6 +2221,8 @@ x86_decode(
                             op_bytes = 8;
                         }
                     }
+                    else
+                        vex.b = 1;
                     switch ( b )
                     {
                     case 0x62:
@@ -2235,7 +2241,7 @@ x86_decode(
                         break;
                     }
                 }
-                if ( mode_64bit() && !vex.r )
+                if ( !vex.r )
                     rex_prefix |= REX_R;
 
                 ext = vex.opcx;

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#1 lore raw
Quoted 2 lines · Jan Beulich

While VEX.R and VEX.X are guaranteed to be 1 in compatibility mode, VEX.B can be encoded as zero, but would be ignored by the processor.

Really? That is unfortunate.

It would have been far more helpful for this to raise #UD, like the other prohibited VEX encodings.

xen/arch/x86/x86_emulate/x86_emulate.c x86_decode( · line 2247

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +1 -1
@@ -2235,7 +2241,7 @@ x86_decode(
                         break;
                     }
                 }
-                if ( mode_64bit() && !vex.r )
+                if ( !vex.r )
                     rex_prefix |= REX_R;
 
                 ext = vex.opcx;

What is the purpose of this change? I doesn't appear to be related to the rest of the patch.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#2 lore raw
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 12.01.17 at 18:29, <[email protected]> wrote:
Quoted 2 lines · Jan Beulich

While VEX.R and VEX.X are guaranteed to be 1 in compatibility mode, VEX.B can be encoded as zero, but would be ignored by the processor.

Really? That is unfortunate.

It would have been far more helpful for this to raise #UD, like the other prohibited VEX encodings.

It's spelled out that way in the doc, and to be sure I've just again verified this to be the case in practice.

Quoted 8 lines
Quoted 8 lines
xen/arch/x86/x86_emulate/x86_emulate.c +1 -1
@@ -2235,7 +2241,7 @@ x86_decode(
                         break;
                     }
                 }
-                if ( mode_64bit() && !vex.r )
+                if ( !vex.r )
                     rex_prefix |= REX_R;
 
Quoted 5 lines

Review x86_decode( · line 2247

Quoted 2 lines
(quoted diff) +0 -0
                 ext = vex.opcx;

What is the purpose of this change? I doesn't appear to be related to the rest of the patch.

It is related - see the first half of the first sentence of the description (still visible above).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#3 lore raw
Quoted 9 lines · Jan Beulich
Quoted 9 lines
Quoted 9 lines
xen/arch/x86/x86_emulate/x86_emulate.c +1 -1
@@ -2235,7 +2241,7 @@ x86_decode(
                         break;
                     }
                 }
-                if ( mode_64bit() && !vex.r )
+                if ( !vex.r )
                     rex_prefix |= REX_R;
 
Quoted 6 lines
Quoted 4 lines

Review x86_decode( · line 2247

Quoted 2 lines
(quoted diff) +0 -0
                 ext = vex.opcx;

What is the purpose of this change? I doesn't appear to be related to the rest of the patch.

It is related - see the first half of the first sentence of the description (still visible above).

Ah ok, in which case it is simply an optimisation rather than a functional change.

With that at least mentioned in the commit message, Reviewed-by: Andrew Cooper <[email protected]>

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel