From patchwork Mon Jun 13 17:04:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vasant Karasulli X-Patchwork-Id: 12879963 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3D06DCCA47B for ; Mon, 13 Jun 2022 19:10:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346213AbiFMTKm (ORCPT ); Mon, 13 Jun 2022 15:10:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33100 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348071AbiFMTIG (ORCPT ); Mon, 13 Jun 2022 15:08:06 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6CB4344FC; Mon, 13 Jun 2022 10:04:34 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 278C01F8F9; Mon, 13 Jun 2022 17:04:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1655139873; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gn9ietmj8gdTelf73NeKf1ZXLc/MBdCgQzvoK6rFKsE=; b=Nv//hmAtfJPmP6Bq4t85OZg9VXxJudcJ7cd0Q8wGPinYnGEwuBdsF0ilpN6GwSiLQ1dRLX 4Tz0I8r2dK/WpWqKM064Z8U7cEp7i/VM0okdqk4GFhCk+BA9T4GOuf8nYFOryOSrBBwPjY w5fSODjvzyElYwowuJKzG31goJuDn5I= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1655139873; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gn9ietmj8gdTelf73NeKf1ZXLc/MBdCgQzvoK6rFKsE=; b=9kDsIcxxI3aP5ACZMBTiUhPfiXzIymnZSscNKj/EmShRMKuR+yNYaya6GodHK9U6H9b50u xhiq1t8hIo/HrNCA== Received: from vasant-suse.fritz.box (unknown [10.163.24.178]) by relay2.suse.de (Postfix) with ESMTP id E0F272C141; Mon, 13 Jun 2022 17:04:32 +0000 (UTC) From: Vasant Karasulli To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: bp@alien8.de, jroedel@suse.de, thomas.lendacky@amd.com, x86@kernel.org, seanjc@google.com, Vasant Karasulli Subject: [PATCH v7 1/4] KVM: SEV-ES: Add configuration options to test #VC handling Date: Mon, 13 Jun 2022 19:04:17 +0200 Message-Id: <20220613170420.18521-2-vkarasulli@suse.de> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220613170420.18521-1-vkarasulli@suse.de> References: <20220613170420.18521-1-vkarasulli@suse.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Configuration options CONFIG_X86_TESTS and CONFIG_AMD_SEV_ES_TEST_VC enable the execution of the KUnit tests added under arch/x86/tests that test AMD SEV-ES #VC handling. Signed-off-by: Vasant Karasulli --- arch/x86/Kbuild | 2 ++ arch/x86/Kconfig.debug | 19 +++++++++++++++++++ arch/x86/kernel/Makefile | 7 +++++++ 3 files changed, 28 insertions(+) -- 2.32.0 diff --git a/arch/x86/Kbuild b/arch/x86/Kbuild index 5a83da703e87..ec01cd024bae 100644 --- a/arch/x86/Kbuild +++ b/arch/x86/Kbuild @@ -28,5 +28,7 @@ obj-y += net/ obj-$(CONFIG_KEXEC_FILE) += purgatory/ +obj-y += tests/ + # for cleaning subdir- += boot tools diff --git a/arch/x86/Kconfig.debug b/arch/x86/Kconfig.debug index 340399f69954..b0687173fb97 100644 --- a/arch/x86/Kconfig.debug +++ b/arch/x86/Kconfig.debug @@ -278,3 +278,22 @@ endchoice config FRAME_POINTER depends on !UNWINDER_ORC && !UNWINDER_GUESS bool + +config X86_TESTS + bool "x86 unit tests" + help + This enables building the tests under arch/x86/tests. + +if X86_TESTS +config AMD_SEV_TEST_VC + bool "Test for AMD SEV VC exception handling" + depends on AMD_MEM_ENCRYPT + select FUNCTION_TRACER + select KPROBES + select KUNIT + help + Enable KUnit-based testing for AMD SEV #VC exception handling. + When this configuration option is enabled, these Kunit tests + get executed at the kernel boot time. Results of the test + execution can be monitored in the kernel log. +endif # X86_TESTS diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile index 03364dc40d8d..3fa9ce2a700e 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -23,6 +23,13 @@ CFLAGS_REMOVE_head64.o = -pg CFLAGS_REMOVE_sev.o = -pg endif +# AMD_SEV_TEST_VC registers a kprobe by function name. IPA-SRA creates +# function copies and renames them to have an .isra suffix, which breaks kprobes' +# lookup. Build with -fno-ipa-sra for the test. +ifdef CONFIG_AMD_SEV_TEST_VC +CFLAGS_sev.o += -fno-ipa-sra +endif + KASAN_SANITIZE_head$(BITS).o := n KASAN_SANITIZE_dumpstack.o := n KASAN_SANITIZE_dumpstack_$(BITS).o := n From patchwork Mon Jun 13 17:04:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vasant Karasulli X-Patchwork-Id: 12879964 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1CF6BCCA485 for ; Mon, 13 Jun 2022 19:10:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348069AbiFMTKq (ORCPT ); Mon, 13 Jun 2022 15:10:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347659AbiFMTIG (ORCPT ); Mon, 13 Jun 2022 15:08:06 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B83AE344E2; Mon, 13 Jun 2022 10:04:34 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 6850C21B02; Mon, 13 Jun 2022 17:04:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1655139873; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=b7X589VWrIUFJoNoWquElpqrv8TAnvhX16s1XXo/inY=; b=RikLPCF39pVt3g3b4ugbUekSgX96TB3fL+WAFK5x9fSGCQxmswvZfjuWv+vJ3UwLpYdd8p JcmvyiN4zHj2LyDI7N12G0RGEThqKpODd59Ua6Wz8i3isycni2n+fXx4oGl9THn+R+BLF4 O4iLbslL8oVJjNLZQdTERDzBBBSxiFY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1655139873; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=b7X589VWrIUFJoNoWquElpqrv8TAnvhX16s1XXo/inY=; b=eNl5+EEZ5LPM+oVlbys4uV0gdHanrKxULODcCG8Ql4RyL6SD3K90H1hwFnLH33O3ZDhIR/ WwTarEir+HDmNQAg== Received: from vasant-suse.fritz.box (unknown [10.163.24.178]) by relay2.suse.de (Postfix) with ESMTP id 2E78F2C142; Mon, 13 Jun 2022 17:04:33 +0000 (UTC) From: Vasant Karasulli To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: bp@alien8.de, jroedel@suse.de, thomas.lendacky@amd.com, x86@kernel.org, seanjc@google.com, Vasant Karasulli Subject: [PATCH v7 2/4] KVM: SEV-ES: Add tests to validate VC handling for CPUID and WBINVD Date: Mon, 13 Jun 2022 19:04:18 +0200 Message-Id: <20220613170420.18521-3-vkarasulli@suse.de> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220613170420.18521-1-vkarasulli@suse.de> References: <20220613170420.18521-1-vkarasulli@suse.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org These tests: 1. install a kretprobe on the #VC handler (sev_es_ghcb_hv_call, to access GHCB before/after the resulting VMGEXIT). 2. trigger an NAE by executing either CPUID or WBINVD. 3. check that the kretprobe was hit with the right exit_code available in GHCB. To run these tests, configuration options CONFIG_X86_TESTS and CONFIG_AMD_SEV_ES_TEST_VC have to be enabled. These tests run at the kernel boot time. Result of the test execution can be monitored in the kernel log. Signed-off-by: Vasant Karasulli --- arch/x86/tests/Makefile | 3 + arch/x86/tests/sev-test-vc.c | 104 +++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 arch/x86/tests/Makefile create mode 100644 arch/x86/tests/sev-test-vc.c -- 2.32.0 diff --git a/arch/x86/tests/Makefile b/arch/x86/tests/Makefile new file mode 100644 index 000000000000..4beca64bd2aa --- /dev/null +++ b/arch/x86/tests/Makefile @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-$(CONFIG_AMD_SEV_TEST_VC) += sev-test-vc.o diff --git a/arch/x86/tests/sev-test-vc.c b/arch/x86/tests/sev-test-vc.c new file mode 100644 index 000000000000..900ca357a273 --- /dev/null +++ b/arch/x86/tests/sev-test-vc.c @@ -0,0 +1,104 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2021 SUSE + * + * Author: Varad Gautam + */ + +#include +#include +#include +#include +#include + +static struct kretprobe hv_call_krp; + +static int hv_call_krp_entry(struct kretprobe_instance *krpi, + struct pt_regs *regs) +{ + unsigned long ghcb_vaddr = regs_get_kernel_argument(regs, 0); + *((unsigned long *) krpi->data) = ghcb_vaddr; + + return 0; +} + +static int hv_call_krp_ret(struct kretprobe_instance *krpi, + struct pt_regs *regs) +{ + unsigned long ghcb_vaddr = *((unsigned long *) krpi->data); + struct ghcb *ghcb = (struct ghcb *) ghcb_vaddr; + struct kunit *test = current->kunit_test; + + if (test && strstr(test->name, "sev_es_")) + *((u64 *)&test->priv) = ghcb->save.sw_exit_code; + + return 0; +} + +int sev_es_test_vc_init(struct kunit *test) +{ + int ret; + + if (!cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT)) { + kunit_info(test, "Not a SEV-ES guest. Skipping."); + ret = -EINVAL; + goto out; + } + + memset(&hv_call_krp, 0, sizeof(hv_call_krp)); + hv_call_krp.entry_handler = hv_call_krp_entry; + hv_call_krp.handler = hv_call_krp_ret; + hv_call_krp.maxactive = 100; + hv_call_krp.data_size = sizeof(unsigned long); + hv_call_krp.kp.symbol_name = "sev_es_ghcb_hv_call"; + hv_call_krp.kp.addr = 0; + + ret = register_kretprobe(&hv_call_krp); + if (ret) { + kunit_info(test, "Could not register kretprobe. Skipping."); + goto out; + } + +out: + return ret; +} + +void sev_es_test_vc_exit(struct kunit *test) +{ + if (hv_call_krp.kp.addr) + unregister_kretprobe(&hv_call_krp); +} + +#define check_op(kt, ec, op) \ +do { \ + struct kunit *t = (struct kunit *) kt; \ + op; \ + KUNIT_EXPECT_EQ(t, (typeof(ec)) ec, \ + ((typeof(ec))((u64)(t->priv))));\ +} while (0) + +static void sev_es_nae_cpuid(struct kunit *test) +{ + unsigned int cpuid_fn = 0x8000001f; + + check_op(test, SVM_EXIT_CPUID, native_cpuid_eax(cpuid_fn)); +} + +static void sev_es_nae_wbinvd(struct kunit *test) +{ + check_op(test, SVM_EXIT_WBINVD, wbinvd()); +} + +static struct kunit_case sev_es_vc_testcases[] = { + KUNIT_CASE(sev_es_nae_cpuid), + KUNIT_CASE(sev_es_nae_wbinvd), + {} +}; + +static struct kunit_suite sev_es_vc_test_suite = { + .name = "sev_es_test_vc", + .init = sev_es_test_vc_init, + .exit = sev_es_test_vc_exit, + .test_cases = sev_es_vc_testcases, +}; +kunit_test_suite(sev_es_vc_test_suite); From patchwork Mon Jun 13 17:04:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vasant Karasulli X-Patchwork-Id: 12879961 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 07E5ACCA47B for ; Mon, 13 Jun 2022 19:10:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347266AbiFMTKj (ORCPT ); Mon, 13 Jun 2022 15:10:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33106 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347693AbiFMTIG (ORCPT ); Mon, 13 Jun 2022 15:08:06 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2C2D35262; Mon, 13 Jun 2022 10:04:34 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id A5AFB21B20; Mon, 13 Jun 2022 17:04:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1655139873; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=y7XqRusKLZHg2MAsYj31tc7FKcxL4xcYD2sDGg0rjck=; b=1uGoiuXPUKuk38YwfxQ/jX0RG/8piqm5i7lVWT4hCP6Kcj485+B2yDRsOBGEegq+hHWsF9 4UrOkwPiz6ZTfjOxVhaLrpb3ABC0etDA9HNeCHJF4cEFefQyw5p1lqjVefO5+oO3jgF52E +WoSsMQsf7FC73XBkp4HvAKDnhRVziY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1655139873; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=y7XqRusKLZHg2MAsYj31tc7FKcxL4xcYD2sDGg0rjck=; b=ETxTyfqN4F2E592ZrQLZuJb45I5sVfYoswnJy5ON37LkEklgxbDy0l4KKTSkvIkE6o6tJh S4FjFoB1G68ubnCA== Received: from vasant-suse.fritz.box (unknown [10.163.24.178]) by relay2.suse.de (Postfix) with ESMTP id 6E6D92C143; Mon, 13 Jun 2022 17:04:33 +0000 (UTC) From: Vasant Karasulli To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: bp@alien8.de, jroedel@suse.de, thomas.lendacky@amd.com, x86@kernel.org, seanjc@google.com, Vasant Karasulli Subject: [PATCH v7 3/4] KVM: SEV-ES: Add tests to validate VC handling for MSR and DR7 register accesses Date: Mon, 13 Jun 2022 19:04:19 +0200 Message-Id: <20220613170420.18521-4-vkarasulli@suse.de> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220613170420.18521-1-vkarasulli@suse.de> References: <20220613170420.18521-1-vkarasulli@suse.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org These tests: 1. install a kretprobe on the #VC handler (sev_es_ghcb_hv_call, to access GHCB before/after the resulting VMGEXIT). 2. trigger an NAE by accessing either MSR or DR7. 3. check that the kretprobe was hit with the right exit_code available in GHCB. To run these tests, configuration options CONFIG_X86_TESTS and CONFIG_AMD_SEV_ES_TEST_VC have to be enabled. These tests run at the kernel boot time. Result of the test execution can be monitored in the kernel log. Signed-off-by: Vasant Karasulli --- arch/x86/tests/sev-test-vc.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) -- 2.32.0 diff --git a/arch/x86/tests/sev-test-vc.c b/arch/x86/tests/sev-test-vc.c index 900ca357a273..629aa0ca1c86 100644 --- a/arch/x86/tests/sev-test-vc.c +++ b/arch/x86/tests/sev-test-vc.c @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -89,9 +90,22 @@ static void sev_es_nae_wbinvd(struct kunit *test) check_op(test, SVM_EXIT_WBINVD, wbinvd()); } +static void sev_es_nae_msr(struct kunit *test) +{ + check_op(test, SVM_EXIT_MSR, __rdmsr(MSR_IA32_TSC)); +} + +static void sev_es_nae_dr7_rw(struct kunit *test) +{ + check_op(test, SVM_EXIT_WRITE_DR7, + native_set_debugreg(7, native_get_debugreg(7))); +} + static struct kunit_case sev_es_vc_testcases[] = { KUNIT_CASE(sev_es_nae_cpuid), KUNIT_CASE(sev_es_nae_wbinvd), + KUNIT_CASE(sev_es_nae_msr), + KUNIT_CASE(sev_es_nae_dr7_rw), {} }; From patchwork Mon Jun 13 17:04:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vasant Karasulli X-Patchwork-Id: 12879965 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 94E53CCA489 for ; Mon, 13 Jun 2022 19:10:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348388AbiFMTKs (ORCPT ); Mon, 13 Jun 2022 15:10:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55774 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347719AbiFMTIG (ORCPT ); Mon, 13 Jun 2022 15:08:06 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 362303700E; Mon, 13 Jun 2022 10:04:35 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id E371821BD6; Mon, 13 Jun 2022 17:04:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1655139873; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7OtOWClBlY6896CUL2CJUoXz5Le8Wk1ooKrdR/9iq2M=; b=xp0mmWyEfPaQm+HgDu9ihr5D73ZTYPe7veggysbfEXnAI4CVVUVIEpa1f0FyNiFf2lYknH g1KdLbOxKBEkWIixu7mecIMyTD4oM/KDOwd+1A6knCkskwZmPItQlXudcI7ewSGsysMtqI /Kp+RhmamogNS2jELlF7u+F8cLiQN9g= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1655139873; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7OtOWClBlY6896CUL2CJUoXz5Le8Wk1ooKrdR/9iq2M=; b=jM7YYjvLtx6DNW15GGyf0dqs+QE8ufAtcH3oKUItx8uYtMOiFYhJ7OHtlPwP/K7AS862hF 2iooN3q0V9F0fpCw== Received: from vasant-suse.fritz.box (unknown [10.163.24.178]) by relay2.suse.de (Postfix) with ESMTP id AC69E2C141; Mon, 13 Jun 2022 17:04:33 +0000 (UTC) From: Vasant Karasulli To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: bp@alien8.de, jroedel@suse.de, thomas.lendacky@amd.com, x86@kernel.org, seanjc@google.com, Vasant Karasulli Subject: [PATCH v7 4/4] KVM SEV-ES: Add tests to validate VC handling for IO instructions Date: Mon, 13 Jun 2022 19:04:20 +0200 Message-Id: <20220613170420.18521-5-vkarasulli@suse.de> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220613170420.18521-1-vkarasulli@suse.de> References: <20220613170420.18521-1-vkarasulli@suse.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org These tests: 1. install a kretprobe on the #VC handler (sev_es_ghcb_hv_call, to access GHCB before/after the resulting VMGEXIT). 2. trigger an NAE by issuing an IO instruction. 3. check that the kretprobe was hit with the right exit_code available in GHCB. To run these tests, configuration options CONFIG_X86_TESTS and CONFIG_AMD_SEV_ES_TEST_VC have to be enabled. These tests run at the kernel boot time. Result of the test execution can be monitored in the kernel log. Signed-off-by: Vasant Karasulli --- arch/x86/tests/sev-test-vc.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) -- 2.32.0 diff --git a/arch/x86/tests/sev-test-vc.c b/arch/x86/tests/sev-test-vc.c index 629aa0ca1c86..33ca761bf9cb 100644 --- a/arch/x86/tests/sev-test-vc.c +++ b/arch/x86/tests/sev-test-vc.c @@ -8,7 +8,9 @@ #include #include #include +#include #include +#include #include #include @@ -101,11 +103,36 @@ static void sev_es_nae_dr7_rw(struct kunit *test) native_set_debugreg(7, native_get_debugreg(7))); } +static void sev_es_nae_ioio(struct kunit *test) +{ + unsigned long port = 0x80; + char val = 0; + + check_op(test, SVM_EXIT_IOIO, val = inb(port)); + check_op(test, SVM_EXIT_IOIO, outb(val, port)); + check_op(test, SVM_EXIT_IOIO, insb(port, &val, sizeof(val))); + check_op(test, SVM_EXIT_IOIO, outsb(port, &val, sizeof(val))); +} + +static void sev_es_nae_mmio(struct kunit *test) +{ + unsigned long lapic_ver_pa = APIC_DEFAULT_PHYS_BASE + APIC_LVR; + unsigned long __iomem *lapic = ioremap(lapic_ver_pa, 0x4); + unsigned long lapic_version = 0; + + check_op(test, SVM_VMGEXIT_MMIO_READ, lapic_version = *lapic); + check_op(test, SVM_VMGEXIT_MMIO_WRITE, *lapic = lapic_version); + + iounmap(lapic); +} + static struct kunit_case sev_es_vc_testcases[] = { KUNIT_CASE(sev_es_nae_cpuid), KUNIT_CASE(sev_es_nae_wbinvd), KUNIT_CASE(sev_es_nae_msr), KUNIT_CASE(sev_es_nae_dr7_rw), + KUNIT_CASE(sev_es_nae_ioio), + KUNIT_CASE(sev_es_nae_mmio), {} };