Skip to contentSkip to conversation

x86emul: correct LFS et al handling for 64-bit mode

PATCH In review Jan Beulich 2 participants 7 messages Dec 11, 2019 – Dec 12, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86emul: correct LFS et al handling for 64-bit mode

AMD and friends explicitly specify that 64-bit operands aren't possible for these insns. Nevertheless REX.W isn't fully ignored: It still cancels a possible operand size override (0x66). Intel otoh explicitly provides for 64-bit operands on the respective insn page of the SDM.

xen/arch/x86/x86_emulate/x86_emulate.c +9 -0
@@ -2640,6 +2640,15 @@ x86_decode_twobyte(
         }
         break;
 
+    case 0xb2: /* lss */
+    case 0xb4: /* lfs */
+    case 0xb5: /* lgs */
+        /* REX.W ignored on a vendor-dependent basis. */
+        if ( op_bytes == 8 &&
+             (ctxt->cpuid->x86_vendor & (X86_VENDOR_AMD | X86_VENDOR_HYGON)) )
+            op_bytes = 4;
+        break;
+
     case 0xb8: /* jmpe / popcnt */
         if ( rep_prefix() )
             ctxt->opcode |= MASK_INSR(vex.pfx, X86EMUL_OPC_PFX_MASK);

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

#1 lore raw
Quoted 6 lines · Jan Beulich

AMD and friends explicitly specify that 64-bit operands aren't possible for these insns. Nevertheless REX.W isn't fully ignored: It still cancels a possible operand size override (0x66). Intel otoh explicitly provides for 64-bit operands on the respective insn page of the SDM.

It is definitely more than just these.  Near jumps have per-vendor behaviour on how long the instruction is, whereas far jump/calls are in the same category as these by the looks of things.

~Andrew

Quoted 9 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2 -0
@@ -2640,6 +2640,15 @@ x86_decode_twobyte(
         }
         break;
 
+    case 0xb2: /* lss */
+    case 0xb4: /* lfs */

xen/arch/x86/x86_emulate/x86_emulate.c x86_decode_twobyte( · line 2654

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +7 -0
+    case 0xb5: /* lgs */
+        /* REX.W ignored on a vendor-dependent basis. */
+        if ( op_bytes == 8 &&
+             (ctxt->cpuid->x86_vendor & (X86_VENDOR_AMD | X86_VENDOR_HYGON)) )
+            op_bytes = 4;
+        break;
+
     case 0xb8: /* jmpe / popcnt */
         if ( rep_prefix() )
             ctxt->opcode |= MASK_INSR(vex.pfx, X86EMUL_OPC_PFX_MASK);

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

#2 lore raw
Quoted 11 lines · Andrew Cooper
Quoted 6 lines · Jan Beulich

AMD and friends explicitly specify that 64-bit operands aren't possible for these insns. Nevertheless REX.W isn't fully ignored: It still cancels a possible operand size override (0x66). Intel otoh explicitly provides for 64-bit operands on the respective insn page of the SDM.

It is definitely more than just these.  Near jumps have per-vendor behaviour on how long the instruction is, whereas far jump/calls are in the same category as these by the looks of things.

But you don't expect me to fold all of these into one patch, do you? We have _some_ vendor dependent behavior already, and I'm slowly adding to it. Our far call/jmp support is rather incomplete in other ways anyway. (As an aside, I'm trying to do the binutils side of some of these before wanting to do the emulator parts. I'm only about to get to the far call/jmp issues there, as reported by you in bug 24546.)

I'd appreciate if you could clarify what exactly your reply means for the patch at hand.

Jan

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

#3 lore raw
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Andrew Cooper
Quoted 5 lines · Jan Beulich

AMD and friends explicitly specify that 64-bit operands aren't possible for these insns. Nevertheless REX.W isn't fully ignored: It still cancels a possible operand size override (0x66). Intel otoh explicitly provides for 64-bit operands on the respective insn page of the SDM.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

It is definitely more than just these.  Near jumps have per-vendor behaviour on how long the instruction is, whereas far jump/calls are in the same category as these by the looks of things.

But you don't expect me to fold all of these into one patch, do you?

short jmp certainly not, but far jmp/call is just two extra case statements in this new code block, no?

Quoted 3 lines

We have _some_ vendor dependent behavior already, and I'm slowly adding to it. Our far call/jmp support is rather incomplete in other ways anyway.

There is different truncation behaviour for %rip which needs altering, but that is a separate area of code.  Anything else?

~Andrew

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

#4 lore raw
Quoted 9 lines · Andrew Cooper
Quoted 8 lines · Jan Beulich
Quoted 7 lines · Andrew Cooper
Quoted 6 lines · Jan Beulich

AMD and friends explicitly specify that 64-bit operands aren't possible for these insns. Nevertheless REX.W isn't fully ignored: It still cancels a possible operand size override (0x66). Intel otoh explicitly provides for 64-bit operands on the respective insn page of the SDM.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

It is definitely more than just these.  Near jumps have per-vendor behaviour on how long the instruction is, whereas far jump/calls are in the same category as these by the looks of things.

But you don't expect me to fold all of these into one patch, do you?

short jmp certainly not, but far jmp/call is just two extra case statements in this new code block, no?

Not exactly (the other change would need to be in x86_decode_onebyte() and depend on ModRM.reg), but yes, I can do this. Yet then it would feel odd to not also deal with the near counterparts at the same time. Which in turn would make is desirable to also deal with near RET as well. At which point we're about to also discuss CALL/JMP with displacement operands and Jcc.

Quoted 6 lines
Quoted 3 lines

We have _some_ vendor dependent behavior already, and I'm slowly adding to it. Our far call/jmp support is rather incomplete in other ways anyway.

There is different truncation behaviour for %rip which needs altering, but that is a separate area of code.  Anything else?

protmode_load_seg() and MOVSXD already have vendor dependent code, if that was your question. For things needing doing see above plus LOOP, J[ER]CXZ, SYSENTER, and SYSEXIT as far as I'm currently aware.

Jan

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

#5 lore raw
Quoted 25 lines · Jan Beulich
Quoted 8 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Andrew Cooper
Quoted 5 lines · Jan Beulich

AMD and friends explicitly specify that 64-bit operands aren't possible for these insns. Nevertheless REX.W isn't fully ignored: It still cancels a possible operand size override (0x66). Intel otoh explicitly provides for 64-bit operands on the respective insn page of the SDM.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

It is definitely more than just these.  Near jumps have per-vendor behaviour on how long the instruction is, whereas far jump/calls are in the same category as these by the looks of things.

But you don't expect me to fold all of these into one patch, do you?

short jmp certainly not, but far jmp/call is just two extra case statements in this new code block, no?

Not exactly (the other change would need to be in x86_decode_onebyte() and depend on ModRM.reg), but yes, I can do this. Yet then it would feel odd to not also deal with the near counterparts at the same time. Which in turn would make is desirable to also deal with near RET as well. At which point we're about to also discuss CALL/JMP with displacement operands and Jcc.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

We have _some_ vendor dependent behavior already, and I'm slowly adding to it. Our far call/jmp support is rather incomplete in other ways anyway.

There is different truncation behaviour for %rip which needs altering, but that is a separate area of code.  Anything else?

protmode_load_seg() and MOVSXD already have vendor dependent code, if that was your question.

I was actually just asking about far jmp/call.

If you're sure that far jmp/call is more complicated than just tweaking this patch, then fine.  Acked-by: Andrew Cooper <[email protected]>

Quoted 3 lines

For things needing doing see
above plus LOOP, J[ER]CXZ, SYSENTER, and SYSEXIT as far as I'm currently aware.

SYSCALL and SYSRET as well.  The way they handle MSR_STAR is vendor specific, as well as #UD conditions.

I've just noticed that I've still got an XSA-204 followup patch still outstanding from 2016...

~Andrew

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

#6 lore raw
Quoted 31 lines · Andrew Cooper
Quoted 30 lines · Jan Beulich
Quoted 8 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Andrew Cooper
Quoted 5 lines · Jan Beulich

AMD and friends explicitly specify that 64-bit operands aren't possible for these insns. Nevertheless REX.W isn't fully ignored: It still cancels a possible operand size override (0x66). Intel otoh explicitly provides for 64-bit operands on the respective insn page of the SDM.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

It is definitely more than just these.  Near jumps have per-vendor behaviour on how long the instruction is, whereas far jump/calls are in the same category as these by the looks of things.

But you don't expect me to fold all of these into one patch, do you?

short jmp certainly not, but far jmp/call is just two extra case statements in this new code block, no?

Not exactly (the other change would need to be in x86_decode_onebyte() and depend on ModRM.reg), but yes, I can do this. Yet then it would feel odd to not also deal with the near counterparts at the same time. Which in turn would make is desirable to also deal with near RET as well. At which point we're about to also discuss CALL/JMP with displacement operands and Jcc.

Quoted 5 lines
Quoted 3 lines

We have _some_ vendor dependent behavior already, and I'm slowly adding to it. Our far call/jmp support is rather incomplete in other ways anyway.

There is different truncation behaviour for %rip which needs altering, but that is a separate area of code.  Anything else?

Quoted 7 lines
Quoted 2 lines

protmode_load_seg() and MOVSXD already have vendor dependent code, if that was your question.

I was actually just asking about far jmp/call.

If you're sure that far jmp/call is more complicated than just tweaking this patch, then fine.  Acked-by: Andrew Cooper <[email protected]>

Thanks much. I'll see to put together the far branch counterpart soon. Perhaps I can also do the near branch parts then.

Quoted 9 lines
Quoted 3 lines

For things needing doing see
above plus LOOP, J[ER]CXZ, SYSENTER, and SYSEXIT as far as I'm currently aware.

SYSCALL and SYSRET as well.  The way they handle MSR_STAR is vendor specific, as well as #UD conditions.

I've just noticed that I've still got an XSA-204 followup patch still outstanding from 2016...

Oh. Looking forward to see it.

Jan

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