Skip to contentSkip to conversation

KVM: nSVM: Enable DecodeAssists for nested guests

PATCH Superseded Tina Zhang 2 participants 21 messages Jul 30 – Aug 13

Subsystems (4)

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Tina Zhang

author
#0 lore raw

v3 cover KVM: nSVM: Enable DecodeAssists for nested guests

The SVM DecodeAssists feature provides decode state for selected VM-Exits. KVM currently does not expose this feature to L1. Some L1 hypervisors may therefore treat the platform's SVM support as incomplete.

In practice, this was observed with Hyper-V running on top of KVM. Hyper-V appears to require DecodeAssists before enabling nested SVM for its guests. Virtualizing the feature lets users enable Hyper-V virtualization features inside a Windows VM when needed, e.g. to run QEMU/KVM in WSL. Without DecodeAssists, Hyper-V does not enable nested SVM because DecodeAssists is missing from KVM's virtual SVM model.

Virtualize DecodeAssists for nested SVM. Propagate instruction bytes from VMCB02 for hardware-originated data #NPF and intercepted data #PF VM-Exits. For KVM-generated exits, synthesize the architectural EXITINFO state and use matching bytes from the emulator when available, fetching missing bytes through L2's address translation only as a fallback. Do not use the fallback for SEV guests, whose encrypted memory cannot provide plaintext instruction bytes to KVM.

The selftest coverage in this version is intentionally broad and may be more extensive than necessary. Some redundant cases can be removed in a later revision, but for now the series provides a comprehensive test set for users to exercise hardware-reflected, KVM-synthesized, userspace-injected, and boundary cases.

The selftest has been run with kvm.force_emulation_prefix both disabled and enabled.

Changes since v2:
- Rebase onto kvm-x86/next.
- Track hardware-provided instruction bytes independently of the VMCB02 exit code, and preserve the bytes when L0 handles an intercepted #PF before reflecting it to L1.
- Select the instruction-byte source using host-owned VMCB02 state instead of control fields in guest-owned VMCB12.
- Record whether a queued #PF VM-Exit has a matching emulator context, so userspace-injected #PF exits do not consume stale emulator bytes.
- Stop fallback instruction fetches at noncanonical addresses and at the 32-bit linear-address boundary.
- Extend the selftest with regression coverage for replacing a hardware #NPF with a synthesized #NPF and for userspace-injected #PF during emulation, and harden its page layout and ucall handling.

v2:
https://lore.kernel.org/r/[email protected]

Tina Zhang (9):
KVM: x86: Add helper to provide intercept linear addresses
KVM: nSVM: Synthesize DecodeAssists EXITINFO for emulated intercepts
KVM: nSVM: Track hardware-provided instruction bytes
KVM: nSVM: Propagate hardware DecodeAssist bytes to VMCB12
KVM: x86: Track emulator-originated nested #PF VM-Exits
KVM: nSVM: Use emulator bytes for synthesized nested #NPF/#PF
KVM: nSVM: Fetch missing DecodeAssist bytes for synthesized #NPF/#PF
KVM: nSVM: Advertise DecodeAssists to L1
KVM: selftests: Add nested SVM DecodeAssists test

 arch/x86/include/asm/kvm_host.h               |   1 +
 arch/x86/kvm/emulate.c                        |  29 +-
 arch/x86/kvm/kvm_emulate.h                    |   1 +
 arch/x86/kvm/svm/nested.c                     | 181 +++-
 arch/x86/kvm/svm/svm.c                        |  72 +-
 arch/x86/kvm/svm/svm.h                        |  20 +-
 arch/x86/kvm/x86.c                            |  33 +-
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/include/x86/processor.h     |   1 +
 .../kvm/x86/svm_nested_decode_assists_test.c  | 791 ++++++++++++++++++
 10 files changed, 1100 insertions(+), 30 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c

base-commit: 567329869b9c75c9d3df74b35fe10af9b51c479d

Signature · 1 line
2.43.7
author
#1 lore raw

PATCH v3 1/9 KVM: x86: Add helper to provide intercept linear addresses

Add get_intercept_linear_addr() to compute the linear address for intercepts that report one, and pass the result through x86_instruction_info.

Handle INVLPG as the initial user. INVLPG's memory operand is decoded with NoAccess, and thus src_val does not contain the operand address. Compute the address from the decoded segment base and effective address in the emulator, where the operand state is available.

This allows intercept handlers to use the linear address directly without duplicating the emulator's address calculation.

---
 arch/x86/kvm/emulate.c     | 29 +++++++++++++++++++++--------
 arch/x86/kvm/kvm_emulate.h |  1 +
 2 files changed, 22 insertions(+), 8 deletions(-)
arch/x86/kvm/emulate.c +21 -8
index 8071b372d233..a0e57b64cadd 100644
@@ -410,6 +410,26 @@ static int em_salc(struct x86_emulate_ctxt *ctxt)
 	_fault ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE; \
 })
 
+static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
+{
+	if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
+		return 0;
+
+	return ctxt->ops->get_cached_segment_base(ctxt, seg);
+}
+
+static u64 get_intercept_linear_addr(struct x86_emulate_ctxt *ctxt,
+				     enum x86_intercept intercept)
+{
+	u64 la;
+
+	if (intercept != x86_intercept_invlpg)
+		return 0;
+
+	la = seg_base(ctxt, ctxt->src.addr.mem.seg) + ctxt->src.addr.mem.ea;
+	return ctxt->mode == X86EMUL_MODE_PROT64 ? la : (u32)la;
+}
+
 static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
 				    enum x86_intercept intercept,
 				    enum x86_intercept_stage stage)
@@ -427,6 +447,7 @@ static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
 		.src_type   = ctxt->src.type,
 		.dst_type   = ctxt->dst.type,
 		.ad_bytes   = ctxt->ad_bytes,
+		.intercept_linear_addr = get_intercept_linear_addr(ctxt, intercept),
 		.rip	    = ctxt->eip,
 		.next_rip   = ctxt->_eip,
 	};
@@ -520,14 +541,6 @@ static u32 desc_limit_scaled(struct desc_struct *desc)
 	return desc->g ? (limit << 12) | 0xfff : limit;
 }
 
-static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
-{
-	if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
-		return 0;
-
-	return ctxt->ops->get_cached_segment_base(ctxt, seg);
-}
-
 static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
 			     u32 error, bool valid)
 {
arch/x86/kvm/kvm_emulate.h +1 -0
index 3e375af15c03..9eda648a1314 100644
@@ -51,6 +51,7 @@ struct x86_instruction_info {
 	u8  src_type;		/* type of source operand		*/
 	u8  dst_type;		/* type of destination operand		*/
 	u8  ad_bytes;           /* size of src/dst address              */
+	u64 intercept_linear_addr;	/* arch-reported linear address, if any */
 	u64 rip;		/* rip of the instruction		*/
 	u64 next_rip;           /* rip following the instruction        */
 };
Signature · 1 line
2.43.7
#2 lore raw

Re: [PATCH v3 1/9] KVM: x86: Add helper to provide intercept linear addresses

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The patch computes intercept_linear_addr and adds it to x86_instruction_info, but fails to actually use it in the architecture-specific intercept handlers (svm_check_intercept and vmx_check_intercept), leaving a severe preexisting bug unfixed. --

--- Patch [1]: [PATCH v3 1/9] KVM: x86: Add helper to provide intercept linear addresses --- commit e0c208c3018cdb4b9689064eac5e998c1b4d07df
Author: Tina Zhang <[email protected]>

KVM: x86: Add helper to provide intercept linear addresses

This commit adds get_intercept_linear_addr() to compute the linear address for INVLPG intercepts. It passes this address through x86_instruction_info so intercept handlers can use it directly without duplicating the calculation.

Quoted 3 lines
arch/x86/kvm/emulate.c +0 -0

[ ... ]

arch/x86/kvm/emulate.c static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt, · line 453

Quoted 8 lines
arch/x86/kvm/emulate.c +1 -0
@@ -427,6 +447,7 @@ static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
 		.src_type   = ctxt->src.type,
 		.dst_type   = ctxt->dst.type,
 		.ad_bytes   = ctxt->ad_bytes,
+		.intercept_linear_addr = get_intercept_linear_addr(ctxt, intercept),
 		.rip	    = ctxt->eip,
 		.next_rip   = ctxt->_eip,
 	};

[Severity: High]
This isn't a bug introduced by this patch, but it looks like the architecture backends might still need to be updated to consume this new field.

If an L2 guest executes an INVLPG instruction that KVM intercepts and emulates, and L1 is also intercepting INVLPG, the KVM emulator populates intercept_linear_addr here.

However, if the handlers in arch/x86/kvm/svm/svm.c:svm_check_intercept() and arch/x86/kvm/vmx/vmx.c:vmx_check_intercept() don't use this new field, does KVM synthesize nested VM-exits for INVLPG incorrectly?

For SVM, if it falls through to the default case and leaves vmcb->control.exit_info_1 unmodified, could this leak previous host or VM-exit data to the L1 hypervisor?

For VMX, does lacking a case for x86_intercept_invlpg entirely cause it to return X86EMUL_UNHANDLEABLE and fail emulation?

Should the hardware intercept handlers be updated in this series to prevent stale data leakage and L2 DoS?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#3 lore raw

Re: [PATCH v3 1/9] KVM: x86: Add helper to provide intercept linear addresses

Quoted 45 lines · On 7/30/2026 8:38 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The patch computes intercept_linear_addr and adds it to x86_instruction_info, but fails to actually use it in the architecture-specific intercept handlers (svm_check_intercept and vmx_check_intercept), leaving a severe preexisting bug unfixed. --

--- Patch [1]: [PATCH v3 1/9] KVM: x86: Add helper to provide intercept linear addresses --- commit e0c208c3018cdb4b9689064eac5e998c1b4d07df
Author: Tina Zhang <[email protected]>

KVM: x86: Add helper to provide intercept linear addresses

This commit adds get_intercept_linear_addr() to compute the linear address for INVLPG intercepts. It passes this address through x86_instruction_info so intercept handlers can use it directly without duplicating the calculation.

Quoted 3 lines
arch/x86/kvm/emulate.c +0 -0

[ ... ]

arch/x86/kvm/emulate.c static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt, · line 453

Quoted 8 lines
arch/x86/kvm/emulate.c +1 -0
@@ -427,6 +447,7 @@ static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
  		.src_type   = ctxt->src.type,
  		.dst_type   = ctxt->dst.type,
  		.ad_bytes   = ctxt->ad_bytes,
+		.intercept_linear_addr = get_intercept_linear_addr(ctxt, intercept),
  		.rip	    = ctxt->eip,
  		.next_rip   = ctxt->_eip,
  	};

[Severity: High]
This isn't a bug introduced by this patch, but it looks like the architecture backends might still need to be updated to consume this new field.

If an L2 guest executes an INVLPG instruction that KVM intercepts and emulates, and L1 is also intercepting INVLPG, the KVM emulator populates intercept_linear_addr here.

However, if the handlers in arch/x86/kvm/svm/svm.c:svm_check_intercept() and arch/x86/kvm/vmx/vmx.c:vmx_check_intercept() don't use this new field, does KVM synthesize nested VM-exits for INVLPG incorrectly?

For SVM, if it falls through to the default case and leaves vmcb->control.exit_info_1 unmodified, could this leak previous host or VM-exit data to the L1 hypervisor?

Quoted 5 lines

For VMX, does lacking a case for x86_intercept_invlpg entirely cause it to return X86EMUL_UNHANDLEABLE and fail emulation?

Should the hardware intercept handlers be updated in this series to prevent stale data leakage and L2 DoS?

The SVM consumer is added in patch 2/9. svm_prepare_decode_assist_exit_info() uses info->intercept_linear_addr for x86_intercept_invlpg and writes it to EXITINFO1 before constructing the nested VM-Exit.

The absence of x86_intercept_invlpg handling in vmx_check_intercept() predates this series. VMX does not implement SVM DecodeAssists or VMCB EXITINFO semantics, and patch 1 does not change the existing VMX behavior. Supporting emulated nested VMX INVLPG would be a separate change.

Thanks,
Tina

Quoted 1 line
author
#4 lore raw

PATCH v3 2/9 KVM: nSVM: Synthesize DecodeAssists EXITINFO for emulated intercepts

In reply to Tina Zhang

When the x86 emulator encounters an instruction intercepted by L1, svm_check_intercept() synthesizes a nested VM-Exit without fresh hardware DecodeAssist state. Populate the architectural EXITINFO fields when DecodeAssists is exposed to L1.

Generate EXITINFO1 with the GPR number for MOV CR/DR, the interrupt vector for INTn, and the linear address for INVLPG. Set EXITINFO1 to zero for CLTS, LMSW, SMSW, and INVLPGA; the INVLPGA address remains in guest rAX. Clear EXITINFO2 for all covered intercepts and leave unrelated intercepts unchanged.

---
 arch/x86/kvm/svm/svm.c | 48 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
arch/x86/kvm/svm/svm.c +48 -0
index d68cba12c772..b63234a2feb8 100644
@@ -4822,6 +4822,52 @@ static const struct __x86_intercept {
 #undef POST_EX
 #undef POST_MEM
 
+static void svm_prepare_decode_assist_exit_info(struct kvm_vcpu *vcpu,
+						const struct x86_instruction_info *info)
+{
+	struct vmcb *vmcb = to_svm(vcpu)->vmcb;
+	u64 exit_info_1;
+
+	if (!guest_cpu_cap_has(vcpu, X86_FEATURE_DECODEASSISTS))
+		return;
+
+	switch (info->intercept) {
+	case x86_intercept_cr_read:
+	case x86_intercept_cr_write:
+		/* MOV CRx: bit 63 set, GPR number in bits 3:0. */
+		exit_info_1 = BIT_ULL(63) | (info->modrm_rm & 0xf);
+		break;
+	case x86_intercept_clts:
+	case x86_intercept_lmsw:
+	case x86_intercept_smsw:
+		/* CLTS/LMSW/SMSW: no decode information, bit 63 clear. */
+		exit_info_1 = 0;
+		break;
+	case x86_intercept_dr_read:
+	case x86_intercept_dr_write:
+		/* MOV DRx: GPR number in bits 3:0. */
+		exit_info_1 = info->modrm_rm & 0xf;
+		break;
+	case x86_intercept_intn:
+		/* INTn: software interrupt number in bits 7:0. */
+		exit_info_1 = info->src_val & 0xff;
+		break;
+	case x86_intercept_invlpg:
+		/* INVLPG: linear address of the target page. */
+		exit_info_1 = info->intercept_linear_addr;
+		break;
+	case x86_intercept_invlpga:
+		/* INVLPGA: the address remains available in guest rAX. */
+		exit_info_1 = 0;
+		break;
+	default:
+		return;
+	}
+
+	vmcb->control.exit_info_1 = exit_info_1;
+	vmcb->control.exit_info_2 = 0;
+}
+
 static int svm_check_intercept(struct kvm_vcpu *vcpu,
 			       struct x86_instruction_info *info,
 			       enum x86_intercept_stage stage,
@@ -4943,6 +4989,8 @@ static int svm_check_intercept(struct kvm_vcpu *vcpu,
 		break;
 	}
 
+	svm_prepare_decode_assist_exit_info(vcpu, info);
+
 	/* TODO: Advertise NRIPS to guest hypervisor unconditionally */
 	if (static_cpu_has(X86_FEATURE_NRIPS))
 		vmcb->control.next_rip  = info->next_rip;
Signature · 1 line
2.43.7
#5 lore raw

Re: [PATCH v3 2/9] KVM: nSVM: Synthesize DecodeAssists EXITINFO for emulated intercepts

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Synthesized EXITINFO1 is incorrectly populated for SVM_EXIT_CR0_SEL_WRITE intercepts triggered by…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Synthesized EXITINFO1 is incorrectly populated for SVM_EXIT_CR0_SEL_WRITE intercepts triggered by MOV CR0, violating the AMD APM. --

--- Patch [2]: [PATCH v3 2/9] KVM: nSVM: Synthesize DecodeAssists EXITINFO for emulated intercepts --- commit 60593c4facd0e8bdc8b0aa501c98449feeced81a
Author: Tina Zhang <[email protected]>

KVM: nSVM: Synthesize DecodeAssists EXITINFO for emulated intercepts

This commit populates the architectural EXITINFO fields when DecodeAssists is exposed to L1 and KVM synthesizes a nested VM-Exit for emulated instructions. It targets specific intercepts like MOV CR/DR, INTn, and INVLPG.

Quoted 5 lines
arch/x86/kvm/svm/svm.c +0 -0
index d68cba12c772b..b63234a2feb8d 100644
@@ -4822,6 +4822,52 @@ static const struct __x86_intercept {

[ ... ]

Quoted 4 lines
arch/x86/kvm/svm/svm.c +4 -0
+static void svm_prepare_decode_assist_exit_info(struct kvm_vcpu *vcpu,
+						const struct x86_instruction_info *info)
+{
+	struct vmcb *vmcb = to_svm(vcpu)->vmcb;

arch/x86/kvm/svm/svm.c static const struct __x86_intercept { · line 4838

Quoted 10 lines
arch/x86/kvm/svm/svm.c +10 -0
+	u64 exit_info_1;
+
+	if (!guest_cpu_cap_has(vcpu, X86_FEATURE_DECODEASSISTS))
+		return;
+
+	switch (info->intercept) {
+	case x86_intercept_cr_read:
+	case x86_intercept_cr_write:
+		/* MOV CRx: bit 63 set, GPR number in bits 3:0. */
+		exit_info_1 = BIT_ULL(63) | (info->modrm_rm & 0xf);

[Severity: High]
Will this incorrectly populate exit_info_1 for selective CR0 writes?

In svm_check_intercept(), the exit code can be dynamically upgraded to SVM_EXIT_CR0_SEL_WRITE for MOV CR0 instructions:

svm_check_intercept() {
        ...
        cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
        val = info->src_val  & ~SVM_CR0_SELECTIVE_MASK;
        if (cr0 ^ val)
                icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
        ...
}

Since svm_prepare_decode_assist_exit_info() is subsequently called and only checks the original instruction intercept (x86_intercept_cr_write), could this wrongly populate exit_info_1 with decode assist information instead of leaving it zero as required by the AMD architecture manual for SVM_EXIT_CR0_SEL_WRITE?

Quoted 2 lines
arch/x86/kvm/svm/svm.c +2 -0
+		break;
+	case x86_intercept_clts:

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author
#6 lore raw

Re: [PATCH v3 2/9] KVM: nSVM: Synthesize DecodeAssists EXITINFO for emulated intercepts

Quoted 51 lines · On 7/30/2026 8:30 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Synthesized EXITINFO1 is incorrectly populated for SVM_EXIT_CR0_SEL_WRITE intercepts triggered by MOV CR0, violating the AMD APM. --

--- Patch [2]: [PATCH v3 2/9] KVM: nSVM: Synthesize DecodeAssists EXITINFO for emulated intercepts --- commit 60593c4facd0e8bdc8b0aa501c98449feeced81a
Author: Tina Zhang <[email protected]>

KVM: nSVM: Synthesize DecodeAssists EXITINFO for emulated intercepts

This commit populates the architectural EXITINFO fields when DecodeAssists is exposed to L1 and KVM synthesizes a nested VM-Exit for emulated instructions. It targets specific intercepts like MOV CR/DR, INTn, and INVLPG.

Quoted 5 lines
arch/x86/kvm/svm/svm.c +0 -0
index d68cba12c772b..b63234a2feb8d 100644
@@ -4822,6 +4822,52 @@ static const struct __x86_intercept {

[ ... ]

Quoted 4 lines
arch/x86/kvm/svm/svm.c +4 -0
+static void svm_prepare_decode_assist_exit_info(struct kvm_vcpu *vcpu,
+						const struct x86_instruction_info *info)
+{
+	struct vmcb *vmcb = to_svm(vcpu)->vmcb;

arch/x86/kvm/svm/svm.c static const struct __x86_intercept { · line 4838

Quoted 10 lines
arch/x86/kvm/svm/svm.c +10 -0
+	u64 exit_info_1;
+
+	if (!guest_cpu_cap_has(vcpu, X86_FEATURE_DECODEASSISTS))
+		return;
+
+	switch (info->intercept) {
+	case x86_intercept_cr_read:
+	case x86_intercept_cr_write:
+		/* MOV CRx: bit 63 set, GPR number in bits 3:0. */
+		exit_info_1 = BIT_ULL(63) | (info->modrm_rm & 0xf);

[Severity: High]
Will this incorrectly populate exit_info_1 for selective CR0 writes?

In svm_check_intercept(), the exit code can be dynamically upgraded to SVM_EXIT_CR0_SEL_WRITE for MOV CR0 instructions:

svm_check_intercept() {
         ...
         cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
         val = info->src_val  & ~SVM_CR0_SELECTIVE_MASK;
         if (cr0 ^ val)
                 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
         ...
}
Quoted 5 lines

Since svm_prepare_decode_assist_exit_info() is subsequently called and only checks the original instruction intercept (x86_intercept_cr_write), could this wrongly populate exit_info_1 with decode assist information instead of leaving it zero as required by the AMD architecture manual for SVM_EXIT_CR0_SEL_WRITE?

SVM_EXIT_CR0_SEL_WRITE does not imply that EXITINFO1 must be zero. The exit code identifies the selective CR0 intercept, whereas the DecodeAssist contents describe the instruction that triggered it.

For MOV-to-CR0, the APM specifies that EXITINFO1 provides the GPR number and identifies the instruction as MOV CRx. For LMSW and CLTS, no additional information is provided and bit 63 is clear.

Checking info->intercept is therefore intentional, as it distinguishes MOV-to-CR0 from LMSW when both can result in SVM_EXIT_CR0_SEL_WRITE.

Thanks,
Tina

Quoted 5 lines
Quoted 3 lines
arch/x86/kvm/svm/svm.c +2 -0
+		break;
+	case x86_intercept_clts:

[ ... ]

author
#7 lore raw

PATCH v3 3/9 KVM: nSVM: Track hardware-provided instruction bytes

In reply to Tina Zhang

DecodeAssists provides instruction bytes for nested page faults and intercepted page faults caused by data accesses. When reflecting such an exit to L1, KVM needs to distinguish bytes produced by the current hardware VM-Exit from stale VMCB02 state.

Clear the VMCB02 instruction-byte fields before each nested run and track whether the nested VM-Exit being reflected originated from hardware. Preserve that state when a hardware #PF is processed by KVM and then reflected to L1.

A subsequent change will use this state to propagate hardware-provided instruction bytes to VMCB12.

---
 arch/x86/kvm/svm/nested.c | 17 +++++++++++++++--
 arch/x86/kvm/svm/svm.c    | 22 ++++++++++++++--------
 arch/x86/kvm/svm/svm.h    |  8 +++++++-
 3 files changed, 36 insertions(+), 11 deletions(-)
arch/x86/kvm/svm/nested.c +15 -2
index 5e3e280cd483..ce059d3a1322 100644
@@ -35,6 +35,13 @@
 
 #define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK
 
+static void nested_svm_clear_insn_bytes(struct vmcb *vmcb)
+{
+	vmcb->control.insn_len = 0;
+	memset(vmcb->control.insn_bytes, 0,
+	       sizeof(vmcb->control.insn_bytes));
+}
+
 static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
 				       struct x86_exception *fault,
 				       bool from_hardware)
@@ -68,6 +75,7 @@ static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
 				    (fault->error_code & ~PFERR_GUEST_FAULT_STAGE_MASK);
 	vmcb->control.exit_info_2 = fault->address;
 
+	svm->nested.vmcb02_insn_bytes_fresh = from_hardware;
 	nested_svm_vmexit(svm);
 }
 
@@ -868,7 +876,10 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
 	/*
 	 * Filled at exit: exit_code, exit_info_1, exit_info_2, exit_int_info,
 	 * exit_int_info_err, next_rip, insn_len, insn_bytes.
+	 * Clear stale DecodeAssist data before L2 runs.
 	 */
+	nested_svm_clear_insn_bytes(vmcb02);
+	svm->nested.vmcb02_insn_bytes_fresh = false;
 
 	if (guest_cpu_cap_has(vcpu, X86_FEATURE_VGIF) &&
 	    (vmcb12_ctrl->int_ctl & V_GIF_ENABLE_MASK))
@@ -1647,14 +1658,16 @@ static int nested_svm_intercept(struct vcpu_svm *svm)
 	return vmexit;
 }
 
-int nested_svm_exit_handled(struct vcpu_svm *svm)
+int nested_svm_exit_handled(struct vcpu_svm *svm, bool vmcb02_insn_bytes_fresh)
 {
 	int vmexit;
 
 	vmexit = nested_svm_intercept(svm);
 
-	if (vmexit == NESTED_EXIT_DONE)
+	if (vmexit == NESTED_EXIT_DONE) {
+		svm->nested.vmcb02_insn_bytes_fresh = vmcb02_insn_bytes_fresh;
 		nested_svm_vmexit(svm);
+	}
 
 	return vmexit;
 }
arch/x86/kvm/svm/svm.c +14 -8
index b63234a2feb8..6e2ceb700567 100644
@@ -1953,14 +1953,20 @@ static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
 static int pf_interception(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
-
 	u64 fault_address = svm->vmcb->control.exit_info_2;
 	u64 error_code = svm->vmcb->control.exit_info_1;
+	int r;
+
+	r = kvm_handle_page_fault(vcpu, error_code, fault_address,
+				  static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
+				  svm->vmcb->control.insn_bytes : NULL,
+				  svm->vmcb->control.insn_len);
 
-	return kvm_handle_page_fault(vcpu, error_code, fault_address,
-			static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
-			svm->vmcb->control.insn_bytes : NULL,
-			svm->vmcb->control.insn_len);
+	if (is_guest_mode(vcpu) && vcpu->arch.exception_vmexit.pending &&
+	    vcpu->arch.exception_vmexit.vector == PF_VECTOR)
+		svm->nested.vmcb02_insn_bytes_fresh = true;
+
+	return r;
 }
 
 static int svm_check_emulate_instruction(struct kvm_vcpu *vcpu, int emul_type,
@@ -2573,7 +2579,7 @@ static bool check_selective_cr0_intercepted(struct kvm_vcpu *vcpu,
 
 	if (cr0 ^ val) {
 		svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
-		ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
+		ret = (nested_svm_exit_handled(svm, false) == NESTED_EXIT_DONE);
 	}
 
 	return ret;
@@ -3725,7 +3731,7 @@ static int svm_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
 		vmexit = nested_svm_exit_special(svm);
 
 		if (vmexit == NESTED_EXIT_CONTINUE)
-			vmexit = nested_svm_exit_handled(svm);
+			vmexit = nested_svm_exit_handled(svm, true);
 
 		if (vmexit == NESTED_EXIT_DONE)
 			return 1;
@@ -4995,7 +5001,7 @@ static int svm_check_intercept(struct kvm_vcpu *vcpu,
 	if (static_cpu_has(X86_FEATURE_NRIPS))
 		vmcb->control.next_rip  = info->next_rip;
 	vmcb->control.exit_code = icpt_info.exit_code;
-	vmexit = nested_svm_exit_handled(svm);
+	vmexit = nested_svm_exit_handled(svm, false);
 
 	ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
 					   : X86EMUL_CONTINUE;
arch/x86/kvm/svm/svm.h +7 -1
index a327bf751ecd..96d2471a03c8 100644
@@ -242,6 +242,12 @@ struct svm_nested_state {
 	 * on its side.
 	 */
 	bool force_msr_bitmap_recalc;
+
+	/*
+	 * True if VMCB02 contains DecodeAssist instruction bytes from the
+	 * hardware VM-Exit currently being reflected to L1.
+	 */
+	bool vmcb02_insn_bytes_fresh;
 };
 
 struct vcpu_sev_es_state {
@@ -887,7 +893,7 @@ static inline void nested_svm_simple_vmexit(struct vcpu_svm *svm, u32 exit_code)
 	nested_svm_vmexit(svm);
 }
 
-int nested_svm_exit_handled(struct vcpu_svm *svm);
+int nested_svm_exit_handled(struct vcpu_svm *svm, bool vmcb02_insn_bytes_fresh);
 int nested_svm_check_permissions(struct kvm_vcpu *vcpu);
 int nested_svm_check_cached_vmcb12(struct kvm_vcpu *vcpu);
 int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
Signature · 1 line
2.43.7
#8 lore raw

Re: [PATCH v3 3/9] KVM: nSVM: Track hardware-provided instruction bytes

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] The patch clears instruction bytes only on nested VMRUN, causing stale DecodeAssist data from…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The patch clears instruction bytes only on nested VMRUN, causing stale DecodeAssist data from L0-handled exits to be falsely flagged as fresh during subsequent intercepts.
- [High] Hardware-provided DecodeAssist data and the fresh flag are lost across live migration for a pending nested #PF.
--

--- Patch [3]: [PATCH v3 3/9] KVM: nSVM: Track hardware-provided instruction bytes --- commit e944fdb4c1650228b5e210eab6c8b6fe2761112a
Author: Tina Zhang <[email protected]>

KVM: nSVM: Track hardware-provided instruction bytes

DecodeAssists provides instruction bytes for nested page faults and intercepted page faults caused by data accesses. When reflecting such an exit to L1, KVM needs to distinguish bytes produced by the current hardware VM-Exit from stale VMCB02 state.

Quoted 4 lines
arch/x86/kvm/svm/svm.c +0 -0
index b63234a2feb8d..6e2ceb700567c 100644

[ ... ]

Quoted 11 lines
arch/x86/kvm/svm/svm.c +4 -1
@@ -1953,14 +1953,20 @@ static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
 static int pf_interception(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
-
 	u64 fault_address = svm->vmcb->control.exit_info_2;
 	u64 error_code = svm->vmcb->control.exit_info_1;
+	int r;
+
+	r = kvm_handle_page_fault(vcpu, error_code, fault_address,
+				  static_cpu_has(X86_FEATURE_DECODEASSISTS) ?

arch/x86/kvm/svm/svm.c static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value) · line 1967

Quoted 10 lines
arch/x86/kvm/svm/svm.c +5 -4
+				  svm->vmcb->control.insn_bytes : NULL,
+				  svm->vmcb->control.insn_len);
 
-	return kvm_handle_page_fault(vcpu, error_code, fault_address,
-			static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
-			svm->vmcb->control.insn_bytes : NULL,
-			svm->vmcb->control.insn_len);
+	if (is_guest_mode(vcpu) && vcpu->arch.exception_vmexit.pending &&
+	    vcpu->arch.exception_vmexit.vector == PF_VECTOR)
+		svm->nested.vmcb02_insn_bytes_fresh = true;
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

[Severity: High]
Does this unconditionally mark the instruction bytes as fresh for any PF_VECTOR, including instruction fetches where DecodeAssist is inactive?

If so, could this result in stale bytes from a previous hardware VM-Exit being incorrectly synthesized to the L1 hypervisor?

Quoted 3 lines
arch/x86/kvm/svm/svm.c +2 -0
+
+	return r;
 }

[ ... ]

arch/x86/kvm/svm/svm.c static int svm_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath) · line 3734

Quoted 6 lines
arch/x86/kvm/svm/svm.c +1 -1
@@ -3725,7 +3731,7 @@ static int svm_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
 		vmexit = nested_svm_exit_special(svm);
 
 		if (vmexit == NESTED_EXIT_CONTINUE)
-			vmexit = nested_svm_exit_handled(svm);
+			vmexit = nested_svm_exit_handled(svm, true);
                                                              ^^^^

[Severity: High]
By passing true here for all NESTED_EXIT_CONTINUE VM-exits, does this incorrectly flag stale bytes as fresh for exits where DecodeAssist is not active?

For example, if L2 causes a data #PF handled by L0 (which writes insn_bytes) and is resumed without clearing the instruction bytes, and then L2 causes a different exit intercepted by L1 (like an MSR intercept), will this pass the stale instruction bytes to L1 as fresh?

Quoted 3 lines
arch/x86/kvm/svm/svm.c +0 -0
 		if (vmexit == NESTED_EXIT_DONE)
 			return 1;

[ ... ]

Quoted 5 lines
arch/x86/kvm/svm/svm.h +0 -0
index a327bf751ecd5..96d2471a03c8c 100644

arch/x86/kvm/svm/svm.h struct svm_nested_state { · line 250

Quoted 9 lines
arch/x86/kvm/svm/svm.h +6 -0
@@ -242,6 +242,12 @@ struct svm_nested_state {
 	 * on its side.
 	 */
 	bool force_msr_bitmap_recalc;
+
+	/*
+	 * True if VMCB02 contains DecodeAssist instruction bytes from the
+	 * hardware VM-Exit currently being reflected to L1.
+	 */
+	bool vmcb02_insn_bytes_fresh;
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

[Severity: High]
Will this tracking state be lost across live migration?

Since it appears this flag is added to the nested state structure but omitted from KVM_GET_NESTED_STATE serialization, if a migration occurs while a nested exit is pending, won't the target host fail to receive the instruction bytes and potentially cause emulation failure in L1?

Quoted 1 line
arch/x86/kvm/svm/svm.h +0 -0
 };
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author
#9 lore raw

Re: [PATCH v3 3/9] KVM: nSVM: Track hardware-provided instruction bytes

Quoted 46 lines · On 7/30/2026 8:34 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The patch clears instruction bytes only on nested VMRUN, causing stale DecodeAssist data from L0-handled exits to be falsely flagged as fresh during subsequent intercepts.
- [High] Hardware-provided DecodeAssist data and the fresh flag are lost across live migration for a pending nested #PF.
--

--- Patch [3]: [PATCH v3 3/9] KVM: nSVM: Track hardware-provided instruction bytes --- commit e944fdb4c1650228b5e210eab6c8b6fe2761112a
Author: Tina Zhang <[email protected]>

KVM: nSVM: Track hardware-provided instruction bytes

DecodeAssists provides instruction bytes for nested page faults and intercepted page faults caused by data accesses. When reflecting such an exit to L1, KVM needs to distinguish bytes produced by the current hardware VM-Exit from stale VMCB02 state.

Quoted 4 lines
arch/x86/kvm/svm/svm.c +0 -0
index b63234a2feb8d..6e2ceb700567c 100644

[ ... ]

Quoted 11 lines
arch/x86/kvm/svm/svm.c +4 -1
@@ -1953,14 +1953,20 @@ static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
  static int pf_interception(struct kvm_vcpu *vcpu)
  {
  	struct vcpu_svm *svm = to_svm(vcpu);
-
  	u64 fault_address = svm->vmcb->control.exit_info_2;
  	u64 error_code = svm->vmcb->control.exit_info_1;
+	int r;
+
+	r = kvm_handle_page_fault(vcpu, error_code, fault_address,
+				  static_cpu_has(X86_FEATURE_DECODEASSISTS) ?

arch/x86/kvm/svm/svm.c static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value) · line 1967

Quoted 10 lines
arch/x86/kvm/svm/svm.c +5 -4
+				  svm->vmcb->control.insn_bytes : NULL,
+				  svm->vmcb->control.insn_len);
 
-	return kvm_handle_page_fault(vcpu, error_code, fault_address,
-			static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
-			svm->vmcb->control.insn_bytes : NULL,
-			svm->vmcb->control.insn_len);
+	if (is_guest_mode(vcpu) && vcpu->arch.exception_vmexit.pending &&
+	    vcpu->arch.exception_vmexit.vector == PF_VECTOR)
+		svm->nested.vmcb02_insn_bytes_fresh = true;
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 6 lines

[Severity: High]
Does this unconditionally mark the instruction bytes as fresh for any PF_VECTOR, including instruction fetches where DecodeAssist is inactive?

If so, could this result in stale bytes from a previous hardware VM-Exit being incorrectly synthesized to the L1 hypervisor?

The fresh flag is not sufficient by itself to copy VMCB02 instruction bytes. Patch 4/9 also requires the current trusted VMCB02 exit to be a data #PF or #NPF. Instruction-fetch faults and unrelated exits, such as MSR exits, therefore leave VMCB12's instruction-byte state cleared.

Quoted 20 lines
Quoted 4 lines
arch/x86/kvm/svm/svm.c +2 -0
+
+	return r;
  }

[ ... ]

arch/x86/kvm/svm/svm.c static int svm_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath) · line 3734

Quoted 6 lines
arch/x86/kvm/svm/svm.c +1 -1
@@ -3725,7 +3731,7 @@ static int svm_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
  		vmexit = nested_svm_exit_special(svm);
 
  		if (vmexit == NESTED_EXIT_CONTINUE)
-			vmexit = nested_svm_exit_handled(svm);
+			vmexit = nested_svm_exit_handled(svm, true);
                                                               ^^^^

[Severity: High]
By passing true here for all NESTED_EXIT_CONTINUE VM-exits, does this incorrectly flag stale bytes as fresh for exits where DecodeAssist is not active?

Quoted 4 lines

For example, if L2 causes a data #PF handled by L0 (which writes insn_bytes) and is resumed without clearing the instruction bytes, and then L2 causes a different exit intercepted by L1 (like an MSR intercept), will this pass the stale instruction bytes to L1 as fresh?

No. The true argument to nested_svm_exit_handled() is consumed only when the current hardware exit is reflected to L1, and the flag is cleared while constructing VMCB12. Exits handled by L0 do not retain the flag.

Quoted 23 lines
Quoted 4 lines
arch/x86/kvm/svm/svm.c +0 -0
  		if (vmexit == NESTED_EXIT_DONE)
  			return 1;

[ ... ]

Quoted 5 lines
arch/x86/kvm/svm/svm.h +0 -0
index a327bf751ecd5..96d2471a03c8c 100644

arch/x86/kvm/svm/svm.h struct svm_nested_state { · line 250

Quoted 9 lines
arch/x86/kvm/svm/svm.h +6 -0
@@ -242,6 +242,12 @@ struct svm_nested_state {
  	 * on its side.
  	 */
  	bool force_msr_bitmap_recalc;
+
+	/*
+	 * True if VMCB02 contains DecodeAssist instruction bytes from the
+	 * hardware VM-Exit currently being reflected to L1.
+	 */
+	bool vmcb02_insn_bytes_fresh;
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 7 lines

[Severity: High]
Will this tracking state be lost across live migration?

Since it appears this flag is added to the nested state structure but omitted from KVM_GET_NESTED_STATE serialization, if a migration occurs while a nested exit is pending, won't the target host fail to receive the instruction bytes and potentially cause emulation failure in L1?

The flag is intentionally not migrated. It describes hardware state produced by a VM-Exit on the current host, which is no longer valid on the destination. A migrated pending #PF is restored without hardware provenance and uses the on-demand instruction-fetch fallback added later in the series. Restoring the flag alone would incorrectly mark the destination's VMCB02 contents as fresh.

Thanks,
Tina

Quoted 3 lines
Quoted 2 lines
arch/x86/kvm/svm/svm.h +0 -0
  };
author
#10 lore raw

PATCH v3 4/9 KVM: nSVM: Propagate hardware DecodeAssist bytes to VMCB12

In reply to Tina Zhang

DecodeAssists provides instruction bytes for nested page faults and intercepted page faults caused by data accesses. When the feature is exposed to L1, copy fresh hardware-provided instruction bytes from VMCB02 to VMCB12 for these exits.

Clear the VMCB12 instruction-byte state before rebuilding it, and leave the length zero for instruction-fetch page faults, unrelated exits, and exits without fresh hardware bytes.

---
 arch/x86/kvm/svm/nested.c | 49 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
arch/x86/kvm/svm/nested.c +49 -0
index ce059d3a1322..5ad744ea668d 100644
@@ -42,6 +42,53 @@ static void nested_svm_clear_insn_bytes(struct vmcb *vmcb)
 	       sizeof(vmcb->control.insn_bytes));
 }
 
+static void nested_svm_copy_insn_bytes(struct vmcb *to,
+				       const struct vmcb *from)
+{
+	u8 insn_len = from->control.insn_len;
+
+	nested_svm_clear_insn_bytes(to);
+
+	if (WARN_ON_ONCE(insn_len > sizeof(from->control.insn_bytes)))
+		return;
+
+	to->control.insn_len = insn_len;
+	memcpy(to->control.insn_bytes, from->control.insn_bytes, insn_len);
+}
+
+static bool nested_svm_vmexit_supports_insn_bytes(struct kvm_vcpu *vcpu,
+						  const struct vmcb *vmcb02)
+{
+	u64 exit_code = vmcb02->control.exit_code;
+
+	if (!guest_cpu_cap_has(vcpu, X86_FEATURE_DECODEASSISTS))
+		return false;
+
+	if (exit_code != SVM_EXIT_NPF &&
+	    exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR)
+		return false;
+
+	return !(vmcb02->control.exit_info_1 & PFERR_FETCH_MASK);
+}
+
+static void nested_svm_update_vmcb12_insn_bytes(struct kvm_vcpu *vcpu,
+						struct vmcb *vmcb12,
+						struct vmcb *vmcb02)
+{
+	struct vcpu_svm *svm = to_svm(vcpu);
+
+	nested_svm_clear_insn_bytes(vmcb12);
+
+	if (!nested_svm_vmexit_supports_insn_bytes(vcpu, vmcb02))
+		goto out;
+
+	if (svm->nested.vmcb02_insn_bytes_fresh)
+		nested_svm_copy_insn_bytes(vmcb12, vmcb02);
+
+out:
+	svm->nested.vmcb02_insn_bytes_fresh = false;
+}
+
 static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
 				       struct x86_exception *fault,
 				       bool from_hardware)
@@ -1311,6 +1358,8 @@ static int nested_svm_vmexit_update_vmcb12(struct kvm_vcpu *vcpu)
 	if (guest_cpu_cap_has(vcpu, X86_FEATURE_NRIPS))
 		vmcb12->control.next_rip  = vmcb02->control.next_rip;
 
+	nested_svm_update_vmcb12_insn_bytes(vcpu, vmcb12, vmcb02);
+
 	if (nested_vmcb12_has_lbrv(vcpu))
 		svm_copy_lbrs(&vmcb12->save, &vmcb02->save);
 
Signature · 1 line
2.43.7
author
#11 lore raw

PATCH v3 5/9 KVM: x86: Track emulator-originated nested #PF VM-Exits

In reply to Tina Zhang

Add has_emulator_context to kvm_queued_exception to record whether a queued exception VM-Exit originated from the current emulator exception. This provenance allows nested virtualization code to determine whether the emulator decode state can be used for a nested #PF VM-Exit.

Explicit provenance is needed because a matching emulator RIP does not prove that the fetch cache describes the faulting instruction. For example, completing pending emulated I/O can advance RIP while leaving the fetch cache populated with the completed instruction.

Determine provenance in kvm_inject_page_fault() and carry it through exception queueing so that it is stored as part of the nested VM-Exit event. Userspace-injected and other unrelated exceptions leave the flag clear.

---
 arch/x86/include/asm/kvm_host.h |  1 +
 arch/x86/kvm/x86.c              | 33 ++++++++++++++++++++++-----------
 2 files changed, 23 insertions(+), 11 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
index 7a258831616f..8466a17cd046 100644
@@ -688,6 +688,7 @@ struct kvm_queued_exception {
 	bool pending;
 	bool injected;
 	bool has_error_code;
+	bool has_emulator_context;
 	u8 vector;
 	u32 error_code;
 	unsigned long payload;
arch/x86/kvm/x86.c +22 -11
index 0f1a829032c0..e7aff420d6c6 100644
@@ -444,7 +444,8 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_deliver_exception_payload);
 
 static void kvm_queue_exception_vmexit(struct kvm_vcpu *vcpu, unsigned int vector,
 				       bool has_error_code, u32 error_code,
-				       bool has_payload, unsigned long payload)
+				       bool has_payload, unsigned long payload,
+				       bool has_emulator_context)
 {
 	struct kvm_queued_exception *ex = &vcpu->arch.exception_vmexit;
 
@@ -452,6 +453,7 @@ static void kvm_queue_exception_vmexit(struct kvm_vcpu *vcpu, unsigned int vecto
 	ex->injected = false;
 	ex->pending = true;
 	ex->has_error_code = has_error_code;
+	ex->has_emulator_context = has_emulator_context;
 	ex->error_code = error_code;
 	ex->has_payload = has_payload;
 	ex->payload = payload;
@@ -459,7 +461,8 @@ static void kvm_queue_exception_vmexit(struct kvm_vcpu *vcpu, unsigned int vecto
 
 static void kvm_multiple_exception(struct kvm_vcpu *vcpu, unsigned int nr,
 				   bool has_error, u32 error_code,
-				   bool has_payload, unsigned long payload)
+				   bool has_payload, unsigned long payload,
+				   bool has_emulator_context)
 {
 	u32 prev_nr;
 	int class1, class2;
@@ -473,7 +476,8 @@ static void kvm_multiple_exception(struct kvm_vcpu *vcpu, unsigned int nr,
 	if (is_guest_mode(vcpu) &&
 	    kvm_nested_call(is_exception_vmexit)(vcpu, nr, error_code)) {
 		kvm_queue_exception_vmexit(vcpu, nr, has_error, error_code,
-					   has_payload, payload);
+					   has_payload, payload,
+					   has_emulator_context);
 		return;
 	}
 
@@ -519,7 +523,7 @@ static void kvm_multiple_exception(struct kvm_vcpu *vcpu, unsigned int nr,
 
 void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
 {
-	kvm_multiple_exception(vcpu, nr, false, 0, false, 0);
+	kvm_multiple_exception(vcpu, nr, false, 0, false, 0, false);
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_queue_exception);
 
@@ -527,14 +531,16 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_queue_exception);
 void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr,
 			   unsigned long payload)
 {
-	kvm_multiple_exception(vcpu, nr, false, 0, true, payload);
+	kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false);
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_queue_exception_p);
 
 static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr,
-				    u32 error_code, unsigned long payload)
+				    u32 error_code, unsigned long payload,
+				    bool has_emulator_context)
 {
-	kvm_multiple_exception(vcpu, nr, true, error_code, true, payload);
+	kvm_multiple_exception(vcpu, nr, true, error_code, true, payload,
+			       has_emulator_context);
 }
 
 void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned int nr,
@@ -580,6 +586,9 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_complete_insn_gp);
 void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault,
 			   bool from_hardware)
 {
+	struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
+	bool has_emulator_context = ctxt && fault == &ctxt->exception;
+
 	++vcpu->stat.pf_guest;
 
 	/*
@@ -589,10 +598,12 @@ void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault,
 	if (is_guest_mode(vcpu) && fault->async_page_fault)
 		kvm_queue_exception_vmexit(vcpu, PF_VECTOR,
 					   true, fault->error_code,
-					   true, fault->address);
+					   true, fault->address,
+					   has_emulator_context);
 	else
 		kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code,
-					fault->address);
+					fault->address,
+					has_emulator_context);
 }
 
 void __kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu,
@@ -627,7 +638,7 @@ void kvm_inject_nmi(struct kvm_vcpu *vcpu)
 
 void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
 {
-	kvm_multiple_exception(vcpu, nr, true, error_code, false, 0);
+	kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false);
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_queue_exception_e);
 
@@ -8949,7 +8960,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 	    kvm_nested_call(is_exception_vmexit)(vcpu, ex->vector, ex->error_code)) {
 		kvm_queue_exception_vmexit(vcpu, ex->vector,
 					   ex->has_error_code, ex->error_code,
-					   ex->has_payload, ex->payload);
+					   ex->has_payload, ex->payload, false);
 		ex->injected = false;
 		ex->pending = false;
 	}
Signature · 1 line
2.43.7
author
#12 lore raw

PATCH v3 6/9 KVM: nSVM: Use emulator bytes for synthesized nested #NPF/#PF

In reply to Tina Zhang

SVM DecodeAssists provides instruction bytes for data-access #NPF and intercepted #PF exits. Hardware-reflected VM-Exits can use fresh VMCB02 bytes, but KVM-synthesized exits have no hardware byte state to propagate.

For a synthesized nested #NPF, use the emulator fetch cache only when the fault is the current emulator exception. For a synthesized intercepted #PF, require the queued exception VM-Exit to carry the same emulator provenance.

Store the bytes in a one-shot buffer that is consumed while constructing VMCB12 and cleared before the next nested run. This prevents unrelated nested VM-Exits from reusing stale emulator bytes.

---
 arch/x86/kvm/svm/nested.c | 54 ++++++++++++++++++++++++++++++++++++++-
 arch/x86/kvm/svm/svm.h    | 12 +++++++++
 2 files changed, 65 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/nested.c +53 -1
index 5ad744ea668d..cfb686c29bb4 100644
@@ -71,22 +71,65 @@ static bool nested_svm_vmexit_supports_insn_bytes(struct kvm_vcpu *vcpu,
 	return !(vmcb02->control.exit_info_1 & PFERR_FETCH_MASK);
 }
 
+static void nested_svm_clear_synthesized_insn_bytes(struct vcpu_svm *svm)
+{
+	svm->nested.synthesized_insn_bytes.prepared = false;
+	svm->nested.synthesized_insn_bytes.insn_len = 0;
+}
+
+static void nested_svm_prepare_synthesized_insn_bytes(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_svm *svm = to_svm(vcpu);
+	struct nested_svm_insn_bytes *insn_bytes =
+		&svm->nested.synthesized_insn_bytes;
+	struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
+
+	static_assert(sizeof(insn_bytes->insn_bytes) >=
+		      sizeof(ctxt->fetch.data));
+
+	nested_svm_clear_synthesized_insn_bytes(svm);
+
+	if (!guest_cpu_cap_has(vcpu, X86_FEATURE_DECODEASSISTS))
+		return;
+
+	if (!ctxt || ctxt->eip != kvm_rip_read(vcpu) ||
+	    ctxt->fetch.end < ctxt->fetch.data ||
+	    ctxt->fetch.end > ctxt->fetch.data + sizeof(ctxt->fetch.data))
+		return;
+
+	insn_bytes->insn_len = ctxt->fetch.end - ctxt->fetch.data;
+	memcpy(insn_bytes->insn_bytes, ctxt->fetch.data,
+	       insn_bytes->insn_len);
+	insn_bytes->prepared = true;
+}
+
 static void nested_svm_update_vmcb12_insn_bytes(struct kvm_vcpu *vcpu,
 						struct vmcb *vmcb12,
 						struct vmcb *vmcb02)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
+	struct nested_svm_insn_bytes *insn_bytes =
+		&svm->nested.synthesized_insn_bytes;
 
 	nested_svm_clear_insn_bytes(vmcb12);
 
 	if (!nested_svm_vmexit_supports_insn_bytes(vcpu, vmcb02))
 		goto out;
 
-	if (svm->nested.vmcb02_insn_bytes_fresh)
+	if (svm->nested.vmcb02_insn_bytes_fresh) {
 		nested_svm_copy_insn_bytes(vmcb12, vmcb02);
+		goto out;
+	}
+
+	if (insn_bytes->prepared) {
+		vmcb12->control.insn_len = insn_bytes->insn_len;
+		memcpy(vmcb12->control.insn_bytes, insn_bytes->insn_bytes,
+		       vmcb12->control.insn_len);
+	}
 
 out:
 	svm->nested.vmcb02_insn_bytes_fresh = false;
+	nested_svm_clear_synthesized_insn_bytes(svm);
 }
 
 static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
@@ -95,6 +138,8 @@ static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
 	struct vmcb *vmcb = svm->vmcb;
+	struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
+	bool from_emulation = ctxt && fault == &ctxt->exception;
 	u64 fault_stage;
 
 	/*
@@ -123,6 +168,8 @@ static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
 	vmcb->control.exit_info_2 = fault->address;
 
 	svm->nested.vmcb02_insn_bytes_fresh = from_hardware;
+	if (from_emulation && !(fault->error_code & PFERR_FETCH_MASK))
+		nested_svm_prepare_synthesized_insn_bytes(vcpu);
 	nested_svm_vmexit(svm);
 }
 
@@ -927,6 +974,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
 	 */
 	nested_svm_clear_insn_bytes(vmcb02);
 	svm->nested.vmcb02_insn_bytes_fresh = false;
+	nested_svm_clear_synthesized_insn_bytes(svm);
 
 	if (guest_cpu_cap_has(vcpu, X86_FEATURE_VGIF) &&
 	    (vmcb12_ctrl->int_ctl & V_GIF_ENABLE_MASK))
@@ -1764,6 +1812,10 @@ static void nested_svm_inject_exception_vmexit(struct kvm_vcpu *vcpu)
 			vmcb->control.exit_info_2 = ex->payload;
 		else
 			vmcb->control.exit_info_2 = vcpu->arch.cr2;
+
+		if (ex->has_emulator_context &&
+		    (!ex->has_error_code || !(ex->error_code & PFERR_FETCH_MASK)))
+			nested_svm_prepare_synthesized_insn_bytes(vcpu);
 	} else if (ex->vector == DB_VECTOR) {
 		/* See kvm_check_and_inject_events().  */
 		kvm_deliver_exception_payload(vcpu, ex);
arch/x86/kvm/svm/svm.h +12 -0
index 96d2471a03c8..cb7287d82107 100644
@@ -209,6 +209,12 @@ struct vmcb_ctrl_area_cached {
 	};
 };
 
+struct nested_svm_insn_bytes {
+	bool prepared;
+	u8 insn_len;
+	u8 insn_bytes[X86_MAX_INSTRUCTION_LENGTH];
+};
+
 struct svm_nested_state {
 	struct kvm_vmcb_info vmcb02;
 	u64 hsave_msr;
@@ -248,6 +254,12 @@ struct svm_nested_state {
 	 * hardware VM-Exit currently being reflected to L1.
 	 */
 	bool vmcb02_insn_bytes_fresh;
+
+	/*
+	 * Emulator fetch bytes captured for a synthesized nested #NPF/#PF.  The
+	 * buffer is consumed and cleared when constructing VMCB12.
+	 */
+	struct nested_svm_insn_bytes synthesized_insn_bytes;
 };
 
 struct vcpu_sev_es_state {
Signature · 1 line
2.43.7
author
#13 lore raw

PATCH v3 7/9 KVM: nSVM: Fetch missing DecodeAssist bytes for synthesized #NPF/#PF

In reply to Tina Zhang

The emulator fetch cache is not guaranteed to contain the full architected 15-byte DecodeAssist window, e.g. it may only contain the bytes needed to decode the instruction.

Keep preparation of synthesized state limited to capturing a matching emulator fetch cache. When constructing VMCB12, copy those bytes and fetch any missing tail through L2 guest page tables, stopping at a translation fault, read failure, non-canonical address, or CS limit overrun. If no matching emulator bytes are available, fetch the full window from L2 RIP, e.g. for a #PF injected by userspace.

For a non-64-bit L2, truncate each incremented linear address to 32 bits so that a fetch whose CS.base makes it cross the 4GB boundary wraps as required.

Do not perform tail or fallback reads for SEV guests. KVM cannot read plaintext instruction bytes from encrypted guest memory, and the existing SEV emulation path treats missing hardware DecodeAssist bytes as unavailable instead of decoding guest memory. For nested SEV, report only matching emulator bytes that are already available, potentially a zero instruction-byte count.

---
 arch/x86/kvm/svm/nested.c | 63 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
arch/x86/kvm/svm/nested.c +63 -0
index cfb686c29bb4..cf2b6bb8126f 100644
@@ -77,6 +77,54 @@ static void nested_svm_clear_synthesized_insn_bytes(struct vcpu_svm *svm)
 	svm->nested.synthesized_insn_bytes.insn_len = 0;
 }
 
+static u8 nested_svm_fetch_insn_bytes(struct kvm_vcpu *vcpu, u8 *bytes,
+				      u8 count, u8 max_bytes)
+{
+	struct kvm_pagewalk *gva_walk = &vcpu->arch.gva_walk;
+	u64 access = PFERR_FETCH_MASK;
+	gva_t rip = kvm_get_linear_rip(vcpu);
+	struct x86_exception e;
+
+	if (kvm_x86_call(get_cpl)(vcpu) == 3)
+		access |= PFERR_USER_MASK;
+
+	if (!is_64_bit_mode(vcpu)) {
+		u32 eip = kvm_rip_read(vcpu);
+		u32 limit = to_svm(vcpu)->vmcb->save.cs.limit;
+
+		if (eip > limit)
+			return 0;
+		max_bytes = min_t(u64, max_bytes, (u64)limit - eip + 1);
+	}
+
+	count = min(count, max_bytes);
+
+	while (count < max_bytes) {
+		gva_t addr = rip + count;
+		unsigned int chunk;
+		gpa_t gpa;
+
+		if (!is_64_bit_mode(vcpu))
+			addr = (u32)addr;
+		else if (is_noncanonical_address(addr, vcpu, 0))
+			break;
+
+		chunk = min_t(unsigned int, max_bytes - count,
+			      PAGE_SIZE - offset_in_page(addr));
+		gpa = gva_walk->gva_to_gpa(vcpu, gva_walk, addr, access, &e);
+
+		if (gpa == INVALID_GPA ||
+		    kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(gpa),
+					     bytes + count,
+					     offset_in_page(gpa), chunk))
+			break;
+
+		count += chunk;
+	}
+
+	return count;
+}
+
 static void nested_svm_prepare_synthesized_insn_bytes(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
@@ -110,6 +158,7 @@ static void nested_svm_update_vmcb12_insn_bytes(struct kvm_vcpu *vcpu,
 	struct vcpu_svm *svm = to_svm(vcpu);
 	struct nested_svm_insn_bytes *insn_bytes =
 		&svm->nested.synthesized_insn_bytes;
+	const u8 max_bytes = sizeof(vmcb12->control.insn_bytes);
 
 	nested_svm_clear_insn_bytes(vmcb12);
 
@@ -125,8 +174,22 @@ static void nested_svm_update_vmcb12_insn_bytes(struct kvm_vcpu *vcpu,
 		vmcb12->control.insn_len = insn_bytes->insn_len;
 		memcpy(vmcb12->control.insn_bytes, insn_bytes->insn_bytes,
 		       vmcb12->control.insn_len);
+
+		if (!is_sev_guest(vcpu))
+			vmcb12->control.insn_len =
+				nested_svm_fetch_insn_bytes(vcpu,
+							    vmcb12->control.insn_bytes,
+							    vmcb12->control.insn_len,
+							    max_bytes);
+		goto out;
 	}
 
+	if (!is_sev_guest(vcpu))
+		vmcb12->control.insn_len =
+			nested_svm_fetch_insn_bytes(vcpu,
+						    vmcb12->control.insn_bytes,
+						    0, max_bytes);
+
 out:
 	svm->nested.vmcb02_insn_bytes_fresh = false;
 	nested_svm_clear_synthesized_insn_bytes(svm);
Signature · 1 line
2.43.7
#14 lore raw

Re: [PATCH v3 7/9] KVM: nSVM: Fetch missing DecodeAssist bytes for synthesized #NPF/#PF

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Already-prepared instruction bytes are discarded if the instruction pointer exceeds the CS limit…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Already-prepared instruction bytes are discarded if the instruction pointer exceeds the CS limit (or due to residual upper bits in 16-bit mode). --

--- Patch [7]: [PATCH v3 7/9] KVM: nSVM: Fetch missing DecodeAssist bytes for synthesized #NPF/#PF --- commit 96d13188ab4298d9414e0345853cc8950d39abb9
Author: Tina Zhang <[email protected]>

KVM: nSVM: Fetch missing DecodeAssist bytes for synthesized #NPF/#PF

This commit limits the preparation of synthesized state to matching the emulator fetch cache and fetches any missing tail bytes through L2 guest page tables. If the emulator cache is empty, it attempts to fetch the full window from the L2 RIP, avoiding fetches for SEV guests.

Quoted 4 lines
arch/x86/kvm/svm/nested.c +0 -0
index cfb686c29bb47..cf2b6bb8126ff 100644

[ ... ]

Quoted 7 lines
arch/x86/kvm/svm/nested.c +7 -0
+static u8 nested_svm_fetch_insn_bytes(struct kvm_vcpu *vcpu, u8 *bytes,
+				      u8 count, u8 max_bytes)
+{
+	struct kvm_pagewalk *gva_walk = &vcpu->arch.gva_walk;
+	u64 access = PFERR_FETCH_MASK;
+	gva_t rip = kvm_get_linear_rip(vcpu);
+	struct x86_exception e;

arch/x86/kvm/svm/nested.c static void nested_svm_clear_synthesized_insn_bytes(struct vcpu_svm *svm) · line 96

Quoted 10 lines
arch/x86/kvm/svm/nested.c +10 -0
+
+	if (kvm_x86_call(get_cpl)(vcpu) == 3)
+		access |= PFERR_USER_MASK;
+
+	if (!is_64_bit_mode(vcpu)) {
+		u32 eip = kvm_rip_read(vcpu);
+		u32 limit = to_svm(vcpu)->vmcb->save.cs.limit;
+
+		if (eip > limit)
+			return 0;

[Severity: Medium]
Should this return count instead of 0?

If an L2 guest in 16-bit or 32-bit mode causes a nested page fault where eip > limit evaluates to true (for example, in 16-bit mode if kvm_rip_read() retains residual upper 16-bits), returning 0 here causes the caller to overwrite vmcb12->control.insn_len with 0.

This would inadvertently discard any already-prepared valid instruction bytes that were passed in via the count parameter, forcing the L1 hypervisor to manually read the instruction from L2 memory and defeating the DecodeAssist optimization.

Quoted 6 lines
arch/x86/kvm/svm/nested.c +6 -0
+		max_bytes = min_t(u64, max_bytes, (u64)limit - eip + 1);
+	}
+
+	count = min(count, max_bytes);
+
+	while (count < max_bytes) {

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
author
#15 lore raw

Re: [PATCH v3 7/9] KVM: nSVM: Fetch missing DecodeAssist bytes for synthesized #NPF/#PF

Quoted 48 lines · On 7/30/2026 8:31 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Already-prepared instruction bytes are discarded if the instruction pointer exceeds the CS limit (or due to residual upper bits in 16-bit mode). --

--- Patch [7]: [PATCH v3 7/9] KVM: nSVM: Fetch missing DecodeAssist bytes for synthesized #NPF/#PF --- commit 96d13188ab4298d9414e0345853cc8950d39abb9
Author: Tina Zhang <[email protected]>

KVM: nSVM: Fetch missing DecodeAssist bytes for synthesized #NPF/#PF

This commit limits the preparation of synthesized state to matching the emulator fetch cache and fetches any missing tail bytes through L2 guest page tables. If the emulator cache is empty, it attempts to fetch the full window from the L2 RIP, avoiding fetches for SEV guests.

Quoted 4 lines
arch/x86/kvm/svm/nested.c +0 -0
index cfb686c29bb47..cf2b6bb8126ff 100644

[ ... ]

Quoted 7 lines
arch/x86/kvm/svm/nested.c +7 -0
+static u8 nested_svm_fetch_insn_bytes(struct kvm_vcpu *vcpu, u8 *bytes,
+				      u8 count, u8 max_bytes)
+{
+	struct kvm_pagewalk *gva_walk = &vcpu->arch.gva_walk;
+	u64 access = PFERR_FETCH_MASK;
+	gva_t rip = kvm_get_linear_rip(vcpu);
+	struct x86_exception e;

arch/x86/kvm/svm/nested.c static void nested_svm_clear_synthesized_insn_bytes(struct vcpu_svm *svm) · line 96

Quoted 10 lines
arch/x86/kvm/svm/nested.c +10 -0
+
+	if (kvm_x86_call(get_cpl)(vcpu) == 3)
+		access |= PFERR_USER_MASK;
+
+	if (!is_64_bit_mode(vcpu)) {
+		u32 eip = kvm_rip_read(vcpu);
+		u32 limit = to_svm(vcpu)->vmcb->save.cs.limit;
+
+		if (eip > limit)
+			return 0;

[Severity: Medium]
Should this return count instead of 0?

If an L2 guest in 16-bit or 32-bit mode causes a nested page fault where eip > limit evaluates to true (for example, in 16-bit mode if kvm_rip_read() retains residual upper 16-bits), returning 0 here causes the caller to overwrite vmcb12->control.insn_len with 0.

Quoted 4 lines

This would inadvertently discard any already-prepared valid instruction bytes that were passed in via the count parameter, forcing the L1 hypervisor to manually read the instruction from L2 memory and defeating the DecodeAssist optimization.

Returning zero is intentional. If the starting EIP is already beyond the CS limit, no instruction byte is architecturally fetchable from CS:EIP, and the APM specifies an instruction length of zero in that case.

A matching emulator fetch cache should not normally contain bytes for this condition. The emulator fetch path uses __linearize() with X86EMUL_F_FETCH and checks the same CS limit before populating its cache.

Also, a 16-bit code segment does not imply that EIP should be unconditionally truncated to 16 bits here. KVM's emulator checks the full non-64-bit EIP against the segment limit, which is consistent with this code.

Thanks,
Tina

Quoted 10 lines
Quoted 7 lines
arch/x86/kvm/svm/nested.c +6 -0
+		max_bytes = min_t(u64, max_bytes, (u64)limit - eip + 1);
+	}
+
+	count = min(count, max_bytes);
+
+	while (count < max_bytes) {

[ ... ]

author
#16 lore raw

PATCH v3 8/9 KVM: nSVM: Advertise DecodeAssists to L1

In reply to Tina Zhang

Advertise DecodeAssists to L1 now that KVM virtualizes the guest-visible DecodeAssist state in VMCB12: EXITINFO1 decode data for MOV CR/DR, INTn, and INVLPG exits, plus instruction bytes for nested page faults and intercepted #PF exits. INVLPGA's linear address remains available directly from the saved guest rAX, as required by the APM.

Expose the feature only when supported by hardware, as KVM still relies on hardware DecodeAssists for VM-Exits that are reflected directly from L2.

With DecodeAssists exposed, QEMU configurations that require the feature (e.g. "-cpu ...,+decodeassists,...,enforce") are no longer rejected.

---
 arch/x86/kvm/svm/svm.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kvm/svm/svm.c +2 -0
index 6e2ceb700567..4e01a6ede424 100644
@@ -5588,6 +5588,8 @@ static __init void svm_set_cpu_caps(void)
 		 */
 		kvm_cpu_cap_set(X86_FEATURE_FLUSHBYASID);
 
+		kvm_cpu_cap_check_and_set(X86_FEATURE_DECODEASSISTS);
+
 		if (nrips)
 			kvm_cpu_cap_set(X86_FEATURE_NRIPS);
 
Signature · 1 line
2.43.7
#17 lore raw

Re: [PATCH v3 8/9] KVM: nSVM: Advertise DecodeAssists to L1

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Missing VENDOR_F(DECODEASSISTS) initialization in CPUID 0x8000000A leaf definition. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Missing VENDOR_F(DECODEASSISTS) initialization in CPUID 0x8000000A leaf definition. --

--- Patch [8]: [PATCH v3 8/9] KVM: nSVM: Advertise DecodeAssists to L1 --- commit 2694559bd05c1133d7f21075f9c95dc19418651a
Author: Tina Zhang <[email protected]>

KVM: nSVM: Advertise DecodeAssists to L1

This patch advertises DecodeAssists to L1 guests by checking and setting the X86_FEATURE_DECODEASSISTS capability when supported by hardware. This enables QEMU configurations that enforce the decodeassists feature.

arch/x86/kvm/svm/svm.c static __init void svm_set_cpu_caps(void) · line 5591

Quoted 9 lines
arch/x86/kvm/svm/svm.c +1 -0
index 6e2ceb700567c..4e01a6ede4246 100644
@@ -5588,6 +5588,8 @@ static __init void svm_set_cpu_caps(void)
 		 */
 		kvm_cpu_cap_set(X86_FEATURE_FLUSHBYASID);
 
+		kvm_cpu_cap_check_and_set(X86_FEATURE_DECODEASSISTS);

[Severity: Low]
Does this feature also need to be initialized in arch/x86/kvm/cpuid.c?

Currently, kvm_cpu_cap_check_and_set() is used here to enable it, but VENDOR_F(DECODEASSISTS) appears to be missing from the CPUID_8000_000A_EDX capabilities array in cpuid.c:

arch/x86/kvm/cpuid.c:kvm_cpu_cap_init() {
    /* All SVM features required additional vendor module enabling. */
    kvm_cpu_cap_init(CPUID_8000_000A_EDX,
        VENDOR_F(NPT),
        VENDOR_F(VMCBCLEAN),
        ...
}

Without VENDOR_F(DECODEASSISTS), this skips KVM's standard compile-time validation and raw CPUID masking logic. Should it be added to the vendor feature list as well?

Quoted 4 lines
arch/x86/kvm/svm/svm.c +1 -0
+
 		if (nrips)
 			kvm_cpu_cap_set(X86_FEATURE_NRIPS);
 
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
author
#18 lore raw

Re: [PATCH v3 8/9] KVM: nSVM: Advertise DecodeAssists to L1

Quoted 39 lines · On 7/30/2026 8:33 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Missing VENDOR_F(DECODEASSISTS) initialization in CPUID 0x8000000A leaf definition. --

--- Patch [8]: [PATCH v3 8/9] KVM: nSVM: Advertise DecodeAssists to L1 --- commit 2694559bd05c1133d7f21075f9c95dc19418651a
Author: Tina Zhang <[email protected]>

KVM: nSVM: Advertise DecodeAssists to L1

This patch advertises DecodeAssists to L1 guests by checking and setting the X86_FEATURE_DECODEASSISTS capability when supported by hardware. This enables QEMU configurations that enforce the decodeassists feature.

arch/x86/kvm/svm/svm.c static __init void svm_set_cpu_caps(void) · line 5591

Quoted 9 lines
arch/x86/kvm/svm/svm.c +1 -0
index 6e2ceb700567c..4e01a6ede4246 100644
@@ -5588,6 +5588,8 @@ static __init void svm_set_cpu_caps(void)
  		 */
  		kvm_cpu_cap_set(X86_FEATURE_FLUSHBYASID);
 
+		kvm_cpu_cap_check_and_set(X86_FEATURE_DECODEASSISTS);

[Severity: Low]
Does this feature also need to be initialized in arch/x86/kvm/cpuid.c?

Currently, kvm_cpu_cap_check_and_set() is used here to enable it, but VENDOR_F(DECODEASSISTS) appears to be missing from the CPUID_8000_000A_EDX capabilities array in cpuid.c:

arch/x86/kvm/cpuid.c:kvm_cpu_cap_init() {
     /* All SVM features required additional vendor module enabling. */
     kvm_cpu_cap_init(CPUID_8000_000A_EDX,
         VENDOR_F(NPT),
         VENDOR_F(VMCBCLEAN),
         ...
}
Quoted 3 lines

Without VENDOR_F(DECODEASSISTS), this skips KVM's standard compile-time validation and raw CPUID masking logic. Should it be added to the vendor feature list as well?

Yes, DecodeAssists should also be declared with VENDOR_F(DECODEASSISTS). I'll add it in the next version.

Thanks,
Tina

Quoted 6 lines
Quoted 5 lines
arch/x86/kvm/svm/svm.c +1 -0
+
  		if (nrips)
  			kvm_cpu_cap_set(X86_FEATURE_NRIPS);
 
author
#19 lore raw

PATCH v3 9/9 KVM: selftests: Add nested SVM DecodeAssists test

In reply to Tina Zhang

Add a nested SVM selftest for DecodeAssists. Verify that KVM exposes the feature to L1 and provides architectural exit state for MOV CR/DR, CLTS, LMSW, SMSW, INTn, INVLPG, and INVLPGA intercepts.

For data #NPF and intercepted #PF exits, cover instruction bytes from hardware, the emulator fetch cache, and on-demand fetching from L2 RIP. Verify that instruction-fetch #PF reports no bytes, that fetching stops at an unreadable page or non-canonical address, and that 32-bit linear addresses wrap correctly.

Add regressions for a synthesized #NPF that follows a hardware #NPF in the same emulated instruction, and for a userspace-injected #PF while an emulated MMIO read is awaiting completion.

The synthesized OUTSB #NPF and userspace-injected #PF paths run by default. The remaining synthesized paths are covered when kvm.force_emulation_prefix=1 is enabled.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/include/x86/processor.h     |   1 +
 .../kvm/x86/svm_nested_decode_assists_test.c  | 791 ++++++++++++++++++
 3 files changed, 793 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 88c6c8046dde..e31e3a2412ae 100644
@@ -116,6 +116,7 @@ TEST_GEN_PROGS_x86 += x86/vmx_preemption_timer_test
 TEST_GEN_PROGS_x86 += x86/svm_vmcall_test
 TEST_GEN_PROGS_x86 += x86/svm_int_ctl_test
 TEST_GEN_PROGS_x86 += x86/svm_nested_clear_efer_svme
+TEST_GEN_PROGS_x86 += x86/svm_nested_decode_assists_test
 TEST_GEN_PROGS_x86 += x86/svm_nested_shutdown_test
 TEST_GEN_PROGS_x86 += x86/svm_nested_soft_inject_test
 TEST_GEN_PROGS_x86 += x86/svm_nested_vmcb12_gpa
tools/testing/selftests/kvm/include/x86/processor.h +1 -0
index b161174ece45..42e009af525e 100644
@@ -219,6 +219,7 @@ struct kvm_x86_cpu_feature {
 #define	X86_FEATURE_LBRV		KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 1)
 #define	X86_FEATURE_NRIPS		KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 3)
 #define X86_FEATURE_TSCRATEMSR          KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 4)
+#define X86_FEATURE_DECODEASSISTS       KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 7)
 #define X86_FEATURE_PAUSEFILTER         KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 10)
 #define X86_FEATURE_PFTHRESHOLD         KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 12)
 #define	X86_FEATURE_V_VMSAVE_VMLOAD	KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 15)
tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c new file +791 -0

791 lines not on this page · raw message

Signature · 1 line
2.43.7
#20 lore raw

Re: [PATCH v3 9/9] KVM: selftests: Add nested SVM DecodeAssists test

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The selftest `test_synthesized_npf_linear_wrap` incorrectly asserts that 64-bit linear addresses…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The selftest test_synthesized_npf_linear_wrap incorrectly asserts that 64-bit linear addresses wrap at 4GB in compatibility mode, cementing a preexisting KVM emulator bug. --

--- Patch [9]: [PATCH v3 9/9] KVM: selftests: Add nested SVM DecodeAssists test --- commit 90a4009aecfc1596a0c559275e2cf280935e0a29
Author: Tina Zhang <[email protected]>

KVM: selftests: Add nested SVM DecodeAssists test

Add a nested SVM selftest for DecodeAssists. Verify that KVM exposes the feature to L1 and provides architectural exit state for various instructions and intercepts.

Quoted 4 lines
tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c new file +0 -0
new file mode 100644

[ ... ]

Quoted 4 lines
tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c new file +4 -0
+static void test_synthesized_npf_linear_wrap(struct svm_test_data *svm,
+					     gpa_t npf_gpa)
+{
+	struct vmcb *vmcb = svm->vmcb;

[ ... ]

tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c line 512

Quoted 7 lines
tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c new file +7 -0
+	prepare_l2_for_vmrun(svm, setup_eip);
+	run_guest(vmcb, svm->vmcb_gpa);
+	GUEST_ASSERT_EQ(vmcb->control.exit_code, SVM_EXIT_NPF);
+	GUEST_ASSERT_EQ(vmcb->control.exit_info_2, npf_gpa);
+	GUEST_ASSERT_EQ(vmcb->save.rip, outsb_eip);
+	GUEST_ASSERT_EQ(vmcb->control.insn_len,
+			sizeof(linear_wrap_insn_bytes));

[Severity: Medium]
Does this test enforce an architectural violation by requiring the KVM emulator to wrap 64-bit linear addresses to 32 bits?

By setting up L2 in compatibility mode (CS.L=0) with a non-zero segment base and an EIP that crosses the 4GB boundary, this asserts that KVM successfully fetches all 15 instruction bytes from GVA 0. This appears to cement a pre-existing KVM emulator bug rather than verifying correct hardware behavior (where the correct 64-bit linear address would be 0x100000000).

Quoted 7 lines
tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c new file +7 -0
+	GUEST_ASSERT(!memcmp(vmcb->control.insn_bytes,
+			     linear_wrap_insn_bytes,
+			     sizeof(linear_wrap_insn_bytes)));
+	vmcb->save.cs.attrib = cs_attrib;
+	vmcb->save.cs.base = cs_base;
+	vmcb->save.cs.limit = cs_limit;
+}

[ ... ]

Quoted 2 lines
tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c new file +2 -0
+static void prepare_linear_wrap_outsb_code(struct kvm_vm *vm)
+{

[ ... ]

tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c line 683

Quoted 10 lines
tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c new file +10 -0
+	memcpy(&high_page[LINEAR_WRAP_OUTSB_OFFSET],
+	       linear_wrap_insn_bytes,
+	       PAGE_SIZE - LINEAR_WRAP_OUTSB_OFFSET);
+	memcpy(low_page,
+	       &linear_wrap_insn_bytes[PAGE_SIZE - LINEAR_WRAP_OUTSB_OFFSET],
+	       sizeof(linear_wrap_insn_bytes) -
+	       (PAGE_SIZE - LINEAR_WRAP_OUTSB_OFFSET));
+
+	virt_map(vm, LINEAR_WRAP_CODE_GVA, high_gpa, 1);
+	virt_map(vm, 0, low_gpa, 1);

[Severity: Medium]
Is mapping GVA 0 here anticipating and validating the emulator's incorrect 32-bit truncation of the linear address?

If the emulator is ever fixed to comply with the AMD APM and use the full 64-bit address, wouldn't this test begin failing since it maps the fallback wrapped address?

Quoted 1 line

+}

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9