Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp410488pxp; Sat, 19 Mar 2022 06:05:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyqIzlxisFv7osB1xR2SEKB715KET/7u9UQmFEApIMA9h8n1RAQza5G8Jb9dlp5+UdIWM+I X-Received: by 2002:a62:bd17:0:b0:4fa:7622:d510 with SMTP id a23-20020a62bd17000000b004fa7622d510mr6517189pff.73.1647695126396; Sat, 19 Mar 2022 06:05:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647695126; cv=none; d=google.com; s=arc-20160816; b=y1igrmbpxi56JySm/kinmfEr7MEfRvIf4eH64kIO074zfy6mNdPGAuAIvYyaS+Ox4z h4gFSRQqzrUr3Cm1AUP4xnVxEDo7cqIk4EfVZaxnSr8Gg2XLkk0uNpfzhQgUiBccSlWw yLfqQG5nErrLk+mQ1jxpWWUKcaGjRvD6ma9dClcWJir1KFKzJe/AGfMtKlKmbUeQJfyE WR0kaG1Dgi/2nBSBp3eS2BezM7GsE7BiphDdXdISTjCV3AhlkzgE+N4J0fVKwWBjZLfn J0++WYSsEbkK96NVBSSaN9y01/C/Ynhv/CkIVaJ4ZplBQZz4A3+2rYfZyghj3CmJIrHR e7JA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-signature; bh=j91Po3nZulBUNLEMmTrcd0Gf5q5qXtcw62oM/y9MtGM=; b=uQObDJfbXbS16TFoDGg+vZl73cQPwq8VutaWhdCJq+xiV7qKwWes1UPk0uOttNxlSH OaxYWty1CUlpcloew1cqLpbb0hOYxZdauDI2d4Ln8Y58m6x8aSKOYSWwnyAUfSbwU0f8 wQhGeyevawtCZEQw7aC8IX9Il5/CgoLqMyh9yBYvY4vlMlkfEhNPFUqSLbPL9/cgQp4/ wfEXXOQk/vNyfBtrEqH0B5rsnIEWgmcx5I7wM4/PG57J6UKBq8zDHJR/zy7OW6TN2VMk ElQp5xl1t2PRVfbogxpW2T3rMZEx5a+IVINz9mxzeHe10T3iUXFGmZAH7eBeEF9SU6Ih nBgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=RwOUpvVv; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b="nVfrRN/s"; 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=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k197-20020a636fce000000b003816043efe7si8193011pgc.476.2022.03.19.06.05.14; Sat, 19 Mar 2022 06:05:26 -0700 (PDT) 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=@suse.de header.s=susede2_rsa header.b=RwOUpvVv; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b="nVfrRN/s"; 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=NONE sp=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234614AbiCRJq7 (ORCPT + 99 others); Fri, 18 Mar 2022 05:46:59 -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 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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 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 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); -- 2.32.0