* [PATCH v5 0/2] Add support for cmpxchg16b emulation
@ 2026-11-26 6:05 Sairaj Kodilkar
2026-11-26 6:05 ` [PATCH v5 1/2] x86/uaccess: Extend CMPXCHG user helpers to 128-bit operands Sairaj Kodilkar
2026-11-26 6:05 ` [PATCH v5 2/2] KVM: x86: Add support for cmpxchg16b emulation Sairaj Kodilkar
0 siblings, 2 replies; 5+ messages in thread
From: Sairaj Kodilkar @ 2026-11-26 6:05 UTC (permalink / raw)
To: H. Peter Anvin, Borislav Petkov, Dave Hansen, Ingo Molnar,
Paolo Bonzini, Sairaj Kodilkar, Sean Christopherson,
Thomas Gleixner, kvm, linux-kernel, x86
Cc: Vasant.Hegde, Suravee.Suthikulpanit
This series adds 128-bit compare-exchange support needed for KVM to
emulate guest cmpxchg16b instructions.
The AMD IOMMU driver updates 256-bit device table entries with two
128-bit cmpxchg operations. For hardware-accelerated vIOMMU, QEMU traps
those DTE accesses to install nested page tables [1]. KVM must emulate
guest cmpxchg16b on the trapped MMIO regions; without that, DTE access
emulation fails.
Patch 1: extends the x86 user CMPXCHG helpers with
__try_cmpxchg128_user_asm() (cmpxchg16b on x86-64), wired into
unsafe_try_cmpxchg_user().
Patch 2: extends KVM CMPXCHG8B emulation to 16-byte operands (REX.W=1) and
uses the new helper for atomic 16-byte compare-exchange on guest RAM via
emulator_cmpxchg_emulated().
You can find the KUT to test this series at [2].
[1] https://github.com/AMDESE/qemu-iommu/blob/wip/for_iommufd_hw_queue-v8_amd_viommu_20260106/hw/i386/amd_viommu.c#L517
[2] https://lore.kernel.org/kvm/20260706062153.346-1-sarunkod@amd.com/T/#u
==================
Changes since v4:
https://lore.kernel.org/all/20260908074739.10748-1-sarunkod@amd.com/
Patch 2: Check if guest has cmpxchg(8/16)b capability present before emulating
it [sashiko].
Changes since v3:
https://lore.kernel.org/all/20260826070004.8100-1-sarunkod@amd.com/
Patch 1: Split the old value across RAX and RDX using "+a" and "+b" instead of
using "+A" [Dave]
Patch 2: Use __c instead of ctx in cmpxchg macro [Sashiko]
Changes since v2:
https://lore.kernel.org/all/20260706063035.1139-1-sarunkod@amd.com/
Patch 1: Trigger build failure if __try_cmpxchg128_user_asm is called on 32 bit
machines [Sean]
Sairaj Kodilkar (2):
x86/uaccess: Extend CMPXCHG user helpers to 128-bit operands
KVM: x86: Add support for cmpxchg16b emulation
arch/x86/include/asm/uaccess.h | 63 +++++++++++++++++++++++++++++++++-
arch/x86/kvm/emulate.c | 52 ++++++++++++++++++++--------
arch/x86/kvm/kvm_emulate.h | 8 +++++
arch/x86/kvm/x86.c | 19 +++++++++-
4 files changed, 126 insertions(+), 16 deletions(-)
--
2.34.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v5 1/2] x86/uaccess: Extend CMPXCHG user helpers to 128-bit operands
2026-11-26 6:05 [PATCH v5 0/2] Add support for cmpxchg16b emulation Sairaj Kodilkar
@ 2026-11-26 6:05 ` Sairaj Kodilkar
2026-11-26 6:22 ` sashiko-bot
2026-11-26 6:05 ` [PATCH v5 2/2] KVM: x86: Add support for cmpxchg16b emulation Sairaj Kodilkar
1 sibling, 1 reply; 5+ messages in thread
From: Sairaj Kodilkar @ 2026-11-26 6:05 UTC (permalink / raw)
To: H. Peter Anvin, Borislav Petkov, Dave Hansen, Ingo Molnar,
Paolo Bonzini, Sairaj Kodilkar, Sean Christopherson,
Thomas Gleixner, kvm, linux-kernel, x86
Cc: Vasant.Hegde, Suravee.Suthikulpanit
Extend the existing user CMPXCHG helpers to support 16-byte operands on
x86-64, using LOCK_PREFIX "cmpxchg16b". This mirrors the existing
__try_cmpxchg64_user_asm() / cmpxchg8b path provided for 32-bit kernels,
where KVM needs an atomic compare-exchange wider than the generic
cmpxchg helper can provide.
On 32-bit kernels, stub the helper to generate build failure because
cmpxchg16b requires 64-bit GPRs and is not available.
KVM uses this to atomically emulate guest cmpxchg16b on guest RAM mapped
via userspace addresses.
Signed-off-by: Sairaj Kodilkar <sarunkod@amd.com>
---
arch/x86/include/asm/uaccess.h | 63 +++++++++++++++++++++++++++++++++-
1 file changed, 62 insertions(+), 1 deletion(-)
diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
index f4700ad6b5ec..2c1418c6d61b 100644
--- a/arch/x86/include/asm/uaccess.h
+++ b/arch/x86/include/asm/uaccess.h
@@ -353,6 +353,27 @@ do { \
if (unlikely(!success)) \
*_old = __old; \
likely(success); })
+#else // !CONFIG_X86_32
+#define __try_cmpxchg128_user_asm(_ptr, _pold, _new, label) ({ \
+ bool success; \
+ __typeof__(_ptr) _old = (__typeof__(_ptr))(_pold); \
+ u64 __old_low = (u64)*_old; \
+ u64 __old_high = (u64)(*_old >> 64); \
+ __typeof__(*(_ptr)) __new = (_new); \
+ asm_goto_output("\n" \
+ "1: " LOCK_PREFIX "cmpxchg16b %[ptr]\n" \
+ _ASM_EXTABLE_UA(1b, %l[label]) \
+ : "=@ccz" (success), \
+ "+a" (__old_low), \
+ "+d" (__old_high), \
+ [ptr] "+m" (*_ptr) \
+ : "b" ((u64)__new), \
+ "c" ((u64)((u128)__new >> 64)) \
+ : "memory" \
+ : label); \
+ if (unlikely(!success)) \
+ *_old = ((u128)__old_high << 64) | __old_low; \
+ likely(success); })
#endif // CONFIG_X86_32
#else // !CONFIG_CC_HAS_ASM_GOTO_TIED_OUTPUT
#define __try_cmpxchg_user_asm(itype, ltype, _ptr, _pold, _new, label) ({ \
@@ -409,6 +430,32 @@ do { \
if (unlikely(!__result)) \
*_old = __old; \
likely(__result); })
+#else //!CONFIG_X86_32
+#define __try_cmpxchg128_user_asm(_ptr, _pold, _new, label) ({ \
+ int __result; \
+ __typeof__(_ptr) _old = (__typeof__(_ptr))(_pold); \
+ u64 __old_low = (u64)*_old; \
+ u64 __old_high = (u64)(*_old >> 64); \
+ __typeof__(*(_ptr)) __new = (_new); \
+ asm volatile("\n" \
+ "1: " LOCK_PREFIX "cmpxchg16b %[ptr]\n" \
+ "mov $0, %[result]\n\t" \
+ "setz %b[result]\n" \
+ "2:\n" \
+ _ASM_EXTABLE_TYPE_REG(1b, 2b, EX_TYPE_EFAULT_REG, \
+ %[result]) \
+ : [result] "=q" (__result), \
+ "+a" (__old_low), \
+ "+d" (__old_high), \
+ [ptr] "+m" (*_ptr) \
+ : "b" ((u64)__new), \
+ "c" ((u64)((u128)__new >> 64)) \
+ : "memory", "cc"); \
+ if (unlikely(__result < 0)) \
+ goto label; \
+ if (unlikely(!__result)) \
+ *_old = ((u128)__old_high << 64) | __old_low; \
+ likely(__result); })
#endif // CONFIG_X86_32
#endif // CONFIG_CC_HAS_ASM_GOTO_TIED_OUTPUT
@@ -497,11 +544,17 @@ do { \
extern void __try_cmpxchg_user_wrong_size(void);
-#ifndef CONFIG_X86_32
+#ifdef CONFIG_X86_32
+/*
+ * Always fail on 32 bit arch or 64 arch without 128 bit cmpxchg support
+ */
+#define __try_cmpxchg128_user_asm(_ptr, _pold, _new, label) ({ BUILD_BUG_ON(1); 0; })
+#else
#define __try_cmpxchg64_user_asm(_ptr, _oldp, _nval, _label) \
__try_cmpxchg_user_asm("q", "r", (_ptr), (_oldp), (_nval), _label)
#endif
+
/*
* Force the pointer to u<size> to match the size expected by the asm helper.
* clang/LLVM compiles all cases and only discards the unused paths after
@@ -526,6 +579,14 @@ extern void __try_cmpxchg_user_wrong_size(void);
case 8: __ret = __try_cmpxchg64_user_asm((__force u64 *)(_ptr), (_oldp),\
(_nval), _label); \
break; \
+ case 16: \
+ if (boot_cpu_has(X86_FEATURE_CX16)) \
+ __ret = __try_cmpxchg128_user_asm( \
+ (__force u128 *)(_ptr), \
+ (_oldp), (_nval), _label); \
+ else \
+ __ret = 0; \
+ break; \
default: __try_cmpxchg_user_wrong_size(); \
} \
__ret; })
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v5 2/2] KVM: x86: Add support for cmpxchg16b emulation
2026-11-26 6:05 [PATCH v5 0/2] Add support for cmpxchg16b emulation Sairaj Kodilkar
2026-11-26 6:05 ` [PATCH v5 1/2] x86/uaccess: Extend CMPXCHG user helpers to 128-bit operands Sairaj Kodilkar
@ 2026-11-26 6:05 ` Sairaj Kodilkar
2026-11-26 6:28 ` sashiko-bot
1 sibling, 1 reply; 5+ messages in thread
From: Sairaj Kodilkar @ 2026-11-26 6:05 UTC (permalink / raw)
To: H. Peter Anvin, Borislav Petkov, Dave Hansen, Ingo Molnar,
Paolo Bonzini, Sairaj Kodilkar, Sean Christopherson,
Thomas Gleixner, kvm, linux-kernel, x86
Cc: Vasant.Hegde, Suravee.Suthikulpanit
AMD and Intel both provides support for 128 bit cmpxchg operands using
cmpxchg8b/cmpxchg16b instructions (opcode 0FC7). However, kvm does not
support emulating cmpxchg16b (i.e when destination memory is 128 bit and
REX.W = 1) which causes emulation failure when QEMU guest performs a
cmpxchg16b on a memory region setup as a IO.
This failure is seen on the AMD IOMMU driver which writes 256-bit device
table entries with two 128-bit cmpxchg operations. For guests using
hardware-accelerated vIOMMU, QEMU traps device table accesses to set up
nested page tables (see [1]). Without 128-bit cmpxchg emulation, KVM
cannot handle these traps and DTE access emulation fails.
Hence extend cmpxchg8b to perform cmpxchg16b when the destination memory
is 128 bit.Also check guest CPUID for X86_FEATURE_CX8 / X86_FEATURE_CX16
before emulating CMPXCHG8B / CMPXCHG16B, and inject #UD if userspace has
not exposed the corresponding feature. This mirrors MOVBE/RDPID handling
and ensures KVM does not emulate an instruction the VMM withheld from
the guest.
[1] https://github.com/AMDESE/qemu-iommu/blob/wip/for_iommufd_hw_queue-v8_amd_viommu_20260106/hw/i386/amd_viommu.c#L517
Signed-off-by: Sairaj Kodilkar <sarunkod@amd.com>
---
arch/x86/kvm/emulate.c | 52 ++++++++++++++++++++++++++++----------
arch/x86/kvm/kvm_emulate.h | 8 ++++++
arch/x86/kvm/x86.c | 19 +++++++++++++-
3 files changed, 64 insertions(+), 15 deletions(-)
diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index c1b21282187f..a3483dcc011e 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -2184,23 +2184,41 @@ static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)
return rc;
}
+#define em_cmpxchg8b_16b(__c, rbits, mbits)\
+do { \
+ u##mbits old = __c->dst.orig_val##mbits; \
+ \
+ BUILD_BUG_ON(rbits * 2 != mbits); \
+ \
+ if (((u##rbits) (old >> 0) != (u##rbits) reg_read(__c, VCPU_REGS_RAX)) || \
+ ((u##rbits) (old >> rbits) != (u##rbits) reg_read(__c, VCPU_REGS_RDX))) { \
+ *reg_write(__c, VCPU_REGS_RAX) = (u##rbits) (old >> 0); \
+ *reg_write(__c, VCPU_REGS_RDX) = (u##rbits) (old >> rbits); \
+ __c->eflags &= ~X86_EFLAGS_ZF; \
+ } else { \
+ __c->dst.val##mbits = ((u##mbits)reg_read(__c, VCPU_REGS_RCX) << rbits) | \
+ (u##rbits) reg_read(__c, VCPU_REGS_RBX); \
+ \
+ __c->eflags |= X86_EFLAGS_ZF; \
+ } \
+} while (0)
+
static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
{
- u64 old = ctxt->dst.orig_val64;
-
- if (ctxt->dst.bytes == 16)
+ if (WARN_ON_ONCE(8 + !!(ctxt->rex_bits & REX_W) * 8 != ctxt->dst.bytes))
return X86EMUL_UNHANDLEABLE;
- if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
- ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
- *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
- *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
- ctxt->eflags &= ~X86_EFLAGS_ZF;
- } else {
- ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
- (u32) reg_read(ctxt, VCPU_REGS_RBX);
- ctxt->eflags |= X86_EFLAGS_ZF;
+ if (!(ctxt->rex_bits & REX_W)) {
+ if (!ctxt->ops->guest_has_cmpxchg8b(ctxt))
+ return X86EMUL_UNHANDLEABLE;
+ em_cmpxchg8b_16b(ctxt, 32, 64);
+#ifdef CONFIG_X86_64
+ } else {
+ if (!ctxt->ops->guest_has_cmpxchg16b(ctxt))
+ return X86EMUL_UNHANDLEABLE;
+ em_cmpxchg8b_16b(ctxt, 64, 128);
+#endif
}
return X86EMUL_CONTINUE;
}
@@ -5414,8 +5432,14 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt, bool check_intercepts)
goto done;
}
}
- /* Copy full 64-bit value for CMPXCHG8B. */
- ctxt->dst.orig_val64 = ctxt->dst.val64;
+ /* Copy full 64/128-bit value for CMPXCHG8B. */
+
+#ifdef CONFIG_X86_64
+ if (ctxt->dst.bytes == 16)
+ ctxt->dst.orig_val128 = ctxt->dst.val128;
+ else
+#endif
+ ctxt->dst.orig_val64 = ctxt->dst.val64;
special_insn:
diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h
index 10886bea82c9..6926865826c9 100644
--- a/arch/x86/kvm/kvm_emulate.h
+++ b/arch/x86/kvm/kvm_emulate.h
@@ -236,6 +236,8 @@ struct x86_emulate_ops {
bool (*guest_has_movbe)(struct x86_emulate_ctxt *ctxt);
bool (*guest_has_fxsr)(struct x86_emulate_ctxt *ctxt);
bool (*guest_has_rdpid)(struct x86_emulate_ctxt *ctxt);
+ bool (*guest_has_cmpxchg8b)(struct x86_emulate_ctxt *ctxt);
+ bool (*guest_has_cmpxchg16b)(struct x86_emulate_ctxt *ctxt);
bool (*guest_cpuid_is_intel_compatible)(struct x86_emulate_ctxt *ctxt);
void (*set_nmi_mask)(struct x86_emulate_ctxt *ctxt, bool masked);
@@ -263,6 +265,9 @@ struct operand {
union {
unsigned long orig_val;
u64 orig_val64;
+#ifdef CONFIG_X86_64
+ u128 orig_val128;
+#endif
};
union {
unsigned long *reg;
@@ -276,6 +281,9 @@ struct operand {
union {
unsigned long val;
u64 val64;
+#ifdef CONFIG_X86_64
+ u128 val128;
+#endif
char valptr[sizeof(avx256_t)];
sse128_t vec_val;
avx256_t vec_val2;
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 0549c0b48901..0e8cc7073858 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -5314,7 +5314,7 @@ static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
int r;
/* guests cmpxchg8b have to be emulated atomically */
- if (bytes > 8 || (bytes & (bytes - 1)))
+ if (bytes > 2 * sizeof(unsigned long) || (bytes & (bytes - 1)))
goto emul_write;
gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
@@ -5354,6 +5354,11 @@ static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
case 8:
r = emulator_try_cmpxchg_user(u64, hva, old, new);
break;
+#ifdef CONFIG_X86_64
+ case 16:
+ r = emulator_try_cmpxchg_user(u128, hva, old, new);
+ break;
+#endif
default:
BUG();
}
@@ -5774,6 +5779,16 @@ static bool emulator_guest_has_rdpid(struct x86_emulate_ctxt *ctxt)
return guest_cpu_cap_has(emul_to_vcpu(ctxt), X86_FEATURE_RDPID);
}
+static bool emulator_guest_has_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
+{
+ return guest_cpu_cap_has(emul_to_vcpu(ctxt), X86_FEATURE_CX8);
+}
+
+static bool emulator_guest_has_cmpxchg16b(struct x86_emulate_ctxt *ctxt)
+{
+ return guest_cpu_cap_has(emul_to_vcpu(ctxt), X86_FEATURE_CX16);
+}
+
static bool emulator_guest_cpuid_is_intel_compatible(struct x86_emulate_ctxt *ctxt)
{
return guest_cpuid_is_intel_compatible(emul_to_vcpu(ctxt));
@@ -5894,6 +5909,8 @@ static const struct x86_emulate_ops emulate_ops = {
.guest_has_movbe = emulator_guest_has_movbe,
.guest_has_fxsr = emulator_guest_has_fxsr,
.guest_has_rdpid = emulator_guest_has_rdpid,
+ .guest_has_cmpxchg8b = emulator_guest_has_cmpxchg8b,
+ .guest_has_cmpxchg16b = emulator_guest_has_cmpxchg16b,
.guest_cpuid_is_intel_compatible = emulator_guest_cpuid_is_intel_compatible,
.set_nmi_mask = emulator_set_nmi_mask,
.is_smm = emulator_is_smm,
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v5 1/2] x86/uaccess: Extend CMPXCHG user helpers to 128-bit operands
2026-11-26 6:05 ` [PATCH v5 1/2] x86/uaccess: Extend CMPXCHG user helpers to 128-bit operands Sairaj Kodilkar
@ 2026-11-26 6:22 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-26 6:22 UTC (permalink / raw)
To: Sairaj Kodilkar, kvm
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] x86/uaccess: wrong fallback when CX16 is absent in
unsafe_try_cmpxchg_user()
--
commit 41f10bc42b22924b13230c70050428ef6b8cacb9
Author: Sairaj Kodilkar <sarunkod@amd.com>
x86/uaccess: Extend CMPXCHG user helpers to 128-bit operands
This commit adds 128-bit operand support to the user-space cmpxchg helpers
on x86-64 using cmpxchg16b, and stubs the 128-bit helper on 32-bit kernels
with BUILD_BUG_ON().
> diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
> index 3a0dd3c2b233e..15425af59de92 100644
> --- a/arch/x86/include/asm/uaccess.h
> +++ b/arch/x86/include/asm/uaccess.h
[ ... ]
> @@ -580,6 +633,14 @@ extern void __try_cmpxchg_user_wrong_size(void);
> case 8: __ret = __try_cmpxchg64_user_asm((__force u64 *)(_ptr), (_oldp),\
> (_nval), _label); \
> break; \
> + case 16: \
> + if (boot_cpu_has(X86_FEATURE_CX16)) \
> + __ret = __try_cmpxchg128_user_asm( \
> + (__force u128 *)(_ptr), \
> + (_oldp), (_nval), _label); \
> + else \
> + __ret = 0; \
> + break; \
[Severity: Medium]
Should the !boot_cpu_has(X86_FEATURE_CX16) fallback jump to _label instead
of setting __ret = 0?
unsafe_try_cmpxchg_user() returns false (0) on a compare-and-exchange value
mismatch after updating *_oldp, and jumps to _label when the access faults:
arch/x86/include/asm/uaccess.h:__try_cmpxchg_user() {
int __ret = -EFAULT; \
__uaccess_begin_nospec(); \
__ret = !unsafe_try_cmpxchg_user(_ptr, _oldp, _nval, _label); \
_label: \
__uaccess_end(); \
__ret; \
}
Setting __ret = 0 leaves *_oldp unmodified and causes __try_cmpxchg_user()
to return 1 (value mismatch) instead of -EFAULT, so callers retrying on
mismatch can loop forever.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261126060515.2198-2-sarunkod@amd.com?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v5 2/2] KVM: x86: Add support for cmpxchg16b emulation
2026-11-26 6:05 ` [PATCH v5 2/2] KVM: x86: Add support for cmpxchg16b emulation Sairaj Kodilkar
@ 2026-11-26 6:28 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-26 6:28 UTC (permalink / raw)
To: Sairaj Kodilkar, kvm
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] KVM: x86: em_cmpxchg8b() returns X86EMUL_UNHANDLEABLE instead
of #UD
- [Medium] KVM: x86: em_cmpxchg8b() checks CPUID after destination
memory read
--
commit 98ac1f736784d209fed9239b44c3756356d76e28
Author: Sairaj Kodilkar <sarunkod@amd.com>
KVM: x86: Add support for cmpxchg16b emulation
This commit adds 128-bit CMPXCHG16B emulation support to em_cmpxchg8b()
and emulator_cmpxchg_emulated(), and checks guest CPUID for
X86_FEATURE_CX8 and X86_FEATURE_CX16 before emulating CMPXCHG8B and
CMPXCHG16B.
[Severity: High]
In arch/x86/kvm/emulate.c, em_cmpxchg8b() returns X86EMUL_UNHANDLEABLE
when ctxt->ops->guest_has_cmpxchg8b(ctxt) or
ctxt->ops->guest_has_cmpxchg16b(ctxt) returns false. Should this call
emulate_ud(ctxt) instead, like em_rdpid() and em_movbe() do?
Returning X86EMUL_UNHANDLEABLE translates to EMULATION_FAILED without
queuing UD_VECTOR. Because group9 marks em_cmpxchg8b with PageTable, a
write to a shadow-protected page table calls
kvm_unprotect_and_retry_on_failure(), unprotecting the GFN and re-entering
the guest to execute the withheld instruction natively on the host CPU.
For non-page-table accesses such as MMIO, handle_emulation_failure() exits
to userspace with KVM_EXIT_INTERNAL_ERROR when running at L1 CPL 0 or when
exit_on_emulation_error is enabled, rather than injecting #UD into the
guest.
[Severity: Medium]
Also in arch/x86/kvm/emulate.c, does checking guest_has_cmpxchg8b() and
guest_has_cmpxchg16b() inside em_cmpxchg8b() perform the CPUID check too
late?
In x86_emulate_insn(), because ctxt->dst.type is OP_MEM and Mov is not
set, segmented_read() linearizes, validates, and reads the 8-byte or
16-byte destination operand before calling ctxt->execute(ctxt). If CX8 or
CX16 is withheld in guest CPUID, executing CMPXCHG8B or CMPXCHG16B on an
MMIO address exits to userspace with KVM_EXIT_MMIO to perform the read
before checking CPUID, and an unaligned 16-byte address or unmapped page
injects #GP(0) or #PF instead of #UD. Checking the feature in a
check_perm callback (with CheckPerm) would ensure #UD is raised before
any destination memory access.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261126060515.2198-2-sarunkod@amd.com?part=2
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-26 6:28 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 6:05 [PATCH v5 0/2] Add support for cmpxchg16b emulation Sairaj Kodilkar
2026-11-26 6:05 ` [PATCH v5 1/2] x86/uaccess: Extend CMPXCHG user helpers to 128-bit operands Sairaj Kodilkar
2026-11-26 6:22 ` sashiko-bot
2026-11-26 6:05 ` [PATCH v5 2/2] KVM: x86: Add support for cmpxchg16b emulation Sairaj Kodilkar
2026-11-26 6:28 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox