Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp8199178rwl; Tue, 10 Jan 2023 10:11:28 -0800 (PST) X-Google-Smtp-Source: AMrXdXv3grGvRvCMc3WwOT3HzPrsHJ5wLi5Sp7sN/SXHaexkrx2NYR80XzOE2upQY/2XEQ1l5yrh X-Received: by 2002:a17:90b:1c04:b0:225:e88c:33c2 with SMTP id oc4-20020a17090b1c0400b00225e88c33c2mr66933079pjb.7.1673374288166; Tue, 10 Jan 2023 10:11:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673374288; cv=none; d=google.com; s=arc-20160816; b=FYXdIrrQx3EFq0DSQF4FJwayt6aymNM8vKb5RvwtwW2qqxUwCgyQDXgD4GQcNxuHrf 0PcibRUi1uRhgl4xPB0PHvrB3vovERP4oel9jxuLzU5UZMrtO0IpZZB4CLjFcRr3QZlA hq14jI1y8R3keZfuMtDG1/rPiuQLoTF9SC6etA/WCuEOue/sKyyBr3Jpar3N+6E52PGs Mwx0/2ih48kWTDOEBaToOpl2LLzTRKsNdpBNieCbQZj38r5CuaNcRqcm6OwcvqEYGjXQ 3uI1JobRKwRiPYNHa7XyVlbQWKADHlc6oPybEDrYm+9mfXVxf+LGYxgDqRS9D0dJDW4t adwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=Kovpab8ufZORC8OQwD/Ffe4tjc6Lhn/m/+zBtc75xtg=; b=AWqYg6yrNErvtHrUo90jVVzgisN+8lxjvoMD5LJrBLBrvyiLlnIs0crr1u/smz7/mC 6N0EaklXxCWKS+orQjFS0VmcH9DJ/poJM2s7W02JfUFS1bvT9pZLezUUbXiBGgLoIVCY y8KrlXq4jw3o0hXCq31UQEqjH0svdI0SItmfyLXS21v/fhcV8si0T1voaIfE5y+7yvP2 P/OUMvt6qmS96F2M3D/K2DZ9fKL+n+P5UingS/b7dBtWunKCowgcbLjADSBu3acfSmNi vxUutNwDAI4wJKKWPpJQnhtHIiCVQcGapESQpQZmmLmQywIJ6CI9d6+gVcCdwaMl76W1 pOyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=jMZkxMMs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w8-20020a63f508000000b00479503041c4si13607329pgh.368.2023.01.10.10.11.21; Tue, 10 Jan 2023 10:11:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=jMZkxMMs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235267AbjAJRvq (ORCPT + 53 others); Tue, 10 Jan 2023 12:51:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36080 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234892AbjAJRvS (ORCPT ); Tue, 10 Jan 2023 12:51:18 -0500 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6DE8F50062 for ; Tue, 10 Jan 2023 09:51:17 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id l17-20020a170902f69100b001928d6b3efcso8806278plg.2 for ; Tue, 10 Jan 2023 09:51:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Kovpab8ufZORC8OQwD/Ffe4tjc6Lhn/m/+zBtc75xtg=; b=jMZkxMMsqWv4TR7GzZ0A7yhf0SNqy4OOCj6DEGOQl/IHadL2JpeOyeLieFcExLaRH+ nC7f/8hRRK2ZDY8aJJS7Isg8WIjwIqAAP3IDu3db0HKTcWPRoWdVX1XSuF+7WySHus97 UC5xJljIGHCht8YAYZgSXXAMH42IdS9I42o0QHTMnN1Of3nWaBtQixI6ZDup12CBJQnL 02FdR3pDScSNA4le2Wc+FdRUb3KHDY3NQb7agC5kIDo40Jh7nROKzfi/rV4ABfS00M8n 2bz7L5pb9uXenODU9xH+unSgUVvxe3BdcNIMWg3FcoFhvIWRhR4p+mAgEL7y8SdmAj5+ 9P7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Kovpab8ufZORC8OQwD/Ffe4tjc6Lhn/m/+zBtc75xtg=; b=r0voSburJZrtutOoI7et0kNkY8NTfcDFVavuG+95GopCK5ovlDsY1EVFp1IbZyuDK2 S0Tc+Hd9CSMoo6PInhn4qaRc4vi0uZ+RnejQvp5LTvuJnEFT9sOmdmM47b6+NgJ4uFTp UjjQUMAAlxLnsjFW2f11E6zfX0yZ/H1475pHa+dCmD03GVAUMDv/PzNAs/OtJGmkoeEp A6T1ga1E0i3YdiW50LIRNLWD/4adgb2bEYrRAgJgcBPJ7HxFT3zajrsndDQab/S1KKv8 M10DCw9qCl5lAfhGpT4c81vS9vBp+ep+gg8cegz5T4y1zOEGYg2ndSP/unqTSk9J2jnH LnPw== X-Gm-Message-State: AFqh2kpaBuotOFfcwsCjSUXK5FrPSSG5VmQC/sJ4lFMLVv4KBHIhw6p2 8VSxw9IWCtwoAqM1xHNLWKo3Dq4bhH8= X-Received: from pgonda1.kir.corp.google.com ([2620:0:1008:11:8358:4c2a:eae1:4752]) (user=pgonda job=sendgmr) by 2002:a17:90a:206:b0:226:9980:67f3 with SMTP id c6-20020a17090a020600b00226998067f3mr280pjc.1.1673373076771; Tue, 10 Jan 2023 09:51:16 -0800 (PST) Date: Tue, 10 Jan 2023 09:50:57 -0800 In-Reply-To: <20230110175057.715453-1-pgonda@google.com> Message-Id: <20230110175057.715453-8-pgonda@google.com> Mime-Version: 1.0 References: <20230110175057.715453-1-pgonda@google.com> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Subject: [PATCH V6 7/7] KVM: selftests: Add simple sev vm testing From: Peter Gonda To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Peter Gonda , Paolo Bonzini , Sean Christopherson , Vishal Annapurve , Ackerley Tng , Andrew Jones , Michael Roth Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org A very simple of booting SEV guests that checks related CPUID bits. This is a stripped down version of "[PATCH v2 08/13] KVM: selftests: add SEV boot tests" from Michael but much simpler. Cc: Paolo Bonzini Cc: Sean Christopherson Cc: Vishal Annapurve Cc: Ackerley Tng cc: Andrew Jones Suggested-by: Michael Roth Signed-off-by: Peter Gonda --- tools/testing/selftests/kvm/.gitignore | 84 +++++++++++++++++++ tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/x86_64/sev_all_boot_test.c | 84 +++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore index 6d9381d60172..6d826957c6ae 100644 --- a/tools/testing/selftests/kvm/.gitignore +++ b/tools/testing/selftests/kvm/.gitignore @@ -1,7 +1,91 @@ # SPDX-License-Identifier: GPL-2.0-only +<<<<<<< HEAD * !/**/ !*.c !*.h !*.S !*.sh +======= +/aarch64/aarch32_id_regs +/aarch64/arch_timer +/aarch64/debug-exceptions +/aarch64/get-reg-list +/aarch64/hypercalls +/aarch64/psci_test +/aarch64/vcpu_width_config +/aarch64/vgic_init +/aarch64/vgic_irq +/s390x/memop +/s390x/resets +/s390x/sync_regs_test +/s390x/tprot +/x86_64/amx_test +/x86_64/cpuid_test +/x86_64/cr4_cpuid_sync_test +/x86_64/debug_regs +/x86_64/evmcs_test +/x86_64/emulator_error_test +/x86_64/fix_hypercall_test +/x86_64/get_msr_index_features +/x86_64/kvm_clock_test +/x86_64/kvm_pv_test +/x86_64/hyperv_clock +/x86_64/hyperv_cpuid +/x86_64/hyperv_features +/x86_64/hyperv_svm_test +/x86_64/max_vcpuid_cap_test +/x86_64/mmio_warning_test +/x86_64/monitor_mwait_test +/x86_64/nested_exceptions_test +/x86_64/nx_huge_pages_test +/x86_64/platform_info_test +/x86_64/pmu_event_filter_test +/x86_64/set_boot_cpu_id +/x86_64/set_sregs_test +/x86_64/sev_all_boot_test +/x86_64/sev_migrate_tests +/x86_64/smm_test +/x86_64/state_test +/x86_64/svm_vmcall_test +/x86_64/svm_int_ctl_test +/x86_64/svm_nested_soft_inject_test +/x86_64/sync_regs_test +/x86_64/tsc_msrs_test +/x86_64/tsc_scaling_sync +/x86_64/ucna_injection_test +/x86_64/userspace_io_test +/x86_64/userspace_msr_exit_test +/x86_64/vmx_apic_access_test +/x86_64/vmx_close_while_nested_test +/x86_64/vmx_dirty_log_test +/x86_64/vmx_exception_with_invalid_guest_state +/x86_64/vmx_invalid_nested_guest_state +/x86_64/vmx_msrs_test +/x86_64/vmx_preemption_timer_test +/x86_64/vmx_set_nested_state_test +/x86_64/vmx_tsc_adjust_test +/x86_64/vmx_nested_tsc_scaling_test +/x86_64/xapic_ipi_test +/x86_64/xapic_state_test +/x86_64/xen_shinfo_test +/x86_64/xen_vmcall_test +/x86_64/xss_msr_test +/x86_64/vmx_pmu_caps_test +/x86_64/triple_fault_event_test +/access_tracking_perf_test +/demand_paging_test +/dirty_log_test +/dirty_log_perf_test +/hardware_disable_test +/kvm_create_max_vcpus +/kvm_page_table_test +/max_guest_memory_test +/memslot_modification_stress_test +/memslot_perf_test +/rseq_test +/set_memory_region_test +/steal_time +/kvm_binary_stats_test +/system_counter_offset_test +>>>>>>> KVM: selftests: Add simple sev vm testing diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index b7cfb15712d1..66d7ab3da990 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -111,6 +111,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/tsc_msrs_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_pmu_caps_test TEST_GEN_PROGS_x86_64 += x86_64/xen_shinfo_test TEST_GEN_PROGS_x86_64 += x86_64/xen_vmcall_test +TEST_GEN_PROGS_x86_64 += x86_64/sev_all_boot_test TEST_GEN_PROGS_x86_64 += x86_64/sev_migrate_tests TEST_GEN_PROGS_x86_64 += x86_64/amx_test TEST_GEN_PROGS_x86_64 += x86_64/max_vcpuid_cap_test diff --git a/tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c b/tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file mode 100644 index 000000000000..e9e4d7305bc1 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c @@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Basic SEV boot tests. + * + */ +#include +#include +#include +#include +#include + +#include "test_util.h" +#include "kvm_util.h" +#include "processor.h" +#include "svm_util.h" +#include "linux/psp-sev.h" +#include "sev.h" + +#define NR_SYNCS 1 + +#define MSR_AMD64_SEV_BIT 1 + +static void guest_run_loop(struct kvm_vcpu *vcpu) +{ + struct ucall uc; + int i; + + for (i = 0; i <= NR_SYNCS; ++i) { + vcpu_run(vcpu); + switch (get_ucall(vcpu, &uc)) { + case UCALL_SYNC: + continue; + case UCALL_DONE: + return; + case UCALL_ABORT: + REPORT_GUEST_ASSERT(uc); + default: + TEST_FAIL("Unexpected exit: %s", + exit_reason_str(vcpu->run->exit_reason)); + } + } +} + +static void is_sev_enabled(void) +{ + uint64_t sev_status; + + GUEST_ASSERT(this_cpu_has(X86_FEATURE_SEV)); + + sev_status = rdmsr(MSR_AMD64_SEV); + GUEST_ASSERT(sev_status & 0x1); +} + +static void guest_sev_code(void) +{ + GUEST_SYNC(1); + + is_sev_enabled(); + + GUEST_DONE(); +} + +static void test_sev(void *guest_code, uint64_t policy) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + + vm = vm_sev_create_with_one_vcpu(policy, guest_code, &vcpu); + TEST_ASSERT(vm, "vm_sev_create_with_one_vcpu() failed to create VM\n"); + + guest_run_loop(vcpu); + + kvm_vm_free(vm); +} + +int main(int argc, char *argv[]) +{ + TEST_REQUIRE(is_kvm_sev_supported()); + + test_sev(guest_sev_code, SEV_POLICY_NO_DBG); + test_sev(guest_sev_code, 0); + + return 0; +} -- 2.39.0.314.g84b9a713c41-goog