From patchwork Fri Mar 18 09:45:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vasant Karasulli X-Patchwork-Id: 12785084 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 070B6C433F5 for ; Fri, 18 Mar 2022 09:45:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234631AbiCRJq7 (ORCPT ); Fri, 18 Mar 2022 05:46:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44984 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234597AbiCRJq4 (ORCPT ); Fri, 18 Mar 2022 05:46:56 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 931F42C962E; Fri, 18 Mar 2022 02:45:37 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 2B8D5210EC; Fri, 18 Mar 2022 09:45:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1647596736; 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=oKQgMRuo7ok835za3TK4V+jSr9t+hDDNO0dKnygYjCM=; b=NBlkzdMXt28feJicUs+Wloouhu+l1pV+llhBG8O4zHEDKtttVPwCDL9X9b46j89MCzmDTJ oLdkOUBf8S8AA8dwxQpEWfkgC8wbMnuPYtZaHInv9O9kd4rg6xnrXwSMsA1PMuBWvKJKxG Oe+XlNCAx/ELTHTxnYRUHyvDhGYSxQE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1647596736; 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=oKQgMRuo7ok835za3TK4V+jSr9t+hDDNO0dKnygYjCM=; b=30dNNUWyvI4VpyLA6qBqUujDWRZJUQMh5bDfencTOZNNQFruCvUGsxzCAAvcOHgGgvUE72 hwExiV0vvSuTSgAg== Received: from vasant-suse.suse.de (unknown [10.163.24.178]) by relay2.suse.de (Postfix) with ESMTP id D4C67A3B8A; Fri, 18 Mar 2022 09:45:35 +0000 (UTC) From: Vasant Karasulli To: linux-kernel@vger.kernel.org, jroedel@suse.de, kvm@vger.kernel.org Cc: bp@alien8.de, x86@kernel.org, thomas.lendacky@amd.com, varad.gautam@suse.com, Vasant Karasulli Subject: [PATCH v6 1/4] x86/tests: Add tests for AMD SEV-ES #VC handling Add Kconfig options for testing AMD SEV related features. Date: Fri, 18 Mar 2022 10:45:29 +0100 Message-Id: <20220318094532.7023-2-vkarasulli@suse.de> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220318094532.7023-1-vkarasulli@suse.de> References: <20220318094532.7023-1-vkarasulli@suse.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Signed-off-by: Vasant Karasulli --- arch/x86/Kbuild | 2 ++ arch/x86/Kconfig.debug | 16 ++++++++++++++++ arch/x86/kernel/Makefile | 7 +++++++ arch/x86/tests/Makefile | 1 + 4 files changed, 26 insertions(+) create mode 100644 arch/x86/tests/Makefile -- 2.32.0 diff --git a/arch/x86/Kbuild b/arch/x86/Kbuild index f384cb1a4f7a..90470c76866a 100644 --- a/arch/x86/Kbuild +++ b/arch/x86/Kbuild @@ -26,5 +26,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 d3a6f74a94bd..e4f61af66816 100644 --- a/arch/x86/Kconfig.debug +++ b/arch/x86/Kconfig.debug @@ -279,3 +279,19 @@ endchoice config FRAME_POINTER depends on !UNWINDER_ORC && !UNWINDER_GUESS bool + +config X86_TESTS + bool "Tests for x86" + 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. +endif # X86_TESTS diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile index 6aef9ee28a39..69472a576909 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -24,6 +24,13 @@ CFLAGS_REMOVE_sev.o = -pg CFLAGS_REMOVE_cc_platform.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 diff --git a/arch/x86/tests/Makefile b/arch/x86/tests/Makefile new file mode 100644 index 000000000000..f66554cd5c45 --- /dev/null +++ b/arch/x86/tests/Makefile @@ -0,0 +1 @@ +# SPDX-License-Identifier: GPL-2.0 From patchwork Fri Mar 18 09:45:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vasant Karasulli X-Patchwork-Id: 12785082 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 ACDFBC433FE for ; Fri, 18 Mar 2022 09:45:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234616AbiCRJq5 (ORCPT ); Fri, 18 Mar 2022 05:46:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234599AbiCRJq4 (ORCPT ); Fri, 18 Mar 2022 05:46:56 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C21782E7120; Fri, 18 Mar 2022 02:45:37 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 7E56521106; Fri, 18 Mar 2022 09:45:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1647596736; 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=j91Po3nZulBUNLEMmTrcd0Gf5q5qXtcw62oM/y9MtGM=; b=RwOUpvVvs9ls3ETEjNWteR5wQwfZaNS6yPViQJXddBVnM2kbTsb9aol43NQ/nekbHz1VDM sNCWXwBL8E6w7hdkYwuYbWZQcqZIfMMFbeetmrjhuNREz/qzujN00pocoC8bMCcX82TCQd 9ogOGNg70ZYoCF6IX+v+nmBkzazxDQw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1647596736; 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=j91Po3nZulBUNLEMmTrcd0Gf5q5qXtcw62oM/y9MtGM=; b=nVfrRN/sFcj/FxvSo62iki3OTGABkLx8HwPBaMqx6va6Lwrv7isqka72kpvR3Tin159dRj uzAa1m9Nr3T8PRBg== Received: from vasant-suse.suse.de (unknown [10.163.24.178]) by relay2.suse.de (Postfix) with ESMTP id 321ECA3B92; Fri, 18 Mar 2022 09:45:36 +0000 (UTC) From: Vasant Karasulli To: linux-kernel@vger.kernel.org, jroedel@suse.de, kvm@vger.kernel.org Cc: bp@alien8.de, x86@kernel.org, thomas.lendacky@amd.com, varad.gautam@suse.com, Vasant Karasulli Subject: [PATCH v6 2/4] x86/tests: Add tests for AMD SEV-ES #VC handling Add KUnit based tests to validate Linux's VC handling for instructions cpuid and wbinvd. 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. Date: Fri, 18 Mar 2022 10:45:30 +0100 Message-Id: <20220318094532.7023-3-vkarasulli@suse.de> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220318094532.7023-1-vkarasulli@suse.de> References: <20220318094532.7023-1-vkarasulli@suse.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Signed-off-by: Vasant Karasulli --- arch/x86/tests/Makefile | 2 + arch/x86/tests/sev-test-vc.c | 114 +++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) 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 index f66554cd5c45..4beca64bd2aa 100644 --- a/arch/x86/tests/Makefile +++ b/arch/x86/tests/Makefile @@ -1 +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..9d415b9708dc --- /dev/null +++ b/arch/x86/tests/sev-test-vc.c @@ -0,0 +1,114 @@ +// 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_") && test->priv) + *((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; + } + + test->priv = kunit_kzalloc(test, sizeof(u64), GFP_KERNEL); + if (!test->priv) { + ret = -ENOMEM; + kunit_info(test, "Could not allocate. Skipping."); + goto out; + } + +out: + return ret; +} + +void sev_es_test_vc_exit(struct kunit *test) +{ + if (test->priv) + kunit_kfree(test, test->priv); + + 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) *)(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 Fri Mar 18 09:45:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vasant Karasulli X-Patchwork-Id: 12785083 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 3F95BC43217 for ; Fri, 18 Mar 2022 09:45:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234622AbiCRJq6 (ORCPT ); Fri, 18 Mar 2022 05:46:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45020 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234600AbiCRJq4 (ORCPT ); Fri, 18 Mar 2022 05:46:56 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C2CD2C3DFE; Fri, 18 Mar 2022 02:45:38 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id DF9C31F390; Fri, 18 Mar 2022 09:45:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1647596736; 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=46+RjIEClxR9XFFyT7pvwEuTqwhr+bO0mNiF6x4dYhs=; b=XYVS/T7iOGGrjQCLLd0iRjY6srB39CwNkYjb8Gu5a6YDoEV2rlkpHIAYqmYTr57eEtgRLZ HvSJp9msNyR5FedXA1ISWuv0yvd+4DUihbAiYeeEjHLOzELE+SXdWntpVLyyIdJPaenr81 x4OeqnopjJt+OAtWXIr8jKHJDHdfvfQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1647596736; 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=46+RjIEClxR9XFFyT7pvwEuTqwhr+bO0mNiF6x4dYhs=; b=dm1dO3EBFos5cJfavv8X3IDH550LLeHa3PtB3orJ9jpF20O14xrQS/xj9wV/bScv4iS0Z9 MF+kFGAKojj1wMDw== Received: from vasant-suse.suse.de (unknown [10.163.24.178]) by relay2.suse.de (Postfix) with ESMTP id 8B942A3B81; Fri, 18 Mar 2022 09:45:36 +0000 (UTC) From: Vasant Karasulli To: linux-kernel@vger.kernel.org, jroedel@suse.de, kvm@vger.kernel.org Cc: bp@alien8.de, x86@kernel.org, thomas.lendacky@amd.com, varad.gautam@suse.com, Vasant Karasulli Subject: [PATCH v6 3/4] x86/tests: Add tests for AMD SEV-ES #VC handling Add KUnit based tests to validate Linux's VC handling for instructions accessing registers such as MSR and DR7. 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. Date: Fri, 18 Mar 2022 10:45:31 +0100 Message-Id: <20220318094532.7023-4-vkarasulli@suse.de> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220318094532.7023-1-vkarasulli@suse.de> References: <20220318094532.7023-1-vkarasulli@suse.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 9d415b9708dc..b27b9f114a12 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 @@ -99,9 +100,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 Fri Mar 18 09:45:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vasant Karasulli X-Patchwork-Id: 12785085 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 AB76AC4332F for ; Fri, 18 Mar 2022 09:45:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234639AbiCRJrB (ORCPT ); Fri, 18 Mar 2022 05:47:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45070 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234607AbiCRJq4 (ORCPT ); Fri, 18 Mar 2022 05:46:56 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89FC92E712A; Fri, 18 Mar 2022 02:45:38 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 333261F391; Fri, 18 Mar 2022 09:45:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1647596737; 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=cR+ONwp2/+ryeWptl7wJzyb3Z8fYKZlMJQsL90dVKGQ=; b=BC7g7veMclXFvQjeQkqXnAaRMSNVAevRFocGWDD4ad4si+yxAbYxXBrVz1Wdpoxf7h9ED0 +oeqqpePl8ZyDq6GZXG260skZcCy4V0ZNVsM3U9Ez7JmW0guANFYU56hw1UR59EgCDfnHK Tdjo+k7V+m75hOOWDyJ5DLmfShW7Udk= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1647596737; 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=cR+ONwp2/+ryeWptl7wJzyb3Z8fYKZlMJQsL90dVKGQ=; b=q1UFfysaym+7ZOPejrJs4fVRKhAaM7DBCbrbfZqu4RlH5g39wRteRHkwyQMVrK/jpdzWMw BaxLorALHw1FZgAA== Received: from vasant-suse.suse.de (unknown [10.163.24.178]) by relay2.suse.de (Postfix) with ESMTP id E6FF7A3B8A; Fri, 18 Mar 2022 09:45:36 +0000 (UTC) From: Vasant Karasulli To: linux-kernel@vger.kernel.org, jroedel@suse.de, kvm@vger.kernel.org Cc: bp@alien8.de, x86@kernel.org, thomas.lendacky@amd.com, varad.gautam@suse.com, Vasant Karasulli Subject: [PATCH v6 4/4] x86/tests: Add tests for AMD SEV-ES #VC handling Add KUnit based tests to validate Linux's VC handling for IO instructions. 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. Date: Fri, 18 Mar 2022 10:45:32 +0100 Message-Id: <20220318094532.7023-5-vkarasulli@suse.de> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220318094532.7023-1-vkarasulli@suse.de> References: <20220318094532.7023-1-vkarasulli@suse.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 b27b9f114a12..d0e742e2bf9f 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 @@ -111,11 +113,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), {} };