From patchwork Wed Mar 24 17:04:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408964 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 46F42C433E3 for ; Wed, 24 Mar 2021 17:05:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0C80B61A13 for ; Wed, 24 Mar 2021 17:05:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237040AbhCXRFU (ORCPT ); Wed, 24 Mar 2021 13:05:20 -0400 Received: from mail-bn8nam11on2078.outbound.protection.outlook.com ([40.107.236.78]:50401 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S236883AbhCXREy (ORCPT ); Wed, 24 Mar 2021 13:04:54 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IPML2SFD4jmHIR2PszDU3Vp4A2vxZ/RK3e4DiqliTfwC5J2g/+zVbf9+0zvV2eX+o8AsWcrZRp/3bFyryxjX7XfMlnGfTZYSO9OChZB1CG/4jB6wJUGUENF4q9fgUEOQCsRcS2ZUbuwhG/vRWKFfvMTrKjaGj62IumKeY6P4M7KPeUY+6liA7f7n8IsPSv/nbmUk1XgxC3GbBMYpEkyldtpKbqTA+eucOkykvPKrte3L97dmf9/HTqTnZWhMmjBYeKRkswI44EENJl1R6I5hB7GchV/WGKTqmSzamLwJ0UHCyuSCuqek+FuPXTullWddpexb5dzO4CEvZPnhej4dqA== 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-SenderADCheck; bh=FLIgnglfwxb6MRfi4trwa2szQhyt+oIcEfzRrEzx9fQ=; b=RVT1fFFFQyA4/NFjk3UNcL0Dadae+sjQNGl7SP4/TCd0Qsrm6X6Dwbe9IpV87rnXEfimeemYAeNVt40ed/Ft5oJhLEhHJ+Ss23HWek9B172H7Uu7A7SkMc3rHbtQIMVOMzxU26jo/+WnJaHmq+jN3m/SN5O5pwykeTZ72P6uuEvE7Tj3/Cr28Mi9YOchSWduWyS0CGGqvddLQ9XARUhx068UKAoWoa+htX6MlpNsutLNfoKYUBXyPXZ9NII3CbxRQh5n5GxIMgmCjvRWUxouvcP69bLw/NmUmiGli+jkRO+z1v1i9Jf6LPKHbcKqEdZs5IybyeKGhoQexfAqb9tgog== 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=FLIgnglfwxb6MRfi4trwa2szQhyt+oIcEfzRrEzx9fQ=; b=JDOVSJNvtvOZQWZXIZmkrxDEoj5IDIWz97x9IRW72oXxGeNdkQKVDjf/MXA7xTKgBmU9GWFKNHKGH52BUyu+eU7i2B8GNi7oNd6HEfF4tevfvPlLlxO5e7bDukFkT4uGzfIis6TvbxqjDOhLUcvEL3cs/sC8iXRIKL0ONqWrrCE= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4557.namprd12.prod.outlook.com (2603:10b6:806:9d::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.24; Wed, 24 Mar 2021 17:04:51 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:04:51 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson Subject: [RFC Part2 PATCH 01/30] x86: Add the host SEV-SNP initialization support Date: Wed, 24 Mar 2021 12:04:07 -0500 Message-Id: <20210324170436.31843-2-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:04:51 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 162c34f5-ead8-4fdd-de9e-08d8eee6f05d X-MS-TrafficTypeDiagnostic: SA0PR12MB4557: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: CwExjS+mMprWmh/f0+fKc3Py6iADibBOC/8LtqT5vLisXiEygkQ2X6UTeH58NnZE3VZj19hn2EC6UBdT7KiUQFNPXvVM98/46OI76l2EJXqfbbKePn12LvAVbJ1ukJQLApMfIF7PNoIm0Elxv4Vy/Uy9OH9A4r/7gBLpsC00uwOBr3Zv9PwdxQ5pNWbvqP7AF3I6DcwDjtRpmsIKDZglYP5+upG+3p0Sq6TtnM8Q//yu3P32zmCYG40pJGPVJdQyCqzAMxm8/ZVmIgQorQ9PGPy6AOOqBopGrzJl8xSjD33Wx8gPVk1z3Qy2jomqqp+EyTvar35Ey0IIkmYUEVytDrdgegGdrDDART5urLs6n8ceScu7sHSWG5iBQfhPJLS7NyWP9mbljJNL10CpS0rRN+SHPlro71KIb0JpXcprp614WAi1St9R7oulgdg0/gBBpbtfh9vIfoe+mWp/3RdHG0qFX6c2zSXOksdohXPNBtYWFs6UbpJL9vZhyeNr8JEJ7ptdxAp3EIktb7maT8L2+bTgAe6/ELfcW3f/UdWi35kbtyxz6YEgsf25T7z1mRy6kKwjUpBaX8Gz/AOy+XvTVSuh4+dRE2s3N9PXmcitUUUqpP6HOGsRyMofrHvxyUTBN4zwU6H+qwOSKVyeuEQpY1uBlhlvEC39esageNfhVZs= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(376002)(396003)(346002)(136003)(39860400002)(86362001)(54906003)(186003)(44832011)(6666004)(4326008)(38100700001)(26005)(316002)(66476007)(8676002)(16526019)(1076003)(2616005)(83380400001)(5660300002)(6486002)(956004)(52116002)(8936002)(478600001)(7416002)(7696005)(66556008)(36756003)(66946007)(2906002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: W8uVOt1DE25xw+pyNgHzMQlxZY7ReZVVXDK2NJhLSY3mQVa/jnLCiHd1nlpsRExgzzy6mg23avkPkwUMteaSCqajJpyBzeC+guThCVe92HA3Wbogs7mE08LmF+4KLaqWZ0yDwemQ8UQw0zD+9JlOU2lxpvTeDm86SSzmuPIFuaHFg9rDk+DQqbsnhquLg2H5AcuCcWwcH7wpuNFL8iot6bfML5u7+ZHxwTzOvyTb/mjs1/40RGvn8askRl+Bl3tJItCLShgHn6JfFXH0NcwgUG576OVNVLvZQ0+nNFhlDr4bEB4uWofP0/HOTsxYDjXoRit3+w4N58ksKAC3v9nna32fCt3SV5wXApb0kEu6BYXVweQAPAOPkK+YMm2YTDIJ9c82OZNw7J5dSU0m5xmQAd/8I6haiP8SOAqA+KtXuOKYMDZmgHglyqdZ7quXr0jIbnMpQ8BYuKBkt3ujXL/8m4Ugmh66V+N8M8Dyzs0Jsj7/yKdDIThD8BojMnIO2VRiDXDrsDs2SvM0mLRXvdUeeP4njQYCFxk+2Gv6AKYq4MXWtZP8JNo1VZ0YD2Ov4pWCt/keGjCyAbGz6tPz+xPB2u/MfDgcep4S5mtPd52GOfhDHzdq7Gnxpy+CbDeigqV/pMJiIw0Nd8pVV7NmVRlgOq2vBrcl+XFf08FPfagvCqjghux6B++oiVP6RkoWXE8oO1JpXgReu89JgPZCmiyZUVAQBgyEL8SOrB80QOuykhfLk4EO6bfyTeYOaGaIL5oartjxzH7+qDDDr40yz5EnDjKV0x+KcPLwPz+z9B+pscAkAYMa0NT+YvrRji9rkcl7/qwIbQE4kkb1IRv3eBwd1p+81tdRK6O3y8guAlKxjgCLBEmNMeYr1w5pFWsNJCK9Ik5GrcfSsPHoeR0BFnc8XkMDftb5q0mDmA+GvQJDm7ARqUYE8xpiodMjBATSBIKIrPUAUf1FGuXf/sqdzCFMPMDtQphAHobFSUELVYUpGRQN6u7w4/OUmYbIvK63mli9bkahVB+j7mr+zbWt2jGyPlYPftMWtTxsoui22vmPXQoa90UYmkDiwqa/Vz7vv4z1y667CcO76C9j5kmI+k0QrcpU2crwycbxj99SewgYRJ2hVjD7TpdBJbc+pyFeMbq9mT8ldsm3cIhv+KZTPWhH8kY7J8vfNFF5N8ZOJzPIkF6IqHmZyuBiqfb04WyMGFTBlm7dLmLXJT+0S77Fe7A3NYe3P9mxNgPTI5x6daHfBokEiZeR8XLUjDE9AmzHLTA3iBSCkb69LRQRi7ZtK/B62CegJ8SAu7TvYjjCjwZYH6gruExd91Yxyp318TZ7Tfke X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 162c34f5-ead8-4fdd-de9e-08d8eee6f05d X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:04:51.7106 (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: v1yTp8jp+rHmSMO1vLLLnzu3woTZbmGKoQnR9tvu7jZWnyfLZ3am0As+35DSLVShq8xY44xyRY5udvgxGRYo5g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4557 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The memory integrity guarantees of SEV-SNP are enforced through a new structure called the Reverse Map Table (RMP). The RMP is a single data structure shared across the system that contains one entry for every 4K page of DRAM that may be used by SEV-SNP VMs. The goal of RMP is to track the owner of each page of memory. Pages of memory can be owned by the hypervisor, owned by a specific VM or owned by the AMD-SP. See APM2 section 15.36.3 for more detail on RMP. The RMP table is used to enforce access control to memory. The table itself is not directly writable by the software. New CPU instructions (RMPUPDATE, PVALIDATE, RMPADJUST) are used to manipulate the RMP entries. Based on the platform configuration, the BIOS reserves the memory used for the RMP table. The start and end address of the RMP table can be queried by reading the RMP_BASE and RMP_END MSRs. If the RMP_BASE and RMP_END are not set then we disable the SEV-SNP feature. The SEV-SNP feature is enabled only after the RMP table is successfully initialized. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/include/asm/msr-index.h | 6 +++ arch/x86/include/asm/sev-snp.h | 10 ++++ arch/x86/mm/mem_encrypt.c | 84 ++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h index b03694e116fe..1142d31eb06c 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -481,6 +481,8 @@ #define MSR_AMD64_SEV_ENABLED BIT_ULL(MSR_AMD64_SEV_ENABLED_BIT) #define MSR_AMD64_SEV_ES_ENABLED BIT_ULL(MSR_AMD64_SEV_ES_ENABLED_BIT) #define MSR_AMD64_SEV_SNP_ENABLED BIT_ULL(MSR_AMD64_SEV_SNP_ENABLED_BIT) +#define MSR_AMD64_RMP_BASE 0xc0010132 +#define MSR_AMD64_RMP_END 0xc0010133 #define MSR_AMD64_VIRT_SPEC_CTRL 0xc001011f @@ -538,6 +540,10 @@ #define MSR_K8_SYSCFG 0xc0010010 #define MSR_K8_SYSCFG_MEM_ENCRYPT_BIT 23 #define MSR_K8_SYSCFG_MEM_ENCRYPT BIT_ULL(MSR_K8_SYSCFG_MEM_ENCRYPT_BIT) +#define MSR_K8_SYSCFG_SNP_EN_BIT 24 +#define MSR_K8_SYSCFG_SNP_EN BIT_ULL(MSR_K8_SYSCFG_SNP_EN_BIT) +#define MSR_K8_SYSCFG_SNP_VMPL_EN_BIT 25 +#define MSR_K8_SYSCFG_SNP_VMPL_EN BIT_ULL(MSR_K8_SYSCFG_SNP_VMPL_EN_BIT) #define MSR_K8_INT_PENDING_MSG 0xc0010055 /* C1E active bits in int pending message */ #define K8_INTP_C1E_ACTIVE_MASK 0x18000000 diff --git a/arch/x86/include/asm/sev-snp.h b/arch/x86/include/asm/sev-snp.h index 59b57a5f6524..f7280d5c6158 100644 --- a/arch/x86/include/asm/sev-snp.h +++ b/arch/x86/include/asm/sev-snp.h @@ -68,6 +68,8 @@ struct __packed snp_page_state_change { #define RMP_X86_PG_LEVEL(level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M) #ifdef CONFIG_AMD_MEM_ENCRYPT +#include + static inline int __pvalidate(unsigned long vaddr, int rmp_psize, int validate, unsigned long *rflags) { @@ -93,6 +95,13 @@ void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr int snp_set_memory_shared(unsigned long vaddr, unsigned int npages); int snp_set_memory_private(unsigned long vaddr, unsigned int npages); +extern struct static_key_false snp_enable_key; +static inline bool snp_key_active(void) +{ + return static_branch_unlikely(&snp_enable_key); +} + + #else /* !CONFIG_AMD_MEM_ENCRYPT */ static inline int __pvalidate(unsigned long vaddr, int psize, int validate, unsigned long *eflags) @@ -114,6 +123,7 @@ early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, unsigned i } static inline int snp_set_memory_shared(unsigned long vaddr, unsigned int npages) { return 0; } static inline int snp_set_memory_private(unsigned long vaddr, unsigned int npages) { return 0; } +static inline bool snp_key_active(void) { return false; } #endif /* CONFIG_AMD_MEM_ENCRYPT */ diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index 35af2f21b8f1..39461b9cb34e 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -30,6 +30,7 @@ #include #include #include +#include #include "mm_internal.h" @@ -44,12 +45,16 @@ u64 sev_check_data __section(".data") = 0; EXPORT_SYMBOL(sme_me_mask); DEFINE_STATIC_KEY_FALSE(sev_enable_key); EXPORT_SYMBOL_GPL(sev_enable_key); +DEFINE_STATIC_KEY_FALSE(snp_enable_key); +EXPORT_SYMBOL_GPL(snp_enable_key); bool sev_enabled __section(".data"); /* Buffer used for early in-place encryption by BSP, no locking needed */ static char sme_early_buffer[PAGE_SIZE] __initdata __aligned(PAGE_SIZE); +static unsigned long rmptable_start, rmptable_end; + /* * When SNP is active, this routine changes the page state from private to shared before * copying the data from the source to destination and restore after the copy. This is required @@ -528,3 +533,82 @@ void __init mem_encrypt_init(void) print_mem_encrypt_feature_info(); } +static __init void snp_enable(void *arg) +{ + u64 val; + + rdmsrl_safe(MSR_K8_SYSCFG, &val); + + val |= MSR_K8_SYSCFG_SNP_EN; + val |= MSR_K8_SYSCFG_SNP_VMPL_EN; + + wrmsrl(MSR_K8_SYSCFG, val); +} + +static __init int rmptable_init(void) +{ + u64 rmp_base, rmp_end; + unsigned long sz; + void *start; + u64 val; + + rdmsrl_safe(MSR_AMD64_RMP_BASE, &rmp_base); + rdmsrl_safe(MSR_AMD64_RMP_END, &rmp_end); + + if (!rmp_base || !rmp_end) { + pr_info("SEV-SNP: Memory for the RMP table has not been reserved by BIOS\n"); + return 1; + } + + sz = rmp_end - rmp_base + 1; + + start = memremap(rmp_base, sz, MEMREMAP_WB); + if (!start) { + pr_err("SEV-SNP: Failed to map RMP table 0x%llx-0x%llx\n", rmp_base, rmp_end); + return 1; + } + + /* + * Check if SEV-SNP is already enabled, this can happen if we are coming from kexec boot. + * Do not initialize the RMP table when SEV-SNP is already. + */ + rdmsrl_safe(MSR_K8_SYSCFG, &val); + if (val & MSR_K8_SYSCFG_SNP_EN) + goto skip_enable; + + /* Initialize the RMP table to zero */ + memset(start, 0, sz); + + /* Flush the caches to ensure that data is written before we enable the SNP */ + wbinvd_on_all_cpus(); + + /* Enable the SNP feature */ + on_each_cpu(snp_enable, NULL, 1); + +skip_enable: + rmptable_start = (unsigned long)start; + rmptable_end = rmptable_start + sz; + + pr_info("SEV-SNP: RMP table physical address 0x%016llx - 0x%016llx\n", rmp_base, rmp_end); + + return 0; +} + +static int __init mem_encrypt_snp_init(void) +{ + if (!boot_cpu_has(X86_FEATURE_SEV_SNP)) + return 1; + + if (rmptable_init()) { + setup_clear_cpu_cap(X86_FEATURE_SEV_SNP); + return 1; + } + + static_branch_enable(&snp_enable_key); + + return 0; +} +/* + * SEV-SNP must be enabled across all CPUs, so make the initialization as a late initcall. + */ +late_initcall(mem_encrypt_snp_init); From patchwork Wed Mar 24 17:04:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408321 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 79CDAC433E2 for ; Wed, 24 Mar 2021 17:05:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2C04561A20 for ; Wed, 24 Mar 2021 17:05:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237086AbhCXRFV (ORCPT ); Wed, 24 Mar 2021 13:05:21 -0400 Received: from mail-bn8nam11on2078.outbound.protection.outlook.com ([40.107.236.78]:50401 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S236969AbhCXREz (ORCPT ); Wed, 24 Mar 2021 13:04:55 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Bthrg5kW6PcX2M/nlcdG4IeDEWaQ1qb7kpdHXIarpaFB5tiwzXsPe81MK6PBUU80M7wXFHEmpigBiqhrm6igXpgKt66khZiaBxmReHg8oeDDt2gGGGQuMFZhuNxXefA7jyQkPIQjDlUdtBX5dY8OMRafphmmw//DSKy+1mR5V5SXTObzaxWSIJhVqjTzOIujB5vDBohVMPOCWs0TPlbktWMzrFgp14o1qf+BJ8Nid8kjeHXSxkPGC3VWDmpZq+IZsZRIFjnpkiW6jLcLPRMYT4jZ7FzxGVnK+Jkx4ZkLeY38UeV7GR4aNxMaX13B8MDKc3jRIC/19Zo9fmFnzkMfsg== 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-SenderADCheck; bh=2cDXjFNVFAaRSlS7j6WayXMMTGKYDSSEn4KD++ibe/I=; b=nOCyc9b9gZVXOSjSFWC1eWcO2PpKRB9A9g/mTY6JfUBIAs8df9eWzQb9KXOcVdugltZGcUeoKPFK/xZnVPHufoBpyTcOBAfbOk/1OAb9uly/M62YGFgatWRyIKJh/Dvi3wl9+eQ5PBeuJlaMqAIEjH80d4LLZAPojk/cwoPsfUe+aeu7OPLtIKhZz3tOzIxPxFXTur3Rz6IDChqQt3vXgwZEc3wWK8mK73f4fJg5vjt0TakcX2S6iTaWtzdBY3WIVQreJQE+Lf/U6MgKS5+it82S4e4A4oIcl3dyRAY5/HWX8e+DoeU9r5XSi0AsK+93u0NHrekMCdvYU4VGukSkxA== 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=2cDXjFNVFAaRSlS7j6WayXMMTGKYDSSEn4KD++ibe/I=; b=pd4P3SzwHC8eTpt7HyPaW41igllN3cB9+pa5GvGklbDO5CUzNmwETJUaKWOjBzt723xfRj6TJw9XbGj6L5RLlF//eUcdO61R7Ymf3bmbz1BNKn6ErK3rh0CEvlViCUlv8mDjoBqvQXvrx9A1uwEfnbQPRSYBJLvi47TEZt/708M= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4557.namprd12.prod.outlook.com (2603:10b6:806:9d::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.24; Wed, 24 Mar 2021 17:04:52 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:04:52 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson Subject: [RFC Part2 PATCH 02/30] x86/sev-snp: add RMP entry lookup helpers Date: Wed, 24 Mar 2021 12:04:08 -0500 Message-Id: <20210324170436.31843-3-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:04:51 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 95e6e956-b01b-45a3-a6f8-08d8eee6f0cd X-MS-TrafficTypeDiagnostic: SA0PR12MB4557: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7691; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: G8ysawRqgCbwERxnaZYWFqsaVSLHpAePAdHpFFyrysTUkWfHj8qUBQ6Uqmoq4kR2NDqOjIthGOKdEFahQF3ib4+lJhLR98Pwho96nF0GSQLfqXvUaeBTYeWP8yQBBjtcKDSwh9O58yjr7miB1pouA0tC5aXAKYiD4Am6a4Z2GiBzcf3fPsTAVytADN3VTSzJd8mbC5NLuTx8cR6YPfM1TMEf6pTwdKWZoQfiARxgeg3z02N5q60DyEuNaJzqfn4OdT0+W8IAR6QpLEn4L0L1XzL7t/YsT5w7IRmSvxuVjRZIqd2THDNTurV4uE9i8MdeR6uibyxST6e7SeKc6iOLYPBJ0g/bMX7yqKbHkrK51so5J313G05qWpArbONGlzHrNqCy8KKe6HhNHz7IBQBmiLktkQZNxI8DWdycHOrM2Q9eWmLDOAhtAfNYcYj1WwxlxBa4CJ9hysviZF/xbiOrJf1dZTJG/wlKb4tOI3J/S7RM1TPsEysa9b2tgcWsvoLXtediiIGKngYHbpsr3qGCk5eZRKOSVSf4ZaPGoODgtf/4RD6vsyEBxTxsGoYOBW+0VsEB/Sb4stKBIpNGXuhkmLC83wWnLVbqH8zpef+lC7Peu+POqwFQD4FOK0y0QEJYcyXtF23+zIE6scCV/jU+LKW8nmmn5Id4KoJprXuabUM= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(376002)(396003)(346002)(136003)(39860400002)(86362001)(54906003)(186003)(44832011)(6666004)(4326008)(38100700001)(26005)(316002)(66476007)(8676002)(16526019)(1076003)(2616005)(83380400001)(5660300002)(6486002)(956004)(52116002)(8936002)(478600001)(7416002)(7696005)(66556008)(36756003)(66946007)(2906002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: cxKarHWY07auMEaK0g2kkhLY9xSePl6b9vveS8Ngr2alygTEqLdBNDKJtTVIrT4j40XoH/Ggae9t6Tfki+46kH0hvAlAFuP1Bn9mTQ0THjMExplnViB1VOaXd/awwPYzHz9OtCgV4z9A5qTOC4vyGB7EMJ/2pufzGFoh88Oq++XqF6EYtUpP/8vOIa73m2cl86VArORNOjEq4HuWOirD8WN8niE04HlR11nVgUZXsURJnMfHvVHr+mYgNPmxafiYOSr0Yd5mMaBR9YODXM/uhciyfyoRd7qwaEX6Xvchzv0ulSBGaVghPzlZisFYyhfOjuVqNJMkHP1FkRTQkfDE7FJEGDCWku7UhMwQERoVwyyPNPM/J3EBKtz7le2hf7rgPMVLCUMYEHhnJNHVTIuWy0Bh3qcQdrKZ2EJC7YrePttBQ1VLOeUPBI1VmPTmmSK58ab7z7E/4nHtjk5Oui2a408aXLYyynSIF6yWaSgaGpr88J14QzckXQhj0LIBcJYdcvDkTMHhovKZe9EJ7Byk9aTGf/gnsKCrDHiq1fJR0/9hjVfkV2Kd9fJ17h7DxPWPwfFW08Y2qq8W5aPqCgGsM50p4ds09tN10Njifrkx4ts9Jq8dCE7kdNTQN6Q8zDcQhM2tHYLk+wEqrJaHTy0F4l6LN5efSRXI1Jh0qvDpe/roO7BxOx57EseFodcObpZQjnyTibnhHL/KJJgN0MqT4dAVYGFrOg8h2ASEpnwdlD16wvdhDQojRxzoOVOYtB8d3Q2bwXqB3o4WuLlbLk3mgr740sJ+x2kJYEI3Q+kqVJ99VqI7EnjsMG4DXdJv+3zJcyZKXXAaJLPfT9w99yRTHYUcsmZWPWRa/o1itJ0z+TLvrQKpdQmr/PEwZ1d355RuPOhr8wpDaD+n138/WXNV/LUULVCGYhxIvJEv9BzdGgCb3HHlSFOvbsKXIPq7lesCC7oAtiMEuyk0iilfNeq3u5x9G/2MCTHwC8PAsHo7Ic4bi4rBmD/VP4DVaFymwbQPC1EN6NGlj7yYqQMtSXN5pJTX3DWdxExQcnrLuqprkdIDSFolVYYElkSQ+bfwI11ibjSGzt5JPCkOL8NBFhx2tk8NyEg/A0a5HG5cgr9v2Y7qBg60Uqo4ybWpAvkDcItq05ySBKJUIPvV1hFK4xero4+YhMRzLuM6o7WjqvQDdUn6MGpeKOH3Sj0Fc/PwGke/aaXwZk8WmMyvobfG/a6YMcZjszCoY4weCchx1RgZvAgyLssbi4RUbrqs6PLAmsjvr5kLU5LBE0rtzPOhwnPSjOmofTL/pM9gakRZm0tnAEFAwnVtpxITHCwtaFFSCqYu X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 95e6e956-b01b-45a3-a6f8-08d8eee6f0cd X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:04:52.4312 (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: 81VWIe+FuX0crAm6RMssgi7CSZvKJXJJ6Y2pkYHL1eylsy8YLo26bmNM7ONk3HMrr+AWZkn/4jT8h+waTAq70A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4557 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The lookup_page_in_rmptable() can be used by the host to read the RMP entry for a given page. The RMP entry format is documented in PPR section 2.1.5.2. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/include/asm/sev-snp.h | 31 +++++++++++++++++++++++++++++++ arch/x86/mm/mem_encrypt.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/arch/x86/include/asm/sev-snp.h b/arch/x86/include/asm/sev-snp.h index f7280d5c6158..2aa14b38c5ed 100644 --- a/arch/x86/include/asm/sev-snp.h +++ b/arch/x86/include/asm/sev-snp.h @@ -67,6 +67,35 @@ struct __packed snp_page_state_change { #define X86_RMP_PG_LEVEL(level) (((level) == PG_LEVEL_4K) ? RMP_PG_SIZE_4K : RMP_PG_SIZE_2M) #define RMP_X86_PG_LEVEL(level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M) +/* RMP table entry format (PPR section 2.1.5.2) */ +struct __packed rmpentry { + union { + struct { + uint64_t assigned:1; + uint64_t pagesize:1; + uint64_t immutable:1; + uint64_t rsvd1:9; + uint64_t gpa:39; + uint64_t asid:10; + uint64_t vmsa:1; + uint64_t validated:1; + uint64_t rsvd2:1; + } info; + uint64_t low; + }; + uint64_t high; +}; + +typedef struct rmpentry rmpentry_t; + +#define rmpentry_assigned(x) ((x)->info.assigned) +#define rmpentry_pagesize(x) (RMP_X86_PG_LEVEL((x)->info.pagesize)) +#define rmpentry_vmsa(x) ((x)->info.vmsa) +#define rmpentry_asid(x) ((x)->info.asid) +#define rmpentry_validated(x) ((x)->info.validated) +#define rmpentry_gpa(x) ((unsigned long)(x)->info.gpa) +#define rmpentry_immutable(x) ((x)->info.immutable) + #ifdef CONFIG_AMD_MEM_ENCRYPT #include @@ -94,6 +123,7 @@ void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr unsigned int npages); int snp_set_memory_shared(unsigned long vaddr, unsigned int npages); int snp_set_memory_private(unsigned long vaddr, unsigned int npages); +rmpentry_t *lookup_page_in_rmptable(struct page *page, int *level); extern struct static_key_false snp_enable_key; static inline bool snp_key_active(void) @@ -124,6 +154,7 @@ early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, unsigned i static inline int snp_set_memory_shared(unsigned long vaddr, unsigned int npages) { return 0; } static inline int snp_set_memory_private(unsigned long vaddr, unsigned int npages) { return 0; } static inline bool snp_key_active(void) { return false; } +static inline rpmentry_t *lookup_page_in_rmptable(struct page *page, int *level) { return NULL; } #endif /* CONFIG_AMD_MEM_ENCRYPT */ diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index 39461b9cb34e..06394b6d56b2 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -34,6 +34,8 @@ #include "mm_internal.h" +#define rmptable_page_offset(x) (0x4000 + (((unsigned long) x) >> 8)) + /* * Since SME related variables are set early in the boot process they must * reside in the .data section so as not to be zeroed out when the .bss @@ -612,3 +614,33 @@ static int __init mem_encrypt_snp_init(void) * SEV-SNP must be enabled across all CPUs, so make the initialization as a late initcall. */ late_initcall(mem_encrypt_snp_init); + +rmpentry_t *lookup_page_in_rmptable(struct page *page, int *level) +{ + unsigned long phys = page_to_pfn(page) << PAGE_SHIFT; + rmpentry_t *entry, *large_entry; + unsigned long vaddr; + + if (!static_branch_unlikely(&snp_enable_key)) + return NULL; + + vaddr = rmptable_start + rmptable_page_offset(phys); + if (WARN_ON(vaddr > rmptable_end)) + return NULL; + + entry = (rmpentry_t *)vaddr; + + /* + * Check if this page is covered by the large RMP entry. This is needed to get + * the page level used in the RMP entry. + * + * e.g. if the page is covered by the large RMP entry then page size is set in the + * base RMP entry. + */ + vaddr = rmptable_start + rmptable_page_offset(phys & PMD_MASK); + large_entry = (rmpentry_t *)vaddr; + *level = rmpentry_pagesize(large_entry); + + return entry; +} +EXPORT_SYMBOL_GPL(lookup_page_in_rmptable); From patchwork Wed Mar 24 17:04:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408965 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 72A95C433E6 for ; Wed, 24 Mar 2021 17:05:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2541761A28 for ; Wed, 24 Mar 2021 17:05:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237092AbhCXRFW (ORCPT ); Wed, 24 Mar 2021 13:05:22 -0400 Received: from mail-bn8nam11on2078.outbound.protection.outlook.com ([40.107.236.78]:50401 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S236981AbhCXRE4 (ORCPT ); Wed, 24 Mar 2021 13:04:56 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UFxqFIWasKsJCttmo8Q0mLFvK6kvnft4ZoSYTQRY97nYR6PUin4QWlwS7OyZEHALeZq+JtL/szSiKcRP5Ymyaahi1w/i/8FYDDxUOe4dKqx+fCYYJOxOG0WlzkDQ3WTvGWxl/SHccqhZUw7ImjW4CeCyeyjIMjLZjiLpqWEC+pVd1d8K0RoM5nh4baEPAh/ZP5n25THLCvV/Tdvq60ENOochtKIhGINjbnLCjYTorg8gDPELbg5ohFPm1L2PtTOV4nE0EGctY4lIg02TgNgvC7TT1/2wFw+x2iRXWbNpL5UUJxw+Y32u0VtvIhLHL9F+LGPZMGOYhO6Ch85n/lapdA== 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-SenderADCheck; bh=/P8ewgCl43XidADqkdBmkjFX6Sfef4igsqANzNI8R+M=; b=V+WNZf+r5507cj0KOsNpgw8VAY0nUGBbtdoBcj+IvBHXm4yAjfUyz75Ve0Fhjp9Kmv6w6Y+d4rEQGhuQxLHrc4gbuDw+sXwZ58L+3w0tSHET3XQNK7YlJDNM4c06RXveQ05GEPu/FLA8PFcaxStYp4mZ/C8C8jx9s2qUF/fgOz6ce0P8ROIeaA2XkcMJuztmx8YFvSn05xIYpQJHYAYvcW9vS1KYo/nv1J83oTLabB/J9iVwEMreSbm0UQBYn0fk5TNLZMclgRfP7DTboeJZ/ieKsGrN1YQAtMOzyMUMvN9qoNRgyfEeI7pz+VwkPmnqe5IZbtvI6q0qFBgwGloRFw== 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=/P8ewgCl43XidADqkdBmkjFX6Sfef4igsqANzNI8R+M=; b=yrR67mFcZZUvmla5flc3pImNu/F491NCm4QDgdy2mbzuseRFemWhxYL7UGlLW4fUdFJmktvSGiTJkFXjocdKo+X62zNMd//K4uFpioAIrWaNUPfU5dWw0X7AhCglX/VqGrGk/CxzAlaObH3nqWDgph4f1TERTSrA8dQJT3C6MhM= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4557.namprd12.prod.outlook.com (2603:10b6:806:9d::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.24; Wed, 24 Mar 2021 17:04:53 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:04:53 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson Subject: [RFC Part2 PATCH 03/30] x86: add helper functions for RMPUPDATE and PSMASH instruction Date: Wed, 24 Mar 2021 12:04:09 -0500 Message-Id: <20210324170436.31843-4-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:04:52 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 2add3e07-d942-4f02-1849-08d8eee6f141 X-MS-TrafficTypeDiagnostic: SA0PR12MB4557: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 40RMwpVJISwjVp+jumvTaDBXe3dwkw3H7fZl1b9qMk09vm9L2fzRydhYPFAjdjFolReOke8MUj0eIvXAX639/Inl1SVJRhOZArWvLQ0dokutwIlxMRig8cau6yvRQmfonC8RRtJXfe51RmAOOJi0FrY9A9ADaSU5CsDMsPjigqhTupOuS0u79f8iEnvLwqAgltOzWVex9y8SooAiUQq+OYdOiEm8/uluzVpyVoPmsRGd29ck15aXF30/kcygT0L51zBTEyM+7iuuRh63Poa9fesaZPLKYUO0xdMRYWicqKW+VFmN9bg/VL7j41s6/DMYQfaBuy4LSLZUfe/qcKsC54JYPELkNk7g8gh8JXL/Eo6MODA7k7YXbXghqXNyKIAdVB7DwqqpyGrqY0R69l6oTN4zFsOwmlmavN2ZWWB2ThQ/uJS8OovdbfIxHXZBfOyd/NRiA501Ed0ElQdI8euZfsI1/NZiM124AkQsJcW62fZdEIdb4JNd3pqyCKJ/BSFPBl3t7MoCw6Sbbv8fgMf79l8o82SzyzG+gq+5Koc3qnugbcaP10atGdW6JT8QB8sd/vPh9pyoiEPr9A2LGOa5CZ6TxBOV8FmsEnB94hhjw/yg8Q2ZFT4D6dH0g1MoMTaazBo1HEknVDXnENPJmMIEcfPhk86TI4Io4AlQemxhccQ= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(376002)(396003)(346002)(136003)(39860400002)(86362001)(54906003)(186003)(44832011)(6666004)(4326008)(38100700001)(26005)(316002)(66476007)(8676002)(16526019)(1076003)(2616005)(83380400001)(5660300002)(6486002)(956004)(52116002)(8936002)(478600001)(7416002)(7696005)(66556008)(36756003)(66946007)(2906002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 4f6t/NMxyEUcGL8Areu0Fzy0j7UuJzWShBXjYhA2PYL1bGneKh7tGL56GbHbwR0xciB6Rg1t3OdN89uRcKwcdljS+x+jMIevVGxlnu7RqSjoamyMBR6rvniZuAlpxER4A/3RRN+D6t/52ybI82BcSDjTyC/hOYemAH9mfQ/EVfD5yIACibf2gE7kl55NorKBYwfGyPVDyiZBD7Phapm5dvOFJ3ejs1gFYW6BL3VRPNh2eM48/R3EQ7y8d9eRpHeDkmHmoOpI2leytP1ioQosw9MwhkhYnIVTHusVnmSplaH+eq8ftrRIiQxHHNCCobAqUB7FifZoAcbU4+8xj0h9MwPECVA6VXpnB8JneEnof0PWSgivLu2xhTtHkv2igFc4MCra5kGBLpnfCHFtucR10HeGDpYanMu0OP8zMTsBacO9AVXw7LocvG+AEhwcVOvNqMt+xpbjGYL1rmKWenYkQ6mCtxBmWgK6vy17dP5QwPKHhNrdzdHZ16HJr8EEkHrkUJb1tvmoxnCbNzIbqqg2WzXRKAv60hgcukaHXQVzDaN519vCI/eYIwk5AXSUnuaKFRbVllzFNrriFyoZzzT1g1bc+tVEEWsenBbrnZjGBMpz79YZcO82jsaP4bzCYBCQFuZaA4+wQ31pBDsETtU6APnDxHUZScpWxFyyCZOpKcfsPSpJ4Cb4131219dvmxfx67cTvL+ZvI41FK5ObfARSHWd5EULeenISPSCcUSNtB0NzlalMGaXVcwxzwPdJ1ctMipY2b7irjIXeptY9Za3TgX6JjzgZEddiaji17fS9bYL3EQq8uNZHTn3zfq5rBQoh/6wNcWhsYiZDtTSVZKONDDc53dOaP9k9w+Pi+7elXGmtbppp/w5xA4aaN1mV2GlXM21ggYCAQITiUwmELUQ3r5C0SGt1RSIz5y92Z4ej0Vpx0n06EY957jGXXbnVxh12iP+LID/slAEyEn8BJt/iAIxgcFVpeg++M3W/Dwte/6ED7/L9U8BZ57R9wMT87zEwen1FYOensovyBWz9GsP/RKBAuO/rZogZuRCK+bCsw82TopGC0iRtYKRB7wtc7td2/M9/7gsQVFHd+XuqOm+qsJQJYgggNvXpBS5NKb4cAt/HMA9gV6IaBiw7AeLSC5VWT3k+JF6wSEBELDMWGiW1Y85KDOIRtOsd1xCTJnDSPrXZRUExTk5g3p3timpzQ2md9sjp/z8I2cfSUFD/bo0A3Octx/a92VNkZ64TaYyrG6T6BPvhrCYq6cN261BMH4bUp+LLz2QPBVYu7PWgYhPg3F3IJodWHXpxP+SYDcdv2KgwiuylFJSUXZuQC7Lw10R X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2add3e07-d942-4f02-1849-08d8eee6f141 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:04:53.1788 (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: geYFke4iVmGLxZVzPKOeqB8z7VAxFjgUrni37C+7V5mm0ZzrlyWgdWI4fihUiM6ndeD2Wlb0s3N3A7ZDJ3F9FQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4557 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The RMPUPDATE instruction writes a new RMP entry in the RMP Table. The hypervisor will use the instruction to add pages to the RMP table. See APM3 for details on the instruction operations. The PSMASH instruction expands a 2MB RMP entry into a corresponding set of contiguous 4KB-Page RMP entries. The hypervisor will use this instruction to adjust the RMP entry without invalidating the previous RMP entry. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/include/asm/sev-snp.h | 27 ++++++++++++++++++++++ arch/x86/mm/mem_encrypt.c | 41 ++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/arch/x86/include/asm/sev-snp.h b/arch/x86/include/asm/sev-snp.h index 2aa14b38c5ed..199d88a38c76 100644 --- a/arch/x86/include/asm/sev-snp.h +++ b/arch/x86/include/asm/sev-snp.h @@ -96,6 +96,29 @@ typedef struct rmpentry rmpentry_t; #define rmpentry_gpa(x) ((unsigned long)(x)->info.gpa) #define rmpentry_immutable(x) ((x)->info.immutable) + +/* Return code of RMPUPDATE */ +#define RMPUPDATE_SUCCESS 0 +#define RMPUPDATE_FAIL_INPUT 1 +#define RMPUPDATE_FAIL_PERMISSION 2 +#define RMPUPDATE_FAIL_INUSE 3 +#define RMPUPDATE_FAIL_OVERLAP 4 + +struct rmpupdate { + u64 gpa; + u8 assigned; + u8 pagesize; + u8 immutable; + u8 rsvd; + u32 asid; +} __packed; + +/* Return code of PSMASH */ +#define PSMASH_FAIL_INPUT 1 +#define PSMASH_FAIL_PERMISSION 2 +#define PSMASH_FAIL_INUSE 3 +#define PSMASH_FAIL_BADADDR 4 + #ifdef CONFIG_AMD_MEM_ENCRYPT #include @@ -124,6 +147,8 @@ void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr int snp_set_memory_shared(unsigned long vaddr, unsigned int npages); int snp_set_memory_private(unsigned long vaddr, unsigned int npages); rmpentry_t *lookup_page_in_rmptable(struct page *page, int *level); +int rmptable_psmash(struct page *page); +int rmptable_rmpupdate(struct page *page, struct rmpupdate *e); extern struct static_key_false snp_enable_key; static inline bool snp_key_active(void) @@ -155,6 +180,8 @@ static inline int snp_set_memory_shared(unsigned long vaddr, unsigned int npages static inline int snp_set_memory_private(unsigned long vaddr, unsigned int npages) { return 0; } static inline bool snp_key_active(void) { return false; } static inline rpmentry_t *lookup_page_in_rmptable(struct page *page, int *level) { return NULL; } +static inline int rmptable_psmash(struct page *page) { return -ENXIO; } +static inline int rmptable_rmpupdate(struct page *page, struct rmpupdate *e) { return -ENXIO; } #endif /* CONFIG_AMD_MEM_ENCRYPT */ diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index 06394b6d56b2..7a0138cb3e17 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -644,3 +644,44 @@ rmpentry_t *lookup_page_in_rmptable(struct page *page, int *level) return entry; } EXPORT_SYMBOL_GPL(lookup_page_in_rmptable); + +int rmptable_psmash(struct page *page) +{ + unsigned long spa = page_to_pfn(page) << PAGE_SHIFT; + int ret; + + if (!static_branch_unlikely(&snp_enable_key)) + return -ENXIO; + + /* Retry if another processor is modifying the RMP entry. */ + do { + asm volatile(".byte 0xF3, 0x0F, 0x01, 0xFF" + : "=a"(ret) + : "a"(spa) + : "memory", "cc"); + } while (ret == PSMASH_FAIL_INUSE); + + return ret; +} +EXPORT_SYMBOL_GPL(rmptable_psmash); + +int rmptable_rmpupdate(struct page *page, struct rmpupdate *val) +{ + unsigned long spa = page_to_pfn(page) << PAGE_SHIFT; + bool flush = true; + int ret; + + if (!static_branch_unlikely(&snp_enable_key)) + return -ENXIO; + + /* Retry if another processor is modifying the RMP entry. */ + do { + asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFE" + : "=a"(ret) + : "a"(spa), "c"((unsigned long)val), "d"(flush) + : "memory", "cc"); + } while (ret == PSMASH_FAIL_INUSE); + + return ret; +} +EXPORT_SYMBOL_GPL(rmptable_rmpupdate); From patchwork Wed Mar 24 17:04:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408957 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 95956C433E9 for ; Wed, 24 Mar 2021 17:05:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6A75661A2B for ; Wed, 24 Mar 2021 17:05:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237097AbhCXRFX (ORCPT ); Wed, 24 Mar 2021 13:05:23 -0400 Received: from mail-bn8nam11on2078.outbound.protection.outlook.com ([40.107.236.78]:50401 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S236991AbhCXRE4 (ORCPT ); Wed, 24 Mar 2021 13:04:56 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hnhL2GWWb++3z+SgRuNKWAwzNndz3bqYahuGsnARLsCmvtO7gf9ILA1rEVv2uhMALPPhCUI7E7hSciglFfSm61rArI44MfauYhmKRX6VseuaC/xrsIBYQQlK0cFb9tq7yvZE3E19qVxFN7Uq80dDZftC69TWNEsCzv92Fur8odGCS0s/S57/uULReCwlfZAmRmuHt1DPuUcUSDTxBtM2R/jalPzsed3F9h7c+0fY2opzfS4UYvoBmRidHKmFwp/Zs2pxVwzcO9RFWq+AX+4O0FEreG7cf+CIr+OZXIyecxO7fS8DorhQd0mTJ3hhcVpH4aJbqdM7YrXWNQQJ/0o+Ag== 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-SenderADCheck; bh=A8bf57MDVmdHiaGAaqUquc6Hxo51aEZoGT7xxVA24Rc=; b=Fto2tyuIyMI65690jClJA3/OlWqw/+4QxXfE9Jyi6g8q3n9yIGLflCbktL7c/IE5cIFqArL46W334eewmbbvuBXak1Pv7z/rF6Knz+DDNKs33Yt8+Yr6aFV1bnXXQ/6oYFaZz+faGFI2Qp3W//mVyMWFNTbW0ZbcMMDaXC8kisGkJYGj6hiVdBHuwDBW6AbnUKamWc2eGdCw24XiXVJsX8MpOLgCZTL/R9TPHiKW2nfKmDUoxWDyXNS5sq0xh4g77W8LJLnWhi5ItvcRkBk5OnTAWFL73snU8eU7c6JrzNcjWiui0ck2xKZXIv6iNR6/+NldEAOTGRhFHstPjJtF9w== 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=A8bf57MDVmdHiaGAaqUquc6Hxo51aEZoGT7xxVA24Rc=; b=YWOjpPwEGhs9VXvJifblobJu24Ex033HYoexLgDq6efYhuRR2KDW1+9QNYtRYZ891iW+Bl5cqYeRhGPY38zpb/e0Jj7Z3h0KrKL624FvBRyupvrrXu71jqBHfDt0d+Abrkl/x0nPsLaY4gGqdj218zUPHo/5iHiaO3oxCy8BTN8= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4557.namprd12.prod.outlook.com (2603:10b6:806:9d::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.24; Wed, 24 Mar 2021 17:04:54 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:04:54 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson Subject: [RFC Part2 PATCH 04/30] x86/mm: split the physmap when adding the page in RMP table Date: Wed, 24 Mar 2021 12:04:10 -0500 Message-Id: <20210324170436.31843-5-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:04:53 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 73a31ea9-7f51-4e92-814f-08d8eee6f1af X-MS-TrafficTypeDiagnostic: SA0PR12MB4557: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:10000; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: l65OYkmUCtuthcaIltPrsDTDzChNSDdkNdpR6OB+msGuJarNMeKgy3JYXS0kjwJ88thtkO/Z1OAffvq90/nVPyVcCuN7RKDuIIZYwOFiuZL10aqQJTeO7liQVH/l0fEjhUU8jOUphD84sUPMyb205TqIAC0BJv5GoekQy/lnWlPD5ScKzUNiy8cfIeDFyZLLWPsx8psFG8hZ1eXX+xPYpAe4xbViUe16HAnzmYQ12xkrplBx6Wh7zf+bBMjTbHsSZ85sIOek8Ne35uv5QgFFjDZ6je6e8PedCI4K7gL9eTAgCAAWV/TaxFfpZJ/pw+HIo+lneWszRnvb/+VGep23J+b9MFD2Ihv+08PEEaHSE84y+vOVFSGHyMTzNsV1dku30h7jsyTRB3F79gLCoIKYN/oE/lXRNI80ocfOaAzj8H2qvVTDyJOzkp8zCjqIAduCOT1YOSV6JtdaOoy0PSYXaxUHMoUe3zJsfilYXj4JPnTWS27LuF/+T5X82atwur5xil+TvIWhtfw65ywr+rY56tc+6eytjmjibHGuVgMOItNT66dwdPxstOiywm+C8QgA4BlREfJ1spiZP6NHcjC3b/xked1JXgK67+pvb4BWWZ4dh68uRm6g9OIbJEs9V9J0MQqFiJymF4r7m6GDjgegYo51V0T/KgAZQ7wwqhE5rOg= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(376002)(396003)(346002)(136003)(39860400002)(86362001)(54906003)(186003)(44832011)(6666004)(4326008)(38100700001)(26005)(316002)(66476007)(8676002)(16526019)(1076003)(2616005)(83380400001)(5660300002)(6486002)(956004)(52116002)(8936002)(478600001)(7416002)(7696005)(66556008)(36756003)(66946007)(2906002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: JBTAY+acsWekW9eXsReuzp+I0EH7YSzTRzI8lWToBZyBxJ7eVUt+RsVAR4ASkiut9rUG//9FNpcu1KdwutbwzJ23ayycKFotlYA2C9iHvsU8KbMCZoAypiXq9HMr8C+owSTkHEXNOQB1lo/zu28WVcoSBigkl2pANNPmABHNJ2ENyF86it3liwFLq0ibdSHB6VzADiH5dRxt1sD+6iX56sgb6pHn12eH4qtIPNnbLvoPPDseIUpAtAth9qJfyiWIuYvt+SsuTx2Hpb0v+3zKK5LnQvlkk2IWw+cg2bsW4XAFsqoe06OFdwZE8Qg1ncMd9TNSKoLUGTgR/FEZeflctyl3/81dCKtnmzo8imhDOTOga2zXkRmxix0LylpFnP1J4NkCs9tSeoduggbGGx5cVIZLPGO1UjatB55/g3O/VEsWaKm93Z2TOGjbs+XE2TAJtnajhR05W4FclD/7qmvZNhsTvies6We8Vt+h+h3KYJcch2W7qjThoKZi72RAHx9jqoUjBWGR1t+J/j8ygHZH1ZlxTjM/eGo07fViFQA+RiEhN5vOVywECFBOQNBGenlPUSM5C2ZpfdZexW4Dk2k6ZZdXfN7WI3gZKvLSM7V33yR9AwRTkps6PBfZrE9QNO/l9ah5MAcu5QxapUkt5Pwt0f4CjS9AoAcGAMdJAFMM3HJ0kj2Zho6PMnMzDOPNz3ltyLqxL2+Jub8JlfUe+oInVQc5o9A/8L1F4bEIkorHbJQSWO4SYyGpbN61SWhrwwZ2SuYVqmAea2a985j5kyeK6bCBC8RXcZxQ2vNJHR2/aPUuqrcr4nRQsdai8n+bV/CI/GQA9EbjivNyAxqxTYyL8hDVHNSmrgVy8ltF3M600Zp6o/jAL8vaeHjpX3tuUCsdpQUWcOtpP8d5+6wA2E7xuIVBKu+PM54UJH+JxdBk578Pg6wEskBd32pQGCec+EAvx2mliW1WD+3DqBbptvtvZgb8h91flk6h3ked0V+l/lYr8hTxU6rj0kAcdpw+sq/6bfmy5NU6bBisrzYhospwul8TEBLgo6GLJazWgMOvU6nP7RKFaoZj5rzfxKaLvCC3kwwrUAm9o5uyAuye//R8/+upA7CFXwp49yvf5A4JMfPAUzAg/TloFCfhWkFPal5z9svvQuK7fGZiGc8UtdQ2a/iQ1uAiis1KidVxw/5mtoHnQwZvXUfTouR35YK1hGUMqDsjMP+tFltGdDruWRNySqnnbCi50N5+4M9UthwoP8pRzFc/1CsqdjSu8yJ3smh4lCb8jLBjhwwFSq/poZxIPY1HSrnRTEh3sKfgCl9MYWehfG03y99XD722wz5TwqrG X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 73a31ea9-7f51-4e92-814f-08d8eee6f1af X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:04:53.9563 (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: nw3XjxSR7e9xx7NAj8ohZyeektuVxWN/ssYx8W90rWwJMv4Zf2Fh2JkCqC48C+GI8IOdRR2Lkp9yNF7IQmSTAA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4557 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The integrity guarantee of SEV-SNP is enforced through the RMP table. The RMP is used in conjuntion with standard x86 and IOMMU page tables to enforce memory restrictions and page access rights. The RMP is indexed by system physical address, and is checked at the end of CPU and IOMMU table walks. The RMP check is enforced as soon as SEV-SNP is enabled globally in the system. Not every memory access requires an RMP check. In particular, the read accesses from the hypervisor do not require RMP checks because the data confidentiality is already protected via memory encryption. When hardware encounters an RMP checks failure, it raise a page-fault exception. The RMP bit in fault error code can be used to determine if the fault was due to an RMP checks failure. A write from the hypervisor goes through the RMP checks. When the hypervisor writes to pages, hardware checks to ensures that the assigned bit in the RMP is zero (i.e page is shared). If the page table entry that gives the sPA indicates that the target page size is a large page, then all RMP entries for the 4KB constituting pages of the target must have the assigned bit 0. If one of entry does not have assigned bit 0 then hardware will raise an RMP violation. To resolve it, we must split the page table entry leading to target page into 4K. This poses a challenge in the Linux memory model. The Linux kernel creates a direct mapping of all the physical memory -- referred to as the physmap. The physmap may contain a valid mapping of guest owned pages. During the page table walk, we may get into the situation where one of the pages within the large page is owned by the guest (i.e assigned bit is set in RMP). A write to a non-guest within the large page will raise an RMP violation. To workaround it, we call set_memory_4k() to split the physmap before adding the page in the RMP table. This ensures that the pages added in the RMP table are used as 4K in the physmap. The spliting of the physmap is a temporary solution until we work to improve the kernel page fault handler to split the pages on demand. One of the disadvtange of splitting is that eventually, we will end up breaking down the entire physmap unless we combine the split pages back to a large page. I am open to the suggestation on various approaches we could take to address this problem. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/mm/mem_encrypt.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index 7a0138cb3e17..4047acb37c30 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -674,6 +674,12 @@ int rmptable_rmpupdate(struct page *page, struct rmpupdate *val) if (!static_branch_unlikely(&snp_enable_key)) return -ENXIO; + ret = set_memory_4k((unsigned long)page_to_virt(page), 1); + if (ret) { + pr_err("SEV-SNP: failed to split physical address 0x%lx (%d)\n", spa, ret); + return ret; + } + /* Retry if another processor is modifying the RMP entry. */ do { asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFE" From patchwork Wed Mar 24 17:04:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408320 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, UNWANTED_LANGUAGE_BODY, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 959E2C433EA for ; Wed, 24 Mar 2021 17:05:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4B50861A1B for ; Wed, 24 Mar 2021 17:05:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237104AbhCXRFY (ORCPT ); Wed, 24 Mar 2021 13:05:24 -0400 Received: from mail-bn8nam11on2078.outbound.protection.outlook.com ([40.107.236.78]:50401 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S236993AbhCXRE5 (ORCPT ); Wed, 24 Mar 2021 13:04:57 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SVViZ7dJiTw8pVAXAkLNbGBXKJ5r4TI3ZxzXrlHfrBQaQy5X9KTJc0usJh+saExU1ibEMB/TtmnSdVSMWZEHpkbRCGCgURBY8BBcD240VMIW7131COVL+nk1zmbql4PzMBjOABpjPM4+/Zg1FT9e6p/g1jmHp7wyzdDF6dZSuIZWojplaUIBRZ1wqU+cThbXtB6O8eZ/23b1zd/toLGyjMG3tBBWayGMlJMD07YweTWNX6KPjI/mZLUtyVNG0kvG/mJ0bkYJjEu08AdDH1rN6rYljURkrE1VA0Eig8e/ha/swaJg2zwD0EOl4ipm5hBee9dlCTVug1Dx5eL9lHTUkg== 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-SenderADCheck; bh=+IG7ljZ5VGQRURIB0G3dZTvCjGlMm9xP1GQIoqinyO0=; b=Uomh6eaPoSI2OyKLqSxY/y+Dh8KAWLiGfDWZm22alS9ip5kZARXMi2nr0EFgAO68uYgRm6lqLVYN1QsIYTyvQ3S2HNI3ue2kpyXJ7QuY3Uu0dLO+pcV8aY3Jizww5AQMuo+iazvqSgsdBGgc6HkUMP4WinYXsVcbxLoCY0vy12oyySgvG3LsvNmZgj1dDn5nGJODtMEZxAi32iUNPzkIpfRo5D1VMY6/uf3+YG0qCT5zJf8qinN0h/gExybV+fXvkEaCxh9WaXojbMZls7LFOSq0FcnuD3yojanqr94cYO17BAT8cGAvogvPUFWuEQZW5mGs9JXMUD5OoFHfjTB8gg== 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=+IG7ljZ5VGQRURIB0G3dZTvCjGlMm9xP1GQIoqinyO0=; b=vX6r6xCyP7kvz9H2KHVLSIMpO3ytmzJU+XiAeFd2REU2hebpww7YVE8Htzp76ekMU9FtjW3ey+mtx75F3kWpuvwMTZtSfUYv0jldJRwQXVBfqISoiU8pRBgvIcmHnZG+fmDv+V86C86u3fXPEwx7P8npoUYeWyXI/K8FQX9wcJg= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4557.namprd12.prod.outlook.com (2603:10b6:806:9d::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.24; Wed, 24 Mar 2021 17:04:54 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:04:54 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson Subject: [RFC Part2 PATCH 05/30] x86: define RMP violation #PF error code Date: Wed, 24 Mar 2021 12:04:11 -0500 Message-Id: <20210324170436.31843-6-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:04:54 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: ba61c764-818f-484f-e00f-08d8eee6f233 X-MS-TrafficTypeDiagnostic: SA0PR12MB4557: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4502; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: VFFZ/MyinJjrgK1lNWaL4Tnt9tfqFGuKzgxs+/Rw+oGquT1wejxet6XHuY2zkCt0kblLaU5iv1VcbQ4/Lxr6sSFvHRO7EGMHB4hf8aBo4Svkavc2Sm0J6121Tpasm43qj3AGIaiFmTxo51+WNW2feB8LihwOImWL/HiYvnrOpazvH3Jql+ZNsbKc66vC2zi+eG39rJ8nhoMzAoXFBxZIYnfMocvosAz6po8lOZByRUXRrrW6UBB38mTT0IMQnNAnV0Qzb3OOiS3tP5gS2s43zSiRGqqPgPlFgTr08VcEYYRWIE0R02yhpXqNh4BtsK0P7F/8TpYMZ/74lPiHL79UmbM95Qq7swErAE6/tZC4lLdslK0MYSdXYy2Gd6e8bjp+HvoDkn6oo2L4XwRqy586MJlDFQxvI5RSkSIf57Lm4mG9BvCJwxsHWnfpb2JF9GlcjrFvk45T5hbMcHe+3YQLCFMsUAjT6v0kDA9CDgnVV5AmkpnE0CYnluUYEckDgRXM1KNXP0GjfxTdZPeluq7zWmepO5c5NqFCIO5ZEqPzISKUWU6JXJsZe+cQ+NUFtmtjpEeAG8/uUKzJS8poTB6NX9bZjxWSYXUZTadg9zOrcR8UP9z1c+6UBwp725zS4sKaTAJD+LvIFL40bbaxEWW3KQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(376002)(396003)(346002)(136003)(39860400002)(86362001)(54906003)(186003)(44832011)(6666004)(4326008)(38100700001)(26005)(316002)(66476007)(8676002)(16526019)(1076003)(2616005)(83380400001)(5660300002)(6486002)(956004)(52116002)(8936002)(478600001)(7416002)(7696005)(66556008)(36756003)(66946007)(2906002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: vUbOCMZ2smncCJJ//K79LXF271PL086l8FXKM5yzYJAspf+ZfcCHmG2kVk8QDunJDfzHS29edknQLZz6GF57aH7ASTOmqkIufDIaVmns54Jj8/NdOLsDkH8w33s4bVV3YOC8CTJ0gyAWxoxDU42GnViTHvbLywlU92Yar/1njojv37Bf/Dss9mBoWV81OL8kJeEymK5J9XyOjPfqGb0Nmlcj3jOMFBukBskicOykTLDo80OPI49veALxXYgeWI/vEci4zHPof9+Qmdg/eWbYY9YyB0JflwPSljZxItdqsdvu1N1o+Wo/Tvm9Q7Ocnb3xdHCyQTj9EmfFpBCbKzvQ+GulPznJ+iG/d5vlKY4dGDGdWfVlqq2H1z1MV8Wal3pjENN3xp8pYAskyYkFScZbksI0q9Au/R8i/v7jhH+AKW+iRcdgPfofIc/6k5phvI0TMHfnDtgqfQpJa7BLzGtKqv3r5rUxeTTSjTFanFOBp5CHNvjjKE56XmCHYA6T/9Cq6M7fbv9A08secUi2Ibq/QwE957ETaA3BHaHNz10d4YLgk7K9e7w/NFpv088KiVCZw0sYGKQ7fYtusxQWgeTzQQykIlk1o7MFU/QCTnH+KHNE86UgUxV968n46F723Sf/w59nWcuYzeogCvTNMgTtlODqpzOsR9vW2LN2DqxhWVBAuDR8anaE3qypJGFXQtW4k27gPFzsXVdaLu+SJObWmfaua2X4HqBd07ES5/ncyNy/f6TpNvNx64oaLQ7eHEcu3ZIbQEc1efRnJcX63MvCkkRJYGnCCr7uVst4n2//12OTpjjIF1KHrEr2RVTgKvMn+/1tNuFOmhn+2Vkfennx0z8dNH6yjwz+Bguk1+kJD+DRRM0B3PlmMP+nNx5OMoGEVBt+4dKH9MdRfxNrW2VuSlFOEjDfzXh/wqwuTHLo1jMY058LnBm632J7zUj96UQmYQiYTf6ajnogBg8apWinozr3SWvategNXU57TRam3xhdLTlqcIQdU3+fzUaRnwDBd//28zwxCaiMhfAfPSiHMfnBxOSoPi66yMPnUtp+Ck8CepMTbzkM1uHJHikyATr/dv72aTaIAF4iGDHceEPnZ4mArsbni1zUpWcoISIW0o1zb3d5oZwdbTC84mtpcVgAbIAai4zxWtmspZsMs4MZVs8QdtNTFLFQXg4MFmRMV0kiPN6o+hFPfFrRmRQdJo3q8LaqQCJXqx3Kv3AsYsmiFwCCInapMJ/rBbmANfgZc3o1gF0J4ayH0okD3AYt1lrbkXONeFiwj3RG2k6RSoVitqLDtaAzxuO+yEJL+OsQyAw+EdO4GR3E1TbxbxLslYrX X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: ba61c764-818f-484f-e00f-08d8eee6f233 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:04:54.7499 (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: 7YGUSCPNjIGqTQeXmoLd/1LRT85f0Mhy8SGfw5jXhpP8x7TQN6h1F3uDf0NP0/J2PGVhcnnCxtNZRl52z5+fTg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4557 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Bit 31 in the page fault-error bit will be set when processor encounters an RMP violation. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/include/asm/trap_pf.h | 2 ++ arch/x86/mm/fault.c | 1 + 2 files changed, 3 insertions(+) diff --git a/arch/x86/include/asm/trap_pf.h b/arch/x86/include/asm/trap_pf.h index 10b1de500ab1..107f9d947e8d 100644 --- a/arch/x86/include/asm/trap_pf.h +++ b/arch/x86/include/asm/trap_pf.h @@ -12,6 +12,7 @@ * bit 4 == 1: fault was an instruction fetch * bit 5 == 1: protection keys block access * bit 15 == 1: SGX MMU page-fault + * bit 31 == 1: fault was an RMP violation */ enum x86_pf_error_code { X86_PF_PROT = 1 << 0, @@ -21,6 +22,7 @@ enum x86_pf_error_code { X86_PF_INSTR = 1 << 4, X86_PF_PK = 1 << 5, X86_PF_SGX = 1 << 15, + X86_PF_RMP = 1ull << 31, }; #endif /* _ASM_X86_TRAP_PF_H */ diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index f1f1b5a0956a..f39b551f89a6 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -547,6 +547,7 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long ad !(error_code & X86_PF_PROT) ? "not-present page" : (error_code & X86_PF_RSVD) ? "reserved bit violation" : (error_code & X86_PF_PK) ? "protection keys violation" : + (error_code & X86_PF_RMP) ? "rmp violation" : "permissions violation"); if (!(error_code & X86_PF_USER) && user_mode(regs)) { From patchwork Wed Mar 24 17:04:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408319 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2A662C433EB for ; Wed, 24 Mar 2021 17:05:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 088BD61A18 for ; Wed, 24 Mar 2021 17:05:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237113AbhCXRF0 (ORCPT ); Wed, 24 Mar 2021 13:05:26 -0400 Received: from mail-bn8nam11on2078.outbound.protection.outlook.com ([40.107.236.78]:50401 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S236997AbhCXRE6 (ORCPT ); Wed, 24 Mar 2021 13:04:58 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cKp4dM+CLtHN4pjO2vXAjL0HzXncS8Zwn1/+F/mq/wUbsSb0Zo9zr2Gw918GsJHi/wqd53wZ3uLpxC/I2wlfgpcbB4v7Ca1/7IBZrqg/IvsIcqvN9JUWikIBxzUHf1NF9JztPpYAyZYU1sJFJXzn2kwhR6qAfQAUVxR5/t6SG8Z8Zn5BFRwCIrI0n0CqsdgIiiqu4Pv/zVicwpWB4P9N0qKciGnNKVeYZuiMUIrM0O1IUmlcXO8MUBSlYDO52SOO1s3mhkz2R5ySO9ilnK3/6NNFkFGgL6NKaD4gdu2sQ9SgpKL/XYfsyIa3a0If0o+ZqnYt0qtDzONaQIo6z/DiuA== 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-SenderADCheck; bh=+yEbF9rxAe78JeeCbPXQ+GaRfrgmMYZagtEiSfDWdsg=; b=N+YMhG/bJrbHZ97ZJRZun0rMrBDr9SlYRio6sXlVU+PAMkLNIqtSzkHGhcMhXhT9GSB1Bi55YXEaHdd79P/d3Gm6s6+Tidmp+WpQjUDaBihE78u/UeVMOhOWaRbw3gzyyqhcaQ+nKhN9nJestO/av90tTYU4hlY/QyEsjCTZjUfpmpVd+2tyfdfZvrgz+0TskMQGK/ZnwH19s4fpeDcIYrXbbztjtdlO/9fJ9w4Ci/25Xx8X+NAYZCOxv1XUvT2F/rRjv/2XnnUOEoIs0eXx1WnnUJp2A2mwKQO1C9b0K0Hn+KD9fjJ3YG06mNlFh2zzp516F85IkidOXuUadh9LZg== 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=+yEbF9rxAe78JeeCbPXQ+GaRfrgmMYZagtEiSfDWdsg=; b=wy8SWfsJPGLpMgesJ+6Q0hv7v1CJJLrM0OQELLyNlLMqtToW+uawNdRDH2TKiGSmHfUzb2uKdGfTgB7p8NWd1gzYLaVDwinYSWyuqHhEKZT/VRt2fbWnmCDom+p5Xc6laQqYjc/gM0ZWcvae9Q4DKfINy6MxB/8cMMf9J50/9vU= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4557.namprd12.prod.outlook.com (2603:10b6:806:9d::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.24; Wed, 24 Mar 2021 17:04:55 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:04:55 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson Subject: [RFC Part2 PATCH 06/30] x86/fault: dump the RMP entry on #PF Date: Wed, 24 Mar 2021 12:04:12 -0500 Message-Id: <20210324170436.31843-7-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:04:54 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: f488b2fe-b9ee-4718-fecb-08d8eee6f2a8 X-MS-TrafficTypeDiagnostic: SA0PR12MB4557: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6108; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: +5F9xfBjhoKcpKkvtcPmV5uOFCQvyaiXetd3PD9P2MusvoK25erSJW2ZEkEu3MCTGKPMgfevjSxm8Js+Cs2cpA+ZsVKxkjOdjNgg/Cjpop89Dw/XulcGnjlfWd9up2i0U0cYoTIoaXFc/MWy/z9Vsraqm+pfFhcEzTYtH0rUZJoYRJ4OmlEecmctbtfB02QG5ZyFZvZRri2T0vbFsOkhQ1+xO2a1GIxLZpbAE/Pp7bkOvtXVjXwJ92+g+kDOmbZ1UDyAF/rszVst+uSafZrJ2URMS1W7MewZg0fcoIVb47JqtqUSFIXIrWQbH9HKG2Z6iX/f8tw9PXcT/fvE7lOOa/CdlXQirKZvpO47XCVFO6sRAb6z0SFTtZE3qFZ3yYQ2ZtYqReuWHM1tj+aEjod6ujCMotNP18FmoJFbqdieEiO6SPr7P//T/wZspomI2LbHGCklDjNxcst7vJXUTfLuWvevtFd+mZRVG08uv5WBQq/IROtH19SI/hj3Txx3U2Q6HJlOB6mkO5ihsM62Eh0L4Qe46atkA2yUe+2u+ViKsjvzw2Wci8ttXim3f6aLaeKmyWkZW6NCKW5/iHPA4h27wjcaBE7dFFCrb7y5CboS9XMMLV87rT02QA+t1nAZne8qsRyGnhPwSAKZn9GV9XMlqw== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(376002)(396003)(346002)(136003)(39860400002)(86362001)(54906003)(186003)(44832011)(6666004)(4326008)(38100700001)(26005)(316002)(66476007)(8676002)(16526019)(1076003)(2616005)(83380400001)(5660300002)(6486002)(956004)(52116002)(8936002)(478600001)(7416002)(7696005)(66556008)(36756003)(66946007)(2906002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: slak0qRABXmMsRitjEDoJc4nVvIuF+f0WoNLSs7zyS/ewzBvRPY5/Q3H2L7OLeuP3d7Ja1NnLK/Ge696J9mIr850Pbz3jBabikJk3ZUEEFliBN58qZaSLn5KyhIjh/B6T+MVJMf6HoMb77lnk1UOMa+l4zKts2k/+/plyRA1Io9hzjWC+nRl7gPa+MyeOPpiWZM2sPqDJ6U+UcAOJ6Io6ZD7NAr6HUpTWcy09fXM9S2tNZeK5SOcST1zkJu/q2bFMX555RZmhjp+7m2rVmhjoX8L+J/kvc/0ffap5ioxhsIgOvzHnoEV1n2oKnYwSLiskLQRSkl7yaD7/LLrX0s1XotYrx8u+ssfW5sJDy8Cg5uJwKxrFnXcwmLCPKvTpaGCHhwSmIDZpIHbRSJA7IqHQa5gs5tna/9HR0kOldA2FRWMumIm4SbV3ZVWZRWg+//OuJpaHg+uuDu/ciPEX0sio2Ezmy6wdiZ9mbKUWEPvq1C/Ng3HE/Ve//GJGiyu8PqK8jDZARUllYTW/l15E4i/4HFcPBOx3NjBMWajIRpw2KjpZ5dtUM0CbD9xP9tPD7J/RjUTiLbHAOJhg1FNchRSAo25rvSQeAgBrbV2wvuiQaYEmyiUP2flA1b3NxylF5ZXIMqNQhxui/0m3eyMpfWGpmnFmAllBE3aYbyUQDfIDDlUn3mQ9YrKjGbkfPSnzAyCQAsfxE7ZZdisp2GqgKyJkZNZmeWngk1yeZ8n42balS0z6NdAz5aKto27iHW5CC/1YeHw3t3+9bK4tu7F8CP5SEmvbTTT0IVCGxVS54oMBNoIqytSaKOuwzEmtihkA97+TpCV+RoE0I10lH/4jmiJM/A8AEVRemKyGqRNtXNNKQ6WC5wu0RPqskstXnhQ73CheFLztRc/w1XLAfXJEZrHmn12AYcRRb/UYzSDFX7GNuQm6Wn6RmeyRTKLVwCM26j0TtOpEoRIfNYRKHKGcbG8BQnKAcAaKWkIFiqZnBpCxQXwpbedISC0xFQzLOows9o27tJEYTzRJzxLruEBSr1iQY40Lfzhz5LdDzRS/hFg84VMTx7UTDzNI7gIQmvmRJhT4vkTJu3wbaKGM/qsxcKDe3CfEZIvLyTLUg57aMm9RcsRyNNjrx32OQnhxwFzd23giv2eWPW4LFzMSf3dTxgDVhET1Sw2+booVD4izikWLQ0ZictBwcTRHfv/pyysAzWl2s5800EE+P51bOQA53dwfkopMMPtCjuqk8uTj1HKSYBP5yxStWj17MJXRRIzGGR/MoxlQWET+TQCBw4gXt3TmUlbGG0TTnTNRjzpHBcnwQwhFIHETEFeRQg+qVjeTRX4 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: f488b2fe-b9ee-4718-fecb-08d8eee6f2a8 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:04:55.5924 (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: HTp773CCWwOLEJaubrYa7cRAdodqgjeq78DS3gYfEz0OMg2jo+SOy9Y0LR1JAl1SIgV5RlVP8oypsM+5wwWTYQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4557 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org If hardware detects an RMP violation, it will raise a page-fault exception with the RMP bit set. To help the debug, dump the RMP entry of the faulting address. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/mm/fault.c | 75 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index f39b551f89a6..7605e06a6dd9 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -31,6 +31,7 @@ #include /* VMALLOC_START, ... */ #include /* kvm_handle_async_pf */ #include /* fixup_vdso_exception() */ +#include /* lookup_rmpentry ... */ #define CREATE_TRACE_POINTS #include @@ -147,6 +148,76 @@ is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr) DEFINE_SPINLOCK(pgd_lock); LIST_HEAD(pgd_list); +static void dump_rmpentry(struct page *page, rmpentry_t *e) +{ + unsigned long paddr = page_to_pfn(page) << PAGE_SHIFT; + + pr_alert("RMPEntry paddr 0x%lx [assigned=%d immutable=%d pagesize=%d gpa=0x%lx asid=%d " + "vmsa=%d validated=%d]\n", paddr, rmpentry_assigned(e), rmpentry_immutable(e), + rmpentry_pagesize(e), rmpentry_gpa(e), rmpentry_asid(e), rmpentry_vmsa(e), + rmpentry_validated(e)); + pr_alert("RMPEntry paddr 0x%lx %016llx %016llx\n", paddr, e->high, e->low); +} + +static void show_rmpentry(unsigned long address) +{ + struct page *page = virt_to_page(address); + rmpentry_t *entry, *large_entry; + int level, rmp_level; + pgd_t *pgd; + pte_t *pte; + + /* Get the RMP entry for the fault address */ + entry = lookup_page_in_rmptable(page, &rmp_level); + if (!entry) { + pr_alert("SEV-SNP: failed to read RMP entry for address 0x%lx\n", address); + return; + } + + dump_rmpentry(page, entry); + + /* + * If fault occurred during the large page walk, dump the RMP entry at base of 2MB page. + */ + pgd = __va(read_cr3_pa()); + pgd += pgd_index(address); + pte = lookup_address_in_pgd(pgd, address, &level); + if ((level > PG_LEVEL_4K) && (!IS_ALIGNED(address, PMD_SIZE))) { + address = address & PMD_MASK; + large_entry = lookup_page_in_rmptable(virt_to_page(address), &rmp_level); + if (!large_entry) { + pr_alert("SEV-SNP: failed to read large RMP entry 0x%lx\n", + address & PMD_MASK); + return; + } + + dump_rmpentry(virt_to_page(address), large_entry); + } + + /* + * If the RMP entry at the faulting address was not assigned, then dump may not provide + * any useful debug information. Iterate through the entire 2MB region, and dump the RMP + * entries if one of the bit in the RMP entry is set. + */ + if (!rmpentry_assigned(entry)) { + unsigned long start, end; + + start = address & PMD_MASK; + end = start + PMD_SIZE; + + for (; start < end; start += PAGE_SIZE) { + entry = lookup_page_in_rmptable(virt_to_page(start), &rmp_level); + if (!entry) + return; + + /* If any of the bits in RMP entry is set then dump it */ + if (entry->high || entry->low) + pr_alert("RMPEntry paddr %lx: %016llx %016llx\n", + page_to_pfn(page) << PAGE_SHIFT, entry->high, entry->low); + } + } +} + #ifdef CONFIG_X86_32 static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address) { @@ -580,6 +651,10 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long ad } dump_pagetable(address); + + if (error_code & X86_PF_RMP) + show_rmpentry(address); + } static noinline void From patchwork Wed Mar 24 17:04:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408312 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EDA82C433EC for ; Wed, 24 Mar 2021 17:05:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A92F261A13 for ; Wed, 24 Mar 2021 17:05:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237106AbhCXRFZ (ORCPT ); Wed, 24 Mar 2021 13:05:25 -0400 Received: from mail-bn8nam11on2078.outbound.protection.outlook.com ([40.107.236.78]:50401 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S236999AbhCXRE7 (ORCPT ); Wed, 24 Mar 2021 13:04:59 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=f6xSxedJfOf5oQ06SwzmFNbO4M/+sF7DpKTbs2N+26k7QGpBBpgVQyO1TEFEXa80Un5bDVA9eNr0eJEzOZ2Z60q+vLdTwi2Fg3LXACmPQXtm5pmNh1TtcYOhSy3UhKwINWxY/CeMN96+iUoV3lb2+3jjrQTUoX3y9Ur/jqEf2rdERjBCbgER5VYu0AuqHLCt7X9yACyQA8B1SYwHOS0Q405ALODoAiG+cjnWx3f0hYyX+fZfSd+quk5KyrZJKs2tJtRGS4W02uwYfzke1cRHkjOBgs4LZ2O9PAxf44VH+KYfjOOwMdIjqeCKPU2rF3bjTa30h2PUJFbPlCxGvcR5fg== 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-SenderADCheck; bh=do0UcJtGd1tsraFXZSOPOCjSYY6X2rNAIohoHuu/wmU=; b=HxI6EXvvNrwfgXI+dKXwKGViv4wBQeAXbVCqwTvp3wHYOr00MSAcamRQfDNInMq7LTk5hfwzSGxrZN8WCdthfyXx7IaR1AbQovxpzN95QTuu+PjB3o+h9lavQ1iSH0eojAMBvcYg9ksMdYjrNmxGXJZLjQ0eaFyY/1gYuhu/K/+4to4ZzrgccTcIrOerTd7Cl36pcgbjCMCw5XwlDY/FgT4nqlMIsGnyKdk2BhXLKw9QHC6FJ7lAI0HKbVt8WmUj5MT7aLzD0Yn6J+UT5EZce/bh8AOmgG5nEMGEr92IaO0ADSgbI7PO5wvBzagEwgfyzAw5pw3KYhP1T8gZOQEpQw== 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=do0UcJtGd1tsraFXZSOPOCjSYY6X2rNAIohoHuu/wmU=; b=zvh/zJNlyIYMBQs+ddfM24s30Naz9H7fBfzKoO8QH/c8zs5ciKdxP81+9WFHr9FMSd0zH/y5g02tc/vlPkx/oWRzKCjWLyZYB7UHQh8sKljTm52Mk7TqNzy8OHSvGzwSQ+y3D48BntdRrohJhV4izL1KUIPmCoG9oS+Jor/dXE8= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4557.namprd12.prod.outlook.com (2603:10b6:806:9d::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.24; Wed, 24 Mar 2021 17:04:56 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:04:56 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson Subject: [RFC Part2 PATCH 07/30] mm: add support to split the large THP based on RMP violation Date: Wed, 24 Mar 2021 12:04:13 -0500 Message-Id: <20210324170436.31843-8-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:04:55 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 19a39c8b-4a3c-4b70-e34d-08d8eee6f337 X-MS-TrafficTypeDiagnostic: SA0PR12MB4557: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:913; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: FXch2kW+LV4YCVqStkMnPkE8+8fcPaHy+H34/FHIXDqkFxzWhUxzWBXKTOLj4oMXOckAaZPZ+53fG27q6/lTA4F202h1hXy7GSSg6bJ0ye3RyVkKBQ64J5DxAiIEu1drBos0L7BsgD8NzLB+ZA2W9l+UePHkTYJmD8Pj1e3GIuPDBiry9N+1Rchyw6fhzSrRbXhCv8ncXEhQxRKyS3cmH4wN3Q0bvvFKm8+pot+2Ap50hlYN2kVvZrLC4dyLto2fnB5K+2qijXgd5QCtmGSJy+XpeWQOBQB00RF2lO3VejgfOAWoP+nN8lXe9CcKWHsGzHAkaO1MYqmv95sBUpzGASuolW6Oc/urEL274H/AF3WCd3aeolJC7j7nbeivy2T9qgAw/OtxLrx8lOzX0nBYroDI0VokZf6B/UyCiqnBv+13iN1zKTtcWeXv33yl1VgIH6D1EbgHz6ElwTb3Y/fJ18EVNicZP6b1itgaq0BoXNzvjUpPGZlHCY98Xnf/NGJQkRRbEfaSC6OnAfcz4QDM34IHnzL4E5jag24qfBnnLQuOK1Mps0p0levRYNsk9oNW1R+w96G6A8CJQPJfaSwcmUF/BrC0AI2ksfi9C6L7N+h2aNiuCY1BFf0G3JFwAujPlpmxjNIbrMpzSGBADCwNJ9hFgocB4C96Qsv5jpzABpQ= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(376002)(396003)(346002)(136003)(39860400002)(86362001)(54906003)(186003)(44832011)(6666004)(4326008)(38100700001)(26005)(316002)(66476007)(8676002)(16526019)(1076003)(2616005)(83380400001)(5660300002)(6486002)(956004)(52116002)(8936002)(478600001)(7416002)(7696005)(66556008)(36756003)(66946007)(2906002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: CiutqyPj5xo6QsRvwHDeUthMliKzoNdDzYoBwbZNk/kF7iD9K45qVfAZnQq43IhlmSJmYhb+5LUca0WnXkuYgkb6b8NT9PEMK6BHSWM21/U0eElFvGbUotj8OA5E1wmPMXrc0qQp31wBGV2Q7s8CMJ+5r1L7Qnw8uBZKYGzuP1ibVgPxHpasF/guXeDA03UEP85yBvUfVrmGah1O7Qg58hM3c70CC2dZMerCGNCmGPdPgk6ZJMS5tuhZsHHcnTKEdt4bl+m5lR7uNFPCHRfMEPzhZSfpki3cfcV73jRZxwFe+Cjhzjfkp2Q9Liydu9spQKYc7YEC2Bs1Gxvvc3r4VUf6SiJOgWK8TxXdK3O66y0wZU5TcUEOFwtNqDN1x41bOz3zhrxy5vjYYOVvaRQ5WwInCF7d3jROJ4MvjmXKBdRkRaKd2BRzn2wHff/TQ5gFasMlTyaL+icLs2TacuM0+1IAwYgQ4IGcGI28su/2LI6RgPiSmJol4fldqwtTHLFy/YYdUQGWjvXd9/OTHWUTehUPPNdPh+CcmkfUWUHsPeRga4uhUXlnyJdNysokj3Ld2EC715f3q7Q/NoaqTxPxTBc6ewLNtooFCabxb+uH9pYeTDu2o/AGgdaambeBNEgctdG3E0s8tzUx1lWus8MXY7qGNXJAkZTK4WIldvmwpzTuAsObixK9LCGa4+rHswPnMWpOqwUjaDMTW2c49aYowg/UbJnQx7X4CrMG8G1QuLIS3jsOJsHJSMwZ1CRFuXVfLqBoJ95s982qnSCE6/iYNqLQAuhNmLvvrsjBh6Fu07+kuvgEDhUP6aO+GVnbw7FEDBGuxhxh1BOSi7YQTUDnItzU7hCXIggbp5D9WhLzODfXUWxrT0JJXB7Rfx0r60P1LqkYIPB9Z9Lp0duJ29J9i+2lzGJA1xNiQcQh+C8hXSrgz5Me/yxlL41wz5L40ddPxM9QfJ6rk4iF4O0/+c3OABc1s77ieZMlxtt840q5eim98YE4y5/QcCdTrcHALe8hImTvVJEYvDP+DYqX7Ly3EGYG+uowLLQgza6wicCBkxKR8AP66AD6MnTElPDJd95Vtbv8E8tQ2TZhb96u6SLdBMGuZCWPCdg6wzjd2eI4pyIZExaHTGBUmYF4xjBuk/u722wHi0PJzG+WNw0KU6XKcRu9r36CpW0/v+XUbWbbDUEJcC7I27gt2EY6d8/QxKd1lztfrxlta6ruKEAfo+/OjHIEH/678CWmtMWJdqIxq4wFdXhNt6AXWlj5d6v53RDA6y/bUfPP8NGlFS9Tt2KU25032gXh9uZYvzgPEJKf/VypaW5duVRlNpUpyIaBYQW+ X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 19a39c8b-4a3c-4b70-e34d-08d8eee6f337 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:04:56.4629 (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: pfqppQlQkuvub/Fc3lQyxJo6k5nWJPwxiwyBJgBsDDxn/aAGtHb5mhJM4LOYqR4EuQqYGv7ba/HP541ilruARw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4557 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org When SEV-SNP is enabled globally in the system, a write from the hypervisor can raise an RMP violation. We can resolve the RMP violation by splitting the virtual address to a lower page level. e.g - guest made a page shared in the RMP entry so that the hypervisor can write to it. - the hypervisor has mapped the pfn as a large page. A write access will cause an RMP violation if one of the pages within the 2MB region is a guest private page. The above RMP violation can be resolved by simply splitting the large page. The architecture specific code will read the RMP entry to determine if the fault can be resolved by splitting and propagating the request to split the page by setting newly introduced fault flag (FAULT_FLAG_PAGE_SPLIT). If the fault cannot be resolved by splitting, then a SIGBUS signal is sent to terminate the process. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/mm/fault.c | 81 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/mm.h | 6 +++- mm/memory.c | 11 ++++++ 3 files changed, 97 insertions(+), 1 deletion(-) diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index 7605e06a6dd9..f6571563f433 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -1305,6 +1305,70 @@ do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, } NOKPROBE_SYMBOL(do_kern_addr_fault); +#define RMP_FAULT_RETRY 0 +#define RMP_FAULT_KILL 1 +#define RMP_FAULT_PAGE_SPLIT 2 + +static inline size_t pages_per_hpage(int level) +{ + return page_level_size(level) / PAGE_SIZE; +} + +/* + * The RMP fault can happen when a hypervisor attempts to write to: + * 1. a guest owned page or + * 2. any pages in the large page is a guest owned page. + * + * #1 will happen only when a process or VMM is attempting to modify the guest page + * without the guests cooperation. If a guest wants a VMM to be able to write to its memory + * then it should make the page shared. If we detect #1, kill the process because we can not + * resolve the fault. + * + * #2 can happen when the page level does not match between the RMP entry and x86 + * page table walk, e.g the page is mapped as a large page in the x86 page table but its + * added as a 4K shared page in the RMP entry. This can be resolved by splitting the address + * into a smaller page level. + */ +static int handle_rmp_page_fault(unsigned long hw_error_code, unsigned long address) +{ + unsigned long pfn, mask; + int rmp_level, level; + rmpentry_t *e; + pte_t *pte; + + /* Get the native page level */ + pte = lookup_address_in_mm(current->mm, address, &level); + if (unlikely(!pte)) + return RMP_FAULT_KILL; + + pfn = pte_pfn(*pte); + if (level > PG_LEVEL_4K) { + mask = pages_per_hpage(level) - pages_per_hpage(level - 1); + pfn |= (address >> PAGE_SHIFT) & mask; + } + + /* Get the page level from the RMP entry. */ + e = lookup_page_in_rmptable(pfn_to_page(pfn), &rmp_level); + if (!e) { + pr_alert("SEV-SNP: failed to lookup RMP entry for address 0x%lx pfn 0x%lx\n", + address, pfn); + return RMP_FAULT_KILL; + } + + /* Its a guest owned page */ + if (rmpentry_assigned(e)) + return RMP_FAULT_KILL; + + /* + * Its a shared page but the page level does not match between the native walk + * and RMP entry. + */ + if (level > rmp_level) + return RMP_FAULT_PAGE_SPLIT; + + return RMP_FAULT_RETRY; +} + /* Handle faults in the user portion of the address space */ static inline void do_user_addr_fault(struct pt_regs *regs, @@ -1315,6 +1379,7 @@ void do_user_addr_fault(struct pt_regs *regs, struct task_struct *tsk; struct mm_struct *mm; vm_fault_t fault; + int ret; unsigned int flags = FAULT_FLAG_DEFAULT; tsk = current; @@ -1377,6 +1442,22 @@ void do_user_addr_fault(struct pt_regs *regs, if (hw_error_code & X86_PF_INSTR) flags |= FAULT_FLAG_INSTRUCTION; + /* + * If its an RMP violation, see if we can resolve it. + */ + if ((hw_error_code & X86_PF_RMP)) { + ret = handle_rmp_page_fault(hw_error_code, address); + if (ret == RMP_FAULT_PAGE_SPLIT) { + flags |= FAULT_FLAG_PAGE_SPLIT; + } else if (ret == RMP_FAULT_KILL) { + fault |= VM_FAULT_SIGBUS; + mm_fault_error(regs, hw_error_code, address, fault); + return; + } else { + return; + } + } + #ifdef CONFIG_X86_64 /* * Faults in the vsyscall page might need emulation. The diff --git a/include/linux/mm.h b/include/linux/mm.h index ecdf8a8cd6ae..1be3218f3738 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -434,6 +434,8 @@ extern pgprot_t protection_map[16]; * @FAULT_FLAG_REMOTE: The fault is not for current task/mm. * @FAULT_FLAG_INSTRUCTION: The fault was during an instruction fetch. * @FAULT_FLAG_INTERRUPTIBLE: The fault can be interrupted by non-fatal signals. + * @FAULT_FLAG_PAGE_SPLIT: The fault was due page size mismatch, split the region to smaller + * page size and retry. * * About @FAULT_FLAG_ALLOW_RETRY and @FAULT_FLAG_TRIED: we can specify * whether we would allow page faults to retry by specifying these two @@ -464,6 +466,7 @@ extern pgprot_t protection_map[16]; #define FAULT_FLAG_REMOTE 0x80 #define FAULT_FLAG_INSTRUCTION 0x100 #define FAULT_FLAG_INTERRUPTIBLE 0x200 +#define FAULT_FLAG_PAGE_SPLIT 0x400 /* * The default fault flags that should be used by most of the @@ -501,7 +504,8 @@ static inline bool fault_flag_allow_retry_first(unsigned int flags) { FAULT_FLAG_USER, "USER" }, \ { FAULT_FLAG_REMOTE, "REMOTE" }, \ { FAULT_FLAG_INSTRUCTION, "INSTRUCTION" }, \ - { FAULT_FLAG_INTERRUPTIBLE, "INTERRUPTIBLE" } + { FAULT_FLAG_INTERRUPTIBLE, "INTERRUPTIBLE" }, \ + { FAULT_FLAG_PAGE_SPLIT, "PAGESPLIT" } /* * vm_fault is filled by the pagefault handler and passed to the vma's diff --git a/mm/memory.c b/mm/memory.c index feff48e1465a..c9dcf9b30719 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4427,6 +4427,12 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf) return 0; } +static int handle_split_page_fault(struct vm_fault *vmf) +{ + __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL); + return 0; +} + /* * By the time we get here, we already hold the mm semaphore * @@ -4448,6 +4454,7 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, pgd_t *pgd; p4d_t *p4d; vm_fault_t ret; + int split_page = flags & FAULT_FLAG_PAGE_SPLIT; pgd = pgd_offset(mm, address); p4d = p4d_alloc(mm, pgd, address); @@ -4504,6 +4511,10 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, pmd_migration_entry_wait(mm, vmf.pmd); return 0; } + + if (split_page) + return handle_split_page_fault(&vmf); + if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) { if (pmd_protnone(orig_pmd) && vma_is_accessible(vma)) return do_huge_pmd_numa_page(&vmf, orig_pmd); From patchwork Wed Mar 24 17:04:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408313 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 88E89C433F4 for ; Wed, 24 Mar 2021 17:05:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5E33F61A17 for ; Wed, 24 Mar 2021 17:05:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237119AbhCXRF0 (ORCPT ); Wed, 24 Mar 2021 13:05:26 -0400 Received: from mail-bn8nam11on2078.outbound.protection.outlook.com ([40.107.236.78]:50401 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237018AbhCXRFA (ORCPT ); Wed, 24 Mar 2021 13:05:00 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Yjfc5B+FstPrCTIEUid3VxrjqA9DtvcdUR6HvE5h6FUar609LFpTvHw88/48JKFtT50d+wmgJw+s1f5odEdW93gyza92pYxpXG+F7SQSDvueoJAEF2VCUvUS/B08q18E77A06GQR1F6Ap7ojBjaPF/5akt2bbS0+DZ0j/Mbo3c3x9wSHsdzWsSteVSdYBVOgQwrknNqzZ0ReJ7luI27oXgMDUPzEXzO/20PpK7I7O+pc2YD8Dweqackt2BYHVo05liydFXGzBB8SCl8TseKOH4+A8Dnzn8kq7zxlZ/VDpwVzYZiRAJybfNC1tKf/DC2LX/0gj1HOs85J9NKRy+5l4A== 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-SenderADCheck; bh=1zIFPFsBZoJQAkDbY+RAwOdM9hZj5RW1qMw2NnWtgbY=; b=I45IuZQYg54mlHZ5LLA9ltTmiAtRXyciCfb2dNxrNnCSwNKPyyVR4MQou4btWfLc5XanAYR52aDzsQzNxpcZJqWpJeWV1TyZNWU3Nv+p/xjOkGDjZst1SjOFbsqXXxF8KD1opguEdERtrX+2oJpkYDfE2zJod49OrxiW+Fm+CWPc1x8YkAEXAwu08MGx3HwXfqh6ktPJdJ6WVTTGI2X8Q7vzBNxFc9KqCA1Iinx4S+lnK/WVG5ulp+yFQtC1anYOrkh18Z0pT2U7eH0Z0RcJqZgnIkImw09FdwBXPTOpkWzMMvB49smeH6uQl52nGjD2eE53A4NAGsjHRLG2xy6XeQ== 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=1zIFPFsBZoJQAkDbY+RAwOdM9hZj5RW1qMw2NnWtgbY=; b=vlg3F4kMQeF1XnYWUlmUQ09fi1A6qzApBF8BQHRgj2q/DN1+MmEWme2FKOvBF7hMhHSn5wV5r72QBHbPta94z/l+DFQ+M8Du8Ayt6xYpXzhbXt2GSynQ2x5RtRvXJuAB4XqCnsT3eYhJHvkjZzXH4Z/dxCFQS23TvfBgHy0w/pQ= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4557.namprd12.prod.outlook.com (2603:10b6:806:9d::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.24; Wed, 24 Mar 2021 17:04:57 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:04:57 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson Subject: [RFC Part2 PATCH 08/30] crypto:ccp: define the SEV-SNP commands Date: Wed, 24 Mar 2021 12:04:14 -0500 Message-Id: <20210324170436.31843-9-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:04:56 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 68a7efcf-27aa-4da0-f923-08d8eee6f3b0 X-MS-TrafficTypeDiagnostic: SA0PR12MB4557: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6790; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ZpjEPeazsynTcYQ+1amrFU+5ND4g80XwwYTvUrkJEOLGi64ZCFijACh8gNxiUD8bRrJxlcKF3EGtQeopEqP0Z7jxOqAIxCbK1lAA7Pz91/teQpfM6KaKJYI1NG2M+sQ+kZoR7ZD+acx2XxY3kvlDZB0vnaMKgmuO1NIkAWHxf96njuAi/n0Q8tjuSRHUw/TzhE7qFjHVvVyiPN9LikBRQMO9HWXoH1jMEnK99p2ACniQwelR9qn0nLxIOCKfUZqMBTz26DEyc7MboGPSbe8Z2+F5gU/gtTtID6O46piSsAubqyvqq1EKR5vY6ByZHAdlbJdcRsHW6Uk+oeXsv/eaWcouK16VljP+eX9SqIvd12DzXeX5qTaltNxhazZn0iwDMYg5NzezHafJc/5yabavSZcwpwWKXSQIV57cRA02/GWgrwoSbJHhoyFMzukWiT4vrIQNn4bXHpH2GUy0gIO4a8qBQM55SYBxBJdqK7ogc3aWDfpTF5kxfSh7yubEWbZh1aiWTtT2K2hPhee8g6VtFJ56k7EcN3/luB3zc1HH3wuP0Hw/yI9ThzS2ZP8GkprwcngpByLN7SMqkhwp/59DzvB22qgcmQTp6yCHOooe9afsq8LX52oXb/spEP1ytXa5uGKFFo5twSY3SMiNM5lCN3WJhoauqFvb0hsfPfF0KrA= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(376002)(396003)(346002)(136003)(39860400002)(86362001)(54906003)(186003)(44832011)(6666004)(4326008)(38100700001)(26005)(316002)(66476007)(8676002)(16526019)(1076003)(30864003)(2616005)(83380400001)(5660300002)(6486002)(956004)(52116002)(8936002)(478600001)(7416002)(7696005)(66556008)(36756003)(66946007)(2906002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: Imrh3UpJGYMqDpKqaf4ur/Hb93OYMN2eIDHMsxHI6zplsKF1hkQt+QSA4b02eoUV/7NHMRcAznI7h8xmOSZH5znH7flc1MIHtAZZrWySrrRVo3P4a+S1Z0GHY92yrDOTATi+v7eva21Ds1TmKHDOYgwgJC5lgC1zNIh93d0fKnP1905zuqlaXPToQtMinWkOwquV0VuqSy94nJvFlZpSZj6pH08cqH/QEs1xCCi2SJAaXCzfvdYfuBlG3L6jpkgYtbh73QRFQr+dbmiTLSs3DaHRo39tmwc8SxcLb6VKdBFvsNeCEcTOoBrHUpJOtOJjGrqr1d0zumojSSS2aBNOo3VZlrOKyOzKEPE/EmUyQCd5HcdVhZNkyyj9lqULq3S32qraFXdmpNhYqI1YcC2nUg+HSEF7+mv6OUxP545M1EUIkTwlAtl7x6mdmmmuT/9kNPIEOQGSPeJPWerOJpmO0Is+21i9uxxnNYvfdinArxAX8DnZMAwdMMOjXM6FL6B71c6FwGItH3dcXxxh+Ldglmb6x4QPvt9trjV8V82LVoBhafWOZjFhY+zkUXsKqZjviRgmW3UUOWqxujg3AApegD79hBdXDZU6kmbAOTrwUi7o6Aq3zupXbmBnaU13kbabB0j417CRCw8Vd4FnsLpSmVCYNMipQ4iStrj5R9jPlpU90Tr7Bh89sIQsWHp87BU89MC4/SUd49BfvxGs7BRIrlKTw94aDinmgyg3qBZSi4D/JO6uDybdVHJjSy8oPyvvUS7NZoxjDPNqwk5TfGyWnPRGxx2SfUYNiZzJtXZND8XKGdPBx80/3jVIEP5wQitADi5bkw1BmAS8qZ1NVL71hGPFUcdAabJU6epHLl7ilqG+zuOf93lUwPDYZ+KAgEbFkwSzvvFcO9T08i1siKF6ri9rb9omJSsCbrHAGoCyi0ZdfBsva4ov5cSo2nRhHcvUmOl+frNY8PY2U8Kyvbr1UC1s1rxaLF0N0R8Q3Wgqv2zZ8S06al2HcHMJ9KbzvsIrZAmyQTtdxcqJj2i9myCfOQxOL9Y3R3VijXunxF1ka8RV5AIwXRKmXCaS1VNPm6TV2txZFdfBKfMR5OGQGlt51YJyb8hp9k+t10ZfJiP1bA5iambZfSWL76ypo30+JjiFDr170mZ9H3KTXGw2TNey1IQNjFNhi/n1OaSjvU4d/ep0kNh50XIRUKtxSClo7eaKDoPKkJqJIbTqCoV6V3Z/8L2dHWqqQBrPZvKkc15GaeGeKo9E5zMnK+idwaG0k1GCRNhKDvJBwGNVUPkL4McTC0iLhkZ87BPOY9z/sPUMKpflD6OddaQ1IK/ujfXZwKdO X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 68a7efcf-27aa-4da0-f923-08d8eee6f3b0 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:04:57.2534 (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: I9iP4Gjx02ztRFm+kp/9I2WjhhWfBE69b/dCA+1IFZ5NWm2sZcFV6H7VJnTHDWnwL/j/cw6gbnlUGiHZq43WHQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4557 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org AMD introduced the next generation of SEV called SEV-SNP (Secure Nested Paging). SEV-SNP builds upon existing SEV and SEV-ES functionality while adding new hardware security protection. Define the commands and structures used to communicate with the AMD-SP when creating and managing the SEV-SNP guests. The SEV-SNP firmware spec is available at developer.amd.com/sev. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- drivers/crypto/ccp/sev-dev.c | 11 ++ include/linux/psp-sev.h | 210 +++++++++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 27 +++++ 3 files changed, 248 insertions(+) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 476113e12489..8a9fd843ad9e 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -128,6 +128,17 @@ static int sev_cmd_buffer_len(int cmd) case SEV_CMD_LAUNCH_UPDATE_SECRET: return sizeof(struct sev_data_launch_secret); case SEV_CMD_DOWNLOAD_FIRMWARE: return sizeof(struct sev_data_download_firmware); case SEV_CMD_GET_ID: return sizeof(struct sev_data_get_id); + case SEV_CMD_SNP_GCTX_CREATE: return sizeof(struct sev_data_snp_gctx_create); + case SEV_CMD_SNP_LAUNCH_START: return sizeof(struct sev_data_snp_launch_start); + case SEV_CMD_SNP_LAUNCH_UPDATE: return sizeof(struct sev_data_snp_launch_update); + case SEV_CMD_SNP_ACTIVATE: return sizeof(struct sev_data_snp_activate); + case SEV_CMD_SNP_DECOMMISSION: return sizeof(struct sev_data_snp_decommission); + case SEV_CMD_SNP_PAGE_RECLAIM: return sizeof(struct sev_data_snp_page_reclaim); + case SEV_CMD_SNP_GUEST_STATUS: return sizeof(struct sev_data_snp_guest_status); + case SEV_CMD_SNP_LAUNCH_FINISH: return sizeof(struct sev_data_snp_launch_finish); + case SEV_CMD_SNP_PAGE_UNSMASH: return sizeof(struct sev_data_snp_page_unsmash); + case SEV_CMD_SNP_PLATFORM_STATUS: return sizeof(struct sev_data_snp_platform_status_buf); + case SEV_CMD_SNP_GUEST_REQUEST: return sizeof(struct sev_data_snp_guest_request); default: return 0; } diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 49d155cd2dfe..df89f0207099 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -83,6 +83,34 @@ enum sev_cmd { SEV_CMD_DBG_DECRYPT = 0x060, SEV_CMD_DBG_ENCRYPT = 0x061, + /* SNP specific commands */ + SEV_CMD_SNP_INIT = 0x81, + SEV_CMD_SNP_SHUTDOWN = 0x82, + SEV_CMD_SNP_PLATFORM_STATUS = 0x83, + SEV_CMD_SNP_DF_FLUSH = 0x84, + SEV_CMD_SNP_DOWNLOAD_FIRMWARE = 0x85, + SEV_CMD_SNP_GET_ID = 0x86, + SEV_CMD_SNP_DECOMMISSION = 0x90, + SEV_CMD_SNP_ACTIVATE = 0x91, + SEV_CMD_SNP_GUEST_STATUS = 0x92, + SEV_CMD_SNP_GCTX_CREATE = 0x93, + SEV_CMD_SNP_GUEST_REQUEST = 0x94, + SEV_CMD_SNP_ACTIVATE_EX = 0x95, + SEV_CMD_SNP_LAUNCH_START = 0xA0, + SEV_CMD_SNP_LAUNCH_UPDATE = 0xA1, + SEV_CMD_SNP_LAUNCH_FINISH = 0xA2, + SEV_CMD_SNP_DBG_DECRYPT = 0xB0, + SEV_CMD_SNP_DBG_ENCRYT = 0xB1, + SEV_CMD_SNP_PAGE_SWAP_OUT = 0xC0, + SEV_CMD_SNP_PAGE_SWAP_IN = 0xC1, + SEV_CMD_SNP_PAGE_MOVE = 0xC2, + SEV_CMD_SNP_PAGE_MD_INIT = 0xC3, + SEV_CMD_SNP_PAGE_MD_RECLAIM = 0xC4, + SEV_CMD_SNP_PAGE_RO_RECLAIM = 0xC5, + SEV_CMD_SNP_PAGE_RO_RESTORE = 0xC6, + SEV_CMD_SNP_PAGE_RECLAIM = 0xC7, + SEV_CMD_SNP_PAGE_UNSMASH = 0xC8, + SEV_CMD_MAX, }; @@ -483,6 +511,188 @@ struct sev_data_dbg { u32 len; /* In */ } __packed; +/** + * struct sev_data_snp_platform_status_buf - SNP_PLATFORM_STATUS command params + * + * @address: physical address where the status should be copied + */ +struct sev_data_snp_platform_status_buf { + u64 status_paddr; /* In */ +} __packed; + +/** + * struct sev_data_snp_download_firmware - SNP_DOWNLOAD_FIRMWARE command params + * + * @address: physical address of firmware image + * @len: len of the firmware image + */ +struct sev_data_snp_download_firmware { + u64 address; /* In */ + u32 len; /* In */ +} __packed; + +/** + * struct sev_data_snp_gctx_create - SNP_GCTX_CREATE command params + * + * @gctx_paddr: system physical address of the page donated to firmware by + * the hypervisor to contain the guest context. + */ +struct sev_data_snp_gctx_create { + u64 gctx_paddr; /* In */ +} __packed; + +/** + * struct sev_data_snp_activate - SNP_ACTIVATE command params + * + * @gctx_paddr: system physical address guest context page + * @asid: ASID to bind to the guest + */ +struct sev_data_snp_activate { + u64 gctx_paddr; /* In */ + u32 asid; /* In */ +} __packed; + +/** + * struct sev_data_snp_decommission - SNP_DECOMMISSION command params + * + * @address: system physical address guest context page + */ +struct sev_data_snp_decommission { + u64 gctx_paddr; /* In */ +} __packed; + +/** + * struct sev_data_snp_launch_start - SNP_LAUNCH_START command params + * + * @gctx_addr: system physical address of guest context page + * @policy: guest policy + * @ma_gctx_addr: system physical address of migration agent + * @imi_en: launch flow is launching an IMI for the purpose of + * guest-assisted migration. + * @ma_en: the guest is associated with a migration agent + */ +struct sev_data_snp_launch_start { + u64 gctx_paddr; /* In */ + u64 policy; /* In */ + u64 ma_gctx_paddr; /* In */ + u32 ma_en:1; /* In */ + u32 imi_en:1; /* In */ + u32 rsvd:30; +} __packed; + +/* SNP support page type */ +enum { + SNP_PAGE_TYPE_NORMAL = 0x1, + SNP_PAGE_TYPE_VMSA = 0x2, + SNP_PAGE_TYPE_ZERO = 0x3, + SNP_PAGE_TYPE_UNMEASURED = 0x4, + SNP_PAGE_TYPE_SECRET = 0x5, + SNP_PAGE_TYPE_CPUID = 0x6, + + SNP_PAGE_TYPE_MAX +}; + +/** + * struct sev_data_snp_launch_update - SNP_LAUNCH_UPDATE command params + * + * @gctx_addr: system physical address of guest context page + * @imi_page: indicates that this page is part of the IMI of the guest + * @page_type: encoded page type + * @page_size: page size 0 indicates 4K and 1 indicates 2MB page + * @address: system physical address of destination page to encrypt + * @vmpl3_perms: VMPL permission mask for VMPL3 + * @vmpl2_perms: VMPL permission mask for VMPL2 + * @vmpl1_perms: VMPL permission mask for VMPL1 + */ +struct sev_data_snp_launch_update { + u64 gctx_paddr; /* In */ + u32 page_size:1; /* In */ + u32 page_type:3; /* In */ + u32 imi_page:1; /* In */ + u32 rsvd:27; + u32 rsvd2; + u64 address; /* In */ + u32 rsvd3:8; + u32 vmpl3_perms:8; /* In */ + u32 vmpl2_perms:8; /* In */ + u32 vmpl1_perms:8; /* In */ + u32 rsvd4; +} __packed; + +/** + * struct sev_data_snp_launch_finish - SNP_LAUNCH_FINISH command params + * + * @gctx_addr: system pphysical address of guest context page + */ +struct sev_data_snp_launch_finish { + u64 gctx_paddr; + u64 id_block_paddr; + u64 id_auth_paddr; + u8 id_block_en:1; + u8 auth_key_en:1; + u64 rsvd:62; + u8 host_data[32]; +} __packed; + +/** + * struct sev_data_snp_guest_status - SNP_GUEST_STATUS command params + * + * @gctx_paddr: system physical address of guest context page + * @address: system physical address of guest status page + */ +struct sev_data_snp_guest_status { + u64 gctx_paddr; + u64 address; +} __packed; + +/** + * struct sev_data_snp_page_reclaim - SNP_PAGE_RECLAIM command params + * + * @paddr: system physical address of page to be claimed. The BIT0 indicate + * the page size. 0h indicates 4 kB and 1h indicates 2 MB page. + */ +struct sev_data_snp_page_reclaim { + u64 paddr; +} __packed; + +/** + * struct sev_data_snp_page_unsmash - SNP_PAGE_UNMASH command params + * + * @paddr: system physical address of page to be unmashed. The BIT0 indicate + * the page size. 0h indicates 4 kB and 1h indicates 2 MB page. + */ +struct sev_data_snp_page_unsmash { + u64 paddr; +} __packed; + +/** + * struct sev_data_dbg - DBG_ENCRYPT/DBG_DECRYPT command parameters + * + * @handle: handle of the VM to perform debug operation + * @src_addr: source address of data to operate on + * @dst_addr: destination address of data to operate on + * @len: len of data to operate on + */ +struct sev_data_snp_dbg { + u64 gctx_paddr; /* In */ + u64 src_addr; /* In */ + u64 dst_addr; /* In */ + u32 len; /* In */ +} __packed; + +/** + * struct sev_snp_guest_request - SNP_GUEST_REQUEST command params + * + * @gctx_paddr: system physical address of guest context page + * @req_paddr: system physical address of request page + * @res_paddr: system physical address of response page + */ +struct sev_data_snp_guest_request { + u64 gctx_paddr; /* In */ + u64 req_paddr; /* In */ + u64 res_paddr; /* In */ +} __packed; + #ifdef CONFIG_CRYPTO_DEV_SP_PSP /** diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index 91b4c63d5cbf..9bc63b026091 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -61,6 +61,11 @@ typedef enum { SEV_RET_INVALID_PARAM, SEV_RET_RESOURCE_LIMIT, SEV_RET_SECURE_DATA_INVALID, + SEV_RET_INVALID_PAGE_SIZE, + SEV_RET_INVALID_PAGE_STATE, + SEV_RET_INVALID_MDATA_ENTRY, + SEV_RET_INVALID_PAGE_OWNER, + SEV_RET_INVALID_PAGE_AEAD_OFLOW, SEV_RET_MAX, } sev_ret_code; @@ -147,6 +152,28 @@ struct sev_user_data_get_id2 { __u32 length; /* In/Out */ } __packed; +/** + * struct sev_data_snp_platform_status - Platform status + * + * @major: API major version + * @minor: API minor version + * @state: current platform state + * @build: firmware build id for the API version + * @guest_count: the number of guest currently managed by the firmware + * @tcb_version: current TCB version + */ +struct sev_user_snp_status { + __u8 api_major; /* Out */ + __u8 api_minor; /* Out */ + __u8 state; /* Out */ + __u8 rsvd; + __u32 build_id; /* Out */ + __u32 rsvd1; + __u32 guest_count; /* Out */ + __u64 tcb_version; /* Out */ + __u64 rsvd2; +} __packed; + /** * struct sev_issue_cmd - SEV ioctl parameters * From patchwork Wed Mar 24 17:04:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408318 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, UNWANTED_LANGUAGE_BODY, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 619BBC433F1 for ; Wed, 24 Mar 2021 17:05:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3A9EF61A14 for ; Wed, 24 Mar 2021 17:05:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237129AbhCXRF2 (ORCPT ); Wed, 24 Mar 2021 13:05:28 -0400 Received: from mail-bn8nam11on2064.outbound.protection.outlook.com ([40.107.236.64]:50400 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237021AbhCXRFB (ORCPT ); Wed, 24 Mar 2021 13:05:01 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HxoCSNWs7xHF/VQDpAg8rjazhzEr8w4kTXJyATM4HqVKyxRq5RRKjTbB0Tf2OOvz6yRoeduaCg+4qKNOxlKOaqgzruvdsc6sRyyaslj553zJzzI338kEySLhLWlBzlbCDEF+9sTx7Dnn2UASeeKS18FEw9aGiIlhzsD85T86+6H9tqXQbtwSPd2VF1W0J0tBwqnFuTFiNQqYd1DMEzFkwdd+/VCnyhhFKOML+34qBAgT+cItz0in/H62oBNKOIiW5r9R8p0CKMhdr6THwqItru7yjL6bV2vSJbHRKXcw5Ob0+Bis1j79FIVDpzsjcsiOb/1W2tP42wFzDONgwA7DXA== 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-SenderADCheck; bh=/EcTEZyk7jn3rjLoIyiG4fYKdTet59gdRvYdymkKwvc=; b=TS3IcVOxhbqoqaTCOnEQujxJ/Emu/b3o9RAnxssBE/pFypZaX4FB8X89YAkdPmjPr2LaWRXMqPyEIIbL3AGV8OtVRBaaVBGgCLKDlrKQ/6Y3e6UPlx664exHrn/pWWQQQfMRxEwoRtfBbbNFAaxm1j0vyYhCY7bCUL6fB8Umwl6/Y+kzzlIVFQr0jLAd1FXwFQxBs51+0tKyyJ8O3ltbnhOp14Say2gG+39IKOaBdBt7v2AYEqAabSF+zm0MlPeuM8kCjAN/ASIaK6FVzss2kXSIr/2FWRj8cFoqrmw3xnf2JFEJTsRWfQQTDT9O53FEWuR5RqAdR1hpU4il20fnZw== 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=/EcTEZyk7jn3rjLoIyiG4fYKdTet59gdRvYdymkKwvc=; b=R7Gp81rXLLjGRgQt0opF9uPoTGfPBT5BhegAWfA2yF1c7yGTFX3FNhaxAttve3OudBmwZ9SVQ3We8CnpNXrudrLPEj0PX5xsIos/0zYBcIr67SLE97K+UBpPjqNURFQ1ssNDIp0re4owXYegRAjMAlxKGLLuEj8YG3NVRL5Xmfc= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4557.namprd12.prod.outlook.com (2603:10b6:806:9d::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.24; Wed, 24 Mar 2021 17:04:58 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:04:58 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson Subject: [RFC Part2 PATCH 09/30] crypto: ccp: Add support to initialize the AMD-SP for SEV-SNP Date: Wed, 24 Mar 2021 12:04:15 -0500 Message-Id: <20210324170436.31843-10-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:04:57 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 0e7fda90-31a5-4bf1-e21e-08d8eee6f43d X-MS-TrafficTypeDiagnostic: SA0PR12MB4557: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Yl1yPvvYh+nxC+sW9QR2a1LofmL2DPSTjayBZabX2mJNg/jdnopsHzScbXDlhEcKLjSNOA7CmxpIx4HJAmpIjeYKAfW9RnrFB+VAwnqGVSjYX8APi1+eEXPOD2hUpVKqNPWGmCIb4lw727fZVSwptY4b2RVzChpEEBCpv7ueM8dxaBDLqt3HUGFLWgQMsaq1K9wHPebMURvfvWJmnmzP2wwWkDxzgVqeZYEc7cmoaE/B05hrWXx+klOeqbK3Ivq9O8iznjYgZisvZTVrwVLSLikrb7un8Im9MYYe8WnmxsOCTk7PAlmkAZytiVm8ocbzF483YWhtq7KjamFYxSRqkOaOK3GZTDpKIG2lLmy6t/Wg0DOP/cfJKOw8XPWdisl753TauQcsDQbjbAJ9D2eI4AJwesNFHv408B1G9quIDiDCTTLwFcZQ51/5r0gQ9Pkbj6j0/4D/SWP9eBc8okdGXh9KKO1Fp/b2sL54a0HB9oaCsulDZd+3r4EkaJdqCYAfEfFJpbFu6aVIX+1z0do3y8lfWDZkJNnx2/X7IniiANFXJTSo1kXB4b9O9kdeMrzvwMqNHUnvOrJwrj2nmnL6dutDT6lgcOFzUshjjkLEenAMXNN5WrSIDrP5abGS7re58rm9sVZbU1lIkkJWztbiZL4h2Yxt6kmgV/s2fRHQ33Nr3VjoBbVsI4B6MeLbcnz5 X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(376002)(396003)(346002)(136003)(39860400002)(86362001)(54906003)(186003)(44832011)(6666004)(4326008)(38100700001)(26005)(316002)(66476007)(8676002)(16526019)(1076003)(2616005)(83380400001)(5660300002)(6486002)(956004)(52116002)(8936002)(478600001)(7416002)(7696005)(66556008)(36756003)(66946007)(2906002)(134885004); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 7a5YSUo5oy4zenvCQE4V4uGb3HBQCL59aA1uGIVPH37lzBm+rpucfn0lYFhQFek8ak/HP2GdkdZoqwfJuL4yfLRcoDlBi1RKtXLNQeuWRUilWVSd2jo/gJrsnQz8dyvFy0MX0zAfHXzYm6dS5IW7h0WCdYtR5mFKkprH6ZfQlfCSQ0uzAKfH/lOnF+1zOLewZoyZ82WVO67x58JKJ/9GGlaY0ej7FuuykGKdC2DQaDQjmPdFhDQM5BsIFmWWJsZ6+SftuyqT9uyPDgfK3frhHDoNqhg1/fCNNCVWYO1qovOyhQBi+ge86j6WHYP1rr9UXj/XQmg2F8XFwueA0qgtx//T3zt5jdoZLvmy5WNi+Dt7BtEosVpesUkTz1v9WvG4nqu8gkJCOo0HRTcxwRDPWzi1i8NSMMPwWmpzGRqCfwH0HKZe68yLfyVqXO8wHK36ftqeKSi1i7mP4VeIRq/x59h61uoSYGevNbeNL1+koTydy/4ci5vpZJ/eFB+G2MD3r5jFkF1HqCVr+tRakadwgssBlCWoy7NZY61GLVKeoBhFMlYQ0bwGGWAfq3K5z0+AR9WLse54/gNiftJIPnwXvZv+nM85oRV+nLhvjwKpNNyF1FhF/g5zSPxNNx6RZBB6vlsk4dcl8WOxeq74Z1Qv2pS4VH7f32IAjmzetllK4mzpl3nlzYc/r1VtK+yvBcK1Qwxmaf2+n94HHMg5PAJk0KPQEpttTfVxxtYGYiUgQcGGhlLXQYDk9ZvkSKIJ8TIhCvfuZFkV7lqwaAKBGdAKBXgWkoT5Ik33tnzrWnwynIZtFXA+X49wbOeiJVrnQuZO+SqOxONk1vda1WVLKsqwKIGXSQkk+steanY6NsKwTT7pdKRBzFjKC5h+hInuiqHRWQhKCpzQQuJbtUJq8CSgXL3E78yOamyrFVpqxKGNNSh3YBm2GdLWDjGO8K89/s5ikvnXLis3zo7MdoF0iYoSEaG+DBCF88s8JumW5npkd2Sb5YLY+cfKridgxwcmqZBP/TpolDVTSHS7AaErNLTChYdOjqgbdXVV3HmzYhovwY1qgwGuPVMlK+ncDgGaU00ZDvDrVeN9m+/PwtE6jJDRCNVRugnDo6vAY447/UE/R9OkDGNj/Xo8rbS10Qc1iVIQlVOsAIRIbq1dkfpE3sGmAElxZ9cUW42nQywI8aQ2VIbJ9ZfWLGxvSwSWvbr/x3j4Itv/bbHuuAectFEt394l3FGcYToB7FjikzhQZ03Y0jZHgwZJNJegewZ0km8eqj3u/h6RG3GmsX4qlHal6JzwcNN0Q5+6OQTASdDD++lz2s5qV0CbtIL6x59wAP9TZDVe X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0e7fda90-31a5-4bf1-e21e-08d8eee6f43d X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:04:58.1919 (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: 2YMHuDVYEfRoGPSqBIVD9FiAY/8tI2/wrh+xPExM/Jmrixx5WtESiBgWEm8IkZnmdM/RtE8Rl018qotTGJ+7Gw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4557 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Before SNP VMs can be launched, the platform must be appropriately configured and initialized. Platform initialization is accomplished via the SNP_INIT command. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- drivers/crypto/ccp/sev-dev.c | 164 ++++++++++++++++++++++++++++++++++- drivers/crypto/ccp/sev-dev.h | 2 + include/linux/psp-sev.h | 16 ++++ 3 files changed, 179 insertions(+), 3 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 8a9fd843ad9e..c983a8b040c3 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -21,8 +21,10 @@ #include #include #include +#include #include +#include #include "psp-dev.h" #include "sev-dev.h" @@ -30,6 +32,7 @@ #define DEVICE_NAME "sev" #define SEV_FW_FILE "amd/sev.fw" #define SEV_FW_NAME_SIZE 64 +#define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT) static DEFINE_MUTEX(sev_cmd_mutex); static struct sev_misc_dev *misc_dev; @@ -574,6 +577,93 @@ static int sev_update_firmware(struct device *dev) return ret; } +static void snp_set_hsave_pa(void *arg) +{ + wrmsrl(MSR_VM_HSAVE_PA, 0); +} + +static int __sev_snp_init_locked(int *error) +{ + struct psp_device *psp = psp_master; + struct sev_device *sev; + int rc = 0; + + if (!psp || !psp->sev_data) + return -ENODEV; + + sev = psp->sev_data; + + if (sev->snp_inited && sev->state >= SEV_STATE_INIT) + return 0; + + if (!snp_key_active()) { + dev_notice(sev->dev, "SNP is not enabled\n"); + return -ENODEV; + } + + /* SNP_INIT requires the MSR_VM_HSAVE_PA must be set to 0h across all cores. */ + on_each_cpu(snp_set_hsave_pa, NULL, 1); + + /* Prepare for first SEV guest launch after INIT */ + wbinvd_on_all_cpus(); + + /* Issue the SNP_INIT firmware command. */ + rc = __sev_do_cmd_locked(SEV_CMD_SNP_INIT, NULL, error); + if (rc) + return rc; + + sev->snp_inited = true; + sev->state = SEV_STATE_INIT; + dev_dbg(sev->dev, "SEV-SNP firmware initialized\n"); + + return rc; +} + +int sev_snp_init(int *error) +{ + int rc; + + mutex_lock(&sev_cmd_mutex); + rc = __sev_snp_init_locked(error); + mutex_unlock(&sev_cmd_mutex); + + return rc; +} +EXPORT_SYMBOL_GPL(sev_snp_init); + +static int __sev_snp_shutdown_locked(int *error) +{ + struct sev_device *sev = psp_master->sev_data; + int ret; + + ret = __sev_do_cmd_locked(SEV_CMD_SNP_SHUTDOWN, NULL, error); + if (ret) + return ret; + + wbinvd_on_all_cpus(); + + ret = __sev_do_cmd_locked(SEV_CMD_SNP_DF_FLUSH, NULL, error); + if (ret) + dev_err(sev->dev, "SEV-SNP firmware DF_FLUSH failed\n"); + + sev->snp_inited = false; + sev->state = SEV_STATE_UNINIT; + dev_dbg(sev->dev, "SEV-SNP firmware shutdown\n"); + + return ret; +} + +static int sev_snp_shutdown(int *error) +{ + int rc; + + mutex_lock(&sev_cmd_mutex); + rc = __sev_snp_shutdown_locked(NULL); + mutex_unlock(&sev_cmd_mutex); + + return rc; +} + static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable) { struct sev_device *sev = psp_master->sev_data; @@ -1043,6 +1133,42 @@ int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, } EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user); +static int sev_snp_firmware_state(void) +{ + struct sev_data_snp_platform_status_buf *buf = NULL; + struct page *status_page = NULL; + int state = SEV_STATE_UNINIT; + int rc, error; + + status_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); + if (!status_page) + return -ENOMEM; + + buf = kzalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT); + if (!buf) { + __free_page(status_page); + return -ENOMEM; + } + + buf->status_paddr = __sme_page_pa(status_page); + rc = sev_do_cmd(SEV_CMD_SNP_PLATFORM_STATUS, buf, &error); + + /* + * The status buffer is allocated as a hypervisor page. As per the SEV spec, + * if the firmware is in INIT state then status buffer must be either a + * the firmware page or the default page. Since our status buffer is in + * the hypervisor page, so, if firmware is in INIT state then we should + * fail with INVALID_PAGE_STATE. + */ + if (rc && error == SEV_RET_INVALID_PAGE_STATE) + state = SEV_STATE_INIT; + + kfree(buf); + __free_page(status_page); + + return state; +} + void sev_pci_init(void) { struct sev_device *sev = psp_master->sev_data; @@ -1052,8 +1178,20 @@ void sev_pci_init(void) if (!sev) return; + /* Check if the SEV feature is enabled */ + if (!boot_cpu_has(X86_FEATURE_SEV)) { + dev_err(sev->dev, "SEV: Memory Encryption is disabled by the BIOS\n"); + return; + } + psp_timeout = psp_probe_timeout; + /* check if SNP firmware is in INIT state, If so shutdown */ + if (boot_cpu_has(X86_FEATURE_SEV_SNP)) { + if (sev_snp_firmware_state() == SEV_STATE_INIT) + sev_snp_shutdown(NULL); + } + if (sev_get_api_version()) goto err; @@ -1086,6 +1224,21 @@ void sev_pci_init(void) "SEV: TMR allocation failed, SEV-ES support unavailable\n"); } + /* + * If boot CPU supports the SNP, then let first attempt to initialize + * the SNP firmware. + */ + if (boot_cpu_has(X86_FEATURE_SEV_SNP)) { + rc = sev_snp_init(&error); + if (rc) { + /* + * If we failed to INIT SNP then don't abort the probe. + * Continue to initialize the legacy SEV firmware. + */ + dev_err(sev->dev, "SEV-SNP: failed to INIT error %#x\n", error); + } + } + /* Initialize the platform */ rc = sev_platform_init(&error); if (rc && (error == SEV_RET_SECURE_DATA_INVALID)) { @@ -1105,8 +1258,8 @@ void sev_pci_init(void) return; } - dev_info(sev->dev, "SEV API:%d.%d build:%d\n", sev->api_major, - sev->api_minor, sev->build); + dev_info(sev->dev, "SEV%s API:%d.%d build:%d\n", sev->snp_inited ? + "-SNP" : "", sev->api_major, sev->api_minor, sev->build); return; @@ -1119,7 +1272,12 @@ void sev_pci_exit(void) if (!psp_master->sev_data) return; - sev_platform_shutdown(NULL); + if (boot_cpu_has(X86_FEATURE_SEV)) + sev_platform_shutdown(NULL); + + if (boot_cpu_has(X86_FEATURE_SEV_SNP)) + sev_snp_shutdown(NULL); + if (sev_es_tmr) { /* The TMR area was encrypted, flush it from the cache */ diff --git a/drivers/crypto/ccp/sev-dev.h b/drivers/crypto/ccp/sev-dev.h index dd5c4fe82914..18b116a817ff 100644 --- a/drivers/crypto/ccp/sev-dev.h +++ b/drivers/crypto/ccp/sev-dev.h @@ -52,6 +52,8 @@ struct sev_device { u8 api_major; u8 api_minor; u8 build; + + bool snp_inited; }; int sev_dev_init(struct psp_device *psp); diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index df89f0207099..ec45c18c3b0a 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -709,6 +709,20 @@ struct sev_data_snp_guest_request { */ int sev_platform_init(int *error); +/** + * sev_snp_init - perform SEV SNP_INIT command + * + * @error: SEV command return code + * + * Returns: + * 0 if the SEV successfully processed the command + * -%ENODEV if the SEV device is not available + * -%ENOTSUPP if the SEV does not support SEV + * -%ETIMEDOUT if the SEV command timed out + * -%EIO if the SEV returned a non-zero return code + */ +int sev_snp_init(int *error); + /** * sev_platform_status - perform SEV PLATFORM_STATUS command * @@ -816,6 +830,8 @@ sev_platform_status(struct sev_user_data_status *status, int *error) { return -E static inline int sev_platform_init(int *error) { return -ENODEV; } +static inline int sev_snp_init(int *error) { return -ENODEV; } + static inline int sev_guest_deactivate(struct sev_data_deactivate *data, int *error) { return -ENODEV; } From patchwork Wed Mar 24 17:04:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408963 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 52D08C433DB for ; Wed, 24 Mar 2021 17:05:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DC69F61A13 for ; Wed, 24 Mar 2021 17:05:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236969AbhCXRF1 (ORCPT ); Wed, 24 Mar 2021 13:05:27 -0400 Received: from mail-bn8nam11on2064.outbound.protection.outlook.com ([40.107.236.64]:50400 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237044AbhCXRFC (ORCPT ); Wed, 24 Mar 2021 13:05:02 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DNIhQBXwc/LEtIdXq4nk+PWarLhmzVRua/Nd0Y0YglOBezr6rzHmAYDDBH/7h+w/lfPZbVyGcqOewzKMY61s9ePexTsq5YfoUW8+stP1gUsqfKLX5hln4w3l1R1LR/rQfsBxyz86f5zAcN+md/9cuAc7OBkE8nNjPZFwZI9daF0dYTaG1tAZ98InYOkO79iAJkfD1F4G8LX8+w/saX8HDSooBLcS12BhrgP01d1DHj/0uE3J/H+pFQ6bYzQtuzyGIeiHQoaaHf6hNjn+5CPmz9US/GaUrws8tHOjXmc1u8Ol656aW16cre9CNyatyOXcCnDUCFNceCey7LPe0H0koQ== 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-SenderADCheck; bh=RL4so6K4Duxd2cYW0iNJTrQh2PfwP1WZNs7lcYb3RT8=; b=oBH/mQ68zpgcTm7yV/ax2mUlqv+rnQGCV/EPXQJZButngoxKv0+lboUyRyxqWRMqFq99nRT+2X3FlLrK5jA3nQjmu1dX7I0bCFrUCXwnhU4g++5l6u7+TPKvhruJYNrHWIHGyiiUHTCbgI+b2iKi5AkaZq6F9/ZMKoHAiw69q9ZVT+0ELsrfbowMlb5BssDQpAsGcpHHwj5+K7xw8U7NSnj+nr71e1cz5Es4va0ayRQKpalTvnYTwEn6DIyB/SHHXySKK1OAQX6QPldUFkEHz/a+JcFN/EMTjeR61PrbPuLvAhm0YAUjhFx2wnaG9D2yyRpiALo35eI3gE/4gRIx1w== 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=RL4so6K4Duxd2cYW0iNJTrQh2PfwP1WZNs7lcYb3RT8=; b=24iWt0KpoIxSW4Xv2zeG8tTdHHJv/Z+jaFrirQ4Hsl6gOLLW4FA5x23QyO8RmDmCuEY63sypb55AFmjyMIQTpQMURupkx1UAlI0KTes+uNkEPekBNfegp3apEpG1jRSCSw5jhOnaTBhXgKFLfWMztxp1Dprgb+8tS6ceoYM0fzo= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4557.namprd12.prod.outlook.com (2603:10b6:806:9d::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.24; Wed, 24 Mar 2021 17:04:59 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:04:59 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson Subject: [RFC Part2 PATCH 10/30] crypto: ccp: shutdown SNP firmware on kexec Date: Wed, 24 Mar 2021 12:04:16 -0500 Message-Id: <20210324170436.31843-11-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:04:58 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 55fabc58-df1c-4645-d5fe-08d8eee6f4ab X-MS-TrafficTypeDiagnostic: SA0PR12MB4557: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:901; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xoJBj3miF+ihG2pdO1xgYAHuETwOQG9fNQgjpSQq6M/dpDKnf29fPMsUDZdXQcT5UgHo2hHcPGnIHq/exGyXxDbRO4l4cnMAGonyJVYDKC5jLTkubPuyR3BagYnjnT7CBlk3I7y6ELXft8v4TQWJHaJPV+WWE1oQcyESyUUFK8ST2JchdDgcRrlofyit4GPCA/VWnt4cQg++E4ujLpNA4CpVEFlDWSMAlWAfN1LyuHBSDlmzHPpi3ZF0YIh1Bht09xPo5uEvdQklUWER4Bz8KgMLjCEV5rkHIwwVCsKp9WDLiuVObjU7mRoq6jXOhnnnCaX5RPBiiLpi/ybT5w7Fuf0B7x0pjALxLMPd1UAl+VLDIZQMUWqK0OsoCrtRFFpte7Nx2ha31eONyzRuW3dqtTwRiEnF2H7oIb+s49xL0fPFvpYS1tAV3evOQnRqZzTukZdIhfqaMwSCLO1fWbqMHbaXiMRQsv82gmzxsV0IatHJV67MTwZ2kTadn6gcSUltYz0vrwEH3PmYHmydxUaTGCKvC/r0j0MNDUes4IqJW8xrsIf0+4+Hls4OacwAHTIvBrU+zpFTrcvaE09MU03AwcQCmu9nry2vnclapMC+CKq8U2VI+QO+CAiv4xHh5UU1/fP+H01qdqAlKXTUdokOnw== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(376002)(396003)(346002)(136003)(39860400002)(86362001)(54906003)(186003)(44832011)(6666004)(4326008)(38100700001)(26005)(316002)(66476007)(8676002)(16526019)(1076003)(2616005)(83380400001)(5660300002)(6486002)(956004)(52116002)(8936002)(478600001)(7416002)(7696005)(66556008)(36756003)(66946007)(2906002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 6PlCbVDSDrML/30+N+qfznLtLvpVzmidf3bprXH/QRl9icbrjr0qA0okv2f4RLOJMo1cduh26GYcXLNaAQOA3R/lH/oZBj0XUQuLFK/ZTv7E1NXFqATA7gXRp7UxvkZvsDLd7hqxnVuOA/5e/tUYhbRY8uvOXDTip+yN2ZLB7UdTVy/IrVu3JdS8cDQqdqU0+uEgCaugiCg0Jrk8hqzor5p+BDUr98RJKEPnZAkiURgVbKCyuS/tPURAxKDfeadfun7J2TKiG9UrxZyY+vCujEjXoWmLNx/6xTzxJvyNn6aSCH0Awon+iSn3/Z7uSM3jiHN4GrGtJbCkdqMQN827aitIMgOdmAK59R1F1FaCle0ooQc/Tf4sBe0d+91/92aFz+vtu99mY86ovYvJVdb/BkAa/6zTGCBc1NchcY8HUiImnEvrEIhgXWoFH46cy6aApboVeujfJzypYDNhfOamuTjB7U1gIkTdwPDk41iYEFOMUDNvVd5Q5Fw9ARvOoBBg3f8+aqH44LJBN3m7oX5hTF4alO+OPf+8q2we/qQHY71rv3qAdgHnYp8w7m+yzgRm6ac1wozdRpX7XaOlvrcst89c7izhQ0WuaWHu/8hwJD7us000g1OIJQ8RSvOHlteOcvTofdCvuvT1FhuoXUrnzogVLrgL8LefFof8Cy4KM1YLVj1FC48eG1kovTOzyuIIJbjS4TlDFpRs8AMV+pNWglJGDK7KFkNdDD7a45y2PCmUngUMWk0cunLIUkW5Q38s4WgP2Fy0qGDktmOeAwRrvjMX9jRyLLJDqQ849r27BBcz7GQQTUdY2XBBoe1CpukwfQYxym+gg/j3eG+KbU4Yhc29UFE3kHFMPbdquid+HSzjPjv+pgblg2ZR7/7CugHxMmZ/yijToDzJiZSgRNjRWJSWM56GtuApKYNHXKukdFhgqTBmSarBOj/0Zp72aHmnNsLp6hA3yF4B45biFM5SUSUFYA3flNRHaBROtHp7qFQtRUuQHXLieIMgvtt2izzhBT0KEHNuP3JHhQ1kZY0oa9vhLKbWk5GXJmA6TZCBJ2POg+4Vy1FK2EFT0HKlg5mqZAqeae210NLAxsRbYmwun+se0obu9Pv+c3RM33i57Wf6vTs3nVOcIKgBR9RKZ3DQqSOeBuxgOm9t9o5zsLbQMhT93hJJh7rKLpOr96HdWr4F3vF7M2svb0/qdmzQSNt9BUi3AwRbRbvXTYyxVtvt7Hcbf2MhktLB6cFjBeKB/7g6P2bC8lwzJTSlENVy9s37EtDDa56TQqL1fngSf7BviUk5gMeXXZtBgLrUveKTuzjbQKDd3gi7+6WJsIhwVEic X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 55fabc58-df1c-4645-d5fe-08d8eee6f4ab X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:04:58.9295 (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: xzqv2Pfmfj0mDFHneyu9HVHu3xurrUcMTsIXDaP3xtL7O5HelfsJJ6HwQXYDQUiw9JQhJ8MLi1RRNVncspwr5w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4557 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org When the kernel is getting ready to kexec, it calls the device_shutdown() to allow drivers to cleanup before the kexec. If SEV firmware is initialized then shut it down before kexec'ing the new kernel. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- drivers/crypto/ccp/sev-dev.c | 18 ++++++++++++------ drivers/crypto/ccp/sp-pci.c | 12 ++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index c983a8b040c3..562501c43d8f 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -1110,6 +1110,15 @@ int sev_dev_init(struct psp_device *psp) return ret; } +static void sev_firmware_shutdown(void) +{ + if (boot_cpu_has(X86_FEATURE_SEV)) + sev_platform_shutdown(NULL); + + if (boot_cpu_has(X86_FEATURE_SEV_SNP)) + sev_snp_shutdown(NULL); +} + void sev_dev_destroy(struct psp_device *psp) { struct sev_device *sev = psp->sev_data; @@ -1117,6 +1126,8 @@ void sev_dev_destroy(struct psp_device *psp) if (!sev) return; + sev_firmware_shutdown(); + if (sev->misc) kref_put(&misc_dev->refcount, sev_exit); @@ -1272,12 +1283,7 @@ void sev_pci_exit(void) if (!psp_master->sev_data) return; - if (boot_cpu_has(X86_FEATURE_SEV)) - sev_platform_shutdown(NULL); - - if (boot_cpu_has(X86_FEATURE_SEV_SNP)) - sev_snp_shutdown(NULL); - + sev_firmware_shutdown(); if (sev_es_tmr) { /* The TMR area was encrypted, flush it from the cache */ diff --git a/drivers/crypto/ccp/sp-pci.c b/drivers/crypto/ccp/sp-pci.c index f471dbaef1fb..9210bfda91a2 100644 --- a/drivers/crypto/ccp/sp-pci.c +++ b/drivers/crypto/ccp/sp-pci.c @@ -239,6 +239,17 @@ static int sp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) return ret; } +static void sp_pci_shutdown(struct pci_dev *pdev) +{ + struct device *dev = &pdev->dev; + struct sp_device *sp = dev_get_drvdata(dev); + + if (!sp) + return; + + sp_destroy(sp); +} + static void sp_pci_remove(struct pci_dev *pdev) { struct device *dev = &pdev->dev; @@ -368,6 +379,7 @@ static struct pci_driver sp_pci_driver = { .id_table = sp_pci_table, .probe = sp_pci_probe, .remove = sp_pci_remove, + .shutdown = sp_pci_shutdown, .driver.pm = &sp_pci_pm_ops, }; From patchwork Wed Mar 24 17:04:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408958 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E620FC433FB for ; Wed, 24 Mar 2021 17:05:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A082F61A13 for ; Wed, 24 Mar 2021 17:05:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237025AbhCXRF2 (ORCPT ); Wed, 24 Mar 2021 13:05:28 -0400 Received: from mail-bn8nam11on2064.outbound.protection.outlook.com ([40.107.236.64]:50400 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237047AbhCXRFD (ORCPT ); Wed, 24 Mar 2021 13:05:03 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Puz3y+wInmHEtv9Uz0MAPPgPLwlIutZRaFbNDJOCTAMm6r4njNYXvxKOSAPUu0XoLCfK/LLabNqWQyYQkdZOBquEzGZpHpNu2W8jKcknWmtbvUDcoqS7f4Lb5NgHCmJEwFGWqfz98acjkGpmvHHxj6hkkMLZnkjdFZLBhdYokWCo0I1K+PlLFn+JU5fqsXphb+uZ6XVl9ur1NX3uKYuZNgzSTW620fGYfnpV5wLpdy9xgy5eZMd+OPUhL/MFS1GwwzJlGLnhHpkNbPebzmobj4I/7Ij6m+bEDsuo81Q9lRp8K4H4RpUYJYK1hr7wYRwq5Y0OG01bz63v45E0mDKOpQ== 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-SenderADCheck; bh=Ij9VyltMae1y0jaZt3/gH2pMt/vbKrK9SCRaYzkZA8c=; b=BF7zWHSxrUBpgGuKahSPBQs4/QIAM7zKNymf0GeluS06hCKPkJ7EqQhfgEulMWY/L/BiuZ2AGU1IWJXd9g2cNj6WllAahNsJJUf0A1UHfJ1IxeNH+Rt4nvGA5MWr7GmUrukRC4fsNRD6YK2jVdaFp/CtCiuV88q/zxkT9zosCNzz47QMf9agYQgJUI22zNgyfwsNvkzj5FUHmkQfa+JE8vcP3VxIGq//j89//RPnmgCxDjHIF9TLGKLbfgRDT00qC4ge670K7DgoiRivL3lmlV4/BNfXXQEECLEeqzAfIMemgFzpg9/jhfNQ8lDpTKFrOKPKhUp4Z+YwTiiattqvDQ== 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=Ij9VyltMae1y0jaZt3/gH2pMt/vbKrK9SCRaYzkZA8c=; b=kxCrki7jORxZgYHY1xI0SP5ZfG7ziOMigAFRALGPEWDCnCW9+LV10wSeEyA0RnSgvUImrJpY938Id7eMcXS6kkPmnkAmRVW8WwI+4OkMsetVQZ895RwMA4xLLuAy3axkdc9G1syMovJ9CIqtUFptEpntqGF5AQASV413mJWLdnk= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4557.namprd12.prod.outlook.com (2603:10b6:806:9d::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.24; Wed, 24 Mar 2021 17:04:59 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:04:59 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson Subject: [RFC Part2 PATCH 11/30] crypto:ccp: provide APIs to issue SEV-SNP commands Date: Wed, 24 Mar 2021 12:04:17 -0500 Message-Id: <20210324170436.31843-12-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:04:59 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: ca4559e1-dbfe-4aa5-69e0-08d8eee6f521 X-MS-TrafficTypeDiagnostic: SA0PR12MB4557: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6790; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qQHdyxRr/NnZ1LQHDC17POkX7KGJLBTL0eL2+VzOj8ndYxs6/ngoO19qENqp9fYEY1PyrIiivQS/fjlSeZmOxmw2mPPPOpFmuQgi3yl5lJm/f2I2jgviK1hG1xPgDIwY2Z/FWDECk6Z5EL44UIAKe9tS/sWK3zG1/n2BgyT5lsYsm7Yfihzsj8w68QHXum1HVTniJekYvFOcBt9f+WN1R9vyPOEbx4qh+JzoyVXTjPRpid0de939IohztIss7/qCG+ci7SPn4eZBk4V22X0SXFGrU9iAP+zDuu12mR4lScEh5s91b52IoEEfvLIwBkV0h31x/5PIVQ0JwyDzFBcOs8/LgZnVBlU3/gydIhk1okmRetQvl+dmRagVYo16yc3xt1rRAIDj1+g/yUdhA1L+kdt1avI3URjwc9Xg+ZTq/g0NTDzb63XSraItDBkdaQJrCP/amFVkqL13TRzbQurZEvxZ3H3DUb1RMlmxXArwAODvaGDP0I/QR9ruI75iHLlWsgbqug/Y0QP83zB+7xSCd9zOVCwt52UPJH+pw6cws68wvAw1Eicb9xdKVW/iub28B7oyPDS5aU1dJhmyRn/gXaHRLFU+AV3hwpRq9If+9/7KKbk4f7fm6h8KEQnV77uqAfU5Xi03WT4MHU9tXDncO2Mk1geBmiTBnUxsSWe2sVnrqlz/H1/njCSDMCC5tqd/ X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(376002)(396003)(346002)(136003)(39860400002)(86362001)(54906003)(186003)(44832011)(6666004)(4326008)(38100700001)(26005)(316002)(66476007)(8676002)(16526019)(1076003)(2616005)(83380400001)(5660300002)(6486002)(956004)(52116002)(8936002)(478600001)(7416002)(7696005)(66556008)(36756003)(66946007)(2906002)(134885004); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: CPy6CfXGh4P+xoGN/AL4Mbmuksgr27jhuWG1HgIdXLs/o9E22rVdQvRgGtGjAATDotALcj/elKhaxD4fXwdErYtHLLbdnc69bPSKPANWyLhTepml7eck3Frhxnk2v0otl/Hpe1oGUrlVZbcWZDOaVVqBmWqaiJWEfbYNogMzGNrG5mi+qMCYVdOK95FIocFG5IE0uqepc5OWw+2Js61q+9w0qsEkMmT2NYpNrmxXbD2WgDHAtVbW/NGkfJM1iDwytRreOQk51jtzoTHXguCQHdtm8lOCVy0woInhxOXvLZ8kFSpnZBw/Yr4f6kH3ls4E5RSsCusVE7ZSEgDHOLXw2Qmi4rt8VoExsawapAN9W5SOhZUNel67TXTqR4pg0RiSfNva+bEjizGAopmcSVtCUlDPFHYm/j2DjtvMgwA96zqr9MxY1C1zm+pnNkwxi1K5trpXprJmDXwAdu4EikqCPUoPhX7poPVOaKtvgq0jGlRHua9i/eHav7ugzawCHpen0/qppE224JTkdAsnAQMVOpy8e+tTsBYiFxEuhBTL4Q2oyR7yamxUdnujE44pLFhbWXu7FekK81Fl5JszsyBCnLrT3Ta6oNEtjN1ZJDwQOJOEzQoD1Z0dd15CUoFboVTE7usE3mZdDQtfG7JcZP4w2yNcH+7BuNmU7y6ZVBKc2ak7aGeKQC/wwYxDeijth6SlsgkENsQzMTay+mWjDl+L2lKfFn1WQ8PMSTNXZRoS3Xk9XYsdtmlaOCqToozqyinrkOz1iyOLZ6oV3XsWZ+s8IPPVcQNhapTqPwENk6Ryr73IP3mEr9B68pUVlpLW1lYvPrXmaiEDnnxb8WsS4cJRDdVxvR6l8V7SBnbMtXkfteVUbFuYc/bndvM1TAvydDtBaV1TFqPIfd9DFiofY3D4XmsEktHBi1Jw0Q5tHfrGidojwdFh/UXKYeSYbgxRlpmk8kq1oyWreXsXTCqqGsny/BZ9fKRUQRjGpd6S+xHg9QID/35dA5J0FEOixzZE3ZclgND0vgjgP+FYApNNip1Iqt8UPhG5Zk31kHr0/VZgyeUCpqJFu6Hn+YPJ4GoTdNKyPUoL0lQyIOdA7jto2ZLzsyZMCddeDeF7WDk0O/SxZpJKeUwZjxua6abNcp5WYBWWeqG1EQEk9VTA/JQctTj2OJy/9PJK29NwvCK0qATwAcldjkR+gOYB0/qC3Dkb35VyxAt76ur77MuUAhXgihm3xaw+m0kqanFr7mTXx2LeV1VDsy1lGC+z2iAbJy7C/5iT/0NpRcO+WFGiPjQxK0Z34/9Zp/ew2M/6nmflUp96rMopf/Wn9iKP8sAEU1Kijbpq X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: ca4559e1-dbfe-4aa5-69e0-08d8eee6f521 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:04:59.6940 (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: p8wG38vvFpqzJSa4gWfuwfdzpoSKAX6/6QMYlK+eaXPXrIzhGEt6DuluXjt0bd8MBAWBb1vZLq/YtxwJ3NA8nQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4557 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Provide the APIs for the hypervisor to manage an SEV-SNP guest. The commands for SEV-SNP is defined in the SEV-SNP firmware specification. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- drivers/crypto/ccp/sev-dev.c | 41 +++++++++++++++++ include/linux/psp-sev.h | 85 ++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 562501c43d8f..242c4775eb56 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -1019,6 +1019,47 @@ int sev_guest_df_flush(int *error) } EXPORT_SYMBOL_GPL(sev_guest_df_flush); +int sev_guest_snp_decommission(struct sev_data_snp_decommission *data, int *error) +{ + return sev_do_cmd(SEV_CMD_SNP_DECOMMISSION, data, error); +} +EXPORT_SYMBOL_GPL(sev_guest_snp_decommission); + +int sev_guest_snp_df_flush(int *error) +{ + return sev_do_cmd(SEV_CMD_SNP_DF_FLUSH, NULL, error); +} +EXPORT_SYMBOL_GPL(sev_guest_snp_df_flush); + +int sev_snp_reclaim(struct sev_data_snp_page_reclaim *data, int *error) +{ + return sev_do_cmd(SEV_CMD_SNP_PAGE_RECLAIM, data, error); +} +EXPORT_SYMBOL_GPL(sev_snp_reclaim); + +int sev_snp_unsmash(unsigned long paddr, int *error) +{ + struct sev_data_snp_page_unsmash *data; + int rc; + + data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); + if (!data) + return -ENOMEM; + + data->paddr = paddr; + rc = sev_do_cmd(SEV_CMD_SNP_PAGE_UNSMASH, data, error); + + kfree(data); + return rc; +} +EXPORT_SYMBOL_GPL(sev_snp_unsmash); + +int sev_guest_snp_dbg_decrypt(struct sev_data_snp_dbg *data, int *error) +{ + return sev_do_cmd(SEV_CMD_SNP_DBG_DECRYPT, data, error); +} +EXPORT_SYMBOL_GPL(sev_guest_snp_dbg_decrypt); + static void sev_exit(struct kref *ref) { misc_deregister(&misc_dev->misc); diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index ec45c18c3b0a..32532df37446 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -821,6 +821,80 @@ int sev_guest_df_flush(int *error); */ int sev_guest_decommission(struct sev_data_decommission *data, int *error); +/** + * sev_guest_df_flush - perform SNP DF_FLUSH command + * + * @sev_ret: sev command return code + * + * Returns: + * 0 if the sev successfully processed the command + * -%ENODEV if the sev device is not available + * -%ENOTSUPP if the sev does not support SEV + * -%ETIMEDOUT if the sev command timed out + * -%EIO if the sev returned a non-zero return code + */ +int sev_guest_snp_df_flush(int *error); + +/** + * sev_guest_snp_decommission - perform SNP_DECOMMISSION command + * + * @decommission: sev_data_decommission structure to be processed + * @sev_ret: sev command return code + * + * Returns: + * 0 if the sev successfully processed the command + * -%ENODEV if the sev device is not available + * -%ENOTSUPP if the sev does not support SEV + * -%ETIMEDOUT if the sev command timed out + * -%EIO if the sev returned a non-zero return code + */ +int sev_guest_snp_decommission(struct sev_data_snp_decommission *data, int *error); + +/** + * sev_snp_reclaim - perform SNP_PAGE_RECLAIM command + * + * @decommission: sev_snp_page_reclaim structure to be processed + * @sev_ret: sev command return code + * + * Returns: + * 0 if the sev successfully processed the command + * -%ENODEV if the sev device is not available + * -%ENOTSUPP if the sev does not support SEV + * -%ETIMEDOUT if the sev command timed out + * -%EIO if the sev returned a non-zero return code + */ +int sev_snp_reclaim(struct sev_data_snp_page_reclaim *data, int *error); + +/** + * sev_snp_reclaim - perform SNP_PAGE_UNSMASH command + * + * @decommission: sev_snp_page_unmash structure to be processed + * @sev_ret: sev command return code + * + * Returns: + * 0 if the sev successfully processed the command + * -%ENODEV if the sev device is not available + * -%ENOTSUPP if the sev does not support SEV + * -%ETIMEDOUT if the sev command timed out + * -%EIO if the sev returned a non-zero return code + */ +int sev_snp_unsmash(unsigned long paddr, int *error); + +/** + * sev_guest_snp_dbg_decrypt - perform SEV SNP_DBG_DECRYPT command + * + * @sev_ret: sev command return code + * + * Returns: + * 0 if the sev successfully processed the command + * -%ENODEV if the sev device is not available + * -%ENOTSUPP if the sev does not support SEV + * -%ETIMEDOUT if the sev command timed out + * -%EIO if the sev returned a non-zero return code + */ +int sev_guest_snp_dbg_decrypt(struct sev_data_snp_dbg *data, int *error); + + void *psp_copy_user_blob(u64 uaddr, u32 len); #else /* !CONFIG_CRYPTO_DEV_SP_PSP */ @@ -848,6 +922,17 @@ sev_issue_cmd_external_user(struct file *filep, unsigned int id, void *data, int static inline void *psp_copy_user_blob(u64 __user uaddr, u32 len) { return ERR_PTR(-EINVAL); } +static inline int +sev_guest_snp_decommission(struct sev_data_snp_decommission *data, int *error) { return -ENODEV; } + +static inline int sev_guest_snp_df_flush(int *error) { return -ENODEV; } + +static inline int sev_snp_reclaim(struct sev_data_snp_page_reclaim *data, int *error) { return -ENODEV; } + +static inline int sev_snp_unsmash(unsigned long paddr, int *error) { return -ENODEV; } + +static inline int sev_guest_snp_dbg_decrypt(struct sev_data_snp_dbg *data, int *error) { return -ENODEV; } + #endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */ From patchwork Wed Mar 24 17:04:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408317 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5CB06C433FC for ; Wed, 24 Mar 2021 17:05:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 159D461A16 for ; Wed, 24 Mar 2021 17:05:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237141AbhCXRF3 (ORCPT ); Wed, 24 Mar 2021 13:05:29 -0400 Received: from mail-bn8nam11on2064.outbound.protection.outlook.com ([40.107.236.64]:50400 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237048AbhCXRFE (ORCPT ); Wed, 24 Mar 2021 13:05:04 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=n4xe+Dc7SJ189w66rF26UMtNRjAGOLH+ilGNzPKINKKBKagQHPlEusJBOaZcLLlT/l6xFWql+DcAwLzs14zpsRLBoshEyxdc7Jv6/xUYWjHK3+A33SAu36OGL9e2x5BKj9YR2r95ADXPbeaKvbdQPY2nQ4NU1bp4jwzT5uS8HZUlJRXOv9Z9EE6GufzuaV2lo0ukYb++dYJzp7K8orp9s8eUNG9evRtEa30wwA/3IkZVjMSPyn/3Cd3qwQCZ7YS2IWJ9eLbzNEFhmn21PltphRsgz4OaaNuKJNryMj7bWeUlC7QZyXM71R4j/O7l7HbcO8PouDABWBRNU1T8CVOfOw== 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-SenderADCheck; bh=USWAPSDrkPixl8ICG9MoZEp/4U9M7ze9vOMJuwexQAk=; b=QYfnxJNLX4pZRArzkj8luTONtfNNdUbFbqQqTZFjh7PlJnQAhrZsynVKxxbWHhi1KeGd8MeBtRnGzfcJ2r4E4/sZ4VOquWVwctBzeO4S2aXCzqk3/mcFgmZ+D9olZSKvPkmwhnWZ6UspfncJa7iOdrt5VzzuE+A3JoAvCpWvJtWV8Dr2Y+FGeqaYaj2QA5fyPCr4N+vyU+2j7hgjxSgg64l6SZLLcjnHqM0NhWbXL3MXO2JGgtRdsehgMsSfL84Olc+OPxvExPnI5jzhGM5vMouWHjQIm3ud2aBf2CbcPEXl57VvmhP8DgO+IiWPcf55FrWMfi8Rr0Kzr5j5d+Ft3A== 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=USWAPSDrkPixl8ICG9MoZEp/4U9M7ze9vOMJuwexQAk=; b=T4srTx+VBFok1WZsDzEsQsl8mLYxUhawPHJ2rgLRMmknn2d5nZMkv+p7/sfiQi2UVQ+/SJKbm6z4q4j0fEA6FyMul/UwN2jbTn2Q5iJ4ArhoaysAVylkT70othuxoDdcZ9JqhuEUitzrHei7V2g+12ADjL0B3CQcBDGTS5OoVdY= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4557.namprd12.prod.outlook.com (2603:10b6:806:9d::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.24; Wed, 24 Mar 2021 17:05:00 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:05:00 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson Subject: [RFC Part2 PATCH 12/30] crypto ccp: handle the legacy SEV command when SNP is enabled Date: Wed, 24 Mar 2021 12:04:18 -0500 Message-Id: <20210324170436.31843-13-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:04:59 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 769d9387-8c4a-4d26-3a8c-08d8eee6f596 X-MS-TrafficTypeDiagnostic: SA0PR12MB4557: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:1850; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: R0qR1apenws3tPrJDfOih5Y2NOUQUV5jRerbZhdPAEJ8BfnCjNZ2bwLhBdCG+ldzjqzCYh6X4+TxQYGBsw9o0HZsN7n8SM67O6flyePiUzSYHj1L4Ue/rcDMWbchNoMzhkpGs5r2SS8YU9/HQgGhcKMzW7RHrZMDXcx9610w+s6ixlM12cnsIm0Momv51Xc8ntcl4/42cUuNkk81ZXgWMkjQumT+1ae5rkLF5zEKcyQQTZs000DSugg7Nlom4GPt96oqe6gdQSWRfk4J5i7RRcMAgP6j9JgSMZkXpqBGa4J2zZh3RQWTFacp6g94OEXQxojcLH3hituSFWoVbz9g0EY7mjEbz5xj68ElIrML40jdURAf/GyowrzlUQHhF15GBhSwqarNCl+H9himvnZ4UdFjjOItv6fBwfAqJrwSla/L49gwiNjhUzBW+MRjaBPVU31BP1K3vMuATTjn/acNLf5xuPOiILujUEgiRuGmxYfHPNdxLqMv7gWH0a/uKGaWGTa4ITqGPips5AKShVeX6kD6Qr04+hdyvLStvGNteJCteHr4kAlZuh1bziFBKnkFaRtX2XXFyFN7/BXt4J/RoqPWeEs77zNFYsXQeZF7awpsyH/vjpoBIrYx3fAHPgd6SF999X6Con/d2hPIMOJObjOqd/3CjQ/aAKAlBVzD7VGjPgiWwEw7g3Ra1OOs7stM X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(376002)(396003)(346002)(136003)(39860400002)(86362001)(54906003)(186003)(44832011)(6666004)(4326008)(38100700001)(26005)(316002)(66476007)(8676002)(16526019)(1076003)(2616005)(83380400001)(5660300002)(6486002)(956004)(52116002)(8936002)(478600001)(7416002)(7696005)(66556008)(36756003)(66946007)(2906002)(71600200004); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: Rw2ZGcgxs5MUidHwH/nhYnPN+pcVjaYDU2KatjcYkBwxmT0qG2QbcChVY6vmNbrdPNAns+tCMAPS9gm0Sw2oUXvjAg+q0JBWLMQpSX3plhWSIKdnoiAGYXxjNkw02pW1bWUTb9wXCP3aLQ8gopI8U3d7TINpwubq5Ky9GpmQClRdWkYeYe4id6WsQLd8acDH0d0ncTtn4TVfow/B56+Qgw6n+EuOCn0n2AJ2TyFKtPMGQQCcWQcUVBlYaW7sCpNsjvXlpZvTU9DdX/LKXWlckerxHY+1MbbyMHDCEvfJOMPSgW4BqaVC4Nc43CIVCeXTHJDPaAJ1x+QjXk2Nv6h2UWn10zGsu5mDaWt+CM+9cm3yvMEkOSkfN4mLG4PoMP2zFLsvkZ4W/OTNWN+kT0VFqJUzmtNjnltiCDiD4esAdXwAhck6jLorRqpXVWYfDrqDLBjVIlgcub994u3xVHfzOcRk2qc1dBFXJYEdF4tah1uRvz1MXdNR8RqqcOejhyoW3XZ+bdIAVd66EPS+sJT5JqBkfk2H0bV1rgYLaJt562+hE7/dy+2+PbTYGug/PWuaR70ekqH4GI6Glm2xtSswLxe5UMEEDMW+dReFSXJUOl/7yCXXrpY8NkEx09zGe+kN77M1JLPU4dAvUfe8B6/Yn6LdxZSzLdhMZOET1Z01jlnzl7ewZO/CuNsfv12aYaH8fhAQoQMmhOmyj8J0HoBqFi1cMxK+74JVUMIPO+GD5LFhA5bpFPm5AYKmSak6Ijcvxgbbo5PonoBn/nZDrMg2/88FhCdGE/4OVc9Nk7wxkwvc/dI3RfRmEC1ZVf+ukPkaG+dOKIyVD0OgqTlfPXFsjZy+5gThB8D67dqK8DAuY+idBbGCPw6Jn6V0Gz0jZZvQavFNbqFxPsY48XyDCcp8RnP8zg5dA99Tgj7VZpJHVFMpzG/EeI36phk3KZke8WIeHDz0cKGxvcbQKq7xt+PG/b7aedK4zIIVCxgY/jwm4Ql7zq7oA1zLhzeCoIjdlvQ9SdrqyrTBHs/LUgNEDPaP8Gx/M5NV8z9apHBYhurmwyJ46gMosXlXFmZWdjMJiXidSkLX7NuDVbsBnOvyfrZGMFF3Q46W47uxnBQ5PggmI9VtP6Y/0Of6lkM0u4FL94Y62G2aM2MW2ZaLAl6fFt8ceKxppAZkWCE6xOHXUCYcJ5boON0j1tAfm7hg4qwdaD0O2isUUEPu2xIIWcNLACOzKSRCo4WBQ6wdB/FZhpr/3FHa/J22kIaJ33SGJsUpcH4sJbA8hE6QEoasxxkMn2dvI0Q6++nlo4hx91B5YkzQDSw9zzzsYwqSvAh4bN5+TBsr X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 769d9387-8c4a-4d26-3a8c-08d8eee6f596 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:05:00.4406 (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: QRFz+srWdYmcp4RZIm7z4aCxUY3Pv+/JB48Dx9s9xvX6ZaXj6adSQk8Lmu/1Z7nEIvJheAH4duDcT0jQb7UpJQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4557 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The behavior of the SEV-legacy commands is altered when the SNP firmware is in the INIT state. When SNP is in INIT state, all the SEV-legacy commands that cause the firmware to write to memory must be in the firmware state before issuing the command.. See SEV-SNP spec section 5.3.7 for more detail. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- drivers/crypto/ccp/sev-dev.c | 90 +++++++++++++++++++++++++++++++++--- drivers/crypto/ccp/sev-dev.h | 1 + 2 files changed, 85 insertions(+), 6 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 242c4775eb56..4aa9d4505d71 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -148,12 +148,35 @@ static int sev_cmd_buffer_len(int cmd) return 0; } +static bool sev_legacy_cmd_buf_writable(int cmd) +{ + switch (cmd) { + case SEV_CMD_PLATFORM_STATUS: + case SEV_CMD_GUEST_STATUS: + case SEV_CMD_LAUNCH_START: + case SEV_CMD_RECEIVE_START: + case SEV_CMD_LAUNCH_MEASURE: + case SEV_CMD_SEND_START: + case SEV_CMD_SEND_UPDATE_DATA: + case SEV_CMD_SEND_UPDATE_VMSA: + case SEV_CMD_PEK_CSR: + case SEV_CMD_PDH_CERT_EXPORT: + case SEV_CMD_GET_ID: + return true; + default: + return false; + } +} + static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) { + size_t cmd_buf_len = sev_cmd_buffer_len(cmd); struct psp_device *psp = psp_master; struct sev_device *sev; unsigned int phys_lsb, phys_msb; unsigned int reg, ret = 0; + struct page *cmd_page = NULL; + struct rmpupdate e = {}; if (!psp || !psp->sev_data) return -ENODEV; @@ -163,15 +186,47 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) sev = psp->sev_data; + /* + * Check If SNP is initialized and we are asked to execute a legacy + * command that requires write by the firmware in the command buffer. + * In that case use an intermediate command buffer page to complete the + * operation. + * + * NOTE: If the command buffer contains a pointer which will be modified + * by the firmware then caller must take care of it. + */ + if (sev->snp_inited && sev_legacy_cmd_buf_writable(cmd)) { + cmd_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); + if (!cmd_page) + return -ENOMEM; + + memcpy(page_address(cmd_page), data, cmd_buf_len); + + /* make it as a firmware page */ + e.immutable = true; + e.assigned = true; + ret = rmptable_rmpupdate(cmd_page, &e); + if (ret) { + dev_err(sev->dev, "sev cmd id %#x, failed to change to firmware state (spa 0x%lx ret %d).\n", + cmd, page_to_pfn(cmd_page) << PAGE_SHIFT, ret); + goto e_free; + } + } + /* Get the physical address of the command buffer */ - phys_lsb = data ? lower_32_bits(__psp_pa(data)) : 0; - phys_msb = data ? upper_32_bits(__psp_pa(data)) : 0; + if (cmd_page) { + phys_lsb = data ? lower_32_bits(__sme_page_pa(cmd_page)) : 0; + phys_msb = data ? upper_32_bits(__sme_page_pa(cmd_page)) : 0; + } else { + phys_lsb = data ? lower_32_bits(__psp_pa(data)) : 0; + phys_msb = data ? upper_32_bits(__psp_pa(data)) : 0; + } dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n", cmd, phys_msb, phys_lsb, psp_timeout); print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data, - sev_cmd_buffer_len(cmd), false); + cmd_buf_len, false); iowrite32(phys_lsb, sev->io_regs + sev->vdata->cmdbuff_addr_lo_reg); iowrite32(phys_msb, sev->io_regs + sev->vdata->cmdbuff_addr_hi_reg); @@ -185,6 +240,24 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) /* wait for command completion */ ret = sev_wait_cmd_ioc(sev, ®, psp_timeout); + + /* if an intermediate page is used then copy the data back to original. */ + if (cmd_page) { + int rc; + + /* make it as a hypervisor page */ + memset(&e, 0, sizeof(struct rmpupdate)); + rc = rmptable_rmpupdate(cmd_page, &e); + if (rc) { + dev_err(sev->dev, "sev cmd id %#x, failed to change to hypervisor state ret=%d.\n", + cmd, rc); + /* Set the command page to NULL so that the page is leaked. */ + cmd_page = NULL; + } else { + memcpy(data, page_address(cmd_page), cmd_buf_len); + } + } + if (ret) { if (psp_ret) *psp_ret = 0; @@ -192,7 +265,7 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) dev_err(sev->dev, "sev command %#x timed out, disabling PSP\n", cmd); psp_dead = true; - return ret; + goto e_free; } psp_timeout = psp_cmd_timeout; @@ -207,8 +280,11 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) } print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data, - sev_cmd_buffer_len(cmd), false); + cmd_buf_len, false); +e_free: + if (cmd_page) + __free_page(cmd_page); return ret; } @@ -234,7 +310,7 @@ static int __sev_platform_init_locked(int *error) sev = psp->sev_data; - if (sev->state == SEV_STATE_INIT) + if (sev->legacy_inited && (sev->state == SEV_STATE_INIT)) return 0; if (sev_es_tmr) { @@ -255,6 +331,7 @@ static int __sev_platform_init_locked(int *error) if (rc) return rc; + sev->legacy_inited = true; sev->state = SEV_STATE_INIT; /* Prepare for first SEV guest launch after INIT */ @@ -289,6 +366,7 @@ static int __sev_platform_shutdown_locked(int *error) if (ret) return ret; + sev->legacy_inited = false; sev->state = SEV_STATE_UNINIT; dev_dbg(sev->dev, "SEV firmware shutdown\n"); diff --git a/drivers/crypto/ccp/sev-dev.h b/drivers/crypto/ccp/sev-dev.h index 18b116a817ff..2ee9665a901d 100644 --- a/drivers/crypto/ccp/sev-dev.h +++ b/drivers/crypto/ccp/sev-dev.h @@ -54,6 +54,7 @@ struct sev_device { u8 build; bool snp_inited; + bool legacy_inited; }; int sev_dev_init(struct psp_device *psp); From patchwork Wed Mar 24 17:04:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408962 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 15667C43446 for ; Wed, 24 Mar 2021 17:05:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E96BF619B4 for ; Wed, 24 Mar 2021 17:05:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237147AbhCXRFa (ORCPT ); Wed, 24 Mar 2021 13:05:30 -0400 Received: from mail-bn7nam10on2089.outbound.protection.outlook.com ([40.107.92.89]:12961 "EHLO NAM10-BN7-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237038AbhCXRFG (ORCPT ); Wed, 24 Mar 2021 13:05:06 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HfcNiuNqsVaY/x8zLmQsfUnfNTYaovpLgJkeDG2g+h+eOX/pd1685ABPEDdAbMHsW/yLAfAkE2KTFQ+cpK4gZKzCfP16kOsSQGiHV3FeDYWfZAbnxVgtoU3TsZLnNimHwFOX+UEdYha6So8+EhjTm4iWKtTE+Z+YjaepgzMPcjAyqjwI32EnGTcp0PGpUYZH8TsF/qiO/HoXcnsn9jx5zwDSd4O/BLnMVcxDIZvKcyQVS/UiV9Ah82+HXO+FxfYzeY9y4A3OUjJsMR+SYnvBiMnLhoif3bu3KTta3Xc49bnxDV5+iJzg7oRVdJxAsv8EfdzZO9piTBuulAN49Xb9Qw== 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-SenderADCheck; bh=zgGBQydwdQQknfHd2XQrC6ndF4fFhwTX2JD31hcEizk=; b=Y+JFBWirI3YVXUivf/3rImkn8d4SlFhYnjlCfJ5+c40CvmbRGBy5RJBrHKXdym0Rp+5IrLrK+cooWLplIMDlEh1LcA9Fxo/d5JYYlLpV48iLCUDlI4ia2zVBceSH7keoMT2z1glMPmWdfHW3reQSC9M6HjMEI92vakkDyMMpOh+OE15VxuifUoPDX0d+6yIby2I8IkARstdHwAOYw5M87JqPBt5GvHEgmHoxD+d98UvlDfCqPpIwUpjQkmdov0nKoOBXbh4IWzaE5uFMje9Mu3wW0NCMa5c83Wn6d9IjLP4r4csq9cGjsvNhS5mywCl8SQGCROn47CbiAuywQaHMEg== 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=zgGBQydwdQQknfHd2XQrC6ndF4fFhwTX2JD31hcEizk=; b=gMo04c0woqsuT5aaciqDrzE+X7laOLh4xsUwLYCc515mZK6FB20IX063yrR+83mnRfHPFYOe77j3f5Ir5NO4mo+i7q+RiRb5zW4AM0/joGhgziGet9YC+0I5OTebVyn0HBxX9dMBBlXW7LPCNDALS66MlrEWvSYQZlpUSZV/9Oc= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SN6PR12MB2768.namprd12.prod.outlook.com (2603:10b6:805:72::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3955.18; Wed, 24 Mar 2021 17:05:01 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:05:01 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson Subject: [RFC Part2 PATCH 13/30] KVM: SVM: add initial SEV-SNP support Date: Wed, 24 Mar 2021 12:04:19 -0500 Message-Id: <20210324170436.31843-14-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:05:00 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 572bc454-efb5-4fcc-80fb-08d8eee6f602 X-MS-TrafficTypeDiagnostic: SN6PR12MB2768: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:813; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /Smw9KAojWtGfPbWeY7TxVhj/AT04cMI5hf63B6itpa51tr4123eGIXwyXinftlB99dZDtJgCWyPWOlWEGT5TfL98JLRrKZkBcYJ4J1QvzzaaQ5ZehJjkqxcl5W599KzyMdOQatNw9r1dKNsp8UNHWdr2eIigI2F/PH0wJrICr76JEzm5JYnglP0t65mpWAqgGsxMM4zgOH/IBVShIYhuQDdsrOZL89Wa7s7AIQm+KhOap68zDp4ARu1BHoUHsoF9fvRs/8L4GsdQAQXkaUP1W7jIusgLNlmlQ/WSiyVslHvClHsaUGCR5DrlOwYG7KkHfBb82id1JYiAqGABTTxQf5DlMRCJH8H6GgRWTVnuJYeXmyTxDcsJZeUcsgTq/dYBxqThry8T9ph8m1TOXQ+uhp2wJ1ZTdsfkNK1+Zyh3+SFsxzD1fiOBpidi2VelPMzFboWws+y1JkCZKuZ4sU4aGbR192gKgvugjLae+9fic/m6B1ZKQ+DvJR0FTqCKAWIfq9ONHmxqVvcYD/eYbDfM86Ur8JHqWdqi7pjkUoHwlzu6CjJ2AnL6XpwgS3XzPmG9+y9bsrF7snoQypUfd/Qy9Gi+zi16VW9mD8iEiY6EDBmUI+O1HVMXHwteK8w36xIuhnd3CL0x8Ddma/+pInwOw== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(39860400002)(396003)(376002)(366004)(136003)(346002)(4326008)(5660300002)(7696005)(66946007)(956004)(66476007)(2906002)(1076003)(7416002)(38100700001)(52116002)(86362001)(44832011)(6486002)(2616005)(66556008)(36756003)(8936002)(83380400001)(8676002)(26005)(478600001)(54906003)(6666004)(316002)(186003)(16526019); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: WrgUYY00phhP0Ss72gfmssESsUVSgYvENRI7JMg75mX9a49E4mA615rP4xvIf1RmMBYmQM0P34VDrwQNgldRpauXmz+YP6HVqxklFZVJ9J9is9dGEbFy+xepKNTSnsQLTotHj/7cRVLxa5TcLmrR0ItJwPfecyXETlKDRfz1e5DE+BX8vOqjq9lQEzckiRSUXEPQVqz7JuCkTXDaPQPGLeIzOpmQ3pV/vkPqRWNRsW6RTe6BAoUmU9fz8bc/DPsUS3jBcF4PiJV6j+L2Gjf/A5fDbwWd5OfjWjYWSKiwY3F7FTT1RkKt6Q+VK/VOl57y/zFUz3gu+8czu+bAPyLJfHqS6UgycRfUuX6C0VFJGdS7hL9nmEkw3XYMcHVQUURX9mw+ec3EunV8NtP6V3O1qwn7RBNOrEWkmx2OquPCllEoq3H+BXIy+wKwzfLS9jvVJMY0znfEaa5+gldtJnhPOeP3u+No8dHsE6Fb+37dhF4tm6S4GjAw97dmDnYnbWshwpDxgjlZR3Zb5mn8TuD7GVKQl4IxAwtkw9tD2VUXOTBQ3ZIksFsgUqSOgmkXnIhv41LeDaCwpH30/nrPcdGcyvnPDbwBoox9R9cSoxxjzCCFKEvplA1JYUgb9ci2Rn1hIRvyo4YOl5PXMB3xItOEWSAykqILetanVkVjN/ZJgqME/Dm6AN1J0uPy/5cb85tTnmPOk0HLaaBfEo3JFO3h25PtefKZRIuLVof6z2DdeelTKCq9MxBeplxs3kqZz9GwH+UwzOCD1M8m4mNtiuWsAr4/4IeJicP0hZARSDdF/fF7cqLkkg8YO/0uqsmbCyBRXLhRYzGuGvzuaz+chLTP5yBpDBelKTnBoukdhBXB4RnHjgRJiT6Y3sKCWsAhDetsjG8nfv9WUqoGWWqPXMY09dNNfq9UFTz85q5UwOOaXgmK71+L7WRNiy5feiCq+sT7vlReU82rpy7Ps0kewtvCBDiYSyOaxcH4/imdcXUZyWFviHuF4dJytRQWsMKaI35nCIK5cWJ4RHu3bn+Ooz937A4gaAjOeXuTBL1jjPXFC+WzaELzEBI4VGwNIxA/02oTslxbPOd7p1bL/riaRgigEbSrIvis+ANgOlji3LtU5l+pCLFe9aIKTxEpjs6QMPda1KhrNT/wzcL4KWOcdDE3y3R4uxhkn8MzIXxcwxxhDVTUFGEXFLC5P7kuOQON+JQvGDDbP3pzEhla1bec1/RoMlw/Rq3CDZI2No9wZ6nSJxDM3WIL9aoOe2LCiq/Yh8GrC6vGRyIT4PheFp/+DDsOpTO8yUykDnBmxknqh786a8dGcfyQZ/Wk+rZEGl4e9tk5 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 572bc454-efb5-4fcc-80fb-08d8eee6f602 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:05:01.2042 (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: +ZP9CngleqRD2DjU2W26/Z68tCJv5/S0SU5zI9uNHA3/+Zxydxf7EHdHza4CTI5wtDVO+Z8Q4UzG+5LITK3aDw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR12MB2768 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The next generation of SEV is called SEV-SNP (Secure Nested Paging). SEV-SNP builds upon existing SEV and SEV-ES functionality while adding new hardware based security protection. SEV-SNP adds strong memory encryption integrity protection to help prevent malicious hypervisor-based attacks such as data replay, memory re-mapping, and more, to create an isolated execution environment. The SNP feature can be enabled in the KVM by passing the sev-snp module parameter. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/kvm/svm/sev.c | 17 +++++++++++++++++ arch/x86/kvm/svm/svm.c | 5 +++++ arch/x86/kvm/svm/svm.h | 13 +++++++++++++ 3 files changed, 35 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 48017fef1cd9..b720837faf5a 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -19,6 +19,7 @@ #include #include +#include #include "x86.h" #include "svm.h" @@ -1249,6 +1250,7 @@ void sev_vm_destroy(struct kvm *kvm) void __init sev_hardware_setup(void) { unsigned int eax, ebx, ecx, edx; + bool sev_snp_supported = false; bool sev_es_supported = false; bool sev_supported = false; @@ -1298,9 +1300,24 @@ void __init sev_hardware_setup(void) pr_info("SEV-ES supported: %u ASIDs\n", min_sev_asid - 1); sev_es_supported = true; + /* SEV-SNP support requested? */ + if (!sev_snp) + goto out; + + /* Does the CPU support SEV-SNP? */ + if (!boot_cpu_has(X86_FEATURE_SEV_SNP)) + goto out; + + if (!snp_key_active()) + goto out; + + pr_info("SEV-SNP supported: %u ASIDs\n", min_sev_asid - 1); + sev_snp_supported = true; + out: sev = sev_supported; sev_es = sev_es_supported; + sev_snp = sev_snp_supported; } void sev_hardware_teardown(void) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 3442d44ca53b..aa7ff4685c87 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -197,6 +197,10 @@ module_param(sev, int, 0444); int sev_es = IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT); module_param(sev_es, int, 0444); +/* enable/disable SEV-SNP support */ +int sev_snp = IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT); +module_param(sev_snp, int, 0444); + bool __read_mostly dump_invalid_vmcb; module_param(dump_invalid_vmcb, bool, 0644); @@ -986,6 +990,7 @@ static __init int svm_hardware_setup(void) } else { sev = false; sev_es = false; + sev_snp = false; } svm_adjust_mmio_mask(); diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 6e7d070f8b86..3dd60d2a567a 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -73,6 +73,7 @@ enum { struct kvm_sev_info { bool active; /* SEV enabled guest */ bool es_active; /* SEV-ES enabled guest */ + bool snp_active; /* SEV-SNP enabled guest */ unsigned int asid; /* ASID used for this guest */ unsigned int handle; /* SEV firmware handle */ int fd; /* SEV device fd */ @@ -241,6 +242,17 @@ static inline bool sev_es_guest(struct kvm *kvm) #endif } +static inline bool sev_snp_guest(struct kvm *kvm) +{ +#ifdef CONFIG_KVM_AMD_SEV + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + + return sev_es_guest(kvm) && sev->snp_active; +#else + return false; +#endif +} + static inline void vmcb_mark_all_dirty(struct vmcb *vmcb) { vmcb->control.clean = 0; @@ -407,6 +419,7 @@ static inline bool gif_set(struct vcpu_svm *svm) extern int sev; extern int sev_es; +extern int sev_snp; extern bool dump_invalid_vmcb; u32 svm_msrpm_offset(u32 msr); From patchwork Wed Mar 24 17:04:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408316 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C8EA3C4345E for ; Wed, 24 Mar 2021 17:05:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 959D961A17 for ; Wed, 24 Mar 2021 17:05:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237152AbhCXRFh (ORCPT ); Wed, 24 Mar 2021 13:05:37 -0400 Received: from mail-bn7nam10on2089.outbound.protection.outlook.com ([40.107.92.89]:12961 "EHLO NAM10-BN7-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237051AbhCXRFH (ORCPT ); Wed, 24 Mar 2021 13:05:07 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cgF95gNb/Tca5Gtibzl2up07k6PDDuHSbblTFpsIyqC4MvfvKGJJO3efW+PMODmGHIkZiCqKWWQXQ0+UEw09PSkyZXtpu1gJEgkZg1MdKeVlSedL1F8DE0txNvO0lPuMp+fDm9xuTi476y6SZcChSyGjaM5KcLC8NvNdDPvY9scRngQGTYOLUnW+W+N+xjtFjJ6ICgCjPhW7CJhLdgaNGo+VuO8Z+6WSDFZwTZMeuBpCzHnuWzj9HyfSubNf5SPHlJtXPVX0R2e81vsCVJjKn+0LlL0SwD3eHbahY/2IsCJdgS2ZipIANsCp4S47nmzICCtQxROSvemrsImIXHLPjQ== 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-SenderADCheck; bh=onz7IFNSkbYQBgnOp2uPu5Ik/9kXiYPCYwVrWPc74G8=; b=h3G4eSzf6yyn8uTes8W2ag/zAOo4tWeMgJfIddYOtFvp5pkBliiRePS9KFe3GPSE8TmZmdQzBQfRXbplwYZnm+TIDNWUIjy5r+SEWC4dFn4Vzn8OxfP4g1v65bl0VTIREtv/eWDkBy45QLPZ3Jwr4Kj7ID7AxcK017uJo+Q3TSeAIuBlLx6NZCqwNBLRYJvLRT0UXgs5rUsBLOg3n+q6HKkS7AWLLcv/cd5l5Tojy0ha69+wuIskP8koFKgGpa8D087L7lYgJwucnI/c79fkSAEgyt8aO0IagsW5n3jzEpVspgNu48G8drFGcCPPYkfRbrzTN0qj59AWriBU/h+TLw== 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=onz7IFNSkbYQBgnOp2uPu5Ik/9kXiYPCYwVrWPc74G8=; b=I9rHiedJYSzGOFltFtBUVf089w5nRS1B1WvW9R12mOlht+2ryPydIrX4mBwpsADB20MdIYVtUb8R9vLnT+Xn5oHKAAlfxRrdSFA6Q8dpUgzFViHpCQyx8FqFmPQ6xuV/lr6iDTfoZa7fL/LChDbj0eWZ8te0RqzFiqdCV5Zlv7o= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SN6PR12MB2768.namprd12.prod.outlook.com (2603:10b6:805:72::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3955.18; Wed, 24 Mar 2021 17:05:02 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:05:02 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson , Marc Orr Subject: [RFC Part2 PATCH 14/30] KVM: SVM: make AVIC backing, VMSA and VMCB memory allocation SNP safe Date: Wed, 24 Mar 2021 12:04:20 -0500 Message-Id: <20210324170436.31843-15-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:05:01 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: fbe3b245-49d4-4e16-ed30-08d8eee6f680 X-MS-TrafficTypeDiagnostic: SN6PR12MB2768: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bBcseBjLvH+srAw9FwWNzQGViBHIaoj98s3AbaJluq6TO4Vc52oI9sh8mxaJJrXNulKvuOTmGw67eBo5WBMltnlUt9WA7yoWtHy+JTHcKKAJrsAI03sV4m59fIPuQKfiSnSzcuKs7KuiP3NTBHuxIdGbVpO2quLj0BrTyacXcVxp/KZzYk7V4DTzgb0iuVsVqHMdvmX2SXRrWPzN9oIzfrU4Gg8D0ZevKRoCiUvSNCpS23Osvthx8qFfzib7KwALffFHBEQT4Q/2W8iEdVMYO6Ii0G4XlmXkS86RdxTZwHE4Z99IeupnRVUlj8KfskBtDsP7cfeJvPvtttb5wFpqYk8UJQxxanmq8g35/Npt7qxbXzCSjNOByXCLTSorWVNWn/C8rT2L4dcUrhkZLctzOQr90B2JA8SbxnEFRzEbZ+9cBikzZluMn6LlC2jpHhO/yfmPOuu+A6iWKFhFRoVI4c9YzIAy9aAt4DPlwLkZe5MZIUq6BYCnRes1XU5uGGee9SnqEvLmajCbyyOg8lS8TMnskpOi70MO35rwVeInV6lqUuBn7CebF9bWB10DJmVsPeqr6sdbzG8TBxENFdiIU8BHgaiCBpnjE+xkeV1bkE68KGNS/rtoyGqOI7nxNTRGLPrtH41+eXAIBD+8bi40Wg== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(39860400002)(396003)(376002)(366004)(136003)(346002)(4326008)(5660300002)(7696005)(66946007)(956004)(66476007)(2906002)(1076003)(7416002)(38100700001)(52116002)(86362001)(44832011)(6486002)(2616005)(66556008)(36756003)(8936002)(83380400001)(8676002)(26005)(478600001)(54906003)(6666004)(316002)(186003)(16526019); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: VpCdt+TdqwThJ/ATQIWRNfwjTGAFjleVs7KC+53J+TJgOeFWQ5aWD5x23MUAQus8Ko2QlUmfQy1uj8Sswfxt1m/+kChoI3pexLc0iMrR0gmK054LMnFvenp32VbgjcUx0FZoOlyHRZg6ez5C5dyANWAobXTQTHlcPh6x4bHx4X4zyNfas8i7Rg04WS1m0LrNA0DM2Sv3k5MS8HDno8nG3erWMBnWfJNvCZhYlD1hSEzMYp4IUgoowrk9IKqr6fcv563ggFZInwx6MgkokVulurF7cuRUheL+lPpkFfX/iRY1FxEA0Bce/E9YYL6B7wsQ/GsZfGwW54KCQ1OVjXQ9SEzjIrKE71nAhItSn+I+eIcswiFOisXXaArb43jBE271fnSHgNfzF7RDwaYodkq1e74JFrDedFi2FlE2Mfs68Big+n4a/v+iA8umWfUmSJIHHZy0XQ7e5rJA2hZLPbUdGV41Yk++NxQkX4BgN5dDWyGq4BIu0wrIJA3kGXPSRicynMwPUez2+A0qwFqL7cEWIec5EvtjU0BGiPrfMAOkRQLUpRzVt/hml+HLoRcw+wVJgpWVwWPxUhRrRRnzbL2SRaGF80l5rvZI196HGZ2VPIZsD7AYiUnqs3mLdGCIwMYOLlIzV+CHBuJ3jq2MYi+v1fTNCOYJj0XuRRWkJjVdRQMTASwm3Ora2FhacY/yNQQurjFq2SJxr3D6yk/PD5rAyoZvGq051LvYwbWL/gtl9MwouYTQfwogPlM9hGNloyk31EfbzDayVStvCGMkSUE3Yuo/ajJSwmoVERgIJO87D7VElVCHIMjJt+yBrVPnqzufs5vZkZkYEjOoVY0xtXTkuQLB1jvp5jIQrk2ACrwIlzgAWVGAWDk2rkii9nfS2/xwf8Rdfybla97Om7mE0lyJf9UJFwa4mMA9eARLNF8mFTw66D1iSJCkEcOOXKTIYJB8GWT4bPP5ec5InDtxW/67gB4StkPC1ac9ne1E0gAVav5CJ2xo2+8Y9qjoGRkeh+oNYqQt5iWDB92p2nfK62NnpeP6/TBwBMoinAdlb+0qFDVyfsiAnyAHER11j9QmMy3m1bVasn0vQdPQNARlFctu8yugMN7B1zxmHxhF1FkqncU/yKyr+06qCI+NoYoDKTSl+LScxtPjfe0WPCXiterWSBYOpdHVChCfldKRNbbNsAPzyGjgBU+3ttD3kXxTcFL5rAWYxnm9uK3ryQskB2mM9qXi7gfUuEis3cnNxW10EHHK+C5xPqrvYroL33n+oYFoqBq6O2NUlkokyW8/kjKEJqDS8TnMTXNlH6ED21S2B0A3kfuKiBSbJeoxU8crJoji X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: fbe3b245-49d4-4e16-ed30-08d8eee6f680 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:05:02.0936 (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: zfS+VSRYtkdQcEB9Gd10Nm500x1fPDXgzuF8JMaJvzOufqult+SUmIZ0U9bJuY6Emeq82l0nyjBg5kc6obxJ5Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR12MB2768 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org When SEV-SNP is globally enabled on a system, the VMRUN instruction performs additional security checks on AVIC backing, VMSA, and VMCB page. On a successful VMRUN, these pages are marked "in-use" by the hardware in the RMP entry, and any attempt to modify the RMP entry for these pages will result in page-fault (RMP violation check). While performing the RMP check, hardware will try to create a 2MB TLB entry for the large page accesses. When it does this, it first reads the RMP for the base of 2MB region and verifies that all this memory is safe. If AVIC backing, VMSA, and VMCB memory happen to be the base of 2MB region, then RMP check will fail because of the "in-use" marking for the base entry of this 2MB region. e.g. 1. A VMCB was allocated on 2MB-aligned address. 2. The VMRUN instruction marks this RMP entry as "in-use". 3. Another process allocated some other page of memory that happened to be within the same 2MB region. 4. That process tried to write its page using physmap. If the physmap entry in step #4 uses a large (1G/2M) page, then the hardware will attempt to create a 2M TLB entry. The hardware will find that the "in-use" bit is set in the RMP entry (because it was a VMCB page) and will cause an RMP violation check. See APM2 section 15.36.12 for more information on VMRUN checks when SEV-SNP is globally active. A generic allocator can return a page which are 2M aligned and will not be safe to be used when SEV-SNP is globally enabled. Add a snp_safe_alloc_page() helper that can be used for allocating the SNP safe memory. The helper allocated 2 pages and splits them into order-1 allocation. It frees one page and keeps one of the page which is not 2M aligned. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Co-developed-by: Marc Orr Signed-off-by: Marc Orr Signed-off-by: Brijesh Singh --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/lapic.c | 5 ++++- arch/x86/kvm/svm/sev.c | 27 +++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 16 ++++++++++++++-- arch/x86/kvm/svm/svm.h | 1 + 5 files changed, 47 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 3d6616f6f6ef..ccd5f8090ff6 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1301,6 +1301,7 @@ struct kvm_x86_ops { int (*complete_emulated_msr)(struct kvm_vcpu *vcpu, int err); void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu, u8 vector); + void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 43cceadd073e..7e0151838273 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2425,7 +2425,10 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns) vcpu->arch.apic = apic; - apic->regs = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT); + if (kvm_x86_ops.alloc_apic_backing_page) + apic->regs = kvm_x86_ops.alloc_apic_backing_page(vcpu); + else + apic->regs = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT); if (!apic->regs) { printk(KERN_ERR "malloc apic regs error for vcpu %x\n", vcpu->vcpu_id); diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index b720837faf5a..4d5be5d2b05c 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2079,3 +2079,30 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) */ ghcb_set_sw_exit_info_2(svm->ghcb, 1); } + +struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) +{ + unsigned long pfn; + struct page *p; + + if (!snp_key_active()) + return alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); + + p = alloc_pages(GFP_KERNEL_ACCOUNT | __GFP_ZERO, 1); + if (!p) + return NULL; + + /* split the page order */ + split_page(p, 1); + + /* Find a non-2M aligned page */ + pfn = page_to_pfn(p); + if (IS_ALIGNED(__pfn_to_phys(pfn), PMD_SIZE)) { + pfn++; + __free_page(p); + } else { + __free_page(pfn_to_page(pfn + 1)); + } + + return pfn_to_page(pfn); +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index aa7ff4685c87..13df2cbfc361 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1324,7 +1324,7 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu) svm = to_svm(vcpu); err = -ENOMEM; - vmcb_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmcb_page = snp_safe_alloc_page(vcpu); if (!vmcb_page) goto out; @@ -1333,7 +1333,7 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu) * SEV-ES guests require a separate VMSA page used to contain * the encrypted register state of the guest. */ - vmsa_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmsa_page = snp_safe_alloc_page(vcpu); if (!vmsa_page) goto error_free_vmcb_page; @@ -4423,6 +4423,16 @@ static int svm_vm_init(struct kvm *kvm) return 0; } +static void *svm_alloc_apic_backing_page(struct kvm_vcpu *vcpu) +{ + struct page *page = snp_safe_alloc_page(vcpu); + + if (!page) + return NULL; + + return page_address(page); +} + static struct kvm_x86_ops svm_x86_ops __initdata = { .hardware_unsetup = svm_hardware_teardown, .hardware_enable = svm_hardware_enable, @@ -4546,6 +4556,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .complete_emulated_msr = svm_complete_emulated_msr, .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, + + .alloc_apic_backing_page = svm_alloc_apic_backing_page, }; static struct kvm_x86_init_ops svm_init_ops __initdata = { diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 3dd60d2a567a..9e8cd39bd703 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -603,6 +603,7 @@ void sev_es_create_vcpu(struct vcpu_svm *svm); void sev_es_vcpu_load(struct vcpu_svm *svm, int cpu); void sev_es_vcpu_put(struct vcpu_svm *svm); void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector); +struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu); /* vmenter.S */ From patchwork Wed Mar 24 17:04:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408954 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E6478C4345F for ; Wed, 24 Mar 2021 17:05:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B4C7061A16 for ; Wed, 24 Mar 2021 17:05:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237150AbhCXRFb (ORCPT ); Wed, 24 Mar 2021 13:05:31 -0400 Received: from mail-dm6nam11on2071.outbound.protection.outlook.com ([40.107.223.71]:41848 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237050AbhCXRFF (ORCPT ); Wed, 24 Mar 2021 13:05:05 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Z/boGn1todGX3/Tc7n0xZ/hjJF7ABJRooS/KfFkiyFxbNMC6Of5mpRnnCtKnA8K7yGUKlEWnoyOAPhDvrC190z3Agnvgj+Xiwg++0hQVJvVGTD2uHwgouz8dC1/QqwO5vL5L1z5mKx8ZNL6ggGo/NbiYwiHOVRCN6xtNF0XZziS+ZLRQaiazXPKtTy01s/29mxdsLLTF2IsdLmDMkbszQxA4IIYId3QZa0vkaE5FRw9aJcUWG4JdYEXDrZuG/d1ZpYifqqZ6sq7GzifAUUcllLjM7tzRlU2JFkf+FDs/h2tneKRjyZTtT9ZRlp9tAF6P8U3ylmUg6Fa430vkR5uzqA== 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-SenderADCheck; bh=ZxkkNx83skXWaNhyaqn+dzT3KPwLhEIZshU8BEQlKU8=; b=QMHjo+GmVSl0YCKEggBYIQohyZFgnCN7RSRMl6nVa68vE2zOXstpjfpmDRtFGQQdaoxdrKiuk6EBNyfjUq8LaNZq40FZ8aRYO4CX58xHynffcxBDE0+rsHYc02MplxlOx1busCtLJgCoa4wN2PXSfTK3XICW2kEi+QcQL8PHbcmPjFj5KFcOnarcIfvZW69M7PZlKP1ujPOf4tc+KMygDLBucimqNnCMAc2Z/xLMTkfHZnvAeclkYIJBhAqe90Rj6qjCeK7ZSprT9F6m/OV+lDj2ZwTceCVBrcGTNZhTPwDpD7rnp4rpn/SyEc80J8Rw0byxKt66Kz/9s2FMSIriqA== 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=ZxkkNx83skXWaNhyaqn+dzT3KPwLhEIZshU8BEQlKU8=; b=zGHWXBJavnY2VJOZKh4eNK39ZhOyWtnxf71vPADSJm8U5fCDeeuJNvBExD4g5M2tZT9W9iaNsilkmDhfil/YaJ9Qh/gKtw3O7Fp/OiBc/hnJ/ibLBRfb3jrvsZpDW7WlTUK5AcCjWoZUfBadqnGANbjD0dmIMcYAfwiCshcrfVM= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4382.namprd12.prod.outlook.com (2603:10b6:806:9a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25; Wed, 24 Mar 2021 17:05:03 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:05:03 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson Subject: [RFC Part2 PATCH 15/30] KVM: SVM: define new SEV_FEATURES field in the VMCB Save State Area Date: Wed, 24 Mar 2021 12:04:21 -0500 Message-Id: <20210324170436.31843-16-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:05:02 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: d69c3d58-410a-4fba-5bf4-08d8eee6f718 X-MS-TrafficTypeDiagnostic: SA0PR12MB4382: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:5797; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Pqaak33wi6ugApwc/tDwMU3GDwykygInayeeeqoHNGCvjFSlox2FQrRPs+T0m7K95T2M4EvhsY8Td+9O9cgcSnFvwXei9HkXwt+SmA/wRw4mJ+sFDBV1DuCuW7b6Vm1FJ3WA5AA7Z6KmbYM951aMXx93GopSs+wc0n0kPEjARbRGEDqeoKlGmNOJ7+GvO9JpMSt2RP522QNXucbKPpVk+p9S3wmXfvof1e3+zZ9BUnCAt0IGclI5mltooFEkzdQ9PVeNNcSMU949ZAuj21Sx7AIVts7NwxjM2oSs5OOITfUxrio2iOnKZwptDugqg8gv00X4X+6tPo7Rrj9zWNAnnNf8wVVlegsnFlBVVW/P4N9PN+Nh4WL1tQcu0Onv3EO/IgwH1/7g4/Nlxjzdi9OpMqq7cackW1z2zS9PlsuZejlKnao2mURJVDIrVPruEm1DthCB2JsqlQ4BvccEVr+QSRcemIqB50ngY+bkfRD4peT+WnC1iXvE2iNln0NsqKcrfHk1jihkcp3Nn1oOV3UoWXrbQYpQxnoVHqjwx3yDDXE/HIPKXu6vxWdiipDp+sTTmcRX7g8QHyZY1kLzBcD7pLONqWZN5ZuMbb/NUOv/xFZMm7E9Wu9iYzTRcNTgLC9/qWql2FxjwhI+kKSYuhN1xA== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(396003)(136003)(376002)(346002)(366004)(39860400002)(6666004)(44832011)(66946007)(1076003)(66556008)(66476007)(956004)(36756003)(478600001)(2616005)(83380400001)(8676002)(2906002)(8936002)(26005)(86362001)(5660300002)(52116002)(7696005)(38100700001)(4326008)(54906003)(7416002)(16526019)(6486002)(316002)(186003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 18WLDIn3LbTvT/R2AsTnCEm7PKSBKzf3bmVWNMMW+3AqPI3zS8QjCoPfEG8qOOeuJ9UHH6vypJKMiZjswEkCkNDLfIrLDvhMDwE4PG5yiNuB+9l7rABdVt1SsN2mBR/7gzOcCL0CN7Jaa3obEovVuFWS/dBfkTRCMVDOfkUWSabJxNGaYtmrvMJ12MbJuc8Zqm/WDEjlWTjhRgPa7lmdP6aGUcYARnopfvzp1HTL0+BChJUSr3Pn4RFys4yZJSBHukeZ3wYoeC2OB831lnt4BthAcHe0aTFxgeLIyiknfrefT2Ag76As3mYyMM0LstJihNCSxP2NDuvp8i+SibrzGnvEcuesqmKYIBDiO3ma3d6vVEhLi2Eq1XnXwhQIGjujWdhd/oNsDGJvXHfd3JVkNr0L0CMg1y4rSuXL9yD4KZaIy3LBxcPMdzHD+lDJulfbXZvbLqOioNcKtZfaUYQZOTelAZ1XGiYP6dico92t8l+BLNW6WftfrCOVkYqOxp79ClCCLptNIqzHRB+KWfY1lieWA4zudzIFX/p7ogk35ouUC5dhqcSlCPmlLEKCteQ2+TMxv1OeKKiht5N/2poLOmtxUwdZtSZe0FeNU3ekvNtkwIRW5egg+zZWXfYFfGGvHkWQ17aPebAEpKEflWK1lapkQFYCzR2nrjKzM9HVBdVz5L/dawkZjTDlk8gwr4D1rsKiLL5HbZgy9RcRE3L4F5zqmiZ0soi1uc36gDaqiQo3jAWxArk/B1dDrEGZ5APJQYO4GfC0wnzwM4xnbdAj8zMI8YRcYmYq2x1JZP+xQieUFPN/VzR8zHytMHdGdTEFb3m76web8P/W8MaDTO4AtF6SjrOENVR/7YzsnYKGUiSb9qQcfjiqUHYYi+fnJ5j1OFhM7oiNLF+ShpVVleOqKuwsCVZYgloBhrcAL8XEc8+Xl8vt+VoLI/zorHCnjf3OqlvIelxNph2rOMDcRP0PfQN75LjwhQlFy2XTpkfaoyIwyRDOmckC3YQRQ40LlEbqznTTKWTCRxzDCX+XoHdUB/Z2vw02ue9RgQkJODzelp/JlI/PFq2ipjXH3Gx0mc6QcvDPbJpYc6QTyEwQ2++YsN8w56NZEMSVi7FyQl53qnvSP5/T1xhgL1SPx0yShDGtKNsBNFFcPrd3DMPsKVIKOaJH+JyaTS9F1Idt7XcvvMt5FeFOcAmyTH3jIRMX81nLzkSt2bUoAKJpIIWY4MalWV6UhBeBGOyhTeSOH66LBhtYKe8Ej13sIP/oiScqFq7orpT5+UqOjX7N4QDA2MMbJSpw6x0G8wqtk5vbwsuoqSE1s4ojQdx9YYttCsPuv9Ko X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: d69c3d58-410a-4fba-5bf4-08d8eee6f718 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:05:03.0301 (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: BkPOntVO2D/rWODHJK2FRQGB2KY8zffy/vtIplas3DJrgcOgPzUlm/JKASJdsX5WtYSqKaQ2ipy66pFIvU7kJw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4382 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The hypervisor uses the SEV_FEATURES field (offset 3B0h) in the Save State Area to control the SEV-SNP guest features such as SNPActive, vTOM, ReflectVC etc. An SEV-SNP guest can read the SEV_FEATURES fields through the SEV_STATUS MSR. See APM2 Table 15-34 and B-4 for more details. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: Vitaly Kuznetsov Cc: Wanpeng Li Cc: Jim Mattson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/include/asm/svm.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 1c561945b426..c38783a1d24f 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -212,6 +212,15 @@ struct __attribute__ ((__packed__)) vmcb_control_area { #define SVM_NESTED_CTL_SEV_ENABLE BIT(1) #define SVM_NESTED_CTL_SEV_ES_ENABLE BIT(2) +#define SVM_SEV_FEATURES_SNP_ACTIVE BIT(0) +#define SVM_SEV_FEATURES_VTOM BIT(1) +#define SVM_SEV_FEATURES_REFLECT_VC BIT(2) +#define SVM_SEV_FEATURES_RESTRICTED_INJECTION BIT(3) +#define SVM_SEV_FEATURES_ALTERNATE_INJECTION BIT(4) +#define SVM_SEV_FEATURES_DEBUG_SWAP BIT(5) +#define SVM_SEV_FEATURES_PREVENT_HOST_IBS BIT(6) +#define SVM_SEV_FEATURES_BTB_ISOLATION BIT(7) + struct vmcb_seg { u16 selector; u16 attrib; @@ -293,7 +302,8 @@ struct vmcb_save_area { u64 sw_exit_info_1; u64 sw_exit_info_2; u64 sw_scratch; - u8 reserved_11[56]; + u64 sev_features; + u8 reserved_11[48]; u64 xcr0; u8 valid_bitmap[16]; u64 x87_state_gpa; From patchwork Wed Mar 24 17:04:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408956 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 65E2CC4345C for ; Wed, 24 Mar 2021 17:05:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3015C61A36 for ; Wed, 24 Mar 2021 17:05:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237155AbhCXRFj (ORCPT ); Wed, 24 Mar 2021 13:05:39 -0400 Received: from mail-dm6nam11on2071.outbound.protection.outlook.com ([40.107.223.71]:41848 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237053AbhCXRFH (ORCPT ); Wed, 24 Mar 2021 13:05:07 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EWIsyRFEZJ7KW6chubLFkfW0aZlnwfIYps+fkmwl7ef7mhSvwu3j4P212xwb7Cs3sYUfetxvc31taRsSrs/9UoaYqk19ZaLjXcsaSbiX4iPExaqBGxyKtoNDM+rkj713tqUY/J7T9JVr104mzaka+429o4w7iiYOk69NRJO9fakeInuSsy7KyUK3jSJA+7QE2uK1jr4EzcTgCprddJPxvlv/BD+Hv81Rf7DG6CBVKbQNTN3e3nx7TORUyxusTHtyd3VqXxTj4z/VmoLQKkiP9bOA4y5LVkUuvPPuuSLBrrsGcpOqhVqn3HoTzJewvoywLC63omrpNPb6JWOenMsZ2w== 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-SenderADCheck; bh=IXuxXQ4tqvmrxI/BUP7vB4cfLO5sTDWVRfqUI4qssOA=; b=PuNRGCg92Lgpvdjt8UY9aKBciPGe3yYYmolW3plI1axQCQiekZWwe/BHStgWkrxFb45/3zs/Xkf75Yxwpae+jrdJepXtjbx/XfRKUc70bSVRi0Fawy2UD8sQqWEMD4Spcmg8dOf/z0yoDRt0IZ/IwpOi39DtjreP+EkNccyJBZjpO+5uCAeSnewGCA179YVsdXtTgWXCcIRi0vfaWYxDViCR0IRoB+6TdbR9lmSfDphAYwGICxPw6lDCDEWeu38Vv6b0Fw8z+FeuVCR823D9XjVyT+xLIZixQMjrM4J8z9ud2JR9EHImLcXJDyYsDaVN38hmSckldCAavyyKr/ygHQ== 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=IXuxXQ4tqvmrxI/BUP7vB4cfLO5sTDWVRfqUI4qssOA=; b=L8pBVHuS06oAW3EwFP/nzzd+RZKbOZsX5vwA6WswbEIOSJNvvWLljRvFltvpQGXWVg4nNVutAopb8sq5ZfwpJHas4hPOaNDBT7t5DpQNRChQex0E+4OmN77QhGRXS8G/Ji9PEOnISXmZUIC2F0oFG1MojrsQc1VfnQuEUMjtjec= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4382.namprd12.prod.outlook.com (2603:10b6:806:9a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25; Wed, 24 Mar 2021 17:05:04 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:05:04 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson Subject: [RFC Part2 PATCH 16/30] KVM: SVM: add KVM_SNP_INIT command Date: Wed, 24 Mar 2021 12:04:22 -0500 Message-Id: <20210324170436.31843-17-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:05:03 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: b1b7e277-f662-46ce-cc02-08d8eee6f79d X-MS-TrafficTypeDiagnostic: SA0PR12MB4382: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4303; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hOJhOxOYCFbSyB0NJebtuAea1MtoG2LJ7gp1PVTG3f0qObori1bcnVZowSrKgQz3oRM1OJ95cB0NJjwbj85Erbr0Da62lvK7cV2aIAZl/GnaWdJ3wHIP2yCxNQeOoiCa1RnDuEySu994cxaKnGlbO4EP4FX2+We4lPzBBGf3T6zv7s6dc5bZfvmpVuNGmxkIRcUnWlKjgBsLWW9hmt1ngE65kQnR60t5Y4d4BsDW9wVrxvGgSgpGtcN/mb9yC/QHW1cUxHX6rJUpOPmMz5yBe2ULSh3oThGjOQa9fAlSLC8OwnmxRVU5HrkPHy4hkpQO9pNfyq9IabnA4NeKeSpk/DGYlOP7Jiv+EgQ7PyrdDXfWICiUEl2+3pDLmmDiwI7u/0GRc8DDTuiCSttMFtFvV9E2FhWxwNtq27OZWmajPRfkYkKFkMXmNZvLSrs8SKJSJ6M05jRMthF776D0uB4zevYFbvsWgLTjp1t2P1JxI7Kx/80JcLNJ24ZvrupcxFudcQT2DLecLDol27H/qczUCFHoClN45J3mtWR/emxyhbNscUQZCchyb+f6+SrdciGqwG2+6Ep1URpALVT31QXWswDDN5XWNiXk1yZAE4kkmBQp+bZG+KxzaeTqXEKaYDFfV7IrHXeXR59KtPulx4xEiA== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(396003)(136003)(376002)(346002)(366004)(39860400002)(6666004)(44832011)(66946007)(1076003)(66556008)(66476007)(956004)(36756003)(478600001)(2616005)(83380400001)(8676002)(2906002)(8936002)(26005)(86362001)(5660300002)(52116002)(7696005)(38100700001)(4326008)(54906003)(7416002)(16526019)(6486002)(316002)(186003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: oXafx1dIypzjScjtVqsXCyP/nJgg5663sHuYMltJtqCsUeVOa7J5SgPoXPNRdWN93Op8yMmMtnsQU069pYP0bpZYPmDFQ8yv44cWOD1Yv54GXINDPRmjjzvmKKvkPLuBGp9QvVcTqNJzGvwemOmMkHwkARGbwY7Q2lROHil1qvHjs68HwEgR67GuO0QjL1J+0uBbMrFliZmjinlazM+mmj2ajkLO8AWDr2Gs3kIwFIyst7Fl0Mws08gOn9gwm8+U08UuDl7t+ifvw9+9w6ke936k75etgW/VVL85SPonuzenC1RtvXW9+WfYqJJvr643Ks/6iuObQ1EIwcW16XAaBxVyQ+Ed42wPebQkbHjym241QmhpaZdrQpVycIfNHhmjWm2+icDJ7kLIiet2g+bEKVETGZ6drL0gl5rZqxxjYVDByYUllUTEcwHihoLim2iqRy2Ri8jlNJDszlLTJpmBL4AeVdj+sOXmLVgK3DXheuuwMSKv2rUa9ZYU1GEMTB9of+bAhyBYBT7CeroU1WOlGcNK8fWxV8onpLKz0IxWhxtdQDnwezpYaOWNLXdG6QHSZMPBHI2MQL2+RpZcUbkPn5At3uyNvZK6PIYKI0LH+wo56/iNcyZVpU1pJthx9jh7iyMVUWxBgqj6fZ2tJw5cNSmZrKVUpfbEZv/E3LnZiDPaCekCUGwQAHu2P3WVd6xA5nrvIdJ3QY8CYDg2H5IF2SV0e18xgWWrAN16ADCb+UKBVs4cSpXF3H6zEQxVhfwLKegSBctRJLotYq3cS0rjy5R3iISN1Sv5N3Pjy4xd1v2SHnlTh48O3Zy5Q7SrmdyWJse6EqDNZK6BKB+Y19y8HkakCE/hp82BojVi35+iAkvTsIf7L+UnZ/N03eqDnj8z+auOhJVr7JS2eCq9QLCtla4tuWTI0xcgZ7VPI4xwwVW5W1u37yYElm3r0FQGnpg4Volf+2pa0HODJAE/T+rdGhfgGb0jWjCFYJoIhdboQaWHfwNpw3ygYQ8XfAn35yo+iETJL81y067qmces2EqnMrn7UqZWoR9Z9MEzSSEo7/JDpT6cV0Gby1eM0ZcjhZS7T1olE8hYhWXe+gijkbflmtiwl3lsb2ci+0zMiOb+Kyw+S0S8GbH1dqARGozbO+P460ugmnVGitc5EJ7kcQitDkARc+UtN8+3ieWbQw8T/hgo9nNEQ8nJLrEH9M8vbJyJx5cmgacmOA9gG8yf2yvvka90jFhSabQHmN3xx3AWAMC22InzALYxjPG4wSQKCefJyNgYYSgTDLVaEZVqav4MmjKFXYLUzti1cHi0j2pkdhCtx7SOiQv1Br1gPw2s+WXF X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: b1b7e277-f662-46ce-cc02-08d8eee6f79d X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:05:03.8786 (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: jt2hguuXBGVTMl2wW9y5a2ojvstMtcpybB/swUuiV6ZMqrcMxJtSKsEZ0lB4F+Q7z+ED2QkDLMDpIYplUYJ6vw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4382 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The KVM_SNP_INIT command is used by the hypervisor to initialize the SEV-SNP platform context. In a typical workflow, this command should be the first command issued. When creating SEV-SNP guest, the VMM must use this command instead of the KVM_SEV_INIT or KVM_SEV_ES_INIT. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: Vitaly Kuznetsov Cc: Wanpeng Li Cc: Jim Mattson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/kvm/svm/sev.c | 41 ++++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/svm/svm.c | 5 +++++ arch/x86/kvm/svm/svm.h | 1 + include/uapi/linux/kvm.h | 3 +++ 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 4d5be5d2b05c..36042a2b19b3 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -189,7 +189,10 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) if (asid < 0) return ret; - ret = sev_platform_init(&argp->error); + if (sev->snp_active) + ret = sev_snp_init(&argp->error); + else + ret = sev_platform_init(&argp->error); if (ret) goto e_free; @@ -206,12 +209,19 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) static int sev_es_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) { + int ret; + if (!sev_es) return -ENOTTY; + /* Must be set so that sev_asid_new() allocates ASID from the ES ASID range. */ to_kvm_svm(kvm)->sev_info.es_active = true; - return sev_guest_init(kvm, argp); + ret = sev_guest_init(kvm, argp); + if (ret) + to_kvm_svm(kvm)->sev_info.es_active = false; + + return ret; } static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error) @@ -1042,6 +1052,23 @@ static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp) return ret; } +static int sev_snp_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + int rc; + + if (!sev_snp) + return -ENOTTY; + + rc = sev_es_guest_init(kvm, argp); + if (rc) + return rc; + + sev->snp_active = true; + + return 0; +} + int svm_mem_enc_op(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -1092,6 +1119,9 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp) case KVM_SEV_LAUNCH_SECRET: r = sev_launch_secret(kvm, &sev_cmd); break; + case KVM_SEV_SNP_INIT: + r = sev_snp_guest_init(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; @@ -1955,6 +1985,13 @@ int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in) svm->ghcb_sa, svm->ghcb_sa_len, in); } +void sev_snp_init_vmcb(struct vcpu_svm *svm) +{ + struct vmcb_save_area *save = &svm->vmcb->save; + + save->sev_features |= SVM_SEV_FEATURES_SNP_ACTIVE; +} + void sev_es_init_vmcb(struct vcpu_svm *svm) { struct kvm_vcpu *vcpu = &svm->vcpu; diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 13df2cbfc361..72fc1bd8737c 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1281,6 +1281,11 @@ static void init_vmcb(struct vcpu_svm *svm) /* Perform SEV-ES specific VMCB updates */ sev_es_init_vmcb(svm); } + + if (sev_snp_guest(svm->vcpu.kvm)) { + /* Perform SEV-SNP specific VMCB Updates */ + sev_snp_init_vmcb(svm); + } } vmcb_mark_all_dirty(svm->vmcb); diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 9e8cd39bd703..9d41735699c6 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -604,6 +604,7 @@ void sev_es_vcpu_load(struct vcpu_svm *svm, int cpu); void sev_es_vcpu_put(struct vcpu_svm *svm); void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector); struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu); +void sev_snp_init_vmcb(struct vcpu_svm *svm); /* vmenter.S */ diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 374c67875cdb..e0e7dd71a863 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1594,6 +1594,9 @@ enum sev_cmd_id { /* Guest certificates commands */ KVM_SEV_CERT_EXPORT, + /* SNP specific commands */ + KVM_SEV_SNP_INIT, + KVM_SEV_NR_MAX, }; From patchwork Wed Mar 24 17:04:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408314 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2C629C43460 for ; Wed, 24 Mar 2021 17:06:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 022EB61A25 for ; Wed, 24 Mar 2021 17:05:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237164AbhCXRFk (ORCPT ); Wed, 24 Mar 2021 13:05:40 -0400 Received: from mail-dm6nam11on2071.outbound.protection.outlook.com ([40.107.223.71]:41848 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237054AbhCXRFI (ORCPT ); Wed, 24 Mar 2021 13:05:08 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=C5+6fLq+YPpJfF2UhHuAWVYVXSdpfYtNd+Cr9/aGtL8t7byQosvF5p6Mni1CQC1UtbsYfK0vHSFhFTZjpHqnf3RFSY8szgcIXY6VtKaeftS3W8iyjCKjql3aU6LtX78C0bB//eO9Y9iYg2KhFJOA5/0VzwOhk4VL0WCRVr4fYglM0P0aDcyby2268PtbRNnPA4CiDHcnybej+pLABFDMD4gPe65rcTNHqSmGvGyKBBKd1SIYCUxBnbAUv2fKUYSZDok/a8rRiJ2vx0sd2cFjcnLTJJ2Wdw2z6O/gaI99RVzwrZ2uSboyiFMaRmtX/NWX0J90JKjuLxwD+HCeVrejtw== 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-SenderADCheck; bh=62dt+AxoNHZGDqiA1RV1K24dKu7zv7Iirhr+Lsn5cYM=; b=EQAl/TZrbxfdTrsPiOcNNhO6BVKUigEo4HANNez9w1z+c0WRDW8qsiD20RtMtxaKt3jOZmgdXa3gOanx5svsKK7Yh2sp6snYAqQljYGZAln7CHrxHlMTRoNrG7IjBOHluDdj97BIgr1vh6ky4cj58v3kgnnFE/GwdlvldEQwvrNhNIFsn0h01e4Agfo0gfiiLVom8FBAuHpLMY1ARSCd2FPEuFUSLwLAOoS3w/5Oq7d47lPzfwlS0droxrXiKj20RAy+1TUo10qBKX/KRrzpGG2AInClH6GKd2IgVofke3iOVtFSowmgjmrdugalP8emmQDQKMW5CfqyyvlKl4e3YQ== 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=62dt+AxoNHZGDqiA1RV1K24dKu7zv7Iirhr+Lsn5cYM=; b=lwIywfEWhg9cb0Xuel6DVMIojvSlr/kM7H0cb1pu5T/dSfrENlJWR2yY6GAu7zjFZSjOdMtN/0lIf3+QQtI4K4Acest8a0bySibDqNfuJAUhV/arF+gFNrmDdwxWE3Ky+Fm+AtrteP1odnZYpkZrA34zYRFkCah0m0pmmF9bjZc= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4382.namprd12.prod.outlook.com (2603:10b6:806:9a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25; Wed, 24 Mar 2021 17:05:05 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:05:05 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson Subject: [RFC Part2 PATCH 17/30] KVM: SVM: add KVM_SEV_SNP_LAUNCH_START command Date: Wed, 24 Mar 2021 12:04:23 -0500 Message-Id: <20210324170436.31843-18-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:05:04 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 9ff53f3b-06cb-4d23-6e9c-08d8eee6f838 X-MS-TrafficTypeDiagnostic: SA0PR12MB4382: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:5797; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: NinE5UI/msX0HYZ8iOSc3L8Hd/iiKgxClDkwjJdCt9wfNtszp8ajXL35H4G1asfZHlKvrckkUxn3+Kda635FXHlgj2PGbFIJcPpWinmFbZ6rJu2zw+Qa9PjJvfF3tLod3rUnjKKC53OB5URE5d9UxBd2uo82gVHY4CE9Zb17FesGfnnoZRP6P2nZ4aadtOHyWGKfD4Pi6Ay09GUmXaFSbah2neNoT2/Za7i9NuLSoWjE+ieQNN3pADHarY7RGtdo3H6Il72UaCP5ekbKMmVIYAoPVxjAwpu10/klVd9ikt25D2LYQKCgOYWzd7purqqAAFzDu2uwhuvWQ5FRyePv9haPgb5oprhu+3DdXeoNv653iJCY06DB0pqM8jjNJal2qT7oLehY4UeNUFNsEbSmpEX0VHSvPS5qyOWLHYkpavT4pqLbPSOj9/JMyOUK3mMS+THcxSKaCuTS88tB0D4Xexay5kFvKSFxerJy20kjzskBWQhd7tMkAI6/A+OE0kBWzFzQ6aSjiwD9hQCcnrRM/1pvbx5W5m4EH3dQT8PSEGe63zDCD/lTLEB0qchKpTTRpg+4/3HZ/bky8I8OhdNQP42/Ljdpy+gQ7k+4XogHIaN9dzs1LEaInl6MFz2xAY/B2z+fdKaOppGtgfbEbmZ9zg== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(396003)(136003)(376002)(346002)(366004)(39860400002)(6666004)(44832011)(66946007)(1076003)(66556008)(66476007)(956004)(36756003)(478600001)(2616005)(83380400001)(8676002)(2906002)(8936002)(26005)(86362001)(5660300002)(52116002)(7696005)(38100700001)(4326008)(54906003)(7416002)(16526019)(6486002)(316002)(186003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: J+rBy8j4YzjeARwPvGlMeqNOkvrPyZD6AJZYa94mo77WUfahhMSDQVLzHBfI65OGLmX21kqe0pdTHbYjQlznqJN2+kUvXNokjULDU6T/dzqcX/45a07Q/nUac4Te4vy1pYXL5lMGW8q+uqxPAqwuHc1A2Fn1GVILzZzBIoi8ZIvbFnxATfw4rw+nP4cHjkCtJkGOWh3MIG0wcL+Eya4ZvMy0OkQim82B4zyugC19i7cDg7RG1xBis3xrDVYnRfsnsY/mjsOsnxUWl6rs+a31PUlZpgahDSnfdHNrvUdid5HrToqPt25Scs+vchDkP386/+Xoq4hjmgHwn6IhPtJwmzHqOaFOqSmUVNt0YNfVvBAKjWYhVTvhdiMONXul1e9YZg3wBTWKCs0IPe0+rwJjlyKdytORAVudKL60SPT1gtwr0+2pKDDFVaHSuv5PVF9TN3NimsBnoT3OmpqN9poRsOFENSn4zyg6gOmyXHea665iUmb2vgnx1gxLtCT3EjmQ81ltgHEXvCLvLLOY0iCXEsWH8w0L6119roByiYIwOCdnGf6aTNxh14IfDjqsvmHU+Vei767oyQsAQFxmlH68OjXs23qiy0u1UaqsuxgpW2m+eOyZBmxWAmVIaIZP2VRJYL1gZJQ2diTpp60fnu7ig8Tr8zaJJaCI8qVYXYzZuC/L5RghupSGINV7OgjJF/0IW+fQo2BHkY/vW93Em2XIhTqsA+WqT1GA8fRBGpN0pXSH5IpBnLApDysqDNtOOzF4++UQxICGaG29vn2FA37Q8rb3Mj6gJJwhmb8qwK+5+/6tiTZlLapVk96cUWi8QFmJACkNWvIeG0RLROTLD5sge9mu8HXXBbEb64AfYuSmWPB4INXm70kyOdcgoYLy73mg6IF2aSA2ux1O1HCIoH7YHIvH8DbckbhgXO7RTvie8Fb+AGTzZiXpnYk8TRFR0mWlDxppA9mXfKum/Z9OcShXGnJLneg6rZ7qu6RKJY55oP2dBFO4gtMLUZ1p394Jq6NyqGoNtUBbQ7SJEEWZIL+1R7LyO2W3gEVVByIQYUrk3vyePMKr5ql3Rw7NOXl8Qv1l63Q3DChf9JPQsZyJhW8H+9zTZPIgN6zFzeCDnRukFunN9FePOl5rEfs9QjQRloaf3wBLyelDkQwAXvkHGGiKgJ5WemX7S78laUiAhTkgsu1oJN59mPmms9Lgg5z36oOhTu1Wixlhw8WSYOJUpbIz+mPEp8UTYXwnMc4eGR/uRjjz0BrOFd4QmefKF+j85faXEXvBs3Ik4jEWsdAZ3LPInAwhZyEOsGDAPdws9BPV0YL5t/NG3muW6K0rfqFUqEdw X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9ff53f3b-06cb-4d23-6e9c-08d8eee6f838 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:05:04.9480 (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: e3gQyYaR0ABzX8lZms1qiuviHfVplMo7PzykyT7/XPzvD+E7eGStGiLODArFBTfJlz5mg0ouAcL7IeSuaSZI5Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4382 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org KVM_SEV_SNP_LAUNCH_START begins the launch process for an SEV-SNP guest. The command initializes a cryptographic digest context used to construct the measurement of the guest. If the guest is expected to be migrated, the command also binds a migration agent (MA) to the guest. For more information see the SEV-SNP spec section 4.5 and 8.11. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: Vitaly Kuznetsov Cc: Wanpeng Li Cc: Jim Mattson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/kvm/svm/sev.c | 221 ++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/svm/svm.h | 1 + include/uapi/linux/kvm.h | 8 ++ 3 files changed, 229 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 36042a2b19b3..7652e57f7e01 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -37,6 +37,9 @@ static unsigned int min_sev_asid; static unsigned long *sev_asid_bitmap; static unsigned long *sev_reclaim_asid_bitmap; +static void snp_free_context_page(struct page *page); +static int snp_decommission_context(struct kvm *kvm); + struct enc_region { struct list_head list; unsigned long npages; @@ -1069,6 +1072,181 @@ static int sev_snp_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) return 0; } +static int snp_page_reclaim(struct page *page, int rmppage_size) +{ + struct sev_data_snp_page_reclaim *data; + struct rmpupdate e = {}; + int rc, error; + + data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); + if (!data) + return -ENOMEM; + + data->paddr = __sme_page_pa(page) | rmppage_size; + rc = sev_snp_reclaim(data, &error); + if (rc) + goto e_free; + + rc = rmptable_rmpupdate(page, &e); + +e_free: + kfree(data); + + return rc; +} + +static void snp_free_context_page(struct page *page) +{ + /* Reclaim the page before changing the attribute */ + if (snp_page_reclaim(page, RMP_PG_SIZE_4K)) { + pr_info("SEV-SNP: failed to reclaim page, leaking it.\n"); + return; + } + + __free_page(page); +} + +static struct page *snp_alloc_context_page(void) +{ + struct rmpupdate val = {}; + struct page *page = NULL; + int rc; + + page = alloc_page(GFP_KERNEL); + if (!page) + return NULL; + + /* Transition the context page to the firmware state.*/ + val.immutable = 1; + val.assigned = 1; + val.pagesize = RMP_PG_SIZE_4K; + rc = rmptable_rmpupdate(page, &val); + if (rc) + goto e_free; + + return page; + +e_free: + __free_page(page); + + return NULL; +} + +static struct page *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct sev_data_snp_gctx_create *data; + struct page *context = NULL; + int rc; + + data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); + if (!data) + return NULL; + + /* Allocate memory for context page */ + context = snp_alloc_context_page(); + if (!context) + goto e_free; + + data->gctx_paddr = __sme_page_pa(context); + rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, data, &argp->error); + if (rc) { + snp_free_context_page(context); + context = NULL; + } + +e_free: + kfree(data); + + return context; +} + +static int snp_bind_asid(struct kvm *kvm, int *error) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_activate *data; + int asid = sev_get_asid(kvm); + int ret, retry_count = 0; + + data = kmalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); + if (!data) + return -ENOMEM; + + /* Activate ASID on the given context */ + data->gctx_paddr = __sme_page_pa(sev->snp_context); + data->asid = asid; +again: + ret = sev_issue_cmd(kvm, SEV_CMD_SNP_ACTIVATE, data, error); + + /* Check if the DF_FLUSH is required, and try again */ + if (ret && (*error == SEV_RET_DFFLUSH_REQUIRED) && (!retry_count)) { + /* Guard DEACTIVATE against WBINVD/DF_FLUSH used in ASID recycling */ + down_read(&sev_deactivate_lock); + wbinvd_on_all_cpus(); + ret = sev_guest_snp_df_flush(error); + up_read(&sev_deactivate_lock); + + if (ret) + goto e_free; + + /* only one retry */ + retry_count = 1; + + goto again; + } + +e_free: + kfree(data); + + return ret; +} + +static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_launch_start *start; + struct kvm_sev_snp_launch_start params; + int rc; + + if (!sev_snp_guest(kvm)) + return -ENOTTY; + + if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) + return -EFAULT; + + /* Initialize the guest context */ + sev->snp_context = snp_context_create(kvm, argp); + if (!sev->snp_context) + return -ENOTTY; + + rc = -ENOMEM; + start = kzalloc(sizeof(*start), GFP_KERNEL_ACCOUNT); + if (!start) + goto e_free_context; + + /* Issue the LAUNCH_START command */ + start->gctx_paddr = __sme_page_pa(sev->snp_context); + start->policy = params.policy; + rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_START, start, &argp->error); + if (rc) + goto e_free_context; + + /* Bind ASID to this guest */ + sev->fd = argp->sev_fd; + rc = snp_bind_asid(kvm, &argp->error); + if (rc) + goto e_free_context; + + goto e_free_start; + +e_free_context: + snp_decommission_context(kvm); + +e_free_start: + kfree(start); + + return rc; +} + int svm_mem_enc_op(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -1122,6 +1300,9 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp) case KVM_SEV_SNP_INIT: r = sev_snp_guest_init(kvm, &sev_cmd); break; + case KVM_SEV_SNP_LAUNCH_START: + r = snp_launch_start(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; @@ -1241,6 +1422,36 @@ int svm_unregister_enc_region(struct kvm *kvm, return ret; } +static int snp_decommission_context(struct kvm *kvm) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_decommission *data; + int ret; + + /* If context is not created then do nothing */ + if (!sev->snp_context) + return 0; + + data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); + if (!data) + return -ENOMEM; + + data->gctx_paddr = __sme_page_pa(sev->snp_context); + ret = sev_guest_snp_decommission(data, NULL); + if (ret) { + pr_err("SEV-SNP: failed to decommission context, leaking the context page\n"); + return ret; + } + + /* free the context page now */ + snp_free_context_page(sev->snp_context); + sev->snp_context = NULL; + + kfree(data); + + return 0; +} + void sev_vm_destroy(struct kvm *kvm) { struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; @@ -1273,7 +1484,15 @@ void sev_vm_destroy(struct kvm *kvm) mutex_unlock(&kvm->lock); - sev_unbind_asid(kvm, sev->handle); + if (sev_snp_guest(kvm)) { + if (snp_decommission_context(kvm)) { + pr_err("SEV-SNP: failed to free guest context, leaking asid!\n"); + return; + } + } else { + sev_unbind_asid(kvm, sev->handle); + } + sev_asid_free(sev->asid); } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 9d41735699c6..97efdca498ed 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -80,6 +80,7 @@ struct kvm_sev_info { unsigned long pages_locked; /* Number of pages locked */ struct list_head regions_list; /* List of registered regions */ u64 ap_jump_table; /* SEV-ES AP Jump Table address */ + struct page *snp_context; /* SNP guest context page */ }; struct kvm_svm { diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index e0e7dd71a863..84a242597d81 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1596,6 +1596,7 @@ enum sev_cmd_id { /* SNP specific commands */ KVM_SEV_SNP_INIT, + KVM_SEV_SNP_LAUNCH_START, KVM_SEV_NR_MAX, }; @@ -1648,6 +1649,13 @@ struct kvm_sev_dbg { __u32 len; }; +struct kvm_sev_snp_launch_start { + __u64 policy; + __u64 ma_uaddr; + __u8 ma_en; + __u8 imi_en; +}; + #define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0) #define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1) #define KVM_DEV_ASSIGN_MASK_INTX (1 << 2) From patchwork Wed Mar 24 17:04:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408311 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3F17DC43461 for ; Wed, 24 Mar 2021 17:06:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 221A161A1A for ; Wed, 24 Mar 2021 17:06:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237161AbhCXRFk (ORCPT ); Wed, 24 Mar 2021 13:05:40 -0400 Received: from mail-dm6nam11on2071.outbound.protection.outlook.com ([40.107.223.71]:41848 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237056AbhCXRFJ (ORCPT ); Wed, 24 Mar 2021 13:05:09 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SyOzjPCOx7xtOy+bT7FHIer2uknP4s9m++Hq6bBQ/G0+cYlS+sbOsxKEozqye+WH0Wb5mdwvMp2az3HT614Y6uXPQIx+VwGCVuMYf7Ftc+XO6RX3e+i9ntcWB46qWob6NLnMIGuBbqmZRDn1WTuWFwgdr3sp+k3orehIa5zyh2XUFcak9sqRxvdq4eew+XqIA3SmLbEoubGdTsttrDW/Y7W3nizaoXDTc3c9Gj/quiIVBsX82R0AEPyInUyvhBm9t6Sq9BcOrroJR8xijow8JtcJA4aa/i4aXfPfQgM2pD/YopzgykSLjJycvsFo55pqlcuckoiNvmqh08aR4ZQpTw== 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-SenderADCheck; bh=3jxrfGjRV0UlSbWizl9lwESjUMQ6w/6OcWGsn+CeVVk=; b=Z9xsHf9aSDJ9di3pCuPTUvCv23GeFjZp6TuBxp05dEjM0lIF11sxHSq+aZxy4yovYA+Fi9ZsWEpPZt9eNvDB3O13hPg33+3dYOtpvXJSmUJgrDwMc3+SkAz9qV6lf86HBG/zyeZN6Q2fA12qmO9CmcYdQxjRwQKa08YYm9oM+UNtsLx23qUSVpvg8sbacSefHIeRbr3TIzPfuhzXpV6uhQlza2UOfioBZjdnT41trKc2cIQZkoaD6oL9mTYlyXnUaBXOv3wmQ+LflXedDiBxy5ouOezremXGKrbBfSTcJ6XC58vo7CmmQhXJSwGHui8NVetdUSAGoklt/QTtjdMtaA== 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=3jxrfGjRV0UlSbWizl9lwESjUMQ6w/6OcWGsn+CeVVk=; b=GKSMtYSXJah68GU5aBEt3Enkw1JeTAbGHhXHYVcs2VFy9bOOXJkKE6UsLZ89sOqLeAvydQRHwws72PfGcCL4c2nGIU2akfkFIw3A/wLoq1mhvihDUIw8aJPhhxlfxP1o6/ohUFRbbbAGVJ5BNHX8Bew653Po5QNuljB9az3/0kM= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4382.namprd12.prod.outlook.com (2603:10b6:806:9a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25; Wed, 24 Mar 2021 17:05:07 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:05:05 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson Subject: [RFC Part2 PATCH 18/30] KVM: SVM: add KVM_SEV_SNP_LAUNCH_UPDATE command Date: Wed, 24 Mar 2021 12:04:24 -0500 Message-Id: <20210324170436.31843-19-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:05:05 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 4c364de6-c7a7-48cd-3138-08d8eee6f8bc X-MS-TrafficTypeDiagnostic: SA0PR12MB4382: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3173; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cICYGtNTxOEC0DpunZ65mdwTMu2CCisGTLw7qC85frPgom7phr7DXehsBdlNQjFzVLfm8KFj7ljW0SRWSoXWho3IevUmeZI8p3lBAEHbPZ1k2spZpB5u/329waKLygC7ntSni85Ni28s5li5lf70gK2JDsmAuMvQEgWrLrgX+5gFe7uqR5Lc19JycNuN4xOj2WTKPxEn0DRLgvAXEcnqkGTn/wMQOIXxX9yPuLQx07KGl2Z5cUcoz4Ib7IsGtu9XDS4DwgsiuoXVmKGYG4b4VAMLE2BOFfHs6Et6CqF1iYw9vpXYYy8i6EvysFVuUqBfdcnp8/faSlNL0+MlIVJLzZzrUsLtUOLBIjLcyMi64AnwjZje69eWFXqyAEY1O+uB0fCW8fgaFhj5CKIFyXXfBErxzSU54cUti1dkZZW5zDXiGPuuncbbGGkkz3fO7lXf8DGDpacpONsWU5XVyo4OKMGqOUrhCtfOHtSHvFE4BEC1wjS4U2w5IbOUsuHsJd6ZiqVtjI6ht2KAx5mGPI02JC0VKECVdWE9iLFJSl0Q2yc4QCGibPKanX05LH/0owUW1/9oC7D439Uort/IkaIiWqjmAveZXQL6MpWhosN9qcTfIQm1Y+7snR3Pd8d55pi+zfY5/rEpwxSb9OdweCqHPA== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(396003)(136003)(376002)(346002)(366004)(39860400002)(6666004)(44832011)(66946007)(1076003)(66556008)(66476007)(956004)(36756003)(478600001)(2616005)(83380400001)(8676002)(2906002)(8936002)(26005)(86362001)(5660300002)(52116002)(7696005)(38100700001)(4326008)(54906003)(7416002)(16526019)(6486002)(316002)(186003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: eizz2Xrd5r/Js+aDANTzwnkoQPW7bWqRxc8ouU9YLUdhUzvcG1zsZdsbr7cftLg4R7vet5UPKEummMau6JxCeU0/eLpYyy4a88bAvl2KJqdhcWFpferHUtQl7+Z0gwZZReNq5H7NqBWg0yVMD3yiONjDyy1KR6eWwgFnIugfBngQ/sTzWHQX/6AcyIPFbUse24AA/W7LkTRyUqtYIWtuI8yhH0aDo61l8g7YAD3sezu7qqqvBr7hG6+6FFT/f/XlCvsvM3mpP4vXZRbZ81LXkmU1h1f2hxjkJ1nBGdXL2I2YlzoPIyaR7DB4kWbzcvjBYPLMPAgzChgRmMpieq/b/L4QkshPGnwffoA4PM5ikFGVyE52IfWy97pNdqzXMuToEZpyTLNIE5XwRLji4s04JEIhbniFxtfoKxtZ1PsMsBbxfB2wfBoBW//60x/commINm8irBkYP+mHVRwQ2Yx4P72adtGnjphkcnF9oixV1bsGk3w33nPuwrqH05DH0qKTqAPmQGPX0nBgWoxl3z/OAx+Qp9S+nWSkU7Ts0zQ0rtJYzr6fL7YsiJskJLUJTc3VOfW7+Qxf7VGsENwNl6N0uLMsxtswg6YeVagIJqqtb5gXbv+nuV7aoeplOcK8K3OYAXhK8BjwiWoLGXUIjxNrthWiYqC4/v5OA835q4r+3OtQ3EmPS3DBR7oo4VO44SPC5QE1nV3c8Bp7IRABaIqrU7xCZglFsjACVcshXsctxejZvbS+ccTm3ixgjpc/7/rjaDkkcPT4lKp7zu6WxVCquQeKz6k9sHTMWBn2695fvo9aN49p2OEm3aKJsKwLzKXuBIQwfth02NqQXVEDFAThge7doCqkRxLWB/EC7cQ7KqwRirC2JqzBTrqqwul7kLN74u+HNOL4b3YXcd6Ar8shIvPHR87IjCtBK8C/ugsM+V2mc88eAMR4OJrQo5RWQlqegXma3pve0vAytt9h0nrrVvERE9cRkdTzDF1JErvaI6LF9rPF83a/hzb9z9KKDXka3l5S9RGh9pKJsCrMMXE2DmXDbH1HAdCZpkcCJwN6GZ/Nj63ReTWehgb09QRTTl88+AMYOSsWlV6TW0ZbddpeU7nIWM6r0PuKNgPsrqVSGgFJ402Scpp9ow1fn/XQ4GgdYMqGZJBVqxQ1v1e9DtoZnduzTXdqpAv17AGnz/X/GlJ05+vzhLhH9FIo8u9IDfCO7BCFWIJBewCl3gki2SIOLJGug8lYxEzNIHkAqOIiuFiwcbkBnAkKhkblQlKM89ZOMHs2i8ldt99Xfk0jgwVWtTtjQ+v7kbN4v1tA04n3fOCpm58ybrAv6PBUDorkVakw X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4c364de6-c7a7-48cd-3138-08d8eee6f8bc X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:05:05.7485 (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: 9Y7xhmEimQflMxnMKV62ccWd6Ky5LY50mvcRtg2DSCp+9f11O+He9uQolyMvvyoRax+X9Ailxvmq3oEMDVancg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4382 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The KVM_SEV_SNP_LAUNCH_UPDATE command can be used to insert data into the guest's memory. The data is encrypted with the cryptographic context created with the KVM_SEV_SNP_LAUNCH_START. In addition to the inserting data, it can insert a two special pages into the guests memory: the secrets page and the CPUID page. For more information see the SEV-SNP spec section 4.5 and 8.12. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: Vitaly Kuznetsov Cc: Wanpeng Li Cc: Jim Mattson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/kvm/svm/sev.c | 136 +++++++++++++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 18 ++++++ 2 files changed, 154 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 7652e57f7e01..1a0c8c95d178 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -1247,6 +1247,139 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) return rc; } +static struct kvm_memory_slot *hva_to_memslot(struct kvm *kvm, unsigned long hva) +{ + struct kvm_memslots *slots = kvm_memslots(kvm); + struct kvm_memory_slot *memslot; + + kvm_for_each_memslot(memslot, slots) { + if (hva >= memslot->userspace_addr && + hva < memslot->userspace_addr + (memslot->npages << PAGE_SHIFT)) + return memslot; + } + + return NULL; +} + +static bool hva_to_gpa(struct kvm *kvm, unsigned long hva, gpa_t *gpa) +{ + struct kvm_memory_slot *memslot; + gpa_t gpa_offset; + + memslot = hva_to_memslot(kvm, hva); + if (!memslot) + return false; + + gpa_offset = hva - memslot->userspace_addr; + *gpa = ((memslot->base_gfn << PAGE_SHIFT) + gpa_offset); + + return true; +} + +static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + unsigned long npages, vaddr, vaddr_end, i, next_vaddr; + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_launch_update *data; + struct kvm_sev_snp_launch_update params; + int *error = &argp->error; + struct kvm_vcpu *vcpu; + struct page **inpages; + struct rmpupdate e; + int ret; + + if (!sev_snp_guest(kvm)) + return -ENOTTY; + + if (!sev->snp_context) + return -EINVAL; + + if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) + return -EFAULT; + + data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); + if (!data) + return -ENOMEM; + + data->gctx_paddr = __sme_page_pa(sev->snp_context); + data->vmpl1_perms = 0xf; + data->vmpl2_perms = 0xf; + data->vmpl3_perms = 0xf; + + /* Lock the user memory. */ + inpages = sev_pin_memory(kvm, params.uaddr, params.len, &npages, 1); + if (!inpages) { + ret = -ENOMEM; + goto e_free; + } + + vcpu = kvm_get_vcpu(kvm, 0); + vaddr = params.uaddr; + vaddr_end = vaddr + params.len; + + for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i++) { + unsigned long psize, pmask; + int level = PG_LEVEL_4K; + gpa_t gpa; + + if (!hva_to_gpa(kvm, vaddr, &gpa)) { + ret = -EINVAL; + goto e_unpin; + } + + psize = page_level_size(level); + pmask = page_level_mask(level); + gpa = gpa & pmask; + + /* Transition the page state to pre-guest */ + memset(&e, 0, sizeof(e)); + e.assigned = 1; + e.gpa = gpa; + e.asid = sev_get_asid(kvm); + e.immutable = true; + e.pagesize = X86_RMP_PG_LEVEL(level); + ret = rmptable_rmpupdate(inpages[i], &e); + if (ret) { + ret = -EFAULT; + goto e_unpin; + } + + data->address = __sme_page_pa(inpages[i]); + data->page_size = e.pagesize; + data->page_type = params.page_type; + ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_UPDATE, data, error); + if (ret) { + snp_page_reclaim(inpages[i], e.pagesize); + goto e_unpin; + } + + next_vaddr = (vaddr & pmask) + psize; + } + +e_unpin: + /* Content of memory is updated, mark pages dirty */ + memset(&e, 0, sizeof(e)); + for (i = 0; i < npages; i++) { + set_page_dirty_lock(inpages[i]); + mark_page_accessed(inpages[i]); + + /* + * If its an error, then update RMP entry to change page ownership + * to the hypervisor. + */ + if (ret) + rmptable_rmpupdate(inpages[i], &e); + } + + /* Unlock the user pages */ + sev_unpin_memory(kvm, inpages, npages); + +e_free: + kfree(data); + + return ret; +} + int svm_mem_enc_op(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -1303,6 +1436,9 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp) case KVM_SEV_SNP_LAUNCH_START: r = snp_launch_start(kvm, &sev_cmd); break; + case KVM_SEV_SNP_LAUNCH_UPDATE: + r = snp_launch_update(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 84a242597d81..a9f7aa9e412d 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1597,6 +1597,7 @@ enum sev_cmd_id { /* SNP specific commands */ KVM_SEV_SNP_INIT, KVM_SEV_SNP_LAUNCH_START, + KVM_SEV_SNP_LAUNCH_UPDATE, KVM_SEV_NR_MAX, }; @@ -1656,6 +1657,23 @@ struct kvm_sev_snp_launch_start { __u8 imi_en; }; +#define KVM_SEV_SNP_PAGE_TYPE_NORMAL 0x1 +#define KVM_SEV_SNP_PAGE_TYPE_VMSA 0x2 +#define KVM_SEV_SNP_PAGE_TYPE_ZERO 0x3 +#define KVM_SEV_SNP_PAGE_TYPE_UNMEASURED 0x4 +#define KVM_SEV_SNP_PAGE_TYPE_SECRETS 0x5 +#define KVM_SEV_SNP_PAGE_TYPE_CPUID 0x6 + +struct kvm_sev_snp_launch_update { + __u64 uaddr; + __u32 len; + __u8 imi_page; + __u8 page_type; + __u8 vmpl3_perms; + __u8 vmpl2_perms; + __u8 vmpl1_perms; +}; + #define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0) #define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1) #define KVM_DEV_ASSIGN_MASK_INTX (1 << 2) From patchwork Wed Mar 24 17:04:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408960 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8CC8AC433E0 for ; Wed, 24 Mar 2021 17:06:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7004461A12 for ; Wed, 24 Mar 2021 17:06:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237167AbhCXRFm (ORCPT ); Wed, 24 Mar 2021 13:05:42 -0400 Received: from mail-dm6nam11on2071.outbound.protection.outlook.com ([40.107.223.71]:41848 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237057AbhCXRFK (ORCPT ); Wed, 24 Mar 2021 13:05:10 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jSQQpivSXDWSsjZVkEkPAuzs63+GW2wcPkSvIbfnkLMVuFfDwCKg0J6z8/pqGDPDy9fmxEK9ncDu00/GIcUMcjS3V+7aKAOLfk+0cj06K4GeR6StT/pu9wLSKYDZMfKyU6DiDX4XCa5lcZgwNc7CIuFHeRF7eMrROaZlGEmgqwriNIAToQuMQfrPb1VbVsU5WpIjAE9AwxQLB+2lM3xGSNrpOS0FRA53XbuYP+A+y6YdT+Uyb584voLCbiXfZatSAXcbq7cD541HnH1PAvwLpDE+w4GTHVnw5LGFTMHJ2GxMwAb5FAgVXEhx8U1kscOu10vx9QDfqU5Rg/xflMKd6Q== 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-SenderADCheck; bh=eEeQVYXvtWtNQ+zNaDBS4s6QtTIRTEqfNIi5bXd9meY=; b=GsmtvsLw4lg1+/MxqdzfI6PhLcyPluxr/rzHTkuH+a5movJ3jHt1Zd1lD4cWHMdjq7DmtVFMRVhKsN/YFP+mRz2ySHekMv2eyeYfJOswJCqMWQhruL7ehlnd3wTkiLP6nMcuE/hmAGByfQeHxUOf2DeCdjTKleysQ0Cl3JcLUL1EGe9Y/LBKQy3aUwljOg8CsLBBWW+f9Nzu2O1XmFRSx851eN5goEApJOMfPxbYrVdjzOCDQWsTKEtTTeLThEFiVNb3fbYdTSwbOg26rlvbZSUre6jXmCMi0dqfrI7PwsNlEu5F/EwuZiJo7ugxg7SzMCy02oVt7j1YdkQvPNeSmg== 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=eEeQVYXvtWtNQ+zNaDBS4s6QtTIRTEqfNIi5bXd9meY=; b=WrjxtaicxZCGDPdDMpwuzVys3zDqRKpuQ9+AT+eyyfr1YBPyDsBt+BKR9j8fLmsoblKjjAhSlorvNO9IJAHxyBD+T/EoHomHtIeppGpqwaHARgY31DvQgKPn0syg7yrC/7yCbBFqaVmM0uJrvxOH6XP4y00ZYungIHzHRoW7upw= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4382.namprd12.prod.outlook.com (2603:10b6:806:9a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25; Wed, 24 Mar 2021 17:05:07 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:05:07 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson Subject: [RFC Part2 PATCH 19/30] KVM: SVM: Reclaim the guest pages when SEV-SNP VM terminates Date: Wed, 24 Mar 2021 12:04:25 -0500 Message-Id: <20210324170436.31843-20-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:05:05 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 16fc2ff5-b5da-447c-1f5d-08d8eee6f93e X-MS-TrafficTypeDiagnostic: SA0PR12MB4382: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7691; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: oZjWH/hMH0ykAAftn6j0VqPHfkjAcd5HcEY5ojGKaYcnHltY7CC+4QrCjl/agvZOXFxQHjZ7VF/ItO9a4rlB9dCOZ1WdBRd0bLv2fw59938/DofKQVPDMJNt1H4FuYZVXugliSOinkt8tzrzDL2051fItXRWRe2g4SlK/e8Fd85LMjReWbv3M4YT4q+fHJL3uSi8DHaH3vqh14M3Tn49b5fStBPT+3htVf+QCCrByygHut2IbBBR2EZVla6ETGV4pUrecML/X0FhXljDv7H6D+fezWWVEvFq1ut6wZbkZld4YBNvY9AMBxYpEl/068FUDyu1HPtxYVlW8CWyKNgoONPXwoIAjMJK90k1A7xUkAaxVAQtgwYMm0bPx2eWhb+cEmOpxzcvDsUmhaf0ULQfQYLVrnhYL2d1C8iTunpXhajlqUNGjzeK3RYjpsQM3v675kCOnR/BhCIx81NB2oV6hW3TzKPsYX43eyQKE0i/75XmabyrCIcnE9SzbpmxAOzOXikSMdE/HuybfDGdiQregCFp9RTkmucfN66w2ON9ZZ9sTKl+bwFz0Y0MnqVzWgTcCsBLRh/SE/FhyigKCUcSgp1KIcecbpnUvU8RptQz1I51h/IQxRYCrq7H0YpXyTaEiQEZLaRGslBpYeakKRf4xQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(396003)(136003)(376002)(346002)(366004)(39860400002)(6666004)(44832011)(66946007)(1076003)(66556008)(66476007)(956004)(36756003)(478600001)(2616005)(83380400001)(8676002)(2906002)(8936002)(26005)(86362001)(5660300002)(52116002)(7696005)(38100700001)(4326008)(54906003)(7416002)(16526019)(6486002)(316002)(186003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 3hYMXxRaSuYpXE9mZ5TCn1bsmUNDCPC8u4fQl2jGupYv1BGmqNItRltcsS/pJp4xRloX/Az9Sj1ZG6GQ4Mp+4k7Dqoo8uSZnrHqnRP4iT0XHHu+3RncqWihPWcwRXlqsyDbPRF44tC4Uoe4Xl/nitF72OBkIMBA3/wcCLI2XJRkFccTdVLuMqok7ZqSG8lUnFFkK6Kg3eJwmoKwC/vccQy6vpGbd/sdsdTmwXmdodQXqvh8T9Gepx/cfVo48zv34QT73L218F+QObr4fPunIel2PUvzPRF5Z2SLWGQORQ+AteYVTtmQ93iYSXdimAvDzOwBLre1U6YuMdDttyI5skiGzgECcXrZuzchDpCAwI+yYPgSc+K6yQDU4tVd2iot9Lub4YZzQFCMFMFog0rIuu00j233D/QUtqbaMgBvTuQZSPomM6sIkxzu6x0NO6y8qlKOUnhQvCeOrhAcAaBerTgwnsSi8pDkd0DYULQXPYrE9lpTqyf0ER+jak7Id72n62pa0PEnsWdsqv7yOvoprP8TsSU+iH5iCuBR76sK89981RhCtZm1l3e5JY/FC8pwJT9fSyvzxl3z5Xc3Jg1/5tCt7vfMmR8fUJoh1ZpdfXbeKkIDG+pK6eDR2sD4v0O6GgO5UMiN7mgMXpPVaQ2rGX7Eo8HOrcjmbGEU6VHBEwh7RAJlT1r4bKVCOGHa4tkN2Bco0YmERwJdTabRbGozkMQfCS782Bd79qWVEotBXYVeklX7stdRGndGbsDOCGdfKmVrBN66sKv31Tl86bC0ruBK2nn+Njp79SBtiBsJN34NMe6mnDruV7tGGshguUjpA7/tDIY9KjzWryppJo2fAz3kytcSSXscaHy//NiVwItTTBphlFLZqlEgCsLCwtG2hgWubWpmY7++pW/lJXy7k82PuYeTrqQX/vJfx+DWVD1chQS9A2FGsGSGupXWtiHroU7Yo1CVtfm8YuckwwVZ/EMgBYpHBwNaAdqz+tAr9GDZXAt+YDtHLacKQm2CyVOsHCC4SvK2k/T4oYNAXjL05KlVpKsdz3cuWLABhDEqXRoO4JfxP2U1dc25nwJ5CVb7gy8OcDB5duexVKBSqiPo3vH577/a7ITVqhO1h0+zkaA+QTgPhhzyfTdSxsodbxx7uevZke0jasyPVkXxog7gEae+NFUokj1hmI01tMjlvFAEioFPl6Jtt2mV1b1UQIaMcnsUW1yzW0Rr22GeTl5hgiC07ukljcSTc02yB5sh9yXprxIRh/0btG8gHFzdSar9d53CbxagEUvBRcfwberKl6FxWbEXS4mhK9+98kjQ7Z32VpzKkwApVN+btzT2M259p X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 16fc2ff5-b5da-447c-1f5d-08d8eee6f93e X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:05:06.6041 (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: /A/Qghj0EtRGs24EL/jJ9JTMKRd2/ylACAK6hFK8J0BPhz14trRL1fE5w8JFGZ+vVwTIGvkH6qVi0+6ixH2sUA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4382 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The guest pages of the SEV-SNP VM maybe added as a private page in the RMP entry (assigned bit is set). While terminating the guest we must unassign those pages so that pages are transitioned to the hypervisor state before they can be freed. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: Vitaly Kuznetsov Cc: Wanpeng Li Cc: Jim Mattson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/kvm/svm/sev.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 1a0c8c95d178..4037430b8d56 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -1517,6 +1517,47 @@ find_enc_region(struct kvm *kvm, struct kvm_enc_region *range) static void __unregister_enc_region_locked(struct kvm *kvm, struct enc_region *region) { + struct rmpupdate val = {}; + unsigned long i, pfn; + rmpentry_t *e; + int level, rc; + + /* + * On SEV-SNP, the guest memory pages are assigned in the RMP table. Un-assigned them + * before releasing the memory. + */ + if (sev_snp_guest(kvm)) { + for (i = 0; i < region->npages; i++) { + pfn = page_to_pfn(region->pages[i]); + + if (need_resched()) + schedule(); + + e = lookup_page_in_rmptable(region->pages[i], &level); + if (!e) { + pr_err("SEV-SNP: failed to read RMP entry (pfn 0x%lx\n", pfn); + continue; + } + + /* If its not a guest assigned page then skip it */ + if (!rmpentry_assigned(e)) + continue; + + /* Is the page part of a 2MB RMP entry? */ + if (level == PG_LEVEL_2M) { + val.pagesize = RMP_PG_SIZE_2M; + pfn &= ~(KVM_PAGES_PER_HPAGE(PG_LEVEL_2M) - 1); + } else { + val.pagesize = RMP_PG_SIZE_4K; + } + + /* Transition the page to hypervisor owned. */ + rc = rmptable_rmpupdate(pfn_to_page(pfn), &val); + if (rc) + pr_err("SEV-SNP: failed to release pfn 0x%lx ret=%d\n", pfn, rc); + } + } + sev_unpin_memory(kvm, region->pages, region->npages); list_del(®ion->list); kfree(region); From patchwork Wed Mar 24 17:04:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408961 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AD826C4332B for ; Wed, 24 Mar 2021 17:06:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 94B5161A19 for ; Wed, 24 Mar 2021 17:06:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237175AbhCXRFn (ORCPT ); Wed, 24 Mar 2021 13:05:43 -0400 Received: from mail-dm6nam11on2071.outbound.protection.outlook.com ([40.107.223.71]:41848 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237058AbhCXRFK (ORCPT ); Wed, 24 Mar 2021 13:05:10 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ApgQqQjiXqtq2KH2NsrMPb0Van8VjHDIXs/UdzDLt+SKann+0fHYGtblLGMkUS5oOWKhg/hsvXzQS7H1XDLi5H1Pdie/KaDpPKpuB4b3Th8vu9XroGqoN8gwR0oiMpRBn3DEWWGUZJ4EPJMUguryfI8Ua6tDRy2uUib997XDg/o2W58ReZZenpceOrZpZpGTzqMp6uEV0N5QWl3plgjnajEQLjUsGR7wOF1eVSDPavjlWnKJTka8yhbl8SNjLN9Mv1SFD/rdOgmqt/x0DHXNBvl2w6HS5GtqcEx6QQMWGAOmlJBoaf8MTZPu9+7cVP+ND1mdEwDyKOACj/yf8mDeMg== 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-SenderADCheck; bh=BzeLaLh96fzHoIyvXZk9oX9ydYKh+Bq12f0ALF6LMmI=; b=gTpTrSuyZshpxFaAXU/vMMBjMqOx545XNvSFzPY3Q+7vm4ABBkneik1Xv7AVWITmLeiHe4mo8XtJmqRAnL87WgUBCuE0JcJsMPbX2oXlLVnwBdcnpivILTUu6H9PNqYf0TypXK+8adIOnhpf4/FLcF1tMGrx67hqGrJNdYQiwEIU5yBopzWsqFxruFHLCfrn0MRbI1BgDnvs7Xa7yACfyTmRr2L5hidxEbfwHdD5AmVyu2RT2lsnRGI8qLqxUtW6iEb3+kz7ULidQu2JTDR5IaVq0nVpDnnI8VDaZXqtk2ugDWl7JB53UF8nbNmsfeZDdVi588qlw1vIAL+DzX10pA== 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=BzeLaLh96fzHoIyvXZk9oX9ydYKh+Bq12f0ALF6LMmI=; b=vpxhy3EP1DUgHn3OdyPEBtgZ9KPVTGHrvY99qE3J+wrOvkDVC3AC2Kse/tNKH+Jhvkt7ZOCSOVc4bFB0gHkA8k55WCC7t1b2wcbDKOc3L9fcO07Cesz3jax40D11rkmO9HliUZXz2D6m5kPZOH37guw43CyS9bMZP/AOFpWsLcc= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4382.namprd12.prod.outlook.com (2603:10b6:806:9a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25; Wed, 24 Mar 2021 17:05:08 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:05:08 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson Subject: [RFC Part2 PATCH 20/30] KVM: SVM: add KVM_SEV_SNP_LAUNCH_FINISH command Date: Wed, 24 Mar 2021 12:04:26 -0500 Message-Id: <20210324170436.31843-21-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:05:06 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 32c7e980-d75b-4ea9-166a-08d8eee6f9bc X-MS-TrafficTypeDiagnostic: SA0PR12MB4382: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:5236; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: zUq8TSipb7vYNPp4lLQW/SyB7s/pFjsDK05PIfdSyMo48w4dToRtwHLRYuPVXfhOBwccbmQLsSi37q2R/fM8V7uZAxIf7ajisDht1aN4w4pcUiYP/iKhhXIgRt385GN1mHsDB5LcF3krsJwUXzIERP60A3+FOCu4qgoQMoup55OFbRnnJLxjahrhaVQI9otnO70XbG8lxL12LleMc6vU3WDAQ+GqSz2WL+pp7MR0QhaIGfXfT6BB0cYCHrAQdZYUs/F95QXewJi9L7LXf2pTy8ctSYkcxKxYskCND4PtHXnPWqyfgz5uSqIY7xRHsU+m3oBEON463XFKZdpy+9Q4qYtd6X9yl3AunW6pdHm4cpHeI2DgBfjUNFnSFEBQO7Xz8B3paM/ZB1FvgN+55+YLRtfQ73JhOWMaQ+DyJwmJiUtOa5aWkZm68Axx0P+8Tc4wMV4QXsjNqCn1XnrmQbEAcpgolY5fjHCcku4qoK1vqWERq07wb5eEhZXjFb3OwXQNXqeotB1MCm3SBeuBdcNsCsv6LgEq588Ebfg0DoXY/WlZjKXQs1G70+X/cSqm1H6MWWrbFvZ82bHzFmf/l2gJs6SXct/Fiwng8tFoMUBOcD6QpETSk+eHSvsj4oQI313wiaNDtEbCKI70XPxGYra2Vw== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(396003)(136003)(376002)(346002)(366004)(39860400002)(6666004)(44832011)(66946007)(1076003)(66556008)(66476007)(956004)(36756003)(478600001)(2616005)(83380400001)(8676002)(2906002)(8936002)(26005)(86362001)(5660300002)(52116002)(7696005)(38100700001)(4326008)(54906003)(7416002)(16526019)(6486002)(316002)(186003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: x/PSyxNkc3B+EdMsnaAMw8iNyISd7fXav+0oX4sgF0K9dYfGM+dvhhgUXEMjxRLjwde9+RBGdrQpiIu+DMotyDBMKvqErHshiTBMFZFwID4jIdKoNUp9jYpJJRwkyJAQq8UVOw+z3/g585eoBopiQtmDYiNs6T60hDeXQaNXMfUdFdzu7BLRt7LDx7G/lUaQUjWT6mgCL4+ps1uPYV/TnhjSez3Q/wjUD7siJjGl10/h+6gY4IAzu8JYH4NSgOlGuWPZIkyVJXxITh++E1W9EF3imNuWVDLtMSfZ+INUcKVfU49NvJqCqQHQB9wVigHvUW3P+z0xKvYHKfZWA9FKkQcH62oaUrzh6Pm4zuxUr1Wma3uh6/RmQxoaq8QXLCPMkV3H/lyWH1cmoA7juvbH6xg+0OqbpNCcmVJvkmHWZssSaIy2Pyl/Iisq4EuJK7C4KvzkXUuUdyGrEqIVYVbUDUk3DmtvxQzvGGTdrApHX8teW3pBbD7lJO8k2iIx+6Gf78TuyS7nmHmzgQpXhVDX42Tmmyk77hnjEjbRYhOSC/dBE4j21k78PlegUmW0FEsBeof5mIAvFzQJVWJdvloypTZYV7+DAqqWwZLEvN5B3+zj7mIsthDmYUrCM+49hEwP21f4JU5cGqZjxvU1T8+lzlBtyifFDyyHcGmUqi9Fhm04Trgl+IS2CY/sx2vWMIxSmHRwccq9P4Xafs/U/FJLhBNmn9OwwBJ6JtarX/X0d6zwQhYlPS10wxKmAyIvWiuIrOSDUpz+iwb04Ogjnqrp71Tvffyn0lbZ//hBOsZ0zfroFrSlHIyXB0hNrZs4Y6QzLtPiU9q6kr90yj2imEV9lD1V9C5d1tBUWBB41JRPVYlGVoXjqQKPzu4BqdGzoNtAkkOXZB/katwRkjzUGWtfjwYkZNC8uXW1jftBnOlc66zaXh/4k4kRdCPbJ9cfNGQ9KJnJbtqTswM4vo1fguwqrQVA2qE6v5cDAhxt3Mi6fqlw7cfGtgSafpU5QzR3vH1u4Sd7O1hGhl35ERxvTj6Njoo8ooNXpizms0lq+3Di1cbB4lBQexMw0w5rYZ8rJuMBUTdGeIFMngHRbIQdQGS+Ih5wHuJU+Th6G00Fgp/OPD9ElwC3nW2d/qoAWi9MMIiWJgODnwbBr3lqSmub/X7XGvMOtWgMSAbFrAqt32mBPbdd0wdjaybO8obI0CBbPcBs9gmjVp6z/ob70S82DgNBXwjpp4sUN7g1w9UXCFQMLQOYYPfTbhjSFf7CnABsnElt2ha83WqQoOmlY19voduRa9CbnI189e13WKr+iLbGyjSCRIMHuGAZ+et9sCj7vKvU X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 32c7e980-d75b-4ea9-166a-08d8eee6f9bc X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:05:07.4646 (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: jkEuQuPKKeRUrQsbbVvDCcqMCakG2ogUZMKKEeEAgIupjFMsdH3r5yjEkArnUaAAmd3YyAHNB/OjJ0Ii4lKpkQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4382 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The KVM_SEV_SNP_LAUNCH_FINISH finalize the cryptographic digest and stores it as the measurement of the guest at launch. While finalizing the launch flow, it also issues the LAUNCH_UPDATE command to encrypt the VMSA pages. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: Vitaly Kuznetsov Cc: Wanpeng Li Cc: Jim Mattson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/kvm/svm/sev.c | 131 +++++++++++++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 13 ++++ 2 files changed, 144 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 4037430b8d56..810fd2b8a9ff 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -1380,6 +1380,117 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) return ret; } +static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_launch_update *data; + int i, ret; + + data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); + if (!data) + return -ENOMEM; + + data->gctx_paddr = __sme_page_pa(sev->snp_context); + data->page_type = SNP_PAGE_TYPE_VMSA; + + for (i = 0; i < kvm->created_vcpus; i++) { + struct vcpu_svm *svm = to_svm(kvm->vcpus[i]); + struct rmpupdate e = {}; + + /* Perform some pre-encryption checks against the VMSA */ + ret = sev_es_sync_vmsa(svm); + if (ret) + goto e_free; + + /* Transition the VMSA page to a firmware state. */ + e.assigned = 1; + e.immutable = 1; + e.asid = sev->asid; + e.gpa = -1; + e.pagesize = RMP_PG_SIZE_4K; + ret = rmptable_rmpupdate(virt_to_page(svm->vmsa), &e); + if (ret) + goto e_free; + + /* Issue the SNP command to encrypt the VMSA */ + data->address = __sme_pa(svm->vmsa); + ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_UPDATE, data, &argp->error); + if (ret) { + snp_page_reclaim(virt_to_page(svm->vmsa), RMP_PG_SIZE_4K); + goto e_free; + } + + svm->vcpu.arch.guest_state_protected = true; + } + +e_free: + kfree(data); + + return ret; +} + +static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_launch_finish *data; + void *id_block = NULL, *id_auth = NULL; + struct kvm_sev_snp_launch_finish params; + int ret; + + if (!sev_snp_guest(kvm)) + return -ENOTTY; + + if (!sev->snp_context) + return -EINVAL; + + if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) + return -EFAULT; + + /* Measure all vCPUs using LAUNCH_UPDATE before we finalize the launch flow. */ + ret = snp_launch_update_vmsa(kvm, argp); + if (ret) + return ret; + + data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); + if (!data) + return -ENOMEM; + + if (params.id_block_en) { + id_block = psp_copy_user_blob(params.id_block_uaddr, KVM_SEV_SNP_ID_BLOCK_SIZE); + if (IS_ERR(id_block)) { + ret = PTR_ERR(id_block); + goto e_free; + } + + data->id_block_en = 1; + data->id_block_paddr = __sme_pa(id_block); + } + + if (params.auth_key_en) { + id_auth = psp_copy_user_blob(params.id_auth_uaddr, KVM_SEV_SNP_ID_AUTH_SIZE); + if (IS_ERR(id_auth)) { + ret = PTR_ERR(id_auth); + goto e_free_id_block; + } + + data->auth_key_en = 1; + data->id_auth_paddr = __sme_pa(id_auth); + } + + data->gctx_paddr = __sme_page_pa(sev->snp_context); + ret = sev_issue_cmd(kvm, SEV_CMD_SNP_LAUNCH_FINISH, data, &argp->error); + + kfree(id_auth); + +e_free_id_block: + kfree(id_block); + +e_free: + kfree(data); + + return ret; +} + int svm_mem_enc_op(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -1439,6 +1550,9 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp) case KVM_SEV_SNP_LAUNCH_UPDATE: r = snp_launch_update(kvm, &sev_cmd); break; + case KVM_SEV_SNP_LAUNCH_FINISH: + r = snp_launch_finish(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; @@ -1820,6 +1934,23 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu) if (vcpu->arch.guest_state_protected) sev_flush_guest_memory(svm, svm->vmsa, PAGE_SIZE); + + /* + * If its an SNP guest, then VMSA was added in the RMP entry as a guest owned page. + * Transition the page to hyperivosr state before releasing it back to the system. + */ + if (sev_snp_guest(vcpu->kvm)) { + struct rmpupdate e = {}; + int rc; + + rc = rmptable_rmpupdate(virt_to_page(svm->vmsa), &e); + if (rc) { + pr_err("SEV-SNP: failed to clear RMP entry error %d, leaking the page\n", + rc); + return; + } + } + __free_page(virt_to_page(svm->vmsa)); if (svm->ghcb_sa_free) diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index a9f7aa9e412d..bfd5340e153d 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1598,6 +1598,7 @@ enum sev_cmd_id { KVM_SEV_SNP_INIT, KVM_SEV_SNP_LAUNCH_START, KVM_SEV_SNP_LAUNCH_UPDATE, + KVM_SEV_SNP_LAUNCH_FINISH, KVM_SEV_NR_MAX, }; @@ -1609,6 +1610,18 @@ struct kvm_sev_cmd { __u32 sev_fd; }; +#define KVM_SEV_SNP_ID_BLOCK_SIZE 96 +#define KVM_SEV_SNP_ID_AUTH_SIZE 4096 +#define KVM_SEV_SNP_FINISH_DATA_SIZE 32 + +struct kvm_sev_snp_launch_finish { + __u64 id_block_uaddr; + __u64 id_auth_uaddr; + __u8 id_block_en; + __u8 auth_key_en; + __u8 host_data[KVM_SEV_SNP_FINISH_DATA_SIZE]; +}; + struct kvm_sev_launch_start { __u32 handle; __u32 policy; From patchwork Wed Mar 24 17:04:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408315 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 571E1C43462 for ; Wed, 24 Mar 2021 17:06:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 387FD61A19 for ; Wed, 24 Mar 2021 17:06:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237171AbhCXRFm (ORCPT ); Wed, 24 Mar 2021 13:05:42 -0400 Received: from mail-dm6nam11on2071.outbound.protection.outlook.com ([40.107.223.71]:41848 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237059AbhCXRFL (ORCPT ); Wed, 24 Mar 2021 13:05:11 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GkK4+GHpsGpSDv+KiHr6i/8gsscT/hwsowvOQqxMB0TcMDXWYFeynyl0MDX2YSxB1yhgwaxQeJJD5mABJ1se8alqPafremk7hJ9O9cEyusgPi5KJkaP1AwhTq4qNtDwgBXUsTnZ4RqnOvSHOtXxgQtVKI0U3a/uuiSGly65hxTQ4BpN4m0+FIFMdx4t0h2fz4pi78UQkkwzkLXeKhNTXOJzpr4ws2NpscPCZOj97HxYaieLHBWndk6Vs69/aET3bGON/+QMAklOnE9eVo+nWcQkQSzKHPSl19YG+1nQuopX7Y7GSMNkoDl7SoqI+oKnv20bV7tjNnqdV+eZeqK96XA== 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-SenderADCheck; bh=s3wJJvnEJeQlSpr5Sp2HUBdeSopoZrJJ6wWWbsSJSnI=; b=Q8RPCUC6kGvr3WwzaNW3T718vm86dpD3olYPPxZn23RJ4k3khbTvj3wUs6h9WENN549jij+bacgiS5razsLwI7PvnLkZM3j4fI9WXXW59srVLlLtRGg1I0AcmdUMWa+2Zglo8iZl1whOUNFN3aEMxtBPcfxa2dFkyWRl0vpgZw0g5E0vSA43DrV0vIJfU6AEUvqciqNxIBHM/OAlWbRRPtGnjq0+vF2fAbdyCxGuyzyVgAM4ZLrsgX2/hPzN85dG6Q6lXWo4kaI1jqJu0H7E9ZVX44cnxV6/o7FwJrnmQdEOiDB6OAWz359//MQELnWBdwGr4yg10qPvHmbELkoS3w== 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=s3wJJvnEJeQlSpr5Sp2HUBdeSopoZrJJ6wWWbsSJSnI=; b=Fr87ZSDkVoWe0YFXxX4tZrsfpWxPtZjo3TJHEkw3xYYkH0ym39WVpmfVyoA3svGv5xKBKtOiiBMLfZKAxyQYX+5dq7yhgdvNwfFdMoZJV81VeLmd0qbap6rzglhq1XrWrY7MlD64C08UqlMFzNDzKK0hOFgPtT/YVEjGDvtQw1I= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4382.namprd12.prod.outlook.com (2603:10b6:806:9a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25; Wed, 24 Mar 2021 17:05:08 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:05:08 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson Subject: [RFC Part2 PATCH 21/30] KVM: X86: Add kvm_x86_ops to get the max page level for the TDP Date: Wed, 24 Mar 2021 12:04:27 -0500 Message-Id: <20210324170436.31843-22-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:05:07 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 8457ae41-7df8-470b-49aa-08d8eee6fa42 X-MS-TrafficTypeDiagnostic: SA0PR12MB4382: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:5516; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: iCN5KTEsCVbOA3rsJ6xevgAecAgiHjHI2UW3lNoSguzA2Q18v5z9wkh9wcOrTSZn9IT/gsWaCJjg5lKIT1hHxwbL7Tgpf5wwa1TNvKh6Yor+zZrPId4DkQHOKPz7pEocqHRwZMniK79C0fFTfj5Dw2eNJJduxg/bRmhvAK3H8Af2sM58MwDm2dt4GHEJV9pyHu5APCT2d5WAcLjB7RfIWGpLbtL3nL+lpzsmAc8pjzdq1NSJDIBzd64sJYUshF+q4sFAHZSG9dyNOp8dsUCtR+JvqRiVfq3LFzvGcvLighM0HRc+YQf2+7UqmB8o+27IQC6j+BV5OyDcMIXA3OLxn1b5B2PnscPiBfivLRfCz+iqR1IMGPoWWn1TZHItSSr9Vbs+88F3/jJYFyp86LlS6fr3CvYfRuOfZ49l/7yTTI/OeIJZOeoagL69YFtEycvR1e8LFaWjWeW6QGWVy0XztfP1jaI4OT/OBRxYJA2DORI0w17oPbOBq5slTWZoOgH0UuVOsWZGXTraiRlc0zToq7Ichwzn/MfQkKqF6g0pA/ArZPA/QBWT/fEd54Sc9PZYAxnyS/RKVfC1/JDfObBLREG84NmUN4m21MxwcjlF9d1O6pytl8VKVIBkEV7AJm/l8z8H/i4Di+y9hgFbAiu3m7DV7jqjeWtstIZzd0aF8xo= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(396003)(136003)(376002)(346002)(366004)(39860400002)(6666004)(44832011)(66946007)(1076003)(66556008)(66476007)(956004)(36756003)(478600001)(2616005)(83380400001)(8676002)(2906002)(8936002)(26005)(86362001)(5660300002)(52116002)(7696005)(38100700001)(4326008)(54906003)(7416002)(16526019)(6486002)(316002)(186003)(15583001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 8gL3BW6+BIc7Lt4KTmDeT625VyERiyYuURTOwC0h1bKlMCPEnsFfNyAvJphh/dV48D4n7M6inc7oml131wxNdC/ux/RJ6VFROBDgHrSTi9MeXgOM5m5AvVky5bnVS1/L3S7Cutu1RfRyl9mSYSBwaOtdfBfcfTo/5UULo1zVP4BVqy/6pLshlOgoXrPeM5yyChbUHpgiIeqnOzSAsf/X8FIC4Sty5b9Zd91lKM4Nf1/504g1U9kBvAOUEg5+bPW33XPsempQEtjxZj/RkXs21wiVnJREPfR+y5vmtuCUdWW6s5hYEaq6D8YOI5NYF7+TE7Zo+UisAZAev/fnYmY/IXqH5Nxl/dhf7lHYmylMEes6wRmDLGYPbawO4g+xnLTDR3W0+3W6mMvWGLUir41XErd6KatplbY0oezlaVnw0VEVBD5qfDMNI+lEc1fAhmcXGyZ3EBq/3mj0quV7RG/D6PE1I+JrCtsVOm/KAiBrTSkdD8iOYlN5l5sQjGSUZ1HeSivdbASykO+0tRs07wD+CnavqdYlP/riZ/EGCjsxVsm8kGJz01TwIh6zzJf+y3CA3aklPYJL2ADs4n0zAIu5TKYAxEcEKgwUVhsPkAOAPYXeqAO9hhzqsIdDR1Pue3LVyQjBB6pjnEzhPN+clfkA52ITuWMVlouA+7pTWBlotql56Jm4WgmdgCV7/RJSI0q7vjjVJjtaqsaWM10E6pYLLWs1po8fZTrxaHm+kk93ANryD4rFDEJR21kEJ3sD7jz/jREdYpbxHaAj0xU25de7qO6nnu/hZokqcygZsi0xf1sC61Mgj6av7HNUqhOXR7BD65+lhXwQPbk+LZ93K0cWFKmQ5WQW97aVeRGkfkQ1IwaOiHNSGMXl77DQlCFJ6kixtbhf8FfSNy7onFC6cyUU/r6dUWY6f8sKoKKK6FyGISpxfCeCckb/gR7ceJ3lODgFm9nfLX1IkKSGY7/ORxBmMbJTRwg+v4/YN3guVSHImCBdB2JjSvAwCtm+KpLE8Xp/+UhXaNxqLnQvYcHvuv0rqxqGlK+WkgiOSfx9eDNTsz3IaIoI4tvZW3ItyIdVt7UXpgIJvuFX8Tze1ejFso2P90jxw4QbpiAyyVxRZdcNId12s2IXGHBb5piO5+rqmuYNXl/IhZAdwA/CgBzilNEDl8OYH4nR6qpZApYFIhb9SeKSHXgJysPtdvmYmbxnKFGjpaBYmFEy8wCrbLeBJGJXoWmHICBi9oD72rJDLrpUxRpvKMRXisTfFnFaFWB5z3Ah+Q0rMPxi7pfcllNc+vHirAGj9vZ3yGab2SApthhAuBeCPyTBpoQPgR4Vd0erBMvl X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8457ae41-7df8-470b-49aa-08d8eee6fa42 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:05:08.3291 (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: 9+Q3ukwSFat1he0MkMpMXesJcxH2HUYRsO4e+Q6zok3yZ5mKgvFP9nNm/4S/RsaRiFBNgFrYz6WwPrKDYWp3yg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4382 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org When running an SEV-SNP VM, the sPA used to index the RMP entry is obtained through the TDP translation (gva->gpa->spa). The TDP page level is checked against the page level programmed in the RMP entry. If the page level does not match, then it will cause a nested page fault with the RMP bit set to indicate the RMP violation. To resolve the fault, we must match the page levels between the TDP and RMP entry. Add a new kvm_x86_op (get_tdp_max_page_level) that can be used to query the current the RMP page size. The page fault handler will call the architecture code to get the maximum allowed page level for the GPA and limit the TDP page level. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: Vitaly Kuznetsov Cc: Wanpeng Li Cc: Jim Mattson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 6 ++++-- arch/x86/kvm/svm/sev.c | 20 ++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 1 + arch/x86/kvm/vmx/vmx.c | 8 ++++++++ 6 files changed, 35 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index ccd5f8090ff6..93dc4f232964 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1302,6 +1302,7 @@ struct kvm_x86_ops { void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu, u8 vector); void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); + int (*get_tdp_max_page_level)(struct kvm_vcpu *vcpu, gpa_t gpa, int max_level); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 6d16481aa29d..e55df7b4e297 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3747,11 +3747,13 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, bool prefault) { + int max_level = kvm_x86_ops.get_tdp_max_page_level(vcpu, gpa, PG_LEVEL_2M); + pgprintk("%s: gva %lx error %x\n", __func__, gpa, error_code); /* This path builds a PAE pagetable, we can map 2mb pages at maximum. */ return direct_page_fault(vcpu, gpa & PAGE_MASK, error_code, prefault, - PG_LEVEL_2M, false); + max_level, false); } int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code, @@ -3792,7 +3794,7 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, { int max_level; - for (max_level = KVM_MAX_HUGEPAGE_LEVEL; + for (max_level = kvm_x86_ops.get_tdp_max_page_level(vcpu, gpa, KVM_MAX_HUGEPAGE_LEVEL); max_level > PG_LEVEL_4K; max_level--) { int page_num = KVM_PAGES_PER_HPAGE(max_level); diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 810fd2b8a9ff..e66be4d305b9 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2670,3 +2670,23 @@ struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) return pfn_to_page(pfn); } + +int sev_get_tdp_max_page_level(struct kvm_vcpu *vcpu, gpa_t gpa, int max_level) +{ + rmpentry_t *e; + kvm_pfn_t pfn; + int level; + + if (!sev_snp_guest(vcpu->kvm)) + return max_level; + + pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa)); + if (is_error_noslot_pfn(pfn)) + return max_level; + + e = lookup_page_in_rmptable(pfn_to_page(pfn), &level); + if (unlikely(!e)) + return max_level; + + return min_t(uint32_t, level, max_level); +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 72fc1bd8737c..73259a3564eb 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4563,6 +4563,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, .alloc_apic_backing_page = svm_alloc_apic_backing_page, + .get_tdp_max_page_level = sev_get_tdp_max_page_level, }; static struct kvm_x86_init_ops svm_init_ops __initdata = { diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 97efdca498ed..9b095f8fc0cf 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -606,6 +606,7 @@ void sev_es_vcpu_put(struct vcpu_svm *svm); void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector); struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu); void sev_snp_init_vmcb(struct vcpu_svm *svm); +int sev_get_tdp_max_page_level(struct kvm_vcpu *vcpu, gpa_t gpa, int max_level); /* vmenter.S */ diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index eb69fef57485..ca0c1c1fbf92 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7587,6 +7587,12 @@ static int vmx_cpu_dirty_log_size(void) return enable_pml ? PML_ENTITY_NUM : 0; } + +static int vmx_get_tdp_max_page_level(struct kvm_vcpu *vcpu, gpa_t gpa, int max_level) +{ + return max_level; +} + static struct kvm_x86_ops vmx_x86_ops __initdata = { .hardware_unsetup = hardware_unsetup, @@ -7720,6 +7726,8 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = { .cpu_dirty_log_size = vmx_cpu_dirty_log_size, .vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector, + + .get_tdp_max_page_level = vmx_get_tdp_max_page_level, }; static __init int hardware_setup(void) From patchwork Wed Mar 24 17:04:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408959 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1662FC432C3 for ; Wed, 24 Mar 2021 17:06:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EEC5F61A19 for ; Wed, 24 Mar 2021 17:06:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237177AbhCXRFo (ORCPT ); Wed, 24 Mar 2021 13:05:44 -0400 Received: from mail-dm6nam11on2071.outbound.protection.outlook.com ([40.107.223.71]:41848 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237062AbhCXRFM (ORCPT ); Wed, 24 Mar 2021 13:05:12 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NskGhWGriLel4TmQWc4Eg8l0LwQzmZzIh0VbZKyPg4g74MzOCJ90zQAuFnwEt2PoSHkTC5EE543haiRQbFCIHM/coWdn0h+jHarF9Qe/gOwQHMSSLsfTh1Qgdns2K58ElH6qpFOOEYqlmcHQNXUDBja7Ndi2ug7/NFy2F8qCZwZxQGe9l94yusG4rDV448A6uJiolgUPmwkb14b8GRQ8WT/c3ZRsFiDfEzQ6cj19N3D2b7faMzCjNGfm04LqgXu8HoegX4kzYM2Mz5QnrQgE0EPBGPE4AmZnWMVfJ3MZFZhR24fafjh36EUzwSSsW2C/LfwDKP8cCMl5eScS61DHpA== 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-SenderADCheck; bh=rkaNcJi+It6It4AFqPXfKx5o9ZdPI/+jG/Un8afHthE=; b=Wc330QUTrXGZt0ndJ0REzXjxHdKafPkSdyVjdJMdNtS6dsZ8vKtcl3JJ1/2XifxABQftUPUjf76ZbjGc0Ee0RZR0sDXKv5svbe/qz8wgZOR8Y7Bz94Xy/crQvKkgycFqErAJg5e760c6Ag8CP4fO7S2nrer5YPNjJc+rA7PhyQLRBxM5AtGoD3OvJbOzHepdZ45Huh7uYf7OQrAwhnxplCE/S9Tp+24H1q1qvlU3Z4kB8MZIFswUCr0Z5lQFP1ocssiqlGcLhVzV4E2ycDYFg74kSAAw3Nu8Pr4rYKoW+h+iLsEe/Ocdd0dWpLGL1rlxmeeGUzypiJ0oGmdLb1HJ2w== 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=rkaNcJi+It6It4AFqPXfKx5o9ZdPI/+jG/Un8afHthE=; b=qEvHSG3+IHUbPMET02ucfC8S1lVtmiIqltjQMyRefmE7SXdmoaVrM+ZP/sITc76fEy1pk1xaI6vFnh3WoQP6YPRyXqA0U1rKk1MDCOlTEBc1cFeFnydNgb6FsfopY5MtoUHHantDD9MhAnJlvfVWKhj23ijXgfQ828quq2uR2oc= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4382.namprd12.prod.outlook.com (2603:10b6:806:9a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25; Wed, 24 Mar 2021 17:05:09 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:05:09 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson Subject: [RFC Part2 PATCH 22/30] x86/mmu: Introduce kvm_mmu_map_tdp_page() for use by SEV Date: Wed, 24 Mar 2021 12:04:28 -0500 Message-Id: <20210324170436.31843-23-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:05:08 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 97662453-063d-43fc-8a5c-08d8eee6fac2 X-MS-TrafficTypeDiagnostic: SA0PR12MB4382: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8882; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: rrF+EOQoOL5hQ62GeEsYpmt6j38MbOWbw4WN9hlWfpleuiXyupO+g99hUoqiNDH0IYhnZS0WOk7pDOIR2wqAINtiN1VWl6yyoLrYts2avsflSmKGtJigYYYjpg95T6uzbyY7RWSt1rSQFIvWlwX1WtT/HO/fql81GcT9t2sfCsT28Ee6MKLdj/5rmcBbaV2BNLFNytjHcC3BABwcfcuROgUsB34rSLTNVEonDbr7mREQS6/qcItArp74NI48Hzs1wxXSJoMl20aSJLzjuBVm0tdLywsZhprZ0nwAL/tGFPN9rAqWKt2QJdqmvHN0cDzH1oikJZ9bjsMqMVY39tm+3hV0H/lD1UYvWU30IcpragbY9KIIhd44lom/0RqFRdP4nzyWrPGfQbqbhYVlDzeC53n4LadEdKVYjVXNHF0FOJ7CTo5mKcZSDn0cj6rbicoa7wRmev1ZaMqn58cIUlZk5j2bY4G3LLr6zJYDANDhqkHljOgyNbcnfJ0H2S/j1Z6PmewLKaj95pBUU4bJUdQFrde9U2DiXE3jNrqw1hpsKX1nKKFCffe8iGu1lmnpIrcVbaVVswN/mBJWE8CJFhuFnRlQ7E4xY9BOccHWrR6Uvti+WXy3rEZW0BBFom48VSrulXpz3GWsb+EMpsjkw8M5aQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(396003)(136003)(376002)(346002)(366004)(39860400002)(6666004)(44832011)(66946007)(1076003)(66556008)(66476007)(956004)(36756003)(478600001)(2616005)(83380400001)(8676002)(2906002)(8936002)(26005)(86362001)(5660300002)(52116002)(7696005)(38100700001)(4326008)(54906003)(7416002)(16526019)(6486002)(316002)(186003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: Ymywn1j8u8HMVGBvylVj6HNFMbZFdWJx1pNJrn05HrvZP6AvdKXQ8yelvxXh1208UqCo4ddpiSx7sucWw85MROUs9eCLNciUCcvHG26DrYGZtVx46fBJJ+gptkI1Y9KmmqcGzsxeNM94qxY/+6kVUbnDGDb/IuPJpHuztsoraPM/3eUtdTL0RTpFcKwJgdbssEEDf1pyiFihSBc0y2K+l+nnYMjMq8SzMF8nZ9lZN88hyCLCpDaUUpvYHx0ejj6PDsuHNQzSscNuEO/XFAjAtGzUUqK8lei3QJ4iqh29P7Lrkcm8h+eQ24UFyV0iIa4ugJP0RbeFA70pMBQfi7XENe6ZpMtnMTWMTly0wIaYDmV3JBhE+rmK+rAzjfhwjfViXi0L540Y1O0a0NpFkUN78m8Ph4vGDgGwoMtXHsytxtpFjzxgKCIxhTfglbjbqrt9Ae8u79ZU6K+6ypodjLkiz8vg5On64hlotW3PU9Zx2mLjl46Cv/SwTDfbpf3v7PQrD3kBe76zgJN/skk1i/7DLU9Q9I5q506aWsElESBEE/YXCY2UP0tMulUQh9F3TCPbJyX9c1f9xKCMEgcBJ55JW5Gk76mspC3Zhex79wiO0azqjjU8mL0aumSS3YVxH0JCo8dzVA450iKFM4mFMbKn3UBxSzOhqmbFg85qAIDunD8RLQVwifjZbUZZkr0mOoPO6lVm2wmbcZT6XWhEyE6/Duv3reRNVR7QvK7xldqRoXfHCfzqmSVMceA7oeMjIxSSIEZ4YGkQneuUHT6HPlfjBI3Q7Ue2+ZARUCcKONDiXRwNflwPNWGUzu1AFBE/U0eZYMzA7ADRXOLoj4q0RgCdUDs6u0dzF3gMQyl/OloFxM7JatqtIhraCC0071rjckHJwQeiXJGYBG6gbjrEyjTZD78WMxR7oyq1F+1Eue+K3M67A4eV8+N8wfq8N/UcmSDpgK6epfFCm2v4E9OoxbFI6dEsp5id+2omBSRS/S7A8nO1QOE5Q/WkUeyadjpjAN+Nx5+fli3fsKFW0A4Ix1g+Xewl6da9SbtK3zjdPAlI2f6wBakOyTWrD54a+MzlO3UJC4sBKcfx414I8VtX9t3iRU7NNqvu4tQamfMsxViVI0AEQonUnrD2fkvfU5FdvFcdu+VLXpqSuLCkDf4o57irouzWpxE38wQNl/WsK1YemQKTmPEVmWbKkedhDtMEzN7HF1PPY9tSdMpgLkWpnR8hv2iFlLEN7lgT8BkibmfKKz817r3mY6bFBkor4ka3Nv1RrJEIFuHzKqXua+jmefI1+69P4Wb3T+nzqVlRpZrcZvMcmSZgfzoPZ/tSIeHV662q X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 97662453-063d-43fc-8a5c-08d8eee6fac2 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:05:09.1216 (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: nVbL0rrV3a6Z1LxjUgPMrurtIiM6mo1zXdm+ChfJ9IVm5EIfdP1ToIdKZr2bGWk/I3ovsgOEo5yOjB9lO0ceJQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4382 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Introduce a helper to directly fault-in a TDP page without going through the full page fault path. This allows SEV-SNP to build the netsted-page-table while handling the page state change VMGEXIT. A guest may issue a page state change VMGEXIT before accessing the page. Creating a fault-in, we can get the TDP page level and PFN which will be used while calculating the RMP page size. SEV-SNP guest calls, page state change VMGEXIT followed by the PVALIDATE. If the page is not present in the TDP then PVALIDATE will cause a nested page fault. If we can build the TDP while handling the page state change VMGEXIT, it can also avoid a nested page fault due to the page not being present. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/kvm/mmu.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index 261be1d2032b..70dce26a5882 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -109,6 +109,8 @@ static inline void kvm_mmu_load_pgd(struct kvm_vcpu *vcpu) int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, bool prefault); +int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, int max_level); + static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u32 err, bool prefault) { diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index e55df7b4e297..33104943904b 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3808,6 +3808,26 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, max_level, true); } +int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, int max_level) +{ + int r; + + /* + * Loop on the page fault path to handle the case where an mmu_notifier + * invalidation triggers RET_PF_RETRY. In the normal page fault path, + * KVM needs to resume the guest in case the invalidation changed any + * of the page fault properties, i.e. the gpa or error code. For this + * path, the gpa and error code are fixed by the caller, and the caller + * expects failure if and only if the page fault can't be fixed. + */ + do { + r = direct_page_fault(vcpu, gpa, error_code, false, max_level, true); + } while (r == RET_PF_RETRY); + + return r; +} +EXPORT_SYMBOL_GPL(kvm_mmu_map_tdp_page); + static void nonpaging_init_context(struct kvm_vcpu *vcpu, struct kvm_mmu *context) { From patchwork Wed Mar 24 17:04:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408310 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 61EFFC4321A for ; Wed, 24 Mar 2021 17:06:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 420E6619B4 for ; Wed, 24 Mar 2021 17:06:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237185AbhCXRFo (ORCPT ); Wed, 24 Mar 2021 13:05:44 -0400 Received: from mail-dm6nam11on2071.outbound.protection.outlook.com ([40.107.223.71]:41848 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237063AbhCXRFM (ORCPT ); Wed, 24 Mar 2021 13:05:12 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=d+/2k2gb3mWVNdfDrfABA2eYD0UKZfuJ8t6g4B6dyAJWiZ6WLJGBoOI1Fe1TZov7KJWtro4fk2WfFiEHfej/kKZ2jcx8EVsHAghJhhZxLL7WXTnQs4cuP8Bq2wKrHJgNFBil2TE125SGjXknLnBTASkLGK6J+ips2304XRJjXbFqSkZJSZYFsybdDAmjvG3+wcmw7/2kTZO89dyC+Ewm2Wbs0znNlJ+K5knnndpnBySyyJBWBfgf2/6h7MwQZjZfkT/my63iTAYVQS9aGVJ0ft3q7lJIgdzScALA0LyUkUsIcIr3vKGlMmxcT73nKp0FFMfU9SUzXWoSZbnz4PrpwA== 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-SenderADCheck; bh=Zs710fNLbqRp3/buPF9DtwxvV6+8m/Tb/z97SaRom2U=; b=By8vuOuoNNZlzP67In6kQ13x/NOA+O4dEllQafoJcmm1R775QinMQm8BY+XVoWD7KOAZ0G1EMPolG91r5HXw47lsqnP8a9kyA7Ym1Y8/B7TNI9HnTIABGA3dStqbPAH9txiKwsqasXu2rLI439lgR8DnKdmkTnFaazkWisLfJap7EXJIeh3mylNHOkdxaw8+rGsFhiHkPZejRMkeRaRWmAvZYiyG2Am2Wrwu7bE53A4AzxkprgLMInP/09dNj4W7Ghp7qKqw04wm+HW0iLyVDSxirXKCzFP1ErLHT61gFhNkktoKr4+caZVMcrxu8mJg1Ck/WNmlZkz6AIaGjVtUiQ== 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=Zs710fNLbqRp3/buPF9DtwxvV6+8m/Tb/z97SaRom2U=; b=XDLnRY1hToMW5S3NWf5z84gO/9/QUpQL58aGy7xecJTm70wQsYiCbBavlxvSODYnZDBD1Z+nr7pHY6tWcyfbxUdPd85qhnYshQvSPmNRaa1vbZlImGm4W9qEPjfmeeLVcwUGg8ZjYZyrRcGNpKTVwYKjxwgxF+SA5nk8lc2UYaI= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4382.namprd12.prod.outlook.com (2603:10b6:806:9a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25; Wed, 24 Mar 2021 17:05:10 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:05:10 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson Subject: [RFC Part2 PATCH 23/30] KVM: X86: Introduce kvm_mmu_get_tdp_walk() for SEV-SNP use Date: Wed, 24 Mar 2021 12:04:29 -0500 Message-Id: <20210324170436.31843-24-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:05:09 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: dde0f0c5-b118-4612-6212-08d8eee6fb4e X-MS-TrafficTypeDiagnostic: SA0PR12MB4382: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8882; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: nFTVFu+Hm2tfpSIKax+V9qs3u6ZOm5Qrkpz76f6f7X0cE0nti/vm8Tof95aoRYcSlhV+RU/AbTdYsBqoCfNgNehA8MD3ra1FClmXCJR49BNbjjvdFFnvB+ihWqOpkRcvemRHmz/dtLKKFZPt4++323s0zfaFbhWc5QcNwUGy2BRnU8ccukm8itwyoUwZInRl3CH2qYZfNOdJwdr01a9ipZTl4tyLnPov+RaCRvNFljwyK8ypVkA/IrLArppBtnuYEcKYT/2GX0DvJzccObOHd+jWnET5yJcCGH9xSiOsxoYexLnv4vQUz7ccajJ+CRrF3QRSLPiTK/VjfGbeJ180xgPGOtqXwZjpErkNrR/e64h79o+a7prAUYSyBvgQn1idYosyUnLhsLwdlWvqZ5mWo4NbAL4Wee8uwdtM7epFhOiciYt+oKkdcjKvkC2F6UKVA3JAvjmp2VzeKH7CiMk4NS8uuN9b3cI1aLsghJkD7bKMZuGC+hi3sfQQ5Z8Yv2KC62j7Asj6UPWBUIqAqiJLajBqApaHvKPA/LFjfBtCmOLCEkNPnPvZoIR8142b8Gz4jbnnYXyj/vOfO8QatnAf4Zehc7oiIN5S9kGGyQfRqf/jLSduusS9zj5dfk01/dHoX9nuFcbruOiPN5Oi07a4bQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(396003)(136003)(376002)(346002)(366004)(39860400002)(6666004)(44832011)(66946007)(1076003)(66556008)(66476007)(956004)(36756003)(478600001)(2616005)(83380400001)(8676002)(2906002)(8936002)(26005)(86362001)(5660300002)(52116002)(7696005)(38100700001)(4326008)(54906003)(7416002)(16526019)(6486002)(316002)(186003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 75UguT9FVoQRHZUhxLfC9hiRsnR9DUUGM/xpTG4sbYimyyrVkwpl3fHHqrXee0lZEyHJEMeXDbUBhtNTPd5Y5xcUZfqFdaxTGO0q/hkTBaMkha7dD0F0hdnxlZHbZCaw0zPh7dTg4WsFmDApQXXTguvM/5cal+G50PY8Ak++ygqjfqoXfXONKmMhSQUSbm45iLGJW80fBVdZBsVmoxXSfKLPTnuZmvhgXFvWJ4VvsPCjGrcJFGD3Vnc5Q4yCHbfrnIcvSjj90pWriBOYVWVpubG7b10SOCUIOm7faaEdZ9suU98kOGT9Y5Icu8xdClh0FIg1GWYIEQMRYodJdbJAjKv6y+0gDc+ghSYJN6+yN4P4OrIdbSwlJJcvxsmn4uIqVTKErrSTtyEfWxFD6Iq41R+vhLLLD+KtEmbEZvAe79FeuMabH7rgOVyJoDSKxcQcSbk45yUT96SEHsXG3qcKMGSZUdd7KQDdPojPR5L+9gWxcJWfueSbUuA+LxNRhPBjPBUaE3sv9QHWuVXbtjyqnqXN9kNT/TeERkYIJugU4zVc6BEwd/PeDshC8WSAiUtSmFcC2cpXsgDQHlKnPrv2vFqwWrBF0FVmXrD0FKGKyh+YgiE0gWgu5+l3p1hY1dHuXgvtUa5Klm3ZRkXlGDBNJ5mXskD6XOHSNvTfqps32T4rvmZUMvZcgYny4MbICNxehvMq7RTc2/2e+zq643Vjl6Ywsz6rMK5521RYS8t4jMVZTkyOOw6uEDbXYBxoszhDnEHW7uVZoP93o/juXcrVxs7ve6AElfSsxNaw9f/p826+dlOWI/u80rWZkZYWRl6llHMJaSyzi7XdW41G/LS+HWpCqDhDbJVqVB4ywN8LaYuI7lnVBVu712RUfzAOeYSiDcjhlaOD7zo62GYNNdPk0B4Ba21PKqkv/pCvgz++8dsJ65XDfXItyYFVPRjWinpDcor96EPhzr2x3HARULGHnDnsgU7HpfrIufa26E+n/vuuExqkntaFhqc/Z1Yn6nCsPNOxvPCQXGXx/W3mpKx2/OsWjbKRuWgRuOaZQUWIlcHdbtuM00K53smva4gmKy1USEJAukL2jOF+YM5N3PeHY7mmVZQIbMISd4QkpgJuDLOUBBJCyDE0cdHbjUzi/cJE7MP9yenrSfhg/hRMGalhArbUFVIvWX6/OJxXCWQU1n7D/nCMAszPyijGXqsKxxcex9xOXpQpC8cTNN3RN/OBKpn9kAFHQVjTAW25Cwvp2ri1k65w9ZbASd7HVPfiZ95eG9jP8n2LLdGVlgnWIAzFK6r53QvZZQM/SnFxA4LFaiEBVEZiOctVt5elulbUQ1Jb X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: dde0f0c5-b118-4612-6212-08d8eee6fb4e X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:05:10.1010 (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: J0pVuxeA1o2eF/Zz4RBpJCm+WnNxASHpTnAGZysMMdx66R0HPBdhS2q0KKbnjSrDX4gCkl9V/P0iO8XUO9g3RQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4382 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The SEV-SNP VMs may call the page state change VMGEXIT to add the GPA as private or shared in the RMP table. The page state change VMGEXIT will contain the RMP page level to be used in the RMP entry. If the page level between the TDP and RMP does not match then, it will result in nested-page-fault (RMP violation). The SEV-SNP VMGEXIT handler will use the kvm_mmu_get_tdp_walk() to get the current page-level in the TDP for the given GPA and calculate a workable page level. If a GPA is mapped as a 4K-page in the TDP, but the guest requested to add the GPA as a 2M in the RMP entry then the 2M request will be broken into 4K-pages to keep the RMP and TDP page-levels in sync. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: Vitaly Kuznetsov Cc: Wanpeng Li Cc: Jim Mattson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/kvm/mmu.h | 1 + arch/x86/kvm/mmu/mmu.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index 70dce26a5882..e7c4e55215bf 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -110,6 +110,7 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, bool prefault); int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, int max_level); +bool kvm_mmu_get_tdp_walk(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t *pfn, int *level); static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u32 err, bool prefault) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 33104943904b..147f22bda6e7 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3828,6 +3828,35 @@ int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, int m } EXPORT_SYMBOL_GPL(kvm_mmu_map_tdp_page); +bool kvm_mmu_get_tdp_walk(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t *pfn, int *level) +{ + u64 sptes[PT64_ROOT_MAX_LEVEL + 1]; + int leaf, root; + + if (is_tdp_mmu_root(vcpu->kvm, vcpu->arch.mmu->root_hpa)) + leaf = kvm_tdp_mmu_get_walk(vcpu, gpa, sptes, &root); + else + leaf = get_walk(vcpu, gpa, sptes, &root); + + if (unlikely(leaf < 0)) + return false; + + /* Check if the leaf SPTE is present */ + if (!is_shadow_present_pte(sptes[leaf])) + return false; + + *pfn = spte_to_pfn(sptes[leaf]); + if (leaf > PG_LEVEL_4K) { + u64 page_mask = KVM_PAGES_PER_HPAGE(leaf) - KVM_PAGES_PER_HPAGE(leaf - 1); + *pfn |= (gpa_to_gfn(gpa) & page_mask); + } + + *level = leaf; + + return true; +} +EXPORT_SYMBOL_GPL(kvm_mmu_get_tdp_walk); + static void nonpaging_init_context(struct kvm_vcpu *vcpu, struct kvm_mmu *context) { From patchwork Wed Mar 24 17:04:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408309 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7E62FC43600 for ; Wed, 24 Mar 2021 17:06:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5DF3661A13 for ; Wed, 24 Mar 2021 17:06:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236999AbhCXRFq (ORCPT ); Wed, 24 Mar 2021 13:05:46 -0400 Received: from mail-dm6nam11on2061.outbound.protection.outlook.com ([40.107.223.61]:33120 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237064AbhCXRFN (ORCPT ); Wed, 24 Mar 2021 13:05:13 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Skg/rKVq66h4a0lz7YWpeLJ8DdO8FGjyBACA1WJ1NiHNcWbTuIgiAthIN/WtmxvSk29QRPsrM927sqMvp0kPBooiH9Me8pRdHekE8xcDV8c2nDT8MOdQtb2hzNhKCKu3Fn25P7bhHMynslY7nz2c9Ek1+39c91AP4arkfFzjoIuLxJJwKhHekRxR8+60JVTLgF3MCMJhz4OfEzjA/X0eatNyVnsAwA4EpZvRgx/Ky2CESZ0DkUFGWvwJCTw0j33XWbJRDV99geybHfizlO0eSeStgj4+dQLRCSnG6xmYFgEAFn47lYqezZrsXN3GaTsVhTehf1+ca3e+tScKGpNM7Q== 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-SenderADCheck; bh=KRup7kY4fRGY3mEwn4QZs1gDzyCYuXw7CLdv8NxS2q0=; b=Iqpi5veuZQBPZEn+VIzB9zI0OwtuOjCK13OaelLmXj3Yj8KH8K5Z1c2NqFIKU1Y3Yuy6Qqrop7eIwYkyPwWFa8rk3ltTV5jELpzed6nzUrkc7pa7MQfyUZi197s+AiVItu2nWSzUs3Ejm7PIsmf5Hnr15E5uuJf/pMrd4eN3aqH8lfoaCiQ5b13gt6pQktvEBYDz2aehXfYggx9qKgtTesLf1GqDXt1JPM2TYcZGwiUVJOGwQYDa1hztGaJwOz4Inh9HxtuDME1Nfg5+ijwkR6/CyeKwRY+TkXZKSAM6EqRlJ0CHxZ8EKRf++MzZdGSZ8kTUOWZ+Y7jH6JNi0WKCuQ== 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=KRup7kY4fRGY3mEwn4QZs1gDzyCYuXw7CLdv8NxS2q0=; b=BDvIkjzvxCaRulah1FFHKJT80iyWRG8MjBivfq5WOyuzymeqPJa/0z0SBdikK8D4E1DFg0rORxrfPwbCd6m1HzjpLD4A8zHde2wq/fLB//AJdwxh1yiJWOZyRjBBGx77pLDKV896MYXprqsUPUqJWzpJHXezDWfOG//DwRlZm6M= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4382.namprd12.prod.outlook.com (2603:10b6:806:9a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25; Wed, 24 Mar 2021 17:05:11 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:05:11 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson Subject: [RFC Part2 PATCH 24/30] KVM: X86: define new RMP check related #NPF error bits Date: Wed, 24 Mar 2021 12:04:30 -0500 Message-Id: <20210324170436.31843-25-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:05:10 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 35b5b1d2-6d69-401d-b35d-08d8eee6fbd8 X-MS-TrafficTypeDiagnostic: SA0PR12MB4382: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3513; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9WalZLa9Sa0ASb3oZ8Dq9UUDsySh0w79yNjaJwNjTxsFJrTzyrA7h0a3JPOwEm5olUugcQjnIdQDoFy9nbZiAwnleUJyySbVS6XcN1LXb6YpwHc7VhqhHKuz8vfbF7nuRcr3a2cQBeg6oe0464gBKEVGhhvVLCpTXsEV6+z7/Doddi2oq+5UsS+klMnY9JRpsTBpfi4Exrju+62M1jKblfmDdWBnyjGvL4qvssoYomgBenXKvtQiAZkIKbyaQxm+yJ+lx1oOpDPkom+xqNszrVBJhEKiRK3tS6syWwC8188NYPmP9pP3eMwjKOlPvJxhwvFYO3l29khqRKDtC0+habyhkhOMeWN9zF9nDzWxznE/xNMBTAPE45SjhWfmgaPWasknilcKf9CVjXRurPdTohMgu8kPSp9gePzqkWi6p0j5tAb3fJy0FKg6MSE8CFqGYeHifib1EoWrPEbAUINBi8VQXMZ/ip1eUgQn/APXNi1Ife5lMbuHi6LMBmMmL2r1PoZ0oKV6r/3EKJ9JM9hIVa64+lgpflu+eInx4NTVd/rhPwxDTHtaYixBvkja4xtQJdsmo64qMsLtHSrm75WzIr4NkxtidOK2XFaXNlDYNk2RtDySjax2o0gwPXOVs1hQ5qJSbJzQuk52qcxpz9V5aA== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(396003)(136003)(376002)(346002)(366004)(39860400002)(6666004)(44832011)(66946007)(1076003)(66556008)(66476007)(956004)(36756003)(478600001)(2616005)(83380400001)(8676002)(2906002)(8936002)(26005)(86362001)(5660300002)(52116002)(7696005)(38100700001)(4326008)(54906003)(7416002)(16526019)(6486002)(316002)(186003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: stwoRDOQ/QAyDOBDF81I6E5m1kikqeIzlLdfQocm2kbvUC2k32rCbWEwVlOabafgbkgFpKp6xdIgqhbF4HothekrAi33u8WyVGccofWtx2C7N9u8QuoKPKe6NtONsUXVuykRKGoGOmwOQIjHZVhYaEOOXLhjlRwUOa6byAKMSrgU377QQ0LHd5NDFpyqMIe9ag4G9GtWV69MGLF4b+oFcvzHpBKqi35rZZ4suADD45hxwea5KfO8SjplrgOgJpw/Xi88A/cmW0ywbyv6UvK+qRPLXN4hagnU/NfWzMiWW2sD+D59rJp/ShmWoEjiOqLSwYAZFPxX+RU8mJCw9hUN7NOPvtK2/CpPcjTPFtmgKcPjL3H/gVFh0KQ2RnROXzsJVq3k3UCFSIY+i0FS17M71q811+A0YdUaWNpcUYacAYdjzAa94dvG7+nJWf8C3/65E/JL/VN6mOxv3+Wvg4UUnFeQjSF8j/gYDB2qSJiMx4fSCm9rxShVQBO/nVPJejQWyrlMmHEWO6/xjQsHMuJPE6pVVs9nMHfWqLXbnQ98iuYuQtWHR4raF95vD352FA38QkMGDCyPZRANYbC7DOwRf0AOtHq3R+422m5iiK13/fl571yzVvEK6RXKxRUUPgSuhqttpBZt4ktYI0nfSYZOhZF4rxc3RXl2M97J5dFNzg7VV88oghxWAm2gIKtRMfxoQsXXU/IqzbUvP5r8k9igOcF1x4xA4Wx371jAmf5OuU01igncw2Xwc3wkahfGKZoJOyFlbBmhNEom7mh05wTJK9VyQ1BxcngFYMLq/L+LvUR2mUigj/KRxRAbyZNLjkGVRypx3uUIuDW2pzvUSNvd/e5hr+yQziKkK5J+R9NBpUzbhGc/WUIw0w+uLku9nw5+FhV0AcYxMjpS0ETsWH18PCQ43GYJpVkfKe9ABysXFftG5tlsOFFZ4eARFZyCCx8yVGFCAAW494AyoK3631EpqBD/3pJzlgYF59Floo7hUshTzJ5a9CRbUf1LvRClDVsr5q50vxz5mQFP5pWpq+yHU1OE3Ds12Ytt416jWxO+VT4FTi4ZzWBHNMS0fPxRtT2pvtowH7kU7ZsVF3wWjVBFnqtP2okLV2eVjChLdZiwEiNXy6UiGH49q8bz5zV3cSe5pLeUaj0I2pa9OI0aZluKAYSzr9yqbWMTwraXn+44qq5cNfxJCwbmG5EThdP4mIb9B3pCDwuakfNyZsVKc7RF5vhAxMYSFdNzS8qPVLO22SVqp1QRjLCspsE2IwOutpLe/eclB4GADN05wOpNlEDzRlWLayeyS20nIffW7nAw+mVi1YrBR9p/hRc6TRDRa+IR X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 35b5b1d2-6d69-401d-b35d-08d8eee6fbd8 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:05:10.9795 (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: N/x3UYRU0Wjt+QbqfvuVsYM77MEyscha0maO0WepxefaRYdW/eKgZtIV4CbthsY8aCFNBB5jRE++DjSnePcfMw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4382 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org When SEV-SNP is enabled globally, the hardware places restrictions on all memory accesses based on the RMP entry, whether the hyperviso or a VM, performs the accesses. When hardware encounters an RMP access violation during a guest access, it will cause a #VMEXIT(NPF). See APM2 section 16.36.10 for more details. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: Vitaly Kuznetsov Cc: Wanpeng Li Cc: Jim Mattson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/include/asm/kvm_host.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 93dc4f232964..074605408970 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -216,8 +216,12 @@ enum x86_intercept_stage; #define PFERR_RSVD_BIT 3 #define PFERR_FETCH_BIT 4 #define PFERR_PK_BIT 5 +#define PFERR_GUEST_RMP_BIT 31 #define PFERR_GUEST_FINAL_BIT 32 #define PFERR_GUEST_PAGE_BIT 33 +#define PFERR_GUEST_ENC_BIT 34 +#define PFERR_GUEST_SIZEM_BIT 35 +#define PFERR_GUEST_VMPL_BIT 36 #define PFERR_PRESENT_MASK (1U << PFERR_PRESENT_BIT) #define PFERR_WRITE_MASK (1U << PFERR_WRITE_BIT) @@ -227,6 +231,10 @@ enum x86_intercept_stage; #define PFERR_PK_MASK (1U << PFERR_PK_BIT) #define PFERR_GUEST_FINAL_MASK (1ULL << PFERR_GUEST_FINAL_BIT) #define PFERR_GUEST_PAGE_MASK (1ULL << PFERR_GUEST_PAGE_BIT) +#define PFERR_GUEST_RMP_MASK (1ULL << PFERR_GUEST_RMP_BIT) +#define PFERR_GUEST_ENC_MASK (1ULL << PFERR_GUEST_ENC_BIT) +#define PFERR_GUEST_SIZEM_MASK (1ULL << PFERR_GUEST_SIZEM_BIT) +#define PFERR_GUEST_VMPL_MASK (1ULL << PFERR_GUEST_VMPL_BIT) #define PFERR_NESTED_GUEST_PAGE (PFERR_GUEST_PAGE_MASK | \ PFERR_WRITE_MASK | \ From patchwork Wed Mar 24 17:04:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408955 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 310A8C43333 for ; Wed, 24 Mar 2021 17:06:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 12136619B4 for ; Wed, 24 Mar 2021 17:06:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237191AbhCXRFp (ORCPT ); Wed, 24 Mar 2021 13:05:45 -0400 Received: from mail-dm6nam11on2061.outbound.protection.outlook.com ([40.107.223.61]:33120 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237065AbhCXRFO (ORCPT ); Wed, 24 Mar 2021 13:05:14 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Gf0qebKFykfm5id4QygZIJBwiHcxpKtrT/NFDABHN6O+w1qENWEfM5/ieHb89zvbVp02hsc93iS/kzETwmhLmiprTl+04xCSy4LHxJkgpIWF/wVJdqo9TLV4RIw0xLdt5kp9mFpYZVIcyMEJJsfJ1DFUtWIQQ0JgHNnnfsQOq7IMN5Dqs623TKFD9OwoJEm5MCFfEwQC1L8DkWifbQdRM1BkzDTd2BmhxjMM9CLN5kT8yy1jcqla+9DbmkOHgYns/7Dh3tlIma2zuMeqQz0MbnmPqnhv1a9KVj2yHNBxhjrSs65memFEIlih8VNZls0xjzYR8Xgq8cIlupOp43fu+Q== 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-SenderADCheck; bh=Nr48WiKmgDb3qwxztPkZbWysX4pDIJ0p6abxE6mpxNY=; b=HsdvlAV4oR2xgJaNSWHV91b2m91xiS8SsblJfa5FeNvh4aBU7z4doTl4X1bAQJ0CZsm4Y+Z0p2VjUfGq5PdDzetf2jkC3lsua7c/iGj6bl7/s1AWOP0U2pVZ6h/XIfiBNxkzwtcs7H4GxFuovXW+IGhDLUttsu+64lKE54t0f9c7XHHOPLfevSW60TY8TkaUziZddYQoR+ZoRHqBMKktx0ZSdyZLS8DaEqr49YqddkZa7lHFT+OAjWZSqWWKYGcUm4rq9vAmv7G0YolsLUFCfiSmrfiN7oA0ZDXCRMj8hbBjEj7k5/M3T9HbwyW6CakwrxvkyJds7XLOHnVwPNrATw== 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=Nr48WiKmgDb3qwxztPkZbWysX4pDIJ0p6abxE6mpxNY=; b=iwkHsAVHrHaZFchZ6s9yW/E/1/CiZGRdSUywOemrRStPsnoZ/wqMYUrf63dHDlDpkGRclPwu9t7CTyIQQJ+3B9kPZA9mfnUXKB+DwQKBa5hhgOTxZ2gSq313ACcFjjBMsY3PIAoK0lyYm/i6T3ah9EfZM6vhaoMyj4teGu8mA1k= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4382.namprd12.prod.outlook.com (2603:10b6:806:9a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25; Wed, 24 Mar 2021 17:05:11 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:05:11 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson Subject: [RFC Part2 PATCH 25/30] KVM: X86: update page-fault trace to log the 64-bit error code Date: Wed, 24 Mar 2021 12:04:31 -0500 Message-Id: <20210324170436.31843-26-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:05:11 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 3a74d890-cd5f-463a-f889-08d8eee6fc56 X-MS-TrafficTypeDiagnostic: SA0PR12MB4382: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:5797; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: sW+b3GiY9HUa5zenSxkAvLB9BZCscPkgqjyKiTWLF9z9XREQR2Biw/4k00h6Otr0CgLOwAPFpGcNu8/MD3jwocj5rzWcEOhN5KFKORHhT4T7YxiD59gSduapvfpYH825zU+We06AEyg8W5KAYnXRuAzPqQdaXtYwJXs6bZnweiOItHzmK6hpRWEzzlVryARpq7eGdTObBr8GFRVF0+NA2UWWkfjoUrWFZjYRujYyeLpWAnDyshK2+9qS5rplfq+8VkLEEJAHz/VxEPnLztjpTG7GrCUgRBd/KSVyyP4zJ/cnruGti6KsWjwIrdMEB27K0g4wegM9CLyJVfeKcX8v9XGOzDe74FbUVCUSLBOpy1tOqo/5nOovc441o9HgWxpwNsYWdWvNSeFwfnEh0bu3UWcebvEa7ngOmtG30ftgQ5MZXyEdWN0YHBTBlY2s8tp61Is54mcZEJ+KmwrMzvlKyFZ4GAWjkfReoPclk772FBeroxrldJ2hSvrQTKs8gPaQgYmGNjfd/CwQzlyr7UHGAIkxrRp2fvrSsVqLkI9KxBoSP+U8MeZaDUgfVtCtWfUU4zTkK+0fhlrDY5LybyJS/cu9PmrYNYhEr+nJVuwEYqWd9AcFPl+OlpNJbYPosDE45KngRAm8huqQvtJjNxqEdQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(396003)(136003)(376002)(346002)(366004)(39860400002)(6666004)(44832011)(66946007)(1076003)(66556008)(66476007)(956004)(36756003)(478600001)(2616005)(83380400001)(8676002)(2906002)(8936002)(26005)(86362001)(5660300002)(52116002)(7696005)(38100700001)(4326008)(54906003)(7416002)(16526019)(6486002)(316002)(186003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: S63ZhysfwVoIwbAUNrHoZ9LUNg3LDkAIkPu0m+RNHzCrvUEy+9KBze4Ho6+modkjJHhhhr1gSn344sGl4xxMgFq5+Yz4Nas7xspr/U5kNDMfIMeldbm6xzOpIP3qZh70NsH3LFxpc9rUPxxrsW5ihXfRLXkGaAYBIzsrsy/fehU/gBx2lPHZqing+Sh7u4AFnLzyS5wt6cGuTpMJzlc7pSdoDJ0koH0WB03ncsjBFhfpbyIh/sYQcp+pDh1EQOX53iQIxJp5ui8h7LIBHzJB5wuzi0bg323lUiHv3dmjs3KR1ehdac3VByiifPsbLk3Z6KpDZaLRvhpIlMkmmP6jdmuqchRl0BMBMxb6IAw2O5Ne94BKksNnONS7XrKldf5Zclm3Cu7D81O+b2VdHXppQA+v5ytapV+Gd1cDYKLujfDygM6rd/sdf1CeviqSbRb9uQ7i3MTdIUaLr3tGea7TT2a9vhVfzQAhCvl/b48mIgdbQERjnGlRaHdhKMTnB3kKt9Vc/8Dsr1NSFVO5jl2WbSeJoYTPMJ/fzgdyhzyFuBHIkHtTIkMGMJg2wPtlybTppakxcZFz3GUKI7r2dCDVhzPlxv8rwGl8QEN9eF6ongHSa0S6RifsejZjwd+LPvZGlAnDg3+eAYQrqPEGR8gc/Gjsc0IVBI4FaPbjrXlQz5YnQvCNXtgj4HgNnXU7CnyKR2cF59FjkRuJwPtbRdPUO5X7bVf208QlnAVKxK8y0ABbTd0PnBMeRR1K1N6CAoBbhML/i17aZjK8j9jIr1N/uQkw42r89Y9vWl7en6P3OVs3g21WFwC5Brvgc0mCeajbO6LxaTqDkJlOqCLFKmKYZFKvvfUnvM0B7oNZtJ2vKjMBy8mR54NNy7cmgiSzqpnzf+yf67u/1al2zqpC7mUZ3SLg+KJj6ntwqS25sP2tox9c/MhSdiej3gqAnzQ3DsG42MfaVfgumPRGtfb+GEYXuMMXaeq37tA3eNi5mPkuUpAV1VzRUUeNHPYrbCccLrZRtYSSYiOSeZSxVkeVPasTpQW6iadokuRyboHq3iLDtv+/itHekH+ZRT5uXXe4ee3+zPdfNUKF+UdTUax6BfdfPIifK7XgwlK2Xlzn84rEUYtoXRxPJxaC2yYrV9cg6twm3tLkCyLjCLnSE9B5/5AGM/aghNPI0zt/6d7yIYHPh3NvoJkTjqrw1xuiFbQ07HeTcDTNyqfPw67fgtYH3JFhEtZ4ZtAghzPMZJ6xWpPzGbYPrHl0YmsNac2gmEMmAJA0EAWAJ5bujZv/XTWBJeb4eE4S2wqy2fKN84NlRqrFw1HHrJiMTQtbSUBWONir3MhU X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3a74d890-cd5f-463a-f889-08d8eee6fc56 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:05:11.8051 (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: 0+XoHA6q0hgIa0Swsi39pe7voRfivWGeCK7P/vNpfoGCi1WLkeT91SB84nUjFmAqPWQL/pIHrJv3zVMomSfEfA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4382 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The page-fault error code is a 64-bit value, but the trace prints only the lower 32-bits. Some of the SEV-SNP RMP fault error codes are available in the upper 32-bits. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: Vitaly Kuznetsov Cc: Wanpeng Li Cc: Jim Mattson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/kvm/trace.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index 2de30c20bc26..16236a8b42eb 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -329,12 +329,12 @@ TRACE_EVENT(kvm_inj_exception, * Tracepoint for page fault. */ TRACE_EVENT(kvm_page_fault, - TP_PROTO(unsigned long fault_address, unsigned int error_code), + TP_PROTO(unsigned long fault_address, u64 error_code), TP_ARGS(fault_address, error_code), TP_STRUCT__entry( __field( unsigned long, fault_address ) - __field( unsigned int, error_code ) + __field( u64, error_code ) ), TP_fast_assign( @@ -342,7 +342,7 @@ TRACE_EVENT(kvm_page_fault, __entry->error_code = error_code; ), - TP_printk("address %lx error_code %x", + TP_printk("address %lx error_code %llx", __entry->fault_address, __entry->error_code) ); From patchwork Wed Mar 24 17:04:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408952 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A9D92C433C1 for ; Wed, 24 Mar 2021 17:06:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 90CF661A13 for ; Wed, 24 Mar 2021 17:06:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237205AbhCXRFs (ORCPT ); Wed, 24 Mar 2021 13:05:48 -0400 Received: from mail-dm6nam11on2061.outbound.protection.outlook.com ([40.107.223.61]:33120 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237066AbhCXRFP (ORCPT ); Wed, 24 Mar 2021 13:05:15 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=W/Yn4pyz2BuJDR3yMdEXRqrTDDV+xeOVP0BBAvQwI2Hq3YmkQIlrTJrEvQjFAjEKqmO8lxOGngH8LkH304xXxioo6cHevZV/l8ECCOETtw7FHtztA4QASgxnv3N3zaCS6oGlEf4WeIdZvt7R8gRpCeDUj/yNX8AF/51IlFdtDK3ajiK97Msch6fmrls4pd5N9Wx1ke4U5XnCg5NXVbvIbgB+/CyuyGTWiVe1IwByfriEF6II/6pEpWLyuhrSKB+Z8sxvcH9fbpUxTpizF8ZJsdYIUvXdTVRpsFMUBK2vZHMNqeKcf6QLxX8chLCIonhanLaVTzyedLd0kFCrS8qiug== 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-SenderADCheck; bh=QU8SJa4pGsdar656VjblxPQz8w2okpR/9hoCkmfzbmI=; b=dbihrTaILAOcosdfAROjSeZepBR/+MnHvmkRIDAUSzop0XacpZa7vu0bBekYseyiaii8gZrfNCjNZds1b5pk5/9WkkwkOMuYmjLZwLP2hW06kEQkL7KWCCRjuFKBgtbsEzqgW9Od/MT86DCJ0T2WzrD6QPfFRN2+Ci9QzZUDxGN2IflRPl0He3hLW8trRZ/OYi308AizDa2k9j1IWzuBKWMxfvxRk1JQ0upwXnN/mC3w4He3B5HJqp0Ws1Bj0VcT8aSEHmOWy2ZFtOSqu67iZ22UXLJ1N1pxlGqbcaXxrq9iXxE4tjpRI8RtTColBc30c/S9or3KWQl1UIo5TaWupg== 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=QU8SJa4pGsdar656VjblxPQz8w2okpR/9hoCkmfzbmI=; b=IE9M+hIffUhKnHAGJQGJU3xOnJgC5IdT4s01TIlS7QdXF2v1+IYIqw5tpftyvH+OT2MWrbmwOzH8LJnEMnQMmQkcA378GYhaSiSJA6H5AcO+HDXAhAH7C2WvvFEyL3i9/Dga0QtHFh3p0da6bsz1I2nVndfILmdPM+G28Xww18M= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4382.namprd12.prod.outlook.com (2603:10b6:806:9a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25; Wed, 24 Mar 2021 17:05:12 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:05:12 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson Subject: [RFC Part2 PATCH 26/30] KVM: SVM: add support to handle GHCB GPA register VMGEXIT Date: Wed, 24 Mar 2021 12:04:32 -0500 Message-Id: <20210324170436.31843-27-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:05:12 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 2d6fcdcd-add5-4974-a566-08d8eee6fcdb X-MS-TrafficTypeDiagnostic: SA0PR12MB4382: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: y9Q7eQqXyMtAKDHjEKE3UKFfGCB5BCoacu1LoJ1N3XipYqIavgrrwmaURUEhNZTTwfu2Ug3hKWzFU7lwB6T92dNcc1ptNU8+2G8Vixk2FlnXcOyqueJ/QcW4DFK/l2sfEmruuDfClC08dYhdwcT0FKy95blPWz3voBKeTz2L70KfC4luZDtZc7iK82f6gGB2H6rMYsrT4BmHRDRXObzx3YNxk9IAkD32jk5FZ+wWP318FC1pI2oFM5ctMOZtMMPpwj64m8BiPo298Gf7auPpyfzeSO6uyeDcpb97apTBjIomdYBcuxJx7vYBxI1c3/2L/AaK+v4u0b82eLh7irR2HPZW1gaqzkUDjmtvnuYhQO1EkucCSQuPhyRQtO8U32TEXdyBKKrcc2hQYWcdIex/8ZgPrhj8Z9Gwb+o5IOeLGEw+EbZgY9ySTQ5W/3rxQsNdGJyE6DhRsTpIRPBaT4dTz80HI9cvHPjhtz4Di9E1gZx7fhdDYQjlVAbtMz3TQ7BdJ3jKl6aNaImPQQiwefi89GvZET8RzXN3+oMn1e351g+3CoYv+c8kbMeRJcy+qPC66sOEPGWmv2sZf0uTAw9HIKtuCaX5vTWgOtWSYvL7VmU387ludo2ZklBlXfH4ZFhHHXDiagfzip76ykraLwZzYg== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(396003)(136003)(376002)(346002)(366004)(39860400002)(6666004)(44832011)(66946007)(1076003)(66556008)(66476007)(956004)(36756003)(478600001)(2616005)(83380400001)(8676002)(2906002)(8936002)(26005)(86362001)(5660300002)(52116002)(7696005)(38100700001)(4326008)(54906003)(7416002)(16526019)(6486002)(316002)(186003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: DPwDTqcPTkTe6Tj6sUupVvhFMkAT7xujvfaNtGOxnvQxk/zNCNE8s+dk4AkbvLxEuMoLt27RgR/DHUOXHRakepI7EEZ35HS4XGNS69Z7iwjcd4wrwbKbVMYcRPATKvLKkWbzjp/dYMINUs2WHN016jtH6TgvFmuAynfqv5zk05Rs+pmtB00XiJAbJSjxtzExQ6uyyQRFnnHKWhfYF1vZXUVf7OYKF+NxXJy0qzJGQnbmMk6jn+3hMCHeWmMV7OWQZnX5z/l1HH+z2kKxw7k0Uk5IzEk4NlAHQx40ElpEDKqiU+36L1LzUByfl7SdRE8QHikYZkvZDE4L2055LVbuclWSh7b0idi3sCNW31LTqxfJpafX1gjFPXWQzdd+cnhqNs/agFqdT0PdEXfX7lw5HQ7enxl5twUmL0ZjpggT6sOElQbyIvmC7pc3h7/NVN66aa9RaeJcS7zxifZwb9Q9MwrDF/rvCwRh4oakFNcL6gxbSyhjfvTpTPlvKkhIPbhjocw5aRipw/1n51jQn5ECzaHC8czNsJoH7sNtofXnz5X0SLkJL6oYL7Cj0Hk5v4eYEi9aDeGbt1TmrV5Dfkt8BFDGK9/CJlVi+VBoObmNvOrF/+rC/2OgbHEGflBKBucNBt4Qe/cIw3aJd9J8bgCqYnrcDdBG0YnnTqyUGYDMTbZ3p1Dbp9pRwYLkmSZo0m5kyaR7W3S/lWPnVMijT9JRfxcvQ6/URzjQoEInjoPGjGwL3dFoENu4qxu3jvOCpvflnd6vDQjslCx5YmHAD6JcTYg4V245onJNXQYfF+BmcFwFzWZ0jx3nSpvIiUH4kqtaTcRdhHT22BOSq44P/NarlRB4VGlogN8hrDBjPjRk7ZbltKK0jdLHfokq2rYtapDA1dx/llvbAUd/I7/uOgN0QvyzjOSsGr0da58xEwo2Q/wE0OrfczBKFimTXbGD4iDvzbvnXquT86BuJ0LUhbkeHztXQDzQtYKkHXZb5bTBiW92InvQofYGSX3s2cf2gY0Jb7TroU/4oj4WB7l20dncOcadjLH/8Ra/Z1OWk8X/Ap/nMMEzGWds/9z113G9jLXjwQs0CNqrUSmQ7bRD8VX8Ez73hPOs8fRpPYA0qtFhDhjWYaf3qTiiCZsMdCKf4UPV8lCB0OUywpotSHg9qmmlRV+pGJeYCiQXPpe5HiMhkUJ6oBMJYBNIeneLFXlbVrOKOJOIltkwI/kLoVanAJ+T54lM32s6UwmW/cIbFg8iGO3gL7O9C0LtSTM/DBpaivVtN5dthrXPeehrjL1qftVDctTiEdmNjvyKatzEPuN+ZVICEBPJ6+yQ90N942AO0B6E X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2d6fcdcd-add5-4974-a566-08d8eee6fcdb X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:05:12.6916 (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: X5fHQ7Z7tmctlKMgrKfzP6Z3XLzkpCRrsl4R8XX5i50+dd3nFZ7J3mNoltdFlOdGn/Ik8Kz3yL5XiJ0L6UmPrA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4382 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org SEV-SNP guests are required to perform a GHCB GPA registration (see section 2.5.2 in GHCB specification). Before using a GHCB GPA for a vCPU the first time, a guest must register the vCPU GHCB GPA. If hypervisor can work with the guest requested GPA then it must respond back with the same GPA otherwise return -1. On every VMEXIT, we verify that GHCB GPA matches with the registered value. If a mismatch is detected then abort the guest. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: Vitaly Kuznetsov Cc: Wanpeng Li Cc: Jim Mattson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/kvm/svm/sev.c | 28 ++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 15 +++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index e66be4d305b9..7c242c470eba 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2378,6 +2378,28 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_POS); break; } + case GHCB_MSR_GHCB_GPA_REGISTER_REQ: { + kvm_pfn_t pfn; + u64 gfn; + + gfn = get_ghcb_msr_bits(svm, + GHCB_MSR_GHCB_GPA_REGISTER_VALUE_MASK, + GHCB_MSR_GHCB_GPA_REGISTER_VALUE_POS); + + pfn = kvm_vcpu_gfn_to_pfn(vcpu, gfn); + if (is_error_noslot_pfn(pfn)) + gfn = GHCB_MSR_GHCB_GPA_REGISTER_ERROR; + else + svm->ghcb_registered_gpa = gfn_to_gpa(gfn); + + set_ghcb_msr_bits(svm, gfn, + GHCB_MSR_GHCB_GPA_REGISTER_VALUE_MASK, + GHCB_MSR_GHCB_GPA_REGISTER_VALUE_POS); + set_ghcb_msr_bits(svm, GHCB_MSR_GHCB_GPA_REGISTER_RESP, + GHCB_MSR_INFO_MASK, + GHCB_MSR_INFO_POS); + break; + } case GHCB_MSR_TERM_REQ: { u64 reason_set, reason_code; @@ -2418,6 +2440,12 @@ int sev_handle_vmgexit(struct vcpu_svm *svm) return -EINVAL; } + /* SEV-SNP guest requires that the GHCB GPA must be registered */ + if (sev_snp_guest(svm->vcpu.kvm) && !ghcb_gpa_is_registered(svm, ghcb_gpa)) { + vcpu_unimpl(&svm->vcpu, "vmgexit: GHCB GPA [%#llx] is not registered.\n", ghcb_gpa); + return -EINVAL; + } + if (kvm_vcpu_map(&svm->vcpu, ghcb_gpa >> PAGE_SHIFT, &svm->ghcb_map)) { /* Unable to map GHCB from guest */ vcpu_unimpl(&svm->vcpu, "vmgexit: error mapping GHCB [%#llx] from guest\n", diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 9b095f8fc0cf..0de7c77b0d59 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -194,6 +194,8 @@ struct vcpu_svm { u64 ghcb_sa_len; bool ghcb_sa_sync; bool ghcb_sa_free; + + u64 ghcb_registered_gpa; }; struct svm_cpu_data { @@ -254,6 +256,13 @@ static inline bool sev_snp_guest(struct kvm *kvm) #endif } +#define GHCB_GPA_INVALID 0xffffffffffffffff + +static inline bool ghcb_gpa_is_registered(struct vcpu_svm *svm, u64 val) +{ + return svm->ghcb_registered_gpa == val; +} + static inline void vmcb_mark_all_dirty(struct vmcb *vmcb) { vmcb->control.clean = 0; @@ -574,6 +583,12 @@ void svm_vcpu_unblocking(struct kvm_vcpu *vcpu); #define GHCB_MSR_CPUID_REG_POS 30 #define GHCB_MSR_CPUID_REG_MASK 0x3 +#define GHCB_MSR_GHCB_GPA_REGISTER_REQ 0x012 +#define GHCB_MSR_GHCB_GPA_REGISTER_VALUE_POS 12 +#define GHCB_MSR_GHCB_GPA_REGISTER_VALUE_MASK 0xfffffffffffff +#define GHCB_MSR_GHCB_GPA_REGISTER_RESP 0x013 +#define GHCB_MSR_GHCB_GPA_REGISTER_ERROR 0xfffffffffffff + #define GHCB_MSR_TERM_REQ 0x100 #define GHCB_MSR_TERM_REASON_SET_POS 12 #define GHCB_MSR_TERM_REASON_SET_MASK 0xf From patchwork Wed Mar 24 17:04:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408308 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0A5D8C433E2 for ; Wed, 24 Mar 2021 17:06:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BF83A61A13 for ; Wed, 24 Mar 2021 17:06:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237198AbhCXRFr (ORCPT ); Wed, 24 Mar 2021 13:05:47 -0400 Received: from mail-dm6nam11on2061.outbound.protection.outlook.com ([40.107.223.61]:33120 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237067AbhCXRFP (ORCPT ); Wed, 24 Mar 2021 13:05:15 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JeQYem4d57KvWE9n9sgFuyKqBP0tAN9Quv8V8fsfyTVK1J0N+r6KF8j4OSMnnsPvFwkCQa4t33XRqclUJrndlfldOGxd2fmD9kZWKbTSqHQKHQnrJ1huH09oyY7qIzy+5C7sdx0UqliXnL0XEXLQvtDC5UKsVa3NqqDnRvkPrg+fO6IXrJycrC2HKaHIE+gxmR8t4rk7xfkzojAu2zLUZOSgSDqxRhLsF7X2U5iuai9+WQXatPMsbIUjSxK3gNKzsprt1R480CwPZR+Vpj2QGKYZ3u+LIKfp6sKmbAX7oLjp3fvBYS1cFLetxI2UOZGi4kj7JEJEApvFu2Wb1d+xpA== 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-SenderADCheck; bh=nyMcfVc4mLhnGDEYlXoyasE1fLuujpYl7sOTzIFVynU=; b=VgwQGwHFjbgQi+C8klLRQG7Ks1bZWqNY5rOSOm/uJ9kg4iNA5UH51wKuXvEUIUJX0ZHCwnWJQI/8RnyxdlU68uikQgEPegC6hL5zvCHitjeJZmdch31qPAr5OJLqjp2pmdglAZ5ZhB970bBfoac+KrpbA28baBUToKwDUcZLSPg18R2E15zlAMQJ0wopGZOxfy+JlQsAPGavuboRu9Ynx8tSSTfIjFbsLp57O3QQhOH0hkYGX1TfHqaw28As03OZLw2nI/kFHGaxMVBY6V2iSS/LMRHWc2HktJhD72HRqHF4wgGvHmN/fSU+37YnfpYHo1DzDQPzvgqMRJ8NO64uMg== 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=nyMcfVc4mLhnGDEYlXoyasE1fLuujpYl7sOTzIFVynU=; b=N9oTj+SdoWZVE8h+/2K4+uG2JEDsKtR5fcRTmWq7rHY0XCFajy27pyHdZKNJ6cKtbW66dPP8OOE58UPZeJutc4xTiwPMpONaDGBviK7npmbSw5uEjRWoKgCAEQxIY+unzQ2mB2nB2JVaox8xYw/adcJpt4CDvYipUzo8zLnnzzM= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4382.namprd12.prod.outlook.com (2603:10b6:806:9a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25; Wed, 24 Mar 2021 17:05:13 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:05:13 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson Subject: [RFC Part2 PATCH 27/30] KVM: SVM: add support to handle MSR based Page State Change VMGEXIT Date: Wed, 24 Mar 2021 12:04:33 -0500 Message-Id: <20210324170436.31843-28-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:05:12 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: ce5ce69a-4367-438f-f2e0-08d8eee6fd5e X-MS-TrafficTypeDiagnostic: SA0PR12MB4382: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8882; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: nVcNxsea4WxxZikEJsPibpSwnHWw6qqHjHK4TINFBrADxiCM6DiLxmRonZpEPhBJ3+EF3PsrZfXEbkw6nXyJniOkrEUkoQ2N7+w4IUQCcBOwOQL7iJRNx+nJ90e/OSp360mFaivuCtAeTPHNBb3TOuvgU6XYrjTDr0jD36Q3ULrweEv5mw8qyW5ShPxRldBVwvVTOrsENK7jCByShKlDkoP2UOHyQai7f6nsnFI2D2mTMQvRSOmTr6UdpxXjpCCrj302XxaH8D98qudvgxh/DeecoufZvColk72SRN1hChpmawlqCdkQBsOCiKrFPQJJAeHsRuw44Kk/6nd4JTCEIjr4dFF3hHR7vDds0Ka3GUl3wQ6O3NYGCPHM6umC+JL98Ak0vSdzdwOAWT4EKMNREBsXJqa49BNyrltbXagqWLGtIJVTPHMYGkTCrBNPG0VzO6jve5MmsadX6BtJqHBKHkjhooY6QUo8yHiygZ4WqjLMTaSwUvt65JiM0QTSif82KiTv2Og2hNdUuj9bCvZXL/SAvkqKyQCj8jgA4j4fxG9fOH0ynYmO8/9neZwVANKh012AI2EFKJJU29/HgmyOj2YYLoc1CByr9t38WQgVLEwBLJYA7f3XilDphD5sPLgYcIcXUI2+dZptpPi/WmmdTg== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(396003)(136003)(376002)(346002)(366004)(39860400002)(6666004)(44832011)(66946007)(1076003)(66556008)(66476007)(956004)(36756003)(478600001)(2616005)(83380400001)(8676002)(2906002)(8936002)(26005)(86362001)(5660300002)(52116002)(7696005)(38100700001)(4326008)(54906003)(7416002)(16526019)(6486002)(316002)(186003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: tkbBDBqaD/pZXYXk2Ix7MkqeB0yz3njJt/aYizMRPU11nGHKLU136KycLe3BmZr3EAQncjmzxzXn8IpI097Co0TDtmKDUNEFISyaLvV41MEKTlHB3OdlSDRfRe+xCGCG2CIuNVlcjWQRcMkr0txVu0Mihb2+0xQe1sK45FTA5lLKRrLED7mAAFsQx2L3NezXGSeoLCR4HFu3ekGas9JFfzZKx9YWvAfQ9BniMA9Bi9xxxZEQD+J56v2wksflq6JyfgNCfsEoO6GwxGGWKnTQMrlUgW1ckC7+MRtCU2IY+DYTn8xx0XcN4PVCRl4bca2kcWEG9Cx9Ra4rzjQgjcW9tmmdghLyQquABGSscvXvLtVOTmojcF9jXSGBxkB2TdzmPU40VVfhnK33EO2dexhL363+jJaZRNcJ2lURY2pITbhHrHDEuoDeJqfoAJ8FqoUEVI6OiB+p+2kefTKFRVow8VSw5pmMZ3xSolG1XGxk9xhskSzcK1cSCqd5gpbnqug+EJjmYVlFo0rq+IcStya/5o7u1Zl5u9O3zTV99dheaDtTSVZM/4j6s/ZQY4LdRKuaLmPN0zsB3RfJ17JcrDfvTokpHUJqQ/ydR/wXgybl6FR0eOJZVjgQG9PI9nGxCddAxGYkWGX+P6f9mKSkcrMM5vMuZX/lAMmV359i5VqvyfMQqdanLKNdRbkksu9uVqBztZAP+g0gFIqUneoVSBarigWXcYUxigPzS2adqnHho81EwT/zIUvdgR3FIC966UEDyVGUNnPWk4gRJZAM/9z6HVRFaccueByQ6Zsl4WhldAYYpOCs3XNONgKi6bLARQqhLDnpGl0FktmrSUst8M7HLE1/bMQ6Zq+SzbvDA9KCSKPHWBhOVDFSihht9sre3kLo0tuqyOkETjxSDKRZ5hOy2WC3mrvGF7rY/yoTQvSoHG4bW3cVsp/ZJ0xFJAJOF3dYVJkdvX2oDCXc8ZL8D/4hoco0EVgcqoe7KgH4Et/4NzK3sUnbjNwCyjX+Ec96V920NOtdd5ap0mUVVk9hHg8jNE8s5oxGtuUw9CFKH0YxjJh44xw39VTGMBDYaA69oqA0BvTZ5thp8gkCX5XBEMv8OYWZC7rhlY8AGaKyUV0I/oJ91VApZUL/W4g074KpVvxr3CegIorxYSmpQQ06J/lOb7fS2WyiWQOoNfwEG05gBvrZFSmWVgS0KVR0GwlGTkmUfKSfALnf4zVMM6uP+9MkgqaFxuBRert6HzNRQUHT2vKSkY3vme1xK5sv2rRcB9MrJUk2k9zbu+Jq9nfY2/EAiotQ9g0SwyKOLYwVBfJ1EZONdrmAUf2ULB12bpk/5fi+ X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: ce5ce69a-4367-438f-f2e0-08d8eee6fd5e X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:05:13.5631 (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: j3V4YG/6w5wAO8AarvZqgrKT3JyGdcJhACyLBG3dTE7KCqjGtVy2j2IOeKLCDF1rD7wVYSFkg5XIlVGFhvJxpA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4382 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org SEV-SNP VMs can ask the hypervisor to change the page state in the RMP table to be private or shared using the Page State Change MSR protocol as defined in the GHCB specification section 2.5.1. Before changing the page state in the RMP entry, we lookup the page in the TDP to make sure that there is a valid mapping for it. If the mapping exist then try to find a workable page level between the TDP and RMP for the page. If the page is not mapped in the TDP, then create a fault such that it gets mapped before we change the page state in the RMP entry. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: Vitaly Kuznetsov Cc: Wanpeng Li Cc: Jim Mattson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/kvm/svm/sev.c | 148 +++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 11 +++ 2 files changed, 159 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 7c242c470eba..8f046b45c424 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -25,6 +25,7 @@ #include "svm.h" #include "cpuid.h" #include "trace.h" +#include "mmu.h" #define __ex(x) __kvm_handle_fault_on_reboot(x) @@ -2322,6 +2323,128 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) svm->vmcb->control.ghcb_gpa = value; } +static int snp_rmptable_psmash(struct kvm_vcpu *vcpu, kvm_pfn_t pfn) +{ + pfn = pfn & ~(KVM_PAGES_PER_HPAGE(PG_LEVEL_2M) - 1); + + /* Split the 2MB-page RMP entry into a corresponding set of contiguous 4KB-page RMP entry */ + return rmptable_psmash(pfn_to_page(pfn)); +} + +static int snp_make_page_shared(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t pfn, int level) +{ + struct rmpupdate val; + int rc, rmp_level; + rmpentry_t *e; + + e = lookup_page_in_rmptable(pfn_to_page(pfn), &rmp_level); + if (!e) + return -EINVAL; + + if (!rmpentry_assigned(e)) + return 0; + + /* Log if the entry is validated */ + if (rmpentry_validated(e)) + pr_debug_ratelimited("Remove RMP entry for a validated gpa 0x%llx\n", gpa); + + /* + * Is the page part of an existing 2M RMP entry ? Split the 2MB into multiple of 4K-page + * before making the memory shared. + */ + if ((level == PG_LEVEL_4K) && (rmp_level == PG_LEVEL_2M)) { + rc = snp_rmptable_psmash(vcpu, pfn); + if (rc) + return rc; + } + + memset(&val, 0, sizeof(val)); + val.pagesize = X86_RMP_PG_LEVEL(level); + return rmptable_rmpupdate(pfn_to_page(pfn), &val); +} + +static int snp_make_page_private(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t pfn, int level) +{ + struct kvm_sev_info *sev = &to_kvm_svm(vcpu->kvm)->sev_info; + struct rmpupdate val; + int rmp_level; + rmpentry_t *e; + + e = lookup_page_in_rmptable(pfn_to_page(pfn), &rmp_level); + if (!e) + return -EINVAL; + + /* Log if the entry is validated */ + if (rmpentry_validated(e)) + pr_err_ratelimited("Asked to make a pre-validated gpa %llx private\n", gpa); + + memset(&val, 0, sizeof(val)); + val.gpa = gpa; + val.asid = sev->asid; + val.pagesize = X86_RMP_PG_LEVEL(level); + val.assigned = true; + + return rmptable_rmpupdate(pfn_to_page(pfn), &val); +} + +static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, int op, gpa_t gpa, int level) +{ + struct kvm *kvm = vcpu->kvm; + gpa_t end, next_gpa; + int rc, tdp_level; + kvm_pfn_t pfn; + + end = gpa + page_level_size(level); + + for (; end > gpa; gpa = next_gpa) { + /* + * Get the pfn and level for the gpa from the nested page table. + * + * If the TDP walk failed, then its safe to say that we don't have a valid + * mapping for the gpa in the nested page table. Create a fault to map the + * page is nested page table. + */ + if (!kvm_mmu_get_tdp_walk(vcpu, gpa, &pfn, &tdp_level)) { + pfn = kvm_mmu_map_tdp_page(vcpu, gpa, PFERR_USER_MASK, level); + if (is_error_noslot_pfn(pfn)) + goto out; + + if (!kvm_mmu_get_tdp_walk(vcpu, gpa, &pfn, &tdp_level)) + goto out; + } + + /* Adjust the level so that we don't go higher than the backing page level */ + level = min_t(size_t, level, tdp_level); + + spin_lock(&kvm->mmu_lock); + + switch (op) { + case SNP_PAGE_STATE_SHARED: + rc = snp_make_page_shared(vcpu, gpa, pfn, level); + break; + case SNP_PAGE_STATE_PRIVATE: + rc = snp_make_page_private(vcpu, gpa, pfn, level); + break; + default: + rc = -EINVAL; + break; + } + + spin_unlock(&kvm->mmu_lock); + + if (rc) { + pr_err_ratelimited("Error op %d gpa %llx pfn %llx level %d rc %d\n", + op, gpa, pfn, level, rc); + goto out; + } + + next_gpa = gpa + page_level_size(level); + } + +out: + return rc; +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -2400,6 +2523,31 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_POS); break; } + case GHCB_MSR_PAGE_STATE_CHANGE_REQ: { + gfn_t gfn; + int ret; + u8 op; + + gfn = get_ghcb_msr_bits(svm, + GHCB_MSR_PAGE_STATE_CHANGE_GFN_MASK, + GHCB_MSR_PAGE_STATE_CHANGE_GFN_POS); + op = get_ghcb_msr_bits(svm, + GHCB_MSR_PAGE_STATE_CHANGE_OP_MASK, + GHCB_MSR_PAGE_STATE_CHANGE_OP_POS); + + ret = __snp_handle_page_state_change(vcpu, op, gfn_to_gpa(gfn), PG_LEVEL_4K); + + set_ghcb_msr_bits(svm, ret, + GHCB_MSR_PAGE_STATE_CHANGE_ERROR_MASK, + GHCB_MSR_PAGE_STATE_CHANGE_ERROR_POS); + set_ghcb_msr_bits(svm, 0, + GHCB_MSR_PAGE_STATE_CHANGE_RSVD_MASK, + GHCB_MSR_PAGE_STATE_CHANGE_RSVD_POS); + set_ghcb_msr_bits(svm, GHCB_MSR_PAGE_STATE_CHANGE_RESP, + GHCB_MSR_INFO_MASK, + GHCB_MSR_INFO_POS); + break; + } case GHCB_MSR_TERM_REQ: { u64 reason_set, reason_code; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 0de7c77b0d59..31bc9cc12c44 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -589,6 +589,17 @@ void svm_vcpu_unblocking(struct kvm_vcpu *vcpu); #define GHCB_MSR_GHCB_GPA_REGISTER_RESP 0x013 #define GHCB_MSR_GHCB_GPA_REGISTER_ERROR 0xfffffffffffff +#define GHCB_MSR_PAGE_STATE_CHANGE_REQ 0x014 +#define GHCB_MSR_PAGE_STATE_CHANGE_GFN_POS 12 +#define GHCB_MSR_PAGE_STATE_CHANGE_GFN_MASK 0xffffffffff +#define GHCB_MSR_PAGE_STATE_CHANGE_OP_POS 52 +#define GHCB_MSR_PAGE_STATE_CHANGE_OP_MASK 0xf +#define GHCB_MSR_PAGE_STATE_CHANGE_RESP 0x015 +#define GHCB_MSR_PAGE_STATE_CHANGE_ERROR_POS 32 +#define GHCB_MSR_PAGE_STATE_CHANGE_ERROR_MASK 0xffffffff +#define GHCB_MSR_PAGE_STATE_CHANGE_RSVD_POS 12 +#define GHCB_MSR_PAGE_STATE_CHANGE_RSVD_MASK 0xfffff + #define GHCB_MSR_TERM_REQ 0x100 #define GHCB_MSR_TERM_REASON_SET_POS 12 #define GHCB_MSR_TERM_REASON_SET_MASK 0xf From patchwork Wed Mar 24 17:04:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408307 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8B7EEC433E5 for ; Wed, 24 Mar 2021 17:06:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 68CA8619B4 for ; Wed, 24 Mar 2021 17:06:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237213AbhCXRFt (ORCPT ); Wed, 24 Mar 2021 13:05:49 -0400 Received: from mail-dm6nam11on2061.outbound.protection.outlook.com ([40.107.223.61]:33120 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S236994AbhCXRFQ (ORCPT ); Wed, 24 Mar 2021 13:05:16 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IkF7WI7yuPd6YC5dkwvcziaEnnRlpbf5Or+wtjY3aPJ/hRUJk2Vn6ihufnrAUfkuPXM7EBOvVTP3m5SZO4YglzU/tHPxWojpd32dny8CuIHivOeLNwhsYbbLsaPl7OyCfZG+Uqc9eqXPReksUrBXWphysbP0wdDNIsAWCnEKguZcyDBKRS9lVa1IpPXDA1LjDJVzh/MaJIPr5ZlRxaih9JwhOVK1PLqTGO9iZTaTioaGbvC9c0ifSzoAEfmE9Qc264sfHRf4a7EcLP4MM1ar++xA6ZJ93AZmecYT1xlcMm8UNwSewI1Cdc/R/AFAEWAnKJse79T0OJEoZoS+8jM4Ag== 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-SenderADCheck; bh=hIECAcruEgVnm28ZA47pi51qvtvUNQB6wUDCQ0LIx6U=; b=oXNjH9j2jPCZDLRfeU6Gx/zs2MoCmxgYFsuBkVgV+buyZfP/r0pdJaI1nRmfJYmjwpvtdTQFnboNe1isHXfRf2wV6wee47rKp6//tuCeDPWRBNQJOauQ/B+D+HwXCWUu3/caOIb6bqbIJ+Z7QCvmMPWxi1K1BTDxo0ZWjqgmK3OTCv1dctEumKK38g1DiZ5vzlmzt3R+1o+jiieMjQReEk33YupTBSVf5Iqno6xvJ/+Zr9S3RUUQBVqh8IyR2Be9X9KTZVEJnUFQja0uEmBRHUz3L8hPc2tgV4otKJ4HfaNWrdcX4F9vEthYvWYg/FwA06iEYyEtjCOJsiVAyQUSaA== 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=hIECAcruEgVnm28ZA47pi51qvtvUNQB6wUDCQ0LIx6U=; b=DSU6d1JXVOydq9s+nGn78/sFt5h2fENvphwx748LkATHKjTfIEd+2RlDVRfXuRNPnXcVMnb9jglO8+I1ljbM4VhxKmY2iSAT5D6AhONXyqeF38xelbJgqEsdFhur1/o5dd5oAOhw27Paj7efLmbEXL/4uTvft2p7GwOMFBUdnt0= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4382.namprd12.prod.outlook.com (2603:10b6:806:9a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25; Wed, 24 Mar 2021 17:05:14 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:05:14 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson Subject: [RFC Part2 PATCH 28/30] KVM: SVM: add support to handle Page State Change VMGEXIT Date: Wed, 24 Mar 2021 12:04:34 -0500 Message-Id: <20210324170436.31843-29-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:05:13 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 3c15bc95-e9a0-414d-857a-08d8eee6fde6 X-MS-TrafficTypeDiagnostic: SA0PR12MB4382: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6108; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: XkFmFRR5W7Fla82uirixMCgEwcEb6eLzhS0L2TH5OZFssAVo8PYJ+Bg/V6y+rHg2r4rPk9w5FOGiKx2UAqBaB5wgDYxLx7tWu//yLAb6WQDKpDAZRdQcalcc4HBcQQgEjNTPAtbhJJSUnPOw5gCW8TAhAR5Sg+3aff+rKGgQe2cUPRTlbqTCohQv6M2PwcVfldYQIvlpHsBwFskFObnrRX82z+BIiFQnrbuq8qGlUMd+N3R+93eqgoRVGMmSURKmafdT6U74MZubKy5XvyfmwZsq2zPor4wQlG6IBaZ3CxojVltobwOwc86sdHb7PEz9YI1QTUiU6ZXlLA8ykDjyPRe/346jDUfEdzjLvGNimcw28LBImiWcNlyxz9udVxNPyXaBYlhTbnftsszBkgUR0e6PP7zy/4ri2BI98Rkzjwi9i8dAs2fL2bwCDnWgTgHntSOs+13l/xXS2YH76ldF1wflpFEQkVv5Hx6+296eh6XHuIcxDRBR6fuLrfUyvlp0a/hW7I6qATqeqaD1qW7KGUyMokbAqerYit1PE0CW7AytGa6qi2GBTmhNuULdsM8euSLP4tl1Tj2DujRZmdjFf5a3MpWKl+4w48pCUJ1pPWSU/FZMMARp9tvCqs6n0/q+zny3Jb7zpEVzN2IaxAZb1g== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(396003)(136003)(376002)(346002)(366004)(39860400002)(6666004)(44832011)(66946007)(1076003)(66556008)(66476007)(956004)(36756003)(478600001)(2616005)(83380400001)(8676002)(2906002)(8936002)(26005)(86362001)(5660300002)(52116002)(7696005)(38100700001)(4326008)(54906003)(7416002)(16526019)(6486002)(316002)(186003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: uSBdoKekaY6UBAXo6/7ILqfX3ajwc+LtLjpedJgYyclCuXZ873NmHYwhCpeeW40OQOXRngeaeCA+8JwwGdioyY/IW6qBStiBNmEDTRE/hYK6jvnjXMMCxdobiWwVkcyOuqw1Ve0iR7dKyLwQ9dbX++cTcmw5sAyBocUsCqwGU1Okg2C8gMRs8CYIVL6O5DfksUxVc9E+ZttuihoZqFTbfaTop7vFIndKyqfupGOKYngVuD50tOwgKzX/zNrUR5IzcosGeKiTRxMhEcSskoGWNklf3VoXsmoKwOieCgv8yNioe6at3X4mrcoNU9QACq7o+O+DH2TdJezOcMAEYwSY06xo8N0fKxjnkHdI1D9EgOAkTYaBqlNSEfYwCET6hqx4zq8QJMagtWCgUFY5zQxF3qfgrnFz8IUiel2DgTMaWuON/UYuG2fOPzstp/aKQfI9V8kP7WrRDwx/j6RoePXoNq4qQXmh4AvLGqLXeKnq6/fCHu7mjw6eIBZFOlIeqjTa2n5uA5OpK1G1aZUzdMiVWP1CA4DKwwMp30sTN0uwGT4I6ww/A3d5D3UbCkTZcEMwkr4bBTyODM7Olgw0OOFBeRBYL50VtmGXGmAWJ8V0evX5Am9xP6lY0MtqmKIM8S41rHymqG12npD+z36WSKR0t4KqHIA0CeDQlWOmkDhf4nJbgklJBMooobH0SvYFOknythPeYtaoRJBtPFsWr7KJuA+WQHMh4zo21IygRHKpoGLNe1AmcI9LhmjDB44v/qCcTkP5z7jqezckOuMBpZsMVeLvul55yenasdiYO+q/pvAw30SOHIQdY8mIxgCK2IJVH0mb9+Lq5sP88EmvEkoGPhbQwdpYIFtJJXyYF63W5Easns2/Vp8wNlusi94bUN8POK2lOF7yRgxHp2n+U5ZjWXxRwqcIyvyfbbSk8D6wM483dTYwKveJbFkWsxLCFr8li2PEFuV4JsZn3RYcuE0WHEbAdflpGFxxY4nUI5jcjy3ner+emLc05g5FLBsHgpiG3lFAQu2sIQhzf3Yh1KqT0oQrHcptQz9TL4D5gfHWFbUP5ETTeM2FnKACI5zLEowyKRLHpOq+CPwXK6aMuJv3ATHWdchaBrgliuGOsrq+Wqc7hMXpcRbIAND8cJDhhyIpM5v0B9FZF7SxcMSMECq4WuhFWBQjO2oVUL6lWLk0F23nE9zMpZwqlLs9bSs40zIpdUA50nmxzD/zPxssdSdYQRHI/1PDS7q2BKguVihhypTqn04lvPMZ4/wcLpl5uJuwH3EF80tA7HfiECXZQZ4WKUtzPkWWh/Xjd6QpkifFrcfOwGa5c/ztFHCD1ABWdkLu X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3c15bc95-e9a0-414d-857a-08d8eee6fde6 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:05:14.4006 (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: 7zZMTchPuhDdxy1zh6RwpzQM0mv3J1m7zfuo7GLqAzuN/tViCvHZd7L+PMXWZ0xcCzRxYNvqk4oLbLEN+GuqIQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4382 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org SEV-SNP VMs can ask the hypervisor to change the page state in the RMP table to be private or shared using the Page State Change NAE event as defined in the GHCB specification section 4.1.6. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: Vitaly Kuznetsov Cc: Wanpeng Li Cc: Jim Mattson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/kvm/svm/sev.c | 58 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 4 +++ 2 files changed, 62 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 8f046b45c424..35e7a7bbf878 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2141,6 +2141,7 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) case SVM_VMGEXIT_AP_HLT_LOOP: case SVM_VMGEXIT_AP_JUMP_TABLE: case SVM_VMGEXIT_UNSUPPORTED_EVENT: + case SVM_VMGEXIT_PAGE_STATE_CHANGE: break; default: goto vmgexit_err; @@ -2425,6 +2426,7 @@ static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, int op, gpa_t g case SNP_PAGE_STATE_PRIVATE: rc = snp_make_page_private(vcpu, gpa, pfn, level); break; + /* TODO: Add USMASH and PSMASH support */ default: rc = -EINVAL; break; @@ -2445,6 +2447,53 @@ static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, int op, gpa_t g return rc; } +static unsigned long snp_handle_page_state_change(struct vcpu_svm *svm, struct ghcb *ghcb) +{ + struct snp_page_state_entry *entry; + struct kvm_vcpu *vcpu = &svm->vcpu; + struct snp_page_state_change *info; + unsigned long rc; + int level, op; + gpa_t gpa; + + if (!sev_snp_guest(vcpu->kvm)) + return -ENXIO; + + if (!setup_vmgexit_scratch(svm, true, sizeof(ghcb->save.sw_scratch))) { + pr_err("vmgexit: scratch area is not setup.\n"); + return -EINVAL; + } + + info = (struct snp_page_state_change *)svm->ghcb_sa; + entry = &info->entry[info->header.cur_entry]; + + if ((info->header.cur_entry >= SNP_PAGE_STATE_CHANGE_MAX_ENTRY) || + (info->header.end_entry >= SNP_PAGE_STATE_CHANGE_MAX_ENTRY) || + (info->header.cur_entry > info->header.end_entry)) + return VMGEXIT_PAGE_STATE_INVALID_HEADER; + + while (info->header.cur_entry <= info->header.end_entry) { + entry = &info->entry[info->header.cur_entry]; + gpa = gfn_to_gpa(entry->gfn); + level = RMP_X86_PG_LEVEL(entry->pagesize); + op = entry->operation; + + if (!IS_ALIGNED(gpa, page_level_size(level))) { + rc = VMGEXIT_PAGE_STATE_INVALID_ENTRY; + goto out; + } + + rc = __snp_handle_page_state_change(vcpu, op, gpa, level); + if (rc) + goto out; + + info->header.cur_entry++; + } + +out: + return rc; +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -2667,6 +2716,15 @@ int sev_handle_vmgexit(struct vcpu_svm *svm) ret = 1; break; } + case SVM_VMGEXIT_PAGE_STATE_CHANGE: { + unsigned long rc; + + ret = 1; + + rc = snp_handle_page_state_change(svm, ghcb); + ghcb_set_sw_exit_info_2(ghcb, rc); + break; + } case SVM_VMGEXIT_UNSUPPORTED_EVENT: vcpu_unimpl(&svm->vcpu, "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n", diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 31bc9cc12c44..9fcfceb4d71e 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -600,6 +600,10 @@ void svm_vcpu_unblocking(struct kvm_vcpu *vcpu); #define GHCB_MSR_PAGE_STATE_CHANGE_RSVD_POS 12 #define GHCB_MSR_PAGE_STATE_CHANGE_RSVD_MASK 0xfffff +#define VMGEXIT_PAGE_STATE_INVALID_HEADER 0x100000001 +#define VMGEXIT_PAGE_STATE_INVALID_ENTRY 0x100000002 +#define VMGEXIT_PAGE_STATE_FIRMWARE_ERROR(x) ((x & 0xffffffff) | 0x200000000) + #define GHCB_MSR_TERM_REQ 0x100 #define GHCB_MSR_TERM_REASON_SET_POS 12 #define GHCB_MSR_TERM_REASON_SET_MASK 0xf From patchwork Wed Mar 24 17:04:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408953 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 48B7DC43618 for ; Wed, 24 Mar 2021 17:06:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0C7A961A19 for ; Wed, 24 Mar 2021 17:06:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237208AbhCXRFs (ORCPT ); Wed, 24 Mar 2021 13:05:48 -0400 Received: from mail-dm6nam11on2061.outbound.protection.outlook.com ([40.107.223.61]:33120 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237068AbhCXRFR (ORCPT ); Wed, 24 Mar 2021 13:05:17 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YpITB7/XgkT4CAoQKdTX+erCps0VhunE+vgipuTpDp23m3JRk53fAN8k1WpPzhRRtvKR3RLA+aN4VS+Lte/s/6gGpnwVUcLq/pIq+KGxpMzgPZWG9po0TuioKD7rHJWlYSpBHsl2AeO3bYgLU+H5lGpNe1TW/lJxtfiApPCCt8GnTrqkmnEAQrgxrwyY8wOFH9IT/b+COapbR92wM9/lqHAj2C1Ujaf4qkHpf6kSVFOYyGVheMSv/LpfS14vKXk26nl0tu7v3m8Bfi37Mrf8ZTM+pIXJwDWS2htmTF9uXoQ80UeHlcjV5t+g145MLX4vsSkjxoNYKXZpWQUXjJ0EXQ== 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-SenderADCheck; bh=U6Oqw9HC1lruIFm7FhqWvtud3iXExTAuuTQZCcScbxs=; b=eO1OSSHx3Qq6/XuZ1y4pB7oA5e/RIM1AZyjF+o3jJCMskC0RG3RFiQNQtiWeHyFrJskFToFgFIi/Pbgi9V+0ENAyy03NklE/KysFBNpArM5TN0M6U9qdeua0duSLLQL0FN7p9cv3ZWSfYjREnpwql2oOdXVymcrKZ7jvNp5Is5WxyAW5/KjVy1CSYQ35tlZGAgcXK/e3QwuhfOwmgzRMAedQ0xZQNqK6agTppK/6LGxKfgN0I+XI7lZ3uy7k2P8ZSgBeZhWumOZ+epRZCLPYptDTZHTgaJDBQVKtBxBisKzL3wzJSg/BcVeKSfGEgJhC499kq89Og8ivNdqebLUDWw== 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=U6Oqw9HC1lruIFm7FhqWvtud3iXExTAuuTQZCcScbxs=; b=4sX5dnM6spWD6zExsbJ6h7pYlgXakCLL2QMfyRc5qaF4JdJqazYmMqbfTkSKWIjM8AztfwY5iYo+SSPVAQ9QbmNOLD53xtHBGTWPnLdVmi5YRIYeColOQXbUlZ3qdPHC6F02xbRMvafdIDQgDEFa8OBiUZrXYQtq1BAgLhT/R3k= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4382.namprd12.prod.outlook.com (2603:10b6:806:9a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25; Wed, 24 Mar 2021 17:05:15 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:05:15 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson Subject: [RFC Part2 PATCH 29/30] KVM: X86: export the kvm_zap_gfn_range() for the SNP use Date: Wed, 24 Mar 2021 12:04:35 -0500 Message-Id: <20210324170436.31843-30-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:05:14 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 0651ad6b-d5c7-45c1-9aa9-08d8eee6fe68 X-MS-TrafficTypeDiagnostic: SA0PR12MB4382: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2399; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: NncvWd+d40Orx8eYWtneAdMp3yBxlSQG8OB3v/Dgava0CU4GU+pfd9hrnYO7JimHS0zMjHCQfqZVj2u9lTAoDIz/BKkhRFhSA1SbWN/wVBDKmbcxjQug/s2ZcWq1aBmAcysEu5dFOL5hFKIyHRJSJ5ZoTIJ99/50iHgImnUSh1AWBa8sHBPO3idCIlhqmM0pVwwUtoJZPF7VPZKGXIWulxkbVl2pIlwN4bk6QqPrF/G6lyK9yC2IxErN7UEcZRcNj0HgZTF2TWi+Jfzo2eHVOEBd2iGlKqBCi6jVp0yNzhjRn+C7Kr1o7GcObZJyMTsaeRjCq3TKSGg3gxEgRt0iWojU5kj9OAcO9WJvnSi2zHGYKekFDJf8iRxlLlztNaCaBWlIWhBOrQnusVnuGpLqUwYCRuj/FbFjKbKh/0k0V25YAM5O0S1tIKZW3IzK5lXYSLcmjCnmymogR88znOfgxGUVYlD0+IK5zVAN8Kqyk2bGcXYWrY0Y0MJKjoiiALhvIkpAuI9bPywE5wRz1bDBPlTVgzmzTJTYw9vcvEnpno9BUVCqhspuvpnTrZNTfQDjl63bgCaLirFP/VNel84Hosu3lcFnrbK5V+/jOcYF9qixXlT8dhIBFiJJdDVx94kSDnXoh91BM8FmnnBlHYLXVQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(396003)(136003)(376002)(346002)(366004)(39860400002)(6666004)(44832011)(66946007)(1076003)(66556008)(66476007)(956004)(36756003)(478600001)(2616005)(83380400001)(8676002)(2906002)(8936002)(26005)(86362001)(5660300002)(52116002)(7696005)(38100700001)(4326008)(54906003)(7416002)(16526019)(6486002)(316002)(186003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: os+Y8FLO3XADUNNqty1YBQ+GCeC7ZXU7r7jbUxFbkqWJtIRcJe/Qfevqk2hcypBuFwG7RATHHtuB2mLou0VviQVxz88mITYHQwRPr2YfjsF2owM3XB/vJhA5462jUH9xfB5QtxuUeD/ontZbwaaGTW5b55QB0tBXg6Y9yyk7mz+hjnRCM5c306QvHpH1IalRcTlnWAs8gozRsxqj85GIFfMNbnNoqiBIXanmE2LJu2A8yh/wLyM9oIkWBGRKHBdDPTfU0xTDwlDbqyeg80OXr0Gf4B7chaAcJ9/uJKEW9y+gIqL2uqakUzTOtcr8ZokshGyPRCt/RPMkiJH2KE0FJ6mptB9yIXtck2gaox237N1PbVlGvFIEr9YhGFLfrutEoDaMtXOR4XwIXf+M7ATnrKucqIPsK156khZeY0uI3p5dxUl+Wag4C3pp/fu2KOurJO9H//ZEVis79nAyrrMkNxbWhdo73RUd2k0L3iXDg0Pei5B3xkDMsPFpz0q7u89mB8gAjfx/wBUyRdDXqSr49Wn2w4WXmQdTwmNYpZaPTGhYg9BzyHqz/x+svEZjKuyZJDSNXvY2EZKUERy5zeoF5j5D6SKwNxLq4Wx5wBwH9GZQmYnKVGLrMxfteQg7N2jzUSo93aYxFK8nuWlQJfAB4mMBxlSDKQtjHk+F3FKm/jkVar8kNoxPWL6T4MOaWHTSXtVrVGnju04SeixCOvz7mybVb2wuL8pQcueI+6m44v5UfePEdbO0H+j6m2sMQiq6YHYlofvaDP4eFJx5Z9aJchjWq4nwapKwR+bSBMTCsJIPEvEH3ti4vzTEodDQZGrLQ8ywwG4w/tkHt36qZQS/XGLe0yfeTJn/s8Y3rmdEVMVP0g8wH5wL2orInokn9JujDG3ja/LmhnslqRZu/tz32I9fIzMRnMHZEuSjmmORsS4LK+eq0Mn4gas9uNtUIkK1fHlOvcYg65vOsaA1eynPT0KBD232NIo1cAWzbmOMt0Buka0tM5Q4szGUIqh9CxOaPzAxXi3gNrtPFSYB2gw+X/XtaZ78LWNuvk8WowrzXUdFelBS4LtIxZSD1KIbXcgQ5frQK2TNvOizXUJfUDG3vxJvy8Qh/4MAnb3bNyfaqfDTXvnqwV9XxDB+fcRyXCAi1vsWHqayGGSNDOHIatUH2pZUg9xEHwAEhgmwPiIvQkt0mZW416HQX+mk3F28ollyq6hNrpg9cNcSmhaCW025WfuLG9D1dU4UFHOEF5WCCJw5IbqMoH0OKI2Vpbkspcoy3+tM6DrvziRtOiSzZRdQbXKrzq3ogCAOeDDIZX7GN3kXwqmgDZ9VCeyB5z7+mGZi X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0651ad6b-d5c7-45c1-9aa9-08d8eee6fe68 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:05:15.2891 (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: Pkg7MU5mERkMI9RdzSBiUhrkQGdHtou8YHc65vKOkZBd0nBGx9n+O21iDmzEa/nuVFmQ9izJTXPdJ696yPgLOA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4382 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org While resolving the RMP page fault, we may run into cases where the page level between the RMP entry and TDP does not match and the 2M RMP entry must be split into 4K RMP entries. Or a 2M TDP page need to be broken into multiple of 4K pages. To keep the RMP and TDP page level in sync, we will zap the gfn range after splitting the pages in the RMP entry. The zap should force the TDP to gets rebuilt with the new page level. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: Vitaly Kuznetsov Cc: Wanpeng Li Cc: Jim Mattson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu.h | 2 -- arch/x86/kvm/mmu/mmu.c | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 074605408970..5ea584606885 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1397,6 +1397,8 @@ void kvm_mmu_zap_all(struct kvm *kvm); void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, u64 gen); unsigned long kvm_mmu_calculate_default_mmu_pages(struct kvm *kvm); void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned long kvm_nr_mmu_pages); +void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end); + int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3); bool pdptrs_changed(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index e7c4e55215bf..5f7ebe4afd63 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -223,8 +223,6 @@ static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, return -(u32)fault & errcode; } -void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end); - int kvm_arch_write_log_dirty(struct kvm_vcpu *vcpu); int kvm_mmu_post_init_vm(struct kvm *kvm); diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 147f22bda6e7..1e057e046ca4 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -5569,6 +5569,7 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end) spin_unlock(&kvm->mmu_lock); } +EXPORT_SYMBOL_GPL(kvm_zap_gfn_range); static bool slot_rmap_write_protect(struct kvm *kvm, struct kvm_rmap_head *rmap_head) From patchwork Wed Mar 24 17:04:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408951 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BEB7DC433DB for ; Wed, 24 Mar 2021 17:06:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9765361A2E for ; Wed, 24 Mar 2021 17:06:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237444AbhCXRGX (ORCPT ); Wed, 24 Mar 2021 13:06:23 -0400 Received: from mail-bn7nam10on2072.outbound.protection.outlook.com ([40.107.92.72]:51654 "EHLO NAM10-BN7-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237226AbhCXRFv (ORCPT ); Wed, 24 Mar 2021 13:05:51 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gNAW+QHBYkyRWXf4W6Vn00BdcBvsVxh23hERaYv30nLVWFh26uUQ9fGNShJ14W8V/Rb7l44/RipqFJTgnjz4qR6Md8zGcrhmjT4lYE7LC1/jP5+NqqN3wcEgedKtDPDtMtSo2ChYA+YXjGKuUbLSI+c9jjaht8+9kIK91TB8CNTA9J6Ll1m62LxT61ImFUFD9bzNxHYQvbNrwwyVI3dnlm75XJl6hAtQYsDh0EeSeQLjg0QPhIZumDC4M1a/kiYkw6Pq6+bEjAompoXLH9XA8BfAGODfIWqfCXPEK9MJZ2rnj2ScFvnA9cyCF/ix8NThcmHdkRcgT2C8Zfmd+vQQoQ== 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-SenderADCheck; bh=7x+N0yGmpF2otih8SQhYfsdXYY2z8SzCskGptcGBbOc=; b=Mt4057975D83dCIj3PCemUlz3i3N+Jk/CTG6ay2gFvCdJLbW9sZM1Y3FtVrgBYTZB4NN7sw9toKkaz4tTAGTtPKJCY80S/3XkUHDrfjLHqJEyuPlifryk9WUVxtc1DC0nBRgCmaNxrpCHTjPD/M7F0e9UEWPA9++tWqOvC7zQQjLhGiBkiCPPfrxWRFtL3ycmxJWtOWk1ZJlVT/D4orD/99ONMIEKf+zumfUrQMQwhGJWnOPo/aEPtx6HVWDUsApMSgHPYba5bG+orPZZC8nkaoKthgSqwBaZNOLvPZgnv6SmOSRnxHXhNNBFnG6zovL/ry5XW5FQUoyfpB0kdv64Q== 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=7x+N0yGmpF2otih8SQhYfsdXYY2z8SzCskGptcGBbOc=; b=ukxpg7sevLRGntBZ1t1CN8Ja7KwuyNeGMxQzvOZHCAuxJk2GmtJqnl3FkRVhW4q8YuYcJsB7NvXfQi3xYYiCSxN/1vTXKJ/P0yVMIVHtxK8+MiqpAuCehRcOtm4jpZCsGlv/9FqThbejV2jUzSbXfDOKFQGXT+Dmcw3PHQnsEDQ= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4413.namprd12.prod.outlook.com (2603:10b6:806:9e::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3955.23; Wed, 24 Mar 2021 17:05:46 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:05:46 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson Subject: [RFC Part2 PATCH 30/30] KVM: X86: Add support to handle the RMP nested page fault Date: Wed, 24 Mar 2021 12:04:36 -0500 Message-Id: <20210324170436.31843-31-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:05:15 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 89d29cb3-835b-413b-b818-08d8eee6feef X-MS-TrafficTypeDiagnostic: SA0PR12MB4413: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7691; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: j4V74uXh4qNO+O0s1Z/5E137EbgrBDE/epZLTLH8c1/r0Pu2ozmEaDLuUHq38MhJyFhTQmlnGFQ1qsdgnHjdMms/7CECJ3K3W9O9NNeaTRPX/+QJD9kWEYeuWRefn9f/xmW+4qozjSS3c43gmARUH3/enPfv1Z8MLctow0brT0VaFH+I17hbI0fmxBTPHE+j9gn+fU4hlFLHQnM5PS+vleyHmsguRl9wEldqJhBDRlZrotVA32hQC3tdKJPLzO3MjTom56bfvRsLgF2xbeEbutQURpieQh1daIFJ5sxNFa3tf7tb6LxU00Kl+Waid362fyDNQa4GQ2B/v7W5YxL8Min7ujMLTuzT/KW21qRuLUkXns1B215wU7uUiQvU3+8qIeVJw1ZqbJdrXnMymdh9jlUjlCxCv8wkRtiVASzhms/qxIGgIghHQcHLR+f2991+L8wHl7x/UsUWgzr+Rs197O3R/1zj02IWXooS0MWEbDoSQQR286xlm1T4Bfgc/S07CbKnDg0lGqCEADkQG8uFuv0Be3YzXsR9ukCmQHJ+KteqWLQH/QZ9XekKb9Pyu7y43xen6new2t/LECOLixKY991GALaURCF7f1iVAUypyOD00bm4wJubfaoBbzMEHS2fCrQm/8tecK2bh7jpy3Ujjw== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(396003)(346002)(366004)(136003)(39860400002)(376002)(2616005)(6486002)(83380400001)(956004)(478600001)(44832011)(86362001)(54906003)(7416002)(186003)(66556008)(8676002)(4326008)(7696005)(66476007)(2906002)(16526019)(36756003)(8936002)(5660300002)(66946007)(6666004)(26005)(38100700001)(1076003)(52116002)(316002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: tpEXq/f4RJha6AfyMTnzMvgQFnvRSRDsGbaYHy8LYjiy8jGG+GbVSlyhvYY9NW/+Hw35MtwMD9iAV2iQ1JEjlZ1c2+xm4wmemkOQF0hJV2x81B1bJn1PKII8yN0JpxZma2dcaFxI119/Vw4dUBofnr2BZjpFfnbrfN2y9yz8yCb44sQ0v3y/faPxVlakAnL64mrnrTI3zzL/1LZxSnsnlPWHwf36NgaQZFhlIGnlKejIV6iKJmb1DnDOsGzBd3yggOyFIW2e3YVNZgnEVEWr17hXIxIOrKSMoKYuhhCdY7K0rCdEjua6Xtkj2ZXKsn/3IWEeKSgjmAEJJCmuUxZ6YxmYW4U3BqY+U6m1ccZkORSnW52Enk0h09xvhmRMlZsJL7sM01SsgarvUhTtIvnd6Tn+jv2uqzoshZXBdKwU/63K+TEWjpVQAHwEp2wxdQXHTsKdH3jRoJNMXig3SX6PyOHotLonjH8J4/WFXrVVp3byL1u9chplKDv4csDt1TLjySRWxekMGA+rzEDOQimgGOBj64DhuX9HjUbh1JJ2CHNeIhwMnq7co9SvICvP9Ffu9gws31h7cWA6EVceiTVbHVrF0KywWUchuBz91MESZ3dy2dhkhBDGEV07QzFfTBqehWw4/s2/G2GrNnIiXFHyKZP8AwF6ENPPvfUXr4TZtuNl6VaxQWpi6M9PSnEVdVPnvSanljczRk5g1+Rkof9DI6roZr+pVQEfHe8yJaSbYzlqtysqBViARazWTJzEWeB0ge+qhNhSnm5FSdOdDxKlTT/2ozutlT5ho8ZtGD2GeNWoEB1TFktoMZOipgAGj3Otl7WwVTF90X7I64Hy3Ik9Hi51/GUDe+SmaNpKlm9YQ7m3CDXDq+M88x0D2mnqFufjhm5gViL6KB7Eps8c9TuQb3ii6Fbl03mGCkeu7QNnCfFRk5ZBB7W3AvB6ppS0NXmDYy+S2/+4ncuAYVSP143kv8mHJTQv6r+b8ftarM6wL6Kc5bbz77pgGIlB7jfY4A8HwPnJnoWZdq60k3Cordp+f0KU6IGxjwuZQsG8bL9R4c2muTd0y9mqxik9AmYSNVnFy3TdsOAnj6/uctewQYTlMfdFefaRCugkjjm9SKzHMdxNEw+BxojFp+ccAnX2zQodvuKkjEZsqypB8ftrjizZI2l87OxZ+5gSHHoLCtyVuzT32JLtZw+oDBSkuPWJnxXYBf5HYTKcm9/gVBUvAH0Dz2jYxukhoIM/ycVvpyv7eVOJsjSlod0+91j6AR29wdtKBvM/vBFKxfStkfE5UwKYi4BB8sF5KG0fkvLWuxaHlNsFbzAXdSNOB95hez7Dx+L/ X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 89d29cb3-835b-413b-b818-08d8eee6feef X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:05:16.4814 (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: g4Hny2g9wtLGSEOHlPJQGbVq7/sFK06rut4xj/gaJm7bUiCEkwhfphydeaghuSszTfMD2NP1IID+LmFXrVZ4WQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4413 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Follow the recommendation from APM2 section 15.36.10 and 15.36.11 to resolve the RMP violation encountered during the NPT table walk. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: Vitaly Kuznetsov Cc: Wanpeng Li Cc: Jim Mattson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 20 ++++++++++++ arch/x86/kvm/svm/sev.c | 57 +++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 2 ++ 5 files changed, 82 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 5ea584606885..79dec4f93808 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1311,6 +1311,8 @@ struct kvm_x86_ops { void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu, u8 vector); void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); int (*get_tdp_max_page_level)(struct kvm_vcpu *vcpu, gpa_t gpa, int max_level); + int (*handle_rmp_page_fault)(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t pfn, + int level, u64 error_code); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 1e057e046ca4..ec396169706f 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -5105,6 +5105,18 @@ int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva) } EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt); +static int handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) +{ + kvm_pfn_t pfn; + int level; + + if (unlikely(!kvm_mmu_get_tdp_walk(vcpu, gpa, &pfn, &level))) + return RET_PF_RETRY; + + kvm_x86_ops.handle_rmp_page_fault(vcpu, gpa, pfn, level, error_code); + return RET_PF_RETRY; +} + int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code, void *insn, int insn_len) { @@ -5121,6 +5133,14 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code, goto emulate; } + if (unlikely(error_code & PFERR_GUEST_RMP_MASK)) { + r = handle_rmp_page_fault(vcpu, cr2_or_gpa, error_code); + if (r == RET_PF_RETRY) + return 1; + else + return r; + } + if (r == RET_PF_INVALID) { r = kvm_mmu_do_page_fault(vcpu, cr2_or_gpa, lower_32_bits(error_code), false); diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 35e7a7bbf878..dbb4f15de9ba 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2924,3 +2924,60 @@ int sev_get_tdp_max_page_level(struct kvm_vcpu *vcpu, gpa_t gpa, int max_level) return min_t(uint32_t, level, max_level); } + +int snp_handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t pfn, + int level, u64 error_code) +{ + int rlevel, rc = 0; + rmpentry_t *e; + bool private; + gfn_t gfn; + + e = lookup_page_in_rmptable(pfn_to_page(pfn), &rlevel); + if (!e) + return 1; + + private = !!(error_code & PFERR_GUEST_ENC_MASK); + + /* + * See APM section 15.36.11 on how to handle the RMP fault for the large pages. + * + * npt rmp access action + * -------------------------------------------------- + * 4k 2M C=1 psmash + * x x C=1 if page is not private then add a new RMP entry + * x x C=0 if page is private then make it shared + * 2M 4k C=x zap + */ + if ((error_code & PFERR_GUEST_SIZEM_MASK) || + ((level == PG_LEVEL_4K) && (rlevel == PG_LEVEL_2M) && private)) { + rc = snp_rmptable_psmash(vcpu, pfn); + goto zap_gfn; + } + + /* + * If it's a private access, and the page is not assigned in the RMP table, create a + * new private RMP entry. + */ + if (!rmpentry_assigned(e) && private) { + rc = snp_make_page_private(vcpu, gpa, pfn, PG_LEVEL_4K); + goto zap_gfn; + } + + /* + * If it's a shared access, then make the page shared in the RMP table. + */ + if (rmpentry_assigned(e) && !private) + rc = snp_make_page_shared(vcpu, gpa, pfn, PG_LEVEL_4K); + +zap_gfn: + /* + * Now that we have updated the RMP pagesize, zap the existing rmaps for + * large entry ranges so that nested page table gets rebuilt with the updated RMP + * pagesize. + */ + gfn = gpa_to_gfn(gpa) & ~(KVM_PAGES_PER_HPAGE(PG_LEVEL_2M) - 1); + kvm_zap_gfn_range(vcpu->kvm, gfn, gfn + 512); + + return 0; +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 73259a3564eb..ab30c3e3956f 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4564,6 +4564,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .alloc_apic_backing_page = svm_alloc_apic_backing_page, .get_tdp_max_page_level = sev_get_tdp_max_page_level, + .handle_rmp_page_fault = snp_handle_rmp_page_fault, }; static struct kvm_x86_init_ops svm_init_ops __initdata = { diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 9fcfceb4d71e..eacae54de9b5 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -637,6 +637,8 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector); struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu); void sev_snp_init_vmcb(struct vcpu_svm *svm); int sev_get_tdp_max_page_level(struct kvm_vcpu *vcpu, gpa_t gpa, int max_level); +int snp_handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t pfn, + int level, u64 error_code); /* vmenter.S */