From: Jan Beulich <jbeulich@suse.com>
To: "xen-devel@lists.xenproject.org" <xen-devel@lists.xenproject.org>
Cc: "Andrew Cooper" <andrew.cooper3@citrix.com>,
"Teddy Astie" <teddy.astie@vates.tech>,
"Roger Pau Monné" <roger@xenproject.org>
Subject: [PATCH 05/30] x86emul: replace earlier relaxation of decode_gpr()
Date: Wed, 11 Nov 2026 15:22:31 +0100 [thread overview]
Message-ID: <01eb736f-2b1e-43bf-ab4a-5e5bbb67d212@suse.com> (raw)
In-Reply-To: <17d56a5d-41f2-4691-aea7-d6dfdde5ad32@suse.com>
Handle eGPR-s in a new out-of-line helper, while restoring some code to
the state before commit e8af03bf9a32 ("x86emul: prepare for AVX512F S/G
insns"). Some of the function invocations are "just in case"; to allow
these to continue to work without adding logic to tell whether for a
particular insn a particular operand is a GPR or another knd of
register, record register numbers in the new structure in all cases,
while fetching register values only when eGPR-s are actually enabled.
Signed-off-by: Jan Beulich <jbeulich@suse.com>
---
The almost-pure C variant of the register latching would be something
like
switch ( reg )
{
#define CASE(regno, basereg) \
case regno: \
/* REX2 prefixes with W, R4 and possibly R3 set. */ \
asm ( ".byte 0xd5, 0x48 | %c1; mov %%" #basereg ", %0" \
: "+m" (egpr->val[i]) \
: "i" ((regno & 8) >> 1) )
CASE(16, eax); break;
CASE(17, ecx); break;
CASE(18, edx); break;
CASE(19, ebx); break;
CASE(20, esp); break;
CASE(21, ebp); break;
CASE(22, esi); break;
CASE(23, edi); break;
CASE(24, eax); break;
CASE(25, ecx); break;
CASE(26, edx); break;
CASE(27, ebx); break;
CASE(28, esp); break;
CASE(29, ebp); break;
CASE(30, esi); break;
CASE(31, edi); break;
#undef CASE
default:
ASSERT_UNREACHABLE();
break;
}
which, because of the way the compiler translates the switch(), ends up
larger without (imo) being that much easier to read.
--- a/tools/fuzz/x86_instruction_emulator/Makefile
+++ b/tools/fuzz/x86_instruction_emulator/Makefile
@@ -32,7 +32,7 @@ GCOV_FLAGS := --coverage
OBJS := fuzz-emul.o x86-emulate.o
OBJS += x86_emulate/0f01.o x86_emulate/0fae.o x86_emulate/0fc7.o
-OBJS += x86_emulate/decode.o x86_emulate/fpu.o
+OBJS += x86_emulate/decode.o x86_emulate/fpu.o x86_emulate/util.o
WRAPPED = $(shell sed -n 's,^ *WRAP(\([[:alnum:]_]*\));,\1,p' \
../../tests/x86_emulator/x86-emulate.h)
--- a/tools/tests/x86_emulator/x86-emulate.h
+++ b/tools/tests/x86_emulator/x86-emulate.h
@@ -143,6 +143,8 @@ static inline uint64_t xgetbv(uint32_t x
return ((uint64_t)hi << 32) | lo;
}
+#define get_xcr0() xgetbv(0)
+
/* Intentionally checking OSXSAVE here. */
#define cpu_has_xsave (cpu_policy.basic.raw[1].c & (1u << 27))
@@ -225,6 +227,8 @@ void wrpkru(unsigned int val);
xcr0_mask(0xe6))
#define cpu_has_avx10_2_aux (cpu_policy.avx10.avx10_v2_aux && \
xcr0_mask(0xe6))
+#define cpu_has_apx_f (cpu_policy.feat.apx_f && \
+ xcr0_mask(X86_XCR0_APX))
#define cpu_has_xgetbv1 (cpu_has_xsave && cpu_policy.xstate.xgetbv1)
--- a/xen/arch/x86/x86_emulate/util.c
+++ b/xen/arch/x86/x86_emulate/util.c
@@ -7,6 +7,74 @@
*/
#include "private.h"
+#ifdef __XEN__
+#include <asm/xstate.h>
+#endif
+
+unsigned long *decode_egpr(struct egpr *egpr, unsigned int reg)
+{
+ unsigned int i;
+
+ for ( i = 0; i < ARRAY_SIZE(egpr->regno); ++i )
+ {
+ unsigned int xcr0 = 0;
+
+ if ( egpr->regno[i] == reg )
+ break;
+
+ if ( egpr->regno[i] )
+ continue;
+
+ egpr->regno[i] = reg;
+
+ /*
+ * Obtain the active XCR0 here, for not having emulation context or an
+ * ops structure available, as this is a utility function used also
+ * from outside of the emulator.
+ */
+ if ( cpu_has_apx_f )
+ xcr0 = get_xcr0();
+
+ /* Fetch the register value if accessible. */
+ if ( xcr0 & X86_XCR0_APX )
+ {
+#ifdef __x86_64__
+ asm ( "sub $17, %[reg]\n\t"
+
+ /* mov %r16, (%rdi) */
+ ".byte 0xd5, 0x48; mov %%eax, (%%rdi)\n\t"
+
+ /* cfcmovz %r17, (%rdi) */
+ ".byte 0x62, 0xe4, 0xfc, 0x0c, 0x44, 0x0f\n\t"
+
+ ".irp n, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30\n\t"
+
+ "dec %[reg]\n\t"
+
+ /* cfcmovz %r\n, (%rdi) */
+ ".byte 0x62, 0x64 + ((~\\n & 8) << 4), 0xfc, 0x0c\n\t"
+ ".byte 0x44, 0x07 + ((\\n & 7) << 3)\n\t"
+
+ ".endr\n\t"
+
+ /* cfcmovg %r31, (%rdi) */
+ ".byte 0x62, 0x64, 0xfc, 0x0c, 0x4f, 0x3f"
+
+ : [reg] "+r" (reg), "=m" (egpr->val[i])
+ : "D" (&egpr->val[i]) );
+
+ egpr->orig[i] = egpr->val[i];
+#endif
+ }
+
+ break;
+ }
+
+ ASSERT(i < ARRAY_SIZE(egpr->val));
+
+ /* Note that this also acts as array_access_nospec() stand-in. */
+ return &egpr->val[i & (ARRAY_SIZE(egpr->val) - 1)];
+}
unsigned int x86_insn_length(const struct x86_emulate_state *s,
const struct x86_emulate_ctxt *ctxt)
--- a/xen/arch/x86/x86_emulate/x86_emulate.h
+++ b/xen/arch/x86/x86_emulate/x86_emulate.h
@@ -634,27 +634,36 @@ int x86_emulate_wrapper(
/* Map GPRs by ModRM encoding to their offset within struct cpu_user_regs. */
extern const uint8_t cpu_user_regs_gpr_offsets[X86_NR_GPRS];
+/*
+ * APX insn can reference up to 4 GPRs: Either up to three register operands
+ * or up to two register operands and one memory operand, the latter using
+ * potentially both base and index register.
+ */
struct egpr {
+ unsigned char regno[4];
+ unsigned long val[4], orig[4];
};
+unsigned long *decode_egpr(struct egpr *egpr, unsigned int reg);
+
/*
- * Given the 'reg' portion of a ModRM byte, and a register block, return a
- * pointer into the block that addresses the relevant register.
+ * Given the 'reg' portion of a ModRM byte, and a pair of register blocks,
+ * return a pointer into either block that addresses the relevant register.
*/
static inline unsigned long *decode_gpr(struct cpu_user_regs *regs,
struct egpr *egpr,
unsigned int reg)
{
+ if ( reg & 0x10 )
+ return decode_egpr(egpr, reg);
+
/* Check that the array is a power of two. */
BUILD_BUG_ON(ARRAY_SIZE(cpu_user_regs_gpr_offsets) &
(ARRAY_SIZE(cpu_user_regs_gpr_offsets) - 1));
- /*
- * Note that this also acts as array_access_nospec() stand-in. Higher
- * bits may legitimately come in set here, from EVEX decoding, and
- * hence truncation is what we want (bits not ignored will get checked
- * elsewhere).
- */
+ ASSERT(reg < ARRAY_SIZE(cpu_user_regs_gpr_offsets));
+
+ /* Note that this also acts as array_access_nospec() stand-in. */
reg &= ARRAY_SIZE(cpu_user_regs_gpr_offsets) - 1;
return (void *)regs + cpu_user_regs_gpr_offsets[reg];
next prev parent reply other threads:[~2026-11-11 14:22 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-11 14:19 [PATCH 00/30] x86: permit guests to use APX Jan Beulich
2026-11-11 14:20 ` [PATCH 01/30] x86/CPUID: introduce APX_F Jan Beulich
2026-11-11 14:21 ` [PATCH 02/30] x86/CPUID: enable APX sub-leaf Jan Beulich
2026-11-11 14:21 ` [PATCH 03/30] x86/xstate: enable APX_F Jan Beulich
2026-11-11 14:22 ` [PATCH 04/30] x86emul: add eGPR parameter to decode_gpr() Jan Beulich
2026-11-11 14:22 ` Jan Beulich [this message]
2026-11-11 14:22 ` [PATCH 06/30] VMX: support APX Jan Beulich
2026-11-11 14:23 ` [PATCH 07/30] x86emul: latch XCR0 early during decode Jan Beulich
2026-11-11 14:23 ` [PATCH 08/30] x86emul: support REX2-prefixed insns Jan Beulich
2026-11-11 14:24 ` [PATCH 09/30] x86emul: APX_F REX2 testing Jan Beulich
2026-11-11 14:24 ` [PATCH 10/30] x86emul: support JMPABS Jan Beulich
2026-11-11 14:25 ` [PATCH 11/30] x86emul: switch to EVEX.R{3,4} nomenclature Jan Beulich
2026-11-11 14:25 ` [PATCH 12/30] x86emul: switch to EVEX.B{3,4} nomenclature Jan Beulich
2026-11-11 14:25 ` [PATCH 13/30] x86emul: switch to EVEX.X3 nomenclature Jan Beulich
2026-11-11 14:26 ` [PATCH 14/30] x86emul: support basic APX EVEX-encoded ALU insns Jan Beulich
2026-11-11 14:26 ` [PATCH 15/30] x86emul: support APX EVEX-encoded shift/rotate insns Jan Beulich
2026-11-11 14:27 ` [PATCH 16/30] x86emul: support remaining basic APX EVEX-Map4 insns Jan Beulich
2026-11-11 14:27 ` [PATCH 17/30] x86emul: support APX SCC insns Jan Beulich
2026-11-11 14:27 ` [PATCH 18/30] x86emul: support APX+ADX insns Jan Beulich
2026-11-11 14:28 ` [PATCH 19/30] x86emul: extend copy_VEX() for APX use Jan Beulich
2026-11-11 14:28 ` [PATCH 20/30] x86emul: support APX+AVX512 insns Jan Beulich
2026-11-11 14:29 ` [PATCH 21/30] x86emul: support APX+BMI1 insns Jan Beulich
2026-11-11 14:29 ` [PATCH 22/30] x86emul: support APX+BMI2 insns Jan Beulich
2026-11-11 14:29 ` [PATCH 23/30] x86emul: support APX+CMPccXADD insns Jan Beulich
2026-11-11 14:30 ` [PATCH 24/30] x86emul: support APX+{LZ,POP}CNT insns Jan Beulich
2026-11-11 14:30 ` [PATCH 25/30] x86emul: support APX+MOVBE insn Jan Beulich
2026-11-11 14:31 ` [PATCH 26/30] x86emul: support APX+MOVDIR/ENQCMD insns Jan Beulich
2026-11-11 14:31 ` [PATCH 27/30] x86emul: support APX+MOVRS insn Jan Beulich
2026-11-11 14:31 ` [PATCH 28/30] x86emul: support APX MSR access insns Jan Beulich
2026-11-11 14:32 ` [PATCH 29/30] x86: permit guests to use APX Jan Beulich
2026-11-11 14:33 ` [PATCH 30/30] x86emul: use latched XCR0 in x86emul_get_fpu() Jan Beulich
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=01eb736f-2b1e-43bf-ab4a-5e5bbb67d212@suse.com \
--to=jbeulich@suse.com \
--cc=andrew.cooper3@citrix.com \
--cc=roger@xenproject.org \
--cc=teddy.astie@vates.tech \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.