From patchwork Tue Oct 5 23:44:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 12538215 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0DBB1C433EF for ; Tue, 5 Oct 2021 23:46:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EA2C861215 for ; Tue, 5 Oct 2021 23:46:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237102AbhJEXrw (ORCPT ); Tue, 5 Oct 2021 19:47:52 -0400 Received: from mail-bn8nam11hn2217.outbound.protection.outlook.com ([52.100.171.217]:8641 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237047AbhJEXrq (ORCPT ); Tue, 5 Oct 2021 19:47:46 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CFZ+AGoea3Sxt5AR1JWVs8N20PWbn1sv9pktxTKTwrwr81edozS8KnwWRgW5qkCAmn6GX3fK4As+7ryEDLmRfjjbcRFDU4iqquZkYOr5ZtfDUv8Zzrz12k7fXk81LnMUb/XAUnOgbj+JjmWLk0SDgqKpkk1m21tisVCZhuXcMuiAvqVopLDoyAWbjOzifwxbCTXn0lrE4CgMRZg7Iz6BDtfTzXt9WMzsL+haMQI7cCfnK/r85pArSFvJ4B48Hfde5GgHgj/OnxolaRUdIKHKvfNhKRP8s7Zee4TgGA+MLVglnci/JM84+pphkEMmhboOlT4E8Zl4vAtok0lrnKeHQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=jeB6FnD9KJxn+Wn2IDh5RFP6Ze4RfCvcJDiUDf/SMk0=; b=LfBwSsbAnVN10/qe+NXs9NHUo/DR9EQE8XwW9M8Ancl5Eeyxjnj0vwtUPZIl305nD7HR5aBgEq8nO4qUQ1iTSI/ORLvfkycLbPFMJEAi8GsPJvuM7Up5otAs9sHJxSU1LJL/xfl9Z34rHJnMGAdn9+rHeLNEmvTBOmGDtIxdRKeFGMiJoq5ZMuoEZ+1vB994pBhXvmMad0jlHZZLqAzj7z4GepBHBFkIxrjQHLHfDsMzkhPcmrOfGOFhOkO41ChPiexuzDFXK6VrWvu7i3AhEay04zs8Ol8B3tilruSAEVC/6To7uZsazLLgO2lyNs2xQB3cjfYtqElMXhTzI5BAZg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=jeB6FnD9KJxn+Wn2IDh5RFP6Ze4RfCvcJDiUDf/SMk0=; b=hSAVsrAAKhHKUDC4T7NMrReGIVgm5PuUtK0lwQ3rriSo+WmI2dSL8tCHJha+UX2SiSS3XO8b0yLihTuHUSGyb6B6Z+RzQsYF6/zBsCO6AF1kfbKIJQLj5ib5+Uni0GPY67yAPmeVcW8zxHKKAnhsLikbTPHbU0GJ/FELa9Wq+qE= Authentication-Results: vger.kernel.org; dkim=none (message not signed) header.d=none;vger.kernel.org; dmarc=none action=none header.from=amd.com; Received: from CH2PR12MB4133.namprd12.prod.outlook.com (2603:10b6:610:7a::13) by CH2PR12MB3925.namprd12.prod.outlook.com (2603:10b6:610:21::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4566.22; Tue, 5 Oct 2021 23:45:51 +0000 Received: from CH2PR12MB4133.namprd12.prod.outlook.com ([fe80::59b0:c983:56:ec27]) by CH2PR12MB4133.namprd12.prod.outlook.com ([fe80::59b0:c983:56:ec27%5]) with mapi id 15.20.4587.018; Tue, 5 Oct 2021 23:45:51 +0000 From: Michael Roth To: linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, Nathan Tempelman , Marc Orr , Steve Rutherford , Sean Christopherson , Mingwei Zhang , Brijesh Singh , Tom Lendacky , Varad Gautam , Shuah Khan , Vitaly Kuznetsov , David Woodhouse , Ricardo Koller , Jim Mattson , Wanpeng Li , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H . Peter Anvin" Subject: [RFC 12/16] KVM: selftests: add SEV-ES boot tests Date: Tue, 5 Oct 2021 18:44:55 -0500 Message-Id: <20211005234459.430873-13-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211005234459.430873-1-michael.roth@amd.com> References: <20211005234459.430873-1-michael.roth@amd.com> X-ClientProxiedBy: SA9PR13CA0047.namprd13.prod.outlook.com (2603:10b6:806:22::22) To CH2PR12MB4133.namprd12.prod.outlook.com (2603:10b6:610:7a::13) MIME-Version: 1.0 Received: from localhost (165.204.77.1) by SA9PR13CA0047.namprd13.prod.outlook.com (2603:10b6:806:22::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4587.11 via Frontend Transport; Tue, 5 Oct 2021 23:45:50 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 960770bf-ac7c-450d-2406-08d9885a4388 X-MS-TrafficTypeDiagnostic: CH2PR12MB3925: X-MS-Exchange-Transport-Forked: True X-Microsoft-Ant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NstwadfmBQwcXnfxb0T+QFNj9nR56sB9HIRNMr4fAtdfSsNfCPTyuB3ICIKyj9I7egcve4PMdEyMylZFRRGwviwZAxMrTj1AS4HYqvJuZwVWchN8vEH+o80GoPw+8= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:5;SRV:;IPV:NLI;SFV:SPM;H:CH2PR12MB4133.namprd12.prod.outlook.com;PTR:;CAT:OSPM;SFS:(4636009)(366004)(8936002)(5660300002)(6486002)(6916009)(316002)(8676002)(44832011)(26005)(54906003)(66946007)(38350700002)(66556008)(66476007)(83380400001)(38100700002)(52116002)(956004)(508600001)(36756003)(7416002)(86362001)(2616005)(4326008)(1076003)(6496006)(186003)(2906002)(23200700001);DIR:OUT;SFP:1501; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: aeU0Ri7a/BUUq9bVn4w/xrvMGCgaHAPr/YfHDkJel84DgPMldegn58CzNJzAPguck3fS228Hje/TZsuhgYDT94jChfYjzU0lc0YjnGMVxEJVJy2hHxpyq6Kkkx5Ny7LPbrp4O0+C7OQi8jlrbhbIAdKr8fYxmpu8UO+nV/fDfahwj76yQmyu28v4ffeFDzsXeCDkPdlOYWbovO3ZiGzGwkrAxvIsPocQv9Buu/cu/fPQpFavp7xKLNf5gsAEfXXOxXUOfV0N/X+Gz1qusntkS3hBNK3dmh6ncBOesmezLEfEW7gVDhqOuPIccKBdOLFu0ZHLFkGTVsKldongEEVfoh4nRwDZeMpP8CHJAd8xJq0HPJ6/RD3oxazfmvDN/UoJW/4m8htY20RVKAzBPLTHl+2X+Lk/8Qr/zCGaTjzA+JB4TuuGyIcNy+mEK+I6BqROobVlsr1KaIYOnQoOwloaPnS0WtFU0q94MTh/4bObKIMtINEL+KeUL4gMx8nCCiRmNfcy7unuj1r5qeeV7FMwdwAPuxGtnBUX4VQb+mQnVYdv1uhBGOqLeGaI5U7WgxdVSIi1giJks7WVSm2o9/jcEThDqOx95BLXJOizkgW3GG+4xaHinW9ioFLmTbvnE2nz5rboTcEPv2Q5Z7RXF9lwUy3D3D1LYLnZ1JwJ2JVP5Q3qi24njgl072KZ/g8qEHd0eqnN4eUjIN9e0Ymmf84gmkAjxUNCPAe2C7Bx4YbW1yTUht5ZqqojpnTKUVjQyJPLoOkO4nH5yuyuR8ghOIcUhQ1oJ57EJ+ggg0v+CIS6vEqfRLbN9IKQockPN2IH/CjaCc6x4NOUul1V66ora63NqEvomWtEZWF1gfZdY2HCjzZqgCUn+pYezk2juPASRDvcsnyKrghPBs332s1Zt3TUOFhIQ1b61ZijvI9meTwQYTnl8KnmIVzJyu5GAwnwfY7y5Q4stqZny3Ok1mBtrMpDEx9xivyz3jOYJauSUYZXvZV67/ViolADDQEksXwQWZFra39DuEURpk9IazUJa+bETjdz5cZfmehnrDwva6bD4q3Yyud4Ko/9YNKzeRgH5aTsoyxoRS1beQ40rK4gdXpbkdG/Xh3dltz2JKK5/1XBLWf7vTA1h3EU+2SexpSIkSmxUJGAIfuMFczOnR7CJnZ6yF9o3fIGIRi0I5uV2E24tZ/rP9kQuQc5W2hkQCjHF/DcRQYt73ADqC0lGZ9m4mXKk4RnUX3WHjuE8aqtM0QOZcZ2vkbMokwgrnBC4jS/d+xJd7Jssn8Pb+vdCvqgRF+TkWqE4tzyM/I6cof8mI2yIv/acvWTGKrDj0ktJSQONvmE X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 960770bf-ac7c-450d-2406-08d9885a4388 X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB4133.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Oct 2021 23:45:51.3654 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: uP+YS58aqD3mD5iMPrveRLp0ajj8BzwUGL5oT+Or8lULRElVUWoH2NMfzOBGZdeU4xSY7dLKhWTOSpdtLKMyWw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB3925 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Extend the existing SEV boot tests to also cover SEV-ES guests. Also add some tests for handling #VC exceptions for cpuid instructions using both MSR-based and GHCB-based vmgexits. Signed-off-by: Michael Roth --- .../selftests/kvm/x86_64/sev_all_boot_test.c | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) 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 index 8df7143ac17d..58c57c4c0ec1 100644 --- a/tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c +++ b/tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c @@ -18,6 +18,7 @@ #include "svm_util.h" #include "linux/psp-sev.h" #include "sev.h" +#include "sev_exitlib.h" #define VCPU_ID 2 #define PAGE_SIZE 4096 @@ -31,6 +32,10 @@ #define TOTAL_PAGES (512 + SHARED_PAGES + PRIVATE_PAGES) +/* Globals for use by #VC handler. */ +static void *ghcb0_gva; +static vm_paddr_t ghcb0_gpa; + static void fill_buf(uint8_t *buf, size_t pages, size_t stride, uint8_t val) { int i, j; @@ -171,6 +176,47 @@ guest_sev_code(struct sev_sync_data *sync, uint8_t *shared_buf, uint8_t *private guest_test_done(sync); } +static void vc_handler(struct ex_regs *regs) +{ + sev_es_handle_vc(ghcb0_gva, ghcb0_gpa, regs); +} + +static void __attribute__((__flatten__)) +guest_sev_es_code(struct sev_sync_data *sync, uint8_t *shared_buf, + uint8_t *private_buf, uint64_t ghcb_gpa, void *ghcb_gva) +{ + uint32_t eax, ebx, ecx, edx, token = 1; + uint64_t sev_status; + + guest_test_start(sync); + +again: + /* Check CPUID values via GHCB MSR protocol. */ + eax = 0x8000001f; + ecx = 0; + cpuid(&eax, &ebx, &ecx, &edx); + + /* Check SEV bit. */ + SEV_GUEST_ASSERT(sync, token++, eax & (1 << 1)); + /* Check SEV-ES bit. */ + SEV_GUEST_ASSERT(sync, token++, eax & (1 << 3)); + + if (!ghcb0_gva) { + ghcb0_gva = ghcb_gva; + ghcb0_gpa = ghcb_gpa; + /* Check CPUID bits again using GHCB-based protocol. */ + goto again; + } + + /* Check SEV and SEV-ES enabled bits (bits 0 and 1, respectively). */ + sev_status = rdmsr(MSR_AMD64_SEV); + SEV_GUEST_ASSERT(sync, token++, (sev_status & 0x3) == 3); + + guest_test_common(sync, shared_buf, private_buf); + + guest_test_done(sync); +} + static void setup_test_common(struct sev_vm *sev, void *guest_code, vm_vaddr_t *sync_vaddr, vm_vaddr_t *shared_vaddr, vm_vaddr_t *private_vaddr) @@ -216,7 +262,18 @@ static void test_sev(void *guest_code, uint64_t policy) setup_test_common(sev, guest_code, &sync_vaddr, &shared_vaddr, &private_vaddr); /* Set up guest params. */ - vcpu_args_set(vm, VCPU_ID, 4, sync_vaddr, shared_vaddr, private_vaddr); + if (policy & SEV_POLICY_ES) { + vm_vaddr_t ghcb_vaddr = vm_vaddr_alloc_shared(vm, PAGE_SIZE, 0); + + vcpu_args_set(vm, VCPU_ID, 6, sync_vaddr, shared_vaddr, private_vaddr, + addr_gva2gpa(vm, ghcb_vaddr), ghcb_vaddr); + /* Set up VC handler. */ + vm_init_descriptor_tables(vm); + vm_install_exception_handler(vm, 29, vc_handler); + vcpu_init_descriptor_tables(vm, VCPU_ID); + } else { + vcpu_args_set(vm, VCPU_ID, 4, sync_vaddr, shared_vaddr, private_vaddr); + } sync = addr_gva2hva(vm, sync_vaddr); shared_buf = addr_gva2hva(vm, shared_vaddr); @@ -248,5 +305,9 @@ int main(int argc, char *argv[]) test_sev(guest_sev_code, SEV_POLICY_NO_DBG); test_sev(guest_sev_code, 0); + /* SEV-ES tests */ + test_sev(guest_sev_es_code, SEV_POLICY_ES | SEV_POLICY_NO_DBG); + test_sev(guest_sev_es_code, SEV_POLICY_ES); + return 0; }