Skip to contentSkip to conversation

KVM: nSVM: Enable DecodeAssists for nested guests

PATCH Superseded Tina Zhang 3 participants 16 messages Sep 18

Subsystems (4)

v7 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Tina Zhang

author
#0 lore raw

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

Before rebasing, the updated selftest passed with kvm.force_emulation_prefix both disabled and enabled in an isolated VM using the v7 implementation. The non-FEP cases also passed on the physical host. After rebasing onto the base commit below, the affected KVM objects and selftest build successfully; runtime validation on the new base is still pending. SEV paths have not been tested.

Changes since v6:
- Rebase onto kvm-x86/next at the base commit listed below and use cpu_feature_enabled() in place of the removed static_cpu_has() API.
- Document why get_invlpg_linear_addr() ignores __linearize() failures:
the address is computed before a failure and matches em_invlpg().
- Use CR_VALID and SVM_EXITINFO_REG_MASK for MOV CR/DR decode state, and retain MOV-to-CR0 information for selective CR0 write intercepts.
- Use the common VMCB02 invalidation helper for synthesized exits that do not provide instruction bytes.
- Extend kvm_read_guest_virt_helper() with an optional bytes_read output and make kvm_fetch_guest_virt() return the number of bytes read. Remove the nSVM page loop and handle instruction-fetch address wrapping and canonicality checks in the generic helper's existing loop.
- Run instruction-intercept tests without FEP as well as with FEP when enabled. Add a MOV-to-CR0 selective-intercept case and poison insn_len and insn_bytes before each run. Keep the CLTS check limited to bit 63.
- Add Jim's Reviewed-by tags for hardware-byte propagation and emulator cache reuse.

Previous versions (including earlier changelogs):
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: 70c944caf570fda2d79baa71435589a8db39f048

Signature · 1 line
2.43.7
author
#1 lore raw

PATCH v7 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..2972a35b0ac0 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;
+
+	/*
+	 * Use the same address calculation as em_invlpg() for the address
+	 * reported to L1.  Ignore address-check failures, as __linearize()
+	 * sets linear before reporting a failure.
+	 */
+	__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
#2 lore raw

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

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

---
 arch/x86/kvm/emulate.c     | 23 +++++++++++++++++++++++
 arch/x86/kvm/kvm_emulate.h |  1 +
 2 files changed, 24 insertions(+)
arch/x86/kvm/emulate.c +8 -0
index c1b21282187f..2972a35b0ac0 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;

arch/x86/kvm/emulate.c static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt, · line 704

Quoted 10 lines
arch/x86/kvm/emulate.c +10 -0
+       unsigned long linear = 0;
+
+       if (intercept != x86_intercept_invlpg)
+               return 0;
+
+       /*
+        * Use the same address calculation as em_invlpg() for the address
+        * reported to L1.  Ignore address-check failures, as __linearize()
+        * sets linear before reporting a failure.
+        */

That comment misses the main point. Perhaps something like:

/*
* Ignore faults, just as em_invlpg() does. Calling __linearize()
* exactly as em_invlpg() does guarantees agreement with
* em_invlpg() on the linear address.
*/

Quoted 8 lines
arch/x86/kvm/emulate.c +5 -0
+       __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,
author
#3 lore raw

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

Quoted 7 lines · Jim Mattson

That comment misses the main point. Perhaps something like:

/*
* Ignore faults, just as em_invlpg() does. Calling __linearize()
* exactly as em_invlpg() does guarantees agreement with
* em_invlpg() on the linear address.
*/

Thanks. I'll use your suggested comment in the next version.

Tina

author
#4 lore raw

PATCH v7 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
#5 lore raw

Re: [PATCH v7 2/8] KVM: nSVM: Synthesize DecodeAssists EXITINFO for emulated intercepts

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] kvm: nSVM: Stale EXITINFO2 leaked to L1 for Decode Assists and SWINT/ICEBP - [High] kvm: nSVM…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] kvm: nSVM: Stale EXITINFO2 leaked to L1 for Decode Assists and SWINT/ICEBP
- [High] kvm: nSVM: Unconditional CR_VALID injects spurious #GP into 32-bit guests
- [Medium] kvm: nSVM: Missing Decode Assists synthesis for SVM instructions --

commit 426ea34e024e5747a2d0375a678f4064cd396fdd
Author: Tina Zhang <[email protected]>

KVM: nSVM: Synthesize DecodeAssists EXITINFO for emulated intercepts

This commit adds synthesis of DecodeAssists EXITINFO1 fields for emulated instruction intercepts such as MOV CR/DR, INTn, and INVLPG when the guest CPU exposes DecodeAssists capabilities.

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

[ ... ]

arch/x86/kvm/svm/svm.c static int svm_check_intercept(struct kvm_vcpu *vcpu, · line 4852

Quoted 7 lines
arch/x86/kvm/svm/svm.c +2 -0
@@ -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) {

[Severity: Medium]
Does this switch statement miss several mandatory intercepts defined in the hardware specification for Decode Assists?

According to AMD APM Vol 2, Table 15-22, Decode Assists also apply to VMRUN, VMLOAD, VMSAVE, CLGI, STGI, and PAUSE, requiring EXITINFO1 to be explicitly cleared to 0.

Since these intercepts are omitted, will they fall through to the default case and leave uninitialized, stale garbage in EXITINFO1 instead of the architecturally required 0?

arch/x86/kvm/svm/svm.c static int svm_check_intercept(struct kvm_vcpu *vcpu, · line 4860

Quoted 8 lines
arch/x86/kvm/svm/svm.c +5 -0
 	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;

[Severity: High]
Does setting CR_VALID unconditionally here, and below in SVM_EXIT_WRITE_CR0, inject spurious #GP faults into 32-bit guests?

AMD APM defines Bit 63 (CR_VALID) of EXITINFO1 for CR accesses under Decode Assists as "1 = instruction is 64-bit, 0 = instruction is 32-bit".

If a 32-bit L2 guest executes a CR access and its general purpose register happens to have non-zero upper 32 bits (which is permissible), L1 will see Bit 63 set.

If L1 treats this as a 64-bit instruction, will it read the full 64-bit vCPU register and fail safety checks (like cr0 & 0xffffffff00000000UL), resulting in a spurious #GP being injected into L2?

[Severity: High]
Should exit_info_2 also be unconditionally zeroed here when decode_assists is true?

AMD APM mandates that when Decode Assists is enabled, EXITINFO2 must contain NextRIP for INTn and ICEBP, and must be 0 for other intercepts like MOV CRx.

Because KVM does not zero exit_info_2 prior to calling svm_check_intercept(), could this retain a stale value in the L2 VMCB from a prior exit (such as a #NPF faulting GPA) and leak it to L1?

Quoted 9 lines
arch/x86/kvm/svm/svm.c +5 -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;
+

[ ... ]

arch/x86/kvm/svm/svm.c static int svm_check_intercept(struct kvm_vcpu *vcpu, · line 4920

Quoted 4 lines
arch/x86/kvm/svm/svm.c +3 -0
 	case SVM_EXIT_SWINT:
+		if (decode_assists)
+			vmcb->control.exit_info_1 = info->src_val & 0xff;
+		break;

[Severity: High]
Does this miss setting exit_info_2 for the SWINT intercept?

AMD APM mandates that EXITINFO2 must contain NextRIP for INTn (SVM_EXIT_SWINT) and ICEBP (SVM_EXIT_ICEBP) when Decode Assists is enabled.

By not setting exit_info_2 = info->next_rip for SWINT, and completely omitting handling for SVM_EXIT_ICEBP, does L1 read the stale EXITINFO2 as NextRIP?

If L1 relies on this for NextRIP, could it cause L2 to resume at a corrupted instruction pointer and crash the L2 guest?

Quoted 4 lines
arch/x86/kvm/svm/svm.c +3 -0
 	case SVM_EXIT_INVLPG:
+		if (decode_assists)
+			vmcb->control.exit_info_1 = info->invlpg_linear_addr;
+		break;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Reviewed-by on v7 2/8
#6 lore raw

Re: [PATCH v7 2/8] KVM: nSVM: Synthesize DecodeAssists EXITINFO for emulated intercepts

In reply to Tina Zhang
Quoted 6 lines · 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.

Quoted 8 lines

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.

I think Sashiko is hallucinating.

author
#7 lore raw

PATCH v7 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 23d29597d6bf..378467288716 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
Reviewed-by on v7 3/8
#8 lore raw

Re: [PATCH v7 3/8] KVM: nSVM: Track valid hardware DecodeAssist bytes

Quoted 11 lines · 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.

Quoted 4 lines

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

author
#9 lore raw

PATCH v7 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 378467288716..d99706e1b9c0 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
#10 lore raw

PATCH v7 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 d99706e1b9c0..c17df8c2e75f 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
author
#11 lore raw

PATCH v7 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 2972a35b0ac0..0e67e70f7b70 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 c17df8c2e75f..1d3086eb08b7 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
Reviewed-by on v7 6/8
#12 lore raw

Re: [PATCH v7 6/8] KVM: nSVM: Use emulator bytes for synthesized nested #NPF

Quoted 9 lines · 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.

Quoted 7 lines

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.

Oops. My bad.

author
#13 lore raw

PATCH v7 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
author
#14 lore raw

PATCH v7 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 96bab7002d39..bb55a8c32c42 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
Reviewed-by on v7 8/8
#15 lore raw

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

Quoted 20 lines · 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.

Quoted 5 lines

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.