Re: [PATCH v9 12/20] KVM: x86: Handle EGPR index and REX2-incompatible opcodes

From: Jim Mattson

Date: Wed Nov 18 2026 - 18:03:00 EST


On Tue, Nov 17, 2026 at 10:32 AM Chang S. Bae <chang.seok.bae@xxxxxxxxx> wrote:
>
> Prepare the emulator for REX2 handling by introducing the NoRex2 opcode
> flag and supporting extended register indices. For the latter, factor out
> common logic for calculating register IDs.
>
> REX2 does not support three-byte opcodes. Instead, the REX2.M bit selects
> between one- and two-byte opcode tables, which were previously
> distinguished by the 0x0F escape byte.
>
> Some legacy instructions in those tables never reference extended
> registers. When prefixed with REX, such instructions are treated as if
> the prefix were absent. In contrast, a REX2 prefix causes a #UD, which
> should be handled explicitly.
>
> JMPABS (0xA1) is an exception: it is defined for REX2 but mark it as not
> implemented, since emulating non-memory operation isn't practically
> that useful here.
>
> Also, ignore the high-order register index bits when accessing vector
> registers (XMM and YMM).
>
> Suggested-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
> Tested-by: Zhao Liu <zhao1.liu@xxxxxxxxx>
> Signed-off-by: Chang S. Bae <chang.seok.bae@xxxxxxxxx>
> Link: https://lore.kernel.org/1ebf3a23-5671-41c1-8daa-c83f2f105936@xxxxxxxxxx
> ---
> V8 -> V9:
> * Fix the vector register ID (Jim Mattson)
> * Clarify that JMPABS is not implemented (Jim Mattson)
> * Move the NoRex2 check below group flag handling (Jim Mattson)
> ---

[ ... ]

> 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.