Skip to contentSkip to conversation

KVM: nSVM: Enable DecodeAssists for nested guests

PATCH In review Tina Zhang 4 participants 16 messages Sep 23 – Oct 13

Subsystems (4)
lore
Share & tools
lore

Tina Zhang

author
#0 lore raw

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

Virtualize both parts of DecodeAssists for nested SVM. For emulated MOV CR/DR, INTn, INVLPG, and related intercepts, populate EXITINFO1 as specified by the architecture. Preserve MOV-to-CR0 decode information for selective CR0 write intercepts. INVLPGA's address remains in saved guest rAX. Leave EXITINFO1 unchanged when DecodeAssists is not exposed.

For data #NPF and intercepted data #PF exits, propagate current hardware instruction bytes when available. For an emulator-generated #NPF, keep the bytes used to decode the instruction and fetch any missing tail. Queued #PF exits, including userspace-injected exceptions, use an on-demand fetch from the current L2 CS:RIP. Instruction-fetch faults report zero bytes, and SEV guests do not use the memory-fetch fallback.

The selftest checks hardware and forced-emulation instruction intercepts against the same expected values, including selective CR0 writes. It also covers hardware and synthesized instruction bytes, truncated fetches, cache preservation after instruction memory changes during MMIO, and userspace-injected #PF after MMIO completion.

The complete selftest passed with kvm.force_emulation_prefix both disabled and enabled in an isolated VM running a kernel built from this series on the base commit below. SEV and live migration have not been tested.

Changes since v7:
- Rebase onto kvm-x86/next at the base commit listed below.
- Revise the INVLPG comment as suggested by Jim.

Previous versions (including earlier changelogs):
v7:
https://lore.kernel.org/r/[email protected]
v6:
https://lore.kernel.org/r/[email protected]
v5:
https://lore.kernel.org/r/[email protected]
v4:
https://lore.kernel.org/r/[email protected]
v3:
https://lore.kernel.org/r/[email protected]
v2:
https://lore.kernel.org/r/[email protected]
v1:
https://lore.kernel.org/r/[email protected]

Tina Zhang (8):
KVM: x86: Provide INVLPG linear address to intercept handlers
KVM: nSVM: Synthesize DecodeAssists EXITINFO for emulated intercepts
KVM: nSVM: Track valid hardware DecodeAssist bytes
KVM: nSVM: Propagate hardware DecodeAssist bytes to VMCB12
KVM: nSVM: Fetch DecodeAssist bytes for synthesized faults
KVM: nSVM: Use emulator bytes for synthesized nested #NPF
KVM: nSVM: Advertise DecodeAssists to L1
KVM: selftests: Add nested SVM DecodeAssists test

 arch/x86/kvm/cpuid.c                          |   1 +
 arch/x86/kvm/emulate.c                        |  45 ++
 arch/x86/kvm/kvm_emulate.h                    |   4 +
 arch/x86/kvm/svm/nested.c                     | 142 ++++-
 arch/x86/kvm/svm/svm.c                        |  29 +
 arch/x86/kvm/svm/svm.h                        |   3 +
 arch/x86/kvm/x86.c                            |  56 +-
 arch/x86/kvm/x86.h                            |   2 +
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/include/x86/processor.h     |   1 +
 .../kvm/x86/svm_nested_decode_assists_test.c  | 590 ++++++++++++++++++
 11 files changed, 862 insertions(+), 12 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c

base-commit: 30b5175943e709911702d8a9364145e911f57e3f

Signature · 1 line
2.43.7
author
#1 lore raw

PATCH v8 1/8 KVM: x86: Provide INVLPG linear address to intercept handlers

INVLPG's memory operand is decoded with NoAccess, and thus src_val does not contain the operand address. Intercept handlers therefore cannot construct exit state that reports the linear address from the existing x86_instruction_info fields.

Add get_invlpg_linear_addr() to compute the address through __linearize(), using the same flags as em_invlpg(), and pass the result through x86_instruction_info.

---
 arch/x86/kvm/emulate.c     | 23 +++++++++++++++++++++++
 arch/x86/kvm/kvm_emulate.h |  1 +
 2 files changed, 24 insertions(+)
arch/x86/kvm/emulate.c +23 -0
index c1b21282187f..299aeaca6cf2 100644
@@ -411,6 +411,9 @@ static int em_salc(struct x86_emulate_ctxt *ctxt)
 	_fault ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE; \
 })
 
+static u64 get_invlpg_linear_addr(struct x86_emulate_ctxt *ctxt,
+				  enum x86_intercept intercept);
+
 static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
 				    enum x86_intercept intercept,
 				    enum x86_intercept_stage stage)
@@ -428,6 +431,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,
+		.invlpg_linear_addr = get_invlpg_linear_addr(ctxt, intercept),
 		.rip	    = ctxt->eip,
 		.next_rip   = ctxt->_eip,
 	};
@@ -684,6 +688,25 @@ static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
 		return emulate_gp(ctxt, 0);
 }
 
+static u64 get_invlpg_linear_addr(struct x86_emulate_ctxt *ctxt,
+				  enum x86_intercept intercept)
+{
+	unsigned int max_size;
+	unsigned long linear = 0;
+
+	if (intercept != x86_intercept_invlpg)
+		return 0;
+
+	/*
+	 * Ignore faults, just as em_invlpg() does. Calling __linearize()
+	 * exactly as em_invlpg() does guarantees agreement with
+	 * em_invlpg() on the linear address.
+	 */
+	__linearize(ctxt, ctxt->src.addr.mem, &max_size, 1, ctxt->mode,
+		    &linear, X86EMUL_F_INVLPG);
+	return linear;
+}
+
 static int linearize(struct x86_emulate_ctxt *ctxt,
 		     struct segmented_address addr,
 		     unsigned size, bool write,
arch/x86/kvm/kvm_emulate.h +1 -0
index 3e375af15c03..d05558ef28ae 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 invlpg_linear_addr;	/* linear address, if INVLPG */
 	u64 rip;		/* rip of the instruction		*/
 	u64 next_rip;           /* rip following the instruction        */
 };
Signature · 1 line
2.43.7
Reviewed-by on v8 1/8
#2 lore raw

Re: [PATCH v8 1/8] KVM: x86: Provide INVLPG linear address to intercept handlers

Quoted 11 lines · Tina Zhang

INVLPG's memory operand is decoded with NoAccess, and thus src_val does not contain the operand address. Intercept handlers therefore cannot construct exit state that reports the linear address from the existing x86_instruction_info fields.

Add get_invlpg_linear_addr() to compute the address through __linearize(), using the same flags as em_invlpg(), and pass the result through x86_instruction_info.

author
#3 lore raw

PATCH v8 2/8 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 EXITINFO1 field when DecodeAssists is exposed to L1.

Provide the GPR number for MOV CR/DR, the interrupt vector for INTn, and the linear address for INVLPG. Preserve the MOV-to-CR0 decode information when a selective CR0 write intercept is triggered. Leave EXITINFO1 clear for CLTS, LMSW, SMSW, and INVLPGA; the INVLPGA address remains in guest rAX. Leave EXITINFO1 unchanged when DecodeAssists is not exposed, and leave unrelated intercepts unchanged.

---
 arch/x86/kvm/svm/svm.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
arch/x86/kvm/svm/svm.c +27 -0
index f5aa3d7d3a10..e83e4e5e7b93 100644
@@ -4837,6 +4837,7 @@ static int svm_check_intercept(struct kvm_vcpu *vcpu,
 	int vmexit, ret = X86EMUL_CONTINUE;
 	struct __x86_intercept icpt_info;
 	struct vmcb *vmcb = svm->vmcb;
+	bool decode_assists;
 
 	if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
 		goto out;
@@ -4846,14 +4847,26 @@ static int svm_check_intercept(struct kvm_vcpu *vcpu,
 	if (stage != icpt_info.stage)
 		goto out;
 
+	decode_assists = guest_cpu_cap_has(vcpu, X86_FEATURE_DECODEASSISTS);
+
 	switch (icpt_info.exit_code) {
 	case SVM_EXIT_READ_CR0:
 		if (info->intercept == x86_intercept_cr_read)
 			icpt_info.exit_code += info->modrm_reg;
+
+		if (decode_assists)
+			vmcb->control.exit_info_1 =
+				info->intercept == x86_intercept_cr_read ?
+				CR_VALID | (info->modrm_rm & SVM_EXITINFO_REG_MASK) : 0;
 		break;
 	case SVM_EXIT_WRITE_CR0: {
 		unsigned long cr0, val;
 
+		if (decode_assists)
+			vmcb->control.exit_info_1 =
+				info->intercept == x86_intercept_cr_write ?
+				CR_VALID | (info->modrm_rm & SVM_EXITINFO_REG_MASK) : 0;
+
 		/*
 		 * Adjust the exit code accordingly if a CR other than CR0 is
 		 * being written, and skip straight to the common handling as
@@ -4898,6 +4911,20 @@ static int svm_check_intercept(struct kvm_vcpu *vcpu,
 	case SVM_EXIT_READ_DR0:
 	case SVM_EXIT_WRITE_DR0:
 		icpt_info.exit_code += info->modrm_reg;
+		if (decode_assists)
+			vmcb->control.exit_info_1 = info->modrm_rm & SVM_EXITINFO_REG_MASK;
+		break;
+	case SVM_EXIT_SWINT:
+		if (decode_assists)
+			vmcb->control.exit_info_1 = info->src_val & 0xff;
+		break;
+	case SVM_EXIT_INVLPG:
+		if (decode_assists)
+			vmcb->control.exit_info_1 = info->invlpg_linear_addr;
+		break;
+	case SVM_EXIT_INVLPGA:
+		if (decode_assists)
+			vmcb->control.exit_info_1 = 0;
 		break;
 	case SVM_EXIT_MSR:
 		if (info->intercept == x86_intercept_wrmsr)
Signature · 1 line
2.43.7
author
#4 lore raw

PATCH v8 3/8 KVM: nSVM: Track valid hardware DecodeAssist bytes

In reply to Tina Zhang

VMCB02 instruction bytes are valid only for the hardware VM-Exit that populated them. Track whether VMCB02 contains instruction bytes for the data #PF or #NPF currently being reflected to L1 so that stale bytes are not copied to VMCB12.

Invalidate VMCB02's instruction-byte state when preparing VMCB02, normally in response to an emulated VMRUN. Mark the bytes as valid only when the current VMCB02 exit is a data #PF or #NPF. Opcode exits and instruction-fetch faults are excluded by inspecting the exit state.

A subsequent change will use this state when propagating hardware DecodeAssist instruction bytes to VMCB12.

---
 arch/x86/kvm/svm/nested.c | 38 +++++++++++++++++++++++++++++++++++++-
 arch/x86/kvm/svm/svm.h    |  3 +++
 2 files changed, 40 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/nested.c +37 -1
index 82c49a24dd75..a87b88d8eeac 100644
@@ -35,6 +35,35 @@
 
 #define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK
 
+static void nested_svm_invalidate_insn_bytes(struct vmcb *vmcb)
+{
+	vmcb->control.insn_len = 0;
+}
+
+static bool nested_svm_vmexit_supports_insn_bytes(const struct vmcb *vmcb)
+{
+	u64 exit_code = vmcb->control.exit_code;
+
+	if (exit_code != SVM_EXIT_NPF &&
+	    exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR)
+		return false;
+
+	return !(vmcb->control.exit_info_1 & PFERR_FETCH_MASK);
+}
+
+static void nested_svm_set_vmcb02_insn_bytes_valid(struct vcpu_svm *svm)
+{
+	svm->nested.vmcb02_insn_bytes_valid =
+		cpu_feature_enabled(X86_FEATURE_DECODEASSISTS) &&
+		nested_svm_vmexit_supports_insn_bytes(svm->nested.vmcb02.ptr);
+}
+
+static void nested_svm_invalidate_vmcb02_insn_bytes(struct vcpu_svm *svm)
+{
+	nested_svm_invalidate_insn_bytes(svm->nested.vmcb02.ptr);
+	svm->nested.vmcb02_insn_bytes_valid = false;
+}
+
 static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
 				       struct x86_exception *fault,
 				       bool from_hardware)
@@ -68,6 +97,10 @@ 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;
 
+	if (from_hardware)
+		nested_svm_set_vmcb02_insn_bytes_valid(svm);
+	else
+		nested_svm_invalidate_vmcb02_insn_bytes(svm);
 	nested_svm_vmexit(svm);
 }
 
@@ -873,6 +906,7 @@ 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.
 	 */
+	nested_svm_invalidate_vmcb02_insn_bytes(svm);
 
 	if (guest_cpu_cap_has(vcpu, X86_FEATURE_VGIF) &&
 	    (vmcb12_ctrl->int_ctl & V_GIF_ENABLE_MASK))
@@ -1653,8 +1687,10 @@ int nested_svm_exit_handled(struct vcpu_svm *svm)
 
 	vmexit = nested_svm_intercept(svm);
 
-	if (vmexit == NESTED_EXIT_DONE)
+	if (vmexit == NESTED_EXIT_DONE) {
+		nested_svm_set_vmcb02_insn_bytes_valid(svm);
 		nested_svm_vmexit(svm);
+	}
 
 	return vmexit;
 }
arch/x86/kvm/svm/svm.h +3 -0
index e958943b8162..1e12ef1105c0 100644
@@ -243,6 +243,9 @@ struct svm_nested_state {
 	 * on its side.
 	 */
 	bool force_msr_bitmap_recalc;
+
+	/* True if VMCB02 has instruction bytes for the current nested exit. */
+	bool vmcb02_insn_bytes_valid;
 };
 
 struct vcpu_sev_es_state {
Signature · 1 line
2.43.7
author
#5 lore raw

PATCH v8 4/8 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.

Leave the VMCB12 instruction-byte state untouched when DecodeAssists is not exposed. Otherwise, invalidate the state for instruction-fetch page faults, unrelated exits, and exits without fresh hardware bytes.

---
 arch/x86/kvm/svm/nested.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
arch/x86/kvm/svm/nested.c +37 -0
index a87b88d8eeac..e839f61211c9 100644
@@ -40,6 +40,20 @@ static void nested_svm_invalidate_insn_bytes(struct vmcb *vmcb)
 	vmcb->control.insn_len = 0;
 }
 
+static void nested_svm_copy_insn_bytes(struct vmcb *to,
+				       const struct vmcb *from)
+{
+	u8 insn_len = from->control.insn_len;
+
+	if (WARN_ON_ONCE(insn_len > sizeof(from->control.insn_bytes))) {
+		nested_svm_invalidate_insn_bytes(to);
+		return;
+	}
+
+	memcpy(to->control.insn_bytes, from->control.insn_bytes, insn_len);
+	to->control.insn_len = insn_len;
+}
+
 static bool nested_svm_vmexit_supports_insn_bytes(const struct vmcb *vmcb)
 {
 	u64 exit_code = vmcb->control.exit_code;
@@ -64,6 +78,27 @@ static void nested_svm_invalidate_vmcb02_insn_bytes(struct vcpu_svm *svm)
 	svm->nested.vmcb02_insn_bytes_valid = false;
 }
 
+static void nested_svm_update_vmcb12_insn_bytes(struct kvm_vcpu *vcpu,
+						struct vmcb *vmcb12,
+						const struct vmcb *vmcb02)
+{
+	struct vcpu_svm *svm = to_svm(vcpu);
+
+	if (!guest_cpu_cap_has(vcpu, X86_FEATURE_DECODEASSISTS))
+		goto out;
+
+	if (!nested_svm_vmexit_supports_insn_bytes(vmcb02) ||
+	    !svm->nested.vmcb02_insn_bytes_valid) {
+		nested_svm_invalidate_insn_bytes(vmcb12);
+		goto out;
+	}
+
+	nested_svm_copy_insn_bytes(vmcb12, vmcb02);
+
+out:
+	svm->nested.vmcb02_insn_bytes_valid = false;
+}
+
 static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
 				       struct x86_exception *fault,
 				       bool from_hardware)
@@ -1335,6 +1370,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
#6 lore raw

PATCH v8 5/8 KVM: nSVM: Fetch DecodeAssist bytes for synthesized faults

In reply to Tina Zhang

Hardware-reflected #NPF and #PF VM-Exits can propagate instruction bytes from VMCB02, but KVM-synthesized exits have no hardware-provided state for the current exit.

Extend kvm_read_guest_virt_helper() to optionally report the number of bytes read, including partial reads. Add kvm_fetch_guest_virt() to apply instruction-fetch permissions and return that count, reusing the generic helper's page-by-page read loop.

When a synthesized data #NPF or #PF has no valid VMCB02 instruction bytes, fetch up to 15 bytes from L2's RIP while constructing VMCB12. Preserve the bytes already read if a subsequent read cannot be completed.

For instruction fetches, truncate each linear address to 32 bits outside 64-bit mode and stop at a non-canonical address in 64-bit mode. Limit the fetch window at the code-segment limit outside 64-bit mode.

Do not attempt the fallback for SEV guests, as KVM cannot read encrypted guest memory directly.

---
 arch/x86/kvm/svm/nested.c | 41 +++++++++++++++++++++++++---
 arch/x86/kvm/x86.c        | 56 +++++++++++++++++++++++++++++++--------
 arch/x86/kvm/x86.h        |  2 ++
 3 files changed, 85 insertions(+), 14 deletions(-)
arch/x86/kvm/svm/nested.c +38 -3
index e839f61211c9..813b0fc663c6 100644
@@ -78,22 +78,57 @@ static void nested_svm_invalidate_vmcb02_insn_bytes(struct vcpu_svm *svm)
 	svm->nested.vmcb02_insn_bytes_valid = false;
 }
 
+static u8 nested_svm_get_insn_bytes_len(struct kvm_vcpu *vcpu, u8 max_bytes)
+{
+	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);
+	}
+
+	return max_bytes;
+}
+
+static u8 nested_svm_fetch_insn_bytes(struct kvm_vcpu *vcpu, u8 *bytes,
+				      u8 max_bytes)
+{
+	gva_t rip = kvm_get_linear_rip(vcpu);
+
+	max_bytes = nested_svm_get_insn_bytes_len(vcpu, max_bytes);
+
+	return kvm_fetch_guest_virt(vcpu, rip, bytes, max_bytes);
+}
+
 static void nested_svm_update_vmcb12_insn_bytes(struct kvm_vcpu *vcpu,
 						struct vmcb *vmcb12,
 						const struct vmcb *vmcb02)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
+	const u8 max_bytes = sizeof(vmcb12->control.insn_bytes);
 
 	if (!guest_cpu_cap_has(vcpu, X86_FEATURE_DECODEASSISTS))
 		goto out;
 
-	if (!nested_svm_vmexit_supports_insn_bytes(vmcb02) ||
-	    !svm->nested.vmcb02_insn_bytes_valid) {
+	if (!nested_svm_vmexit_supports_insn_bytes(vmcb02)) {
 		nested_svm_invalidate_insn_bytes(vmcb12);
 		goto out;
 	}
 
-	nested_svm_copy_insn_bytes(vmcb12, vmcb02);
+	if (svm->nested.vmcb02_insn_bytes_valid) {
+		nested_svm_copy_insn_bytes(vmcb12, vmcb02);
+		goto out;
+	}
+
+	if (!is_sev_guest(vcpu))
+		vmcb12->control.insn_len =
+			nested_svm_fetch_insn_bytes(vcpu,
+						    vmcb12->control.insn_bytes,
+						    max_bytes);
+	else
+		nested_svm_invalidate_insn_bytes(vmcb12);
 
 out:
 	svm->nested.vmcb02_insn_bytes_valid = false;
arch/x86/kvm/x86.c +45 -11

99 lines not on this page · raw message

arch/x86/kvm/x86.h +2 -0
index 1d9a66048b01..dbf0a4e6e9c1 100644
@@ -357,6 +357,8 @@ static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
 int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
 	gva_t addr, void *val, unsigned int bytes,
 	struct x86_exception *exception);
+unsigned int kvm_fetch_guest_virt(struct kvm_vcpu *vcpu, gva_t addr,
+				 void *val, unsigned int bytes);
 
 int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu,
 	gva_t addr, void *val, unsigned int bytes,
Signature · 1 line
2.43.7
Reviewed-by on v8 5/8
#7 lore raw

Re: [PATCH v8 5/8] KVM: nSVM: Fetch DecodeAssist bytes for synthesized faults

Quoted 117 lines · Tina Zhang

Hardware-reflected #NPF and #PF VM-Exits can propagate instruction bytes from VMCB02, but KVM-synthesized exits have no hardware-provided state for the current exit.

Extend kvm_read_guest_virt_helper() to optionally report the number of bytes read, including partial reads. Add kvm_fetch_guest_virt() to apply instruction-fetch permissions and return that count, reusing the generic helper's page-by-page read loop.

When a synthesized data #NPF or #PF has no valid VMCB02 instruction bytes, fetch up to 15 bytes from L2's RIP while constructing VMCB12. Preserve the bytes already read if a subsequent read cannot be completed.

For instruction fetches, truncate each linear address to 32 bits outside 64-bit mode and stop at a non-canonical address in 64-bit mode. Limit the fetch window at the code-segment limit outside 64-bit mode.

Do not attempt the fallback for SEV guests, as KVM cannot read encrypted guest memory directly.

---
 arch/x86/kvm/svm/nested.c | 41 +++++++++++++++++++++++++---
 arch/x86/kvm/x86.c        | 56 +++++++++++++++++++++++++++++++--------
 arch/x86/kvm/x86.h        |  2 ++
 3 files changed, 85 insertions(+), 14 deletions(-)
arch/x86/kvm/svm/nested.c +38 -3
index e839f61211c9..813b0fc663c6 100644
@@ -78,22 +78,57 @@ static void nested_svm_invalidate_vmcb02_insn_bytes(struct vcpu_svm *svm)
        svm->nested.vmcb02_insn_bytes_valid = false;
 }
 
+static u8 nested_svm_get_insn_bytes_len(struct kvm_vcpu *vcpu, u8 max_bytes)
+{
+       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);
+       }
+
+       return max_bytes;
+}
+
+static u8 nested_svm_fetch_insn_bytes(struct kvm_vcpu *vcpu, u8 *bytes,
+                                     u8 max_bytes)
+{
+       gva_t rip = kvm_get_linear_rip(vcpu);
+
+       max_bytes = nested_svm_get_insn_bytes_len(vcpu, max_bytes);
+
+       return kvm_fetch_guest_virt(vcpu, rip, bytes, max_bytes);
+}
+
 static void nested_svm_update_vmcb12_insn_bytes(struct kvm_vcpu *vcpu,
                                                struct vmcb *vmcb12,
                                                const struct vmcb *vmcb02)
 {
        struct vcpu_svm *svm = to_svm(vcpu);
+       const u8 max_bytes = sizeof(vmcb12->control.insn_bytes);
 
        if (!guest_cpu_cap_has(vcpu, X86_FEATURE_DECODEASSISTS))
                goto out;
 
-       if (!nested_svm_vmexit_supports_insn_bytes(vmcb02) ||
-           !svm->nested.vmcb02_insn_bytes_valid) {
+       if (!nested_svm_vmexit_supports_insn_bytes(vmcb02)) {
                nested_svm_invalidate_insn_bytes(vmcb12);
                goto out;
        }
 
-       nested_svm_copy_insn_bytes(vmcb12, vmcb02);
+       if (svm->nested.vmcb02_insn_bytes_valid) {
+               nested_svm_copy_insn_bytes(vmcb12, vmcb02);
+               goto out;
+       }
+
+       if (!is_sev_guest(vcpu))
+               vmcb12->control.insn_len =
+                       nested_svm_fetch_insn_bytes(vcpu,
+                                                   vmcb12->control.insn_bytes,
+                                                   max_bytes);
+       else
+               nested_svm_invalidate_insn_bytes(vmcb12);
 
 out:
        svm->nested.vmcb02_insn_bytes_valid = false;
arch/x86/kvm/x86.c +3 -3
index 1705e7be46ec..ad8b35f2219d 100644
@@ -4772,20 +4772,34 @@ gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
 
 static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
                                      struct kvm_vcpu *vcpu, u64 access,
-                                     struct x86_exception *exception)
+                                     struct x86_exception *exception,
+                                     unsigned int *bytes_read)
 {
        struct kvm_pagewalk *gva_walk = &vcpu->arch.gva_walk;
        void *data = val;
        int r = X86EMUL_CONTINUE;
 
        while (bytes) {
-               gpa_t gpa = gva_walk->gva_to_gpa(vcpu, gva_walk, addr, access, exception);
+               gpa_t gpa;
                unsigned offset = addr & (PAGE_SIZE-1);
                unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
                int ret;
 
-               if (gpa == INVALID_GPA)

arch/x86/kvm/x86.c gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva, · line 4796

Quoted 10 lines
arch/x86/kvm/x86.c +9 -1
-                       return X86EMUL_PROPAGATE_FAULT;
+               /* Apply address wrapping or canonicality checks before each fetch chunk. */
+               if (access & PFERR_FETCH_MASK) {
+                       if (!is_64_bit_mode(vcpu))
+                               addr = (u32)addr;
+                       else if (is_noncanonical_address(addr, vcpu, 0)) {
+                               r = X86EMUL_UNHANDLEABLE;
+                               goto out;
+                       }
+               }

I hadn't anticipated the need for this special-case, and it's more intrusive than just returning the number of bytes read.

I see now that it's complicated to match the behavior of the emulator's __do_insn_fetch_bytes(). You could clamp the number of bytes to read up-front to deal with the end of the address space, but you would still need a second call to handle the wrap. Something like the following (mangled by my MUA):

static unsigned int __kvm_fetch_guest_virt(struct kvm_vcpu *vcpu, gva_t addr,
void *val, unsigned int bytes)
{
struct x86_exception exception = {};
unsigned int bytes_read;
u64 access = PFERR_FETCH_MASK;

if (kvm_x86_call(get_cpl)(vcpu) == 3)
access |= PFERR_USER_MASK;

kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
&exception, &bytes_read);
return bytes_read;
}

unsigned int kvm_fetch_guest_virt(struct kvm_vcpu *vcpu, gva_t addr,
void *val, unsigned int bytes)
{
unsigned int first, bytes_read;
bool wraps;
u64 avail;

/* An instruction-sized fetch can wrap at most once. */ if (WARN_ON_ONCE(bytes > X86_MAX_INSTRUCTION_LENGTH)) return 0;

if (!is_64_bit_mode(vcpu)) {
addr = (u32)addr;
avail = BIT_ULL(32) - addr;
wraps = true;
} else if (is_noncanonical_address(addr, vcpu, 0)) { return 0;
} else if (addr < BIT_ULL(63)) {
avail = BIT_ULL(vcpu_virt_addr_bits(vcpu) - 1) - addr; wraps = false;
} else {
avail = -addr;
wraps = true;
}

first = min_t(u64, bytes, avail);
bytes_read = __kvm_fetch_guest_virt(vcpu, addr, val, first);

if (wraps && bytes_read == first && first < bytes) bytes_read += __kvm_fetch_guest_virt(vcpu, 0, val + first,

    bytes - first);

return bytes_read;
}

I think this solution may be worse than what you have now, but I will defer to Sean. Maybe he will have a better idea. (Maybe my suggestion to return the number of bytes read was misguided.)

author
#8 lore raw

Re: [PATCH v8 5/8] KVM: nSVM: Fetch DecodeAssist bytes for synthesized faults

Quoted 56 lines · Jim Mattson

I hadn't anticipated the need for this special-case, and it's more intrusive than just returning the number of bytes read.

I see now that it's complicated to match the behavior of the emulator's __do_insn_fetch_bytes(). You could clamp the number of bytes to read up-front to deal with the end of the address space, but you would still need a second call to handle the wrap. Something like the following (mangled by my MUA):

static unsigned int __kvm_fetch_guest_virt(struct kvm_vcpu *vcpu, gva_t addr,
void *val, unsigned int bytes)
{
struct x86_exception exception = {};
unsigned int bytes_read;
u64 access = PFERR_FETCH_MASK;

if (kvm_x86_call(get_cpl)(vcpu) == 3)
access |= PFERR_USER_MASK;

kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
&exception, &bytes_read);
return bytes_read;
}

unsigned int kvm_fetch_guest_virt(struct kvm_vcpu *vcpu, gva_t addr,
void *val, unsigned int bytes)
{
unsigned int first, bytes_read;
bool wraps;
u64 avail;

/* An instruction-sized fetch can wrap at most once. */ if (WARN_ON_ONCE(bytes > X86_MAX_INSTRUCTION_LENGTH)) return 0;

if (!is_64_bit_mode(vcpu)) {
addr = (u32)addr;
avail = BIT_ULL(32) - addr;
wraps = true;
} else if (is_noncanonical_address(addr, vcpu, 0)) { return 0;
} else if (addr < BIT_ULL(63)) {
avail = BIT_ULL(vcpu_virt_addr_bits(vcpu) - 1) - addr; wraps = false;
} else {
avail = -addr;
wraps = true;
}

first = min_t(u64, bytes, avail);
bytes_read = __kvm_fetch_guest_virt(vcpu, addr, val, first);

if (wraps && bytes_read == first && first < bytes) bytes_read += __kvm_fetch_guest_virt(vcpu, 0, val + first,

     bytes - first);
Quoted 9 lines

return bytes_read;
}

I think this solution may be worse than what you have now, but I will defer to Sean. Maybe he will have a better idea. (Maybe my suggestion to return the number of bytes read was misguided.)

Thanks, Jim, for the suggestion and review.

Hi Sean, what do you think?

Thanks,
Tina

author
#9 lore raw

Re: [PATCH v8 5/8] KVM: nSVM: Fetch DecodeAssist bytes for synthesized faults

Hi Sean,

Just a gentle ping on this. I'd appreciate your thoughts when you have a chance.

Thanks,
Tina

Quoted 74 lines · Tina Zhang
Quoted 56 lines · Jim Mattson

I hadn't anticipated the need for this special-case, and it's more intrusive than just returning the number of bytes read.

I see now that it's complicated to match the behavior of the emulator's __do_insn_fetch_bytes(). You could clamp the number of bytes to read up-front to deal with the end of the address space, but you would still need a second call to handle the wrap.  Something like the following (mangled by my MUA):

static unsigned int __kvm_fetch_guest_virt(struct kvm_vcpu *vcpu, gva_t addr, void *val, unsigned int bytes)
{
struct x86_exception exception = {};
unsigned int bytes_read;
u64 access = PFERR_FETCH_MASK;

if (kvm_x86_call(get_cpl)(vcpu) == 3)
access |= PFERR_USER_MASK;

kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, &exception, &bytes_read);
return bytes_read;
}

unsigned int kvm_fetch_guest_virt(struct kvm_vcpu *vcpu, gva_t addr, void *val, unsigned int bytes)
{
unsigned int first, bytes_read;
bool wraps;
u64 avail;

/* An instruction-sized fetch can wrap at most once. */ if (WARN_ON_ONCE(bytes > X86_MAX_INSTRUCTION_LENGTH)) return 0;

if (!is_64_bit_mode(vcpu)) {
addr = (u32)addr;
avail = BIT_ULL(32) - addr;
wraps = true;
} else if (is_noncanonical_address(addr, vcpu, 0)) { return 0;
} else if (addr < BIT_ULL(63)) {
avail = BIT_ULL(vcpu_virt_addr_bits(vcpu) - 1) - addr; wraps = false;
} else {
avail = -addr;
wraps = true;
}

first = min_t(u64, bytes, avail);
bytes_read = __kvm_fetch_guest_virt(vcpu, addr, val, first);

if (wraps && bytes_read == first && first < bytes) bytes_read += __kvm_fetch_guest_virt(vcpu, 0, val + first, bytes - first);

Quoted 9 lines

return bytes_read;
}

I think this solution may be worse than what you have now, but I will defer to Sean. Maybe he will have a better idea. (Maybe my suggestion to return the number of bytes read was misguided.)

Thanks, Jim, for the suggestion and review.

Hi Sean, what do you think?

Thanks,
Tina

#10 lore raw

Re: [PATCH v8 5/8] KVM: nSVM: Fetch DecodeAssist bytes for synthesized faults

Quoted 4 lines · Tina Zhang

Hi Sean,

Just a gentle ping on this. I'd appreciate your thoughts when you have a chance.

This is on my TODO list, but it will be at least a week before I get to it (merge window and other horrors). Sorry :-(

author
#11 lore raw

PATCH v8 6/8 KVM: nSVM: Use emulator bytes for synthesized nested #NPF

In reply to Tina Zhang

Preserve the bytes used by the emulator when synthesizing a data #NPF. Reuse the fetch cache only when the fault is the current emulator exception, identified by fault == &ctxt->exception.

Add an emulator accessor to copy cached instruction bytes, checking the RIP and cache bounds. These checks alone do not establish that the cache belongs to the current emulation.

Write the cached bytes directly to VMCB02 and fetch any missing tail through the instruction-fetch helper. If the tail cannot be read, report only the bytes already available. For SEV guests, retain only the cached bytes because KVM cannot fetch plaintext guest instructions.

---
 arch/x86/kvm/emulate.c     | 22 ++++++++++++++++++++++
 arch/x86/kvm/kvm_emulate.h |  3 +++
 arch/x86/kvm/svm/nested.c  | 36 ++++++++++++++++++++++++++++++++++--
 3 files changed, 59 insertions(+), 2 deletions(-)
arch/x86/kvm/emulate.c +22 -0
index 299aeaca6cf2..f15780116e8d 100644
@@ -900,6 +900,28 @@ static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
 		return X86EMUL_CONTINUE;
 }
 
+/*
+ * The caller is responsible for ensuring that the fetch cache belongs to
+ * the current instruction.  Matching EIP alone does not guarantee this.
+ */
+unsigned int x86_emulator_copy_insn_bytes(struct x86_emulate_ctxt *ctxt,
+					  unsigned long eip, u8 *bytes,
+					  unsigned int max_bytes)
+{
+	unsigned int nr_bytes;
+
+	if (!ctxt || ctxt->eip != eip ||
+	    ctxt->fetch.end < ctxt->fetch.data ||
+	    ctxt->fetch.end > ctxt->fetch.data + sizeof(ctxt->fetch.data))
+		return 0;
+
+	nr_bytes = min_t(unsigned int, ctxt->fetch.end - ctxt->fetch.data,
+			 max_bytes);
+	memcpy(bytes, ctxt->fetch.data, nr_bytes);
+	return nr_bytes;
+}
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(x86_emulator_copy_insn_bytes);
+
 /* Fetch next part of the instruction being emulated. */
 #define insn_fetch(_type, _ctxt)					\
 ({	_type _x;							\
arch/x86/kvm/kvm_emulate.h +3 -0
index d05558ef28ae..5361d42e6e05 100644
@@ -528,6 +528,9 @@ enum x86_intercept {
 };
 
 int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, int emulation_type);
+unsigned int x86_emulator_copy_insn_bytes(struct x86_emulate_ctxt *ctxt,
+					  unsigned long eip, u8 *bytes,
+					  unsigned int max_bytes);
 bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt);
 #define EMULATION_FAILED -1
 #define EMULATION_OK 0
arch/x86/kvm/svm/nested.c +34 -2
index 813b0fc663c6..c1aef51bc1be 100644
@@ -93,13 +93,40 @@ static u8 nested_svm_get_insn_bytes_len(struct kvm_vcpu *vcpu, u8 max_bytes)
 }
 
 static u8 nested_svm_fetch_insn_bytes(struct kvm_vcpu *vcpu, u8 *bytes,
-				      u8 max_bytes)
+				      u8 count, u8 max_bytes)
 {
 	gva_t rip = kvm_get_linear_rip(vcpu);
 
 	max_bytes = nested_svm_get_insn_bytes_len(vcpu, max_bytes);
+	count = min(count, max_bytes);
 
-	return kvm_fetch_guest_virt(vcpu, rip, bytes, max_bytes);
+	return count + kvm_fetch_guest_virt(vcpu, rip + count, bytes + count,
+					   max_bytes - count);
+}
+
+static void nested_svm_prepare_synthesized_insn_bytes(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_svm *svm = to_svm(vcpu);
+	struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
+	struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
+	u8 max_bytes = sizeof(vmcb02->control.insn_bytes);
+	u8 count;
+
+	nested_svm_invalidate_vmcb02_insn_bytes(svm);
+
+	if (!guest_cpu_cap_has(vcpu, X86_FEATURE_DECODEASSISTS))
+		return;
+
+	max_bytes = nested_svm_get_insn_bytes_len(vcpu, max_bytes);
+	count = x86_emulator_copy_insn_bytes(ctxt, kvm_rip_read(vcpu),
+					     vmcb02->control.insn_bytes, max_bytes);
+	if (!is_sev_guest(vcpu))
+		count = nested_svm_fetch_insn_bytes(vcpu,
+						    vmcb02->control.insn_bytes,
+						    count, max_bytes);
+	vmcb02->control.insn_len = count;
+
+	svm->nested.vmcb02_insn_bytes_valid = true;
 }
 
 static void nested_svm_update_vmcb12_insn_bytes(struct kvm_vcpu *vcpu,
@@ -126,6 +153,7 @@ static void nested_svm_update_vmcb12_insn_bytes(struct kvm_vcpu *vcpu,
 		vmcb12->control.insn_len =
 			nested_svm_fetch_insn_bytes(vcpu,
 						    vmcb12->control.insn_bytes,
+						    0,
 						    max_bytes);
 	else
 		nested_svm_invalidate_insn_bytes(vmcb12);
@@ -140,6 +168,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;
 
 	/*
@@ -169,6 +199,8 @@ static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
 
 	if (from_hardware)
 		nested_svm_set_vmcb02_insn_bytes_valid(svm);
+	else if (from_emulation && !(fault->error_code & PFERR_FETCH_MASK))
+		nested_svm_prepare_synthesized_insn_bytes(vcpu);
 	else
 		nested_svm_invalidate_vmcb02_insn_bytes(svm);
 	nested_svm_vmexit(svm);
Signature · 1 line
2.43.7
author
#12 lore raw

PATCH v8 7/8 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/cpuid.c   | 1 +
 arch/x86/kvm/svm/svm.c | 2 ++
 2 files changed, 3 insertions(+)
arch/x86/kvm/cpuid.c +1 -0
index 7c63ef90fc60..52dcde011987 100644
@@ -1195,6 +1195,7 @@ void kvm_initialize_cpu_caps(void)
 		VENDOR_F(NPT),
 		VENDOR_F(VMCBCLEAN),
 		VENDOR_F(FLUSHBYASID),
+		VENDOR_F(DECODEASSISTS),
 		VENDOR_F(NRIPS),
 		VENDOR_F(TSCRATEMSR),
 		VENDOR_F(V_VMSAVE_VMLOAD),
arch/x86/kvm/svm/svm.c +2 -0
index e83e4e5e7b93..82de844f63ad 100644
@@ -5557,6 +5557,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
#13 lore raw

Re: [PATCH v8 7/8] KVM: nSVM: Advertise DecodeAssists to L1

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] kvm: nSVM: missing CR_VALID bit in exit_info_1 for synthesized DR intercepts --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] kvm: nSVM: missing CR_VALID bit in exit_info_1 for synthesized DR intercepts --

commit 442db7fa42ab1144e62575ec3079cbc2d2e5ea40
Author: Tina Zhang <[email protected]>

KVM: nSVM: Advertise DecodeAssists to L1

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. Expose the feature only when supported by hardware, as KVM still relies on hardware DecodeAssists for VM-Exits that are reflected directly from L2.

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

Quoted 10 lines
arch/x86/kvm/svm/svm.c +2 -0
index e83e4e5e7b934..82de844f63ade 100644
@@ -5557,6 +5557,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: Medium]
Will advertising DECODEASSISTS here cause issues for synthesized DR intercepts?

Looking at svm_check_intercept() in arch/x86/kvm/svm/svm.c, when an L2 guest executes a MOV DRx instruction with decode assists enabled, KVM synthesizes an intercept but doesn't set the CR_VALID bit (bit 63) in exit_info_1:

	case SVM_EXIT_READ_DR0:
	case SVM_EXIT_WRITE_DR0:
		icpt_info.exit_code += info->modrm_reg;
		if (decode_assists)
			vmcb->control.exit_info_1 = info->modrm_rm & SVM_EXITINFO_REG_MASK;
		break;

Does the AMD APM require bit 63 to be set for DR intercepts? If it does, would an L1 hypervisor that strictly validates the EXITINFO1 field reject this synthesized intercept data, leading to emulation fallbacks or failures?

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

PATCH v8 8/8 KVM: selftests: Add nested SVM DecodeAssists test

In reply to Tina Zhang

Add a focused nested SVM selftest for DecodeAssists. Verify that KVM exposes the feature to L1 and provides EXITINFO for representative MOV CR/DR, CLTS, INTn, INVLPG, and INVLPGA intercepts, including a MOV-to-CR0 selective intercept. Check hardware and forced-emulation paths against the same expected values. Poison the instruction-byte fields before each instruction-intercept test and verify that the byte count is cleared on exit.

Exercise instruction bytes for hardware and synthesized #NPF/#PF exits. Cover a synthesized #NPF that follows a hardware #NPF in the same emulated instruction, an unreadable instruction tail that leaves only the cached opcode, and the absence of bytes for an instruction-fetch #PF. Modify the MOVSB opcode while its MMIO read is pending to verify that the emulator's cached bytes are preserved.

Inject a userspace #PF while an MMIO read is pending. Verify that after MMIO completion, L1 receives instruction bytes from the current RIP, not the previous instruction's emulator cache.

The hardware instruction-intercept, synthesized OUTSB #NPF, and userspace-injected #PF cases run by default. The forced-emulation #PF and instruction-intercept paths require kvm.force_emulation_prefix=1.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/include/x86/processor.h     |   1 +
 .../kvm/x86/svm_nested_decode_assists_test.c  | 590 ++++++++++++++++++
 3 files changed, 592 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 239bc61ea384..de9423693399 100644
@@ -118,6 +118,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 7579e692bc2b..bd281f521eb1 100644
@@ -222,6 +222,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 +590 -0

590 lines not on this page · raw message

Signature · 1 line
2.43.7
#15 lore raw

Re: [PATCH v8 8/8] KVM: selftests: Add nested SVM DecodeAssists test

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] kvm: selftests: missing .global directive on inline assembly labels causes LTO link error --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] kvm: selftests: missing .global directive on inline assembly labels causes LTO link error --

commit 08e87505d5c0bae297f7e3e768c2e12a61d10322
Author: Tina Zhang <[email protected]>

KVM: selftests: Add nested SVM DecodeAssists test

This commit adds a nested SVM selftest to verify KVM's virtualization of DecodeAssists. It covers instruction byte extraction for various instructions and edge cases such as synthesized nested page faults and userspace-injected page faults.

Quoted 6 lines
tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c new file +0 -0
new file mode 100644
index 0000000000000..aba91d8bb1a28
@@ -0,0 +1,590 @@

[ ... ]

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

Quoted 7 lines
tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c new file +7 -0
+extern u8 l2_movsb_insn[];
+
+static void l2_movsb_code(void)
+{
+	asm volatile("mov %0, %%rsi\n\t"
+		     "mov %1, %%rdi\n\t"
+		     "l2_movsb_insn:\n\t"

[Severity: Low]
Is a .global (or .globl) directive needed for this assembly label?

When building the selftests with Link-Time Optimization (LTO) enabled, the l2_movsb_code function and the functions referencing the extern l2_movsb_insn array (like test_synthesized_npf_after_hardware_npf) might be partitioned into different LTRANS units.

Since this label lacks a global directive, it remains strictly local to its inline assembly block's partition. The linker therefore cannot resolve the extern declaration in other partitions, causing an undefined reference build error.

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

Quoted 12 lines
tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c new file +12 -0
+		     "movsb"
+		     : : "r"(&mmio_source), "r"(&npf_target)
+		     : "rsi", "rdi", "memory");
+	GUEST_FAIL("L2 MOVSB did not cause a nested page fault");
+}
+
+extern u8 l2_userspace_pf_insn[];
+
+static void l2_userspace_pf_code(void)
+{
+	asm volatile("movzbl (%0), %%eax\n\t"
+		     "l2_userspace_pf_insn:\n\t"

[Severity: Low]
Does this label also need a .global directive to prevent LTO linkage failures, similar to the l2_movsb_insn label above?

Quoted 5 lines
tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c new file +5 -0
+		     "nop"
+		     : : "r"(&mmio_source) : "rax", "memory");
+	GUEST_FAIL("Userspace-injected #PF was not intercepted by L1");
+}
+

[ ... ]

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