Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3BA41401A29 for ; Thu, 12 Mar 2026 20:03:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773345808; cv=none; b=geNfylqoxZ3UicRg2PzwxSOJhP/d1Xfs6/rXsLNjtF/agaoy1KAvDKDTyNVWAxk+f4EKz/xErbGucGdGGks1WfyJInhYWS8MiCDdPPfXRRPKl41ao1LZO60wkJpZ3e/rQ1QS0ByJtHhJbM6cxGbjilTQsNwefNaFz/nxg2eM9B0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773345808; c=relaxed/simple; bh=ZLte43AgyqCBTBTVJva5FHfvvh0lFjVcFj1ZAZlZRdM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=L+wgDwSTQkt6ROR4u7Wkspr+8ZTmBOPrh6rdZKKRRjx9LPjROn20nlIV+HT6nWYQaRV4S+bWG/Ndy7ukBt0Xql+B97hG5VffXhlUQoj5DJLYInAOvYP+ttpi4y59qZYIvcr3RG1kuX382pLRfRxQMhfyXI1Jv+skiMVwwQMNbFc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--chengkev.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=x+ebQbAa; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--chengkev.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="x+ebQbAa" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-35842aa350fso8861164a91.0 for ; Thu, 12 Mar 2026 13:03:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1773345805; x=1773950605; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Vg2Mq6iBeWpOAkGTRcOZV3k7DhwYMvBaG2sfKPCD8oI=; b=x+ebQbAanG7wrxvlXAYuJW/z5o8vH/IxQWPsitl72rPzYthvWpuUVbL33Pq6vcLjir CqMVhPeALfFbM8qyl6bGJyCCGKUyT8tJXf0S37etInU9gokIVLsOcvqAffzQT8XEMmVk EhP8CM8uf+nvwj+4PV3ZHj1VZLwEDoBaHAVOl+pTwT2B4AoBpFwPc873wgchP30foaQh yBUddXkjIYqlpl1MmeUAMMstDODMK/hZbAGfQhjJ8iun7xhwGcPd73KK4+gouxqHk8E0 Cwu90LecIJcM8apQyM6IlHBU1diOkic83vqJ3jjSn8DPUXCnF1mcIz3PXzaZgmUdMIDT P9+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773345805; x=1773950605; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Vg2Mq6iBeWpOAkGTRcOZV3k7DhwYMvBaG2sfKPCD8oI=; b=F2/VCzJWM8DpC87i00/NDLCVmpBWvZeBDYAVwVvDrajIaS7XcTKrguuLBJx51oJlQ0 0998MmFBS8KDzvVVcGaVOlAq/1w8MnwyvLX/ktN7gGYUcxxB+qAgIRggTWCdMZ5OOrxI 7tiIoEX6/2HvWJYCa+E1vMoauY2KkQ5IfeWuwzdfVrp8/pBaG5bJXVAqw+RcPIyqZVOS Nd8aEyE1ZevucYLXS/jzpjQJ6nxJsjasQHADN/Mcod+PYBpshvJRRgbB62FP59zbApEl GGvZgb/i/I91Lepu1IxtUz66pSiwZ00DCcttXCRTNJQ+l0I4DtkpdRxrlilu/7DJLOWJ T8Qg== X-Gm-Message-State: AOJu0YxUb0eK1o8JL38ZZRhljAzFOjVIikZvTflkEW1YHICVCcDzYbm0 hXOvk7lQobsM+gEqPd4juxTTMt7FET3fhR6B9boegopjDwRQYMWpleH1WxujnpfAazSp6q6zWza bRpTAIh/cLhCHt6IrBZl61c5raDL4APziDY2MfYv8kXJPIyFzyGC9sCpyz8p/r/N8jTJff+YuYY YPqenJ7E+MaSvGJ/A02A5kamSlXIkEAhR662kNTp+mQNw= X-Received: from pgbeq17.prod.google.com ([2002:a05:6a02:2691:b0:c6e:3a04:21e3]) (user=chengkev job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4f4a:b0:359:8411:a40 with SMTP id 98e67ed59e1d1-35a21c97534mr864440a91.0.1773345805160; Thu, 12 Mar 2026 13:03:25 -0700 (PDT) Date: Thu, 12 Mar 2026 20:03:07 +0000 In-Reply-To: <20260312200308.3089379-1-chengkev@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260312200308.3089379-1-chengkev@google.com> X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog Message-ID: <20260312200308.3089379-10-chengkev@google.com> Subject: [kvm-unit-tests PATCH v3 09/10] x86/svm: Add event injection check tests From: Kevin Cheng To: kvm@vger.kernel.org Cc: yosry@kernel.org, andrew.jones@linux.dev, thuth@redhat.com, pbonzini@redhat.com, seanjc@google.com, Kevin Cheng Content-Type: text/plain; charset="UTF-8" The APM Vol #2 - 15.20 lists illegal combinations related to event injection. Add testing to verify that these illegal combinations cause an invalid VM exit. Also add testing to verify that legal combinations for event injection work as intended. This includes testing all valid injection types and injecting all exceptions when the exception type is specified. Signed-off-by: Kevin Cheng --- lib/x86/processor.h | 5 ++ x86/svm_tests.c | 195 ++++++++++++++++++++++++++++---------------- x86/unittests.cfg | 9 +- 3 files changed, 137 insertions(+), 72 deletions(-) diff --git a/lib/x86/processor.h b/lib/x86/processor.h index b073ee168ce4..0ae812df59b1 100644 --- a/lib/x86/processor.h +++ b/lib/x86/processor.h @@ -528,6 +528,11 @@ static inline bool this_cpu_has_mwait(void) return this_cpu_has(X86_FEATURE_MWAIT); } +static inline bool this_cpu_has_shstk(void) +{ + return this_cpu_has(X86_FEATURE_SHSTK); +} + struct far_pointer32 { u32 offset; u16 selector; diff --git a/x86/svm_tests.c b/x86/svm_tests.c index a40468693b39..5d2728612933 100644 --- a/x86/svm_tests.c +++ b/x86/svm_tests.c @@ -1674,74 +1674,6 @@ static bool vnmi_check(struct svm_test *test) return get_test_stage(test) == 3; } -static volatile int count_exc = 0; - -static void my_isr(struct ex_regs *r) -{ - count_exc++; -} - -static void exc_inject_prepare(struct svm_test *test) -{ - default_prepare(test); - handle_exception(DE_VECTOR, my_isr); - handle_exception(NMI_VECTOR, my_isr); -} - - -static void exc_inject_test(struct svm_test *test) -{ - asm volatile ("vmmcall\n\tvmmcall\n\t"); -} - -static bool exc_inject_finished(struct svm_test *test) -{ - switch (get_test_stage(test)) { - case 0: - if (vmcb->control.exit_code != SVM_EXIT_VMMCALL) { - report_fail("VMEXIT not due to vmmcall. Exit reason 0x%x", - vmcb->control.exit_code); - return true; - } - vmcb->save.rip += 3; - vmcb->control.event_inj = NMI_VECTOR | SVM_EVTINJ_TYPE_EXEPT | SVM_EVTINJ_VALID; - break; - - case 1: - if (vmcb->control.exit_code != SVM_EXIT_ERR) { - report_fail("VMEXIT not due to error. Exit reason 0x%x", - vmcb->control.exit_code); - return true; - } - report(count_exc == 0, "exception with vector 2 not injected"); - vmcb->control.event_inj = DE_VECTOR | SVM_EVTINJ_TYPE_EXEPT | SVM_EVTINJ_VALID; - break; - - case 2: - if (vmcb->control.exit_code != SVM_EXIT_VMMCALL) { - report_fail("VMEXIT not due to vmmcall. Exit reason 0x%x", - vmcb->control.exit_code); - return true; - } - vmcb->save.rip += 3; - report(count_exc == 1, "divide overflow exception injected"); - report(!(vmcb->control.event_inj & SVM_EVTINJ_VALID), "eventinj.VALID cleared"); - break; - - default: - return true; - } - - inc_test_stage(test); - - return get_test_stage(test) == 3; -} - -static bool exc_inject_check(struct svm_test *test) -{ - return count_exc == 1 && get_test_stage(test) == 3; -} - static volatile bool virq_fired; static volatile unsigned long virq_rip; @@ -2548,6 +2480,129 @@ static void test_dr(void) vmcb->save.dr7 = dr_saved; } +/* Returns true if exception can be injected via the SVM_EVTINJ_TYPE_EXEPT type */ +static bool is_injectable_exception(int vec) +{ + /* + * Vectors that do not correspond to an exception are excluded. NMI is + * not an exception so it is excluded. BR and OF are excluded because + * BOUND and INTO are not legal in 64-bit mode. + * + * The VE vector is excluded because it is Intel only. + * + * The HV and VC vectors are excluded because they are only relevant + * within secure guest VMs. + */ + u8 exception_vectors[32] = { + [DE_VECTOR] = 1, [DB_VECTOR] = 1, + [BP_VECTOR] = 1, [UD_VECTOR] = 1, + [NM_VECTOR] = 1, [DF_VECTOR] = 1, + [TS_VECTOR] = 1, [NP_VECTOR] = 1, + [SS_VECTOR] = 1, [GP_VECTOR] = 1, + [PF_VECTOR] = 1, [MF_VECTOR] = 1, + [AC_VECTOR] = 1, [MC_VECTOR] = 1, + [XF_VECTOR] = 1, [CP_VECTOR] = this_cpu_has_shstk(), + [SX_VECTOR] = 1, + }; + + return exception_vectors[vec]; +} + +static bool is_valid_injection_type_mask(int type_mask) +{ + return type_mask == SVM_EVTINJ_TYPE_INTR || + type_mask == SVM_EVTINJ_TYPE_NMI || + type_mask == SVM_EVTINJ_TYPE_EXEPT || + type_mask == SVM_EVTINJ_TYPE_SOFT; +} + +static volatile bool event_injection_handled; +static void event_injection_irq_handler(isr_regs_t *regs) +{ + event_injection_handled = true; + vmmcall(); +} + +static void event_injection_exception_handler(struct ex_regs *r) +{ + event_injection_handled = true; + vmmcall(); +} + +static void svm_event_injection(void) +{ + u32 event_inj_saved = vmcb->control.event_inj, vector = 0x22, event_inj; + int type, type_mask; + bool reserved; + + handle_exception(DE_VECTOR, event_injection_exception_handler); + handle_irq(vector, event_injection_irq_handler); + + /* Setting reserved values of TYPE is illegal */ + for (type = 0; type < 8; type++) { + type_mask = type << SVM_EVTINJ_TYPE_SHIFT; + reserved = !is_valid_injection_type_mask(type_mask); + event_injection_handled = false; + event_inj = SVM_EVTINJ_VALID; + + switch (type_mask) { + case SVM_EVTINJ_TYPE_EXEPT: + event_inj |= DE_VECTOR; + break; + default: + event_inj |= vector; + } + + vmcb->control.event_inj = event_inj | + (type << SVM_EVTINJ_TYPE_SHIFT); + if (reserved) { + report(svm_vmrun() == SVM_EXIT_ERR, + "Test EVENTINJ error code with type %d", type); + report(!event_injection_handled, + "Reserved type %d ignores EVENTINJ vector field", type); + } else { + report(svm_vmrun() == SVM_EXIT_VMMCALL, + "Test EVENTINJ delivers with type %d", type); + } + + if (type_mask == SVM_EVTINJ_TYPE_NMI) + report(!event_injection_handled, + "Injected NMI ignores EVENTINJ vector field"); + else if (!reserved) + report(event_injection_handled, + "Test EVENTINJ IRQ handler invoked with type %d", type); + + vmcb->control.event_inj = event_inj_saved; + } + + /* + * It is illegal to specify event injection type 3 (Exception) with a + * vector that does not correspond to an exception. + */ + event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT; + for (vector = 0; vector < 256; vector++) { + vmcb->control.event_inj = event_inj | vector; + event_injection_handled = false; + + if (vector >= 32 || !is_injectable_exception(vector)) { + report(svm_vmrun() == SVM_EXIT_ERR, + "Test EVENTINJ exception type error code with vector %d", + vector); + } else { + handle_exception(vector, event_injection_exception_handler); + report(svm_vmrun() == SVM_EXIT_VMMCALL, + "Test EVENTINJ exception type delivers with vector %d", + vector); + report(event_injection_handled, + "Test EVENTINJ exception handler invoked with vector %d", + vector); + } + + vmcb->control.event_inj = event_inj_saved; + } +} + + asm( "insn_sidt: sidt idt_descr;ret\n\t" "insn_sgdt: sgdt gdt_descr;ret\n\t" @@ -4074,9 +4129,6 @@ struct svm_test svm_tests[] = { { "latency_svm_insn", default_supported, lat_svm_insn_prepare, default_prepare_gif_clear, null_test, lat_svm_insn_finished, lat_svm_insn_check }, - { "exc_inject", default_supported, exc_inject_prepare, - default_prepare_gif_clear, exc_inject_test, - exc_inject_finished, exc_inject_check }, { "pending_event", default_supported, pending_event_prepare, default_prepare_gif_clear, pending_event_test, pending_event_finished, pending_event_check }, @@ -4164,6 +4216,7 @@ struct svm_test svm_tests[] = { TEST(pause_filter_test), TEST(svm_shutdown_intercept_test), TEST(svm_insn_intercept_test), + TEST(svm_event_injection), { NULL, NULL, NULL, NULL, NULL, NULL, NULL } }; diff --git a/x86/unittests.cfg b/x86/unittests.cfg index 65dcf8b6cba8..118e7cdd0286 100644 --- a/x86/unittests.cfg +++ b/x86/unittests.cfg @@ -253,7 +253,14 @@ arch = x86_64 [svm] file = svm.flat smp = 2 -test_args = "-pause_filter_test -svm_intr_intercept_mix_smi -svm_insn_intercept_test -svm_pf_exception_test -svm_pf_exception_forced_emulation_test -svm_pf_inv_asid_test -svm_pf_inv_tlb_ctl_test" +test_args = "-pause_filter_test -svm_intr_intercept_mix_smi -svm_insn_intercept_test -svm_pf_exception_test -svm_pf_exception_forced_emulation_test -svm_pf_inv_asid_test -svm_pf_inv_tlb_ctl_test -svm_event_injection" +qemu_params = -cpu max,+svm -m 4g +arch = x86_64 +groups = svm + +[svm_event_injection] +file = svm.flat +test_args = svm_event_injection qemu_params = -cpu max,+svm -m 4g arch = x86_64 groups = svm -- 2.53.0.851.ga537e3e6e9-goog