Re: [PATCH v9 12/20] KVM: x86: Handle EGPR index and REX2-incompatible opcodes
From: Chang S. Bae
Date: Wed Nov 18 2026 - 20:55:51 EST
On 11/18/2026 3:02 PM, Jim Mattson wrote:
static void __decode_register_operand(struct x86_emulate_ctxt *ctxt,
struct operand *op, int reg)
{
if ((ctxt->d & Avx) && ctxt->op_bytes == 32) {
+ reg &= 0xf;
Thanks. That takes care of the BUG() in _kvm_read_sse_reg(). However,
you didn't respond to the other half of that comment on v8:
Similarly, em_mov_rm_sreg() and em_mov_sreg_rm() use ctxt->modrm_reg
directly without masking off REX.R / REX2.R3 and REX2.R4, so setting
R3=1 or R4=1 on opcode 0x8C or 0x8E will erroneously inject #UD.
Per Section 3.1.2.1 ("REX2 Prefix") of the Intel APX Architecture
Specification (rev. 9.0):
Furthermore, if ModRM.Reg addresses a segment register, both REX2.R4
and REX2.R3 are ignored.
Bah...thanks for the catch (again)! I suppose masking should've been
there since REX as well.
With the changelog,
"... Also, ignore the high-order register index bits when accessing
vector registers (XMM and YMM) or segment registers."
diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index c6e11f551438..2b649d15d5f5 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -3359,6 +3359,9 @@ static int em_store_sreg(struct x86_emulate_ctxt *ctxt, int segment)
static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
{
+ /* Ignore higher register index bits from REX/REX2: */
+ ctxt->modrm_reg &= 7;
+
if (ctxt->modrm_reg > VCPU_SREG_GS)
return emulate_ud(ctxt);
@@ -3369,6 +3372,9 @@ static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
{
u16 sel = ctxt->src.val;
+ /* Ignore higher register index bits from REX/REX2: */
+ ctxt->modrm_reg &= 7;
+
if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
return emulate_ud(ctxt);