AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/08/01 01:24 flow patch-triage
0m Args:
null

Results:
{
  "EnableConfigs": [
    "KVM",
    "KVM_AMD"
  ],
  "FocusSymbols": [
    "nested_svm_exit_handled",
    "kvm_inject_page_fault",
    "kvm_queue_exception",
    "kvm_queue_exception_p",
    "kvm_queue_exception_e"
  ],
  "Reasoning": "The patch series implements support for X86_FEATURE_DECODEASSISTS in KVM's nested SVM (AMD virtualization). It modifies the handling of instruction intercepts and page faults to properly capture and synthesize instruction bytes for nested guests. This involves changes to core KVM exception queuing (kvm_queue_exception*, kvm_inject_page_fault) and nested SVM exit handling (nested_svm_exit_handled). These are functional changes to reachable core virtualization logic and should be fuzzed.",
  "WorthFuzzing": true
}

1/1 2026/08/01 01:24 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit a195452056def57ccba90493b6e20a90e99d9f34\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Sat Aug 1 01:24:34 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h\nindex 7a258831616fa..8466a17cd046f 100644\n--- a/arch/x86/include/asm/kvm_host.h\n+++ b/arch/x86/include/asm/kvm_host.h\n@@ -688,6 +688,7 @@ struct kvm_queued_exception {\n \tbool pending;\n \tbool injected;\n \tbool has_error_code;\n+\tbool has_emulator_context;\n \tu8 vector;\n \tu32 error_code;\n \tunsigned long payload;\ndiff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c\nindex 8071b372d233e..a0e57b64cadd4 100644\n--- a/arch/x86/kvm/emulate.c\n+++ b/arch/x86/kvm/emulate.c\n@@ -410,6 +410,26 @@ static int em_salc(struct x86_emulate_ctxt *ctxt)\n \t_fault ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE; \\\n })\n \n+static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)\n+{\n+\tif (ctxt-\u003emode == X86EMUL_MODE_PROT64 \u0026\u0026 seg \u003c VCPU_SREG_FS)\n+\t\treturn 0;\n+\n+\treturn ctxt-\u003eops-\u003eget_cached_segment_base(ctxt, seg);\n+}\n+\n+static u64 get_intercept_linear_addr(struct x86_emulate_ctxt *ctxt,\n+\t\t\t\t     enum x86_intercept intercept)\n+{\n+\tu64 la;\n+\n+\tif (intercept != x86_intercept_invlpg)\n+\t\treturn 0;\n+\n+\tla = seg_base(ctxt, ctxt-\u003esrc.addr.mem.seg) + ctxt-\u003esrc.addr.mem.ea;\n+\treturn ctxt-\u003emode == X86EMUL_MODE_PROT64 ? la : (u32)la;\n+}\n+\n static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,\n \t\t\t\t    enum x86_intercept intercept,\n \t\t\t\t    enum x86_intercept_stage stage)\n@@ -427,6 +447,7 @@ static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,\n \t\t.src_type   = ctxt-\u003esrc.type,\n \t\t.dst_type   = ctxt-\u003edst.type,\n \t\t.ad_bytes   = ctxt-\u003ead_bytes,\n+\t\t.intercept_linear_addr = get_intercept_linear_addr(ctxt, intercept),\n \t\t.rip\t    = ctxt-\u003eeip,\n \t\t.next_rip   = ctxt-\u003e_eip,\n \t};\n@@ -520,14 +541,6 @@ static u32 desc_limit_scaled(struct desc_struct *desc)\n \treturn desc-\u003eg ? (limit \u003c\u003c 12) | 0xfff : limit;\n }\n \n-static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)\n-{\n-\tif (ctxt-\u003emode == X86EMUL_MODE_PROT64 \u0026\u0026 seg \u003c VCPU_SREG_FS)\n-\t\treturn 0;\n-\n-\treturn ctxt-\u003eops-\u003eget_cached_segment_base(ctxt, seg);\n-}\n-\n static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,\n \t\t\t     u32 error, bool valid)\n {\ndiff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h\nindex 3e375af15c035..9eda648a13143 100644\n--- a/arch/x86/kvm/kvm_emulate.h\n+++ b/arch/x86/kvm/kvm_emulate.h\n@@ -51,6 +51,7 @@ struct x86_instruction_info {\n \tu8  src_type;\t\t/* type of source operand\t\t*/\n \tu8  dst_type;\t\t/* type of destination operand\t\t*/\n \tu8  ad_bytes;           /* size of src/dst address              */\n+\tu64 intercept_linear_addr;\t/* arch-reported linear address, if any */\n \tu64 rip;\t\t/* rip of the instruction\t\t*/\n \tu64 next_rip;           /* rip following the instruction        */\n };\ndiff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c\nindex 73f37b050d0a0..2ade1ee0d2761 100644\n--- a/arch/x86/kvm/svm/nested.c\n+++ b/arch/x86/kvm/svm/nested.c\n@@ -35,12 +35,174 @@\n \n #define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK\n \n+static void nested_svm_clear_insn_bytes(struct vmcb *vmcb)\n+{\n+\tvmcb-\u003econtrol.insn_len = 0;\n+\tmemset(vmcb-\u003econtrol.insn_bytes, 0,\n+\t       sizeof(vmcb-\u003econtrol.insn_bytes));\n+}\n+\n+static void nested_svm_copy_insn_bytes(struct vmcb *to,\n+\t\t\t\t       const struct vmcb *from)\n+{\n+\tu8 insn_len = from-\u003econtrol.insn_len;\n+\n+\tnested_svm_clear_insn_bytes(to);\n+\n+\tif (WARN_ON_ONCE(insn_len \u003e sizeof(from-\u003econtrol.insn_bytes)))\n+\t\treturn;\n+\n+\tto-\u003econtrol.insn_len = insn_len;\n+\tmemcpy(to-\u003econtrol.insn_bytes, from-\u003econtrol.insn_bytes, insn_len);\n+}\n+\n+static bool nested_svm_vmexit_supports_insn_bytes(struct kvm_vcpu *vcpu,\n+\t\t\t\t\t\t  const struct vmcb *vmcb02)\n+{\n+\tu64 exit_code = vmcb02-\u003econtrol.exit_code;\n+\n+\tif (!guest_cpu_cap_has(vcpu, X86_FEATURE_DECODEASSISTS))\n+\t\treturn false;\n+\n+\tif (exit_code != SVM_EXIT_NPF \u0026\u0026\n+\t    exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR)\n+\t\treturn false;\n+\n+\treturn !(vmcb02-\u003econtrol.exit_info_1 \u0026 PFERR_FETCH_MASK);\n+}\n+\n+static void nested_svm_clear_synthesized_insn_bytes(struct vcpu_svm *svm)\n+{\n+\tsvm-\u003enested.synthesized_insn_bytes.prepared = false;\n+\tsvm-\u003enested.synthesized_insn_bytes.insn_len = 0;\n+}\n+\n+static u8 nested_svm_fetch_insn_bytes(struct kvm_vcpu *vcpu, u8 *bytes,\n+\t\t\t\t      u8 count, u8 max_bytes)\n+{\n+\tstruct kvm_pagewalk *gva_walk = \u0026vcpu-\u003earch.gva_walk;\n+\tu64 access = PFERR_FETCH_MASK;\n+\tgva_t rip = kvm_get_linear_rip(vcpu);\n+\tstruct x86_exception e;\n+\n+\tif (kvm_x86_call(get_cpl)(vcpu) == 3)\n+\t\taccess |= PFERR_USER_MASK;\n+\n+\tif (!is_64_bit_mode(vcpu)) {\n+\t\tu32 eip = kvm_rip_read(vcpu);\n+\t\tu32 limit = to_svm(vcpu)-\u003evmcb-\u003esave.cs.limit;\n+\n+\t\tif (eip \u003e limit)\n+\t\t\treturn 0;\n+\t\tmax_bytes = min_t(u64, max_bytes, (u64)limit - eip + 1);\n+\t}\n+\n+\tcount = min(count, max_bytes);\n+\n+\twhile (count \u003c max_bytes) {\n+\t\tgva_t addr = rip + count;\n+\t\tunsigned int chunk;\n+\t\tgpa_t gpa;\n+\n+\t\tif (!is_64_bit_mode(vcpu))\n+\t\t\taddr = (u32)addr;\n+\t\telse if (is_noncanonical_address(addr, vcpu, 0))\n+\t\t\tbreak;\n+\n+\t\tchunk = min_t(unsigned int, max_bytes - count,\n+\t\t\t      PAGE_SIZE - offset_in_page(addr));\n+\t\tgpa = gva_walk-\u003egva_to_gpa(vcpu, gva_walk, addr, access, \u0026e);\n+\n+\t\tif (gpa == INVALID_GPA ||\n+\t\t    kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(gpa),\n+\t\t\t\t\t     bytes + count,\n+\t\t\t\t\t     offset_in_page(gpa), chunk))\n+\t\t\tbreak;\n+\n+\t\tcount += chunk;\n+\t}\n+\n+\treturn count;\n+}\n+\n+static void nested_svm_prepare_synthesized_insn_bytes(struct kvm_vcpu *vcpu)\n+{\n+\tstruct vcpu_svm *svm = to_svm(vcpu);\n+\tstruct nested_svm_insn_bytes *insn_bytes =\n+\t\t\u0026svm-\u003enested.synthesized_insn_bytes;\n+\tstruct x86_emulate_ctxt *ctxt = vcpu-\u003earch.emulate_ctxt;\n+\n+\tstatic_assert(sizeof(insn_bytes-\u003einsn_bytes) \u003e=\n+\t\t      sizeof(ctxt-\u003efetch.data));\n+\n+\tnested_svm_clear_synthesized_insn_bytes(svm);\n+\n+\tif (!guest_cpu_cap_has(vcpu, X86_FEATURE_DECODEASSISTS))\n+\t\treturn;\n+\n+\tif (!ctxt || ctxt-\u003eeip != kvm_rip_read(vcpu) ||\n+\t    ctxt-\u003efetch.end \u003c ctxt-\u003efetch.data ||\n+\t    ctxt-\u003efetch.end \u003e ctxt-\u003efetch.data + sizeof(ctxt-\u003efetch.data))\n+\t\treturn;\n+\n+\tinsn_bytes-\u003einsn_len = ctxt-\u003efetch.end - ctxt-\u003efetch.data;\n+\tmemcpy(insn_bytes-\u003einsn_bytes, ctxt-\u003efetch.data,\n+\t       insn_bytes-\u003einsn_len);\n+\tinsn_bytes-\u003eprepared = true;\n+}\n+\n+static void nested_svm_update_vmcb12_insn_bytes(struct kvm_vcpu *vcpu,\n+\t\t\t\t\t\tstruct vmcb *vmcb12,\n+\t\t\t\t\t\tstruct vmcb *vmcb02)\n+{\n+\tstruct vcpu_svm *svm = to_svm(vcpu);\n+\tstruct nested_svm_insn_bytes *insn_bytes =\n+\t\t\u0026svm-\u003enested.synthesized_insn_bytes;\n+\tconst u8 max_bytes = sizeof(vmcb12-\u003econtrol.insn_bytes);\n+\n+\tnested_svm_clear_insn_bytes(vmcb12);\n+\n+\tif (!nested_svm_vmexit_supports_insn_bytes(vcpu, vmcb02))\n+\t\tgoto out;\n+\n+\tif (svm-\u003enested.vmcb02_insn_bytes_fresh) {\n+\t\tnested_svm_copy_insn_bytes(vmcb12, vmcb02);\n+\t\tgoto out;\n+\t}\n+\n+\tif (insn_bytes-\u003eprepared) {\n+\t\tvmcb12-\u003econtrol.insn_len = insn_bytes-\u003einsn_len;\n+\t\tmemcpy(vmcb12-\u003econtrol.insn_bytes, insn_bytes-\u003einsn_bytes,\n+\t\t       vmcb12-\u003econtrol.insn_len);\n+\n+\t\tif (!is_sev_guest(vcpu))\n+\t\t\tvmcb12-\u003econtrol.insn_len =\n+\t\t\t\tnested_svm_fetch_insn_bytes(vcpu,\n+\t\t\t\t\t\t\t    vmcb12-\u003econtrol.insn_bytes,\n+\t\t\t\t\t\t\t    vmcb12-\u003econtrol.insn_len,\n+\t\t\t\t\t\t\t    max_bytes);\n+\t\tgoto out;\n+\t}\n+\n+\tif (!is_sev_guest(vcpu))\n+\t\tvmcb12-\u003econtrol.insn_len =\n+\t\t\tnested_svm_fetch_insn_bytes(vcpu,\n+\t\t\t\t\t\t    vmcb12-\u003econtrol.insn_bytes,\n+\t\t\t\t\t\t    0, max_bytes);\n+\n+out:\n+\tsvm-\u003enested.vmcb02_insn_bytes_fresh = false;\n+\tnested_svm_clear_synthesized_insn_bytes(svm);\n+}\n+\n static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,\n \t\t\t\t       struct x86_exception *fault,\n \t\t\t\t       bool from_hardware)\n {\n \tstruct vcpu_svm *svm = to_svm(vcpu);\n \tstruct vmcb *vmcb = svm-\u003evmcb;\n+\tstruct x86_emulate_ctxt *ctxt = vcpu-\u003earch.emulate_ctxt;\n+\tbool from_emulation = ctxt \u0026\u0026 fault == \u0026ctxt-\u003eexception;\n \tu64 fault_stage;\n \n \t/*\n@@ -68,6 +230,9 @@ static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,\n \t\t\t\t    (fault-\u003eerror_code \u0026 ~PFERR_GUEST_FAULT_STAGE_MASK);\n \tvmcb-\u003econtrol.exit_info_2 = fault-\u003eaddress;\n \n+\tsvm-\u003enested.vmcb02_insn_bytes_fresh = from_hardware;\n+\tif (from_emulation \u0026\u0026 !(fault-\u003eerror_code \u0026 PFERR_FETCH_MASK))\n+\t\tnested_svm_prepare_synthesized_insn_bytes(vcpu);\n \tnested_svm_vmexit(svm);\n }\n \n@@ -868,7 +1033,11 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)\n \t/*\n \t * Filled at exit: exit_code, exit_info_1, exit_info_2, exit_int_info,\n \t * exit_int_info_err, next_rip, insn_len, insn_bytes.\n+\t * Clear stale DecodeAssist data before L2 runs.\n \t */\n+\tnested_svm_clear_insn_bytes(vmcb02);\n+\tsvm-\u003enested.vmcb02_insn_bytes_fresh = false;\n+\tnested_svm_clear_synthesized_insn_bytes(svm);\n \n \tif (guest_cpu_cap_has(vcpu, X86_FEATURE_VGIF) \u0026\u0026\n \t    (vmcb12_ctrl-\u003eint_ctl \u0026 V_GIF_ENABLE_MASK))\n@@ -1297,6 +1466,8 @@ static int nested_svm_vmexit_update_vmcb12(struct kvm_vcpu *vcpu)\n \tif (guest_cpu_cap_has(vcpu, X86_FEATURE_NRIPS))\n \t\tvmcb12-\u003econtrol.next_rip  = vmcb02-\u003econtrol.next_rip;\n \n+\tnested_svm_update_vmcb12_insn_bytes(vcpu, vmcb12, vmcb02);\n+\n \tif (nested_vmcb12_has_lbrv(vcpu))\n \t\tsvm_copy_lbrs(\u0026vmcb12-\u003esave, \u0026vmcb02-\u003esave);\n \n@@ -1643,14 +1814,16 @@ static int nested_svm_intercept(struct vcpu_svm *svm)\n \treturn vmexit;\n }\n \n-int nested_svm_exit_handled(struct vcpu_svm *svm)\n+int nested_svm_exit_handled(struct vcpu_svm *svm, bool vmcb02_insn_bytes_fresh)\n {\n \tint vmexit;\n \n \tvmexit = nested_svm_intercept(svm);\n \n-\tif (vmexit == NESTED_EXIT_DONE)\n+\tif (vmexit == NESTED_EXIT_DONE) {\n+\t\tsvm-\u003enested.vmcb02_insn_bytes_fresh = vmcb02_insn_bytes_fresh;\n \t\tnested_svm_vmexit(svm);\n+\t}\n \n \treturn vmexit;\n }\n@@ -1698,6 +1871,10 @@ static void nested_svm_inject_exception_vmexit(struct kvm_vcpu *vcpu)\n \t\t\tvmcb-\u003econtrol.exit_info_2 = ex-\u003epayload;\n \t\telse\n \t\t\tvmcb-\u003econtrol.exit_info_2 = vcpu-\u003earch.cr2;\n+\n+\t\tif (ex-\u003ehas_emulator_context \u0026\u0026\n+\t\t    (!ex-\u003ehas_error_code || !(ex-\u003eerror_code \u0026 PFERR_FETCH_MASK)))\n+\t\t\tnested_svm_prepare_synthesized_insn_bytes(vcpu);\n \t} else if (ex-\u003evector == DB_VECTOR) {\n \t\t/* See kvm_check_and_inject_events().  */\n \t\tkvm_deliver_exception_payload(vcpu, ex);\ndiff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c\nindex c1e6d62512b95..366f287366faf 100644\n--- a/arch/x86/kvm/svm/svm.c\n+++ b/arch/x86/kvm/svm/svm.c\n@@ -1953,14 +1953,20 @@ static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)\n static int pf_interception(struct kvm_vcpu *vcpu)\n {\n \tstruct vcpu_svm *svm = to_svm(vcpu);\n-\n \tu64 fault_address = svm-\u003evmcb-\u003econtrol.exit_info_2;\n \tu64 error_code = svm-\u003evmcb-\u003econtrol.exit_info_1;\n+\tint r;\n+\n+\tr = kvm_handle_page_fault(vcpu, error_code, fault_address,\n+\t\t\t\t  static_cpu_has(X86_FEATURE_DECODEASSISTS) ?\n+\t\t\t\t  svm-\u003evmcb-\u003econtrol.insn_bytes : NULL,\n+\t\t\t\t  svm-\u003evmcb-\u003econtrol.insn_len);\n \n-\treturn kvm_handle_page_fault(vcpu, error_code, fault_address,\n-\t\t\tstatic_cpu_has(X86_FEATURE_DECODEASSISTS) ?\n-\t\t\tsvm-\u003evmcb-\u003econtrol.insn_bytes : NULL,\n-\t\t\tsvm-\u003evmcb-\u003econtrol.insn_len);\n+\tif (is_guest_mode(vcpu) \u0026\u0026 vcpu-\u003earch.exception_vmexit.pending \u0026\u0026\n+\t    vcpu-\u003earch.exception_vmexit.vector == PF_VECTOR)\n+\t\tsvm-\u003enested.vmcb02_insn_bytes_fresh = true;\n+\n+\treturn r;\n }\n \n static int svm_check_emulate_instruction(struct kvm_vcpu *vcpu, int emul_type,\n@@ -2571,7 +2577,7 @@ static bool check_selective_cr0_intercepted(struct kvm_vcpu *vcpu,\n \n \tif (cr0 ^ val) {\n \t\tsvm-\u003evmcb-\u003econtrol.exit_code = SVM_EXIT_CR0_SEL_WRITE;\n-\t\tret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);\n+\t\tret = (nested_svm_exit_handled(svm, false) == NESTED_EXIT_DONE);\n \t}\n \n \treturn ret;\n@@ -3723,7 +3729,7 @@ static int svm_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)\n \t\tvmexit = nested_svm_exit_special(svm);\n \n \t\tif (vmexit == NESTED_EXIT_CONTINUE)\n-\t\t\tvmexit = nested_svm_exit_handled(svm);\n+\t\t\tvmexit = nested_svm_exit_handled(svm, true);\n \n \t\tif (vmexit == NESTED_EXIT_DONE)\n \t\t\treturn 1;\n@@ -4820,6 +4826,52 @@ static const struct __x86_intercept {\n #undef POST_EX\n #undef POST_MEM\n \n+static void svm_prepare_decode_assist_exit_info(struct kvm_vcpu *vcpu,\n+\t\t\t\t\t\tconst struct x86_instruction_info *info)\n+{\n+\tstruct vmcb *vmcb = to_svm(vcpu)-\u003evmcb;\n+\tu64 exit_info_1;\n+\n+\tif (!guest_cpu_cap_has(vcpu, X86_FEATURE_DECODEASSISTS))\n+\t\treturn;\n+\n+\tswitch (info-\u003eintercept) {\n+\tcase x86_intercept_cr_read:\n+\tcase x86_intercept_cr_write:\n+\t\t/* MOV CRx: bit 63 set, GPR number in bits 3:0. */\n+\t\texit_info_1 = BIT_ULL(63) | (info-\u003emodrm_rm \u0026 0xf);\n+\t\tbreak;\n+\tcase x86_intercept_clts:\n+\tcase x86_intercept_lmsw:\n+\tcase x86_intercept_smsw:\n+\t\t/* CLTS/LMSW/SMSW: no decode information, bit 63 clear. */\n+\t\texit_info_1 = 0;\n+\t\tbreak;\n+\tcase x86_intercept_dr_read:\n+\tcase x86_intercept_dr_write:\n+\t\t/* MOV DRx: GPR number in bits 3:0. */\n+\t\texit_info_1 = info-\u003emodrm_rm \u0026 0xf;\n+\t\tbreak;\n+\tcase x86_intercept_intn:\n+\t\t/* INTn: software interrupt number in bits 7:0. */\n+\t\texit_info_1 = info-\u003esrc_val \u0026 0xff;\n+\t\tbreak;\n+\tcase x86_intercept_invlpg:\n+\t\t/* INVLPG: linear address of the target page. */\n+\t\texit_info_1 = info-\u003eintercept_linear_addr;\n+\t\tbreak;\n+\tcase x86_intercept_invlpga:\n+\t\t/* INVLPGA: the address remains available in guest rAX. */\n+\t\texit_info_1 = 0;\n+\t\tbreak;\n+\tdefault:\n+\t\treturn;\n+\t}\n+\n+\tvmcb-\u003econtrol.exit_info_1 = exit_info_1;\n+\tvmcb-\u003econtrol.exit_info_2 = 0;\n+}\n+\n static int svm_check_intercept(struct kvm_vcpu *vcpu,\n \t\t\t       struct x86_instruction_info *info,\n \t\t\t       enum x86_intercept_stage stage,\n@@ -4941,11 +4993,13 @@ static int svm_check_intercept(struct kvm_vcpu *vcpu,\n \t\tbreak;\n \t}\n \n+\tsvm_prepare_decode_assist_exit_info(vcpu, info);\n+\n \t/* TODO: Advertise NRIPS to guest hypervisor unconditionally */\n \tif (static_cpu_has(X86_FEATURE_NRIPS))\n \t\tvmcb-\u003econtrol.next_rip  = info-\u003enext_rip;\n \tvmcb-\u003econtrol.exit_code = icpt_info.exit_code;\n-\tvmexit = nested_svm_exit_handled(svm);\n+\tvmexit = nested_svm_exit_handled(svm, false);\n \n \tret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED\n \t\t\t\t\t   : X86EMUL_CONTINUE;\n@@ -5522,6 +5576,8 @@ static __init void svm_set_cpu_caps(void)\n \t\t */\n \t\tkvm_cpu_cap_set(X86_FEATURE_FLUSHBYASID);\n \n+\t\tkvm_cpu_cap_check_and_set(X86_FEATURE_DECODEASSISTS);\n+\n \t\tif (nrips)\n \t\t\tkvm_cpu_cap_set(X86_FEATURE_NRIPS);\n \ndiff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h\nindex a327bf751ecd5..cb7287d82107f 100644\n--- a/arch/x86/kvm/svm/svm.h\n+++ b/arch/x86/kvm/svm/svm.h\n@@ -209,6 +209,12 @@ struct vmcb_ctrl_area_cached {\n \t};\n };\n \n+struct nested_svm_insn_bytes {\n+\tbool prepared;\n+\tu8 insn_len;\n+\tu8 insn_bytes[X86_MAX_INSTRUCTION_LENGTH];\n+};\n+\n struct svm_nested_state {\n \tstruct kvm_vmcb_info vmcb02;\n \tu64 hsave_msr;\n@@ -242,6 +248,18 @@ struct svm_nested_state {\n \t * on its side.\n \t */\n \tbool force_msr_bitmap_recalc;\n+\n+\t/*\n+\t * True if VMCB02 contains DecodeAssist instruction bytes from the\n+\t * hardware VM-Exit currently being reflected to L1.\n+\t */\n+\tbool vmcb02_insn_bytes_fresh;\n+\n+\t/*\n+\t * Emulator fetch bytes captured for a synthesized nested #NPF/#PF.  The\n+\t * buffer is consumed and cleared when constructing VMCB12.\n+\t */\n+\tstruct nested_svm_insn_bytes synthesized_insn_bytes;\n };\n \n struct vcpu_sev_es_state {\n@@ -887,7 +905,7 @@ static inline void nested_svm_simple_vmexit(struct vcpu_svm *svm, u32 exit_code)\n \tnested_svm_vmexit(svm);\n }\n \n-int nested_svm_exit_handled(struct vcpu_svm *svm);\n+int nested_svm_exit_handled(struct vcpu_svm *svm, bool vmcb02_insn_bytes_fresh);\n int nested_svm_check_permissions(struct kvm_vcpu *vcpu);\n int nested_svm_check_cached_vmcb12(struct kvm_vcpu *vcpu);\n int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,\ndiff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c\nindex 0f1a829032c06..e7aff420d6c6f 100644\n--- a/arch/x86/kvm/x86.c\n+++ b/arch/x86/kvm/x86.c\n@@ -444,7 +444,8 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_deliver_exception_payload);\n \n static void kvm_queue_exception_vmexit(struct kvm_vcpu *vcpu, unsigned int vector,\n \t\t\t\t       bool has_error_code, u32 error_code,\n-\t\t\t\t       bool has_payload, unsigned long payload)\n+\t\t\t\t       bool has_payload, unsigned long payload,\n+\t\t\t\t       bool has_emulator_context)\n {\n \tstruct kvm_queued_exception *ex = \u0026vcpu-\u003earch.exception_vmexit;\n \n@@ -452,6 +453,7 @@ static void kvm_queue_exception_vmexit(struct kvm_vcpu *vcpu, unsigned int vecto\n \tex-\u003einjected = false;\n \tex-\u003epending = true;\n \tex-\u003ehas_error_code = has_error_code;\n+\tex-\u003ehas_emulator_context = has_emulator_context;\n \tex-\u003eerror_code = error_code;\n \tex-\u003ehas_payload = has_payload;\n \tex-\u003epayload = payload;\n@@ -459,7 +461,8 @@ static void kvm_queue_exception_vmexit(struct kvm_vcpu *vcpu, unsigned int vecto\n \n static void kvm_multiple_exception(struct kvm_vcpu *vcpu, unsigned int nr,\n \t\t\t\t   bool has_error, u32 error_code,\n-\t\t\t\t   bool has_payload, unsigned long payload)\n+\t\t\t\t   bool has_payload, unsigned long payload,\n+\t\t\t\t   bool has_emulator_context)\n {\n \tu32 prev_nr;\n \tint class1, class2;\n@@ -473,7 +476,8 @@ static void kvm_multiple_exception(struct kvm_vcpu *vcpu, unsigned int nr,\n \tif (is_guest_mode(vcpu) \u0026\u0026\n \t    kvm_nested_call(is_exception_vmexit)(vcpu, nr, error_code)) {\n \t\tkvm_queue_exception_vmexit(vcpu, nr, has_error, error_code,\n-\t\t\t\t\t   has_payload, payload);\n+\t\t\t\t\t   has_payload, payload,\n+\t\t\t\t\t   has_emulator_context);\n \t\treturn;\n \t}\n \n@@ -519,7 +523,7 @@ static void kvm_multiple_exception(struct kvm_vcpu *vcpu, unsigned int nr,\n \n void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)\n {\n-\tkvm_multiple_exception(vcpu, nr, false, 0, false, 0);\n+\tkvm_multiple_exception(vcpu, nr, false, 0, false, 0, false);\n }\n EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_queue_exception);\n \n@@ -527,14 +531,16 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_queue_exception);\n void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr,\n \t\t\t   unsigned long payload)\n {\n-\tkvm_multiple_exception(vcpu, nr, false, 0, true, payload);\n+\tkvm_multiple_exception(vcpu, nr, false, 0, true, payload, false);\n }\n EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_queue_exception_p);\n \n static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr,\n-\t\t\t\t    u32 error_code, unsigned long payload)\n+\t\t\t\t    u32 error_code, unsigned long payload,\n+\t\t\t\t    bool has_emulator_context)\n {\n-\tkvm_multiple_exception(vcpu, nr, true, error_code, true, payload);\n+\tkvm_multiple_exception(vcpu, nr, true, error_code, true, payload,\n+\t\t\t       has_emulator_context);\n }\n \n void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned int nr,\n@@ -580,6 +586,9 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_complete_insn_gp);\n void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault,\n \t\t\t   bool from_hardware)\n {\n+\tstruct x86_emulate_ctxt *ctxt = vcpu-\u003earch.emulate_ctxt;\n+\tbool has_emulator_context = ctxt \u0026\u0026 fault == \u0026ctxt-\u003eexception;\n+\n \t++vcpu-\u003estat.pf_guest;\n \n \t/*\n@@ -589,10 +598,12 @@ void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault,\n \tif (is_guest_mode(vcpu) \u0026\u0026 fault-\u003easync_page_fault)\n \t\tkvm_queue_exception_vmexit(vcpu, PF_VECTOR,\n \t\t\t\t\t   true, fault-\u003eerror_code,\n-\t\t\t\t\t   true, fault-\u003eaddress);\n+\t\t\t\t\t   true, fault-\u003eaddress,\n+\t\t\t\t\t   has_emulator_context);\n \telse\n \t\tkvm_queue_exception_e_p(vcpu, PF_VECTOR, fault-\u003eerror_code,\n-\t\t\t\t\tfault-\u003eaddress);\n+\t\t\t\t\tfault-\u003eaddress,\n+\t\t\t\t\thas_emulator_context);\n }\n \n void __kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu,\n@@ -627,7 +638,7 @@ void kvm_inject_nmi(struct kvm_vcpu *vcpu)\n \n void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)\n {\n-\tkvm_multiple_exception(vcpu, nr, true, error_code, false, 0);\n+\tkvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false);\n }\n EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_queue_exception_e);\n \n@@ -8949,7 +8960,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)\n \t    kvm_nested_call(is_exception_vmexit)(vcpu, ex-\u003evector, ex-\u003eerror_code)) {\n \t\tkvm_queue_exception_vmexit(vcpu, ex-\u003evector,\n \t\t\t\t\t   ex-\u003ehas_error_code, ex-\u003eerror_code,\n-\t\t\t\t\t   ex-\u003ehas_payload, ex-\u003epayload);\n+\t\t\t\t\t   ex-\u003ehas_payload, ex-\u003epayload, false);\n \t\tex-\u003einjected = false;\n \t\tex-\u003epending = false;\n \t}\ndiff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm\nindex 88c6c8046ddec..e31e3a2412aea 100644\n--- a/tools/testing/selftests/kvm/Makefile.kvm\n+++ b/tools/testing/selftests/kvm/Makefile.kvm\n@@ -116,6 +116,7 @@ TEST_GEN_PROGS_x86 += x86/vmx_preemption_timer_test\n TEST_GEN_PROGS_x86 += x86/svm_vmcall_test\n TEST_GEN_PROGS_x86 += x86/svm_int_ctl_test\n TEST_GEN_PROGS_x86 += x86/svm_nested_clear_efer_svme\n+TEST_GEN_PROGS_x86 += x86/svm_nested_decode_assists_test\n TEST_GEN_PROGS_x86 += x86/svm_nested_shutdown_test\n TEST_GEN_PROGS_x86 += x86/svm_nested_soft_inject_test\n TEST_GEN_PROGS_x86 += x86/svm_nested_vmcb12_gpa\ndiff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/testing/selftests/kvm/include/x86/processor.h\nindex b161174ece453..42e009af525e7 100644\n--- a/tools/testing/selftests/kvm/include/x86/processor.h\n+++ b/tools/testing/selftests/kvm/include/x86/processor.h\n@@ -219,6 +219,7 @@ struct kvm_x86_cpu_feature {\n #define\tX86_FEATURE_LBRV\t\tKVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 1)\n #define\tX86_FEATURE_NRIPS\t\tKVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 3)\n #define X86_FEATURE_TSCRATEMSR          KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 4)\n+#define X86_FEATURE_DECODEASSISTS       KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 7)\n #define X86_FEATURE_PAUSEFILTER         KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 10)\n #define X86_FEATURE_PFTHRESHOLD         KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 12)\n #define\tX86_FEATURE_V_VMSAVE_VMLOAD\tKVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 15)\ndiff --git a/tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c b/tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c\nnew file mode 100644\nindex 0000000000000..13511ca8473b2\n--- /dev/null\n+++ b/tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c\n@@ -0,0 +1,791 @@\n+// SPDX-License-Identifier: GPL-2.0-only\n+/*\n+ * Test KVM's virtualization of SVM DecodeAssists for nested guests.\n+ */\n+\n+#include \"test_util.h\"\n+#include \"kvm_util.h\"\n+#include \"processor.h\"\n+#include \"svm_util.h\"\n+\n+#define TEST_INT_VECTOR 0x81\n+\n+/* Any canonical virtual address that is never mapped by the selftest VM. */\n+#define PF_TEST_GVA BIT_ULL(40)\n+#define PF_FETCH_TEST_GVA BIT_ULL(41)\n+\n+#define OUTSB_OPCODE 0x6e\n+#define BOUNDARY_OUTSB_CODE_SIZE 15\n+#define LINEAR_WRAP_CODE_GVA (BIT_ULL(32) - PAGE_SIZE)\n+#define LINEAR_WRAP_OUTSB_OFFSET (PAGE_SIZE - 8)\n+#define LINEAR_WRAP_SETUP_SIZE 9\n+#define LINEAR_WRAP_SETUP_OFFSET \\\n+\t(LINEAR_WRAP_OUTSB_OFFSET - LINEAR_WRAP_SETUP_SIZE)\n+#define LINEAR_WRAP_CS_BASE PAGE_SIZE\n+#define TEST_IOPM_SIZE (3 * PAGE_SIZE)\n+\n+static u8 npf_target[PAGE_SIZE] __aligned(PAGE_SIZE);\n+static u8 mmio_source __aligned(PAGE_SIZE);\n+static u8 boundary_outsb_code[2 * PAGE_SIZE] __aligned(PAGE_SIZE);\n+static const u8 linear_wrap_insn_bytes[15] = {\n+\tOUTSB_OPCODE, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,\n+\t0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,\n+};\n+\n+static void l2_read_code(void)\n+{\n+\tasm volatile(\"mov (%0), %%rax\" : : \"r\"(\u0026npf_target) : \"rax\", \"memory\");\n+\tGUEST_FAIL(\"L2 read did not cause a nested page fault\");\n+}\n+\n+static void l2_outsb_code(void)\n+{\n+\tasm volatile(\"mov %0, %%rsi\\n\\t\"\n+\t\t     \"mov $0x80, %%dx\\n\\t\"\n+\t\t     \"outsb\"\n+\t\t     : : \"r\"(\u0026npf_target) : \"rsi\", \"rdx\", \"memory\");\n+\tGUEST_FAIL(\"L2 OUTSB did not cause a nested page fault\");\n+}\n+\n+static void l2_movsb_code(void)\n+{\n+\tasm volatile(\"mov %0, %%rsi\\n\\t\"\n+\t\t     \"mov %1, %%rdi\\n\\t\"\n+\t\t     \"movsb\"\n+\t\t     : : \"r\"(\u0026mmio_source), \"r\"(\u0026npf_target)\n+\t\t     : \"rsi\", \"rdi\", \"memory\");\n+\tGUEST_FAIL(\"L2 MOVSB did not cause a nested page fault\");\n+}\n+\n+static void l2_pf_code(void)\n+{\n+\tasm volatile(\"mov (%0), %%rax\"\n+\t\t     : : \"r\"(PF_TEST_GVA) : \"rax\", \"memory\");\n+\tGUEST_FAIL(\"L2 access to an unmapped VA did not #PF\");\n+}\n+\n+static void l2_fep_pf_code(void)\n+{\n+\tasm volatile(KVM_FEP \"mov (%0), %%rax\"\n+\t\t     : : \"r\"(PF_TEST_GVA) : \"rax\", \"memory\");\n+\tGUEST_FAIL(\"L2 forced-emulated access to an unmapped VA did not #PF\");\n+}\n+\n+static void l2_stale_emulator_pf_code(void)\n+{\n+\tasm volatile(\"movb (%0), %%al\\n\\t\"\n+\t\t     \"nop\"\n+\t\t     : : \"r\"(\u0026mmio_source) : \"rax\", \"memory\");\n+\tGUEST_FAIL(\"Userspace-injected #PF was not intercepted by L1\");\n+}\n+\n+static void l2_mov_from_cr4_code(void)\n+{\n+\tasm volatile(\"mov %%cr4, %%r10\" : : : \"r10\");\n+\tGUEST_FAIL(\"L2 MOV-from-CR4 was not intercepted\");\n+}\n+\n+static void l2_fep_mov_from_cr4_code(void)\n+{\n+\tasm volatile(KVM_FEP \"mov %%cr4, %%r10\" : : : \"r10\");\n+\tGUEST_FAIL(\"L2 forced-emulated MOV-from-CR4 was not intercepted\");\n+}\n+\n+static void l2_mov_to_cr4_code(void)\n+{\n+\tasm volatile(\"mov %%cr4, %%rax\\n\\t\"\n+\t\t     \"mov %%rax, %%cr4\" : : : \"rax\");\n+\tGUEST_FAIL(\"L2 MOV-to-CR4 was not intercepted\");\n+}\n+\n+static void l2_fep_mov_to_cr4_code(void)\n+{\n+\tasm volatile(\"mov %%cr4, %%rax\\n\\t\"\n+\t\t     KVM_FEP \"mov %%rax, %%cr4\" : : : \"rax\");\n+\tGUEST_FAIL(\"L2 forced-emulated MOV-to-CR4 was not intercepted\");\n+}\n+\n+static void l2_mov_to_dr7_code(void)\n+{\n+\tasm volatile(\"mov %%dr7, %%rax\\n\\t\"\n+\t\t     \"mov %%rax, %%rbx\\n\\t\"\n+\t\t     \"mov %%rbx, %%dr7\" : : : \"rax\", \"rbx\");\n+\tGUEST_FAIL(\"L2 MOV-to-DR7 was not intercepted\");\n+}\n+\n+static void l2_fep_mov_to_dr7_code(void)\n+{\n+\tasm volatile(\"mov %%dr7, %%rax\\n\\t\"\n+\t\t     \"mov %%rax, %%rbx\\n\\t\"\n+\t\t     KVM_FEP \"mov %%rbx, %%dr7\" : : : \"rax\", \"rbx\");\n+\tGUEST_FAIL(\"L2 forced-emulated MOV-to-DR7 was not intercepted\");\n+}\n+\n+static void l2_mov_from_dr7_code(void)\n+{\n+\tasm volatile(\"mov %%dr7, %%r10\" : : : \"r10\");\n+\tGUEST_FAIL(\"L2 MOV-from-DR7 was not intercepted\");\n+}\n+\n+static void l2_fep_mov_from_dr7_code(void)\n+{\n+\tasm volatile(KVM_FEP \"mov %%dr7, %%r10\" : : : \"r10\");\n+\tGUEST_FAIL(\"L2 forced-emulated MOV-from-DR7 was not intercepted\");\n+}\n+\n+static void l2_clts_code(void)\n+{\n+\tasm volatile(\"clts\" : : : \"memory\");\n+\tGUEST_FAIL(\"L2 CLTS was not intercepted\");\n+}\n+\n+static void l2_fep_clts_code(void)\n+{\n+\tasm volatile(KVM_FEP \"clts\" : : : \"memory\");\n+\tGUEST_FAIL(\"L2 forced-emulated CLTS was not intercepted\");\n+}\n+\n+static void l2_lmsw_code(void)\n+{\n+\tasm volatile(\"smsw %%ax\\n\\t\"\n+\t\t     \"lmsw %%ax\" : : : \"rax\", \"memory\");\n+\tGUEST_FAIL(\"L2 LMSW was not intercepted\");\n+}\n+\n+static void l2_fep_lmsw_code(void)\n+{\n+\tasm volatile(\"smsw %%ax\\n\\t\"\n+\t\t     KVM_FEP \"lmsw %%ax\" : : : \"rax\", \"memory\");\n+\tGUEST_FAIL(\"L2 forced-emulated LMSW was not intercepted\");\n+}\n+\n+static void l2_smsw_code(void)\n+{\n+\tasm volatile(\"smsw %%ax\" : : : \"rax\", \"memory\");\n+\tGUEST_FAIL(\"L2 SMSW was not intercepted\");\n+}\n+\n+static void l2_fep_smsw_code(void)\n+{\n+\tasm volatile(KVM_FEP \"smsw %%ax\" : : : \"rax\", \"memory\");\n+\tGUEST_FAIL(\"L2 forced-emulated SMSW was not intercepted\");\n+}\n+\n+static void l2_int_code(void)\n+{\n+\tasm volatile(\"int %0\" : : \"i\"(TEST_INT_VECTOR));\n+\tGUEST_FAIL(\"L2 INTn was not intercepted\");\n+}\n+\n+static void l2_fep_int_code(void)\n+{\n+\tasm volatile(KVM_FEP \"int %0\" : : \"i\"(TEST_INT_VECTOR));\n+\tGUEST_FAIL(\"L2 forced-emulated INTn was not intercepted\");\n+}\n+\n+static void l2_invlpg_code(void)\n+{\n+\tasm volatile(\"invlpg (%0)\" : : \"r\"(\u0026npf_target) : \"memory\");\n+\tGUEST_FAIL(\"L2 INVLPG was not intercepted\");\n+}\n+\n+static void l2_fep_invlpg_code(void)\n+{\n+\tasm volatile(KVM_FEP \"invlpg (%0)\" : : \"r\"(\u0026npf_target) : \"memory\");\n+\tGUEST_FAIL(\"L2 forced-emulated INVLPG was not intercepted\");\n+}\n+\n+static void l2_invlpga_code(void)\n+{\n+\tasm volatile(\"invlpga\" : : \"a\"(\u0026npf_target), \"c\"(0) : \"memory\");\n+\tGUEST_FAIL(\"L2 INVLPGA was not intercepted\");\n+}\n+\n+static void l2_fep_invlpga_code(void)\n+{\n+\tasm volatile(KVM_FEP \"invlpga\"\n+\t\t     : : \"a\"(\u0026npf_target), \"c\"(0) : \"memory\");\n+\tGUEST_FAIL(\"L2 forced-emulated INVLPGA was not intercepted\");\n+}\n+\n+struct instruction_intercept_test {\n+\tconst char *name;\n+\tvoid (*code)(void);\n+\tvoid (*fep_code)(void);\n+\tu64 intercept;\n+\tu32 intercept_cr;\n+\tu32 intercept_dr;\n+\tu64 exit_code;\n+\tu64 exit_info_1;\n+\tu64 exit_info_1_mask;\n+\tbool check_rax;\n+\tu64 rax;\n+};\n+\n+static const struct instruction_intercept_test instruction_intercept_tests[] = {\n+\t{\n+\t\t.name = \"MOV-to-CR4\",\n+\t\t.code = l2_mov_to_cr4_code,\n+\t\t.fep_code = l2_fep_mov_to_cr4_code,\n+\t\t.intercept_cr = BIT(INTERCEPT_CR4_WRITE),\n+\t\t.exit_code = SVM_EXIT_WRITE_CR4,\n+\t\t.exit_info_1 = BIT_ULL(63),\n+\t\t.exit_info_1_mask = ~0ULL,\n+\t}, {\n+\t\t.name = \"MOV-from-CR4\",\n+\t\t.code = l2_mov_from_cr4_code,\n+\t\t.fep_code = l2_fep_mov_from_cr4_code,\n+\t\t.intercept_cr = BIT(INTERCEPT_CR4_READ),\n+\t\t.exit_code = SVM_EXIT_READ_CR4,\n+\t\t.exit_info_1 = BIT_ULL(63) | 10,\n+\t\t.exit_info_1_mask = ~0ULL,\n+\t}, {\n+\t\t.name = \"MOV-to-DR7\",\n+\t\t.code = l2_mov_to_dr7_code,\n+\t\t.fep_code = l2_fep_mov_to_dr7_code,\n+\t\t.intercept_dr = BIT(INTERCEPT_DR7_WRITE),\n+\t\t.exit_code = SVM_EXIT_WRITE_DR7,\n+\t\t.exit_info_1 = 3,\n+\t\t.exit_info_1_mask = ~0ULL,\n+\t}, {\n+\t\t.name = \"MOV-from-DR7\",\n+\t\t.code = l2_mov_from_dr7_code,\n+\t\t.fep_code = l2_fep_mov_from_dr7_code,\n+\t\t.intercept_dr = BIT(INTERCEPT_DR7_READ),\n+\t\t.exit_code = SVM_EXIT_READ_DR7,\n+\t\t.exit_info_1 = 10,\n+\t\t.exit_info_1_mask = ~0ULL,\n+\t}, {\n+\t\t.name = \"CLTS\",\n+\t\t.code = l2_clts_code,\n+\t\t.fep_code = l2_fep_clts_code,\n+\t\t.intercept_cr = BIT(INTERCEPT_CR0_WRITE),\n+\t\t.exit_code = SVM_EXIT_WRITE_CR0,\n+\t\t.exit_info_1_mask = BIT_ULL(63),\n+\t}, {\n+\t\t.name = \"LMSW\",\n+\t\t.code = l2_lmsw_code,\n+\t\t.fep_code = l2_fep_lmsw_code,\n+\t\t.intercept_cr = BIT(INTERCEPT_CR0_WRITE),\n+\t\t.exit_code = SVM_EXIT_WRITE_CR0,\n+\t\t.exit_info_1_mask = BIT_ULL(63),\n+\t}, {\n+\t\t.name = \"SMSW\",\n+\t\t.code = l2_smsw_code,\n+\t\t.fep_code = l2_fep_smsw_code,\n+\t\t.intercept_cr = BIT(INTERCEPT_CR0_READ),\n+\t\t.exit_code = SVM_EXIT_READ_CR0,\n+\t\t.exit_info_1_mask = BIT_ULL(63),\n+\t}, {\n+\t\t.name = \"INTn\",\n+\t\t.code = l2_int_code,\n+\t\t.fep_code = l2_fep_int_code,\n+\t\t.intercept = BIT_ULL(INTERCEPT_INTn),\n+\t\t.exit_code = SVM_EXIT_SWINT,\n+\t\t.exit_info_1 = TEST_INT_VECTOR,\n+\t\t.exit_info_1_mask = ~0ULL,\n+\t}, {\n+\t\t.name = \"INVLPG\",\n+\t\t.code = l2_invlpg_code,\n+\t\t.fep_code = l2_fep_invlpg_code,\n+\t\t.intercept = BIT_ULL(INTERCEPT_INVLPG),\n+\t\t.exit_code = SVM_EXIT_INVLPG,\n+\t\t.exit_info_1 = (u64)\u0026npf_target,\n+\t\t.exit_info_1_mask = ~0ULL,\n+\t}, {\n+\t\t.name = \"INVLPGA\",\n+\t\t.code = l2_invlpga_code,\n+\t\t.fep_code = l2_fep_invlpga_code,\n+\t\t.intercept = BIT_ULL(INTERCEPT_INVLPGA),\n+\t\t.exit_code = SVM_EXIT_INVLPGA,\n+\t\t.exit_info_1_mask = ~0ULL,\n+\t\t.check_rax = true,\n+\t\t.rax = (u64)\u0026npf_target,\n+\t},\n+};\n+\n+static void assert_decode_assist_insn_bytes(struct vmcb *vmcb)\n+{\n+\tGUEST_ASSERT(vmcb-\u003econtrol.insn_len);\n+\tGUEST_ASSERT(vmcb-\u003econtrol.insn_len \u003c=\n+\t\t     sizeof(vmcb-\u003econtrol.insn_bytes));\n+\tGUEST_ASSERT(!memcmp(vmcb-\u003econtrol.insn_bytes,\n+\t\t\t     (void *)vmcb-\u003esave.rip,\n+\t\t\t     vmcb-\u003econtrol.insn_len));\n+}\n+\n+static void assert_full_decode_assist_insn_bytes(struct vmcb *vmcb)\n+{\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.insn_len,\n+\t\t\tsizeof(vmcb-\u003econtrol.insn_bytes));\n+\tassert_decode_assist_insn_bytes(vmcb);\n+}\n+\n+static void prepare_l2_for_vmrun(struct svm_test_data *svm, gva_t rip)\n+{\n+\tstruct vmcb *vmcb = svm-\u003evmcb;\n+\n+\tvmcb-\u003esave.rip = rip;\n+\tvmcb-\u003esave.rsp = (u64)svm-\u003estack;\n+}\n+\n+static void run_intercept_test(struct svm_test_data *svm,\n+\t\t\t       const struct instruction_intercept_test *test,\n+\t\t\t       bool synthesized)\n+{\n+\tstruct vmcb *vmcb = svm-\u003evmcb;\n+\tstruct vmcb_control_area *control = \u0026vmcb-\u003econtrol;\n+\tconst char *source = synthesized ? \"synthesized\" : \"hardware\";\n+\tu64 expected_exit_info_1 = test-\u003eexit_info_1 \u0026 test-\u003eexit_info_1_mask;\n+\n+\tcontrol-\u003eintercept |= test-\u003eintercept;\n+\tcontrol-\u003eintercept_cr |= test-\u003eintercept_cr;\n+\tcontrol-\u003eintercept_dr |= test-\u003eintercept_dr;\n+\n+\tif (synthesized) {\n+\t\tcontrol-\u003eexit_info_1 = ~0ULL;\n+\t\tcontrol-\u003eexit_info_2 = ~0ULL;\n+\t\tprepare_l2_for_vmrun(svm, (u64)test-\u003efep_code);\n+\t} else {\n+\t\tprepare_l2_for_vmrun(svm, (u64)test-\u003ecode);\n+\t}\n+\n+\trun_guest(vmcb, svm-\u003evmcb_gpa);\n+\n+\t__GUEST_ASSERT(control-\u003eexit_code == test-\u003eexit_code,\n+\t\t       \"%s (%s): expected exit code %#lx, got %#lx\",\n+\t\t       test-\u003ename, source, (unsigned long)test-\u003eexit_code,\n+\t\t       (unsigned long)control-\u003eexit_code);\n+\t__GUEST_ASSERT((control-\u003eexit_info_1 \u0026 test-\u003eexit_info_1_mask) ==\n+\t\t       expected_exit_info_1,\n+\t\t       \"%s (%s): expected EXITINFO1 %#lx with mask %#lx, got %#lx\",\n+\t\t       test-\u003ename, source, (unsigned long)expected_exit_info_1,\n+\t\t       (unsigned long)test-\u003eexit_info_1_mask,\n+\t\t       (unsigned long)control-\u003eexit_info_1);\n+\t__GUEST_ASSERT(!control-\u003einsn_len,\n+\t\t       \"%s (%s): expected no instruction bytes, got %u\",\n+\t\t       test-\u003ename, source, control-\u003einsn_len);\n+\n+\tif (test-\u003echeck_rax)\n+\t\t__GUEST_ASSERT(vmcb-\u003esave.rax == test-\u003erax,\n+\t\t\t       \"%s (%s): expected rAX %#lx, got %#lx\",\n+\t\t\t       test-\u003ename, source, (unsigned long)test-\u003erax,\n+\t\t\t       (unsigned long)vmcb-\u003esave.rax);\n+\n+\tif (synthesized)\n+\t\t__GUEST_ASSERT(!control-\u003eexit_info_2,\n+\t\t\t       \"%s (%s): expected EXITINFO2 to be clear, got %#lx\",\n+\t\t\t       test-\u003ename, source,\n+\t\t\t       (unsigned long)control-\u003eexit_info_2);\n+\n+\tcontrol-\u003eintercept \u0026= ~test-\u003eintercept;\n+\tcontrol-\u003eintercept_cr \u0026= ~test-\u003eintercept_cr;\n+\tcontrol-\u003eintercept_dr \u0026= ~test-\u003eintercept_dr;\n+}\n+\n+static void test_instruction_intercepts(struct svm_test_data *svm)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i \u003c ARRAY_SIZE(instruction_intercept_tests); i++) {\n+\t\trun_intercept_test(svm, \u0026instruction_intercept_tests[i], false);\n+\n+\t\tif (is_forced_emulation_enabled)\n+\t\t\trun_intercept_test(svm, \u0026instruction_intercept_tests[i],\n+\t\t\t\t\t   true);\n+\t}\n+}\n+\n+static void test_hardware_npf(struct svm_test_data *svm, gpa_t npf_gpa)\n+{\n+\tstruct vmcb *vmcb = svm-\u003evmcb;\n+\n+\tprepare_l2_for_vmrun(svm, (u64)l2_read_code);\n+\trun_guest(vmcb, svm-\u003evmcb_gpa);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.exit_code, SVM_EXIT_NPF);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.exit_info_2, npf_gpa);\n+\tassert_decode_assist_insn_bytes(vmcb);\n+}\n+\n+/*\n+ * The IOIO intercept causes L0 to emulate OUTSB before accessing its source\n+ * operand.  The emulated read then faults on L1's NPT, resulting in a\n+ * KVM-synthesized #NPF.\n+ */\n+static void test_synthesized_npf(struct svm_test_data *svm, gpa_t npf_gpa)\n+{\n+\tstruct vmcb *vmcb = svm-\u003evmcb;\n+\n+\tprepare_l2_for_vmrun(svm, (u64)l2_outsb_code);\n+\trun_guest(vmcb, svm-\u003evmcb_gpa);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.exit_code, SVM_EXIT_NPF);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.exit_info_2, npf_gpa);\n+\tassert_full_decode_assist_insn_bytes(vmcb);\n+}\n+\n+/*\n+ * MOVSB first reads from MMIO, causing a hardware #NPF that L0 emulates.\n+ * After userspace completes the read, the emulated destination write faults\n+ * on L1's NPT.  The new #NPF must not reuse the original hardware exit's GPA.\n+ */\n+static void test_synthesized_npf_after_hardware_npf(struct svm_test_data *svm,\n+\t\t\t\t\t\t    gpa_t npf_gpa)\n+{\n+\tstruct vmcb *vmcb = svm-\u003evmcb;\n+\n+\tprepare_l2_for_vmrun(svm, (u64)l2_movsb_code);\n+\trun_guest(vmcb, svm-\u003evmcb_gpa);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.exit_code, SVM_EXIT_NPF);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.exit_info_2, npf_gpa);\n+\tassert_full_decode_assist_insn_bytes(vmcb);\n+}\n+\n+/*\n+ * OUTSB is the final byte of a mapped code page, and the following page is\n+ * not present in L2's page tables.  DecodeAssist byte fetching must stop at\n+ * the page boundary and report only the OUTSB opcode.\n+ */\n+static void test_synthesized_npf_truncated(struct svm_test_data *svm,\n+\t\t\t\t\t   gpa_t npf_gpa)\n+{\n+\tstruct vmcb *vmcb = svm-\u003evmcb;\n+\n+\tprepare_l2_for_vmrun(svm,\n+\t\t\t     (u64)\u0026boundary_outsb_code[PAGE_SIZE -\n+\t\t\t\t\t\t       BOUNDARY_OUTSB_CODE_SIZE]);\n+\trun_guest(vmcb, svm-\u003evmcb_gpa);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.exit_code, SVM_EXIT_NPF);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.exit_info_2, npf_gpa);\n+\tGUEST_ASSERT_EQ(vmcb-\u003esave.rip,\n+\t\t\t(u64)\u0026boundary_outsb_code[PAGE_SIZE - 1]);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.insn_len, 1);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.insn_bytes[0], OUTSB_OPCODE);\n+}\n+\n+/*\n+ * OUTSB is the final byte in the lower canonical address range.  The first\n+ * byte after OUTSB is non-canonical, and must terminate DecodeAssist fetching\n+ * even though the corresponding high canonical address is mapped.\n+ */\n+static void test_synthesized_npf_canonical_boundary(struct svm_test_data *svm,\n+\t\t\t\t\t\t    gpa_t npf_gpa,\n+\t\t\t\t\t\t    gva_t code_gva)\n+{\n+\tstruct vmcb *vmcb = svm-\u003evmcb;\n+\n+\tprepare_l2_for_vmrun(svm, code_gva + PAGE_SIZE -\n+\t\t\t\t  BOUNDARY_OUTSB_CODE_SIZE);\n+\trun_guest(vmcb, svm-\u003evmcb_gpa);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.exit_code, SVM_EXIT_NPF);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.exit_info_2, npf_gpa);\n+\tGUEST_ASSERT_EQ(vmcb-\u003esave.rip, code_gva + PAGE_SIZE - 1);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.insn_len, 1);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.insn_bytes[0], OUTSB_OPCODE);\n+}\n+\n+/*\n+ * A nonzero CS.base makes the DecodeAssist byte window cross the 32-bit\n+ * linear-address boundary without crossing the CS limit.\n+ */\n+static void test_synthesized_npf_linear_wrap(struct svm_test_data *svm,\n+\t\t\t\t\t     gpa_t npf_gpa)\n+{\n+\tstruct vmcb *vmcb = svm-\u003evmcb;\n+\tu16 cs_attrib = vmcb-\u003esave.cs.attrib;\n+\tu64 cs_base = vmcb-\u003esave.cs.base;\n+\tu32 cs_limit = vmcb-\u003esave.cs.limit;\n+\tu32 outsb_eip = LINEAR_WRAP_CODE_GVA + LINEAR_WRAP_OUTSB_OFFSET -\n+\t\t\tLINEAR_WRAP_CS_BASE;\n+\tu32 setup_eip = LINEAR_WRAP_CODE_GVA + LINEAR_WRAP_SETUP_OFFSET -\n+\t\t\tLINEAR_WRAP_CS_BASE;\n+\n+\tvmcb-\u003esave.cs.attrib \u0026= ~SVM_SELECTOR_L_MASK;\n+\tvmcb-\u003esave.cs.attrib |= SVM_SELECTOR_DB_MASK;\n+\tvmcb-\u003esave.cs.base = LINEAR_WRAP_CS_BASE;\n+\tvmcb-\u003esave.cs.limit = UINT32_MAX;\n+\tprepare_l2_for_vmrun(svm, setup_eip);\n+\trun_guest(vmcb, svm-\u003evmcb_gpa);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.exit_code, SVM_EXIT_NPF);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.exit_info_2, npf_gpa);\n+\tGUEST_ASSERT_EQ(vmcb-\u003esave.rip, outsb_eip);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.insn_len,\n+\t\t\tsizeof(linear_wrap_insn_bytes));\n+\tGUEST_ASSERT(!memcmp(vmcb-\u003econtrol.insn_bytes,\n+\t\t\t     linear_wrap_insn_bytes,\n+\t\t\t     sizeof(linear_wrap_insn_bytes)));\n+\tvmcb-\u003esave.cs.attrib = cs_attrib;\n+\tvmcb-\u003esave.cs.base = cs_base;\n+\tvmcb-\u003esave.cs.limit = cs_limit;\n+}\n+\n+static void test_hardware_intercepted_pf(struct svm_test_data *svm)\n+{\n+\tstruct vmcb *vmcb = svm-\u003evmcb;\n+\n+\tprepare_l2_for_vmrun(svm, (u64)l2_pf_code);\n+\trun_guest(vmcb, svm-\u003evmcb_gpa);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.exit_code, SVM_EXIT_EXCP_BASE + PF_VECTOR);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.exit_info_2, PF_TEST_GVA);\n+\tGUEST_ASSERT(!(vmcb-\u003econtrol.exit_info_1 \u0026 PFERR_PRESENT_MASK));\n+\tGUEST_ASSERT(!(vmcb-\u003econtrol.exit_info_1 \u0026 PFERR_FETCH_MASK));\n+\tassert_decode_assist_insn_bytes(vmcb);\n+}\n+\n+static void test_hardware_intercepted_fetch_pf(struct svm_test_data *svm)\n+{\n+\tstruct vmcb *vmcb = svm-\u003evmcb;\n+\n+\tprepare_l2_for_vmrun(svm, PF_FETCH_TEST_GVA);\n+\trun_guest(vmcb, svm-\u003evmcb_gpa);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.exit_code, SVM_EXIT_EXCP_BASE + PF_VECTOR);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.exit_info_2, PF_FETCH_TEST_GVA);\n+\tGUEST_ASSERT(!(vmcb-\u003econtrol.exit_info_1 \u0026 PFERR_PRESENT_MASK));\n+\tGUEST_ASSERT(vmcb-\u003econtrol.exit_info_1 \u0026 PFERR_FETCH_MASK);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.insn_len, 0);\n+}\n+\n+static void test_synthesized_pf(struct svm_test_data *svm)\n+{\n+\tstruct vmcb *vmcb = svm-\u003evmcb;\n+\n+\tif (!is_forced_emulation_enabled)\n+\t\treturn;\n+\n+\tprepare_l2_for_vmrun(svm, (u64)l2_fep_pf_code);\n+\trun_guest(vmcb, svm-\u003evmcb_gpa);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.exit_code, SVM_EXIT_EXCP_BASE + PF_VECTOR);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.exit_info_2, PF_TEST_GVA);\n+\tGUEST_ASSERT(!(vmcb-\u003econtrol.exit_info_1 \u0026 PFERR_PRESENT_MASK));\n+\tGUEST_ASSERT(!(vmcb-\u003econtrol.exit_info_1 \u0026 PFERR_FETCH_MASK));\n+\tassert_full_decode_assist_insn_bytes(vmcb);\n+}\n+\n+/*\n+ * Inject #PF while an emulated MMIO read is awaiting completion.  KVM\n+ * completes the instruction before constructing the nested VM-Exit, so the\n+ * old emulator fetch cache must not be reported for the following instruction.\n+ */\n+static void test_userspace_injected_pf_during_emulation(struct svm_test_data *svm)\n+{\n+\tstruct vmcb *vmcb = svm-\u003evmcb;\n+\n+\tprepare_l2_for_vmrun(svm, (u64)l2_stale_emulator_pf_code);\n+\trun_guest(vmcb, svm-\u003evmcb_gpa);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.exit_code, SVM_EXIT_EXCP_BASE + PF_VECTOR);\n+\tGUEST_ASSERT_EQ(vmcb-\u003econtrol.exit_info_2, PF_TEST_GVA);\n+\tGUEST_ASSERT(!(vmcb-\u003econtrol.exit_info_1 \u0026 PFERR_FETCH_MASK));\n+\tassert_full_decode_assist_insn_bytes(vmcb);\n+}\n+\n+static void l1_guest_code(struct svm_test_data *svm, gpa_t npf_gpa,\n+\t\t\t  gpa_t iopm_gpa, gva_t canonical_code_gva)\n+{\n+\tstruct vmcb *vmcb = svm-\u003evmcb;\n+\n+\tGUEST_ASSERT(this_cpu_has(X86_FEATURE_DECODEASSISTS));\n+\n+\tgeneric_svm_setup(svm, l2_read_code);\n+\tvmcb-\u003econtrol.iopm_base_pa = iopm_gpa;\n+\n+\tvmcb-\u003econtrol.intercept |= BIT_ULL(INTERCEPT_IOIO_PROT);\n+\tvmcb-\u003econtrol.intercept_exceptions |= 1U \u003c\u003c PF_VECTOR;\n+\n+\ttest_hardware_npf(svm, npf_gpa);\n+\ttest_synthesized_npf(svm, npf_gpa);\n+\ttest_synthesized_npf_after_hardware_npf(svm, npf_gpa);\n+\ttest_synthesized_npf_truncated(svm, npf_gpa);\n+\ttest_synthesized_npf_canonical_boundary(svm, npf_gpa,\n+\t\t\t\t\t\tcanonical_code_gva);\n+\ttest_synthesized_npf_linear_wrap(svm, npf_gpa);\n+\ttest_hardware_intercepted_pf(svm);\n+\ttest_hardware_intercepted_fetch_pf(svm);\n+\ttest_synthesized_pf(svm);\n+\ttest_userspace_injected_pf_during_emulation(svm);\n+\ttest_instruction_intercepts(svm);\n+\n+\tGUEST_DONE();\n+}\n+\n+static void build_boundary_outsb_code(u8 *code)\n+{\n+\tu64 source = (u64)\u0026npf_target;\n+\n+\t/* movabs $npf_target, %rsi */\n+\tcode[0] = 0x48;\n+\tcode[1] = 0xbe;\n+\tmemcpy(\u0026code[2], \u0026source, sizeof(source));\n+\n+\t/* mov $0x80, %dx; outsb */\n+\tcode[10] = 0x66;\n+\tcode[11] = 0xba;\n+\tcode[12] = 0x80;\n+\tcode[13] = 0x00;\n+\tcode[14] = OUTSB_OPCODE;\n+}\n+\n+static void prepare_boundary_outsb_code(struct kvm_vm *vm)\n+{\n+\tgva_t code_gva = (gva_t)\u0026boundary_outsb_code[PAGE_SIZE -\n+\t\t\t\t\t\t      BOUNDARY_OUTSB_CODE_SIZE];\n+\n+\tbuild_boundary_outsb_code(addr_gva2hva(vm, code_gva));\n+}\n+\n+static gva_t prepare_canonical_boundary_outsb_code(struct kvm_vm *vm)\n+{\n+\tgva_t backing_gva = vm_alloc_pages(vm, 2);\n+\tu8 *code_page = addr_gva2hva(vm, backing_gva);\n+\tu8 *alias_page = addr_gva2hva(vm, backing_gva + PAGE_SIZE);\n+\tgpa_t code_gpa = addr_gva2gpa(vm, backing_gva);\n+\tgpa_t alias_gpa = addr_gva2gpa(vm, backing_gva + PAGE_SIZE);\n+\tgva_t code_gva = BIT_ULL(vm-\u003eva_bits - 1) - PAGE_SIZE;\n+\tgva_t alias_gva = ~(BIT_ULL(vm-\u003eva_bits - 1) - 1);\n+\tu8 *code = \u0026code_page[PAGE_SIZE - BOUNDARY_OUTSB_CODE_SIZE];\n+\n+\tbuild_boundary_outsb_code(code);\n+\tmemset(alias_page, 0xcc, PAGE_SIZE);\n+\tvirt_map(vm, code_gva, code_gpa, 1);\n+\tvirt_map(vm, alias_gva, alias_gpa, 1);\n+\n+\treturn code_gva;\n+}\n+\n+static void prepare_linear_wrap_outsb_code(struct kvm_vm *vm)\n+{\n+\tgva_t code_gva = vm_alloc_pages(vm, 2);\n+\tu8 *high_page = addr_gva2hva(vm, code_gva);\n+\tu8 *low_page = addr_gva2hva(vm, code_gva + PAGE_SIZE);\n+\tgpa_t high_gpa = addr_gva2gpa(vm, code_gva);\n+\tgpa_t low_gpa = addr_gva2gpa(vm, code_gva + PAGE_SIZE);\n+\tu32 source = (u32)(u64)\u0026npf_target;\n+\tu8 *setup = \u0026high_page[LINEAR_WRAP_SETUP_OFFSET];\n+\n+\tTEST_ASSERT((u64)\u0026npf_target \u003c= UINT32_MAX,\n+\t\t    \"npf_target must be addressable from compatibility mode\");\n+\n+\t/* mov $npf_target, %esi; mov $0x80, %dx */\n+\tsetup[0] = 0xbe;\n+\tmemcpy(\u0026setup[1], \u0026source, sizeof(source));\n+\tsetup[5] = 0x66;\n+\tsetup[6] = 0xba;\n+\tsetup[7] = 0x80;\n+\tsetup[8] = 0x00;\n+\n+\tmemcpy(\u0026high_page[LINEAR_WRAP_OUTSB_OFFSET],\n+\t       linear_wrap_insn_bytes,\n+\t       PAGE_SIZE - LINEAR_WRAP_OUTSB_OFFSET);\n+\tmemcpy(low_page,\n+\t       \u0026linear_wrap_insn_bytes[PAGE_SIZE - LINEAR_WRAP_OUTSB_OFFSET],\n+\t       sizeof(linear_wrap_insn_bytes) -\n+\t       (PAGE_SIZE - LINEAR_WRAP_OUTSB_OFFSET));\n+\n+\tvirt_map(vm, LINEAR_WRAP_CODE_GVA, high_gpa, 1);\n+\tvirt_map(vm, 0, low_gpa, 1);\n+}\n+\n+static void queue_userspace_pf(struct kvm_vcpu *vcpu)\n+{\n+\tstruct kvm_vcpu_events events;\n+\n+\tvcpu_events_get(vcpu, \u0026events);\n+\tTEST_ASSERT(!events.exception.pending \u0026\u0026 !events.exception.injected,\n+\t\t    \"Unexpected exception queued before userspace #PF injection\");\n+\tTEST_ASSERT(events.flags \u0026 KVM_VCPUEVENT_VALID_PAYLOAD,\n+\t\t    \"KVM_CAP_EXCEPTION_PAYLOAD was not enabled\");\n+\n+\tevents.flags |= KVM_VCPUEVENT_VALID_PAYLOAD;\n+\tevents.exception.injected = false;\n+\tevents.exception.pending = true;\n+\tevents.exception.nr = PF_VECTOR;\n+\tevents.exception.has_error_code = true;\n+\tevents.exception.error_code = 0;\n+\tevents.exception_has_payload = true;\n+\tevents.exception_payload = PF_TEST_GVA;\n+\tvcpu_events_set(vcpu, \u0026events);\n+}\n+\n+static void complete_mmio_read(struct kvm_vcpu *vcpu, gpa_t expected_gpa,\n+\t\t\t       u8 value)\n+{\n+\tTEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_MMIO);\n+\tTEST_ASSERT(!vcpu-\u003erun-\u003emmio.is_write,\n+\t\t    \"Expected an MMIO read, got a write\");\n+\tTEST_ASSERT_EQ(vcpu-\u003erun-\u003emmio.phys_addr, expected_gpa);\n+\tTEST_ASSERT_EQ(vcpu-\u003erun-\u003emmio.len, 1);\n+\tvcpu-\u003erun-\u003emmio.data[0] = value;\n+}\n+\n+static void assert_ucall_done(struct kvm_vcpu *vcpu)\n+{\n+\tstruct ucall uc;\n+\tu64 actual;\n+\n+\tTEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);\n+\tactual = get_ucall(vcpu, \u0026uc);\n+\tif (actual == UCALL_ABORT)\n+\t\tREPORT_GUEST_ASSERT(uc);\n+\n+\tTEST_ASSERT_EQ(actual, UCALL_DONE);\n+}\n+\n+int main(int argc, char *argv[])\n+{\n+\tgva_t svm_gva, npf_gva, boundary_page_gva, iopm_gva;\n+\tgva_t canonical_code_gva;\n+\tgpa_t npf_gpa, mmio_source_gpa, mmio_gpa, iopm_gpa;\n+\tstruct userspace_mem_region *region;\n+\tstruct kvm_vcpu *vcpu;\n+\tstruct kvm_vm *vm;\n+\tu64 *pte;\n+\n+\tTEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));\n+\tTEST_REQUIRE(kvm_cpu_has(X86_FEATURE_NPT));\n+\tTEST_REQUIRE(this_cpu_has(X86_FEATURE_DECODEASSISTS));\n+\tTEST_ASSERT(kvm_cpu_has(X86_FEATURE_DECODEASSISTS),\n+\t\t    \"KVM failed to expose DecodeAssists\");\n+\tTEST_REQUIRE(kvm_has_cap(KVM_CAP_EXCEPTION_PAYLOAD));\n+\n+\tvm = vm_create_with_one_vcpu(\u0026vcpu, l1_guest_code);\n+\tvm_enable_cap(vm, KVM_CAP_EXCEPTION_PAYLOAD, 1);\n+\tprepare_boundary_outsb_code(vm);\n+\tcanonical_code_gva = prepare_canonical_boundary_outsb_code(vm);\n+\tprepare_linear_wrap_outsb_code(vm);\n+\tvm_enable_npt(vm);\n+\tvcpu_alloc_svm(vm, \u0026svm_gva);\n+\tiopm_gva = vm_alloc_pages(vm, TEST_IOPM_SIZE / PAGE_SIZE);\n+\tiopm_gpa = addr_gva2gpa(vm, iopm_gva);\n+\tmemset(addr_gva2hva(vm, iopm_gva), 0, TEST_IOPM_SIZE);\n+\tnpf_gva = (gva_t)\u0026npf_target;\n+\tnpf_gpa = addr_gva2gpa(vm, npf_gva);\n+\n+\ttdp_identity_map_default_memslots(vm);\n+\tpte = tdp_get_pte(vm, npf_gpa);\n+\t*pte \u0026= ~PTE_PRESENT_MASK(\u0026vm-\u003estage2_mmu);\n+\tregion = memslot2region(vm, 0);\n+\tmmio_gpa = region-\u003eregion.guest_phys_addr +\n+\t\t   region-\u003eregion.memory_size + PAGE_SIZE;\n+\tmmio_source_gpa = addr_gva2gpa(vm, (gva_t)\u0026mmio_source);\n+\tpte = tdp_get_pte(vm, mmio_source_gpa);\n+\t*pte = (*pte \u0026 ~PHYSICAL_PAGE_MASK) | mmio_gpa;\n+\n+\tboundary_page_gva = (gva_t)\u0026boundary_outsb_code[PAGE_SIZE];\n+\tpte = vm_get_pte(vm, boundary_page_gva);\n+\t*pte \u0026= ~PTE_PRESENT_MASK(\u0026vm-\u003emmu);\n+\n+\tvcpu_args_set(vcpu, 4, svm_gva, npf_gpa, iopm_gpa,\n+\t\t      canonical_code_gva);\n+\n+\t/* Complete the MMIO source read in the MOVSB #NPF regression test. */\n+\tvcpu_run(vcpu);\n+\tcomplete_mmio_read(vcpu, mmio_gpa, 0xa5);\n+\n+\t/* Leave the second MMIO read pending while injecting #PF. */\n+\tvcpu_run(vcpu);\n+\tcomplete_mmio_read(vcpu, mmio_gpa, 0x5a);\n+\tqueue_userspace_pf(vcpu);\n+\tvcpu_run(vcpu);\n+\tassert_ucall_done(vcpu);\n+\n+\tkvm_vm_free(vm);\n+\treturn 0;\n+}\n"
}

2/1 2026/08/01 01:24 agent patch-evaluator
0m Model:
best-expensive

Args:
null

Results:
{
  "EnableConfigs": [
    "KVM",
    "KVM_AMD"
  ],
  "FocusSymbols": [
    "nested_svm_exit_handled",
    "kvm_inject_page_fault",
    "kvm_queue_exception",
    "kvm_queue_exception_p",
    "kvm_queue_exception_e"
  ],
  "Reasoning": "The patch series implements support for X86_FEATURE_DECODEASSISTS in KVM's nested SVM (AMD virtualization). It modifies the handling of instruction intercepts and page faults to properly capture and synthesize instruction bytes for nested guests. This involves changes to core KVM exception queuing (kvm_queue_exception*, kvm_inject_page_fault) and nested SVM exit handling (nested_svm_exit_handled). These are functional changes to reachable core virtualization logic and should be fuzzed.",
  "WorthFuzzing": true
}

Instruction:
You are an expert Linux kernel maintainer.
Your job is to review a provided patch series and determine
if it makes functional changes to the kernel that should be fuzzed.

IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on your internal knowledge of the kernel. You must actively
use your code access tools to examine the actual source code and confirm any assumptions.

Return WorthFuzzing=false if the patch only contains:
- Modifications to Documentation/, Kconfig files, or code comments.
- Purely decorative changes, such as logging (e.g., pr_err, printk) or tracepoints.
- Changes to numeric constants or macros that do not functionally alter execution flow.
- Code paths that are impossible to reach in virtualized environments like GCE or QEMU,
  even when utilizing software-emulated hardware (e.g., usb gadget, mac80211_hwsim).
- Code in vendor-specific PCIe switch, SmartNIC, or GPU drivers (e.g., mlxsw, pds_core, qed,
  ionic, amdgpu) that require physical PCIe hardware cards not emulated in standard QEMU.
- Driver .remove, .shutdown, or pci_unregister_driver teardown callbacks (e.g., igb_remove)
  that are executed only during PCI hot-unplug or sysfs driver unbind operations.

If it modifies reachable core kernel logic, drivers, or architectures, use your code search
tools to verify the code can be executed, then return WorthFuzzing=true.

When returning WorthFuzzing=true, you MUST ALSO:
1. Extract any specific kernel functions that should be heavily fuzzed into FocusSymbols.
   Avoid listing generic hot-path functions to prevent skewed test distributions.
   Prefer non-static, non-inlined API entrypoint functions over internal static helper functions
   (which are inlined by the compiler and do not have distinct symbol addresses).
2. Identify any specific CONFIG_ options required to properly test this new/modified feature.
   Go and look into the Kconfig files and check for ifdefs around the code, do not make assumptions.
   Also check "depends on" lines in Kconfig to include any non-standard parent subsystem configs
   needed for Kbuild to compile the code statically into vmlinux. List them in the EnableConfigs
   output array, and DO NOT add a 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
For your convenience, here is the diff of the changes:
commit a195452056def57ccba90493b6e20a90e99d9f34
Author: syz-cluster <triage@syzkaller.com>
Date:   Sat Aug 1 01:24:34 2026 +0000

    syz-cluster: applied patch under review

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 7a258831616fa..8466a17cd046f 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -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;
diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index 8071b372d233e..a0e57b64cadd4 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -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)
 {
diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h
index 3e375af15c035..9eda648a13143 100644
--- a/arch/x86/kvm/kvm_emulate.h
+++ b/arch/x86/kvm/kvm_emulate.h
@@ -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        */
 };
diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
index 73f37b050d0a0..2ade1ee0d2761 100644
--- a/arch/x86/kvm/svm/nested.c
+++ b/arch/x86/kvm/svm/nested.c
@@ -35,12 +35,174 @@
 
 #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_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_clear_synthesized_insn_bytes(struct vcpu_svm *svm)
+{
+	svm->nested.synthesized_insn_bytes.prepared = false;
+	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);
+	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;
+	const u8 max_bytes = sizeof(vmcb12->control.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) {
+		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);
+
+		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);
+}
+
 static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
 				       struct x86_exception *fault,
 				       bool from_hardware)
 {
 	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;
 
 	/*
@@ -68,6 +230,9 @@ 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;
+	if (from_emulation && !(fault->error_code & PFERR_FETCH_MASK))
+		nested_svm_prepare_synthesized_insn_bytes(vcpu);
 	nested_svm_vmexit(svm);
 }
 
@@ -868,7 +1033,11 @@ 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;
+	nested_svm_clear_synthesized_insn_bytes(svm);
 
 	if (guest_cpu_cap_has(vcpu, X86_FEATURE_VGIF) &&
 	    (vmcb12_ctrl->int_ctl & V_GIF_ENABLE_MASK))
@@ -1297,6 +1466,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);
 
@@ -1643,14 +1814,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;
 }
@@ -1698,6 +1871,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);
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index c1e6d62512b95..366f287366faf 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -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,
@@ -2571,7 +2577,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;
@@ -3723,7 +3729,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;
@@ -4820,6 +4826,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,
@@ -4941,11 +4993,13 @@ 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;
 	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;
@@ -5522,6 +5576,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);
 
diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
index a327bf751ecd5..cb7287d82107f 100644
--- a/arch/x86/kvm/svm/svm.h
+++ b/arch/x86/kvm/svm/svm.h
@@ -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;
@@ -242,6 +248,18 @@ 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;
+
+	/*
+	 * 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 {
@@ -887,7 +905,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,
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 0f1a829032c06..e7aff420d6c6f 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -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;
 	}
diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index 88c6c8046ddec..e31e3a2412aea 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -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
diff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/testing/selftests/kvm/include/x86/processor.h
index b161174ece453..42e009af525e7 100644
--- a/tools/testing/selftests/kvm/include/x86/processor.h
+++ b/tools/testing/selftests/kvm/include/x86/processor.h
@@ -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)
diff --git a/tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c b/tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c
new file mode 100644
index 0000000000000..13511ca8473b2
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c
@@ -0,0 +1,791 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Test KVM's virtualization of SVM DecodeAssists for nested guests.
+ */
+
+#include "test_util.h"
+#include "kvm_util.h"
+#include "processor.h"
+#include "svm_util.h"
+
+#define TEST_INT_VECTOR 0x81
+
+/* Any canonical virtual address that is never mapped by the selftest VM. */
+#define PF_TEST_GVA BIT_ULL(40)
+#define PF_FETCH_TEST_GVA BIT_ULL(41)
+
+#define OUTSB_OPCODE 0x6e
+#define BOUNDARY_OUTSB_CODE_SIZE 15
+#define LINEAR_WRAP_CODE_GVA (BIT_ULL(32) - PAGE_SIZE)
+#define LINEAR_WRAP_OUTSB_OFFSET (PAGE_SIZE - 8)
+#define LINEAR_WRAP_SETUP_SIZE 9
+#define LINEAR_WRAP_SETUP_OFFSET \
+	(LINEAR_WRAP_OUTSB_OFFSET - LINEAR_WRAP_SETUP_SIZE)
+#define LINEAR_WRAP_CS_BASE PAGE_SIZE
+#define TEST_IOPM_SIZE (3 * PAGE_SIZE)
+
+static u8 npf_target[PAGE_SIZE] __aligned(PAGE_SIZE);
+static u8 mmio_source __aligned(PAGE_SIZE);
+static u8 boundary_outsb_code[2 * PAGE_SIZE] __aligned(PAGE_SIZE);
+static const u8 linear_wrap_insn_bytes[15] = {
+	OUTSB_OPCODE, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+	0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
+};
+
+static void l2_read_code(void)
+{
+	asm volatile("mov (%0), %%rax" : : "r"(&npf_target) : "rax", "memory");
+	GUEST_FAIL("L2 read did not cause a nested page fault");
+}
+
+static void l2_outsb_code(void)
+{
+	asm volatile("mov %0, %%rsi\n\t"
+		     "mov $0x80, %%dx\n\t"
+		     "outsb"
+		     : : "r"(&npf_target) : "rsi", "rdx", "memory");
+	GUEST_FAIL("L2 OUTSB did not cause a nested page fault");
+}
+
+static void l2_movsb_code(void)
+{
+	asm volatile("mov %0, %%rsi\n\t"
+		     "mov %1, %%rdi\n\t"
+		     "movsb"
+		     : : "r"(&mmio_source), "r"(&npf_target)
+		     : "rsi", "rdi", "memory");
+	GUEST_FAIL("L2 MOVSB did not cause a nested page fault");
+}
+
+static void l2_pf_code(void)
+{
+	asm volatile("mov (%0), %%rax"
+		     : : "r"(PF_TEST_GVA) : "rax", "memory");
+	GUEST_FAIL("L2 access to an unmapped VA did not #PF");
+}
+
+static void l2_fep_pf_code(void)
+{
+	asm volatile(KVM_FEP "mov (%0), %%rax"
+		     : : "r"(PF_TEST_GVA) : "rax", "memory");
+	GUEST_FAIL("L2 forced-emulated access to an unmapped VA did not #PF");
+}
+
+static void l2_stale_emulator_pf_code(void)
+{
+	asm volatile("movb (%0), %%al\n\t"
+		     "nop"
+		     : : "r"(&mmio_source) : "rax", "memory");
+	GUEST_FAIL("Userspace-injected #PF was not intercepted by L1");
+}
+
+static void l2_mov_from_cr4_code(void)
+{
+	asm volatile("mov %%cr4, %%r10" : : : "r10");
+	GUEST_FAIL("L2 MOV-from-CR4 was not intercepted");
+}
+
+static void l2_fep_mov_from_cr4_code(void)
+{
+	asm volatile(KVM_FEP "mov %%cr4, %%r10" : : : "r10");
+	GUEST_FAIL("L2 forced-emulated MOV-from-CR4 was not intercepted");
+}
+
+static void l2_mov_to_cr4_code(void)
+{
+	asm volatile("mov %%cr4, %%rax\n\t"
+		     "mov %%rax, %%cr4" : : : "rax");
+	GUEST_FAIL("L2 MOV-to-CR4 was not intercepted");
+}
+
+static void l2_fep_mov_to_cr4_code(void)
+{
+	asm volatile("mov %%cr4, %%rax\n\t"
+		     KVM_FEP "mov %%rax, %%cr4" : : : "rax");
+	GUEST_FAIL("L2 forced-emulated MOV-to-CR4 was not intercepted");
+}
+
+static void l2_mov_to_dr7_code(void)
+{
+	asm volatile("mov %%dr7, %%rax\n\t"
+		     "mov %%rax, %%rbx\n\t"
+		     "mov %%rbx, %%dr7" : : : "rax", "rbx");
+	GUEST_FAIL("L2 MOV-to-DR7 was not intercepted");
+}
+
+static void l2_fep_mov_to_dr7_code(void)
+{
+	asm volatile("mov %%dr7, %%rax\n\t"
+		     "mov %%rax, %%rbx\n\t"
+		     KVM_FEP "mov %%rbx, %%dr7" : : : "rax", "rbx");
+	GUEST_FAIL("L2 forced-emulated MOV-to-DR7 was not intercepted");
+}
+
+static void l2_mov_from_dr7_code(void)
+{
+	asm volatile("mov %%dr7, %%r10" : : : "r10");
+	GUEST_FAIL("L2 MOV-from-DR7 was not intercepted");
+}
+
+static void l2_fep_mov_from_dr7_code(void)
+{
+	asm volatile(KVM_FEP "mov %%dr7, %%r10" : : : "r10");
+	GUEST_FAIL("L2 forced-emulated MOV-from-DR7 was not intercepted");
+}
+
+static void l2_clts_code(void)
+{
+	asm volatile("clts" : : : "memory");
+	GUEST_FAIL("L2 CLTS was not intercepted");
+}
+
+static void l2_fep_clts_code(void)
+{
+	asm volatile(KVM_FEP "clts" : : : "memory");
+	GUEST_FAIL("L2 forced-emulated CLTS was not intercepted");
+}
+
+static void l2_lmsw_code(void)
+{
+	asm volatile("smsw %%ax\n\t"
+		     "lmsw %%ax" : : : "rax", "memory");
+	GUEST_FAIL("L2 LMSW was not intercepted");
+}
+
+static void l2_fep_lmsw_code(void)
+{
+	asm volatile("smsw %%ax\n\t"
+		     KVM_FEP "lmsw %%ax" : : : "rax", "memory");
+	GUEST_FAIL("L2 forced-emulated LMSW was not intercepted");
+}
+
+static void l2_smsw_code(void)
+{
+	asm volatile("smsw %%ax" : : : "rax", "memory");
+	GUEST_FAIL("L2 SMSW was not intercepted");
+}
+
+static void l2_fep_smsw_code(void)
+{
+	asm volatile(KVM_FEP "smsw %%ax" : : : "rax", "memory");
+	GUEST_FAIL("L2 forced-emulated SMSW was not intercepted");
+}
+
+static void l2_int_code(void)
+{
+	asm volatile("int %0" : : "i"(TEST_INT_VECTOR));
+	GUEST_FAIL("L2 INTn was not intercepted");
+}
+
+static void l2_fep_int_code(void)
+{
+	asm volatile(KVM_FEP "int %0" : : "i"(TEST_INT_VECTOR));
+	GUEST_FAIL("L2 forced-emulated INTn was not intercepted");
+}
+
+static void l2_invlpg_code(void)
+{
+	asm volatile("invlpg (%0)" : : "r"(&npf_target) : "memory");
+	GUEST_FAIL("L2 INVLPG was not intercepted");
+}
+
+static void l2_fep_invlpg_code(void)
+{
+	asm volatile(KVM_FEP "invlpg (%0)" : : "r"(&npf_target) : "memory");
+	GUEST_FAIL("L2 forced-emulated INVLPG was not intercepted");
+}
+
+static void l2_invlpga_code(void)
+{
+	asm volatile("invlpga" : : "a"(&npf_target), "c"(0) : "memory");
+	GUEST_FAIL("L2 INVLPGA was not intercepted");
+}
+
+static void l2_fep_invlpga_code(void)
+{
+	asm volatile(KVM_FEP "invlpga"
+		     : : "a"(&npf_target), "c"(0) : "memory");
+	GUEST_FAIL("L2 forced-emulated INVLPGA was not intercepted");
+}
+
+struct instruction_intercept_test {
+	const char *name;
+	void (*code)(void);
+	void (*fep_code)(void);
+	u64 intercept;
+	u32 intercept_cr;
+	u32 intercept_dr;
+	u64 exit_code;
+	u64 exit_info_1;
+	u64 exit_info_1_mask;
+	bool check_rax;
+	u64 rax;
+};
+
+static const struct instruction_intercept_test instruction_intercept_tests[] = {
+	{
+		.name = "MOV-to-CR4",
+		.code = l2_mov_to_cr4_code,
+		.fep_code = l2_fep_mov_to_cr4_code,
+		.intercept_cr = BIT(INTERCEPT_CR4_WRITE),
+		.exit_code = SVM_EXIT_WRITE_CR4,
+		.exit_info_1 = BIT_ULL(63),
+		.exit_info_1_mask = ~0ULL,
+	}, {
+		.name = "MOV-from-CR4",
+		.code = l2_mov_from_cr4_code,
+		.fep_code = l2_fep_mov_from_cr4_code,
+		.intercept_cr = BIT(INTERCEPT_CR4_READ),
+		.exit_code = SVM_EXIT_READ_CR4,
+		.exit_info_1 = BIT_ULL(63) | 10,
+		.exit_info_1_mask = ~0ULL,
+	}, {
+		.name = "MOV-to-DR7",
+		.code = l2_mov_to_dr7_code,
+		.fep_code = l2_fep_mov_to_dr7_code,
+		.intercept_dr = BIT(INTERCEPT_DR7_WRITE),
+		.exit_code = SVM_EXIT_WRITE_DR7,
+		.exit_info_1 = 3,
+		.exit_info_1_mask = ~0ULL,
+	}, {
+		.name = "MOV-from-DR7",
+		.code = l2_mov_from_dr7_code,
+		.fep_code = l2_fep_mov_from_dr7_code,
+		.intercept_dr = BIT(INTERCEPT_DR7_READ),
+		.exit_code = SVM_EXIT_READ_DR7,
+		.exit_info_1 = 10,
+		.exit_info_1_mask = ~0ULL,
+	}, {
+		.name = "CLTS",
+		.code = l2_clts_code,
+		.fep_code = l2_fep_clts_code,
+		.intercept_cr = BIT(INTERCEPT_CR0_WRITE),
+		.exit_code = SVM_EXIT_WRITE_CR0,
+		.exit_info_1_mask = BIT_ULL(63),
+	}, {
+		.name = "LMSW",
+		.code = l2_lmsw_code,
+		.fep_code = l2_fep_lmsw_code,
+		.intercept_cr = BIT(INTERCEPT_CR0_WRITE),
+		.exit_code = SVM_EXIT_WRITE_CR0,
+		.exit_info_1_mask = BIT_ULL(63),
+	}, {
+		.name = "SMSW",
+		.code = l2_smsw_code,
+		.fep_code = l2_fep_smsw_code,
+		.intercept_cr = BIT(INTERCEPT_CR0_READ),
+		.exit_code = SVM_EXIT_READ_CR0,
+		.exit_info_1_mask = BIT_ULL(63),
+	}, {
+		.name = "INTn",
+		.code = l2_int_code,
+		.fep_code = l2_fep_int_code,
+		.intercept = BIT_ULL(INTERCEPT_INTn),
+		.exit_code = SVM_EXIT_SWINT,
+		.exit_info_1 = TEST_INT_VECTOR,
+		.exit_info_1_mask = ~0ULL,
+	}, {
+		.name = "INVLPG",
+		.code = l2_invlpg_code,
+		.fep_code = l2_fep_invlpg_code,
+		.intercept = BIT_ULL(INTERCEPT_INVLPG),
+		.exit_code = SVM_EXIT_INVLPG,
+		.exit_info_1 = (u64)&npf_target,
+		.exit_info_1_mask = ~0ULL,
+	}, {
+		.name = "INVLPGA",
+		.code = l2_invlpga_code,
+		.fep_code = l2_fep_invlpga_code,
+		.intercept = BIT_ULL(INTERCEPT_INVLPGA),
+		.exit_code = SVM_EXIT_INVLPGA,
+		.exit_info_1_mask = ~0ULL,
+		.check_rax = true,
+		.rax = (u64)&npf_target,
+	},
+};
+
+static void assert_decode_assist_insn_bytes(struct vmcb *vmcb)
+{
+	GUEST_ASSERT(vmcb->control.insn_len);
+	GUEST_ASSERT(vmcb->control.insn_len <=
+		     sizeof(vmcb->control.insn_bytes));
+	GUEST_ASSERT(!memcmp(vmcb->control.insn_bytes,
+			     (void *)vmcb->save.rip,
+			     vmcb->control.insn_len));
+}
+
+static void assert_full_decode_assist_insn_bytes(struct vmcb *vmcb)
+{
+	GUEST_ASSERT_EQ(vmcb->control.insn_len,
+			sizeof(vmcb->control.insn_bytes));
+	assert_decode_assist_insn_bytes(vmcb);
+}
+
+static void prepare_l2_for_vmrun(struct svm_test_data *svm, gva_t rip)
+{
+	struct vmcb *vmcb = svm->vmcb;
+
+	vmcb->save.rip = rip;
+	vmcb->save.rsp = (u64)svm->stack;
+}
+
+static void run_intercept_test(struct svm_test_data *svm,
+			       const struct instruction_intercept_test *test,
+			       bool synthesized)
+{
+	struct vmcb *vmcb = svm->vmcb;
+	struct vmcb_control_area *control = &vmcb->control;
+	const char *source = synthesized ? "synthesized" : "hardware";
+	u64 expected_exit_info_1 = test->exit_info_1 & test->exit_info_1_mask;
+
+	control->intercept |= test->intercept;
+	control->intercept_cr |= test->intercept_cr;
+	control->intercept_dr |= test->intercept_dr;
+
+	if (synthesized) {
+		control->exit_info_1 = ~0ULL;
+		control->exit_info_2 = ~0ULL;
+		prepare_l2_for_vmrun(svm, (u64)test->fep_code);
+	} else {
+		prepare_l2_for_vmrun(svm, (u64)test->code);
+	}
+
+	run_guest(vmcb, svm->vmcb_gpa);
+
+	__GUEST_ASSERT(control->exit_code == test->exit_code,
+		       "%s (%s): expected exit code %#lx, got %#lx",
+		       test->name, source, (unsigned long)test->exit_code,
+		       (unsigned long)control->exit_code);
+	__GUEST_ASSERT((control->exit_info_1 & test->exit_info_1_mask) ==
+		       expected_exit_info_1,
+		       "%s (%s): expected EXITINFO1 %#lx with mask %#lx, got %#lx",
+		       test->name, source, (unsigned long)expected_exit_info_1,
+		       (unsigned long)test->exit_info_1_mask,
+		       (unsigned long)control->exit_info_1);
+	__GUEST_ASSERT(!control->insn_len,
+		       "%s (%s): expected no instruction bytes, got %u",
+		       test->name, source, control->insn_len);
+
+	if (test->check_rax)
+		__GUEST_ASSERT(vmcb->save.rax == test->rax,
+			       "%s (%s): expected rAX %#lx, got %#lx",
+			       test->name, source, (unsigned long)test->rax,
+			       (unsigned long)vmcb->save.rax);
+
+	if (synthesized)
+		__GUEST_ASSERT(!control->exit_info_2,
+			       "%s (%s): expected EXITINFO2 to be clear, got %#lx",
+			       test->name, source,
+			       (unsigned long)control->exit_info_2);
+
+	control->intercept &= ~test->intercept;
+	control->intercept_cr &= ~test->intercept_cr;
+	control->intercept_dr &= ~test->intercept_dr;
+}
+
+static void test_instruction_intercepts(struct svm_test_data *svm)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(instruction_intercept_tests); i++) {
+		run_intercept_test(svm, &instruction_intercept_tests[i], false);
+
+		if (is_forced_emulation_enabled)
+			run_intercept_test(svm, &instruction_intercept_tests[i],
+					   true);
+	}
+}
+
+static void test_hardware_npf(struct svm_test_data *svm, gpa_t npf_gpa)
+{
+	struct vmcb *vmcb = svm->vmcb;
+
+	prepare_l2_for_vmrun(svm, (u64)l2_read_code);
+	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);
+	assert_decode_assist_insn_bytes(vmcb);
+}
+
+/*
+ * The IOIO intercept causes L0 to emulate OUTSB before accessing its source
+ * operand.  The emulated read then faults on L1's NPT, resulting in a
+ * KVM-synthesized #NPF.
+ */
+static void test_synthesized_npf(struct svm_test_data *svm, gpa_t npf_gpa)
+{
+	struct vmcb *vmcb = svm->vmcb;
+
+	prepare_l2_for_vmrun(svm, (u64)l2_outsb_code);
+	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);
+	assert_full_decode_assist_insn_bytes(vmcb);
+}
+
+/*
+ * MOVSB first reads from MMIO, causing a hardware #NPF that L0 emulates.
+ * After userspace completes the read, the emulated destination write faults
+ * on L1's NPT.  The new #NPF must not reuse the original hardware exit's GPA.
+ */
+static void test_synthesized_npf_after_hardware_npf(struct svm_test_data *svm,
+						    gpa_t npf_gpa)
+{
+	struct vmcb *vmcb = svm->vmcb;
+
+	prepare_l2_for_vmrun(svm, (u64)l2_movsb_code);
+	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);
+	assert_full_decode_assist_insn_bytes(vmcb);
+}
+
+/*
+ * OUTSB is the final byte of a mapped code page, and the following page is
+ * not present in L2's page tables.  DecodeAssist byte fetching must stop at
+ * the page boundary and report only the OUTSB opcode.
+ */
+static void test_synthesized_npf_truncated(struct svm_test_data *svm,
+					   gpa_t npf_gpa)
+{
+	struct vmcb *vmcb = svm->vmcb;
+
+	prepare_l2_for_vmrun(svm,
+			     (u64)&boundary_outsb_code[PAGE_SIZE -
+						       BOUNDARY_OUTSB_CODE_SIZE]);
+	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,
+			(u64)&boundary_outsb_code[PAGE_SIZE - 1]);
+	GUEST_ASSERT_EQ(vmcb->control.insn_len, 1);
+	GUEST_ASSERT_EQ(vmcb->control.insn_bytes[0], OUTSB_OPCODE);
+}
+
+/*
+ * OUTSB is the final byte in the lower canonical address range.  The first
+ * byte after OUTSB is non-canonical, and must terminate DecodeAssist fetching
+ * even though the corresponding high canonical address is mapped.
+ */
+static void test_synthesized_npf_canonical_boundary(struct svm_test_data *svm,
+						    gpa_t npf_gpa,
+						    gva_t code_gva)
+{
+	struct vmcb *vmcb = svm->vmcb;
+
+	prepare_l2_for_vmrun(svm, code_gva + PAGE_SIZE -
+				  BOUNDARY_OUTSB_CODE_SIZE);
+	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, code_gva + PAGE_SIZE - 1);
+	GUEST_ASSERT_EQ(vmcb->control.insn_len, 1);
+	GUEST_ASSERT_EQ(vmcb->control.insn_bytes[0], OUTSB_OPCODE);
+}
+
+/*
+ * A nonzero CS.base makes the DecodeAssist byte window cross the 32-bit
+ * linear-address boundary without crossing the CS limit.
+ */
+static void test_synthesized_npf_linear_wrap(struct svm_test_data *svm,
+					     gpa_t npf_gpa)
+{
+	struct vmcb *vmcb = svm->vmcb;
+	u16 cs_attrib = vmcb->save.cs.attrib;
+	u64 cs_base = vmcb->save.cs.base;
+	u32 cs_limit = vmcb->save.cs.limit;
+	u32 outsb_eip = LINEAR_WRAP_CODE_GVA + LINEAR_WRAP_OUTSB_OFFSET -
+			LINEAR_WRAP_CS_BASE;
+	u32 setup_eip = LINEAR_WRAP_CODE_GVA + LINEAR_WRAP_SETUP_OFFSET -
+			LINEAR_WRAP_CS_BASE;
+
+	vmcb->save.cs.attrib &= ~SVM_SELECTOR_L_MASK;
+	vmcb->save.cs.attrib |= SVM_SELECTOR_DB_MASK;
+	vmcb->save.cs.base = LINEAR_WRAP_CS_BASE;
+	vmcb->save.cs.limit = UINT32_MAX;
+	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));
+	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;
+}
+
+static void test_hardware_intercepted_pf(struct svm_test_data *svm)
+{
+	struct vmcb *vmcb = svm->vmcb;
+
+	prepare_l2_for_vmrun(svm, (u64)l2_pf_code);
+	run_guest(vmcb, svm->vmcb_gpa);
+	GUEST_ASSERT_EQ(vmcb->control.exit_code, SVM_EXIT_EXCP_BASE + PF_VECTOR);
+	GUEST_ASSERT_EQ(vmcb->control.exit_info_2, PF_TEST_GVA);
+	GUEST_ASSERT(!(vmcb->control.exit_info_1 & PFERR_PRESENT_MASK));
+	GUEST_ASSERT(!(vmcb->control.exit_info_1 & PFERR_FETCH_MASK));
+	assert_decode_assist_insn_bytes(vmcb);
+}
+
+static void test_hardware_intercepted_fetch_pf(struct svm_test_data *svm)
+{
+	struct vmcb *vmcb = svm->vmcb;
+
+	prepare_l2_for_vmrun(svm, PF_FETCH_TEST_GVA);
+	run_guest(vmcb, svm->vmcb_gpa);
+	GUEST_ASSERT_EQ(vmcb->control.exit_code, SVM_EXIT_EXCP_BASE + PF_VECTOR);
+	GUEST_ASSERT_EQ(vmcb->control.exit_info_2, PF_FETCH_TEST_GVA);
+	GUEST_ASSERT(!(vmcb->control.exit_info_1 & PFERR_PRESENT_MASK));
+	GUEST_ASSERT(vmcb->control.exit_info_1 & PFERR_FETCH_MASK);
+	GUEST_ASSERT_EQ(vmcb->control.insn_len, 0);
+}
+
+static void test_synthesized_pf(struct svm_test_data *svm)
+{
+	struct vmcb *vmcb = svm->vmcb;
+
+	if (!is_forced_emulation_enabled)
+		return;
+
+	prepare_l2_for_vmrun(svm, (u64)l2_fep_pf_code);
+	run_guest(vmcb, svm->vmcb_gpa);
+	GUEST_ASSERT_EQ(vmcb->control.exit_code, SVM_EXIT_EXCP_BASE + PF_VECTOR);
+	GUEST_ASSERT_EQ(vmcb->control.exit_info_2, PF_TEST_GVA);
+	GUEST_ASSERT(!(vmcb->control.exit_info_1 & PFERR_PRESENT_MASK));
+	GUEST_ASSERT(!(vmcb->control.exit_info_1 & PFERR_FETCH_MASK));
+	assert_full_decode_assist_insn_bytes(vmcb);
+}
+
+/*
+ * Inject #PF while an emulated MMIO read is awaiting completion.  KVM
+ * completes the instruction before constructing the nested VM-Exit, so the
+ * old emulator fetch cache must not be reported for the following instruction.
+ */
+static void test_userspace_injected_pf_during_emulation(struct svm_test_data *svm)
+{
+	struct vmcb *vmcb = svm->vmcb;
+
+	prepare_l2_for_vmrun(svm, (u64)l2_stale_emulator_pf_code);
+	run_guest(vmcb, svm->vmcb_gpa);
+	GUEST_ASSERT_EQ(vmcb->control.exit_code, SVM_EXIT_EXCP_BASE + PF_VECTOR);
+	GUEST_ASSERT_EQ(vmcb->control.exit_info_2, PF_TEST_GVA);
+	GUEST_ASSERT(!(vmcb->control.exit_info_1 & PFERR_FETCH_MASK));
+	assert_full_decode_assist_insn_bytes(vmcb);
+}
+
+static void l1_guest_code(struct svm_test_data *svm, gpa_t npf_gpa,
+			  gpa_t iopm_gpa, gva_t canonical_code_gva)
+{
+	struct vmcb *vmcb = svm->vmcb;
+
+	GUEST_ASSERT(this_cpu_has(X86_FEATURE_DECODEASSISTS));
+
+	generic_svm_setup(svm, l2_read_code);
+	vmcb->control.iopm_base_pa = iopm_gpa;
+
+	vmcb->control.intercept |= BIT_ULL(INTERCEPT_IOIO_PROT);
+	vmcb->control.intercept_exceptions |= 1U << PF_VECTOR;
+
+	test_hardware_npf(svm, npf_gpa);
+	test_synthesized_npf(svm, npf_gpa);
+	test_synthesized_npf_after_hardware_npf(svm, npf_gpa);
+	test_synthesized_npf_truncated(svm, npf_gpa);
+	test_synthesized_npf_canonical_boundary(svm, npf_gpa,
+						canonical_code_gva);
+	test_synthesized_npf_linear_wrap(svm, npf_gpa);
+	test_hardware_intercepted_pf(svm);
+	test_hardware_intercepted_fetch_pf(svm);
+	test_synthesized_pf(svm);
+	test_userspace_injected_pf_during_emulation(svm);
+	test_instruction_intercepts(svm);
+
+	GUEST_DONE();
+}
+
+static void build_boundary_outsb_code(u8 *code)
+{
+	u64 source = (u64)&npf_target;
+
+	/* movabs $npf_target, %rsi */
+	code[0] = 0x48;
+	code[1] = 0xbe;
+	memcpy(&code[2], &source, sizeof(source));
+
+	/* mov $0x80, %dx; outsb */
+	code[10] = 0x66;
+	code[11] = 0xba;
+	code[12] = 0x80;
+	code[13] = 0x00;
+	code[14] = OUTSB_OPCODE;
+}
+
+static void prepare_boundary_outsb_code(struct kvm_vm *vm)
+{
+	gva_t code_gva = (gva_t)&boundary_outsb_code[PAGE_SIZE -
+						      BOUNDARY_OUTSB_CODE_SIZE];
+
+	build_boundary_outsb_code(addr_gva2hva(vm, code_gva));
+}
+
+static gva_t prepare_canonical_boundary_outsb_code(struct kvm_vm *vm)
+{
+	gva_t backing_gva = vm_alloc_pages(vm, 2);
+	u8 *code_page = addr_gva2hva(vm, backing_gva);
+	u8 *alias_page = addr_gva2hva(vm, backing_gva + PAGE_SIZE);
+	gpa_t code_gpa = addr_gva2gpa(vm, backing_gva);
+	gpa_t alias_gpa = addr_gva2gpa(vm, backing_gva + PAGE_SIZE);
+	gva_t code_gva = BIT_ULL(vm->va_bits - 1) - PAGE_SIZE;
+	gva_t alias_gva = ~(BIT_ULL(vm->va_bits - 1) - 1);
+	u8 *code = &code_page[PAGE_SIZE - BOUNDARY_OUTSB_CODE_SIZE];
+
+	build_boundary_outsb_code(code);
+	memset(alias_page, 0xcc, PAGE_SIZE);
+	virt_map(vm, code_gva, code_gpa, 1);
+	virt_map(vm, alias_gva, alias_gpa, 1);
+
+	return code_gva;
+}
+
+static void prepare_linear_wrap_outsb_code(struct kvm_vm *vm)
+{
+	gva_t code_gva = vm_alloc_pages(vm, 2);
+	u8 *high_page = addr_gva2hva(vm, code_gva);
+	u8 *low_page = addr_gva2hva(vm, code_gva + PAGE_SIZE);
+	gpa_t high_gpa = addr_gva2gpa(vm, code_gva);
+	gpa_t low_gpa = addr_gva2gpa(vm, code_gva + PAGE_SIZE);
+	u32 source = (u32)(u64)&npf_target;
+	u8 *setup = &high_page[LINEAR_WRAP_SETUP_OFFSET];
+
+	TEST_ASSERT((u64)&npf_target <= UINT32_MAX,
+		    "npf_target must be addressable from compatibility mode");
+
+	/* mov $npf_target, %esi; mov $0x80, %dx */
+	setup[0] = 0xbe;
+	memcpy(&setup[1], &source, sizeof(source));
+	setup[5] = 0x66;
+	setup[6] = 0xba;
+	setup[7] = 0x80;
+	setup[8] = 0x00;
+
+	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);
+}
+
+static void queue_userspace_pf(struct kvm_vcpu *vcpu)
+{
+	struct kvm_vcpu_events events;
+
+	vcpu_events_get(vcpu, &events);
+	TEST_ASSERT(!events.exception.pending && !events.exception.injected,
+		    "Unexpected exception queued before userspace #PF injection");
+	TEST_ASSERT(events.flags & KVM_VCPUEVENT_VALID_PAYLOAD,
+		    "KVM_CAP_EXCEPTION_PAYLOAD was not enabled");
+
+	events.flags |= KVM_VCPUEVENT_VALID_PAYLOAD;
+	events.exception.injected = false;
+	events.exception.pending = true;
+	events.exception.nr = PF_VECTOR;
+	events.exception.has_error_code = true;
+	events.exception.error_code = 0;
+	events.exception_has_payload = true;
+	events.exception_payload = PF_TEST_GVA;
+	vcpu_events_set(vcpu, &events);
+}
+
+static void complete_mmio_read(struct kvm_vcpu *vcpu, gpa_t expected_gpa,
+			       u8 value)
+{
+	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_MMIO);
+	TEST_ASSERT(!vcpu->run->mmio.is_write,
+		    "Expected an MMIO read, got a write");
+	TEST_ASSERT_EQ(vcpu->run->mmio.phys_addr, expected_gpa);
+	TEST_ASSERT_EQ(vcpu->run->mmio.len, 1);
+	vcpu->run->mmio.data[0] = value;
+}
+
+static void assert_ucall_done(struct kvm_vcpu *vcpu)
+{
+	struct ucall uc;
+	u64 actual;
+
+	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
+	actual = get_ucall(vcpu, &uc);
+	if (actual == UCALL_ABORT)
+		REPORT_GUEST_ASSERT(uc);
+
+	TEST_ASSERT_EQ(actual, UCALL_DONE);
+}
+
+int main(int argc, char *argv[])
+{
+	gva_t svm_gva, npf_gva, boundary_page_gva, iopm_gva;
+	gva_t canonical_code_gva;
+	gpa_t npf_gpa, mmio_source_gpa, mmio_gpa, iopm_gpa;
+	struct userspace_mem_region *region;
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	u64 *pte;
+
+	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));
+	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_NPT));
+	TEST_REQUIRE(this_cpu_has(X86_FEATURE_DECODEASSISTS));
+	TEST_ASSERT(kvm_cpu_has(X86_FEATURE_DECODEASSISTS),
+		    "KVM failed to expose DecodeAssists");
+	TEST_REQUIRE(kvm_has_cap(KVM_CAP_EXCEPTION_PAYLOAD));
+
+	vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code);
+	vm_enable_cap(vm, KVM_CAP_EXCEPTION_PAYLOAD, 1);
+	prepare_boundary_outsb_code(vm);
+	canonical_code_gva = prepare_canonical_boundary_outsb_code(vm);
+	prepare_linear_wrap_outsb_code(vm);
+	vm_enable_npt(vm);
+	vcpu_alloc_svm(vm, &svm_gva);
+	iopm_gva = vm_alloc_pages(vm, TEST_IOPM_SIZE / PAGE_SIZE);
+	iopm_gpa = addr_gva2gpa(vm, iopm_gva);
+	memset(addr_gva2hva(vm, iopm_gva), 0, TEST_IOPM_SIZE);
+	npf_gva = (gva_t)&npf_target;
+	npf_gpa = addr_gva2gpa(vm, npf_gva);
+
+	tdp_identity_map_default_memslots(vm);
+	pte = tdp_get_pte(vm, npf_gpa);
+	*pte &= ~PTE_PRESENT_MASK(&vm->stage2_mmu);
+	region = memslot2region(vm, 0);
+	mmio_gpa = region->region.guest_phys_addr +
+		   region->region.memory_size + PAGE_SIZE;
+	mmio_source_gpa = addr_gva2gpa(vm, (gva_t)&mmio_source);
+	pte = tdp_get_pte(vm, mmio_source_gpa);
+	*pte = (*pte & ~PHYSICAL_PAGE_MASK) | mmio_gpa;
+
+	boundary_page_gva = (gva_t)&boundary_outsb_code[PAGE_SIZE];
+	pte = vm_get_pte(vm, boundary_page_gva);
+	*pte &= ~PTE_PRESENT_MASK(&vm->mmu);
+
+	vcpu_args_set(vcpu, 4, svm_gva, npf_gpa, iopm_gpa,
+		      canonical_code_gva);
+
+	/* Complete the MMIO source read in the MOVSB #NPF regression test. */
+	vcpu_run(vcpu);
+	complete_mmio_read(vcpu, mmio_gpa, 0xa5);
+
+	/* Leave the second MMIO read pending while injecting #PF. */
+	vcpu_run(vcpu);
+	complete_mmio_read(vcpu, mmio_gpa, 0x5a);
+	queue_userspace_pf(vcpu);
+	vcpu_run(vcpu);
+	assert_ucall_done(vcpu);
+
+	kvm_vm_free(vm);
+	return 0;
+}

3/2 2026/08/01 01:24 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 22461
							output: 54
							thoughts: 876
						

Thoughts:
**Analyzing the KVM Patch Series for Decode Assist Support**

Okay, so I'm looking at this patch series that introduces `X86_FEATURE_DECODEASSISTS` support for nested SVM (AMD virtualization) within KVM. My initial assessment is that this is definitely reachable in environments with nested virtualization enabled, like GCE or QEMU.  Given that it's a functional change within KVM, fuzzing is probably a good idea to identify any potential issues early.

I need to nail down the critical symbols.  The core of the modifications touches on several files: `arch/x86/kvm/svm/nested.c`, `arch/x86/kvm/svm/svm.c`, `arch/x86/kvm/emulate.c`, and `arch/x86/kvm/x86.c`.  A new selftest, `tools/testing/selftests/kvm/x86/svm_nested_decode_assists_test.c`, has been added which is important.

The modified functions are:

*   `nested_svm_clear_insn_bytes`
*   `nested_svm_copy_insn_bytes`
*   `nested_svm_vmexit_supports_insn_bytes`
*   `nested_svm_clear_synthesized_insn_bytes`
*   `nested_svm_fetch_insn_bytes`
*   `nested_svm_prepare_synthesized_insn_bytes`
*   `nested_svm_update_vmcb12_insn_bytes`
*   `nested_svm_inject_npf_exit`
*   `nested_svm_vmexit_update_vmcb12`
*   `nested_svm_exit_handled`
*   `nested_svm_inject_exception_vmexit`
*   `pf_interception`
*   `check_selective_cr0_intercepted`
*   `svm_handle_exit`
*   `svm_prepare_decode_assist_exit_info`
*   `svm_check_intercept`
*   `svm_set_cpu_caps`
*   `kvm_queue_exception_vmexit`
*   `kvm_multiple_exception`
*   `kvm_queue_exception`
*   `kvm_queue_exception_p`
*   `kvm_queue_exception_e_p`
*   `kvm_inject_page_fault`
*   `kvm_queue_exception_e`
*   `kvm_arch_vcpu_ioctl_run`
*   `em_salc` - (I see `seg_base` and `get_intercept_linear_addr` were added in `arch/x86/kvm/emulate.c`)
*   `emulator_check_intercept`
*   `nested_vmcb02_prepare_control`

I need to focus on what's visible from outside these modules so I am going to double-check `arch/x86/kvm/svm/nested.c` and `arch/x86/kvm/svm/svm.c`.

Looks like:
*   `nested_svm_exit_handled` is non-static.
*   `nested_svm_inject_npf_exit`, `nested_svm_vmexit_update_vmcb12`, `svm_check_intercept`, `pf_interception`, `kvm_multiple_exception`, and `kvm_queue_exception_vmexit` are static.
*   `kvm_inject_page_fault`, `kvm_queue_exception`, `kvm_queue_exception_p`, `kvm_queue_exception_e`, and `kvm_arch_vcpu_ioctl_run` are non-static.

To be thorough, I'm going to use `default_api:grepper` to hunt for any functions that are both non-static and *not* inlined. I will start there for my fuzzing plan, and use it as a starting point.



4/2 2026/08/01 01:24 tool grepper
0m Args:
{
  "Expression": "^(int|void|bool|u64|u32).*nested_svm_.*",
  "PathPrefix": "arch/x86/kvm/svm/nested.c"
}

Results:
{
  "Output": "arch/x86/kvm/svm/nested.c=379=typedef unsigned long nsvm_msrpm_merge_t;\narch/x86/kvm/svm/nested.c-380-\narch/x86/kvm/svm/nested.c:381:int __init nested_svm_init_msrpm_merge_offsets(void)\narch/x86/kvm/svm/nested.c-382-{\n--\narch/x86/kvm/svm/nested.c=603=static bool nested_vmcb_check_save(struct kvm_vcpu *vcpu,\n--\narch/x86/kvm/svm/nested.c-651-\narch/x86/kvm/svm/nested.c:652:int nested_svm_check_cached_vmcb12(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/nested.c-653-{\n--\narch/x86/kvm/svm/nested.c=1255=static int nested_svm_copy_vmcb12_to_cache(struct kvm_vcpu *vcpu, u64 vmcb12_gpa)\n--\narch/x86/kvm/svm/nested.c-1281-\narch/x86/kvm/svm/nested.c:1282:int nested_svm_vmrun(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/nested.c-1283-{\n--\narch/x86/kvm/svm/nested.c=1418=static int nested_svm_vmexit_update_vmcb12(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/nested.c-1487-\narch/x86/kvm/svm/nested.c:1488:void nested_svm_vmexit(struct vcpu_svm *svm)\narch/x86/kvm/svm/nested.c-1489-{\n--\narch/x86/kvm/svm/nested.c=1785=static int nested_svm_intercept(struct vcpu_svm *svm)\n--\narch/x86/kvm/svm/nested.c-1816-\narch/x86/kvm/svm/nested.c:1817:int nested_svm_exit_handled(struct vcpu_svm *svm, bool vmcb02_insn_bytes_fresh)\narch/x86/kvm/svm/nested.c-1818-{\n--\narch/x86/kvm/svm/nested.c-1830-\narch/x86/kvm/svm/nested.c:1831:int nested_svm_check_permissions(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/nested.c-1832-{\n--\narch/x86/kvm/svm/nested.c=1898=static int svm_check_nested_events(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/nested.c-1972-\narch/x86/kvm/svm/nested.c:1973:int nested_svm_exit_special(struct vcpu_svm *svm)\narch/x86/kvm/svm/nested.c-1974-{\n--\narch/x86/kvm/svm/nested.c-2006-\narch/x86/kvm/svm/nested.c:2007:void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/nested.c-2008-{\n"
}

5/2 2026/08/01 01:24 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 25268
							output: 53
							thoughts: 57
						

Thoughts:
**Examining the Scope of the Patch**

Okay, so this patch specifically touches `nested_svm_exit_handled`. I need to understand its implications within the larger context. My immediate focus is on figuring out the extent of the changes and how this might interact with other parts of the code.

Let me take a look at what else in `arch/x86/kvm/svm/svm.c` and `arch/x86/kvm/x86.c` isn't declared as `static`. Identifying non-static functions and data structures gives me the best scope for any potential interactions the patch could create. I need to understand what variables are accessible and which functions are callable from outside these specific source files. This will clarify how broadly this change can affect the overall SVM and x86 KVM implementations.



6/2 2026/08/01 01:24 tool grepper
0m Args:
{
  "Expression": "^(int|void|bool|u64|u32).*kvm_.*",
  "PathPrefix": "arch/x86/kvm/x86.c"
}

Results:
{
  "Output": "\nFull output is too long, showing 500 out of 639 lines.\nUse more precise expression if possible.\n\narch/x86/kvm/x86.c=367=static int exception_type(int vector)\n--\narch/x86/kvm/x86.c-392-\narch/x86/kvm/x86.c:393:void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu,\narch/x86/kvm/x86.c-394-\t\t\t\t   struct kvm_queued_exception *ex)\n--\narch/x86/kvm/x86.c=462=static void kvm_multiple_exception(struct kvm_vcpu *vcpu, unsigned int nr,\n--\narch/x86/kvm/x86.c-523-\narch/x86/kvm/x86.c:524:void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)\narch/x86/kvm/x86.c-525-{\n--\narch/x86/kvm/x86.c=528=EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_queue_exception);\n--\narch/x86/kvm/x86.c-530-\narch/x86/kvm/x86.c:531:void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr,\narch/x86/kvm/x86.c-532-\t\t\t   unsigned long payload)\n--\narch/x86/kvm/x86.c=538=static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr,\n--\narch/x86/kvm/x86.c-545-\narch/x86/kvm/x86.c:546:void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned int nr,\narch/x86/kvm/x86.c-547-\t\t\t   bool has_error_code, u32 error_code)\n--\narch/x86/kvm/x86.c=573=EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_requeue_exception);\narch/x86/kvm/x86.c-574-\narch/x86/kvm/x86.c:575:int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)\narch/x86/kvm/x86.c-576-{\n--\narch/x86/kvm/x86.c=584=EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_complete_insn_gp);\narch/x86/kvm/x86.c-585-\narch/x86/kvm/x86.c:586:void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault,\narch/x86/kvm/x86.c-587-\t\t\t   bool from_hardware)\n--\narch/x86/kvm/x86.c-608-\narch/x86/kvm/x86.c:609:void __kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu,\narch/x86/kvm/x86.c-610-\t\t\t\t      struct x86_exception *fault,\n--\narch/x86/kvm/x86.c=631=EXPORT_SYMBOL_FOR_KVM_INTERNAL(__kvm_inject_emulated_page_fault);\narch/x86/kvm/x86.c-632-\narch/x86/kvm/x86.c:633:void kvm_inject_nmi(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-634-{\n--\narch/x86/kvm/x86.c-638-\narch/x86/kvm/x86.c:639:void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)\narch/x86/kvm/x86.c-640-{\n--\narch/x86/kvm/x86.c=643=EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_queue_exception_e);\narch/x86/kvm/x86.c-644-\narch/x86/kvm/x86.c:645:bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr)\narch/x86/kvm/x86.c-646-{\n--\narch/x86/kvm/x86.c=684=static void kvm_load_host_pkru(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/x86.c-697-\narch/x86/kvm/x86.c:698:int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)\narch/x86/kvm/x86.c-699-{\n--\narch/x86/kvm/x86.c=742=EXPORT_SYMBOL_FOR_KVM_INTERNAL(__kvm_set_xcr);\narch/x86/kvm/x86.c-743-\narch/x86/kvm/x86.c:744:int kvm_emulate_xsetbv(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-745-{\n--\narch/x86/kvm/x86.c=755=EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_emulate_xsetbv);\narch/x86/kvm/x86.c-756-\narch/x86/kvm/x86.c:757:void kvm_invalidate_pcid(struct kvm_vcpu *vcpu, unsigned long pcid)\narch/x86/kvm/x86.c-758-{\n--\narch/x86/kvm/x86.c-799-\narch/x86/kvm/x86.c:800:int kvm_emulate_rdpmc(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-801-{\n--\narch/x86/kvm/x86.c=814=EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_emulate_rdpmc);\narch/x86/kvm/x86.c-815-\narch/x86/kvm/x86.c:816:int kvm_emulate_as_nop(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-817-{\n--\narch/x86/kvm/x86.c-820-\narch/x86/kvm/x86.c:821:int kvm_emulate_invd(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-822-{\n--\narch/x86/kvm/x86.c=838=EXPORT_SYMBOL_FOR_KVM_INTERNAL(handle_fastpath_invd);\narch/x86/kvm/x86.c-839-\narch/x86/kvm/x86.c:840:int kvm_handle_invalid_op(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-841-{\n--\narch/x86/kvm/x86.c=848=static int kvm_emulate_monitor_mwait(struct kvm_vcpu *vcpu, const char *insn)\n--\narch/x86/kvm/x86.c-866-}\narch/x86/kvm/x86.c:867:int kvm_emulate_mwait(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-868-{\n--\narch/x86/kvm/x86.c=871=EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_emulate_mwait);\narch/x86/kvm/x86.c-872-\narch/x86/kvm/x86.c:873:int kvm_emulate_monitor(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-874-{\n--\narch/x86/kvm/x86.c=1130=static inline u64 __scale_tsc(u64 ratio, u64 tsc)\n--\narch/x86/kvm/x86.c-1134-\narch/x86/kvm/x86.c:1135:u64 kvm_scale_tsc(u64 tsc, u64 ratio)\narch/x86/kvm/x86.c-1136-{\n--\narch/x86/kvm/x86.c-1144-\narch/x86/kvm/x86.c:1145:u64 kvm_compute_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)\narch/x86/kvm/x86.c-1146-{\n--\narch/x86/kvm/x86.c-1153-\narch/x86/kvm/x86.c:1154:u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)\narch/x86/kvm/x86.c-1155-{\n--\narch/x86/kvm/x86.c=1159=EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_read_l1_tsc);\narch/x86/kvm/x86.c-1160-\narch/x86/kvm/x86.c:1161:u64 kvm_calc_nested_tsc_offset(u64 l1_offset, u64 l2_offset, u64 l2_multiplier)\narch/x86/kvm/x86.c-1162-{\n--\narch/x86/kvm/x86.c=1174=EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_calc_nested_tsc_offset);\narch/x86/kvm/x86.c-1175-\narch/x86/kvm/x86.c:1176:u64 kvm_calc_nested_tsc_multiplier(u64 l1_multiplier, u64 l2_multiplier)\narch/x86/kvm/x86.c-1177-{\n--\narch/x86/kvm/x86.c=1184=EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_calc_nested_tsc_multiplier);\narch/x86/kvm/x86.c-1185-\narch/x86/kvm/x86.c:1186:void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 l1_offset)\narch/x86/kvm/x86.c-1187-{\n--\narch/x86/kvm/x86.c=1247=static void __kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 offset, u64 tsc,\n--\narch/x86/kvm/x86.c-1299-\narch/x86/kvm/x86.c:1300:void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 *user_value)\narch/x86/kvm/x86.c-1301-{\n--\narch/x86/kvm/x86.c=1497=static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp)\n--\narch/x86/kvm/x86.c-1510- */\narch/x86/kvm/x86.c:1511:bool kvm_get_monotonic_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp)\narch/x86/kvm/x86.c-1512-{\n--\narch/x86/kvm/x86.c=1726=static void kvm_setup_guest_pvclock(struct pvclock_vcpu_time_info *ref_hv_clock,\n--\narch/x86/kvm/x86.c-1773-\narch/x86/kvm/x86.c:1774:int kvm_guest_time_update(struct kvm_vcpu *v)\narch/x86/kvm/x86.c-1775-{\n--\narch/x86/kvm/x86.c=2037=static inline void kvm_vcpu_flush_tlb_current(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/x86.c-2048- */\narch/x86/kvm/x86.c:2049:void kvm_service_local_tlb_flush_requests(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-2050-{\n--\narch/x86/kvm/x86.c=2209=static inline u64 kvm_sync_valid_fields(struct kvm *kvm)\n--\narch/x86/kvm/x86.c-2213-\narch/x86/kvm/x86.c:2214:int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)\narch/x86/kvm/x86.c-2215-{\n--\narch/x86/kvm/x86.c=2544=static DEFINE_PER_CPU(struct kvm_vcpu *, last_vcpu);\narch/x86/kvm/x86.c-2545-\narch/x86/kvm/x86.c:2546:void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)\narch/x86/kvm/x86.c-2547-{\n--\narch/x86/kvm/x86.c=2626=static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/x86.c-2672-\narch/x86/kvm/x86.c:2673:void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-2674-{\n--\narch/x86/kvm/x86.c=2934=static struct kvm_queued_exception *kvm_get_exception_to_save(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/x86.c-2950-\narch/x86/kvm/x86.c:2951:void kvm_handle_exception_payload_quirk(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-2952-{\n--\narch/x86/kvm/x86.c=3921=static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,\n--\narch/x86/kvm/x86.c-3935-\narch/x86/kvm/x86.c:3936:void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)\narch/x86/kvm/x86.c-3937-{\n--\narch/x86/kvm/x86.c-3954-\narch/x86/kvm/x86.c:3955:int kvm_vm_ioctl_enable_cap(struct kvm *kvm,\narch/x86/kvm/x86.c-3956-\t\t\t    struct kvm_enable_cap *cap)\n--\narch/x86/kvm/x86.c=4288=static int kvm_arch_suspend_notifier(struct kvm *kvm)\n--\narch/x86/kvm/x86.c-4302-\narch/x86/kvm/x86.c:4303:int kvm_arch_pm_notifier(struct kvm *kvm, unsigned long state)\narch/x86/kvm/x86.c-4304-{\n--\narch/x86/kvm/x86.c=4372=long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,\n--\narch/x86/kvm/x86.c-4384-\narch/x86/kvm/x86.c:4385:int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)\narch/x86/kvm/x86.c-4386-{\n--\narch/x86/kvm/x86.c=4862=static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,\n--\narch/x86/kvm/x86.c-4888-\narch/x86/kvm/x86.c:4889:int kvm_read_guest_virt(struct kvm_vcpu *vcpu,\narch/x86/kvm/x86.c-4890-\t\t\t       gva_t addr, void *val, unsigned int bytes,\n--\narch/x86/kvm/x86.c=4952=static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val,\n--\narch/x86/kvm/x86.c-4967-\narch/x86/kvm/x86.c:4968:int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val,\narch/x86/kvm/x86.c-4969-\t\t\t\tunsigned int bytes, struct x86_exception *exception)\n--\narch/x86/kvm/x86.c=4979=static int kvm_check_emulate_insn(struct kvm_vcpu *vcpu, int emul_type,\n--\narch/x86/kvm/x86.c-4985-\narch/x86/kvm/x86.c:4986:int handle_ud(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-4987-{\n--\narch/x86/kvm/x86.c=5464=static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/x86.c-5480-\narch/x86/kvm/x86.c:5481:int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-5482-{\n--\narch/x86/kvm/x86.c=5945=static void init_emulate_ctxt(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/x86.c-5971-\narch/x86/kvm/x86.c:5972:void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)\narch/x86/kvm/x86.c-5973-{\n--\narch/x86/kvm/x86.c=6044=static void prepare_emulation_ctxt_failure_exit(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/x86.c-6051-\narch/x86/kvm/x86.c:6052:void __kvm_prepare_emulation_failure_exit(struct kvm_vcpu *vcpu, u64 *data,\narch/x86/kvm/x86.c-6053-\t\t\t\t\t  u8 ndata)\n--\narch/x86/kvm/x86.c=6057=EXPORT_SYMBOL_FOR_KVM_INTERNAL(__kvm_prepare_emulation_failure_exit);\narch/x86/kvm/x86.c-6058-\narch/x86/kvm/x86.c:6059:void kvm_prepare_emulation_failure_exit(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-6060-{\n--\narch/x86/kvm/x86.c=6063=EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_prepare_emulation_failure_exit);\narch/x86/kvm/x86.c-6064-\narch/x86/kvm/x86.c:6065:void kvm_prepare_event_vectoring_exit(struct kvm_vcpu *vcpu, gpa_t gpa)\narch/x86/kvm/x86.c-6066-{\n--\narch/x86/kvm/x86.c=6085=EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_prepare_event_vectoring_exit);\narch/x86/kvm/x86.c-6086-\narch/x86/kvm/x86.c:6087:void kvm_prepare_unexpected_reason_exit(struct kvm_vcpu *vcpu, u64 exit_reason)\narch/x86/kvm/x86.c-6088-{\n--\narch/x86/kvm/x86.c=6168=static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,\n--\narch/x86/kvm/x86.c-6182-\narch/x86/kvm/x86.c:6183:int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-6184-{\n--\narch/x86/kvm/x86.c=6297=static bool is_soft_int_instruction(struct x86_emulate_ctxt *ctxt,\n--\narch/x86/kvm/x86.c-6323- */\narch/x86/kvm/x86.c:6324:int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,\narch/x86/kvm/x86.c-6325-\t\t\t\t    void *insn, int insn_len)\n--\narch/x86/kvm/x86.c=6339=EXPORT_SYMBOL_FOR_KVM_INTERNAL(x86_decode_emulated_instruction);\narch/x86/kvm/x86.c-6340-\narch/x86/kvm/x86.c:6341:int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,\narch/x86/kvm/x86.c-6342-\t\t\t    int emulation_type, void *insn, int insn_len)\n--\narch/x86/kvm/x86.c-6560-\narch/x86/kvm/x86.c:6561:int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type)\narch/x86/kvm/x86.c-6562-{\n--\narch/x86/kvm/x86.c=6565=EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_emulate_instruction);\narch/x86/kvm/x86.c-6566-\narch/x86/kvm/x86.c:6567:int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,\narch/x86/kvm/x86.c-6568-\t\t\t\t\tvoid *insn, int insn_len)\n--\narch/x86/kvm/x86.c=6637=static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size,\n--\narch/x86/kvm/x86.c-6657-\narch/x86/kvm/x86.c:6658:int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in)\narch/x86/kvm/x86.c-6659-{\n--\narch/x86/kvm/x86.c=6903=static struct notifier_block pvclock_gtod_notifier = {\n--\narch/x86/kvm/x86.c-6907-\narch/x86/kvm/x86.c:6908:void kvm_setup_xss_caps(void)\narch/x86/kvm/x86.c-6909-{\n--\narch/x86/kvm/x86.c=7005=static void kvm_x86_check_cpu_compat(void *ret)\n--\narch/x86/kvm/x86.c-7009-\narch/x86/kvm/x86.c:7010:int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)\narch/x86/kvm/x86.c-7011-{\n--\narch/x86/kvm/x86.c=7167=EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_x86_vendor_init);\narch/x86/kvm/x86.c-7168-\narch/x86/kvm/x86.c:7169:void kvm_x86_vendor_exit(void)\narch/x86/kvm/x86.c-7170-{\n--\narch/x86/kvm/x86.c=7245=static void kvm_pv_kick_cpu_op(struct kvm *kvm, int apicid)\n--\narch/x86/kvm/x86.c-7260-\narch/x86/kvm/x86.c:7261:bool kvm_apicv_activated(struct kvm *kvm)\narch/x86/kvm/x86.c-7262-{\n--\narch/x86/kvm/x86.c=7265=EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_apicv_activated);\narch/x86/kvm/x86.c-7266-\narch/x86/kvm/x86.c:7267:bool kvm_vcpu_apicv_activated(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-7268-{\n--\narch/x86/kvm/x86.c=7339=static int complete_hypercall_exit(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/x86.c-7348-\narch/x86/kvm/x86.c:7349:int ____kvm_emulate_hypercall(struct kvm_vcpu *vcpu, int cpl,\narch/x86/kvm/x86.c-7350-\t\t\t      int (*complete_hypercall)(struct kvm_vcpu *))\n--\narch/x86/kvm/x86.c=7452=EXPORT_SYMBOL_FOR_KVM_INTERNAL(____kvm_emulate_hypercall);\narch/x86/kvm/x86.c-7453-\narch/x86/kvm/x86.c:7454:int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-7455-{\n--\narch/x86/kvm/x86.c=7497=static void post_kvm_run_save(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/x86.c-7514-\narch/x86/kvm/x86.c:7515:int kvm_check_nested_events(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-7516-{\n--\narch/x86/kvm/x86.c=7789=static void process_nmi(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/x86.c-7826-/* Return total number of NMIs pending injection to the VM */\narch/x86/kvm/x86.c:7827:int kvm_get_nr_pending_nmis(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-7828-{\n--\narch/x86/kvm/x86.c-7832-\narch/x86/kvm/x86.c:7833:void kvm_make_scan_ioapic_request_mask(struct kvm *kvm,\narch/x86/kvm/x86.c-7834-\t\t\t\t       unsigned long *vcpu_bitmap)\n--\narch/x86/kvm/x86.c-7838-\narch/x86/kvm/x86.c:7839:void kvm_make_scan_ioapic_request(struct kvm *kvm)\narch/x86/kvm/x86.c-7840-{\n--\narch/x86/kvm/x86.c-7843-\narch/x86/kvm/x86.c:7844:void __kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-7845-{\n--\narch/x86/kvm/x86.c=7881=static void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/x86.c-7903-\narch/x86/kvm/x86.c:7904:void __kvm_set_or_clear_apicv_inhibit(struct kvm *kvm,\narch/x86/kvm/x86.c-7905-\t\t\t\t      enum kvm_apicv_inhibit reason, bool set)\n--\narch/x86/kvm/x86.c-7948-\narch/x86/kvm/x86.c:7949:void kvm_set_or_clear_apicv_inhibit(struct kvm *kvm,\narch/x86/kvm/x86.c-7950-\t\t\t\t    enum kvm_apicv_inhibit reason, bool set)\n--\narch/x86/kvm/x86.c=7959=EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_set_or_clear_apicv_inhibit);\narch/x86/kvm/x86.c-7960-\narch/x86/kvm/x86.c:7961:void kvm_inc_or_dec_irq_window_inhibit(struct kvm *kvm, bool inc)\narch/x86/kvm/x86.c-7962-{\n--\narch/x86/kvm/x86.c=8023=static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/x86.c-8042-\narch/x86/kvm/x86.c:8043:void kvm_arch_guest_memory_reclaimed(struct kvm *kvm)\narch/x86/kvm/x86.c-8044-{\n--\narch/x86/kvm/x86.c=8515=static bool kvm_vcpu_running(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/x86.c-8520-\narch/x86/kvm/x86.c:8521:bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-8522-{\n--\narch/x86/kvm/x86.c=8565=EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_vcpu_has_events);\narch/x86/kvm/x86.c-8566-\narch/x86/kvm/x86.c:8567:int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-8568-{\n--\narch/x86/kvm/x86.c=8692=static int __kvm_emulate_halt(struct kvm_vcpu *vcpu, int state, int reason)\n--\narch/x86/kvm/x86.c-8712-\narch/x86/kvm/x86.c:8713:int kvm_emulate_halt_noskip(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-8714-{\n--\narch/x86/kvm/x86.c=8717=EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_emulate_halt_noskip);\narch/x86/kvm/x86.c-8718-\narch/x86/kvm/x86.c:8719:int kvm_emulate_halt(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-8720-{\n--\narch/x86/kvm/x86.c=8743=EXPORT_SYMBOL_FOR_KVM_INTERNAL(handle_fastpath_hlt);\narch/x86/kvm/x86.c-8744-\narch/x86/kvm/x86.c:8745:int kvm_emulate_ap_reset_hold(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-8746-{\n--\narch/x86/kvm/x86.c=8752=EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_emulate_ap_reset_hold);\narch/x86/kvm/x86.c-8753-\narch/x86/kvm/x86.c:8754:bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-8755-{\n--\narch/x86/kvm/x86.c-8759-\narch/x86/kvm/x86.c:8760:bool kvm_arch_vcpu_preempted_in_kernel(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-8761-{\n--\narch/x86/kvm/x86.c-8764-\narch/x86/kvm/x86.c:8765:bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-8766-{\n--\narch/x86/kvm/x86.c=8855=static int kvm_x86_vcpu_pre_run(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/x86.c-8883-\narch/x86/kvm/x86.c:8884:int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-8885-{\n--\narch/x86/kvm/x86.c-9002-\narch/x86/kvm/x86.c:9003:int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,\narch/x86/kvm/x86.c-9004-\t\t\t\t    struct kvm_mp_state *mp_state)\n--\narch/x86/kvm/x86.c-9028-\narch/x86/kvm/x86.c:9029:int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,\narch/x86/kvm/x86.c-9030-\t\t\t\t    struct kvm_mp_state *mp_state)\n--\narch/x86/kvm/x86.c-9071-\narch/x86/kvm/x86.c:9072:int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,\narch/x86/kvm/x86.c-9073-\t\t    int reason, bool has_error_code, u32 error_code)\n--\narch/x86/kvm/x86.c=9122=static void kvm_arch_vcpu_guestdbg_update_apicv_inhibit(struct kvm *kvm)\n--\narch/x86/kvm/x86.c-9142-\narch/x86/kvm/x86.c:9143:int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,\narch/x86/kvm/x86.c-9144-\t\t\t\t\tstruct kvm_guest_debug *dbg)\n--\narch/x86/kvm/x86.c-9206- */\narch/x86/kvm/x86.c:9207:int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,\narch/x86/kvm/x86.c-9208-\t\t\t\t    struct kvm_translation *tr)\n--\narch/x86/kvm/x86.c-9227-\narch/x86/kvm/x86.c:9228:int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)\narch/x86/kvm/x86.c-9229-{\n--\narch/x86/kvm/x86.c-9250-\narch/x86/kvm/x86.c:9251:int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)\narch/x86/kvm/x86.c-9252-{\n--\narch/x86/kvm/x86.c=9284=static int sync_regs(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/x86.c-9303-\narch/x86/kvm/x86.c:9304:int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)\narch/x86/kvm/x86.c-9305-{\n--\narch/x86/kvm/x86.c-9342-\narch/x86/kvm/x86.c:9343:int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-9344-{\n--\narch/x86/kvm/x86.c-9439-\narch/x86/kvm/x86.c:9440:void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-9441-{\n--\narch/x86/kvm/x86.c-9453-\narch/x86/kvm/x86.c:9454:void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-9455-{\n--\narch/x86/kvm/x86.c=9485=static void kvm_xstate_reset(struct kvm_vcpu *vcpu, bool init_event)\n--\narch/x86/kvm/x86.c-9531-\narch/x86/kvm/x86.c:9532:void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)\narch/x86/kvm/x86.c-9533-{\n--\narch/x86/kvm/x86.c=9667=EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_vcpu_reset);\narch/x86/kvm/x86.c-9668-\narch/x86/kvm/x86.c:9669:void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)\narch/x86/kvm/x86.c-9670-{\n--\narch/x86/kvm/x86.c=9679=EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_vcpu_deliver_sipi_vector);\narch/x86/kvm/x86.c-9680-\narch/x86/kvm/x86.c:9681:void kvm_arch_enable_virtualization(void)\narch/x86/kvm/x86.c-9682-{\n--\narch/x86/kvm/x86.c-9685-\narch/x86/kvm/x86.c:9686:void kvm_arch_disable_virtualization(void)\narch/x86/kvm/x86.c-9687-{\n--\narch/x86/kvm/x86.c-9690-\narch/x86/kvm/x86.c:9691:int kvm_arch_enable_virtualization_cpu(void)\narch/x86/kvm/x86.c-9692-{\n--\narch/x86/kvm/x86.c-9786-\narch/x86/kvm/x86.c:9787:void kvm_arch_shutdown(void)\narch/x86/kvm/x86.c-9788-{\n--\narch/x86/kvm/x86.c-9805-\narch/x86/kvm/x86.c:9806:void kvm_arch_disable_virtualization_cpu(void)\narch/x86/kvm/x86.c-9807-{\n--\narch/x86/kvm/x86.c-9825-\narch/x86/kvm/x86.c:9826:bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-9827-{\n--\narch/x86/kvm/x86.c=9830=EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_vcpu_is_reset_bsp);\narch/x86/kvm/x86.c-9831-\narch/x86/kvm/x86.c:9832:bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)\narch/x86/kvm/x86.c-9833-{\n--\narch/x86/kvm/x86.c-9836-\narch/x86/kvm/x86.c:9837:void kvm_arch_free_vm(struct kvm *kvm)\narch/x86/kvm/x86.c-9838-{\n\n"
}

7/2 2026/08/01 01:24 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 37572
							output: 51
							thoughts: 106
						

Thoughts:
**Analyzing KVM Function Visibility**

Okay, so I'm taking a look at some KVM code and making some observations about function visibility and inlining. Specifically, I've noted that `kvm_queue_exception`, `kvm_queue_exception_p`, `kvm_queue_exception_e`, `kvm_inject_page_fault`, and `kvm_arch_vcpu_ioctl_run` are all non-static and *not* inlined. Also, I've seen that `nested_svm_exit_handled` is also non-static and not inlined.

Given that, I think the next logical step would be to investigate `arch/x86/kvm/emulate.c`. It's a good bet to find more non-static functions related to the KVM emulation layer, which are likely not inlined, which could be useful to investigate.



8/2 2026/08/01 01:24 tool grepper
0m Args:
{
  "Expression": "^(int|void|bool|u64|u32).*emulator_.*",
  "PathPrefix": "arch/x86/kvm/emulate.c"
}

Results:
{
  "Output": "arch/x86/kvm/emulate.c=2901=static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,\n--\narch/x86/kvm/emulate.c-3001-\narch/x86/kvm/emulate.c:3002:int emulator_task_switch(struct x86_emulate_ctxt *ctxt,\narch/x86/kvm/emulate.c-3003-\t\t\t u16 tss_selector, int idt_index, int reason,\n--\narch/x86/kvm/emulate.c=5304=int x86_emulate_insn(struct x86_emulate_ctxt *ctxt, bool check_intercepts)\n--\narch/x86/kvm/emulate.c-5665-\narch/x86/kvm/emulate.c:5666:void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)\narch/x86/kvm/emulate.c-5667-{\n--\narch/x86/kvm/emulate.c-5670-\narch/x86/kvm/emulate.c:5671:void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)\narch/x86/kvm/emulate.c-5672-{\n--\narch/x86/kvm/emulate.c-5675-\narch/x86/kvm/emulate.c:5676:bool emulator_can_use_gpa(struct x86_emulate_ctxt *ctxt)\narch/x86/kvm/emulate.c-5677-{\n"
}

9/2 2026/08/01 01:24 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 38379
							output: 34
							thoughts: 219
						

Thoughts:
**Analyzing Potential Interception Points**

Okay, let's see. Right, `emulator_check_intercept`, `em_salc`, `get_intercept_linear_addr`, and `seg_base`... all static! That simplifies things considerably. The real targets, then, are these: `nested_svm_exit_handled`, `kvm_queue_exception`, `kvm_queue_exception_p`, `kvm_queue_exception_e`, `kvm_inject_page_fault`, and `kvm_arch_vcpu_ioctl_run`.

Wait a minute, hold up. `kvm_arch_vcpu_ioctl_run`... that's a *very* hot path. I need to avoid that one if at all possible. It'll be a performance bottleneck. But `kvm_queue_exception`, `kvm_queue_exception_p`, `kvm_queue_exception_e`, and `kvm_inject_page_fault` are all promising. And `nested_svm_exit_handled` looks like a solid candidate too.

Right, I'm going to double check the Kconfig for KVM and SVM to ensure nothing crucial is overlooked.



10/2 2026/08/01 01:25 tool grepper
0m Args:
{
  "Expression": "config KVM",
  "PathPrefix": "arch/x86/kvm/Kconfig"
}

Results:
{
  "Output": "arch/x86/kvm/Kconfig=18=if VIRTUALIZATION\narch/x86/kvm/Kconfig-19-\narch/x86/kvm/Kconfig:20:config KVM_X86\narch/x86/kvm/Kconfig-21-\tdef_tristate KVM if (KVM_INTEL != n || KVM_AMD != n)\n--\narch/x86/kvm/Kconfig-51-\narch/x86/kvm/Kconfig:52:config KVM\narch/x86/kvm/Kconfig-53-\ttristate \"Kernel-based Virtual Machine (KVM) support\"\n--\narch/x86/kvm/Kconfig-68-\narch/x86/kvm/Kconfig:69:config KVM_WERROR\narch/x86/kvm/Kconfig-70-\tbool \"Compile KVM with -Werror\"\n--\narch/x86/kvm/Kconfig-82-\narch/x86/kvm/Kconfig:83:config KVM_SW_PROTECTED_VM\narch/x86/kvm/Kconfig-84-\tbool \"Enable support for KVM software-protected VMs\"\n--\narch/x86/kvm/Kconfig-95-\narch/x86/kvm/Kconfig:96:config KVM_INTEL\narch/x86/kvm/Kconfig-97-\ttristate \"KVM for Intel (and compatible) processors support\"\n--\narch/x86/kvm/Kconfig-106-\narch/x86/kvm/Kconfig:107:config KVM_INTEL_PROVE_VE\narch/x86/kvm/Kconfig-108-        bool \"Check that guests do not receive #VE exceptions\"\n--\narch/x86/kvm/Kconfig=122=config X86_SGX_KVM\n--\narch/x86/kvm/Kconfig-133-\narch/x86/kvm/Kconfig:134:config KVM_INTEL_TDX\narch/x86/kvm/Kconfig-135-\tbool \"Intel Trust Domain Extensions (TDX) support\"\n--\narch/x86/kvm/Kconfig-145-\narch/x86/kvm/Kconfig:146:config KVM_AMD\narch/x86/kvm/Kconfig-147-\ttristate \"KVM for AMD processors support\"\n--\narch/x86/kvm/Kconfig-155-\narch/x86/kvm/Kconfig:156:config KVM_AMD_SEV\narch/x86/kvm/Kconfig-157-\tbool \"AMD Secure Encrypted Virtualization (SEV) support\"\n--\narch/x86/kvm/Kconfig-172-\narch/x86/kvm/Kconfig:173:config KVM_IOAPIC\narch/x86/kvm/Kconfig-174-\tbool \"I/O APIC, PIC, and PIT emulation\"\n--\narch/x86/kvm/Kconfig-182-\narch/x86/kvm/Kconfig:183:config KVM_SMM\narch/x86/kvm/Kconfig-184-\tbool \"System Management Mode emulation\"\n--\narch/x86/kvm/Kconfig-193-\narch/x86/kvm/Kconfig:194:config KVM_HYPERV\narch/x86/kvm/Kconfig-195-\tbool \"Support for Microsoft Hyper-V emulation\"\n--\narch/x86/kvm/Kconfig-207-\narch/x86/kvm/Kconfig:208:config KVM_XEN\narch/x86/kvm/Kconfig-209-\tbool \"Support for Xen hypercall interface\"\n--\narch/x86/kvm/Kconfig-216-\narch/x86/kvm/Kconfig:217:config KVM_PROVE_MMU\narch/x86/kvm/Kconfig-218-\tbool \"Prove KVM MMU correctness\"\n--\narch/x86/kvm/Kconfig-229-\narch/x86/kvm/Kconfig:230:config KVM_EXTERNAL_WRITE_TRACKING\narch/x86/kvm/Kconfig-231-\tbool\narch/x86/kvm/Kconfig-232-\narch/x86/kvm/Kconfig:233:config KVM_MAX_NR_VCPUS\narch/x86/kvm/Kconfig-234-\tint \"Maximum number of vCPUs per KVM guest\"\n"
}

11/2 2026/08/01 01:25 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 40110
							output: 34
							thoughts: 123
						

Thoughts:
**Analyzing the Patch's Scope and Dependencies**

Okay, so this patch is touching `arch/x86/kvm/svm/nested.c`, `arch/x86/kvm/svm/svm.c`, `arch/x86/kvm/emulate.c`, and `arch/x86/kvm/x86.c`. That immediately tells me this is related to KVM and, specifically, AMD's Secure Virtual Machine (SVM) implementation. Given the files, I'd say this targets nested virtualization features for KVM on AMD hardware. I know I'll need `KVM` enabled, naturally, and `KVM_AMD`, because SVM is clearly involved here. Ah, right, `KVM_AMD` is a tristate config option, isn't it? That means it can be built-in, modular, or disabled. I need to figure out the build context for this patch, so I'll need to confirm that `KVM_AMD` is either built-in or loaded as a module. Just to be sure, before I go any further, I'll quickly check if `KVM_AMD` has any dependencies that could impact this patch. I need the full picture before I can evaluate the impact and potential integration.



12/2 2026/08/01 01:25 tool grepper
0m Args:
{
  "Expression": "depends on.*",
  "PathPrefix": "arch/x86/kvm/Kconfig"
}

Results:
{
  "Output": "arch/x86/kvm/Kconfig=52=config KVM\narch/x86/kvm/Kconfig-53-\ttristate \"Kernel-based Virtual Machine (KVM) support\"\narch/x86/kvm/Kconfig:54:\tdepends on X86_LOCAL_APIC\narch/x86/kvm/Kconfig-55-\thelp\n--\narch/x86/kvm/Kconfig=69=config KVM_WERROR\n--\narch/x86/kvm/Kconfig-76-\t# the kernel-wide WERROR=y.\narch/x86/kvm/Kconfig:77:\tdepends on KVM_X86 \u0026\u0026 ((EXPERT \u0026\u0026 !KASAN) || WERROR)\narch/x86/kvm/Kconfig-78-\thelp\n--\narch/x86/kvm/Kconfig=83=config KVM_SW_PROTECTED_VM\narch/x86/kvm/Kconfig-84-\tbool \"Enable support for KVM software-protected VMs\"\narch/x86/kvm/Kconfig:85:\tdepends on EXPERT\narch/x86/kvm/Kconfig:86:\tdepends on KVM_X86 \u0026\u0026 X86_64\narch/x86/kvm/Kconfig-87-\tselect KVM_GENERIC_MEMORY_ATTRIBUTES\n--\narch/x86/kvm/Kconfig=96=config KVM_INTEL\narch/x86/kvm/Kconfig-97-\ttristate \"KVM for Intel (and compatible) processors support\"\narch/x86/kvm/Kconfig:98:\tdepends on KVM \u0026\u0026 IA32_FEAT_CTL\narch/x86/kvm/Kconfig-99-\tselect X86_FRED if X86_64\n--\narch/x86/kvm/Kconfig=107=config KVM_INTEL_PROVE_VE\narch/x86/kvm/Kconfig-108-        bool \"Check that guests do not receive #VE exceptions\"\narch/x86/kvm/Kconfig:109:        depends on KVM_INTEL \u0026\u0026 EXPERT\narch/x86/kvm/Kconfig-110-        help\n--\narch/x86/kvm/Kconfig=122=config X86_SGX_KVM\narch/x86/kvm/Kconfig-123-\tbool \"Software Guard eXtensions (SGX) Virtualization\"\narch/x86/kvm/Kconfig:124:\tdepends on X86_SGX \u0026\u0026 KVM_INTEL\narch/x86/kvm/Kconfig-125-\thelp\n--\narch/x86/kvm/Kconfig=134=config KVM_INTEL_TDX\n--\narch/x86/kvm/Kconfig-136-\tdefault y\narch/x86/kvm/Kconfig:137:\tdepends on INTEL_TDX_HOST\narch/x86/kvm/Kconfig-138-\tselect KVM_GENERIC_MEMORY_ATTRIBUTES\n--\narch/x86/kvm/Kconfig=146=config KVM_AMD\narch/x86/kvm/Kconfig-147-\ttristate \"KVM for AMD processors support\"\narch/x86/kvm/Kconfig:148:\tdepends on KVM \u0026\u0026 (CPU_SUP_AMD || CPU_SUP_HYGON)\narch/x86/kvm/Kconfig-149-\thelp\n--\narch/x86/kvm/Kconfig=156=config KVM_AMD_SEV\n--\narch/x86/kvm/Kconfig-158-\tdefault y\narch/x86/kvm/Kconfig:159:\tdepends on KVM_AMD \u0026\u0026 X86_64\narch/x86/kvm/Kconfig:160:\tdepends on CRYPTO_DEV_SP_PSP \u0026\u0026 !(KVM_AMD=y \u0026\u0026 CRYPTO_DEV_CCP_DD=m)\narch/x86/kvm/Kconfig-161-\tselect ARCH_HAS_CC_PLATFORM\n--\narch/x86/kvm/Kconfig=173=config KVM_IOAPIC\n--\narch/x86/kvm/Kconfig-175-\tdefault y\narch/x86/kvm/Kconfig:176:\tdepends on KVM_X86\narch/x86/kvm/Kconfig-177-\thelp\n--\narch/x86/kvm/Kconfig=183=config KVM_SMM\n--\narch/x86/kvm/Kconfig-185-\tdefault y\narch/x86/kvm/Kconfig:186:\tdepends on KVM_X86\narch/x86/kvm/Kconfig-187-\thelp\n--\narch/x86/kvm/Kconfig=194=config KVM_HYPERV\narch/x86/kvm/Kconfig-195-\tbool \"Support for Microsoft Hyper-V emulation\"\narch/x86/kvm/Kconfig:196:\tdepends on KVM_X86\narch/x86/kvm/Kconfig-197-\tdefault y\n--\narch/x86/kvm/Kconfig=208=config KVM_XEN\narch/x86/kvm/Kconfig-209-\tbool \"Support for Xen hypercall interface\"\narch/x86/kvm/Kconfig:210:\tdepends on KVM_X86\narch/x86/kvm/Kconfig-211-\thelp\n--\narch/x86/kvm/Kconfig=217=config KVM_PROVE_MMU\narch/x86/kvm/Kconfig-218-\tbool \"Prove KVM MMU correctness\"\narch/x86/kvm/Kconfig:219:\tdepends on DEBUG_KERNEL\narch/x86/kvm/Kconfig:220:\tdepends on KVM_X86\narch/x86/kvm/Kconfig:221:\tdepends on EXPERT\narch/x86/kvm/Kconfig-222-\thelp\n--\narch/x86/kvm/Kconfig=233=config KVM_MAX_NR_VCPUS\narch/x86/kvm/Kconfig-234-\tint \"Maximum number of vCPUs per KVM guest\"\narch/x86/kvm/Kconfig:235:\tdepends on KVM_X86\narch/x86/kvm/Kconfig-236-\trange 1024 4096\n"
}

13/2 2026/08/01 01:25 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 42247
							output: 44
							thoughts: 285
						

Thoughts:
**Analyzing KVM and AMD SVM Configurations**

Okay, so I'm looking at the configuration options related to KVM and, specifically, AMD's Secure Virtual Machine (SVM) implementation. From what I see, `KVM` and `KVM_AMD` are the relevant configs here. `KVM_AMD` seems to depend on `KVM` being enabled, which makes sense, and also requires either `CPU_SUP_AMD` or `CPU_SUP_HYGON`. I'll be sure to include both `KVM` and `KVM_AMD` in my build.

Now, let's zero in on the focus symbols to understand what's being tweaked. I'm seeing: `nested_svm_exit_handled`, `kvm_queue_exception`, `kvm_queue_exception_p`, `kvm_queue_exception_e`, and `kvm_inject_page_fault`.

Ah, right, `nested_svm_exit_handled` looks like a key one. It's probably worth digging into. Let me quickly scan the code to see if there are other functions in `arch/x86/kvm/svm/nested.c` that are also relevant. I need to make sure I don't miss anything that interacts with it. Okay, I've got: `nested_svm_vmrun`, `nested_svm_check_cached_vmcb12`, `nested_svm_exit_special`, `nested_svm_update_tsc_ratio_msr`, and `nested_svm_check_permissions`.

The patch modifies `nested_svm_exit_handled`, and also touches `nested_svm_inject_exception_vmexit`. Now, that latter one is static, but, it is called from `nested_svm_check_exception` which is non-static. Gotta investigate that relationship further to make sure I'm capturing all the impacts.



14/2 2026/08/01 01:25 tool grepper
0m Args:
{
  "Expression": "nested_svm_inject_exception_vmexit",
  "PathPrefix": "arch/x86/kvm/svm/nested.c"
}

Results:
{
  "Output": "arch/x86/kvm/svm/nested.c=1846=static bool nested_svm_is_exception_vmexit(struct kvm_vcpu *vcpu, u8 vector,\n--\narch/x86/kvm/svm/nested.c-1853-\narch/x86/kvm/svm/nested.c:1854:static void nested_svm_inject_exception_vmexit(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/nested.c-1855-{\n--\narch/x86/kvm/svm/nested.c=1898=static int svm_check_nested_events(struct kvm_vcpu *vcpu)\n--\narch/x86/kvm/svm/nested.c-1929-                        return -EBUSY;\narch/x86/kvm/svm/nested.c:1930:\t\tnested_svm_inject_exception_vmexit(vcpu);\narch/x86/kvm/svm/nested.c-1931-\t\treturn 0;\n"
}

15/2 2026/08/01 01:25 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 43273
							output: 38
							thoughts: 28
						

Thoughts:
Okay, here's my take on those thoughts, tailored for an expert audience, presented in the first person:

**Analyzing the Callers of `svm_check_nested_events`**

Right, so `svm_check_nested_events` is static. That immediately piques my interest. Static functions, especially in a system as complex as this, often indicate something significant about the scope and potential implications of their use. My first thought is always to understand the *why* behind its static nature. Is it truly meant to be scoped within a particular module for encapsulation or for thread safety considerations? Or is it a holdover from an earlier design? These are the sorts of questions I tend to immediately address in the face of such considerations.

Now, because it is static, that means that its access is limited. So, *who* is calling this function? I need to track down all the call sites to fully grasp its role. Understanding its context of use is paramount. Is it called from a handful of carefully controlled locations, hinting at a specific purpose? Or is it called promiscuously, which suggests it is more general purpose than I had assumed? I'll probably start with a simple grep or code search, but likely then want to leverage more sophisticated tools to find the answer. The goal is to fully understand the impact of any changes to this function. It might be a critical piece in a delicate dance of event handling, or possibly a candidate for optimization or refactoring. Let's dig in.



16/2 2026/08/01 01:25 tool grepper
0m Args:
{
  "Expression": "svm_check_nested_events",
  "PathPrefix": "arch/x86/kvm/svm/"
}

Results:
{
  "Output": "arch/x86/kvm/svm/nested.c=1785=static int nested_svm_intercept(struct vcpu_svm *svm)\n--\narch/x86/kvm/svm/nested.c-1803-\t\t * nested_svm_exit_special.  There is nothing to do here,\narch/x86/kvm/svm/nested.c:1804:\t\t * the vmexit is injected by svm_check_nested_events.\narch/x86/kvm/svm/nested.c-1805-\t\t */\n--\narch/x86/kvm/svm/nested.c=1893=static inline bool nested_exit_on_init(struct vcpu_svm *svm)\n--\narch/x86/kvm/svm/nested.c-1897-\narch/x86/kvm/svm/nested.c:1898:static int svm_check_nested_events(struct kvm_vcpu *vcpu)\narch/x86/kvm/svm/nested.c-1899-{\n--\narch/x86/kvm/svm/nested.c=2337=struct kvm_x86_nested_ops svm_nested_ops __initdata = {\n--\narch/x86/kvm/svm/nested.c-2340-\t.is_exception_vmexit = nested_svm_is_exception_vmexit,\narch/x86/kvm/svm/nested.c:2341:\t.check_events = svm_check_nested_events,\narch/x86/kvm/svm/nested.c-2342-\t.triple_fault = nested_svm_triple_fault,\n"
}

17/2 2026/08/01 01:25 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 44084
							output: 197
							thoughts: 615
						

Thoughts:
**My Analysis of the KVM Nested SVM Patch**

Okay, so I'm digging into this KVM patch, and it's pretty clear it's focused on supporting `X86_FEATURE_DECODEASSISTS` within nested SVM (specifically, AMD's virtualization). My initial thought process is to zero in on the key functions being modified. Since it's within the `svm_nested_ops` area, the `nested_svm_exit_handled` function immediately seems like a crucial focus symbol. I'm also seeing changes related to exception queuing and page fault injection, leading me to include `kvm_queue_exception`, `kvm_queue_exception_p`, `kvm_queue_exception_e`, and `kvm_inject_page_fault` in my initial list.

I'm double-checking for relevant configurations, and it looks like the patch activates under `KVM` and `KVM_AMD`, which makes sense. There aren't any other configs tied to this.

Alright, before I finalize the symbols, I want to refine this list. While `kvm_queue_exception` and its derivatives are likely called from many places, this patch specifically alters them by adding a `has_emulator_context` parameter to `kvm_multiple_exception`. That’s a direct modification. I'm taking a look to ensure there aren't any other *non-static* functions that were modified here. Looks like the other exception-related functions touched are static (`kvm_queue_exception_e_p`, `kvm_queue_exception_vmexit`, and `kvm_multiple_exception`), which simplifies things slightly.

I briefly considered `kvm_arch_vcpu_ioctl_run`, which is non-static and a hot path. But I'll avoid it. It's too big, and I'd like to get my analysis done quickly.

So, the definitive list of focus symbols is:

*   `nested_svm_exit_handled`
*   `kvm_inject_page_fault`
*   `kvm_queue_exception`
*   `kvm_queue_exception_p`
*   `kvm_queue_exception_e`

Now, to articulate my reasoning: this patch series introduces support for `X86_FEATURE_DECODEASSISTS` in KVM's nested SVM (AMD). It modifies the way instruction intercepts and page faults are handled to correctly capture and synthesize instruction bytes for nested guests. These are all functional changes to critical virtualization logic that will likely need fuzzing.

My reasoning will also include the configs this patch activates:

*   `KVM`
*   `KVM_AMD`

And these are the focus symbols to be fuzzed:

*   `nested_svm_exit_handled`
*   `kvm_inject_page_fault`
*   `kvm_queue_exception`
*   `kvm_queue_exception_p`
*   `kvm_queue_exception_e`

Time to call `set-results`.



18/2 2026/08/01 01:25 tool set-results
0m Args:
{
  "EnableConfigs": [
    "KVM",
    "KVM_AMD"
  ],
  "FocusSymbols": [
    "nested_svm_exit_handled",
    "kvm_inject_page_fault",
    "kvm_queue_exception",
    "kvm_queue_exception_p",
    "kvm_queue_exception_e"
  ],
  "Reasoning": "The patch series implements support for X86_FEATURE_DECODEASSISTS in KVM's nested SVM (AMD virtualization). It modifies the handling of instruction intercepts and page faults to properly capture and synthesize instruction bytes for nested guests. This involves changes to core KVM exception queuing (kvm_queue_exception*, kvm_inject_page_fault) and nested SVM exit handling (nested_svm_exit_handled). These are functional changes to reachable core virtualization logic and should be fuzzed.",
  "WorthFuzzing": true
}

Results:
{
  "EnableConfigs": [
    "KVM",
    "KVM_AMD"
  ],
  "FocusSymbols": [
    "nested_svm_exit_handled",
    "kvm_inject_page_fault",
    "kvm_queue_exception",
    "kvm_queue_exception_p",
    "kvm_queue_exception_e"
  ],
  "Reasoning": "The patch series implements support for X86_FEATURE_DECODEASSISTS in KVM's nested SVM (AMD virtualization). It modifies the handling of instruction intercepts and page faults to properly capture and synthesize instruction bytes for nested guests. This involves changes to core KVM exception queuing (kvm_queue_exception*, kvm_inject_page_fault) and nested SVM exit handling (nested_svm_exit_handled). These are functional changes to reachable core virtualization logic and should be fuzzed.",
  "WorthFuzzing": true
}

LLM Calls Summary:
Total Calls Total Tokens Avg Tokens Total Duration (Seconds) Avg Duration (Seconds)
Tool Calls Summary:
Total Calls Total Duration (Seconds) Avg Duration (Seconds)