From patchwork Mon Jun 12 04:25:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275472 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A1226C7EE25 for ; Mon, 12 Jun 2023 04:35:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231653AbjFLEf2 (ORCPT ); Mon, 12 Jun 2023 00:35:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39052 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234633AbjFLEeo (ORCPT ); Mon, 12 Jun 2023 00:34:44 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2077.outbound.protection.outlook.com [40.107.93.77]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CFE8A213C; Sun, 11 Jun 2023 21:34:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Hn4q7mHD5BK0aeeOkBE8nYaypjBM5ndCIznV0RoHiQQycl+gJYsFhH+utIgi8HMLM8aOKu5FhIS6l+akwJZClL7bCKeb/WN8GwJzzkSlW1Tt4m89fiUZCZRHwT1VPzjf/uxw615CIWrA8aQAh7In5P8aicxJPzEISuW2U7lSrpipTQzUUfDwOiV8dmOpJEN32iM88b+vK1TjngH7C/YBWTf43XlxyoheY+LVc/n44G63eEHp3AWfv5Ka2LkVNAIY8bDi8/yBJQGjr904jETx/RSvR6bTjmePCjWIdSXtUDC6YegXqsqik4nXLC39q/DoSUTwN6JflrCIY196vyIR3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=H7xoPysbgoWBbQUpSlg9IvZXhRgyNb+X2UAvt5tpwug=; b=Qpg3o+o+tham3Y0PV6lXpigtQCRTiLczp7Le1wxEXfJvHwNMMVRb5GPMebBu4ziZOwG3vzAvgZ2eUW1TCsUMowxH9dnk3xk6Grg5F2Jno4Wfzm7ylMCAF+0HYMI/Mksrmu3rJh/mAEMjKtEdxDOhUwNhAZRjP2ntZPg4lGu0m2zcJEyIDibp3JurgjS3i6ZyqBHu3likKKyLwcSn+wYJml4ybwvc7hJ4PuyM9enn7PcjVZHwNQHc2mOwTPJMze/U20rQcbYv6+7YMGdHhDQsGSgoWF1zj6N8s9JZokJbEaN+l0UK7Z5nvZtWByewUIxxXvvvZVHPB6DerCguqP6CDg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=H7xoPysbgoWBbQUpSlg9IvZXhRgyNb+X2UAvt5tpwug=; b=AGlzkRAtOa6UVTIK60KK83iTDr6noPf6Cc7x0QPYtclEZy8ukP3z/3yDbv6p5bEWo17vdjWTVl8Tu35tJF1kV9yB0Hww0NfQaPDBUi3hk2Cx8imhQX61ZxGCaw/QjZxpsazDsHFnyQM7/y1odkstbq5oJqregniLkoZzNvdj1sA= Received: from DM6PR18CA0031.namprd18.prod.outlook.com (2603:10b6:5:15b::44) by CH0PR12MB5284.namprd12.prod.outlook.com (2603:10b6:610:d7::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.46; Mon, 12 Jun 2023 04:34:03 +0000 Received: from DM6NAM11FT094.eop-nam11.prod.protection.outlook.com (2603:10b6:5:15b:cafe::b7) by DM6PR18CA0031.outlook.office365.com (2603:10b6:5:15b::44) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:34:03 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT094.mail.protection.outlook.com (10.13.172.195) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:34:03 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:34:00 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 01/51] KVM: x86: Add gmem hook for initializing private memory Date: Sun, 11 Jun 2023 23:25:09 -0500 Message-ID: <20230612042559.375660-2-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT094:EE_|CH0PR12MB5284:EE_ X-MS-Office365-Filtering-Correlation-Id: c5ebba7e-e173-429e-4a71-08db6afe4006 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: CpsgVA9e1+4w7kD0JEz9Cl7ky/lBURcO1ZPvbF/ATtYS4+sY7MCZZ56Js2PbVIpzZQAmE2q3Yukxe6mO9sl8b3ifL32ZERyD8YSSeTCkw8M/uclClgFtN/g2j7ibIml6KXNG50VwgzAHKFY7rJrBUWrjIa+/uanHzJjLkSbUGa+5EU4sLWTRR1F+U5T/zhmdcyXlu9ahl/IMVf2pvYXtPX2BID6kDtVudRQ7LX+i0et0moDKAcdfucyGvgakaKX/UOFRFZJ3/7nRvcuJKaobgW3l0eI0S4cZk2+mx78u6DW8VvhmPP6f3QyD3vf0AcZ+Vs7UfFCmF30NK9CidxfUaBzGSvp1QJKkLjONPK2DKwqA4fsMJUwz73iwJE422v2M4at5DkI6bXWTwbCWO6AfA2y4K0FjkVeXrzaGh5NTZx+RxluMk73Ll89Lol1cLJK/tRcMc5ildl6tQnYExKsvXxwAEQ2OITDKIXqwQGMOYdoL8Mnhhn2jbkKW3yGyTV3mycpOHjsP01fHIE286xV4qkUIPE4wLnv0JpHvOs5FJis6MyP3S6HhYDFNdoSs/HzUBbowmRJ7X8Ebj4nfCl9V52WfSADAfJyEPvfJfDgp/6LCWJCfYpXpyKW+qO5Dv4LFh9IjeIwXMj4OHd0TArpUAz6StDEjujoyjO0OiGYmgcDJjC0Gg/mC8Yjg7QDBxC3dI5wX8Juw5IMwPsFI8pvrzBXMj4rzXJk9GBYf5puFpdLsXd3qxlrFmeSAPWKt0G0zbWu3f8tKLmVSPTqOY5jo6Q== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(346002)(396003)(376002)(136003)(39860400002)(451199021)(40470700004)(46966006)(36840700001)(40480700001)(36860700001)(426003)(336012)(47076005)(83380400001)(70206006)(6916009)(70586007)(1076003)(26005)(478600001)(2616005)(54906003)(16526019)(186003)(86362001)(44832011)(7406005)(7416002)(2906002)(8936002)(8676002)(5660300002)(40460700003)(316002)(36756003)(4326008)(82740400003)(81166007)(356005)(41300700001)(82310400005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:34:03.0511 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c5ebba7e-e173-429e-4a71-08db6afe4006 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT094.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5284 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org All gmem pages are expected to be 'private' as defined by a particular arch/platform. Platforms like SEV-SNP require additional operations to move these pages into a private state, so implement a hook that can be used to prepare this memory prior to mapping it into a guest. In the case of SEV-SNP, whether or not a 2MB page can be mapped via a 2MB mapping in the guest's nested page table depends on whether or not any subpages within the range have already been initialized as private in the RMP table, so this hook will also be used by the KVM MMU to clamp the maximum mapping size accordingly. Signed-off-by: Michael Roth --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 3 +++ arch/x86/kvm/mmu/mmu.c | 11 ++++++++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index 13bc212cd4bc..439ba4beb5af 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -133,6 +133,7 @@ KVM_X86_OP(msr_filter_changed) KVM_X86_OP(complete_emulated_msr) KVM_X86_OP(vcpu_deliver_sipi_vector) KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); +KVM_X86_OP_OPTIONAL_RET0(gmem_prepare) #undef KVM_X86_OP #undef KVM_X86_OP_OPTIONAL diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 8ae131dc645d..bd03b6cf40fb 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1732,6 +1732,9 @@ struct kvm_x86_ops { * Returns vCPU specific APICv inhibit reasons */ unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu); + + int (*gmem_prepare)(struct kvm *kvm, struct kvm_memory_slot *slot, + kvm_pfn_t pfn, gfn_t gfn, u8 *max_level); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index dc2b9a2f717c..c54672ad6cbc 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4341,6 +4341,7 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) { int order, r; + u8 max_level; if (!kvm_slot_can_be_private(fault->slot)) return kvm_do_memory_fault_exit(vcpu, fault); @@ -4349,7 +4350,15 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, if (r) return r; - fault->max_level = min(kvm_max_level_for_order(order), fault->max_level); + max_level = kvm_max_level_for_order(order); + r = static_call(kvm_x86_gmem_prepare)(vcpu->kvm, fault->slot, fault->pfn, + fault->gfn, &max_level); + if (r) { + kvm_release_pfn_clean(fault->pfn); + return r; + } + + fault->max_level = min(max_level, fault->max_level); fault->map_writable = !(fault->slot->flags & KVM_MEM_READONLY); return RET_PF_CONTINUE; } From patchwork Mon Jun 12 04:25:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275516 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2F142C7EE2E for ; Mon, 12 Jun 2023 04:41:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234954AbjFLEls (ORCPT ); Mon, 12 Jun 2023 00:41:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43240 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234986AbjFLEl0 (ORCPT ); Mon, 12 Jun 2023 00:41:26 -0400 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on20602.outbound.protection.outlook.com [IPv6:2a01:111:f400:7eab::602]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC9AF10A; Sun, 11 Jun 2023 21:40:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aLqXvpUSK+wHgVafC6iDd01N9ykBb+irdQY0N5faGQhqA3i2Js4B2bWqfYDRBNYFzywdWfRgoGCGPLxjsafajsvvvqaA4xdiPRSgEw0Wp5U02ME/kgVoD+YQw3BPDimqYpHghKuDostZdjZo4TqsZWB4SPLjjEh8rXnMe7o/ayNH9AiO1gj2C3NgYTH/tbWeN1baQ1OI796SXtpdLITzS5WMVzSKwlvnh6JcqNPbevSVwf3TR+2tOO5h0EIMuuj7dIOSTa34FYNKd411mPZqksNKaFgS6yYgL8rSIL64vrRhG70hdLBPpnDCHxT4PjT3vo6gpJCLcwvqlQXJ8eq7LA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=cJNMBNV5gRu6D6upRq1tHyg7V/pfpJqpC1rXwTN2h3M=; b=VbSriC1Hem0AQllHzl/tyFQZElaho6qIyYkM/nJRPL8EhB4tW/0o4Fe5PGmgVU/Y2uLp/MtzC4E3n+yDIzMP95uwPrnZlrrcC7peZktmIzl1XPunIYoJ3rlkqCZPgheE5qkD7zrFOE8HwT2V7Pt1KgdGfLwT8w/rop0qKqt61pMw4I+TxCMi71tEAm6pvWalUGeFkkKsWHkpgdL9//fkl7FB4bxp5cncG28zj5xyjb/6IqCKgMODcB8sWPQG3BtJHzc96gOBaLPF1V1JMzP5c+JpmHGtkT1rz3bt6R47wNje948Xs7Z3QpiBKnBiuLvgSVfLZ9rL7M9AtCPvGj+F9A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=cJNMBNV5gRu6D6upRq1tHyg7V/pfpJqpC1rXwTN2h3M=; b=wyqwRw3j1oIcohVGFf0reuiQzOaTg9uw6OJpTY0eRrGlLyRa/MG/Y1acjkp0kZAsy7m2qkAUB8eUUbMqf0rDavHW+ohmRFGSxGKhl6HuAg9t2jKGCH+6tK4ae4DVfln5yPYLnPPYvXZYA1xHPknBseb7U9DM+kFXDBunDX1F9Rc= Received: from MW4PR03CA0092.namprd03.prod.outlook.com (2603:10b6:303:b7::7) by SA3PR12MB9228.namprd12.prod.outlook.com (2603:10b6:806:39c::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.41; Mon, 12 Jun 2023 04:40:26 +0000 Received: from CO1PEPF000042AD.namprd03.prod.outlook.com (2603:10b6:303:b7:cafe::40) by MW4PR03CA0092.outlook.office365.com (2603:10b6:303:b7::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:40:26 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1PEPF000042AD.mail.protection.outlook.com (10.167.243.42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:40:26 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:40:25 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 02/51] KVM: x86: Add gmem hook for invalidating private memory Date: Sun, 11 Jun 2023 23:25:10 -0500 Message-ID: <20230612042559.375660-3-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000042AD:EE_|SA3PR12MB9228:EE_ X-MS-Office365-Filtering-Correlation-Id: 88d0529d-0589-404a-394b-08db6aff247f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: INa9uhXEUXDUpe7tt2xwI8GF5TLwtw9xLCawQrR/qMMiUwmpoDn90FC775HebqMSW6yPN5nu+wWsCTY/czUsidaJr0/np8VVWk3+0p1Efoc49G5tR9UE3SZdycz6ky0p8u+NiQvzEs4M0pktIZ/Kx67Abrk+R46T4LK+tC1UkfjCoYAOQ8IFKhPnZTg+ngPL8gKOvENrHdrWa2MTk8ojHlqxCbAN+/ZSbATg/QjxBGVkvVrz6tT4Ch+vmSJhGZ/G32safyAtR3AEu/pqgcCZq7GLxzK9QlcWKNk/1aBwoblvM6WQUe9VqjGcM570FGlGQj0QVBDSsm8myuAO0e/sCoe8xlswQ2DY39YcKGuhDgQ+w68lPgc0PBB4PXwEtTI5UzxdwYJHtDOWYqhfJA+zMTISf6mih1hph8ugbjv1Ik6EsttsuLGL7DNlYTe96/SM1d6iORoXo8K3waXuu08enBwbjt5OPoM8m18ynetxO/OhMRFKgNb163HJRH56/T/fDsuAs0UKM8ZbKe+lnRlnN3U4K0i7WpkjCLFE2rZps6FcoUEWXcp8r/PzN0vKU6GDKBWgQf3wsquWje6R7S+ztaguvoPv0hH8SZaIVtRc6bTKk1PmDewIZNtIxaSO/iyLQJONpOdFc2mtsVHHNxTecary+rCfpZbID1doaiYul0G0PMGjyvmlnPSOQDcv74QwV0ThT7zHqU64C9HlINW8kMSO1MTcXhihoexDsKcqSgsYDyRYM/M+ou1H/9mT1xmQJH6UAUjgmBtIQvAENOJc3A== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(136003)(376002)(396003)(346002)(451199021)(46966006)(36840700001)(40470700004)(82310400005)(36860700001)(40480700001)(47076005)(83380400001)(426003)(336012)(356005)(81166007)(4326008)(82740400003)(36756003)(41300700001)(316002)(6666004)(1076003)(26005)(6916009)(70206006)(70586007)(2906002)(86362001)(44832011)(7406005)(7416002)(5660300002)(40460700003)(8936002)(8676002)(2616005)(54906003)(478600001)(16526019)(186003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:40:26.3016 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 88d0529d-0589-404a-394b-08db6aff247f X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1PEPF000042AD.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA3PR12MB9228 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org TODO: add a CONFIG option that can be to completely skip arch invalidation loop and avoid __weak references for arch/platforms that don't need an additional invalidation hook. In some cases, like with SEV-SNP, guest memory needs to be updated in a platform-specific manner before it can be safely freed back to the host. Add hooks to wire up handling of this sort when freeing memory in response to FALLOC_FL_PUNCH_HOLE operations. Also issue invalidations of all allocated pages when releasing the gmem file so that the pages are not left in an unusable state when they get freed back to the host. Signed-off-by: Michael Roth --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 6 ++++ include/linux/kvm_host.h | 3 ++ virt/kvm/guest_mem.c | 48 ++++++++++++++++++++++++++++-- 5 files changed, 57 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index 439ba4beb5af..48f043de2ec0 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -134,6 +134,7 @@ KVM_X86_OP(complete_emulated_msr) KVM_X86_OP(vcpu_deliver_sipi_vector) KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL_RET0(gmem_prepare) +KVM_X86_OP_OPTIONAL(gmem_invalidate) #undef KVM_X86_OP #undef KVM_X86_OP_OPTIONAL diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index bd03b6cf40fb..b3bd24f2a390 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1735,6 +1735,7 @@ struct kvm_x86_ops { int (*gmem_prepare)(struct kvm *kvm, struct kvm_memory_slot *slot, kvm_pfn_t pfn, gfn_t gfn, u8 *max_level); + void (*gmem_invalidate)(struct kvm *kvm, kvm_pfn_t start, kvm_pfn_t end); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index c9e1c9369be2..10d76afa23d9 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13252,6 +13252,12 @@ bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) } EXPORT_SYMBOL_GPL(kvm_arch_no_poll); +#ifdef CONFIG_KVM_PRIVATE_MEM +void kvm_arch_gmem_invalidate(struct kvm *kvm, kvm_pfn_t start, kvm_pfn_t end) +{ + static_call_cond(kvm_x86_gmem_invalidate)(kvm, start, end); +} +#endif int kvm_spec_ctrl_test_value(u64 value) { diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 1a47cedae8a1..7de06add2235 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2343,6 +2343,7 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) #ifdef CONFIG_KVM_PRIVATE_MEM int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, kvm_pfn_t *pfn, int *order); +void kvm_arch_gmem_invalidate(struct kvm *kvm, kvm_pfn_t start, kvm_pfn_t end); #else static inline int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, @@ -2351,6 +2352,8 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm, KVM_BUG_ON(1, kvm); return -EIO; } + +void kvm_arch_gmem_invalidate(struct kvm *kvm, kvm_pfn_t start, kvm_pfn_t end) { } #endif /* CONFIG_KVM_PRIVATE_MEM */ #endif diff --git a/virt/kvm/guest_mem.c b/virt/kvm/guest_mem.c index cdf2d84683c8..a7e926af4255 100644 --- a/virt/kvm/guest_mem.c +++ b/virt/kvm/guest_mem.c @@ -140,16 +140,58 @@ static void kvm_gmem_invalidate_end(struct kvm *kvm, struct kvm_gmem *gmem, KVM_MMU_UNLOCK(kvm); } +void __weak kvm_arch_gmem_invalidate(struct kvm *kvm, kvm_pfn_t start, kvm_pfn_t end) +{ +} + +/* Handle arch-specific hooks needed before releasing guarded pages. */ +static void kvm_gmem_issue_arch_invalidate(struct kvm *kvm, struct file *file, + pgoff_t start, pgoff_t end) +{ + pgoff_t file_end = i_size_read(file_inode(file)) >> PAGE_SHIFT; + pgoff_t index = start; + + end = min(end, file_end); + + while (index < end) { + struct folio *folio; + unsigned int order; + struct page *page; + kvm_pfn_t pfn; + + folio = __filemap_get_folio(file->f_mapping, index, + FGP_LOCK, 0); + if (!folio) { + index++; + continue; + } + + page = folio_file_page(folio, index); + pfn = page_to_pfn(page); + order = folio_order(folio); + + kvm_arch_gmem_invalidate(kvm, pfn, pfn + min((1ul << order), end - index)); + + index = folio_next_index(folio); + folio_unlock(folio); + folio_put(folio); + + cond_resched(); + } +} + static long kvm_gmem_punch_hole(struct file *file, loff_t offset, loff_t len) { struct kvm_gmem *gmem = file->private_data; - pgoff_t start = offset >> PAGE_SHIFT; - pgoff_t end = (offset + len) >> PAGE_SHIFT; struct kvm *kvm = gmem->kvm; + pgoff_t start, end; if (!PAGE_ALIGNED(offset) || !PAGE_ALIGNED(len)) return 0; + start = offset >> PAGE_SHIFT; + end = (offset + len) >> PAGE_SHIFT; + /* * Bindings must stable across invalidation to ensure the start+end * are balanced. @@ -158,6 +200,7 @@ static long kvm_gmem_punch_hole(struct file *file, loff_t offset, loff_t len) kvm_gmem_invalidate_begin(kvm, gmem, start, end); + kvm_gmem_issue_arch_invalidate(kvm, file, start, end); truncate_inode_pages_range(file->f_mapping, offset, offset + len - 1); kvm_gmem_invalidate_end(kvm, gmem, start, end); @@ -264,6 +307,7 @@ static int kvm_gmem_release(struct inode *inode, struct file *file) * pointed at this file. */ kvm_gmem_invalidate_begin(kvm, gmem, 0, -1ul); + kvm_gmem_issue_arch_invalidate(gmem->kvm, file, 0, -1ul); truncate_inode_pages_final(file->f_mapping); kvm_gmem_invalidate_end(kvm, gmem, 0, -1ul); From patchwork Mon Jun 12 04:25:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275549 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D98B1C7EE25 for ; Mon, 12 Jun 2023 04:51:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234953AbjFLEvb (ORCPT ); Mon, 12 Jun 2023 00:51:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235296AbjFLEvI (ORCPT ); Mon, 12 Jun 2023 00:51:08 -0400 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2071.outbound.protection.outlook.com [40.107.102.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5FC5C1710; Sun, 11 Jun 2023 21:50:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Hr9K7FC9j34n1LlFCJy9piR1fC7/nj9koxs6u3uWx9oKk4+w+U3uBCiClu1tSv3/f8mv1hDyDGgkwlozBKTjkP/kgTdqkU1IYn+FlBrBzhPD4yShw6aumeabJ3qtCEB13n1IqUizVqC1zeQJD3FcjbwFRzgj5kdPpPPBfbCZiAURce8AoNJX0GdPp0d0pfu7GdpJ6uvwWNnqKoxw4iU/hVQ8jIO4hMYDP36o8DydfV80kbAogiznQHxHChRDlnCiTkGM0/wpcMWIZZdKC+kFjCjdcjOCg9Q8PUP/o/hwDtWGNhskmYieTAe7mr0BWN/1eRyMbHAqOEKN/quVCcC+nA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=s218HDiFv2eZBxy4ZcFM21BtWf4J8w71c4nYAY2Bnnc=; b=HfFEeFtEQ7p3hTNnj1KWhxvfNIzmoqIxB0egB/sUd5xtuHuCQzmb0NL/RGr7liBuSxUP2ipSzLFuWMWINNYSU9SDlNHvKLIBppBzu9CXDh2cZfAn/4wsCMSbddkf/EKZKGskMg4U4z3ADt/FsHVAZwoXjaktcYL7TAJjTTic2gHxFOLWIrMYQEL0T9Q7Jp5jyIrnYo0x44F9j5cBtRy8ZXd9ZjRCcCJGOyLLW4ASCkIddtF4uNI2/vge9kz4zoiYkVOwuiwAZ4zvxjw5r2qRASaUp+GW6lZyruQK8Y8TU5buctMJ7y1oQvlx5URDXti+fz0hJW8TfBGeAjYRGB7XYQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=s218HDiFv2eZBxy4ZcFM21BtWf4J8w71c4nYAY2Bnnc=; b=UUfF3W1yo8GdhhtHwYnzq4wvAkKs6xROhS4NVgnunYA5lqMw34ZLZeZkIl8cajtZzRhGa6aBLUYTGCdqhqISsv3NdqAhmGkzW4g/Ux5G2osXh1sZ0C+zv/S1WhXEQAImVAZCrUnN0Onz0q8oAIZyiuwyZBQLLaZY3XNqWoOBqpA= Received: from SA1PR05CA0002.namprd05.prod.outlook.com (2603:10b6:806:2d2::8) by DM4PR12MB8475.namprd12.prod.outlook.com (2603:10b6:8:190::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.44; Mon, 12 Jun 2023 04:50:49 +0000 Received: from SA2PEPF00001504.namprd04.prod.outlook.com (2603:10b6:806:2d2:cafe::86) by SA1PR05CA0002.outlook.office365.com (2603:10b6:806:2d2::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:50:48 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by SA2PEPF00001504.mail.protection.outlook.com (10.167.242.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:50:48 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:48:05 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 03/51] KVM: x86: Use full 64-bit error code for kvm_mmu_do_page_fault Date: Sun, 11 Jun 2023 23:25:11 -0500 Message-ID: <20230612042559.375660-4-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SA2PEPF00001504:EE_|DM4PR12MB8475:EE_ X-MS-Office365-Filtering-Correlation-Id: 66e8f260-5b71-4611-a0c9-08db6b009782 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: aDTmU/JF0XyDbGUVKUj65tBEtjtLuT5BwMkK4wsD/2Ki/WhY9VFnF6Rq49Y75ezZ7cTxNJ+WZf7sVCbJXTP0HvFMwXNNoTRn1R6m2DX1ig5NleoiLUUBzYLNwkhlqnTcQN2eLxC3FIdE8jCf6MA2X5+W9fBtzcFY5k43YvJpIXuyP1pXsr9VqZIfex4qbTED18c8HVoAIobgk0PYNqq4zKxIF+7cKR/Way2gOI1KOCcXIhbEHXOjv/y5tLVxRa3fbUOTp26MoooeFiv7kChTmEB+PQE1fPU0wXFxHs6vdThFVM3In3HrCpdxfa2qrPL2/JCf6PKXMKmJAiUEAR+jbXUvl+qAVQILrlEYj35BwOme/tCm9QLUVVmPfdPyCtPeEIxNn+V21acxnF1uBg4SNW8LRk4IOiY98tl/GIPTDV4e3UcAXOq9MEMt8JmjccGfjRzhQX755kZMazURioKJkTkaYJvo9iTER/p2irw6pMnoYXDwS4MKaACC//GE1/nWVVu8CiN1aeX5KqTjN6ETW3GcLk54PMT+JhACavujG4KJ3SinBUmZw34DXS9qtzRZX/4cib8DsIxRdG8cxaaw3ow5vEO985Lx7U44XFHNgGjFhca7M1msx2dOgIe/TcRP8oAELulRsjAJg8Wh78+5+uJoPvdBLm/SerfqU0oZiAFLNjVY74lPnK8ZxHnAFemamNFMC2syWhpG6UKgtBWVDHuAApurLbcwtXXzF7QUwS3tocuMXsNxFDeEtCRD4wrv/HAOPYFV8vaxHcYKih5sCA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(376002)(396003)(346002)(136003)(451199021)(36840700001)(46966006)(40470700004)(426003)(336012)(2616005)(47076005)(83380400001)(36860700001)(356005)(82740400003)(40480700001)(36756003)(86362001)(82310400005)(81166007)(40460700003)(478600001)(54906003)(44832011)(6666004)(8936002)(8676002)(2906002)(5660300002)(7416002)(7406005)(70206006)(70586007)(4326008)(6916009)(41300700001)(316002)(186003)(16526019)(1076003)(26005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:50:48.8524 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 66e8f260-5b71-4611-a0c9-08db6b009782 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SA2PEPF00001504.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB8475 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The upper bits will be needed in some cases to distinguish between nested page faults for private/shared pages, so pass along the full 64-bit value. Signed-off-by: Michael Roth Signed-off-by: Isaku Yamahata --- arch/x86/kvm/mmu/mmu.c | 3 +-- arch/x86/kvm/mmu/mmu_internal.h | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index c54672ad6cbc..0d3983b9aa7e 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -5829,8 +5829,7 @@ int noinline kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 err } if (r == RET_PF_INVALID) { - r = kvm_mmu_do_page_fault(vcpu, cr2_or_gpa, - lower_32_bits(error_code), false, + r = kvm_mmu_do_page_fault(vcpu, cr2_or_gpa, error_code, false, &emulation_type); if (KVM_BUG_ON(r == RET_PF_INVALID, vcpu->kvm)) return -EIO; diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h index f1786698ae00..780b91e1da9f 100644 --- a/arch/x86/kvm/mmu/mmu_internal.h +++ b/arch/x86/kvm/mmu/mmu_internal.h @@ -283,11 +283,11 @@ enum { }; static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, - u32 err, bool prefetch, int *emulation_type) + u64 err, bool prefetch, int *emulation_type) { struct kvm_page_fault fault = { .addr = cr2_or_gpa, - .error_code = err, + .error_code = lower_32_bits(err), .exec = err & PFERR_FETCH_MASK, .write = err & PFERR_WRITE_MASK, .present = err & PFERR_PRESENT_MASK, From patchwork Mon Jun 12 04:25:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275578 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 718DAC7EE2E for ; Mon, 12 Jun 2023 04:58:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235484AbjFLE6m (ORCPT ); Mon, 12 Jun 2023 00:58:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235500AbjFLE6J (ORCPT ); Mon, 12 Jun 2023 00:58:09 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2062b.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e88::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2CEB02722; Sun, 11 Jun 2023 21:57:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dSMM1sXVfktDwQu5Bw0PhsI5OLEzWTkpBZHZ364Ea5i4m7nou3FRlLghQ+gYmxAz/yXxAkVKqqCGW4Ka6ddFdrOYCWBRcGPWogi5NJ+KG5VKsggdPJ/2mDk+aw/qH9O0U1nkzbpKepOWVv9EaJLpHDxMIVH558oWGeEq7bYzyhvM76/hzkCSLARDorN0ifg1bJwKVPvx3bGz54GDHTRhDHN6+8BIH5ytetXG+5lAHMlZ5jnSd95DUXMyyeYrqtJKvjAWKnzZ1ab/cBCcAfWP/rURyWMFMtcWCWtuIfTeVDAdYVPxv9RPaKj9qgh7ObNRN5wRF/48H+Af83Nvh9TGgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=s6CZtGI3H4GAYAVMJzlyGlbmtiu/olxIiSDrYkbBXYE=; b=TM1tUeqOjKRmDzl8XGPwzGl6ckRUP0QezfAjHkdwfyUteeDXfb6AaFIMIipt5lXoF2ZYt/5PeTArKA8/6XZoIfIz7f9xDM1iKB7N/hF52ZF1YB7zAmUpMMXWiqijPyEnnJdLvjVfDlrfNWcmMxHMqKiW19YFvKDfXkc24+lJ3tzajS+TnX311mvyd1h1a/Kw14MqFJMfeKKqFsnLxi36UJfN5QNgN35QGGiSSltymChqqw++Div4YG6KXwydTLIL2001BHWmFT1g6bNe7rHGvI8ftxsnRv9Tyh0yzsY6u1WYgHpzY1yaAQukU6mrmRkixyMbdgVOSxPkVix6QFGLrg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=s6CZtGI3H4GAYAVMJzlyGlbmtiu/olxIiSDrYkbBXYE=; b=xMJCEKRtjEGKkQpanHWDrK8bUbDFvm0/4lpDnaYYJ6CA68hdqoVt7tVNkyfqsg6R3N5pYT9IJUOsPLcNnaL7JwuCptNOaUR5miS70VUl0RrdlKFRW01+hmINDK3UvUvEFogzvfuA+kT1eoPY2MxUAibf244aQKNzjBf1HBgFdJU= Received: from DS7PR06CA0022.namprd06.prod.outlook.com (2603:10b6:8:2a::10) by CY8PR12MB7562.namprd12.prod.outlook.com (2603:10b6:930:95::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.44; Mon, 12 Jun 2023 04:57:08 +0000 Received: from CY4PEPF0000E9DA.namprd05.prod.outlook.com (2603:10b6:8:2a:cafe::c2) by DS7PR06CA0022.outlook.office365.com (2603:10b6:8:2a::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:57:08 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9DA.mail.protection.outlook.com (10.167.241.79) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:57:07 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:56:46 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 04/51] KVM: x86: Determine shared/private faults using a configurable mask Date: Sun, 11 Jun 2023 23:25:12 -0500 Message-ID: <20230612042559.375660-5-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9DA:EE_|CY8PR12MB7562:EE_ X-MS-Office365-Filtering-Correlation-Id: dd3dd272-df8a-4926-2570-08db6b017979 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: OlfPSbJS/xpr/4TLPGEjKnOFjpnp/Juw8RbQsmQ1EqJnBCcdOz+FJzdlYDRAtml3CtOffVki5/hyyKRIrJIcRLOLIeQySe7h0Q2YeAL9TmWLc0RsRfwULdmSYSzshhKNQ9i4EIeZ+GeF/8u1m0j7ywv300fkBRkmjJULSGemQ8MXXKt0742UKsWgudd6Lxr++bEkCuBVNjpuBGHTiqcD6ai9/8gLm1sE+UkZpWbnvA2qGye0eEcBs2ZdbB2DP5i96d6AEjUrTH5zrIsTLodVUXCW8mWoWFzZzX0EQUDj7FCMyz7W1DjlKGwdpWFQ0krPtTLx7gUzFpmKG2+68z1JcKLR/NGrVG49O4KN/Ys1sd9AqdvdsSQ2AU+RVbRSVxfcbE8qB2mrMNWBXURBr1nK37ffQuG9E+XcHeW2EyyfQA3swhEGF4hg4SduFfRoPHremf9Bg+TrfslDXVz75mBqhU+Gamk74MjDSPPGpHW/GkhEmMzhD17wtDy3ORSOstDWpAi2gpo41JkSdXqpJdY02yttmDmsf1mSgXrnv3vLmlk2VMwZTWryJMuyE7hWtE3tLDJiCzq2rejGlGnJgpNIFoLX3RRYUogej7Gjb8i14WUQYnqL4HvmR7SeKSzNaAmRAseAR4mbYpJkemedGO0U/vo0H3x7mUDp8dBUaVC3sqZC60lYz9plI/3cDORElA6DRUWtQ5hUhvOAGXypurmB2HVCr8rQURnMo4AzhsM1cK02xXe9vuNUezt8AxX+jzyXpSpSOxeD3vocXDAc/C5Svg== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(376002)(346002)(136003)(396003)(451199021)(36840700001)(40470700004)(46966006)(54906003)(5660300002)(6916009)(4326008)(7406005)(7416002)(8936002)(8676002)(44832011)(41300700001)(316002)(186003)(16526019)(2906002)(478600001)(70206006)(70586007)(6666004)(40460700003)(1076003)(356005)(82740400003)(81166007)(40480700001)(26005)(83380400001)(426003)(336012)(47076005)(36860700001)(36756003)(86362001)(82310400005)(2616005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:57:07.8929 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: dd3dd272-df8a-4926-2570-08db6b017979 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9DA.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7562 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org This will be used to determine whether or not an #NPF should be serviced using a normal page vs. a guarded/gmem one. Signed-off-by: Michael Roth --- arch/x86/include/asm/kvm_host.h | 7 +++++++ arch/x86/kvm/mmu/mmu_internal.h | 35 ++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index b3bd24f2a390..c26f76641121 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1445,6 +1445,13 @@ struct kvm_arch { */ #define SPLIT_DESC_CACHE_MIN_NR_OBJECTS (SPTE_ENT_PER_PAGE + 1) struct kvm_mmu_memory_cache split_desc_cache; + + /* + * When set, used to determine whether a fault should be treated as + * private in the context of protected VMs which use a separate gmem + * pool to back private guest pages. + */ + u64 mmu_private_fault_mask; }; struct kvm_vm_stat { diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h index 780b91e1da9f..9b9e75aa43f4 100644 --- a/arch/x86/kvm/mmu/mmu_internal.h +++ b/arch/x86/kvm/mmu/mmu_internal.h @@ -252,6 +252,39 @@ struct kvm_page_fault { int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault); +static bool kvm_mmu_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 err) +{ + struct kvm_memory_slot *slot; + bool private_fault = false; + gfn_t gfn = gpa_to_gfn(gpa); + + slot = gfn_to_memslot(kvm, gfn); + if (!slot) { + pr_debug("%s: no slot, GFN: 0x%llx\n", __func__, gfn); + goto out; + } + + if (!kvm_slot_can_be_private(slot)) { + pr_debug("%s: slot is not private, GFN: 0x%llx\n", __func__, gfn); + goto out; + } + + if (kvm->arch.mmu_private_fault_mask) { + private_fault = !!(err & kvm->arch.mmu_private_fault_mask); + goto out; + } + + /* + * Handling below is for UPM self-tests and guests that treat userspace + * as the authority on whether a fault should be private or not. + */ + private_fault = kvm_mem_is_private(kvm, gpa >> PAGE_SHIFT); + +out: + pr_debug("%s: GFN: 0x%llx, private: %d\n", __func__, gfn, private_fault); + return private_fault; +} + /* * Return values of handle_mmio_page_fault(), mmu.page_fault(), fast_page_fault(), * and of course kvm_mmu_do_page_fault(). @@ -301,7 +334,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .max_level = KVM_MAX_HUGEPAGE_LEVEL, .req_level = PG_LEVEL_4K, .goal_level = PG_LEVEL_4K, - .is_private = kvm_mem_is_private(vcpu->kvm, cr2_or_gpa >> PAGE_SHIFT), + .is_private = kvm_mmu_fault_is_private(vcpu->kvm, cr2_or_gpa, err), }; int r; From patchwork Mon Jun 12 04:25:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275580 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4694BC7EE2E for ; Mon, 12 Jun 2023 04:59:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235781AbjFLE71 (ORCPT ); Mon, 12 Jun 2023 00:59:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54070 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235780AbjFLE6i (ORCPT ); Mon, 12 Jun 2023 00:58:38 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2064.outbound.protection.outlook.com [40.107.94.64]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5402510E7; Sun, 11 Jun 2023 21:58:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gNVEd/9vqDK/M27AU3zQDh98UQpgronGZOS0Du7jbejSwaI4pzJduissWfAD4YGXjgVvYfdM0NyTpouiqy5cC0UFyNBnAXARMLdi+mWkEZ2UEBchVr60dILky9KlVHAYVONafBAq2ILD/NU9vs00+6MiDdFsCWNCtitEglIk+lY6SH+dtxNmiLyzKSMf5z9DZAMqtLf7GijeJAyELkbkA7BpiAk/DHEG3EuvvlpB5X1lHLcae4u+1BDnCTJlUqCzw/OZnNTDqcftglKxVcaQ3VAoL6FWd3sF5SaEyvepYejAz8a6yMJwN63xedFVrlsuzSaUha9Km4dlhdLYv/yhbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=CeyhY905hNs52Bqq6Oa0uJDMuGrsDGEdsZEoDZbc4iM=; b=kKAOtqn8UgOEQAldoVX34sEwYtHW/PzvNGtPVbQn7KqIZdb4T13xWgBrr496MVlwzcmoCrZE1zp1M/BCpSIus2R2klaOnAcQUiOli77Ek3sFoEtQG7oudiHSmGB3k8DZOfN/L8suC4AHHXIkm6zlCqE+Q6FojjCVfRTvBV3FLcRbFinH1NLiJtVwDcwqFHC4rLKknQEi7RhXpxyY9EyVuFOLJNP+/4ll+DQ+t+9kBlUzkZ8WqcCsaGmmzkhJFmyvKwPvCP1aY2nbTCx5glDAp/caSwpddOyXM0BinHQSQzrm7hdSTP/kjkJcrOek2MdaRPoLh6G5vJ0AYK2hojqD+g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=CeyhY905hNs52Bqq6Oa0uJDMuGrsDGEdsZEoDZbc4iM=; b=XWPixkjoXVP3U/qxiMxybV5lrJe6T4vufv2cusmsG8hmxvX2ZwDszUgxp9kJdI5I/OLVW84UdiGDWei+LtAV8xk3Nsd2m/NZyN5i3NEcOEHsLLwMunpJYblqDIyEQKQlTFaIWryUURlTNFxQsUXaNxtmbYUe0x9V/m9Aak0jnHo= Received: from CY5PR04CA0015.namprd04.prod.outlook.com (2603:10b6:930:1e::9) by CY8PR12MB7099.namprd12.prod.outlook.com (2603:10b6:930:61::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.33; Mon, 12 Jun 2023 04:58:04 +0000 Received: from CY4PEPF0000E9D9.namprd05.prod.outlook.com (2603:10b6:930:1e:cafe::52) by CY5PR04CA0015.outlook.office365.com (2603:10b6:930:1e::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.34 via Frontend Transport; Mon, 12 Jun 2023 04:58:04 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9D9.mail.protection.outlook.com (10.167.241.77) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:58:03 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:57:57 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , "Kirill A . Shutemov" Subject: [PATCH RFC v9 05/51] x86/coco: move CONFIG_HAS_CC_PLATFORM check down into coco/Makefile Date: Sun, 11 Jun 2023 23:25:13 -0500 Message-ID: <20230612042559.375660-6-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D9:EE_|CY8PR12MB7099:EE_ X-MS-Office365-Filtering-Correlation-Id: 74ea92ed-daa1-464e-f918-08db6b019aea X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: G8Qtn5v8D3fN7nlKCmp3QF5sxzqZo38E/6vXvRNeh/UL9L8OyTg+68KT2Sn58QW1u5k60W6EWag526oFEIxqekXMPO0Qzgy1A/oqZ+Wjq6joklDfYCf5+ihIp41VPn44muVOd7tOtu2Hb99Qs8sDcrObyvj2E6cjgA2PP9jPjmgQtJSY5/UWTXAXsmRoT9M0oPbcksWWUeZhBr9Wr/Sa4q48RpZ7YSrhR/Fh+XWXLwIvRhww7I0zNiISeCdTsSJW/AzMtCBcWg+NHHqJToOiNRODXEVQ0Na4h/GUc51F9YU8RKwVK7p76D9tLbv/5qqVCFixIDOox/vy/T1KKNbaSKJGdRI8sMticR8Uzgwuda05EJ6PgSqPCdXLhBqOrTX61nEPCLj+fHnpP7HiYNoyRTysLfvY83dOiEf5yDPRe6FJDTg2y0p7+q7550rPkJJj2GoOBd3j/Zia79TUB+xblOcJYKZfw/8A3DhskUZEVV54MNwmc86sJbbQD2oupvbDpugggfGyEiA7GO5U60BJCTuKtW+urN9XvL5tEcPiXgAHjQWdLRlsqAsKbQ2d5cjsHcWwf5fv9z5j73A8jFDlr8O7Urvr5VxXkxJ20tqQM6Jo1DS7GwR9Ga4a2eSH59JsExClOIoWVbxw9d+9Siy/0FI99nVkKAdy9eQj2ZG82syMddR0NKQgFkZ2IclOqrXdgeyCzA0SNLnrzFw2zpF15Z3b3/kfHYNoEUgPwAHg9PlXYzkSaozSykLmuZiSt0twNwbWWnZ45dh0WB1fJOZEsg== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(396003)(376002)(136003)(39860400002)(346002)(451199021)(40470700004)(46966006)(36840700001)(44832011)(7406005)(8676002)(7416002)(70206006)(316002)(8936002)(4326008)(70586007)(2906002)(6916009)(41300700001)(5660300002)(54906003)(6666004)(426003)(336012)(26005)(1076003)(186003)(36756003)(2616005)(16526019)(83380400001)(47076005)(36860700001)(82310400005)(478600001)(356005)(82740400003)(81166007)(40460700003)(40480700001)(86362001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:58:03.9724 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 74ea92ed-daa1-464e-f918-08db6b019aea X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D9.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7099 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Currently CONFIG_HAS_CC_PLATFORM is a prereq for building anything in arch/x86/coco, but that is generally only applicable for guest support. For SEV-SNP, helpers related purely to host support will also live in arch/x86/coco. To allow for CoCo-related host support code in arch/x86/coco, move that check down into the Makefile and check for it specifically when needed. Cc: Kirill A. Shutemov Suggested-by: Tom Lendacky Signed-off-by: Michael Roth --- arch/x86/Kbuild | 2 +- arch/x86/coco/Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/x86/Kbuild b/arch/x86/Kbuild index 5a83da703e87..1889cef48b58 100644 --- a/arch/x86/Kbuild +++ b/arch/x86/Kbuild @@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 -obj-$(CONFIG_ARCH_HAS_CC_PLATFORM) += coco/ +obj-y += coco/ obj-y += entry/ diff --git a/arch/x86/coco/Makefile b/arch/x86/coco/Makefile index c816acf78b6a..6aa52e719bf5 100644 --- a/arch/x86/coco/Makefile +++ b/arch/x86/coco/Makefile @@ -3,6 +3,6 @@ CFLAGS_REMOVE_core.o = -pg KASAN_SANITIZE_core.o := n CFLAGS_core.o += -fno-stack-protector -obj-y += core.o +obj-$(CONFIG_ARCH_HAS_CC_PLATFORM) += core.o obj-$(CONFIG_INTEL_TDX_GUEST) += tdx/ From patchwork Mon Jun 12 04:25:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275581 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA114C7EE25 for ; Mon, 12 Jun 2023 05:00:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232113AbjFLFAk (ORCPT ); Mon, 12 Jun 2023 01:00:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234725AbjFLE7i (ORCPT ); Mon, 12 Jun 2023 00:59:38 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2049.outbound.protection.outlook.com [40.107.93.49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 59B702129; Sun, 11 Jun 2023 21:58:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lVNDhns2qWnC7dzNxhUGXl+LjT5/fEx9dW22kTcOdQssl70bqljhrSc5lYccP1O0z6ip6IJ1RO+TqpOpmTCAtKXd/YONOuiafsT4mD7zNuiv8Z7ueEj2RNZmpG3fj7kIbbqgP5n3J/3uWJ932XjeDR0k8KWasDsHD+6+swsAAOZ3K49t8H/p0tv39Wgvih9+/RAqSZESCL4fsYazbOjE+cGFZ8+duMHPLvoIDaCUW/9KJxKxMzKJ6JnqqOSkK/b6cWX10AKECHrzqNQmN/mh60rlFRJYphBd33fJ48XPzVzCPQKVRnkggIkNpCDnFCj/BYE88LTF2vpX4ymW38xvUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=glZy7PdWi8qJOsWcUCj2WROa6to0Lr5SQ7LImMDzPlQ=; b=HRHBUmpUhJ1fumTaD8y19u61lCsV2HCxMYtiAMwwm4y846ZPD+S5NbF46ejNncykKmPSFQ+kexPLQ+OOvjfzLmgPwpasaeAnehgyaNeyCR4kjZNpgoSXorTwbHsoQc4O4D0QObEcEuUWKwm/LOHStvBIfTrQiS9QjnpLnHW8bYLYvlwjYWRS+JOCy2zLv5zFN8G+wHxL++2JrigtS71LOuFIa3fVm3gy1AYEeRtGcXTpiPjxBjMSnX56TcQMICOrEau+eQAA8b6tJocloMv59frl6BPGlYeV4dLT7/yPAv4EVUVMlwBJ5BecNtYDulDI5zmOj4QhNAYGfUHHBqefHA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=glZy7PdWi8qJOsWcUCj2WROa6to0Lr5SQ7LImMDzPlQ=; b=QgHULNBnG/IH8Z9FXbOkHKmxdeFTWaIvKi77Sxd4xq5Xa/vQZVHCVpDFzdN0dDN3L9+zHMYFTMa9o+jLVnVCpR/BLrwhYFMC36b9OQCVhNU4jXCyycFOUxF9zDefnciz2pzMJGLvqOGgUjobh8AdzA+BR6bw9jUWqefEdGbtpaE= Received: from MW4PR04CA0107.namprd04.prod.outlook.com (2603:10b6:303:83::22) by SA3PR12MB7829.namprd12.prod.outlook.com (2603:10b6:806:316::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.44; Mon, 12 Jun 2023 04:58:53 +0000 Received: from CO1NAM11FT078.eop-nam11.prod.protection.outlook.com (2603:10b6:303:83:cafe::56) by MW4PR04CA0107.outlook.office365.com (2603:10b6:303:83::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:58:53 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1NAM11FT078.mail.protection.outlook.com (10.13.175.177) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6455.23 via Frontend Transport; Mon, 12 Jun 2023 04:58:53 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:58:49 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Jarkko Sakkinen , Ashish Kalra Subject: [PATCH RFC v9 06/51] x86/cpufeatures: Add SEV-SNP CPU feature Date: Sun, 11 Jun 2023 23:25:14 -0500 Message-ID: <20230612042559.375660-7-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT078:EE_|SA3PR12MB7829:EE_ X-MS-Office365-Filtering-Correlation-Id: fc23a14d-6efd-4d36-2bca-08db6b01b86a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: yhydRVHiUiYjT4p746BxUSPDAhj9l1dvJ9tTzkkIf2g2kydVfOY/iqTEi5/xlWpnsVRsNw5u6DUF6wxByPLCo0vmhMQz3xkjoNF5lXGJoPzk6j81PFa/XbqfBh7oxEZg7bkFyW6XQbLCnn2/JLd66Gbx238SmF0gbWMBHteor56NWW4F6XnkUdf7Fb54vk2rtYOPv3lcO2recETGHATvAiQeo1yNF1wzcL68XcT1cvrdO5kph8IAntVHhTNElwaOvDvB70Ry5Di2mqTQHcgc95vXJGbthzB/WX1yxUbTHT438a+YN+f4l/uKS722f0DUAeN2igRsAar4RVw0taRnWoJpqjD1pT5AFpNRdJRFNz3eJzdHFP1CBPDmyoNUeu8Su+gGPZUfL1/dGJmHSbiBCQ4UQY0Jyoi0z06sXePp/kb7orfB5P3r3IQL7DH4Z6BRqLfAEBOWvaRknrKDhvHQtcI2MT9wQofqc4xJ4tGyZwTyEG6QzuMdtXlWKwzpLqIFOwAPCmuPtVrlPWVALb95UMk9t6ZyirgmKLOaZj9bSzlGOEqn2sqHzaQvSKQHiL36gpkQxvPmVSIiPcFONml6J/yGQ2YuGDnR4nNWiVf/H2iQ41coXh9Sk3MoOQ5uoR0s7PWgEr2Z+bFEkZMWET0b+HAojMx3rUAzkEjwB9Py2Zxrz/ma5pjPiJf3IouAp9FWYww/8nYa/hlJ97h3qQswoTDn8RkE5lnfb4OFcdkLVKUsqaZXb8sx2Q18QRiimZO+nhyOnnZ3zXRbWO9nvCeLLQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(136003)(346002)(396003)(376002)(451199021)(36840700001)(40470700004)(46966006)(336012)(426003)(2616005)(47076005)(83380400001)(36860700001)(82740400003)(356005)(40480700001)(86362001)(82310400005)(81166007)(36756003)(40460700003)(478600001)(54906003)(44832011)(4326008)(6666004)(8936002)(8676002)(2906002)(5660300002)(7416002)(7406005)(70206006)(70586007)(6916009)(41300700001)(316002)(16526019)(186003)(1076003)(26005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:58:53.4756 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fc23a14d-6efd-4d36-2bca-08db6b01b86a X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT078.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA3PR12MB7829 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh Add CPU feature detection for Secure Encrypted Virtualization with Secure Nested Paging. This feature adds a strong memory integrity protection to help prevent malicious hypervisor-based attacks like data replay, memory re-mapping, and more. Signed-off-by: Brijesh Singh Signed-off-by: Jarkko Sakkinen Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/kernel/cpu/amd.c | 5 +++-- tools/arch/x86/include/asm/cpufeatures.h | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index 97327a1e3aff..b60b32f47884 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -431,6 +431,7 @@ #define X86_FEATURE_SEV (19*32+ 1) /* AMD Secure Encrypted Virtualization */ #define X86_FEATURE_VM_PAGE_FLUSH (19*32+ 2) /* "" VM Page Flush MSR is supported */ #define X86_FEATURE_SEV_ES (19*32+ 3) /* AMD Secure Encrypted Virtualization - Encrypted State */ +#define X86_FEATURE_SEV_SNP (19*32+ 4) /* AMD Secure Encrypted Virtualization - Secure Nested Paging */ #define X86_FEATURE_V_TSC_AUX (19*32+ 9) /* "" Virtual TSC_AUX */ #define X86_FEATURE_SME_COHERENT (19*32+10) /* "" AMD hardware-enforced cache coherency */ diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c index 95cdd08c4cbb..a79774181f22 100644 --- a/arch/x86/kernel/cpu/amd.c +++ b/arch/x86/kernel/cpu/amd.c @@ -558,8 +558,8 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) * SME feature (set in scattered.c). * If the kernel has not enabled SME via any means then * don't advertise the SME feature. - * For SEV: If BIOS has not enabled SEV then don't advertise the - * SEV and SEV_ES feature (set in scattered.c). + * For SEV: If BIOS has not enabled SEV then don't advertise SEV and + * any additional functionality based on it. * * In all cases, since support for SME and SEV requires long mode, * don't advertise the feature under CONFIG_X86_32. @@ -594,6 +594,7 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) clear_sev: setup_clear_cpu_cap(X86_FEATURE_SEV); setup_clear_cpu_cap(X86_FEATURE_SEV_ES); + setup_clear_cpu_cap(X86_FEATURE_SEV_SNP); } } diff --git a/tools/arch/x86/include/asm/cpufeatures.h b/tools/arch/x86/include/asm/cpufeatures.h index b89005819cd5..b4f11a500470 100644 --- a/tools/arch/x86/include/asm/cpufeatures.h +++ b/tools/arch/x86/include/asm/cpufeatures.h @@ -424,6 +424,7 @@ #define X86_FEATURE_SEV (19*32+ 1) /* AMD Secure Encrypted Virtualization */ #define X86_FEATURE_VM_PAGE_FLUSH (19*32+ 2) /* "" VM Page Flush MSR is supported */ #define X86_FEATURE_SEV_ES (19*32+ 3) /* AMD Secure Encrypted Virtualization - Encrypted State */ +#define X86_FEATURE_SEV_SNP (19*32+ 4) /* AMD Secure Encrypted Virtualization - Secure Nested Paging */ #define X86_FEATURE_V_TSC_AUX (19*32+ 9) /* "" Virtual TSC_AUX */ #define X86_FEATURE_SME_COHERENT (19*32+10) /* "" AMD hardware-enforced cache coherency */ From patchwork Mon Jun 12 04:25:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275596 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 60335C7EE2E for ; Mon, 12 Jun 2023 05:01:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235845AbjFLFBC (ORCPT ); Mon, 12 Jun 2023 01:01:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236639AbjFLFAd (ORCPT ); Mon, 12 Jun 2023 01:00:33 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2062.outbound.protection.outlook.com [40.107.94.62]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 137551705; Sun, 11 Jun 2023 21:59:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MBmt2jieVjIJ9OLFCLOWjkwpRsxKrmrz+6skWOzuLb3eLD2hEcU/tULkZxHuROuNnOw5ScOhFt7VXb0E7NfvHhooS5r0eIoQe0iOSqI4Pp52rD0FZ1iYvJ1Re/o+A3HYn7fb42Q2UU2f8KD+Mxsw9vzZewJwtvt4sKvyf/mYi8Y46qM8jW3mcoZNNYQqLZ+BwOtSkTXWcm+pgHCYzrL7EnxOjVBB2krqNkS3I50xtAiWpCoECPs1+aQOJ0c9I47ixr7U67aM9juQvmwF76mX5l2y8HrDEx4wajpBeHV2wpMfkSRSIhaTFL8AjS5SCD7q0m8E5v6rMTPA+1HQPfqcgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=sQGmwA3MslzJEd0Pg5pUDqAuG/ZGYX7PxVZ9iQIDSmY=; b=J/VhEUHrApEKClXN3/OaILzx4FQwW8Drfm6cHnzMrfWhLNYfbbsgV8FSHFSmOp8Y9EIwnOxLMFqaQQgqiELUoS2WXKq/aCMoo9pLEI3/hvtFS5/SPOck6pdPNnrFEoDXI9qEesDtcEg7h7tquxDUKatcyPbVHSqTBBA97+3eYl6q1XVBIJLkCPv5z8TJoVQ0TomKD4J/M4APRdqXOcDFts8y8C1MK9nPdFEQtrWFIpL7A9LWbYthBaYqCjK9THuVwiX2o2Ls0HCiBtEHjvhmhrwbMVIfIinaBxs7DeV4Sln7YgdNp2/yj1YrZHI7+8Od5Qun/bhOFsmfwzLP6tKYmw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=sQGmwA3MslzJEd0Pg5pUDqAuG/ZGYX7PxVZ9iQIDSmY=; b=fsM19JYEGYeC5J1q3jnknNK9vBDKUnFyxg+RP1FKNl+zqTAvAm3lcg/ofl1Qf3/uX5GNI1DeYG7XEwlVIbfyG054ZxL5wKaWCxZmQleLurxR3x6mCfHvxlbA26Ssiw4HQnutgoLdVEFMEekKfrazas1/V7k4G9YeSVc71WN+tGU= Received: from MW4PR04CA0238.namprd04.prod.outlook.com (2603:10b6:303:87::33) by BN9PR12MB5161.namprd12.prod.outlook.com (2603:10b6:408:11a::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.41; Mon, 12 Jun 2023 04:59:40 +0000 Received: from CO1NAM11FT103.eop-nam11.prod.protection.outlook.com (2603:10b6:303:87:cafe::96) by MW4PR04CA0238.outlook.office365.com (2603:10b6:303:87::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:59:39 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1NAM11FT103.mail.protection.outlook.com (10.13.174.252) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.31 via Frontend Transport; Mon, 12 Jun 2023 04:59:39 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:59:33 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 07/51] x86/sev: Add the host SEV-SNP initialization support Date: Sun, 11 Jun 2023 23:25:15 -0500 Message-ID: <20230612042559.375660-8-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT103:EE_|BN9PR12MB5161:EE_ X-MS-Office365-Filtering-Correlation-Id: 0eb86ce6-0fb1-461d-7576-08db6b01d3b8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bBJ4PsVpKpraMgA2z/oBsrD4Jqhcqh2JokcwUWxV56BTRPKrbl2jkQIsk5Gcc4cPymDpj+Vs2hK4XrWdkfzYaoFbTgeC6yEEo7Yy1DWmrJHXM9GRkIRXneFqGL3CbLTAsPRsALJL/MyI8A8iPllh1qewCRoD0UWFeoK4trz92WIGZZUq7bDNVDZeJGPJE7zM71haVXEky17iaz0EQY8ARqghDGOZbg6C1O6D8XGd9T4GjIqaS2DoqVhUHnHBkFOdEE/+hcqq5MzJmbmNXMoVb1W4o/tl/74GgMNHgYNamSV8gXUEPtE9MSsrrqtwt7pTjnb5BPxHuWR7Sjr0L5dWAdJmlg1Ye7+w8TNTSjrMvYrTzhmqCxu7CrfJBzvQp3lOSoomGoNCQmTO/bLQgD16ghc/pSIOI5/nQIiNPw+5AL3SleotwPN4eQams5CytyVMUGWJ3ZUt5F6PD9Yg+c0UcxWAIaKf1j5khhd2xamgNaSZu6LFWlHRQyo/flRnY4fiVnRsPuVyY8UeSLD1Bv+DDDfKEmxiQSMugXT1NRtOiF7GJRexmTAdhJZPdy0MlYiUDTOHH293sQ8kZ/qVGgBhZoqTAZv9QKV4A8NCh/vBEvN0eYMyg7gIpgwLlJZzijjvpUmVFCtrBuMfumUTAATUlxDyJVQij+p3uOJHGMOxj1qk428XvrYuSEQb/3rs/w6NPEqmIgfGPqTQfhqKCUJd49bzurLRVH1gVkgyRN+8FPS5zHh6SpUOSMOJ88LjUPjE+cN7N/YZ8CmE8Klm7uWPpQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(376002)(396003)(346002)(136003)(39860400002)(451199021)(36840700001)(40470700004)(46966006)(8936002)(8676002)(44832011)(5660300002)(7406005)(7416002)(4326008)(6916009)(70586007)(70206006)(316002)(54906003)(2906002)(41300700001)(30864003)(36860700001)(40460700003)(6666004)(478600001)(81166007)(40480700001)(356005)(26005)(1076003)(16526019)(36756003)(426003)(336012)(83380400001)(186003)(47076005)(2616005)(82740400003)(82310400005)(86362001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:59:39.2715 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0eb86ce6-0fb1-461d-7576-08db6b01d3b8 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT103.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5161 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh 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. APM2 section 15.36 details a number of steps needed to detect/enable SEV-SNP and RMP table support on the host: - Detect SEV-SNP support based on CPUID bit - Initialize the RMP table memory reported by the RMP base/end MSR registers and configure IOMMU to be compatible with RMP access restrictions - Set the MtrrFixDramModEn bit in SYSCFG MSR - Set the SecureNestedPagingEn and VMPLEn bits in the SYSCFG MSR - Configure IOMMU RMP table entry format is non-architectural and it can vary by processor. It is defined by the PPR. Restrict SNP support to CPU models/families which are compatible with the current RMP table entry format to guard against any undefined behavior when running on other system types. Future models/support will handle this through an architectural mechanism to allow for broader compatibility. SNP host code depends on CONFIG_KVM_AMD_SEV config flag, which may be enabled even when CONFIG_AMD_MEM_ENCRYPT isn't set, so update the SNP-specific IOMMU helpers used here to rely on CONFIG_KVM_AMD_SEV instead of CONFIG_AMD_MEM_ENCRYPT. Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Co-developed-by: Tom Lendacky Signed-off-by: Tom Lendacky Signed-off-by: Brijesh Singh [mdr: rework commit message to be clearer about what patch does, squash in early_rmptable_check() handling from Tom] Signed-off-by: Michael Roth --- arch/x86/coco/Makefile | 1 + arch/x86/coco/sev/Makefile | 3 + arch/x86/coco/sev/host.c | 212 +++++++++++++++++++++++ arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/msr-index.h | 11 +- arch/x86/include/asm/sev.h | 2 + arch/x86/kernel/cpu/amd.c | 19 ++ drivers/iommu/amd/init.c | 2 +- include/linux/amd-iommu.h | 2 +- 9 files changed, 256 insertions(+), 4 deletions(-) create mode 100644 arch/x86/coco/sev/Makefile create mode 100644 arch/x86/coco/sev/host.c diff --git a/arch/x86/coco/Makefile b/arch/x86/coco/Makefile index 6aa52e719bf5..6a7d876130e2 100644 --- a/arch/x86/coco/Makefile +++ b/arch/x86/coco/Makefile @@ -6,3 +6,4 @@ CFLAGS_core.o += -fno-stack-protector obj-$(CONFIG_ARCH_HAS_CC_PLATFORM) += core.o obj-$(CONFIG_INTEL_TDX_GUEST) += tdx/ +obj-$(CONFIG_KVM_AMD_SEV) += sev/ diff --git a/arch/x86/coco/sev/Makefile b/arch/x86/coco/sev/Makefile new file mode 100644 index 000000000000..27c0500d75c8 --- /dev/null +++ b/arch/x86/coco/sev/Makefile @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-y += host.o diff --git a/arch/x86/coco/sev/host.c b/arch/x86/coco/sev/host.c new file mode 100644 index 000000000000..6907ce887b23 --- /dev/null +++ b/arch/x86/coco/sev/host.c @@ -0,0 +1,212 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * AMD SVM-SEV Host Support. + * + * Copyright (C) 2023 Advanced Micro Devices, Inc. + * + * Author: Ashish Kalra + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * The first 16KB from the RMP_BASE is used by the processor for the + * bookkeeping, the range needs to be added during the RMP entry lookup. + */ +#define RMPTABLE_CPU_BOOKKEEPING_SZ 0x4000 + +static unsigned long rmptable_start __ro_after_init; +static unsigned long rmptable_end __ro_after_init; + +#undef pr_fmt +#define pr_fmt(fmt) "SEV-SNP: " fmt + +static int __mfd_enable(unsigned int cpu) +{ + u64 val; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return 0; + + rdmsrl(MSR_AMD64_SYSCFG, val); + + val |= MSR_AMD64_SYSCFG_MFDM; + + wrmsrl(MSR_AMD64_SYSCFG, val); + + return 0; +} + +static __init void mfd_enable(void *arg) +{ + __mfd_enable(smp_processor_id()); +} + +static int __snp_enable(unsigned int cpu) +{ + u64 val; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return 0; + + rdmsrl(MSR_AMD64_SYSCFG, val); + + val |= MSR_AMD64_SYSCFG_SNP_EN; + val |= MSR_AMD64_SYSCFG_SNP_VMPL_EN; + + wrmsrl(MSR_AMD64_SYSCFG, val); + + return 0; +} + +static __init void snp_enable(void *arg) +{ + __snp_enable(smp_processor_id()); +} + +bool snp_get_rmptable_info(u64 *start, u64 *len) +{ + u64 max_rmp_pfn, calc_rmp_sz, rmp_sz, rmp_base, rmp_end; + + rdmsrl(MSR_AMD64_RMP_BASE, rmp_base); + rdmsrl(MSR_AMD64_RMP_END, rmp_end); + + if (!rmp_base || !rmp_end) { + pr_err("Memory for the RMP table has not been reserved by BIOS\n"); + return false; + } + + rmp_sz = rmp_end - rmp_base + 1; + + /* + * Calculate the amount the memory that must be reserved by the BIOS to + * address the whole RAM, including the bookkeeping area. The RMP itself + * must also be covered. + */ + max_rmp_pfn = max_pfn; + if (PHYS_PFN(rmp_end) > max_pfn) + max_rmp_pfn = PHYS_PFN(rmp_end); + + calc_rmp_sz = (max_rmp_pfn << 4) + RMPTABLE_CPU_BOOKKEEPING_SZ; + + if (calc_rmp_sz > rmp_sz) { + pr_err("Memory reserved for the RMP table does not cover full system RAM (expected 0x%llx got 0x%llx)\n", + calc_rmp_sz, rmp_sz); + return false; + } + + *start = rmp_base; + *len = rmp_sz; + + return true; +} + +static __init int __snp_rmptable_init(void) +{ + u64 rmp_base, sz; + void *start; + u64 val; + + if (!snp_get_rmptable_info(&rmp_base, &sz)) + return 1; + + pr_info("RMP table physical address [0x%016llx - 0x%016llx]\n", + rmp_base, rmp_base + sz - 1); + + start = memremap(rmp_base, sz, MEMREMAP_WB); + if (!start) { + pr_err("Failed to map RMP table addr 0x%llx size 0x%llx\n", rmp_base, sz); + return 1; + } + + /* + * Check if SEV-SNP is already enabled, this can happen in case of + * kexec boot. + */ + rdmsrl(MSR_AMD64_SYSCFG, val); + if (val & MSR_AMD64_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 SNP is enabled. */ + wbinvd_on_all_cpus(); + + /* MFDM must be enabled on all the CPUs prior to enabling SNP. */ + on_each_cpu(mfd_enable, NULL, 1); + + /* Enable SNP on all CPUs. */ + on_each_cpu(snp_enable, NULL, 1); + +skip_enable: + rmptable_start = (unsigned long)start; + rmptable_end = rmptable_start + sz - 1; + + return 0; +} + +static int __init snp_rmptable_init(void) +{ + int family, model; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return 0; + + family = boot_cpu_data.x86; + model = boot_cpu_data.x86_model; + + /* + * RMP table entry format is not architectural and it can vary by processor and + * is defined by the per-processor PPR. Restrict SNP support on the known CPU + * model and family for which the RMP table entry format is currently defined for. + */ + if (!(family == 0x19 && model <= 0xaf) && !(family == 0x1a && model <= 0xf)) + goto nosnp; + + if (amd_iommu_snp_enable()) + goto nosnp; + + if (__snp_rmptable_init()) + goto nosnp; + + cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/rmptable_init:online", __snp_enable, NULL); + + return 0; + +nosnp: + setup_clear_cpu_cap(X86_FEATURE_SEV_SNP); + return -ENOSYS; +} + +/* + * This must be called after the PCI subsystem. This is because amd_iommu_snp_enable() + * is called to ensure the IOMMU supports the SEV-SNP feature, which can only be + * called after subsys_initcall(). + * + * NOTE: IOMMU is enforced by SNP to ensure that hypervisor cannot program DMA + * directly into guest private memory. In case of SNP, the IOMMU ensures that + * the page(s) used for DMA are hypervisor owned. + */ +fs_initcall(snp_rmptable_init); diff --git a/arch/x86/include/asm/disabled-features.h b/arch/x86/include/asm/disabled-features.h index 5dfa4fb76f4b..0a9938aea305 100644 --- a/arch/x86/include/asm/disabled-features.h +++ b/arch/x86/include/asm/disabled-features.h @@ -99,6 +99,12 @@ # define DISABLE_TDX_GUEST (1 << (X86_FEATURE_TDX_GUEST & 31)) #endif +#ifdef CONFIG_KVM_AMD_SEV +# define DISABLE_SEV_SNP 0 +#else +# define DISABLE_SEV_SNP (1 << (X86_FEATURE_SEV_SNP & 31)) +#endif + /* * Make sure to add features to the correct mask */ @@ -123,7 +129,7 @@ DISABLE_ENQCMD) #define DISABLED_MASK17 0 #define DISABLED_MASK18 0 -#define DISABLED_MASK19 0 +#define DISABLED_MASK19 (DISABLE_SEV_SNP) #define DISABLED_MASK20 0 #define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 21) diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h index ad35355ee43e..db0f3a041930 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -574,6 +574,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 /* SNP feature bits enabled by the hypervisor */ #define MSR_AMD64_SNP_VTOM BIT_ULL(3) @@ -675,7 +677,14 @@ #define MSR_K8_TOP_MEM2 0xc001001d #define MSR_AMD64_SYSCFG 0xc0010010 #define MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT 23 -#define MSR_AMD64_SYSCFG_MEM_ENCRYPT BIT_ULL(MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT) +#define MSR_AMD64_SYSCFG_MEM_ENCRYPT BIT_ULL(MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT) +#define MSR_AMD64_SYSCFG_SNP_EN_BIT 24 +#define MSR_AMD64_SYSCFG_SNP_EN BIT_ULL(MSR_AMD64_SYSCFG_SNP_EN_BIT) +#define MSR_AMD64_SYSCFG_SNP_VMPL_EN_BIT 25 +#define MSR_AMD64_SYSCFG_SNP_VMPL_EN BIT_ULL(MSR_AMD64_SYSCFG_SNP_VMPL_EN_BIT) +#define MSR_AMD64_SYSCFG_MFDM_BIT 19 +#define MSR_AMD64_SYSCFG_MFDM BIT_ULL(MSR_AMD64_SYSCFG_MFDM_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.h b/arch/x86/include/asm/sev.h index ebc271bb6d8e..d34c46db7dd1 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -197,6 +197,7 @@ void snp_set_wakeup_secondary_cpu(void); bool snp_init(struct boot_params *bp); void __init __noreturn snp_abort(void); int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsigned long *fw_err); +bool snp_get_rmptable_info(u64 *start, u64 *len); #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } @@ -221,6 +222,7 @@ static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *in { return -ENOTTY; } +static inline bool snp_get_rmptable_info(u64 *start, u64 *len) { return false; } #endif #endif diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c index a79774181f22..1493ddf89fdf 100644 --- a/arch/x86/kernel/cpu/amd.c +++ b/arch/x86/kernel/cpu/amd.c @@ -20,6 +20,7 @@ #include #include #include +#include #ifdef CONFIG_X86_64 # include @@ -546,6 +547,20 @@ static void bsp_init_amd(struct cpuinfo_x86 *c) resctrl_cpu_detect(c); } +static bool early_rmptable_check(void) +{ + u64 rmp_base, rmp_size; + + /* + * For early BSP initialization, max_pfn won't be set up yet, wait until + * it is set before performing the RMP table calculations. + */ + if (!max_pfn) + return true; + + return snp_get_rmptable_info(&rmp_base, &rmp_size); +} + static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) { u64 msr; @@ -587,6 +602,9 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) if (!(msr & MSR_K7_HWCR_SMMLOCK)) goto clear_sev; + if (cpu_has(c, X86_FEATURE_SEV_SNP) && !early_rmptable_check()) + goto clear_snp; + return; clear_all: @@ -594,6 +612,7 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) clear_sev: setup_clear_cpu_cap(X86_FEATURE_SEV); setup_clear_cpu_cap(X86_FEATURE_SEV_ES); +clear_snp: setup_clear_cpu_cap(X86_FEATURE_SEV_SNP); } } diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c index 19a46b9f7357..33ea62d93540 100644 --- a/drivers/iommu/amd/init.c +++ b/drivers/iommu/amd/init.c @@ -3665,7 +3665,7 @@ int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true); } -#ifdef CONFIG_AMD_MEM_ENCRYPT +#ifdef CONFIG_KVM_AMD_SEV int amd_iommu_snp_enable(void) { /* diff --git a/include/linux/amd-iommu.h b/include/linux/amd-iommu.h index 953e6f12fa1c..8f0cde2d451c 100644 --- a/include/linux/amd-iommu.h +++ b/include/linux/amd-iommu.h @@ -206,7 +206,7 @@ int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value); struct amd_iommu *get_amd_iommu(unsigned int idx); -#ifdef CONFIG_AMD_MEM_ENCRYPT +#ifdef CONFIG_KVM_AMD_SEV int amd_iommu_snp_enable(void); #endif From patchwork Mon Jun 12 04:25:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275597 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 82057C7EE25 for ; Mon, 12 Jun 2023 05:01:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236013AbjFLFBZ (ORCPT ); Mon, 12 Jun 2023 01:01:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235453AbjFLFAx (ORCPT ); Mon, 12 Jun 2023 01:00:53 -0400 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2060.outbound.protection.outlook.com [40.107.96.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8EC561FFE; Sun, 11 Jun 2023 22:00:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nGKg4VRglm7w5OTKYyS0K1SdAzDUmCd/9Ld80KGxPNgUly0//ZLWuKdOd+trJX1RTUipKcf3C458Ra6LmEmyLDg1l1V5M9pFnlUS+yZAjm/KDQIaauL62F5QqTCZTZKc2dsr5vRI+8+Czd5WNXh+vDXO83OTMBT3+oC3z3eRbUz4X8sxIBv1d2AL4u5Kamhq7holAS+by4b1xbqw7nG3S31Nb0BHBj3U4i1fV9MTkUs6Or5jwpSDl+bdiH+p3pVolHYohxT7Egt2sjtE8zEFy7y34vzgYthf8gFtGgvpX/6j2WtxXxr+tQ6ctBUiNr/9ye2zqu9axjKkmlWeD7zH4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=JkCAa24YyiWlkukD/LaO5dOR1Z6gqOaYm5Qyl4UEKYk=; b=AXP82GuEEG5gjQda9i0jKKagsCn1/axvwc//tHsK3wAZjcoGZem7q5cyPCRVdGTvzTtMmuLuIKFk+YaPlU6pcgOWfsCxBzI22XqCH3CvLVrZN61JHmYpoJB5YvCawO0BuYY82aTc4UdR/tvIje4vgtldnR5A+20ujlpeWz7MEHIOc8wbIDe7NtgqTQCPVuVV974gDWzG64YOnA4CiuwZ+kAb9k/N+WyhQUpQzhl0plC5EEkuDVikEl3tzCuYqdQDKMPWsYaRW7tMZbdRqA6M2Xewif0V9v9vj3WDRUYBko3XhfEuUiVS5skhXw2dxD1o90xFGVowHWUsxOgOrlLhVg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=JkCAa24YyiWlkukD/LaO5dOR1Z6gqOaYm5Qyl4UEKYk=; b=GcxrSPZvj/0Dcy3Cuh6cSWUJy/83nPOH9MXyUj1HRVqrke+c+O+fIKJNnVxpzSivyX6KcybJFSlvykIAi6hpdrPZ/jempsc93KzRQtQ/Q4YlKgeDaxmTJPhXNIFcZpe1/DbQDNvqQZfzbJGZQG1crQ2SfkBWt0m45TjMdRWYYZs= Received: from MW3PR06CA0028.namprd06.prod.outlook.com (2603:10b6:303:2a::33) by PH8PR12MB6771.namprd12.prod.outlook.com (2603:10b6:510:1c6::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.46; Mon, 12 Jun 2023 05:00:02 +0000 Received: from CO1NAM11FT012.eop-nam11.prod.protection.outlook.com (2603:10b6:303:2a:cafe::17) by MW3PR06CA0028.outlook.office365.com (2603:10b6:303:2a::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 05:00:01 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1NAM11FT012.mail.protection.outlook.com (10.13.175.192) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 05:00:01 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:59:55 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Kim Phillips Subject: [PATCH RFC v9 08/51] x86/speculation: Do not enable Automatic IBRS if SEV SNP is enabled Date: Sun, 11 Jun 2023 23:25:16 -0500 Message-ID: <20230612042559.375660-9-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT012:EE_|PH8PR12MB6771:EE_ X-MS-Office365-Filtering-Correlation-Id: 96b215c0-b78c-4111-db5e-08db6b01e11a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qSzdUl9gWvMpdS01A5WUdoWi5VSKEY+M/DIT8NKlYs5wNCB5MG4aqVMw1tn8Rsjz7NPYLs0ui2xLLBMe5chKrEosF8SUF76emJ/C9vVkrrz/y7tqU+kFUcQknCxMW4a8tJW2JC4k0NoLsNxxX6Bl5CiPvUeYb6W8tCTSA0ceGokW7SsAlCBHUEHx4S88My+Ii4eY+BZ0aLibwwg+9erzpUew4V6dPDPg+R+05yqV37VmtGBjj9rMjzsdgjr1iLmA2BdooEpVm6j8veAMpc8ipqRtBvIcoXZAeDGUcBcEgYp4kkn3pxGlAsm3kw+Lwew4/3pz26mR6DYJt/WN1/rya0VC4vu16OEM2THI7MmFo4iwu7OUQ8EYhq4sTFr1Auw4K34ik/s9FWhvwbERJsg7GqgSlPv7+kXFE/InXyraRDRFhYwbTLqL7niyQspUG2U9tdzZ5b6VfzOl1GSiKgAAr/+IPFOLhaR2LL+wlrapZbsPS4sVWQJv39B+TBfSKCVkXdNuV86yK90tt2f2cyZzDim9sUOGQ9X5O+YRGRBgE9fmc+hu7jFM/gh6oV2Jg3VejaybhiqlGE2qpGEY60TkoOReb26gXlVxG0or60J/KKgb60vc/iTEJqJHb6+cn+ujeXJRQq87nb9TR9vm29PRPJkgPt0DVd1cZGSy2u6k1d94gXaC7BcxVbC1fFd+nH3/bRSX0ggyqv1QYNOKiIIK4S1odBYIsLkvohJRPy5CouNw6Jqd2HncgIFYIXHluEgAoRhjQweNiVVw6rMpPO4QWA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(376002)(346002)(396003)(136003)(451199021)(36840700001)(40470700004)(46966006)(426003)(336012)(2616005)(47076005)(83380400001)(36860700001)(356005)(82740400003)(40480700001)(36756003)(86362001)(82310400005)(81166007)(40460700003)(478600001)(54906003)(44832011)(6666004)(4326008)(8936002)(8676002)(2906002)(5660300002)(7416002)(7406005)(70206006)(70586007)(6916009)(41300700001)(316002)(186003)(16526019)(1076003)(26005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 05:00:01.7267 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 96b215c0-b78c-4111-db5e-08db6b01e11a X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT012.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB6771 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Kim Phillips A hardware limitation prevents the host from enabling Automatic IBRS when SNP is enabled. Instead, fall back to retpolines. Signed-off-by: Kim Phillips Signed-off-by: Michael Roth Signed-off-by: Kim Phillips Signed-off-by: Kim Phillips Signed-off-by: Kim Phillips --- arch/x86/kernel/cpu/bugs.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c index f9d060e71c3e..3fba3623ff64 100644 --- a/arch/x86/kernel/cpu/bugs.c +++ b/arch/x86/kernel/cpu/bugs.c @@ -1507,7 +1507,12 @@ static void __init spectre_v2_select_mitigation(void) if (spectre_v2_in_ibrs_mode(mode)) { if (boot_cpu_has(X86_FEATURE_AUTOIBRS)) { - msr_set_bit(MSR_EFER, _EFER_AUTOIBRS); + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) { + msr_set_bit(MSR_EFER, _EFER_AUTOIBRS); + } else { + pr_err("SNP feature available, not enabling AutoIBRS on the host.\n"); + mode = spectre_v2_select_retpoline(); + } } else { x86_spec_ctrl_base |= SPEC_CTRL_IBRS; update_spec_ctrl(x86_spec_ctrl_base); From patchwork Mon Jun 12 04:25:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275444 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 17F09C7EE23 for ; Mon, 12 Jun 2023 04:27:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233172AbjFLE1E (ORCPT ); Mon, 12 Jun 2023 00:27:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229455AbjFLE1C (ORCPT ); Mon, 12 Jun 2023 00:27:02 -0400 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2059.outbound.protection.outlook.com [40.107.212.59]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76F6510A; Sun, 11 Jun 2023 21:26:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BIG4ldRGetGcHHgn5DhjUD5JrTINL0kA4DtdJ7uu4HR58y1LBpLx/BFHt5N6zhbimhS1Prw1I6tKam6nKylAEgh6QcVnEqeEPisJoJYMPzNzmZ2ZD/XcyP8R8wiJi2KkmszntrUYeDNQ5ZQCmsv+v1O68PqOeYp5DbUxx0Bq2JVVWOQDC7XAT0visrLqsOiVuRr/qBAJHmZpI3Qsx+cCwGa1D9hv/GGhljAEbd672UTTmYZQzbcnlOm2n/lKenkFl1IQzB1vlRhuroQofV3dDtULaADBEf8ufTuiyIQei1jZtSwA3DOKpwhOovxNzCSWDXOLdbrgItgzO+odUVpVMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=WlfUBo/FyI8IPg/IWyuoH/hWaTywD2b9n9I2Lt8R/Us=; b=C1B94VZchwKv3F94bOkJ1tjmpUTzydlPwl0TqTip01c/qMWaHfisWKB237bbrUGeq2ttebcxmvrqvs36DRp520gSLmX1JXnjIFhI/ZY7HCIyiD78oo1uJNR3goh3CxkznLnHQadcGldg3rcOVDTWgfA7AAAM4dV3PTwMQZEh9sq9JSsQ2CYhnEOaC/bt15ZgpK0vQOap2/VnaXntLFVKTNcd7GNkknTmp4aD/hWmkQ+0vhwh/v7FS8HjDvWfT3KvP9uuoeqYs7vLZJQbbjMHUbXbFbSW3wXE6NDc/f9fM+xT7asluv5CH8iydSRIzEhzGGFcNt8Jq2SA55zwSRdULg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=WlfUBo/FyI8IPg/IWyuoH/hWaTywD2b9n9I2Lt8R/Us=; b=KZfhxEGHUCpzZv5AZWYn3o5O0rij1zTtcu2i4BA5BqMbWhvNygQqG8lylRaiGSdx0vav4dk6wOVOjd/mOnady979WlJu7+/OVxCz8EMJL0obXb9LI7JZ/YZT0gnXHvV+HAcUje+6qvEMEA4VBjC9vcnNMDN1dPAEAlnhhI5tjxU= Received: from MW2PR16CA0025.namprd16.prod.outlook.com (2603:10b6:907::38) by CYYPR12MB8701.namprd12.prod.outlook.com (2603:10b6:930:bf::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.44; Mon, 12 Jun 2023 04:26:53 +0000 Received: from CO1PEPF000044F1.namprd05.prod.outlook.com (2603:10b6:907:0:cafe::f8) by MW2PR16CA0025.outlook.office365.com (2603:10b6:907::38) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:26:53 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1PEPF000044F1.mail.protection.outlook.com (10.167.241.71) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:26:52 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:26:51 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 09/51] x86/sev: Add RMP entry lookup helpers Date: Sun, 11 Jun 2023 23:25:17 -0500 Message-ID: <20230612042559.375660-10-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000044F1:EE_|CYYPR12MB8701:EE_ X-MS-Office365-Filtering-Correlation-Id: 280b3df5-4843-4484-af1c-08db6afd3fa0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: DQ826GZCtXMQmsxLj56srWSPkfqaN/BwIjmcqprxu/9TitlM1EnxxLdZ+Y0wVs1CkN2Hh90DoOcRGd8bsohMzUeSE4yTSPDz0ZTWwICEOA6jfG8/LuKq4lJGiSz5DdVo7qWY0xUP3gnPZ/hc0/UiPTV7etC3pdCa0KKOVD7VgBMi+1xN/YbGjNbkTWYQdd3aK3MJyqzKly77zGebHzESoTLUSL+NMAhy068J2leiQxXVgri9BtCeujf1y4CHsCPWiBRNX8WhAiNM2+8ImUOVJ6esEcuVUqTpjVWHruLjEtRkx4luX/dwl52tXHqdNFprwidKy475hsnEjHUnV6TPYyjHjiwhCZifVLqSeVAc+kxEKLSRmG9BhTounJEycTNPloHW1LeEuDesRbUV+5KOQgzn6W5Xzny8UIJm2oYBenz6j6mO4d5HXhu0Iok7LMDa+/7+XINUazJdATlbSKpjRMH4ikISkNlia2HVHnZm0xNeL9c2PqflRJv5hQeS+7xHkT/WLf1xden8OMwuo2L0/E+3YeF3fddy6hvYJRCoDE4ZXcvld+PZlmTAuGlt5k1Rg0D6KqXToKpvqw439zNfgfr3QNtbTq/xdgbjielwQ7Yt9A1QZYCmIwFHIxGaeyuKYmPcviXD7Kx4bkRGLp8G2X8fv/Y4mR+awrtRcmaAxnndFBmnSeypffoSrEdphccNfcCrK4QF8r+NfoCyXCqcTCKQKbXebGmqPeJQijt9P5Ypst2LwTGgS7Rd1IBVK8CYlBnzMVHsasGyhPgOEqEh3oXdy5nwUlxAD9ibKbkk3CY= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(346002)(39860400002)(136003)(376002)(396003)(451199021)(40470700004)(36840700001)(46966006)(7406005)(5660300002)(7416002)(47076005)(8936002)(2906002)(8676002)(41300700001)(83380400001)(336012)(36860700001)(44832011)(426003)(82740400003)(356005)(81166007)(6916009)(70206006)(82310400005)(316002)(2616005)(70586007)(40460700003)(4326008)(186003)(16526019)(40480700001)(26005)(86362001)(1076003)(54906003)(966005)(478600001)(36756003)(6666004)(36900700001)(309714004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:26:52.8260 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 280b3df5-4843-4484-af1c-08db6afd3fa0 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1PEPF000044F1.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CYYPR12MB8701 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh The snp_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 AMD PPR, see https://bugzilla.kernel.org/attachment.cgi?id=296015. Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Signed-off-by: Brijesh Singh [mdr: separate 'assigned' indicator from return code] Signed-off-by: Michael Roth --- arch/x86/coco/sev/host.c | 85 +++++++++++++++++++++++++++++++ arch/x86/include/asm/sev-common.h | 4 ++ arch/x86/include/asm/sev-host.h | 22 ++++++++ arch/x86/include/asm/sev.h | 3 -- 4 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 arch/x86/include/asm/sev-host.h diff --git a/arch/x86/coco/sev/host.c b/arch/x86/coco/sev/host.c index 6907ce887b23..0cc5a6d11b25 100644 --- a/arch/x86/coco/sev/host.c +++ b/arch/x86/coco/sev/host.c @@ -30,11 +30,36 @@ #include #include +/* + * The RMP entry format is not architectural. The format is defined in PPR + * Family 19h Model 01h, Rev B1 processor. + */ +struct rmpentry { + union { + struct { + u64 assigned : 1, + pagesize : 1, + immutable : 1, + rsvd1 : 9, + gpa : 39, + asid : 10, + vmsa : 1, + validated : 1, + rsvd2 : 1; + } info; + u64 low; + }; + u64 high; +} __packed; + /* * The first 16KB from the RMP_BASE is used by the processor for the * bookkeeping, the range needs to be added during the RMP entry lookup. */ #define RMPTABLE_CPU_BOOKKEEPING_SZ 0x4000 +#define RMPENTRY_SHIFT 8 +#define rmptable_page_offset(x) (RMPTABLE_CPU_BOOKKEEPING_SZ + \ + (((unsigned long)x) >> RMPENTRY_SHIFT)) static unsigned long rmptable_start __ro_after_init; static unsigned long rmptable_end __ro_after_init; @@ -210,3 +235,63 @@ static int __init snp_rmptable_init(void) * the page(s) used for DMA are hypervisor owned. */ fs_initcall(snp_rmptable_init); + +static inline unsigned int rmpentry_assigned(const struct rmpentry *e) +{ + return e->info.assigned; +} + +static inline unsigned int rmpentry_pagesize(const struct rmpentry *e) +{ + return e->info.pagesize; +} + +static int rmptable_entry(unsigned long paddr, struct rmpentry *entry) +{ + unsigned long vaddr; + + vaddr = rmptable_start + rmptable_page_offset(paddr); + if (unlikely(vaddr > rmptable_end)) + return -EFAULT; + + *entry = *(struct rmpentry *)vaddr; + + return 0; +} + +static int __snp_lookup_rmpentry(u64 pfn, struct rmpentry *entry, int *level) +{ + unsigned long paddr = pfn << PAGE_SHIFT; + struct rmpentry large_entry; + int ret; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return -ENXIO; + + ret = rmptable_entry(paddr, entry); + if (ret) + return ret; + + /* Read a large RMP entry to get the correct page level used in RMP entry. */ + ret = rmptable_entry(paddr & PMD_MASK, &large_entry); + if (ret) + return ret; + + *level = RMP_TO_X86_PG_LEVEL(rmpentry_pagesize(&large_entry)); + + return 0; +} + +int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) +{ + struct rmpentry e; + int ret; + + ret = __snp_lookup_rmpentry(pfn, &e, level); + if (ret) + return ret; + + *assigned = !!rmpentry_assigned(&e); + return 0; +} +EXPORT_SYMBOL_GPL(snp_lookup_rmpentry); diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index b8357d6ecd47..bf0378136289 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -171,4 +171,8 @@ struct snp_psc_desc { #define GHCB_ERR_INVALID_INPUT 5 #define GHCB_ERR_INVALID_EVENT 6 +/* RMP page size */ +#define RMP_PG_SIZE_4K 0 +#define RMP_TO_X86_PG_LEVEL(level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M) + #endif diff --git a/arch/x86/include/asm/sev-host.h b/arch/x86/include/asm/sev-host.h new file mode 100644 index 000000000000..30d47e20081d --- /dev/null +++ b/arch/x86/include/asm/sev-host.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * AMD SVM-SEV Host Support. + * + * Copyright (C) 2023 Advanced Micro Devices, Inc. + * + * Author: Ashish Kalra + * + */ + +#ifndef __ASM_X86_SEV_HOST_H +#define __ASM_X86_SEV_HOST_H + +#include + +#ifdef CONFIG_KVM_AMD_SEV +int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level); +#else +static inline int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) { return 0; } +#endif + +#endif diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index d34c46db7dd1..446fc7a9f7b0 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -81,9 +81,6 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs); /* Software defined (when rFlags.CF = 1) */ #define PVALIDATE_FAIL_NOUPDATE 255 -/* RMP page size */ -#define RMP_PG_SIZE_4K 0 - #define RMPADJUST_VMSA_PAGE_BIT BIT(16) /* SNP Guest message request */ From patchwork Mon Jun 12 04:25:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275445 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 05943C7EE23 for ; Mon, 12 Jun 2023 04:28:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229455AbjFLE2J (ORCPT ); Mon, 12 Jun 2023 00:28:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36984 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233320AbjFLE1h (ORCPT ); Mon, 12 Jun 2023 00:27:37 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2058.outbound.protection.outlook.com [40.107.95.58]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2B6F31A5; Sun, 11 Jun 2023 21:27:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kVl++eSid4DAaBvxslCd3GmMmzrocTZ9rdLXfv/gpRqdyFisH9GVw3JZXXIV/G+lOSNk2OS34zp7TkWQPgykPQOzRFr0k9OcRwJ1eUqIdxbJAsyIJf6AYX7Hha6NV2HXBm1BXSwADJzHDIvYVlfUtT4wC37NliAz+xm3GV1GIIMBuC/e6tSR7rmq0WSmrrPCakFzuTD2tDd7ewMDEC/NoDm6RxkqJb5WhqWKiQ7sgbgGHwP+igb/XI0hSQLRn/0/679AcJWebHeYz9lhB4pg1iw45XBF92+WCl4qohWqEtyt6ohMLHCqOW1Errz/kxNLwcj1jwaD9iuJo44XgRROfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=aRmnZV9+q0iWnxivqpSKbZUlVCI+wjQITiyoM9Ekv08=; b=h/fHsJsQ7re9v4WUgZZs0GkMMv4KlRrW0H0vrSgb7fxhajkZ9Up0rHFCvt1o86Ds5r8lyH0NdarRExZgOAGDGfwwmM40daKajeVo8tJXnu47u47RVag7m6G8mp31iuXO7kS8R8kz4yXLZFh0wCRK9MD1h8Zpd52l7xoxfB9cCCYyJqIYPL0tHv9B1dMXfKWI7vPPt0VsRfLPIBZIZ0DiE0LXgWEg2Dt055BsgdMeGdhdZRAQW1hSVkyt7I93SpQb6hMb7kmWDDEQFA/KG5zF/3QBWuOIQggJ4zqF5bX86UqLl9YHALi8nDTekr1BRkIy6ypubrmyeWyEhdpvmFTpHw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=temperror (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=temperror action=none header.from=amd.com; dkim=none (message not signed); 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=aRmnZV9+q0iWnxivqpSKbZUlVCI+wjQITiyoM9Ekv08=; b=s48ASt0BZuLSJb9nucZ5nCuw/r7v7eUM5VGKcwEgMETM0TdMYhp3Da/iSy7jB7aIrqiw8GVRf5ueVKz37BO8w17h9P1E17ikLCnIUx/k9DL7vpT8nRjelC9sAczTLNmBrdn6nynKEBocT2Sy61OKwR+IbaWxoDjfavU5ykZOTNc= Received: from CY5PR15CA0136.namprd15.prod.outlook.com (2603:10b6:930:68::24) by PH0PR12MB7840.namprd12.prod.outlook.com (2603:10b6:510:28a::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.29; Mon, 12 Jun 2023 04:27:28 +0000 Received: from CY4PEPF0000E9D2.namprd03.prod.outlook.com (2603:10b6:930:68:cafe::26) by CY5PR15CA0136.outlook.office365.com (2603:10b6:930:68::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.34 via Frontend Transport; Mon, 12 Jun 2023 04:27:28 +0000 X-MS-Exchange-Authentication-Results: spf=temperror (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=temperror action=none header.from=amd.com; Received-SPF: TempError (protection.outlook.com: error in processing during lookup of amd.com: DNS Timeout) Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9D2.mail.protection.outlook.com (10.167.241.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:27:26 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:27:25 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 10/51] x86/fault: Add helper for dumping RMP entries Date: Sun, 11 Jun 2023 23:25:18 -0500 Message-ID: <20230612042559.375660-11-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D2:EE_|PH0PR12MB7840:EE_ X-MS-Office365-Filtering-Correlation-Id: 2ed0c52a-060f-4081-22ae-08db6afd53b4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: sCNboHJeVkb0hpgOeku8Xu7OKeC7TXZ2u1xhdTH7MQ2Y00/p7IcfXeDW/9Fgqr97oUI7x0XkoSGBfot4rJqw2VDQJRb21k4JtpizXwjWM5Mt1YGRXWvTxu6mpso/vTZQdkV7NpHB2IxrWmMieNGKKJ5vNMHbl8gTJfkrERd/tk/ezhWrU2ftgQIWGHHsYPZffmV/LnpUaPnncl1RF7jB3lcVlZ1e0nwkMQ1+ugCWc8JYEtVPLTfiQGISTfzWFFgbm4+Nn6WW9L9YF3yO5x0Hg4Ub7U1iksX56sIpA3lWM2mo3WQ9gljsJM0qgkx2DlPlIcpqNMXn2fAJbAVSxgslTldv4AFjDuHAc7idptaqkpHt5IrnEoQdXYt+iwIcmIyooqCflCgv8m2rbc5TAusCQQ0OO+rax6xUhCJBdiX+YMaCnFjcr5oUGxJ67bwrzg8L4QV/WtuJBQhLqxRgSVC2TLUPq0JuAEC8sovxMSQJglQH57Bh645fEAUaqsCDy85w3XbNwfmN/LfwDXxBv5axiny5KcRONq26ioxPUpfvV9hVtQXsf5u1MTHRS4/zDFhqGDk/qoLJCa/spJKY4yMqYCtmzZzrj+XoBWBj6IoAG/ox7VMlfBVml5B58sj6wbaw0fnI9gPAvtT4J1x6Gb00BGwV2StmCL6Uvohg55w8+YVIK0DfcD10PmH27Em0M+pMGycx3eoEKYibHsy+Q5E8GPkUkO/jG9xpzvwtYrfG4+/pRmWxuucF/n3P5NdJnJv7NK79LS/zNPw6li967hbyYw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(396003)(39860400002)(346002)(136003)(376002)(451199021)(46966006)(36840700001)(40470700004)(40460700003)(6666004)(47076005)(36756003)(63350400001)(426003)(336012)(83380400001)(63370400001)(2616005)(86362001)(82310400005)(82740400003)(81166007)(1076003)(26005)(356005)(40480700001)(36860700001)(186003)(16526019)(2906002)(5660300002)(54906003)(44832011)(8676002)(7406005)(7416002)(8936002)(316002)(41300700001)(6916009)(4326008)(70206006)(70586007)(478600001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:27:26.5235 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2ed0c52a-060f-4081-22ae-08db6afd53b4 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D2.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB7840 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh This information will be useful for debugging things like page faults due to RMP access violations and RMPUPDATE failures. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra [mdr: move helper to standalone patch] Signed-off-by: Michael Roth --- arch/x86/coco/sev/host.c | 43 +++++++++++++++++++++++++++++++++ arch/x86/include/asm/sev-host.h | 2 ++ 2 files changed, 45 insertions(+) diff --git a/arch/x86/coco/sev/host.c b/arch/x86/coco/sev/host.c index 0cc5a6d11b25..d766b3bc6647 100644 --- a/arch/x86/coco/sev/host.c +++ b/arch/x86/coco/sev/host.c @@ -295,3 +295,46 @@ int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) return 0; } EXPORT_SYMBOL_GPL(snp_lookup_rmpentry); + +void sev_dump_rmpentry(u64 pfn) +{ + unsigned long pfn_end; + struct rmpentry e; + int level, ret; + + ret = __snp_lookup_rmpentry(pfn, &e, &level); + if (ret) { + pr_info("Failed to read RMP entry for PFN 0x%llx, error %d\n", pfn, ret); + return; + } + + if (rmpentry_assigned(&e)) { + pr_info("RMPEntry paddr 0x%llx: [high=0x%016llx low=0x%016llx]\n", + pfn << PAGE_SHIFT, e.high, e.low); + return; + } + + /* + * If the RMP entry at the faulting pfn was not assigned, then not sure + * what caused the RMP violation. To get some 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. + */ + pfn = pfn & ~(PTRS_PER_PMD - 1); + pfn_end = pfn + PTRS_PER_PMD; + + while (pfn < pfn_end) { + ret = __snp_lookup_rmpentry(pfn, &e, &level); + if (ret) { + pr_info("Failed to read RMP entry for PFN 0x%llx\n", pfn); + pfn++; + continue; + } + + if (e.low || e.high) + pr_info("RMPEntry paddr 0x%llx: [high=0x%016llx low=0x%016llx]\n", + pfn << PAGE_SHIFT, e.high, e.low); + pfn++; + } +} +EXPORT_SYMBOL_GPL(sev_dump_rmpentry); diff --git a/arch/x86/include/asm/sev-host.h b/arch/x86/include/asm/sev-host.h index 30d47e20081d..85cfe577155c 100644 --- a/arch/x86/include/asm/sev-host.h +++ b/arch/x86/include/asm/sev-host.h @@ -15,8 +15,10 @@ #ifdef CONFIG_KVM_AMD_SEV int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level); +void sev_dump_rmpentry(u64 pfn); #else static inline int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) { return 0; } +static inline void sev_dump_rmpentry(u64 pfn) {} #endif #endif From patchwork Mon Jun 12 04:25:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275447 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BDDF7C7EE23 for ; Mon, 12 Jun 2023 04:29:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234455AbjFLE3H (ORCPT ); Mon, 12 Jun 2023 00:29:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234307AbjFLE2a (ORCPT ); Mon, 12 Jun 2023 00:28:30 -0400 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2040.outbound.protection.outlook.com [40.107.243.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4CD2310FF; Sun, 11 Jun 2023 21:28:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BiN8aGoUKjnn+v6tPniY4KVrcUTrN5gQmlzQqnTifbMNkLRi9jKTMukg7gd0mhMjpSPzbjhfO7CaoqwbnycACOvZ/JSkDLykX76qOYe+7cpuXs0J3JF8mvEwIfzqPKY/1uxWuya+bcrIMfPPLoZtjMtdEGykr6AKw4UCzlWSSNBoY4zzf/gimEXNjHXqRcn2z7a1KTYjOWnHK9Lh5HSc4JTUJWe7YfZixCp3XEshsCRJPRnqLk/yshkI/6siGKntXHhIdilK6S9H8oduhb8eUgKYmiwooMNtv9P/U7GnHk/a7F33riJz7HFdsOxGlZTQcx0mIm629Q5e5VcDgPH4/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=NMB31nd+imPpavDDp2i3hNbkegevF9k3G64/47I11j0=; b=BDrkFr7uObYL01NT3H8A8p5WB610X7EjMMre7+Bhqa3q6RtSn0cGI8oTeuVrfRuFk5lDGF0jHZJwQPYuM47yNskery1sVzgzMDXuxpg6H6QyhbDmTL7CGJJK+5ZKBI5WWy8G8PMtZuYmQLX1yXQFq00jJHNgENtpEjt7oxjcbqTEzV0jTL8C02X3IfPqpvwyJ647Fz1W3yuzjNLIL5Fs4z3Q3Mcs11Va+l8/L6oc8tt3g7teDg9WXaNPcKohi6T3P9i9RfYRXgso1uD/lCWud/faSrl2LnTLsL6VKiLzs0vQwAajhaGWWYJHUUWtyNCGhd2GZDG/c/AJlPVHsuU8Zw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=NMB31nd+imPpavDDp2i3hNbkegevF9k3G64/47I11j0=; b=wqmCPdyThfxb8PAACYTf/ZJxUuvXMoGJJ4cgCVRrxrZtL7eh6uoQwjxY+K2SSxMGhx6yvKDCJtrBSQCWGhz2L+qrFJS/MDuG3ZAYokmVghmlSfthy3hDqdnaRnzWQ8EWhlWHcRd2+CIgK0q22bTPIT3JL6Gcu2UQJtLaWIEAhXw= Received: from CY5PR16CA0012.namprd16.prod.outlook.com (2603:10b6:930:10::20) by DS0PR12MB7560.namprd12.prod.outlook.com (2603:10b6:8:133::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.39; Mon, 12 Jun 2023 04:28:23 +0000 Received: from CY4PEPF0000E9CD.namprd03.prod.outlook.com (2603:10b6:930:10:cafe::98) by CY5PR16CA0012.outlook.office365.com (2603:10b6:930:10::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:28:23 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9CD.mail.protection.outlook.com (10.167.241.140) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:28:22 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:28:18 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 11/51] x86/traps: Define RMP violation #PF error code Date: Sun, 11 Jun 2023 23:25:19 -0500 Message-ID: <20230612042559.375660-12-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9CD:EE_|DS0PR12MB7560:EE_ X-MS-Office365-Filtering-Correlation-Id: e27b4765-fac9-42fc-d1e5-08db6afd7553 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bqCp7dI7Fv/h+/ZknTqMCpt//yb8DFffSDq7a6+PDKu3uLc4SOJ3vmXoEBX20kHv3r+Sr8EsATnXaDGfHP1B4s3UVsmrE2yJhp2/IMo8Ru5CQwvFdKCNF4AiG63cIh9DP87PjvqUo4Mc8kvzt2yscllho9d1JLX9o6FcVrVSHR27N8aSs8WHNioTsxgqe4bFJTlU90dQFe/uVfb88kQ3EZJn3y92Mk99CLOR9pxNPGIYJ+mIPLAwdJdup+ywsbpgAhbDt/97wSkQd4XqtFIi8EH4BnaukBTxTauOf/BvnKdFxn8BJ7e37+Ub5G9S5cjpiTZxXfXPg/L4cRAYS5BUOhy4tvrXM48s1CRYCedkOLKX1tdmGks3IboXRJ4kYsInaitRdrGJf2oD202u2ppMyaJZ7BaHrzq0TFgtiveRspFUG+66aZBzsPwbpN67MjSgUkewKzD/5yTiQj6xfkZ7XVNr2Feckssna7lhWzWtXvhnoLsxntywVvKhc7ieP6HASowb23fXZsu0+WvgGzbnsNjmJuK0apVsde6fOWh5+sLaRt3tQDh+WIsrZNn/1aCdewNaXjIgMh54j5iZgywgypvnFU4XZCjblMi/h3EK3Kb0dKj9y917ZdS/nxhc8NqRwMoE8sSCiOQktK+pm1QTBCm9IVUNyjVOSVw4Lgos44DpDYwT8gbqc7VCgU1z71RQuAj1GYfkM/Y4M6okokkO1hVucLhPfvQrafz8z/WDbQLRCHKLFcnoycthqIE4G+4cN1npeoVlnSFWyKVlhOV0rA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(396003)(376002)(346002)(136003)(39860400002)(451199021)(46966006)(40470700004)(36840700001)(16526019)(186003)(426003)(336012)(2616005)(26005)(1076003)(47076005)(36860700001)(82310400005)(70586007)(36756003)(356005)(70206006)(82740400003)(81166007)(4326008)(478600001)(40460700003)(83380400001)(44832011)(86362001)(5660300002)(8936002)(40480700001)(8676002)(7406005)(54906003)(6916009)(7416002)(316002)(41300700001)(2906002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:28:22.9348 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e27b4765-fac9-42fc-d1e5-08db6afd7553 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9CD.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB7560 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh Bit 31 in the page fault-error bit will be set when processor encounters an RMP violation. While at it, use the BIT_ULL() macro. Signed-off-by: Brijesh Singh Signed-off by: Ashish Kalra Signed-off-by: Michael Roth Acked-by: Dave Hansen --- arch/x86/include/asm/trap_pf.h | 18 +++++++++++------- arch/x86/mm/fault.c | 1 + 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/arch/x86/include/asm/trap_pf.h b/arch/x86/include/asm/trap_pf.h index 10b1de500ab1..295be06f8db7 100644 --- a/arch/x86/include/asm/trap_pf.h +++ b/arch/x86/include/asm/trap_pf.h @@ -2,6 +2,8 @@ #ifndef _ASM_X86_TRAP_PF_H #define _ASM_X86_TRAP_PF_H +#include /* BIT() macro */ + /* * Page fault error code bits: * @@ -12,15 +14,17 @@ * 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 due to RMP violation */ enum x86_pf_error_code { - X86_PF_PROT = 1 << 0, - X86_PF_WRITE = 1 << 1, - X86_PF_USER = 1 << 2, - X86_PF_RSVD = 1 << 3, - X86_PF_INSTR = 1 << 4, - X86_PF_PK = 1 << 5, - X86_PF_SGX = 1 << 15, + X86_PF_PROT = BIT(0), + X86_PF_WRITE = BIT(1), + X86_PF_USER = BIT(2), + X86_PF_RSVD = BIT(3), + X86_PF_INSTR = BIT(4), + X86_PF_PK = BIT(5), + X86_PF_SGX = BIT(15), + X86_PF_RMP = BIT(31), }; #endif /* _ASM_X86_TRAP_PF_H */ diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index a498ae1fbe66..95791071e3cd 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -546,6 +546,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 Mon Jun 12 04:25:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275448 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CDEC3C7EE23 for ; Mon, 12 Jun 2023 04:29:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234307AbjFLE3Z (ORCPT ); Mon, 12 Jun 2023 00:29:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234312AbjFLE2z (ORCPT ); Mon, 12 Jun 2023 00:28:55 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A006610E4; Sun, 11 Jun 2023 21:28:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ctKXp3Rhh+jhEi4kRRRI+iMULRhZj219Og7fQrSvQ4YpY27fgmnKmM1wqngJkDv6atLXothVkz0yiNsaJ8oo/OzGW5pdMY0mHVLnRB9og4P2rqzhPl2L8ys4YFtQc1WaVW9YfXGa67DKL8iax1S7ebLkACCNkVjfwyMikiCGLPTkLvrnAbYnBs1q3s9AhBkRaVgmr1bcgEHRV+b89dSS6+zHtKb8OdlFUJybxiyUf2RwRUVswRnetEiI76wwfh+DQIj6/JXeezJxRkrUb+7ukO8hP0EGelSdpxVSf+DtKJR5Lwh8Ic0ym8uvgMQAhpNZQ5hL4NmMWOyRwWXriwDEdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=jk9yzL0mP2Rv14wKUYtpBKvHMRw9BQjiMQ4iCXVZfbc=; b=Zcyp1GSOR8njiKd74DUV+FhDZ3g+Gzv/Uem3PHBu2x5bXLFHtTg3zcXblc3hQ/PGyxZo838wVlPfdW8aYu91PXLBf4h5CUe3e8w3bKegQ/Nx0nJQIaBkxF64Y3jz9TCUh+RJpy4pA4+UweunotntkI4vU0RhyvIU938X2R2D1HALx4G+ooico4MEIoQ4dZkbvE1FuiE6VUUZ7ZTjFVwx39d9vstbw0e687lPngQ1d94dVFXCHnWEsv7pTc6SVst2luSWd3a94+G9mD9pX5auOsD8WowseG9798MBTE8iDmuHWEJJQbkMgnAusBxa3bkElGXo90kAIXlArL1NFRRFDg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=jk9yzL0mP2Rv14wKUYtpBKvHMRw9BQjiMQ4iCXVZfbc=; b=dS7c9OclY+LS+q9vROLu/KQsyjmE9zu0vC5cNRkcxrvIjnA+Myqp0CvJ6coHDnZlVlCO2m6lDL+fqA7ZXlHEe0L1tGa8L9yZ7pzvvRZB/+NYOfwhftaazyOj5QqFW3hFFuqTfehGlrmFaaJFQelnif3lxQshfHJKmuelydM88Ao= Received: from DM6PR11CA0029.namprd11.prod.outlook.com (2603:10b6:5:190::42) by MW4PR12MB7382.namprd12.prod.outlook.com (2603:10b6:303:22b::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.33; Mon, 12 Jun 2023 04:28:41 +0000 Received: from CY4PEPF0000E9D0.namprd03.prod.outlook.com (2603:10b6:5:190:cafe::f4) by DM6PR11CA0029.outlook.office365.com (2603:10b6:5:190::42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:28:41 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9D0.mail.protection.outlook.com (10.167.241.143) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:28:41 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:28:40 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 12/51] x86/fault: Report RMP page faults for kernel addresses Date: Sun, 11 Jun 2023 23:25:20 -0500 Message-ID: <20230612042559.375660-13-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D0:EE_|MW4PR12MB7382:EE_ X-MS-Office365-Filtering-Correlation-Id: ebbc56db-f55d-489a-5d8f-08db6afd8041 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: dVaVaCSnwHogKXOGyNX3sOMjlPnoUc4F31VjOHowW1uLQaNhgsnmm72zPDfSYO5we62CmLTKkURz727sx73JF5OFEi10FswjAvLJoQiTX0Ck/YJT1bU2InCXubJzqyKNpkvV9NgznC1pfYyleo/eLu18pUI7EJkD7ZkbOMRfLHDKHLDguG92ryGghWhjFctUjl45rA7NVT0cDIsMgXj0Z9FX5VxPg1qlDBOoz2VamFy6rslXKZgxFeuM44Lj5KKIhIHAspxxSLHkEPuOvKtPrxDFV9adNvl/1Aowm7E5woUPSCPSysuILi0b+PAma9wg8uJRh0i3niySO2IilnoKjvn8Y+JVNYqdVdiTEgKyWK4vWwK5mDAM0kaACchF9RVMbexuVYsKVy9P0l5qxrIEFOkP5+Z3Zlba4oeXQ9ZcHqoO5Sb2RKWp9YZ1a88c3oGUl6toYiejKmJDtbnh80G6nBw4cC0i9Y/Kk4QbMTvqGzVAybl41zonmKwb+gn44k1lbsQWW/uUlkQRtuCAl4+ROt0U+ONIUmJ9yYapCA1ULlVkGpHmind/4DAw1/Zo6YMU0IKkGM9WNyl4mmS/spAqEdbSiIsSaVmh+7HrHvBZS75Gs3jWJs3cTCpD+avo8dI49sxeHMOGQhemHLr1AUs4Telu8ImiAdyV9snrpYQjtg1TMeMj0rkzjYRZuRVLtJ1KvDspaXTiqw58IQmxfXhrawjtjNLLHhBZ/CbF5lDQQMXw3BcJyVNUxlSJ5TxGx/DWT8gyUWy/eXF2T1ECjKwT/A== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(396003)(376002)(136003)(39860400002)(346002)(451199021)(36840700001)(40470700004)(46966006)(7406005)(81166007)(7416002)(70206006)(70586007)(6916009)(356005)(4326008)(316002)(41300700001)(8676002)(40460700003)(5660300002)(8936002)(86362001)(36756003)(82310400005)(2906002)(44832011)(40480700001)(16526019)(2616005)(186003)(478600001)(6666004)(1076003)(26005)(47076005)(54906003)(36860700001)(82740400003)(426003)(336012)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:28:41.2532 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ebbc56db-f55d-489a-5d8f-08db6afd8041 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D0.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB7382 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org RMP #PFs on kernel addresses are fatal and should never happen in practice. They indicate a bug in the host kernel somewhere, so dump some information about any RMP entries related to the faulting address to aid with debugging. Signed-off-by: Michael Roth --- arch/x86/mm/fault.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index 95791071e3cd..d46b9cf832b9 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -33,6 +33,7 @@ #include /* kvm_handle_async_pf */ #include /* fixup_vdso_exception() */ #include +#include /* sev_dump_rmpentry() */ #define CREATE_TRACE_POINTS #include @@ -579,6 +580,18 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long ad } dump_pagetable(address); + + if (error_code & X86_PF_RMP) { + unsigned int level; + pgd_t *pgd; + pte_t *pte; + + pgd = __va(read_cr3_pa()); + pgd += pgd_index(address); + pte = lookup_address_in_pgd(pgd, address, &level); + + sev_dump_rmpentry(pte_pfn(*pte)); + } } static noinline void From patchwork Mon Jun 12 04:25:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275449 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8ED0FC7EE23 for ; Mon, 12 Jun 2023 04:30:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234543AbjFLEaX (ORCPT ); Mon, 12 Jun 2023 00:30:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234189AbjFLE36 (ORCPT ); Mon, 12 Jun 2023 00:29:58 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2040.outbound.protection.outlook.com [40.107.237.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89F1D19A6; Sun, 11 Jun 2023 21:29:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QB5gB7E7JZbWL5dBsZrTMV9nkBDPKPIvjmirCxurVZNmEFEVb3vKqLRewFYWroO+iY7crn78wn8NRlWNPCN2ltCTGMqbtW4H5ZtWgOt/a3Ismd8kqHIQOQ83jL56SuvQxKgKYJS4gm9ajJzSJf6SmT2f7HArC/PNtF0A3EIzHYtr3RUZEbLt0kgdVy5SmTqyzPIvtFjezH7eE8DEYYfhM78mcFPk86hOKfS0gvgvpOzet8xmk8Kc8Tgtm3X3fiF50YPhjUKOaWWBq5pjpfucCb4WTmXYIqd95PxH8KFdTdmPs6PsHi/smKY1ACRkLqzRMPb29Oy5WRuM4QISdhtY7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=0d3dQu/aF3mwtDoMAmT2kZoy0ldEPLwmCUnZfhjJTkU=; b=fMKwPt7SBgqPmsvESGp2q35mWSdClXs2Pj/+CDpFCSGzeLN+XqJgeQYkMf4p1pYlAaqNWlGFNs8cX+vz3vO4H8jXy9oJmsIQvYKHhXvniAUufuhtGdldcOkW6IfQzPVmarQmR4tr0VyVG+zOVPKOteTu74V0DxufoMDELCCLndi4rmVqnGb1H/fjHTyW5ZUwPi/4crSuGOQm1p0qOnRrkDpawEhcC1Ayx44rZr8akl8XHsJ8irqMFK7CY9m7mGYTyzzSI++wk5IAEwsvDpZ/I3lLiLI2pzZr8SCDWTFIRm8kSfF2lpFZUZjbpJ8EINsjnNHOfaLd12lS0Y1VdCd0HA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=0d3dQu/aF3mwtDoMAmT2kZoy0ldEPLwmCUnZfhjJTkU=; b=z5K4dPyKhy/4yFKD2LCylE3xg5Fmxyx3XXi8TKox4EGKWgVYu/Jdfu+ZNV1lBlkL8L3sPk7HmM1NdypyZg0pTQPT4ml47uFF/qUoHWiR6ITJ34SHyfxpx6RsohniHD6PJkhmxVmfK3t3KtHPbcwkN9WFtCHhfQq27gpvWChu06w= Received: from DS7PR05CA0059.namprd05.prod.outlook.com (2603:10b6:8:2f::32) by PH7PR12MB8828.namprd12.prod.outlook.com (2603:10b6:510:26b::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.41; Mon, 12 Jun 2023 04:29:36 +0000 Received: from CY4PEPF0000E9D4.namprd03.prod.outlook.com (2603:10b6:8:2f:cafe::2d) by DS7PR05CA0059.outlook.office365.com (2603:10b6:8:2f::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:29:36 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9D4.mail.protection.outlook.com (10.167.241.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:29:36 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:29:35 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Jarkko Sakkinen Subject: [PATCH RFC v9 13/51] x86/fault: Handle RMP page faults for user addresses Date: Sun, 11 Jun 2023 23:25:21 -0500 Message-ID: <20230612042559.375660-14-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D4:EE_|PH7PR12MB8828:EE_ X-MS-Office365-Filtering-Correlation-Id: 4c459862-4178-4799-b1b5-08db6afda10b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: yqxNPlB2qGzRvl/LTI4VOTNiZ5WbycA91BY4M8iNOfGbc1bZ0/bpqyP7WQ/Me002DIp90WL/wGSSaFiBUoRC+O8FzjP1i/rXZW2oprDkBDN5qydg8/fzjdjpL39dtykKYfZ+pKfDh41EGnIgrerTLYhc+3ZY0bzCYqBWYM/Oq+EzKPyQKi92qG/qwAf2FG2LPWicnWJ7sSgOnPk1UMa2C7tFfEaUwZUcscl2rYLDxljx3IRr7RPM9hQnrIFQqwCc58BCQqG1MGoUBUtmO5oV8NrV3IKhflcYYoUIyXIDcyEAfGMdjrX0wnbZ79YV0gqhN08qX67sbWGgbtnWnrJ2F6fHpVwTC2tYIliphRZYifK4eTe5RM8d2sC21BEcQQh5dvqz3EAOJzOOlUanw2mb4PdPXVbAC34bfJHs4ohkKR0ysYFT/wZgXIxG93EYfv9/NI2w7h9/1djsz1t18x1tNyQY0qgyw0BnxvvYZQfalVSqfczrGpCLAddKk/yJnTKFyq/ToysBOUqsscoHMyp+YWYCj42zH+xQ+Sx+pFmRuveFAkZmm0ffc1yY3HPhOldbFQQq5em28TV4a5YOdN4LF7MSWM+YCMQX17mBfUpzwSPGs9ba2YRN//IOBQipFPddC/hoyrM7vRLxyxjniVE/Li1QfJceJAjkczRvfNPfzloAq+ADZezooKbeofM6k584u9TnbYlAF9VrY9WnNzMJONCwQyMTiz2jUTma9WuAwkGPDq9TxAXivmWGQxRSlVhA2X1dALiEa7PFFy1xGy5fWQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(346002)(396003)(376002)(39860400002)(136003)(451199021)(40470700004)(36840700001)(46966006)(316002)(40460700003)(41300700001)(336012)(82310400005)(40480700001)(16526019)(186003)(26005)(44832011)(7416002)(47076005)(86362001)(7406005)(36860700001)(2616005)(2906002)(83380400001)(1076003)(81166007)(82740400003)(356005)(426003)(36756003)(8936002)(5660300002)(8676002)(70206006)(70586007)(54906003)(478600001)(6666004)(4326008)(6916009)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:29:36.2818 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4c459862-4178-4799-b1b5-08db6afda10b X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D4.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB8828 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh When SEV-SNP is enabled globally, a write from the host is subject to checks performed by the hardware against the RMP table (APM2 15.36.10) at the end of a page walk: 1. Assigned bit in the RMP table is not set (i.e page is shared). 2. Immutable bit in the RMP table is not set. 3. 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. Nothing constructive can come of an attempt by userspace to violate case 1) (which will result in writing garbage due to page encryption) or case 2) (userspace should not ever need or be allowed to write to a page that the host has specifically needed to mark immutable). Case 3) is dependent on the hypervisor. In case of KVM, due to how shared/private pages are partitioned into separate memory pools via restricted/guarded memory, there should never be a case where a page in the private pool overlaps with a shared page: either it is a hugepage-sized allocation and all the sub-pages are private, or it is a single-page allocation, in which case it cannot overlap with anything but itself. Therefore, for all 3 cases, it is appropriate to simply kill the userspace process if it ever generates an RMP #PF. Implement that logic here. Co-developed-by: Jarkko Sakkinen Signed-off-by: Jarkko Sakkinen Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Signed-off-by: Brijesh Singh [mdr: drop all previous page-splitting logic since it is no longer needed with restricted/guarded memory, update commit message accordingly] Signed-off-by: Michael Roth --- arch/x86/mm/fault.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index d46b9cf832b9..6465bff9d1ba 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -1329,6 +1329,13 @@ void do_user_addr_fault(struct pt_regs *regs, if (error_code & X86_PF_INSTR) flags |= FAULT_FLAG_INSTRUCTION; + if (error_code & X86_PF_RMP) { + pr_err("Unexpected RMP page fault for address 0x%lx, terminating process\n", + address); + do_sigbus(regs, error_code, address, VM_FAULT_SIGBUS); + return; + } + #ifdef CONFIG_X86_64 /* * Faults in the vsyscall page might need emulation. The From patchwork Mon Jun 12 04:25:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275465 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4816EC7EE23 for ; Mon, 12 Jun 2023 04:30:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233961AbjFLEao (ORCPT ); Mon, 12 Jun 2023 00:30:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234513AbjFLEaQ (ORCPT ); Mon, 12 Jun 2023 00:30:16 -0400 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2040.outbound.protection.outlook.com [40.107.92.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C0A01B7; Sun, 11 Jun 2023 21:30:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ON74fhmb2Z1ghvMvCk83tRfulyUCwUfEX0mIiLRJ5HOrELUXkpCmstlNptzUiuMaPizjCqDnWzHDm13IV14gEhlNr3p1gG067KsT6+hEmEN2Cnj00d7h3pJEXoPS0/O5tlFD0i54M9kJ3YKR3QISbLVyX/voxb8C0kBc61qQ7jwQL3hBWNobnRgROclpLedspwh0EP8IrEaRWa8rWnO/enZAx/vOiLptA/fHbbQg8X0is9Z+XMJg0U9Jfokq5e67R2vag9nTTF6tFnJ40G20AYRj25nWefSieb1KHsojHpmXQTEtoyAF8syTXerIxk/GD+05acuCoVysGik7x2bxYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=vFANIoBEvW5rShPu60LwTEesSM4XP/tF5PsDV4uYZYY=; b=G2rOHByURF1yvDTl6L85QhtZcek4j0tbbVmTvcz+lkabi+f31m1VMe7FAM7bANfy5qYi7H44ENWy0WvE2fPtVHPU9rMhj49upkgH9B3WdeQi0gi0yvaju8Gf3dCC8G35oEVcpJd/4f8zLSeOhgJ3purtmh4t7eD57wmX/0fk2M+rYOGn10aef+zF3veYHBHJAvFmvHy/R83mSNSvR7Mu8N/5efpqWm1jdXmEfvZtFuyh145JwHNM0q2j42CBpi7vGhzJX76Wm7yR0cRx7H3ictIOr+Djj8bAwfYvmUglVHWaCs2MYASk97dwqTAlydMOxt6j74y5kKaVfyTATMrtLw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=vFANIoBEvW5rShPu60LwTEesSM4XP/tF5PsDV4uYZYY=; b=5czQLo7vQZY+/u/TtvChvGoD5d8M+8RiSVaUvU60SCNKA35fwdaExNjiUHBbJBrO2zQcu5j9XFET0KidVLGfxmJm0UlQe7P0YZp83q5NG9iBL4ebavXBSMyV1wlxnQAFwh5gYzyeYpDWWo5QjgaZLzklivau4EGMKH8Pw0UTcG8= Received: from CY5PR16CA0016.namprd16.prod.outlook.com (2603:10b6:930:10::26) by DM6PR12MB4435.namprd12.prod.outlook.com (2603:10b6:5:2a6::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.38; Mon, 12 Jun 2023 04:29:58 +0000 Received: from CY4PEPF0000E9CD.namprd03.prod.outlook.com (2603:10b6:930:10:cafe::dd) by CY5PR16CA0016.outlook.office365.com (2603:10b6:930:10::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:29:58 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9CD.mail.protection.outlook.com (10.167.241.140) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:29:58 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:29:57 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 14/51] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction Date: Sun, 11 Jun 2023 23:25:22 -0500 Message-ID: <20230612042559.375660-15-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9CD:EE_|DM6PR12MB4435:EE_ X-MS-Office365-Filtering-Correlation-Id: 39ba6556-3c53-4fa1-dc99-08db6afdae0d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 04MwOKBMy0hHqW72uwGFCIQAR5VZ0XGfGh13jB0YlT/VVFeD5TBEw1iJJVQCM81pyaGS2kKKNx2Bwct827AM6ESe513J7QhQBRxY38+3R1qZVDVqCOtq/qDPNbIfafwB9zhPLP/gn8rMQzOL46tpSMuvV1gVsp0IktEoIsg1Djfx7bo63PW7Z2E02znn+yMCVCnybCs1i3PQuszH7RPpFqk42flhhK+cs9jjMhpgLAxFcckSmW5nuVxLnlxHP0xke0TF6FUWzRTMb1qsi5uBcQPPKj8hvxL38aBlg8RF1P1p3SdwvtzHl6gVQKrx5NXD72YTvu6zOoUc16mQF4l+ibznEYYn1VHHBtRAdYrdRM2mC6qQ7EtSwlUW1QQwzFB4ozacij4TSDb0HsnmGKS2glp5EXfQVM0qEkdBJ79BqgJ3hc7pLjxNmAGUM7QrCTkd1X7V21hX5xEz09rXNn9s9kmgu+AdBVPxToHoOM30umP4vTeEBm8SaOF0zdatKN+2pWEgwm5uVR4bc7iXKAPPg2zjEw15aUaZU2X98NEjmWt6RKZyNW9YTyA/hDg/dNZMWDXAWgyt6x1+1DsYsIGzudux81NLzmpsLugb81r5SKahHa8PsBrSXUYZ2lJa8hEgiK+91SaA/tyWd2BUkvB0tPnLl2lH4DR8q1ACy5aYTjLrVJkIeXT8VdEbh6LGVqw0xkX1k35wHWOogPcTEBRsq2R4IePsLc2yDej7J0gssohqozPqngsQjIf9BtSok7cj9jivkRY5shtvcXnSOrmfBA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(396003)(136003)(376002)(39860400002)(346002)(451199021)(40470700004)(46966006)(36840700001)(5660300002)(7416002)(8936002)(8676002)(2906002)(70206006)(70586007)(7406005)(4326008)(54906003)(44832011)(6666004)(1076003)(26005)(316002)(6916009)(41300700001)(186003)(16526019)(36860700001)(356005)(82740400003)(426003)(336012)(47076005)(83380400001)(2616005)(40460700003)(478600001)(40480700001)(82310400005)(36756003)(86362001)(81166007)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:29:58.1065 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 39ba6556-3c53-4fa1-dc99-08db6afdae0d X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9CD.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4435 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh 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. Add the following external interface API functions: psmash(): Used to smash a 2MB aligned page into 4K pages while preserving the Validated bit in the RMP. rmp_make_private(): Used to assign a page to guest using the RMPUPDATE instruction. rmp_make_shared(): Used to transition a page to hypervisor/shared state using the RMPUPDATE instruction. Signed-off-by: Ashish Kalra Signed-off-by: Brijesh Singh [mdr: add RMPUPDATE retry logic for transient FAIL_OVERLAP errors] Signed-off-by: Michael Roth --- arch/x86/coco/sev/host.c | 94 +++++++++++++++++++++++++++++++ arch/x86/include/asm/sev-common.h | 14 +++++ arch/x86/include/asm/sev-host.h | 10 ++++ 3 files changed, 118 insertions(+) diff --git a/arch/x86/coco/sev/host.c b/arch/x86/coco/sev/host.c index d766b3bc6647..9df690b0b263 100644 --- a/arch/x86/coco/sev/host.c +++ b/arch/x86/coco/sev/host.c @@ -338,3 +338,97 @@ void sev_dump_rmpentry(u64 pfn) } } EXPORT_SYMBOL_GPL(sev_dump_rmpentry); + +/* + * PSMASH a 2MB aligned page into 4K pages in the RMP table while preserving the + * Validated bit. + */ +int psmash(u64 pfn) +{ + unsigned long paddr = pfn << PAGE_SHIFT; + int ret; + + pr_debug("%s: PFN: 0x%llx\n", __func__, pfn); + + if (!pfn_valid(pfn)) + return -EINVAL; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return -ENXIO; + + /* Binutils version 2.36 supports the PSMASH mnemonic. */ + asm volatile(".byte 0xF3, 0x0F, 0x01, 0xFF" + : "=a"(ret) + : "a"(paddr) + : "memory", "cc"); + + return ret; +} +EXPORT_SYMBOL_GPL(psmash); + +static int rmpupdate(u64 pfn, struct rmp_state *val) +{ + unsigned long paddr = pfn << PAGE_SHIFT; + int ret, level, npages; + int attempts = 0; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return -ENXIO; + + do { + /* Binutils version 2.36 supports the RMPUPDATE mnemonic. */ + asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFE" + : "=a"(ret) + : "a"(paddr), "c"((unsigned long)val) + : "memory", "cc"); + + attempts++; + } while (ret == RMPUPDATE_FAIL_OVERLAP); + + if (ret) { + pr_err("RMPUPDATE failed after %d attempts, ret: %d, pfn: %llx, npages: %d, level: %d\n", + attempts, ret, pfn, npages, level); + sev_dump_rmpentry(pfn); + dump_stack(); + return -EFAULT; + } + + return 0; +} + +/* + * Assign a page to guest using the RMPUPDATE instruction. + */ +int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asid, bool immutable) +{ + struct rmp_state val; + + pr_debug("%s: GPA: 0x%llx, PFN: 0x%llx, level: %d, immutable: %d\n", + __func__, gpa, pfn, level, immutable); + + memset(&val, 0, sizeof(val)); + val.assigned = 1; + val.asid = asid; + val.immutable = immutable; + val.gpa = gpa; + val.pagesize = X86_TO_RMP_PG_LEVEL(level); + + return rmpupdate(pfn, &val); +} +EXPORT_SYMBOL_GPL(rmp_make_private); + +/* + * Transition a page to hypervisor/shared state using the RMPUPDATE instruction. + */ +int rmp_make_shared(u64 pfn, enum pg_level level) +{ + struct rmp_state val; + + pr_debug("%s: PFN: 0x%llx, level: %d\n", __func__, pfn, level); + + memset(&val, 0, sizeof(val)); + val.pagesize = X86_TO_RMP_PG_LEVEL(level); + + return rmpupdate(pfn, &val); +} +EXPORT_SYMBOL_GPL(rmp_make_shared); diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index bf0378136289..9eb20b416251 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -171,8 +171,22 @@ struct snp_psc_desc { #define GHCB_ERR_INVALID_INPUT 5 #define GHCB_ERR_INVALID_EVENT 6 +/* RMUPDATE detected 4K page and 2MB page overlap. */ +#define RMPUPDATE_FAIL_OVERLAP 4 + /* RMP page size */ #define RMP_PG_SIZE_4K 0 +#define RMP_PG_SIZE_2M 1 #define RMP_TO_X86_PG_LEVEL(level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M) +#define X86_TO_RMP_PG_LEVEL(level) (((level) == PG_LEVEL_4K) ? RMP_PG_SIZE_4K : RMP_PG_SIZE_2M) + +struct rmp_state { + u64 gpa; + u8 assigned; + u8 pagesize; + u8 immutable; + u8 rsvd; + u32 asid; +} __packed; #endif diff --git a/arch/x86/include/asm/sev-host.h b/arch/x86/include/asm/sev-host.h index 85cfe577155c..753e80d16433 100644 --- a/arch/x86/include/asm/sev-host.h +++ b/arch/x86/include/asm/sev-host.h @@ -16,9 +16,19 @@ #ifdef CONFIG_KVM_AMD_SEV int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level); void sev_dump_rmpentry(u64 pfn); +int psmash(u64 pfn); +int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asid, bool immutable); +int rmp_make_shared(u64 pfn, enum pg_level level); #else static inline int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) { return 0; } static inline void sev_dump_rmpentry(u64 pfn) {} +static inline int psmash(u64 pfn) { return -ENXIO; } +static inline int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asid, + bool immutable) +{ + return -ENODEV; +} +static inline int rmp_make_shared(u64 pfn, enum pg_level level) { return -ENODEV; } #endif #endif From patchwork Mon Jun 12 04:25:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275466 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DE04FC7EE23 for ; Mon, 12 Jun 2023 04:31:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234453AbjFLEbb (ORCPT ); Mon, 12 Jun 2023 00:31:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234562AbjFLEan (ORCPT ); Mon, 12 Jun 2023 00:30:43 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2043.outbound.protection.outlook.com [40.107.95.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D82D1710; Sun, 11 Jun 2023 21:30:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UXD41z/c3O0mYE3WzOZNFDq7WyLwQv+14HXxsr02H0yiqvvDdRTUB1Mi/8sqzcZ8ju30iyGb4sDrGLm2tQ3X/lF6Z/r8vuyIYWhXMnNQHyUDdNQogWhIrKYxOT5hDwmnG3JQh9IfKr5Cy4IGUtMiutMnbvrKRJlPlOSlqcHRmAMVrVzDiNja4RJ8yLLUN4KpesAAGYCclxySXYxgbFDKOpdXT2kxaOzCATqZOGY6zTsjJ+1QeRdCxu6wyG9Y2BqSK4pAPlZ1vrCfzFKnkDUT9kdbkCRA2F9t2e8106ayZZiIYEf6YHhCj33gCwR3eOkPhRkSE7LpOZYSXSNWJY8ZKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=7D8NSAImjaylny2njAump970VGru0JZBPQv/dh1rn8E=; b=ZX1hh/fhboNKRs5TqYY1dGwwusAdYsdWX5WovPrONiKJ13sS5GVEPxCzDaO2n+3x1PQ84+3A6f/1WbqHCVgIBk7v67qIB32ivv2LwuXtpb9g4cVrCIAVjwJkHipK+O2tX2YnIx9P3rM5YLdrALD/691INo+Z3fBVjNEtpSSizv2o1rVD49X3xczquSj/KO1CKlzbrhr4Jku0RL5NdD0g2iO2Hvj4oDpbA9ra+P8A3sezIrwcO+5Erp8UROThR+secv5MIe8NzyJw7JV8RDOiYVctlichb8lo9/A0pgdQSTQHOS8gTViC4R0FOu22YBOAGdycY+e2bkVXZu0kj9ZIgQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=7D8NSAImjaylny2njAump970VGru0JZBPQv/dh1rn8E=; b=JoK9PkOVJ0xdPfgjKlQfz78T3IuWWYDntxT7O46V8Vvy3xsgJjFtlJ+07miyFT2nu7QDKTxBnS4k52DfxQghDowl1siPHzGu9/FckngwkZ+DYNFozEJuMVfDviuN1bmloX7jwp9+QDiVdRtKbFEnB50QQTXlGyKT2POxh4AByRU= Received: from DS7PR05CA0057.namprd05.prod.outlook.com (2603:10b6:8:2f::21) by BL1PR12MB5111.namprd12.prod.outlook.com (2603:10b6:208:31b::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.38; Mon, 12 Jun 2023 04:30:37 +0000 Received: from CY4PEPF0000E9D4.namprd03.prod.outlook.com (2603:10b6:8:2f:cafe::6e) by DS7PR05CA0057.outlook.office365.com (2603:10b6:8:2f::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:30:36 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9D4.mail.protection.outlook.com (10.167.241.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:30:36 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:30:33 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 15/51] x86/sev: Invalidate pages from the direct map when adding them to the RMP table Date: Sun, 11 Jun 2023 23:25:23 -0500 Message-ID: <20230612042559.375660-16-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D4:EE_|BL1PR12MB5111:EE_ X-MS-Office365-Filtering-Correlation-Id: 37be58db-3847-4dd0-0f9b-08db6afdc4cf X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YVIwqOiaBzXW7N8vmS8g4n7UbapTkP9kys5RWUQR8eX2eA1zFtJ2DUr2Z0JEWW2GpI/4BtsHGyDxcunMADsodtx3ScrOtRiZxyHA4T1jzfgCptWlbuiLHhu30vfy0qdFbUCylMI808UkJ7sF5KFfNqqHJ0JS/bLpTGLIC2RgArSAI2Ozwk1mtmGgeIwya/VysDVSzgUGz8vvShV5WYEZ4PDSpJG9iux9Sh50eLX0deIe7qsJVNcth8FpHErD58gGExlMHO3VqJ1KhNLq4ahgBmYzYBPUVaHSBW3k1/0lwocYU/sYAtSbHxMdY8fu3GTf3Ri3Dq5j+CpcmTajO8jvQUduZj7D2k+DfZ2Yl3CN+UhkLHNpTQO49Gkw7vQcvsRmTZqadcgGxptFsrwBG9ZIIqpUsPe6I12x3THqVZCx9tDO3YC1JC22jTWZDZBcIW7OB1tOyz2iUiV3zOJ4N3qnZfiy+9NXKu4+VJ/f5ClEwnWc+t7dOba8ygs0xHF8CCApS9Z6xg0sMg+QJrBdEMJbcBb+r/4Bo0NuiGjRp0odrEXxrSe+9MGbmlMIMb32V/wXz5SNKlgW2nwILz++PJ2O8t5LQlkIE8Ow2M+DDjF026kR+3ZWWSQuxBllkmea55o9VdOd/a2DU0v8G3+T7/ZDm2OU1TopHnRe8A3F+iRtTF/+qlKsUdlmVLyQou5oWAfdz3j7A+1/A66afcjymifskBT29HnXBeyiLodoPAo/3i74d4QoHaadWrjCt+2LplDA7cnc9n9UgQZN4Dv5EjZlGw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(396003)(376002)(39860400002)(346002)(136003)(451199021)(40470700004)(36840700001)(46966006)(5660300002)(7416002)(8936002)(8676002)(2906002)(70206006)(70586007)(7406005)(54906003)(44832011)(6666004)(4326008)(1076003)(26005)(316002)(6916009)(41300700001)(186003)(16526019)(36860700001)(356005)(82740400003)(336012)(426003)(47076005)(83380400001)(2616005)(40460700003)(478600001)(40480700001)(36756003)(86362001)(82310400005)(81166007)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:30:36.2668 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 37be58db-3847-4dd0-0f9b-08db6afdc4cf X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D4.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5111 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh The integrity guarantee of SEV-SNP is enforced through the RMP table. The RMP is used with standard x86 and IOMMU page tables to enforce memory restrictions and page access rights. The RMP check is enforced as soon as SEV-SNP is enabled globally in the system. When hardware encounters an RMP-check failure, it raises a page-fault exception. The rmp_make_private() and rmp_make_shared() helpers are used to add or remove the pages from the RMP table. Improve the rmp_make_private() to invalidate state so that pages cannot be used in the direct-map after they are added the RMP table, and restored to their default valid permission after the pages are removed from the RMP table. Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Signed-off-by: Brijesh Singh Signed-off-by: Michael Roth --- arch/x86/coco/sev/host.c | 62 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/arch/x86/coco/sev/host.c b/arch/x86/coco/sev/host.c index 9df690b0b263..cd3b4c6a25bc 100644 --- a/arch/x86/coco/sev/host.c +++ b/arch/x86/coco/sev/host.c @@ -366,6 +366,42 @@ int psmash(u64 pfn) } EXPORT_SYMBOL_GPL(psmash); +static int restore_direct_map(u64 pfn, int npages) +{ + int i, ret = 0; + + for (i = 0; i < npages; i++) { + ret = set_direct_map_default_noflush(pfn_to_page(pfn + i)); + if (ret) + break; + } + + if (ret) + pr_warn("Failed to restore direct map for pfn 0x%llx, ret: %d\n", + pfn + i, ret); + + return ret; +} + +static int invalidate_direct_map(u64 pfn, int npages) +{ + int i, ret = 0; + + for (i = 0; i < npages; i++) { + ret = set_direct_map_invalid_noflush(pfn_to_page(pfn + i)); + if (ret) + break; + } + + if (ret) { + pr_warn("Failed to invalidate direct map for pfn 0x%llx, ret: %d\n", + pfn + i, ret); + restore_direct_map(pfn, i); + } + + return ret; +} + static int rmpupdate(u64 pfn, struct rmp_state *val) { unsigned long paddr = pfn << PAGE_SHIFT; @@ -375,6 +411,21 @@ static int rmpupdate(u64 pfn, struct rmp_state *val) if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) return -ENXIO; + level = RMP_TO_X86_PG_LEVEL(val->pagesize); + npages = page_level_size(level) / PAGE_SIZE; + + /* + * If page is getting assigned in the RMP table then unmap it from the + * direct map. + */ + if (val->assigned) { + if (invalidate_direct_map(pfn, npages)) { + pr_err("Failed to unmap %d pages at pfn 0x%llx from the direct_map\n", + npages, pfn); + return -EFAULT; + } + } + do { /* Binutils version 2.36 supports the RMPUPDATE mnemonic. */ asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFE" @@ -393,6 +444,17 @@ static int rmpupdate(u64 pfn, struct rmp_state *val) return -EFAULT; } + /* + * Restore the direct map after the page is removed from the RMP table. + */ + if (!val->assigned) { + if (restore_direct_map(pfn, npages)) { + pr_err("Failed to map %d pages at pfn 0x%llx into the direct_map\n", + npages, pfn); + return -EFAULT; + } + } + return 0; } From patchwork Mon Jun 12 04:25:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275467 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 76B1FC7EE25 for ; Mon, 12 Jun 2023 04:32:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233584AbjFLEcG (ORCPT ); Mon, 12 Jun 2023 00:32:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232185AbjFLEbt (ORCPT ); Mon, 12 Jun 2023 00:31:49 -0400 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2058.outbound.protection.outlook.com [40.107.101.58]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C6981BDF; Sun, 11 Jun 2023 21:31:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=H967fgcnWDfI8f1DAABvenBz5Yfdm+n/GDCDu2fm7tZhV8d+STOHSLDbPsOlnZOo4nOwYX3fBeT5CePouZ9ggI9hPSnQ2LxTcOEk6+x61mKiIK9QCtgAo5+3SnFVWRuYGgIPch0UVfXwRk1tIgQKADy2/qKQHz3pJQ815M/TFMA2cqJcvIu0uW4RJWjZA8qqiXLXvtt59sBUpZUQBABn3M+JdVac3OZZqHhQDVuTWeeRcdz9sV9VgWUHsdbl+IgVr9r2ahG+zbdsmQYyMUnFnXmhPb8mzEUOy28lgB7H/wGZnYgIFJ/fqhiPAOm5MMBCbGGHIEvm6ePEURsQ37ZiaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=2Zsc6mV3GrvXD9w3AgjvVW3vHnvwWqzD9ghwmSVayr4=; b=grKXx+OOh7xGgaYfkGq6vChAsHOg/KH2r6OTTxZTfMymU8XggV+fclKCqe3o8+VDAZwEeT6rdXq6a1DI6lWA1SoNZNz0ZyZrj695q2Miox9Gy3h3aIsls7Zb/7K5j6hduJTg9S2SnyRqVnDG89CuUqNHnqAd1h5G/Y4beSSbjNghD6kE2BJ74yUIEXLM5MxO+/sKBfQfdM9w4mrVzjlpKdL1uJCs4An8iEVDY8/d/U/peTpHKwpUKNZo5yv0zrr2Yas1Cm/fqGwnCEfjo2vgbkYB28QRRvb8Qg8hMBUEhk31QLL9ca4Xf0dVP1qgWsC95I83/8oYy43cu6woBvuwYA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=2Zsc6mV3GrvXD9w3AgjvVW3vHnvwWqzD9ghwmSVayr4=; b=lH/2OwSTOzmy7Jxd1ZRgMVF+OTA0tXuCn+c1Xiy451c4BCuUi0Vg3vKs8GqjgH/F1kL/vK8Q3sy62x7m6Lo9uYT6cScKN87qawWgunHKbLChwqX1LhTQQmwZIMcLh1d79ml/imtNT+pAcjTBtGjPJYddn7cqgJLOG7eqcJw5krc= Received: from CY5PR19CA0115.namprd19.prod.outlook.com (2603:10b6:930:64::23) by CH0PR12MB8532.namprd12.prod.outlook.com (2603:10b6:610:191::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.29; Mon, 12 Jun 2023 04:31:04 +0000 Received: from CY4PEPF0000E9D1.namprd03.prod.outlook.com (2603:10b6:930:64:cafe::e5) by CY5PR19CA0115.outlook.office365.com (2603:10b6:930:64::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:31:03 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9D1.mail.protection.outlook.com (10.167.241.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:31:03 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:31:01 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 16/51] crypto: ccp: Define the SEV-SNP commands Date: Sun, 11 Jun 2023 23:25:24 -0500 Message-ID: <20230612042559.375660-17-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D1:EE_|CH0PR12MB8532:EE_ X-MS-Office365-Filtering-Correlation-Id: bdb54a42-0649-427a-20d3-08db6afdd516 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2qUrvsZ7WQdvcw7jNRRQ1QvZlFnsVj0Ngig1WJFmkuxmkrMzaBfAjhF+qmuBoiXOAwXExGsx/+142TFDJMY1fYB/ai3oiAeTz+T+jT+4uVIQQsvOR/J/ujKZ7nSiursecQFIibzPk5pmFRLYpJZsRBChA0I4K+I8FCuePNxOUtQyvlPVekN0tuttkWGD/F8lPNvBj62iQgu9hloH2VUa1gX0WQA3gRYir/dd35yxkOZ3B31uufMSs69+CWMO1dZ2kthKUEC4DRoWrX53kNc4uTDk+Ed6LdyVZlw/oxT9eAuxx56zgI2Qr9Z+NmxYgF/UcZbXDrbSKUD4chsIcwV/qDuWaev9dTiGfOllD71grNZjFtpPfk6gSzsQbehFkXeZZIYtkpq/p1JfqeUr9+mdhU4mAi6MYtRvvr6aiELP4wYBXmBx1l621WGgcaMZLtTzIw/jaPBOPttf9aScQB5pKIw5DtNfEjAosuQNdWjBAkc2qIws6XlqbPrfEe2WwxGCo+SjosmIzRAjotsaP5FKsOl3fZ5Yz28sqDHtzB6u1hTdOYrCQxwgfEbiXtsMVMLk7DT1eJB7vM96wabW3UtiaqlAp6mfijoKr2XAMKlr6SosQtPojauJyeQBMV0EUfApz6kmZ0RJHoP4U1CzzggvKIgRz6kssze0a2iP1x2lf/tICvrYAdvZ5hIiGtVOVCS06emKAbbrIhQdrDrIpKuaZXikdi3te/N51qTZZxDwuykUElLMvXyaV2KJN9DZ5p6jcRX4/AwFBLKdbE+WjMjLAw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(136003)(396003)(376002)(346002)(451199021)(36840700001)(46966006)(40470700004)(336012)(2616005)(47076005)(83380400001)(426003)(36860700001)(82740400003)(356005)(40480700001)(86362001)(82310400005)(81166007)(36756003)(40460700003)(478600001)(44832011)(54906003)(4326008)(6666004)(84970400001)(8936002)(8676002)(30864003)(2906002)(5660300002)(7416002)(7406005)(70206006)(70586007)(6916009)(41300700001)(316002)(16526019)(186003)(1076003)(26005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:31:03.6074 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: bdb54a42-0649-427a-20d3-08db6afdd516 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D1.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB8532 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh 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. Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Signed-off-by: Brijesh Singh [mdr: update SNP command list and SNP status struct based on current spec] Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 16 +++ include/linux/psp-sev.h | 246 +++++++++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 53 ++++++++ 3 files changed, 315 insertions(+) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index e2f25926eb51..ab3572286755 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -128,6 +128,8 @@ static int sev_cmd_buffer_len(int cmd) switch (cmd) { case SEV_CMD_INIT: return sizeof(struct sev_data_init); case SEV_CMD_INIT_EX: return sizeof(struct sev_data_init_ex); + case SEV_CMD_SNP_SHUTDOWN_EX: return sizeof(struct sev_data_snp_shutdown_ex); + case SEV_CMD_SNP_INIT_EX: return sizeof(struct sev_data_snp_init_ex); case SEV_CMD_PLATFORM_STATUS: return sizeof(struct sev_user_data_status); case SEV_CMD_PEK_CSR: return sizeof(struct sev_data_pek_csr); case SEV_CMD_PEK_CERT_IMPORT: return sizeof(struct sev_data_pek_cert_import); @@ -156,6 +158,20 @@ static int sev_cmd_buffer_len(int cmd) case SEV_CMD_GET_ID: return sizeof(struct sev_data_get_id); case SEV_CMD_ATTESTATION_REPORT: return sizeof(struct sev_data_attestation_report); case SEV_CMD_SEND_CANCEL: return sizeof(struct sev_data_send_cancel); + case SEV_CMD_SNP_GCTX_CREATE: return sizeof(struct sev_data_snp_addr); + 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_addr); + 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_DBG_DECRYPT: return sizeof(struct sev_data_snp_dbg); + case SEV_CMD_SNP_DBG_ENCRYPT: return sizeof(struct sev_data_snp_dbg); + 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_addr); + case SEV_CMD_SNP_GUEST_REQUEST: return sizeof(struct sev_data_snp_guest_request); + case SEV_CMD_SNP_CONFIG: return sizeof(struct sev_user_data_snp_config); default: return 0; } diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 1595088c428b..06d0619ca442 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -86,6 +86,36 @@ 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_INIT_EX = 0x85, + SEV_CMD_SNP_SHUTDOWN_EX = 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_ENCRYPT = 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_SET_STATE = 0xC6, + SEV_CMD_SNP_PAGE_RECLAIM = 0xC7, + SEV_CMD_SNP_PAGE_UNSMASH = 0xC8, + SEV_CMD_SNP_CONFIG = 0xC9, + SEV_CMD_SNP_DOWNLOAD_FIRMWARE_EX = 0xCA, + SEV_CMD_SNP_COMMIT = 0xCB, + SEV_CMD_SNP_VLEK_LOAD = 0xCD, + SEV_CMD_MAX, }; @@ -531,6 +561,222 @@ struct sev_data_attestation_report { u32 len; /* In/Out */ } __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_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_addr - generic SNP command params + * + * @address: system physical address guest context page + */ +struct sev_data_snp_addr { + 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; + u8 gosvw[16]; /* In */ +} __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 + * @vmpl1_perms: VMPL permission mask for VMPL1 + * @vmpl2_perms: VMPL permission mask for VMPL2 + * @vmpl3_perms: VMPL permission mask for VMPL3 + */ +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 vmpl1_perms:8; /* In */ + u32 vmpl2_perms:8; /* In */ + u32 vmpl3_perms:8; /* In */ + u32 rsvd4; +} __packed; + +/** + * struct sev_data_snp_launch_finish - SNP_LAUNCH_FINISH command params + * + * @gctx_addr: system physical 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 0th bit + * in the address indicates 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_UNSMASH command params + * + * @paddr: system physical address of page to be unsmashed. The 0th bit + * in the address indicates 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 + */ +struct sev_data_snp_dbg { + u64 gctx_paddr; /* In */ + u64 src_addr; /* In */ + u64 dst_addr; /* 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; + +/** + * struct sev_data_snp_init - SNP_INIT_EX structure + * + * @init_rmp: indicate that the RMP should be initialized. + * @list_paddr_en: indicate that list_paddr is valid + * @list_paddr: system physical address of range list + */ +struct sev_data_snp_init_ex { + u32 init_rmp:1; + u32 list_paddr_en:1; + u32 rsvd:30; + u32 rsvd1; + u64 list_paddr; + u8 rsvd2[48]; +} __packed; + +/** + * struct sev_data_range - RANGE structure + * + * @base: system physical address of first byte of range + * @page_count: number of 4KB pages in this range + */ +struct sev_data_range { + u64 base; + u32 page_count; + u32 rsvd; +} __packed; + +/** + * struct sev_data_range_list - RANGE_LIST structure + * + * @num_elements: number of elements in RANGE_ARRAY + * @ranges: array of num_elements of type RANGE + */ +struct sev_data_range_list { + u32 num_elements; + u32 rsvd; + struct sev_data_range ranges[0]; +} __packed; + +/** + * struct sev_data_snp_shutdown_ex - SNP_SHUTDOWN_EX structure + * + * @length: len of the command buffer read by the PSP + * @iommu_snp_shutdown: Disable enforcement of SNP in the IOMMU + */ +struct sev_data_snp_shutdown_ex { + u32 length; + u32 iommu_snp_shutdown:1; + u32 rsvd1:31; +} __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..7d8a2dd20273 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -61,6 +61,13 @@ 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_RMP_INIT_REQUIRED, + SEV_RET_MAX, } sev_ret_code; @@ -147,6 +154,52 @@ struct sev_user_data_get_id2 { __u32 length; /* In/Out */ } __packed; +/** + * struct sev_user_data_snp_status - SNP status + * + * @major: API major version + * @minor: API minor version + * @state: current platform state + * @is_rmp_initialized: whether RMP is initialized or not + * @build: firmware build id for the API version + * @mask_chip_id: whether chip id is present in attestation reports or not + * @mask_chip_key: whether attestation reports are signed or not + * @vlek_en: VLEK hashstick is loaded + * @guest_count: the number of guest currently managed by the firmware + * @current_tcb_version: current TCB version + * @reported_tcb_version: reported TCB version + */ +struct sev_user_data_snp_status { + __u8 api_major; /* Out */ + __u8 api_minor; /* Out */ + __u8 state; /* Out */ + __u8 is_rmp_initialized:1; /* Out */ + __u8 rsvd:7; + __u32 build_id; /* Out */ + __u32 mask_chip_id:1; /* Out */ + __u32 mask_chip_key:1; /* Out */ + __u32 vlek_en:1; /* Out */ + __u32 rsvd1:29; + __u32 guest_count; /* Out */ + __u64 current_tcb_version; /* Out */ + __u64 reported_tcb_version; /* Out */ +} __packed; + +/* + * struct sev_user_data_snp_config - system wide configuration value for SNP. + * + * @reported_tcb: The TCB version to report in the guest attestation report. + * @mask_chip_id: Indicates that the CHID_ID field in the attestation report + * will always be zero. + */ +struct sev_user_data_snp_config { + __u64 reported_tcb ; /* In */ + __u32 mask_chip_id:1; /* In */ + __u32 mask_chip_key:1; /* In */ + __u32 rsvd:30; /* In */ + __u8 rsvd1[52]; +} __packed; + /** * struct sev_issue_cmd - SEV ioctl parameters * From patchwork Mon Jun 12 04:25:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275468 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA8BEC7EE23 for ; Mon, 12 Jun 2023 04:32:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231713AbjFLEcY (ORCPT ); Mon, 12 Jun 2023 00:32:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234234AbjFLEb5 (ORCPT ); Mon, 12 Jun 2023 00:31:57 -0400 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2088.outbound.protection.outlook.com [40.107.102.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 193F210EB; Sun, 11 Jun 2023 21:31:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nEW2lAsy/SpMy6HClYRfLE2cqoZPjqGDJiuyY33JssL9+CxuooFyehaieyb/cNnZXL+CiTOg8MjfyCa36qdxonKdFqbFPG2VtpB6nRuSwvIPdoCwhpgdjBYFAUGBmWK7EIUCh0ALTIPU1DaYeKqfvnFkQDHXejDwrEQXTgdbu/KARLHQR53s76PlYL5k2BjIOOuj6McmL7gn/YlS/zWiLJxcAGj7kDSFmksDVaqW7ajf2AhD4IxrRWe5oiBTdw7xkONRlbdbxErhR/jLQ8+AIWbNAWCnuDwU9ihx+zldn/IyI5iSryxFsW9tkO3GOI1d8LjrBDE4wQOPj51f4uTPNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=1xj2/U+IIWPesaqCOErKg1ZrLHLEuUpPUlqimBPs3Vo=; b=JC5u+MdZxXL0SB5ek0dLbNYTVMFj98SRdaMWw6glDZTHWlsi7w5sp6Akj4BjvBBGTcq+sp85JgjHN8uqKTHxByg/eOBaMuWuc4YP2bB+04ZdvZF9ovVwRBFOiIOkz4iaU9NPGCLDGmM9lLMj/+7wyvHOitRjs3NGZHYEufJqkyczMTHA8mET11ZnQkFuNjh7SaYAMM4DkVCf0EpZ1xOr7rcSOd799/+1SjaYAeYmEW4lPG+0vWZ6ii4iRzNYzfFLHbd/ds8QGt6waenozc3gdKuDGQk2MyuekHEKAsp8AC2EfuY4vIPlq5oC2pUDQFOEvIGpJ4f+Scz8oz9UKHEJVQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=1xj2/U+IIWPesaqCOErKg1ZrLHLEuUpPUlqimBPs3Vo=; b=V4IjMOKt1RDBfKmtcF22eBSmW2Spcnbl7rXGOH2QzHOganmsgS8eiScXp2Owd+tW6mhbt6ON+e2InIhwdUKKcQEW/oaIh7EPjbRPstqfXnC2vcBRlrnaOezushkkIMQyJ8NQlVt/3Gmn9YXFrNk3zDDG99GLb0mi8vgnXIJYRL4= Received: from DS7PR05CA0041.namprd05.prod.outlook.com (2603:10b6:8:2f::8) by CH0PR12MB5073.namprd12.prod.outlook.com (2603:10b6:610:e0::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.44; Mon, 12 Jun 2023 04:31:24 +0000 Received: from CY4PEPF0000E9D4.namprd03.prod.outlook.com (2603:10b6:8:2f:cafe::bf) by DS7PR05CA0041.outlook.office365.com (2603:10b6:8:2f::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:31:24 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9D4.mail.protection.outlook.com (10.167.241.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:31:24 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:31:23 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Jarkko Sakkinen Subject: [PATCH RFC v9 17/51] crypto: ccp: Add support to initialize the AMD-SP for SEV-SNP Date: Sun, 11 Jun 2023 23:25:25 -0500 Message-ID: <20230612042559.375660-18-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D4:EE_|CH0PR12MB5073:EE_ X-MS-Office365-Filtering-Correlation-Id: 66d9803f-3d76-49bb-09ad-08db6afde17c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: WUux1d9jyn/bXMRejASeMzmuK+MHo1xI208XauE+psHBZrlF3PntuUmMpKzL9q9MRJuz/yMI9MGijA0JW3cT5JAN9fdtJqjhlaOHVUnlPA9o+xQSB1ZjXGaDgQuk2W6Lpzkzg8GOJvprCCj+6DVdTehVcxxNWrAJ2QMIhUEGu/wTP3YBykBhrYLjBz//dXiVvQeewBQcI5i4bydIacOPX/k+HEuQXVBqiKxeEAN0b8jl1o2NNC07tCOuyVuf8TD7BsdYJnZAvAajwsU7k45QFpHj38dLvLyvx4voQ9z4uatT3uLCHEG/hIljCKTTaNidPA2WdmLef7Of/izenQcSb0D5FgLnpSsRo3JwO8wGKIo4xX4PB8eTjf32qTZ4GoiILXgZcDvXTO90ggAMPSfhGDmvTAviCmMhchDRJkc0fMnIe5D+hzqcrCN4YJlfexpqFNf4MJDpkDVsn0qLwQAT/Oe4EaCMuf1Ak/Z5uoYORricZuGOZUQs5+U2ByIwgPbi6H+rq5KrorKhp9D+B26iDBMXmuj8fIy2WqgQUmKcXc8trJScC5rzE6CdfGFjUbPrwEEj4nyw0su/JjEKdGbdA6subuBD9oKOkQ2BxLcYKhjScP04bZkeYaTFveYIazjZuecGF7VsbPtoesAoF330n1k0sZDR/zuGiGU9PAinDJCgiOH9KpckzDe0iRRmJ7BnLBefmjTd8GUDcr1KybBwEtLQiUbtojdJKWrCKMZJAOTj/Efp2cYruiwilCtSxRtaZbYgrnTHdDhbmXvFB/KFsQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(136003)(346002)(376002)(396003)(39860400002)(451199021)(40470700004)(36840700001)(46966006)(86362001)(82310400005)(7406005)(7416002)(40460700003)(316002)(8676002)(41300700001)(82740400003)(83380400001)(5660300002)(26005)(40480700001)(81166007)(1076003)(356005)(36860700001)(8936002)(44832011)(36756003)(336012)(4326008)(426003)(6916009)(70586007)(47076005)(70206006)(478600001)(186003)(16526019)(30864003)(2906002)(54906003)(2616005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:31:24.3923 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 66d9803f-3d76-49bb-09ad-08db6afde17c X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D4.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5073 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh Before SNP VMs can be launched, the platform must be appropriately configured and initialized. Platform initialization is accomplished via the SNP_INIT command. Make sure to do a WBINVD and issue DF_FLUSH command to prepare for the first SNP guest launch after INIT. During the execution of SNP_INIT command, the firmware configures and enables SNP security policy enforcement in many system components. Some system components write to regions of memory reserved by early x86 firmware (e.g. UEFI). Other system components write to regions provided by the operation system, hypervisor, or x86 firmware. Such system components can only write to HV-fixed pages or Default pages. They will error when attempting to write to other page states after SNP_INIT enables their SNP enforcement. Starting in SNP firmware v1.52, the SNP_INIT_EX command takes a list of system physical address ranges to convert into the HV-fixed page states during the RMP initialization. If INIT_RMP is 1, hypervisors should provide all system physical address ranges that the hypervisor will never assign to a guest until the next RMP re-initialization. For instance, the memory that UEFI reserves should be included in the range list. This allows system components that occasionally write to memory (e.g. logging to UEFI reserved regions) to not fail due to RMP initialization and SNP enablement. Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Co-developed-by: Jarkko Sakkinen Signed-off-by: Jarkko Sakkinen Signed-off-by: Brijesh Singh Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 242 +++++++++++++++++++++++++++++++++-- drivers/crypto/ccp/sev-dev.h | 2 + 2 files changed, 234 insertions(+), 10 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index ab3572286755..d3764ee073f3 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -27,6 +27,7 @@ #include #include +#include #include "psp-dev.h" #include "sev-dev.h" @@ -35,6 +36,10 @@ #define SEV_FW_FILE "amd/sev.fw" #define SEV_FW_NAME_SIZE 64 +/* Minimum firmware version required for the SEV-SNP support */ +#define SNP_MIN_API_MAJOR 1 +#define SNP_MIN_API_MINOR 51 + static DEFINE_MUTEX(sev_cmd_mutex); static struct sev_misc_dev *misc_dev; @@ -78,6 +83,14 @@ static void *sev_es_tmr; #define NV_LENGTH (32 * 1024) static void *sev_init_ex_buffer; +/* + * SEV_DATA_RANGE_LIST: + * Array containing range of pages that firmware transitions to HV-fixed + * page state. + */ +struct sev_data_range_list *snp_range_list; +static int __sev_snp_init_locked(int *error); + static inline bool sev_version_greater_or_equal(u8 maj, u8 min) { struct sev_device *sev = psp_master->sev_data; @@ -462,7 +475,8 @@ static int __sev_platform_init_locked(int *error) { struct psp_device *psp = psp_master; struct sev_device *sev; - int rc = 0, psp_ret = -1; + int psp_ret = -1; + int rc; int (*init_function)(int *error); if (!psp || !psp->sev_data) @@ -473,6 +487,26 @@ static int __sev_platform_init_locked(int *error) if (sev->state == SEV_STATE_INIT) return 0; + rc = __sev_snp_init_locked(error); + if (rc && rc != -ENODEV) { + /* + * Don't abort the probe if SNP INIT failed, + * continue to initialize the legacy SEV firmware. + */ + dev_err(sev->dev, "SEV-SNP: failed to INIT rc %d, error %#x\n", rc, *error); + } + + if (!sev_es_tmr) { + /* Obtain the TMR memory area for SEV-ES use */ + sev_es_tmr = sev_fw_alloc(SEV_ES_TMR_SIZE); + if (sev_es_tmr) + /* Must flush the cache before giving it to the firmware */ + clflush_cache_range(sev_es_tmr, SEV_ES_TMR_SIZE); + else + dev_warn(sev->dev, + "SEV: TMR allocation failed, SEV-ES support unavailable\n"); + } + if (sev_init_ex_buffer) { init_function = __sev_init_ex_locked; rc = sev_read_init_ex_file(); @@ -832,6 +866,191 @@ 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 snp_filter_reserved_mem_regions(struct resource *rs, void *arg) +{ + struct sev_data_range_list *range_list = arg; + struct sev_data_range *range = &range_list->ranges[range_list->num_elements]; + size_t size; + + if ((range_list->num_elements * sizeof(struct sev_data_range) + + sizeof(struct sev_data_range_list)) > PAGE_SIZE) + return -E2BIG; + + switch (rs->desc) { + case E820_TYPE_RESERVED: + case E820_TYPE_PMEM: + case E820_TYPE_ACPI: + range->base = rs->start & PAGE_MASK; + size = (rs->end + 1) - rs->start; + range->page_count = size >> PAGE_SHIFT; + range_list->num_elements++; + break; + default: + break; + } + + return 0; +} + +static int __sev_snp_init_locked(int *error) +{ + struct psp_device *psp = psp_master; + struct sev_data_snp_init_ex data; + struct sev_device *sev; + int rc = 0; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return -ENODEV; + + if (!psp || !psp->sev_data) + return -ENODEV; + + sev = psp->sev_data; + + if (sev->snp_initialized) + return 0; + + if (!sev_version_greater_or_equal(SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR)) { + dev_dbg(sev->dev, "SEV-SNP support requires firmware version >= %d:%d\n", + SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR); + return 0; + } + + /* + * The 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); + + /* + * Starting in SNP firmware v1.52, the SNP_INIT_EX command takes a list of + * system physical address ranges to convert into the HV-fixed page states + * during the RMP initialization. For instance, the memory that UEFI + * reserves should be included in the range list. This allows system + * components that occasionally write to memory (e.g. logging to UEFI + * reserved regions) to not fail due to RMP initialization and SNP enablement. + */ + if (sev_version_greater_or_equal(SNP_MIN_API_MAJOR, 52)) { + /* + * Firmware checks that the pages containing the ranges enumerated + * in the RANGES structure are either in the Default page state or in the + * firmware page state. + */ + snp_range_list = kzalloc(PAGE_SIZE, GFP_KERNEL); + if (!snp_range_list) { + dev_err(sev->dev, + "SEV: SNP_INIT_EX range list memory allocation failed\n"); + return -ENOMEM; + } + + /* + * Retrieve all reserved memory regions setup by UEFI from the e820 memory map + * to be setup as HV-fixed pages. + */ + + rc = walk_iomem_res_desc(IORES_DESC_NONE, IORESOURCE_MEM, 0, ~0, + snp_range_list, snp_filter_reserved_mem_regions); + if (rc) { + dev_err(sev->dev, + "SEV: SNP_INIT_EX walk_iomem_res_desc failed rc = %d\n", rc); + return rc; + } + + memset(&data, 0, sizeof(data)); + data.init_rmp = 1; + data.list_paddr_en = 1; + data.list_paddr = __psp_pa(snp_range_list); + + /* + * Before invoking SNP_INIT_EX with INIT_RMP=1, make sure that + * all dirty cache lines containing the RMP are flushed. + * + * NOTE: that includes writes via RMPUPDATE instructions, which + * are also cacheable writes. + */ + wbinvd_on_all_cpus(); + + rc = __sev_do_cmd_locked(SEV_CMD_SNP_INIT_EX, &data, error); + if (rc) + return rc; + } else { + /* + * SNP_INIT is equivalent to SNP_INIT_EX with INIT_RMP=1, so + * just as with that case, make sure all dirty cache lines + * containing the RMP are flushed. + */ + wbinvd_on_all_cpus(); + + rc = __sev_do_cmd_locked(SEV_CMD_SNP_INIT, NULL, error); + if (rc) + return rc; + } + + /* Prepare for first SNP guest launch after INIT */ + wbinvd_on_all_cpus(); + rc = __sev_do_cmd_locked(SEV_CMD_SNP_DF_FLUSH, NULL, error); + if (rc) + return rc; + + sev->snp_initialized = true; + dev_dbg(sev->dev, "SEV-SNP firmware initialized\n"); + + return rc; +} + +static int __sev_snp_shutdown_locked(int *error) +{ + struct sev_device *sev = psp_master->sev_data; + struct sev_data_snp_shutdown_ex data; + int ret; + + if (!sev->snp_initialized) + return 0; + + memset(&data, 0, sizeof(data)); + data.length = sizeof(data); + data.iommu_snp_shutdown = 1; + + wbinvd_on_all_cpus(); + +retry: + ret = __sev_do_cmd_locked(SEV_CMD_SNP_SHUTDOWN_EX, &data, error); + /* SHUTDOWN may require DF_FLUSH */ + if (*error == SEV_RET_DFFLUSH_REQUIRED) { + ret = __sev_do_cmd_locked(SEV_CMD_SNP_DF_FLUSH, NULL, NULL); + if (ret) { + dev_err(sev->dev, "SEV-SNP DF_FLUSH failed\n"); + return ret; + } + goto retry; + } + if (ret) { + dev_err(sev->dev, "SEV-SNP firmware shutdown failed\n"); + return ret; + } + + sev->snp_initialized = false; + 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(error); + 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; @@ -1279,6 +1498,8 @@ int sev_dev_init(struct psp_device *psp) static void sev_firmware_shutdown(struct sev_device *sev) { + int error; + sev_platform_shutdown(NULL); if (sev_es_tmr) { @@ -1295,6 +1516,13 @@ static void sev_firmware_shutdown(struct sev_device *sev) get_order(NV_LENGTH)); sev_init_ex_buffer = NULL; } + + if (snp_range_list) { + kfree(snp_range_list); + snp_range_list = NULL; + } + + sev_snp_shutdown(&error); } void sev_dev_destroy(struct psp_device *psp) @@ -1350,15 +1578,6 @@ void sev_pci_init(void) } } - /* Obtain the TMR memory area for SEV-ES use */ - sev_es_tmr = sev_fw_alloc(SEV_ES_TMR_SIZE); - if (sev_es_tmr) - /* Must flush the cache before giving it to the firmware */ - clflush_cache_range(sev_es_tmr, SEV_ES_TMR_SIZE); - else - dev_warn(sev->dev, - "SEV: TMR allocation failed, SEV-ES support unavailable\n"); - if (!psp_init_on_probe) return; @@ -1368,6 +1587,9 @@ void sev_pci_init(void) dev_err(sev->dev, "SEV: failed to INIT error %#x, rc %d\n", error, rc); + dev_info(sev->dev, "SEV%s API:%d.%d build:%d\n", sev->snp_initialized ? + "-SNP" : "", sev->api_major, sev->api_minor, sev->build); + return; err: diff --git a/drivers/crypto/ccp/sev-dev.h b/drivers/crypto/ccp/sev-dev.h index 666c21eb81ab..34767657beb5 100644 --- a/drivers/crypto/ccp/sev-dev.h +++ b/drivers/crypto/ccp/sev-dev.h @@ -52,6 +52,8 @@ struct sev_device { u8 build; void *cmd_buf; + + bool snp_initialized; }; int sev_dev_init(struct psp_device *psp); From patchwork Mon Jun 12 04:25:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275469 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 42FE5C7EE25 for ; Mon, 12 Jun 2023 04:33:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234467AbjFLEdX (ORCPT ); Mon, 12 Jun 2023 00:33:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37760 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234630AbjFLEct (ORCPT ); Mon, 12 Jun 2023 00:32:49 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2041.outbound.protection.outlook.com [40.107.94.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2E251985; Sun, 11 Jun 2023 21:32:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=G88XIdgK7dbYM786YjyBSPIoyhGxD8ae4u/yC3a0HRCbUZpudILnGcL9krvoxozyTAqX3ELBm0pF8RULlQQssL8qgmocpDYyaeQw13mecZDqtmMovcaoZBrKAzLnugE25KXbPkDmeeeXbhaAicxlgig2nVjIiJkFfxdhaRXj54ixYFKYzf9/hV88xmpQRYE9YC/0arHFG3NU9+zO52CgXbEsNQjnGD72/4Nz9kiEkHvDPLyQL/lMd8046+9M9rMFcWrVET/WOf7A0/ptiubmKcfzg//KEf2CZfZkMGkNAaC3HDOvTlnE99mpFSjGaXX3b8C4QRHpYCpDrl8103hK5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=dJEJCtXjTD3p6zQVrVmrCzCQ8ktEmSSTgiuPOntccjk=; b=GCablkPMyq91VWPtNUN6bcwXkE7hdCQ1u9D5oaPkWqEZwzKNWLO3ohHc538Bd/Yfbibk+Jk9Kn5VkLm+95xp+BJKDEZnF7r6Eye+Xl6eZ/7bqV7c0gBiDWwTIeHAebR2x4hNa9GePEraZ4qmBBI1ayaD3MdWREO6vQu7p7TjW6LBxJPO2b4fE7oaoeMlbfKit/MU2s9zbRg+HEk03WztIWb1fD3DER1p3S9CK8cWKHDum5KEEtJub+N/2ljs/f+FplQTmSOlpnufKVKjDH4Hkb6bGEAnJXh1qaNTJ8ZBNhUta73tLEtDVfscdRO2b53WEmZcO5CWOVpMAQ6i1sJrNw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=dJEJCtXjTD3p6zQVrVmrCzCQ8ktEmSSTgiuPOntccjk=; b=oCC2mau1k5WHj2ypSYcugidetyViTxVJEp829Fzw36lXXSriVx8+tsBcALcXbAiWYaZbA9cj3NTYukYgu0gMgqtNtZGEH6ghWzIc/ks3UNjA88lH8KdadbLmdVmmwuXih9gJUrhaL4hAmXoGoVxCk0tomcVRGHe11Llng9Z5TV4= Received: from CY5PR03CA0030.namprd03.prod.outlook.com (2603:10b6:930:8::25) by PH7PR12MB6561.namprd12.prod.outlook.com (2603:10b6:510:213::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.29; Mon, 12 Jun 2023 04:32:04 +0000 Received: from CY4PEPF0000E9D2.namprd03.prod.outlook.com (2603:10b6:930:8:cafe::95) by CY5PR03CA0030.outlook.office365.com (2603:10b6:930:8::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.34 via Frontend Transport; Mon, 12 Jun 2023 04:32:04 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9D2.mail.protection.outlook.com (10.167.241.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:32:04 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:32:03 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 18/51] crypto: ccp: Provide API to issue SEV and SNP commands Date: Sun, 11 Jun 2023 23:25:26 -0500 Message-ID: <20230612042559.375660-19-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D2:EE_|PH7PR12MB6561:EE_ X-MS-Office365-Filtering-Correlation-Id: 08a2e49d-4b15-4d5d-5508-08db6afdf95c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: A8tqZ1uVCZaTqtuZtO1IPhsTKT7lYbW5efi5Xli+dF3TsegNHy5ezpWx44nd+PRF4oFxBPmCssmDyKYD1JpZ5rMOZyT8Xapt/BGE9Ik4lqeRztWElaws0x1c6sj1rsD+JFj6XRR96e4rio9SlQ4x8qmnYKTk0twWAHltvx25sGFAfOmmHnjdJfH126a8pl9ahZN79If+cpiizQVVON+nFzU4+p/BYUhlc9029P0poZRlld1lrDdZpbeFuo9RWIHErxea4Gebs4eoyn93/B5KEkNZK9RFQZYniUVrUWMD7vgWiKjz3KrP/gG2a33jCMnxFVymQjsCsb5bpYdWks92M6rnYLo7B83o74e6bErdfcWQKdtSGk+SBW6N/yinCv4zdW1i+QNuJvCurwXKCmlQso4dL08jsh+mOBltXICSb6arVDG0Dd+3Atu1QndSPCxYMAjxbkmHY4sNYsoqEffpRB/ZNodkplzgNQ4ecIEFeCrI1MWDsHS3yDs6L82/AyHzydhBVoQHijtwzP/B9Cpp9bdwxTvD82H4USXyp0+9U0woCyqkNr5y4+DVKUVnwZnfTEUbv91CT7rAdUNqW56Nq17fbQpVgsygMA9YqhIU8+weebDLcuPhw3eopuL5N0uFut5nBYQgje5Mnv80WyfVGSuSidsJ+haqUErN5O3gB771AvzpGjphrVHflpl7SxEoOdIeqP83vWaCPRVgUsf5SKsEnYp8PPATppvdN928VmGWa1qMOD6Z6kTaRh+LpLM4u7X30mrEejm3pou5s89g2nP7hhLChrcUWKAhL5ctk/0= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(396003)(136003)(376002)(346002)(39860400002)(451199021)(46966006)(36840700001)(40470700004)(316002)(40460700003)(41300700001)(336012)(426003)(82310400005)(83380400001)(40480700001)(2616005)(36860700001)(26005)(186003)(7416002)(7406005)(44832011)(2906002)(47076005)(86362001)(16526019)(1076003)(81166007)(356005)(82740400003)(8676002)(36756003)(5660300002)(8936002)(70586007)(70206006)(478600001)(54906003)(4326008)(6666004)(6916009)(36900700001)(134885004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:32:04.4342 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 08a2e49d-4b15-4d5d-5508-08db6afdf95c X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D2.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB6561 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh Make sev_do_cmd() a generic API interface for the hypervisor to issue commands to manage an SEV and SNP guest. The commands for SEV and SNP are defined in the SEV and SEV-SNP firmware specifications. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 3 ++- include/linux/psp-sev.h | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index d3764ee073f3..88c5bf264a87 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -418,7 +418,7 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) return ret; } -static int sev_do_cmd(int cmd, void *data, int *psp_ret) +int sev_do_cmd(int cmd, void *data, int *psp_ret) { int rc; @@ -428,6 +428,7 @@ static int sev_do_cmd(int cmd, void *data, int *psp_ret) return rc; } +EXPORT_SYMBOL_GPL(sev_do_cmd); static int __sev_init_locked(int *error) { diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 06d0619ca442..c8656a36baeb 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -891,6 +891,20 @@ int sev_guest_df_flush(int *error); */ int sev_guest_decommission(struct sev_data_decommission *data, int *error); +/** + * sev_do_cmd - perform SEV 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_do_cmd(int cmd, void *data, int *psp_ret); + void *psp_copy_user_blob(u64 uaddr, u32 len); #else /* !CONFIG_CRYPTO_DEV_SP_PSP */ @@ -906,6 +920,9 @@ sev_guest_deactivate(struct sev_data_deactivate *data, int *error) { return -ENO static inline int sev_guest_decommission(struct sev_data_decommission *data, int *error) { return -ENODEV; } +static inline int +sev_do_cmd(int cmd, void *data, int *psp_ret) { return -ENODEV; } + static inline int sev_guest_activate(struct sev_data_activate *data, int *error) { return -ENODEV; } From patchwork Mon Jun 12 04:25:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275470 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 43E13C7EE25 for ; Mon, 12 Jun 2023 04:33:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233708AbjFLEdn (ORCPT ); Mon, 12 Jun 2023 00:33:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234325AbjFLEdE (ORCPT ); Mon, 12 Jun 2023 00:33:04 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2086.outbound.protection.outlook.com [40.107.95.86]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 661741FD2; Sun, 11 Jun 2023 21:32:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=l5dqAoni8U7DvhaUFVyjNAH5i55tKrKmpNV/NPxsivGBxVJ7Mcl7KvFYB/UsKIKcWBNENyj2IDo8IF3xOPGe/+avB/J8DL2A+icF92I+oaBgaMNaJvNT9pA8mwXJG84dUvykIGeKiexF2R/j5xSwWdERcZJ1XEcpUPwlh6jnFUae4WWu/d5+jrm3a1qdZD4n9Xwf1t7xslo7nQJd1Og/7N1jFvcg6f2jL0tTUTbilSo+wg2e9QRDFk0GcJPeYhohph/DXziom/zB9MxJ12DCWKvpft5SahHeTRyEjaTlivbjf3sxyoXWHrDpFD3ywISls2KCd84A1wM1y0hzZ3t4sw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=gEd/MCK8LB8MNsiER/01EW7HgLCLIvaG50Hd8XNvNzg=; b=mzhy7sN7tIKjhYaD/9AMUKHhsTZi5BHcb0hKNZXZyAZ77eRGamgJFc4Q3KJFLBw4eZyW4JMD1vKIu4lrOvfckUJStDtKOcELnukE9qtrGFFrfx2kJQ+5rNbuLdCtnWqf02ZEJBXwex9obRh8nc7F78hVi/Djj8+jR8ufLt18bb6Pla9OsupNKEfFU2HlypzygOZkQPK9HVshHEypL6HlT12tIjbpixknkpi2X/8e8kru4j6qmwPeUESxybkH1EbaepYhXATRlI8qkkUyISY/zSbe61n1Yddl9THSSnga4P3rpcs3uJDYcZ/ZGJgAGoHd92czOdDUh1U1LPYpJo0B9g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=gEd/MCK8LB8MNsiER/01EW7HgLCLIvaG50Hd8XNvNzg=; b=wrgBiGt0FO4uFsCPHIj+JBuQvEhng2hP8I8bNpinvE+kF2l6oTQBv7SK0OfPm4gJDL94QX2ZQybqbq7uBvHaKgyajgz0tMblb3BP8vCuhLKgDKf3uSKLzLw/nUXTAsiv5iYMXesmFgJblceGAvzKChn8sio5HGrSnylZGX8MScQ= Received: from DS7PR05CA0032.namprd05.prod.outlook.com (2603:10b6:8:2f::31) by PH7PR12MB7355.namprd12.prod.outlook.com (2603:10b6:510:20e::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.32; Mon, 12 Jun 2023 04:32:26 +0000 Received: from CY4PEPF0000E9D4.namprd03.prod.outlook.com (2603:10b6:8:2f:cafe::24) by DS7PR05CA0032.outlook.office365.com (2603:10b6:8:2f::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:32:26 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9D4.mail.protection.outlook.com (10.167.241.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:32:26 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:32:25 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 19/51] x86/sev: Introduce snp leaked pages list Date: Sun, 11 Jun 2023 23:25:27 -0500 Message-ID: <20230612042559.375660-20-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D4:EE_|PH7PR12MB7355:EE_ X-MS-Office365-Filtering-Correlation-Id: 1660822a-76c1-4ebb-d3f6-08db6afe064c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: pg8/+Sf+npALiYaoZ2JZsTyszQWVPMSC384tr/tp6/43WpoR5REFrs4u+S9/hKqE6dj069oECbl6FWPI9hjIF6bgbqqNtaHeqvcJHoVsfK/sTu7k5twIEaXHUu+2scWDROXEJBeiMfzKJFO+1eyA0FuTOv+otbJt6C7pYgdNWAdd2/penWh8ZVtOemlHChqyv0DOyQWgMYzJ9pN6kCqQpVuWjiF7RqgIorQ/qwPKTOmBKv+Jsg9KyHTh1R88v7T/z1jE/YM13hkYgLk2nnhGKvlTb0P6m1Oams7RqNI4/gbpYJQzg0Yy9nNLqaFvMJF3yChsZDA1DcT9v18h4h/1qwd0O2pohEbfvXXAkrZu08DTLw0CMS9PjwHl9SlUklbNUWemhG9lbot6BBqUnDQaPqWYx+atarCc0Tg0/GXNVeuKmiGIkJcDBhrDQccLyYSmnKIz43TL4VeeWCsu+rEgs0J2zHfbEOq2x8+NOZ8fjS/Hlp45NIbWWHEZw9LU5ZJzTez4ZchLjn5KbYucqZzt2f+cg3zPOPHL29KfDdXWfiQLDMEyYTBEP+4dksXGKsKnOfLXfCnVl8SAuZeIt4PEHEPIQyHekljr3Gf2mpcmNoHaZk7yd4GewipQu3ET0HhTCzHofSmghVjjXOpTx5UOvJW2huxQ8jm3FpHd59olulp6n2hAXr0+gSXsHPtBvukt17m42AAJio+CGjgTUwMCnuHwu13E3NGKu2604CCEgSSaEOwW61XwiDzK4Xh5AUSwe2vuTg4jvEcKZkVBIOd71g== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(136003)(39860400002)(396003)(376002)(346002)(451199021)(36840700001)(46966006)(40470700004)(36756003)(2906002)(82310400005)(7406005)(86362001)(7416002)(44832011)(40480700001)(47076005)(186003)(16526019)(83380400001)(36860700001)(40460700003)(426003)(336012)(5660300002)(26005)(1076003)(82740400003)(478600001)(356005)(81166007)(54906003)(70586007)(6916009)(4326008)(2616005)(70206006)(41300700001)(316002)(8936002)(8676002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:32:26.1586 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1660822a-76c1-4ebb-d3f6-08db6afe064c X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D4.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7355 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Ashish Kalra Pages are unsafe to be released back to the page-allocator, if they have been transitioned to firmware/guest state and can't be reclaimed or transitioned back to hypervisor/shared state. In this case add them to an internal leaked pages list to ensure that they are not freed or touched/accessed to cause fatal page faults. Signed-off-by: Ashish Kalra [mdr: relocate to arch/x86/coco/sev/host.c] Signed-off-by: Michael Roth --- arch/x86/coco/sev/host.c | 28 ++++++++++++++++++++++++++++ arch/x86/include/asm/sev-host.h | 3 +++ 2 files changed, 31 insertions(+) diff --git a/arch/x86/coco/sev/host.c b/arch/x86/coco/sev/host.c index cd3b4c6a25bc..373e91f5a337 100644 --- a/arch/x86/coco/sev/host.c +++ b/arch/x86/coco/sev/host.c @@ -64,6 +64,12 @@ struct rmpentry { static unsigned long rmptable_start __ro_after_init; static unsigned long rmptable_end __ro_after_init; +/* list of pages which are leaked and cannot be reclaimed */ +static LIST_HEAD(snp_leaked_pages_list); +static DEFINE_SPINLOCK(snp_leaked_pages_list_lock); + +static atomic_long_t snp_nr_leaked_pages = ATOMIC_LONG_INIT(0); + #undef pr_fmt #define pr_fmt(fmt) "SEV-SNP: " fmt @@ -494,3 +500,25 @@ int rmp_make_shared(u64 pfn, enum pg_level level) return rmpupdate(pfn, &val); } EXPORT_SYMBOL_GPL(rmp_make_shared); + +void snp_leak_pages(unsigned long pfn, unsigned int npages) +{ + struct page *page = pfn_to_page(pfn); + + WARN(1, "psc failed, pfn 0x%lx pages %d (marked offline)\n", pfn, npages); + + spin_lock(&snp_leaked_pages_list_lock); + while (npages--) { + /* + * Reuse the page's buddy list for chaining into the leaked + * pages list. This page should not be on a free list currently + * and is also unsafe to be added to a free list. + */ + list_add_tail(&page->buddy_list, &snp_leaked_pages_list); + sev_dump_rmpentry(pfn); + pfn++; + } + spin_unlock(&snp_leaked_pages_list_lock); + atomic_long_inc(&snp_nr_leaked_pages); +} +EXPORT_SYMBOL_GPL(snp_leak_pages); diff --git a/arch/x86/include/asm/sev-host.h b/arch/x86/include/asm/sev-host.h index 753e80d16433..bab3b226777a 100644 --- a/arch/x86/include/asm/sev-host.h +++ b/arch/x86/include/asm/sev-host.h @@ -19,6 +19,8 @@ void sev_dump_rmpentry(u64 pfn); int psmash(u64 pfn); int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asid, bool immutable); int rmp_make_shared(u64 pfn, enum pg_level level); +void snp_leak_pages(unsigned long pfn, unsigned int npages); + #else static inline int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) { return 0; } static inline void sev_dump_rmpentry(u64 pfn) {} @@ -29,6 +31,7 @@ static inline int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int as return -ENODEV; } static inline int rmp_make_shared(u64 pfn, enum pg_level level) { return -ENODEV; } +void snp_leak_pages(unsigned long pfn, unsigned int npages) {} #endif #endif From patchwork Mon Jun 12 04:25:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275471 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9C119C7EE23 for ; Mon, 12 Jun 2023 04:34:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234325AbjFLEeS (ORCPT ); Mon, 12 Jun 2023 00:34:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231508AbjFLEds (ORCPT ); Mon, 12 Jun 2023 00:33:48 -0400 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2067.outbound.protection.outlook.com [40.107.220.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 691B810C0; Sun, 11 Jun 2023 21:33:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=INMgf6i4cDaPTI+dOPEyrPA2uEoSjGA314/tivaZGNTYqjugRu4VT6A5+etFlwE1uJa7E9iOHsmSsOTrTBb81JEkMiWX+7LHLfmTrjX6flsQfKXF4Ol23n3DQwBGCdilc4omny2PUD9CrfCsQXmhZBOwIZuNPjeEvV+/ZQGiUwFOGKHWWu1HM4wFZQjEozJ3SUjYRpVRdUahkpwJqQAsRqVcPJLDib0eLQ7UdHRn49w3V6bCBz9ROfJ7Jl6w0cp54kZQ24JDJQ4CA3QnLazkp58xVKHD3uU8Kr+3J43Wi4tmPRVjXM25b9kszGT1HSSOXIpA5tHXt2znnYHeXkqKWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=21O9V8fXiW3DdE/EWkyVZmcvDCjg+nAvrOW5CbIppOs=; b=PWfmsjhRAL0/cdL3ZyKLTN6oiaAglpx6UtOe2nqBIe+jpygdUJIK8vLwmZRvV8YWyEy/DBAOInUjMLnJdboX5qvsBAO2a7q26ELIIdK817T7SOYI94iomSLPsCfCjPF0o4CDNw1x8uBnWQ3OBNVhfPOEITEO6a2b5K2ncND/TOD02YQimcvqNteMizEKLZ9GE00hmpSjPPiheJGfyHN5whyHiX/xC4ucTbrAzLCwiuHnkVTeP7XqqfNfSho4WokumN4CL8s1C2MsYSzUz94qgw3TuV+AUPvfG+q0EmhAvXC+0Ggrg2DI4qDjVXfeeTUGDNtfhtxeLvmYVPFurCzufw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=21O9V8fXiW3DdE/EWkyVZmcvDCjg+nAvrOW5CbIppOs=; b=Ap7wXx3YWuMfpLKJLLlGBJyev29ltYk56xeh2jBX0a0KpDHbh0qt5kI/8DMfikFQ+i2DTNwPr0EnEjmP6IhK2XewPOD3OzH8G0+90YWW5PhbNw0bi4vKfGIntXq3Ei6e8Ndjgmh3vP1wLJS4gL9zCMaYp0BSi+YRfDN2+vzbkq4= Received: from DM6PR05CA0050.namprd05.prod.outlook.com (2603:10b6:5:335::19) by MN2PR12MB4254.namprd12.prod.outlook.com (2603:10b6:208:1d0::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.29; Mon, 12 Jun 2023 04:33:10 +0000 Received: from DM6NAM11FT042.eop-nam11.prod.protection.outlook.com (2603:10b6:5:335:cafe::b4) by DM6PR05CA0050.outlook.office365.com (2603:10b6:5:335::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:33:10 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT042.mail.protection.outlook.com (10.13.173.165) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.29 via Frontend Transport; Mon, 12 Jun 2023 04:33:10 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:33:04 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 20/51] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled Date: Sun, 11 Jun 2023 23:25:28 -0500 Message-ID: <20230612042559.375660-21-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT042:EE_|MN2PR12MB4254:EE_ X-MS-Office365-Filtering-Correlation-Id: 1a77be9f-a83b-4c4f-8bdb-08db6afe208b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2Sg4XbqAhiK/QXS0msCLvjxEgyEWAnCLG02T5kqRVLllS2r1QlK0G6v91H/fcXC9+MwUTab9qwrD1yqkI/1HysRny+N4pv41yX0xC8H9WTlnyvmNg1t4Dgwumcda3sPj1UFjHWLJDIkvF2BWFqoGcX/nxwY+nrMJAjqAX6jhM5kKLNXNkSeXlJWZTTMej4HtL6TDx9+bvuBk03GsKyhRR3Bz3510sw4V5hh/Cm6A5r4SH1aU4tQLiAh+3TVFIkenub0ZqCZL5vDL1t9j+pORRlNpIzO//vjlJNvpKxMJ5+pr35hAyZTUKFSMv5GHSqbLEvCO0gUH2OMnB6glRe+5hHsGRKH+ibJ9U/5d6DlyLaS2phqRnbYBNw0IPhBCF8GM7ca2kEODgQDWOlNjaW9ze8AwziXqR+Khg/S38OHQGNYR8rHy5WBTSl7PyMI8qYryTc0HpUnwZBpvi5I3hcQQ6gx+wWCDNfL72dQ1nUthjIT2D2ROIuJbEmnkFKNMrwaIeY/3May7uR1FQS3LJTA9GoDT0KemaHXF8TFOKUYbwzCI+UWzamTJijsNWuWMU1NtZLcpnNu2mdo8MwIsZ5D5pxGT+nM8n97jI7a2oOKJe44jbRayordJNBUMwyU+ZMsjl28CNoGdUkqCfifOib10FYfkKUXZ75AI4SB8MJwrlCqtxVuYxBvhRDJPQKw9O7KgbVp92Xd7nKMZOhlIt7xtnUTD2q8eA6B/nDJKjHvc2UL6LTRG+WA6dgYlQ+4t5ODyaU0d/8IPT3Z2DOti7SEr6w== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(396003)(136003)(376002)(346002)(451199021)(40470700004)(36840700001)(46966006)(5660300002)(7406005)(7416002)(47076005)(8936002)(2906002)(8676002)(41300700001)(83380400001)(336012)(36860700001)(426003)(44832011)(356005)(82740400003)(81166007)(6916009)(70206006)(82310400005)(316002)(2616005)(40460700003)(70586007)(4326008)(186003)(16526019)(40480700001)(26005)(86362001)(1076003)(54906003)(478600001)(36756003)(6666004)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:33:10.2060 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1a77be9f-a83b-4c4f-8bdb-08db6afe208b X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT042.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4254 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh The behavior and requirement for the SEV-legacy command is altered when the SNP firmware is in the INIT state. See SEV-SNP firmware specification for more details. Allocate the Trusted Memory Region (TMR) as a 2mb sized/aligned region when SNP is enabled to satisfy new requirements for the SNP. Continue allocating a 1mb region for !SNP configuration. While at it, provide API that can be used by others to allocate a page that can be used by the firmware. The immediate user for this API will be the KVM driver. The KVM driver to need to allocate a firmware context page during the guest creation. The context page need to be updated by the firmware. See the SEV-SNP specification for further details. Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Signed-off-by: Brijesh Singh [mdr: use struct sev_data_snp_page_reclaim instead of passing paddr directly to SEV_CMD_SNP_PAGE_RECLAIM] Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 150 ++++++++++++++++++++++++++++++++--- include/linux/psp-sev.h | 9 +++ 2 files changed, 150 insertions(+), 9 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 88c5bf264a87..d8124d33c831 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -91,6 +91,13 @@ static void *sev_init_ex_buffer; struct sev_data_range_list *snp_range_list; static int __sev_snp_init_locked(int *error); +/* When SEV-SNP is enabled the TMR needs to be 2MB aligned and 2MB size. */ +#define SEV_SNP_ES_TMR_SIZE (2 * 1024 * 1024) + +static size_t sev_es_tmr_size = SEV_ES_TMR_SIZE; + +static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret); + static inline bool sev_version_greater_or_equal(u8 maj, u8 min) { struct sev_device *sev = psp_master->sev_data; @@ -191,11 +198,131 @@ static int sev_cmd_buffer_len(int cmd) return 0; } +static int snp_reclaim_pages(unsigned long paddr, unsigned int npages, bool locked) +{ + /* Cbit maybe set in the paddr */ + unsigned long pfn = __sme_clr(paddr) >> PAGE_SHIFT; + int ret, err, i, n = 0; + + for (i = 0; i < npages; i++, pfn++, n++) { + struct sev_data_snp_page_reclaim data = {0}; + + data.paddr = pfn << PAGE_SHIFT; + + if (locked) + ret = __sev_do_cmd_locked(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err); + else + ret = sev_do_cmd(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err); + + if (ret) + goto cleanup; + + ret = rmp_make_shared(pfn, PG_LEVEL_4K); + if (ret) + goto cleanup; + } + + return 0; + +cleanup: + /* + * If failed to reclaim the page then page is no longer safe to + * be release back to the system, leak it. + */ + snp_leak_pages(pfn, npages - n); + return ret; +} + +static int rmp_mark_pages_firmware(unsigned long paddr, unsigned int npages, bool locked) +{ + /* Cbit maybe set in the paddr */ + unsigned long pfn = __sme_clr(paddr) >> PAGE_SHIFT; + int rc, n = 0, i; + + for (i = 0; i < npages; i++, n++, pfn++) { + rc = rmp_make_private(pfn, 0, PG_LEVEL_4K, 0, true); + if (rc) + goto cleanup; + } + + return 0; + +cleanup: + /* + * Try unrolling the firmware state changes by + * reclaiming the pages which were already changed to the + * firmware state. + */ + snp_reclaim_pages(paddr, n, locked); + + return rc; +} + +static struct page *__snp_alloc_firmware_pages(gfp_t gfp_mask, int order, bool locked) +{ + unsigned long npages = 1ul << order, paddr; + struct sev_device *sev; + struct page *page; + + if (!psp_master || !psp_master->sev_data) + return NULL; + + page = alloc_pages(gfp_mask, order); + if (!page) + return NULL; + + /* If SEV-SNP is initialized then add the page in RMP table. */ + sev = psp_master->sev_data; + if (!sev->snp_initialized) + return page; + + paddr = __pa((unsigned long)page_address(page)); + if (rmp_mark_pages_firmware(paddr, npages, locked)) + return NULL; + + return page; +} + +void *snp_alloc_firmware_page(gfp_t gfp_mask) +{ + struct page *page; + + page = __snp_alloc_firmware_pages(gfp_mask, 0, false); + + return page ? page_address(page) : NULL; +} +EXPORT_SYMBOL_GPL(snp_alloc_firmware_page); + +static void __snp_free_firmware_pages(struct page *page, int order, bool locked) +{ + struct sev_device *sev = psp_master->sev_data; + unsigned long paddr, npages = 1ul << order; + + if (!page) + return; + + paddr = __pa((unsigned long)page_address(page)); + if (sev->snp_initialized && + snp_reclaim_pages(paddr, npages, locked)) + return; + + __free_pages(page, order); +} + +void snp_free_firmware_page(void *addr) +{ + if (!addr) + return; + + __snp_free_firmware_pages(virt_to_page(addr), 0, false); +} +EXPORT_SYMBOL_GPL(snp_free_firmware_page); + static void *sev_fw_alloc(unsigned long len) { struct page *page; - page = alloc_pages(GFP_KERNEL, get_order(len)); + page = __snp_alloc_firmware_pages(GFP_KERNEL, get_order(len), false); if (!page) return NULL; @@ -443,7 +570,7 @@ static int __sev_init_locked(int *error) data.tmr_address = __pa(sev_es_tmr); data.flags |= SEV_INIT_FLAGS_SEV_ES; - data.tmr_len = SEV_ES_TMR_SIZE; + data.tmr_len = sev_es_tmr_size; } return __sev_do_cmd_locked(SEV_CMD_INIT, &data, error); @@ -466,7 +593,7 @@ static int __sev_init_ex_locked(int *error) data.tmr_address = __pa(sev_es_tmr); data.flags |= SEV_INIT_FLAGS_SEV_ES; - data.tmr_len = SEV_ES_TMR_SIZE; + data.tmr_len = sev_es_tmr_size; } return __sev_do_cmd_locked(SEV_CMD_INIT_EX, &data, error); @@ -499,14 +626,16 @@ static int __sev_platform_init_locked(int *error) if (!sev_es_tmr) { /* Obtain the TMR memory area for SEV-ES use */ - sev_es_tmr = sev_fw_alloc(SEV_ES_TMR_SIZE); - if (sev_es_tmr) + sev_es_tmr = sev_fw_alloc(sev_es_tmr_size); + if (sev_es_tmr) { /* Must flush the cache before giving it to the firmware */ - clflush_cache_range(sev_es_tmr, SEV_ES_TMR_SIZE); - else + if (!sev->snp_initialized) + clflush_cache_range(sev_es_tmr, sev_es_tmr_size); + } else { dev_warn(sev->dev, "SEV: TMR allocation failed, SEV-ES support unavailable\n"); } + } if (sev_init_ex_buffer) { init_function = __sev_init_ex_locked; @@ -1001,6 +1130,8 @@ static int __sev_snp_init_locked(int *error) sev->snp_initialized = true; dev_dbg(sev->dev, "SEV-SNP firmware initialized\n"); + sev_es_tmr_size = SEV_SNP_ES_TMR_SIZE; + return rc; } @@ -1507,8 +1638,9 @@ static void sev_firmware_shutdown(struct sev_device *sev) /* The TMR area was encrypted, flush it from the cache */ wbinvd_on_all_cpus(); - free_pages((unsigned long)sev_es_tmr, - get_order(SEV_ES_TMR_SIZE)); + __snp_free_firmware_pages(virt_to_page(sev_es_tmr), + get_order(sev_es_tmr_size), + false); sev_es_tmr = NULL; } diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index c8656a36baeb..5ae61de96e44 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -906,6 +906,8 @@ int sev_guest_decommission(struct sev_data_decommission *data, int *error); int sev_do_cmd(int cmd, void *data, int *psp_ret); void *psp_copy_user_blob(u64 uaddr, u32 len); +void *snp_alloc_firmware_page(gfp_t mask); +void snp_free_firmware_page(void *addr); #else /* !CONFIG_CRYPTO_DEV_SP_PSP */ @@ -933,6 +935,13 @@ 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 void *snp_alloc_firmware_page(gfp_t mask) +{ + return NULL; +} + +static inline void snp_free_firmware_page(void *addr) { } + #endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */ From patchwork Mon Jun 12 04:25:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275473 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 81BE1C7EE23 for ; Mon, 12 Jun 2023 04:35:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233109AbjFLEf3 (ORCPT ); Mon, 12 Jun 2023 00:35:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234926AbjFLEey (ORCPT ); Mon, 12 Jun 2023 00:34:54 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7F048171D; Sun, 11 Jun 2023 21:34:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=L4kJjcsIR5AhX8Q9VuuNAGKlqFZb7gmylOfq5ShJfOiqEmYwFsIU60DwpMCExMhcd7GOx7fKsaioLkYlztBZ4lfMhT/mXqKHwtSm4+fYkBQp31lSDnw8kbteoNGXL6WUmQNV+iZtwiySxp+J39/tm4Pks1Uo+nFA1MLfcSiqfn7ZJQ5eiSlMuYBtCrAlNzIdp9meZ8sJCIvww9vCDsW2h/1guVHE2FYyPE4KsbQWIbE458EzrAOzlxFt2wnqX9JFSPlIRd1ARDYU3EKLUMkzow0X+J60FBfzCueSKd1Ob12iqA0hT31or1P1JfXL7elJeweGdVnvQR6boeKZ67NVIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=FtlNrZkBs+mDmj0lD3T2KGzFDdVu9lV4Sfe9JlYuGEc=; b=W3f2nMqdaQT0uZjw0gGd1njBfjp1bRJNG8/FXw2Ew8L5GB++tdrW1rfrpR6D0HewRn5BFfhF8u/H9d2pdqRYanZCaK8LkAMmCdx8MWompMevehtxfksD5/00PEbOnYLxp0FFfM+75xFRHiBcexvkdJJWZhLZZC+JLFDQyMN38X1F+ZUg3m4Qr8LkO+nOUzd0OB6O9Applri95L8T8o7hCUmKpe9s2/mktNfY1dtgxOU56SsCZ2FC5/K6bfjpfcxHovQ1LTUHpXLhJuUODtRCVxYWaGZaSeLS1AcQPztZEOL5Yxla0IeA3xBk4jjtMiMMclV5yNrxr++C+wM81muYAw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=FtlNrZkBs+mDmj0lD3T2KGzFDdVu9lV4Sfe9JlYuGEc=; b=3jUmAcJZbc758CLRnNKO8b9lezHl48ZH9S11doolj5rSPdAk5qQs+ylOVb9vVoOAnetZ3VDcHMkHzp6IgqCJs0m/nQ0W93Yi8jY/2BLpqRm5tcbxV4qDeVd/wqLMtdKTsbySIQopG+xpRx4HloGboj5oWbv6ibSUxTxyasAMjaY= Received: from BY3PR03CA0005.namprd03.prod.outlook.com (2603:10b6:a03:39a::10) by BL1PR12MB5335.namprd12.prod.outlook.com (2603:10b6:208:317::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.38; Mon, 12 Jun 2023 04:34:30 +0000 Received: from DM6NAM11FT062.eop-nam11.prod.protection.outlook.com (2603:10b6:a03:39a:cafe::e4) by BY3PR03CA0005.outlook.office365.com (2603:10b6:a03:39a::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:34:30 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT062.mail.protection.outlook.com (10.13.173.40) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:34:29 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:34:25 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 21/51] crypto: ccp: Handle the legacy SEV command when SNP is enabled Date: Sun, 11 Jun 2023 23:25:29 -0500 Message-ID: <20230612042559.375660-22-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT062:EE_|BL1PR12MB5335:EE_ X-MS-Office365-Filtering-Correlation-Id: 0d352f9b-ad2f-42b2-c8e9-08db6afe5009 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: DvPe/j7WtUOROF+vN0nk6wN9zCriPinr01iviun9TQtqQITNq7JHVipiJnL0nWsOB+xDi5ChSoDa7SA1NuDUDKTjfCEqKwNbK3XNuHCXkY3ezPKcs6Cr8iyQhRc6gCRU75OsVRhhe5DEsVmVa3dSF1V+N9x93Tk9cDcKqKmWRDTAFPpWgZhltHbgL98A3Y3JPE2+ipIcethfgQI42U7hLEOS/FgelsFzANRDwRMeftRuziaqxn/o+F2YmQqCetr4i03eFnxv7Dyzz9Th2k1eR4SMPgymfE1flsIrbo8xDk3nzKjW4J8lzoBPZj2wCF8kN0IoJ/hPbaOsaUId6PcKGC7lYCI0i50bT92r01+2+90hHicVk/QiN8zWn0HNk8XfP56PnG+J9R1T8KMEKPR7AALAsVRpn2AyxRwltlSSAInJs9fzFYalVw+HLDstMjvPYLKPtWykKM0T8xK2Tgo9PCTwPcwe5pv0AaAAv5PoGYct04XFQUvAIkKDqNQrxpU+o6kZB2qH0XKe4L8Xw8K7hwUBTCP8tXGYdI2AEaiS+uGC82jY//F0hHcDs+MpK7I3JYo9Ow/27lxN4LrCU5hRjMgifvhoR+mAIn9fa3xpyvfCiHliiKAzeXIzsPvTj2MVx2azGrjr6aH7bJ/J6Z0KKHYuDpeCvv+I9bd7GL3Te8nbvuPDp+HrNezDriYKbqJbQUhj85/UhSy24BakqdqQ7QYNgIJ8EQHOpwIa6fxRbwM0h3sk9ors4noDrZEWn5MAMHYVZ0eMS8I/Si31RQ9ibg== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(136003)(376002)(346002)(39860400002)(396003)(451199021)(36840700001)(40470700004)(46966006)(316002)(40460700003)(41300700001)(336012)(426003)(26005)(83380400001)(36860700001)(82310400005)(7416002)(186003)(16526019)(44832011)(47076005)(1076003)(7406005)(2616005)(2906002)(86362001)(30864003)(82740400003)(356005)(40480700001)(81166007)(36756003)(8936002)(8676002)(70206006)(70586007)(478600001)(5660300002)(54906003)(6916009)(4326008)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:34:29.9331 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0d352f9b-ad2f-42b2-c8e9-08db6afe5009 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT062.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5335 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh 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.. A command buffer may contains a system physical address that the firmware may write to. There are two cases that need to be handled: 1) system physical address points to a guest memory 2) system physical address points to a host memory To handle the case #1, change the page state to the firmware in the RMP table before issuing the command and restore the state to shared after the command completes. For the case #2, use a bounce buffer to complete the request. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 371 ++++++++++++++++++++++++++++++++++- drivers/crypto/ccp/sev-dev.h | 12 ++ 2 files changed, 373 insertions(+), 10 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index d8124d33c831..10bb0a7dcfd6 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -28,6 +28,7 @@ #include #include #include +#include #include "psp-dev.h" #include "sev-dev.h" @@ -258,6 +259,30 @@ static int rmp_mark_pages_firmware(unsigned long paddr, unsigned int npages, boo return rc; } +static int rmp_mark_pages_shared(unsigned long paddr, unsigned int npages) +{ + /* Cbit maybe set in the paddr */ + unsigned long pfn = __sme_clr(paddr) >> PAGE_SHIFT; + int rc, n = 0, i; + + for (i = 0; i < npages; i++, pfn++, n++) { + rc = rmp_make_shared(pfn, PG_LEVEL_4K); + if (rc) + goto cleanup; + } + + return 0; + +cleanup: + /* + * If failed to change the page state to shared, then its not safe + * to release the page back to the system, leak it. + */ + snp_leak_pages(pfn, npages - n); + + return rc; +} + static struct page *__snp_alloc_firmware_pages(gfp_t gfp_mask, int order, bool locked) { unsigned long npages = 1ul << order, paddr; @@ -459,12 +484,295 @@ static int sev_write_init_ex_file_if_required(int cmd_id) return sev_write_init_ex_file(); } +static int alloc_snp_host_map(struct sev_device *sev) +{ + struct page *page; + int i; + + for (i = 0; i < MAX_SNP_HOST_MAP_BUFS; i++) { + struct snp_host_map *map = &sev->snp_host_map[i]; + + memset(map, 0, sizeof(*map)); + + page = alloc_pages(GFP_KERNEL_ACCOUNT, get_order(SEV_FW_BLOB_MAX_SIZE)); + if (!page) + return -ENOMEM; + + map->host = page_address(page); + } + + return 0; +} + +static void free_snp_host_map(struct sev_device *sev) +{ + int i; + + for (i = 0; i < MAX_SNP_HOST_MAP_BUFS; i++) { + struct snp_host_map *map = &sev->snp_host_map[i]; + + if (map->host) { + __free_pages(virt_to_page(map->host), get_order(SEV_FW_BLOB_MAX_SIZE)); + memset(map, 0, sizeof(*map)); + } + } +} + +static int map_firmware_writeable(u64 *paddr, u32 len, bool guest, struct snp_host_map *map) +{ + unsigned int npages = PAGE_ALIGN(len) >> PAGE_SHIFT; + + map->active = false; + + if (!paddr || !len) + return 0; + + map->paddr = *paddr; + map->len = len; + + /* If paddr points to a guest memory then change the page state to firmwware. */ + if (guest) { + if (rmp_mark_pages_firmware(*paddr, npages, true)) + return -EFAULT; + + goto done; + } + + if (!map->host) + return -ENOMEM; + + /* Check if the pre-allocated buffer can be used to fullfil the request. */ + if (len > SEV_FW_BLOB_MAX_SIZE) + return -EINVAL; + + /* Transition the pre-allocated buffer to the firmware state. */ + if (rmp_mark_pages_firmware(__pa(map->host), npages, true)) + return -EFAULT; + + /* Set the paddr to use pre-allocated firmware buffer */ + *paddr = __psp_pa(map->host); + +done: + map->active = true; + return 0; +} + +static int unmap_firmware_writeable(u64 *paddr, u32 len, bool guest, struct snp_host_map *map) +{ + unsigned int npages = PAGE_ALIGN(len) >> PAGE_SHIFT; + + if (!map->active) + return 0; + + /* If paddr points to a guest memory then restore the page state to hypervisor. */ + if (guest) { + if (snp_reclaim_pages(*paddr, npages, true)) + return -EFAULT; + + goto done; + } + + /* + * Transition the pre-allocated buffer to hypervisor state before the access. + * + * This is because while changing the page state to firmware, the kernel unmaps + * the pages from the direct map, and to restore the direct map the pages must + * be transitioned back to the shared state. + */ + if (snp_reclaim_pages(__pa(map->host), npages, true)) + return -EFAULT; + + /* Copy the response data firmware buffer to the callers buffer. */ + memcpy(__va(__sme_clr(map->paddr)), map->host, min_t(size_t, len, map->len)); + *paddr = map->paddr; + +done: + map->active = false; + 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: + case SEV_CMD_ATTESTATION_REPORT: + return true; + default: + return false; + } +} + +#define prep_buffer(name, addr, len, guest, map) \ + func(&((typeof(name *))cmd_buf)->addr, ((typeof(name *))cmd_buf)->len, guest, map) + +static int __snp_cmd_buf_copy(int cmd, void *cmd_buf, bool to_fw, int fw_err) +{ + int (*func)(u64 *paddr, u32 len, bool guest, struct snp_host_map *map); + struct sev_device *sev = psp_master->sev_data; + bool from_fw = !to_fw; + + /* + * After the command is completed, change the command buffer memory to + * hypervisor state. + * + * The immutable bit is automatically cleared by the firmware, so + * no not need to reclaim the page. + */ + if (from_fw && sev_legacy_cmd_buf_writable(cmd)) { + if (rmp_mark_pages_shared(__pa(cmd_buf), 1)) + return -EFAULT; + + /* No need to go further if firmware failed to execute command. */ + if (fw_err) + return 0; + } + + if (to_fw) + func = map_firmware_writeable; + else + func = unmap_firmware_writeable; + + /* + * A command buffer may contains a system physical address. If the address + * points to a host memory then use an intermediate firmware page otherwise + * change the page state in the RMP table. + */ + switch (cmd) { + case SEV_CMD_PDH_CERT_EXPORT: + if (prep_buffer(struct sev_data_pdh_cert_export, pdh_cert_address, + pdh_cert_len, false, &sev->snp_host_map[0])) + goto err; + if (prep_buffer(struct sev_data_pdh_cert_export, cert_chain_address, + cert_chain_len, false, &sev->snp_host_map[1])) + goto err; + break; + case SEV_CMD_GET_ID: + if (prep_buffer(struct sev_data_get_id, address, len, + false, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_PEK_CSR: + if (prep_buffer(struct sev_data_pek_csr, address, len, + false, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_LAUNCH_UPDATE_DATA: + if (prep_buffer(struct sev_data_launch_update_data, address, len, + true, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_LAUNCH_UPDATE_VMSA: + if (prep_buffer(struct sev_data_launch_update_vmsa, address, len, + true, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_LAUNCH_MEASURE: + if (prep_buffer(struct sev_data_launch_measure, address, len, + false, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_LAUNCH_UPDATE_SECRET: + if (prep_buffer(struct sev_data_launch_secret, guest_address, guest_len, + true, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_DBG_DECRYPT: + if (prep_buffer(struct sev_data_dbg, dst_addr, len, false, + &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_DBG_ENCRYPT: + if (prep_buffer(struct sev_data_dbg, dst_addr, len, true, + &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_ATTESTATION_REPORT: + if (prep_buffer(struct sev_data_attestation_report, address, len, + false, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_SEND_START: + if (prep_buffer(struct sev_data_send_start, session_address, + session_len, false, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_SEND_UPDATE_DATA: + if (prep_buffer(struct sev_data_send_update_data, hdr_address, hdr_len, + false, &sev->snp_host_map[0])) + goto err; + if (prep_buffer(struct sev_data_send_update_data, trans_address, + trans_len, false, &sev->snp_host_map[1])) + goto err; + break; + case SEV_CMD_SEND_UPDATE_VMSA: + if (prep_buffer(struct sev_data_send_update_vmsa, hdr_address, hdr_len, + false, &sev->snp_host_map[0])) + goto err; + if (prep_buffer(struct sev_data_send_update_vmsa, trans_address, + trans_len, false, &sev->snp_host_map[1])) + goto err; + break; + case SEV_CMD_RECEIVE_UPDATE_DATA: + if (prep_buffer(struct sev_data_receive_update_data, guest_address, + guest_len, true, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_RECEIVE_UPDATE_VMSA: + if (prep_buffer(struct sev_data_receive_update_vmsa, guest_address, + guest_len, true, &sev->snp_host_map[0])) + goto err; + break; + default: + break; + } + + /* The command buffer need to be in the firmware state. */ + if (to_fw && sev_legacy_cmd_buf_writable(cmd)) { + if (rmp_mark_pages_firmware(__pa(cmd_buf), 1, true)) + return -EFAULT; + } + + return 0; + +err: + return -EINVAL; +} + +static inline bool need_firmware_copy(int cmd) +{ + struct sev_device *sev = psp_master->sev_data; + + /* After SNP is INIT'ed, the behavior of legacy SEV command is changed. */ + return ((cmd < SEV_CMD_SNP_INIT) && sev->snp_initialized) ? true : false; +} + +static int snp_aware_copy_to_firmware(int cmd, void *data) +{ + return __snp_cmd_buf_copy(cmd, data, true, 0); +} + +static int snp_aware_copy_from_firmware(int cmd, void *data, int fw_err) +{ + return __snp_cmd_buf_copy(cmd, data, false, fw_err); +} + static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) { struct psp_device *psp = psp_master; struct sev_device *sev; unsigned int phys_lsb, phys_msb; unsigned int reg, ret = 0; + void *cmd_buf; int buf_len; if (!psp || !psp->sev_data) @@ -484,12 +792,28 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) * work for some memory, e.g. vmalloc'd addresses, and @data may not be * physically contiguous. */ - if (data) - memcpy(sev->cmd_buf, data, buf_len); + if (data) { + if (sev->cmd_buf_active > 2) + return -EBUSY; + + cmd_buf = sev->cmd_buf_active ? sev->cmd_buf_backup : sev->cmd_buf; + + memcpy(cmd_buf, data, buf_len); + sev->cmd_buf_active++; + + /* + * The behavior of the SEV-legacy commands is altered when the + * SNP firmware is in the INIT state. + */ + if (need_firmware_copy(cmd) && snp_aware_copy_to_firmware(cmd, cmd_buf)) + return -EFAULT; + } else { + cmd_buf = sev->cmd_buf; + } /* Get the physical address of the command buffer */ - phys_lsb = data ? lower_32_bits(__psp_pa(sev->cmd_buf)) : 0; - phys_msb = data ? upper_32_bits(__psp_pa(sev->cmd_buf)) : 0; + phys_lsb = data ? lower_32_bits(__psp_pa(cmd_buf)) : 0; + phys_msb = data ? upper_32_bits(__psp_pa(cmd_buf)) : 0; dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n", cmd, phys_msb, phys_lsb, psp_timeout); @@ -532,15 +856,24 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) ret = sev_write_init_ex_file_if_required(cmd); } - print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data, - buf_len, false); - /* * Copy potential output from the PSP back to data. Do this even on * failure in case the caller wants to glean something from the error. */ - if (data) - memcpy(data, sev->cmd_buf, buf_len); + if (data) { + /* + * Restore the page state after the command completes. + */ + if (need_firmware_copy(cmd) && + snp_aware_copy_from_firmware(cmd, cmd_buf, ret)) + return -EFAULT; + + memcpy(data, cmd_buf, buf_len); + sev->cmd_buf_active--; + } + + print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data, + buf_len, false); return ret; } @@ -624,6 +957,14 @@ static int __sev_platform_init_locked(int *error) dev_err(sev->dev, "SEV-SNP: failed to INIT rc %d, error %#x\n", rc, *error); } + /* + * Allocate the intermediate buffers used for the legacy command handling. + */ + if (rc != -ENODEV && alloc_snp_host_map(sev)) { + dev_notice(sev->dev, "Failed to alloc host map (disabling legacy SEV)\n"); + goto skip_legacy; + } + if (!sev_es_tmr) { /* Obtain the TMR memory area for SEV-ES use */ sev_es_tmr = sev_fw_alloc(sev_es_tmr_size); @@ -677,6 +1018,7 @@ static int __sev_platform_init_locked(int *error) dev_info(sev->dev, "SEV API:%d.%d build:%d\n", sev->api_major, sev->api_minor, sev->build); +skip_legacy: return 0; } @@ -1586,10 +1928,12 @@ int sev_dev_init(struct psp_device *psp) if (!sev) goto e_err; - sev->cmd_buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 0); + sev->cmd_buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 1); if (!sev->cmd_buf) goto e_sev; + sev->cmd_buf_backup = (uint8_t *)sev->cmd_buf + PAGE_SIZE; + psp->sev_data = sev; sev->dev = dev; @@ -1655,6 +1999,12 @@ static void sev_firmware_shutdown(struct sev_device *sev) snp_range_list = NULL; } + /* + * The host map need to clear the immutable bit so it must be free'd before the + * SNP firmware shutdown. + */ + free_snp_host_map(sev); + sev_snp_shutdown(&error); } @@ -1726,6 +2076,7 @@ void sev_pci_init(void) return; err: + free_snp_host_map(sev); psp_master->sev_data = NULL; } diff --git a/drivers/crypto/ccp/sev-dev.h b/drivers/crypto/ccp/sev-dev.h index 34767657beb5..19d79f9d4212 100644 --- a/drivers/crypto/ccp/sev-dev.h +++ b/drivers/crypto/ccp/sev-dev.h @@ -29,11 +29,20 @@ #define SEV_CMDRESP_CMD_SHIFT 16 #define SEV_CMDRESP_IOC BIT(0) +#define MAX_SNP_HOST_MAP_BUFS 2 + struct sev_misc_dev { struct kref refcount; struct miscdevice misc; }; +struct snp_host_map { + u64 paddr; + u32 len; + void *host; + bool active; +}; + struct sev_device { struct device *dev; struct psp_device *psp; @@ -52,8 +61,11 @@ struct sev_device { u8 build; void *cmd_buf; + void *cmd_buf_backup; + int cmd_buf_active; bool snp_initialized; + struct snp_host_map snp_host_map[MAX_SNP_HOST_MAP_BUFS]; }; int sev_dev_init(struct psp_device *psp); From patchwork Mon Jun 12 04:25:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275492 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0A6C0C7EE25 for ; Mon, 12 Jun 2023 04:35:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234629AbjFLEft (ORCPT ); Mon, 12 Jun 2023 00:35:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41686 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235235AbjFLEfL (ORCPT ); Mon, 12 Jun 2023 00:35:11 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2062.outbound.protection.outlook.com [40.107.94.62]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 99A9DFA; Sun, 11 Jun 2023 21:35:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IQl1tdIjBibIEFuipN+aaTxMV2HbOsbS8Os/ymr3CFj2GR3isN90l5UOC1ER8OK3ebqzSRJyT6BV+AdHrG/Gy0OfZb6djpIw5GI/d2Ik4bajySzpnIfAXcXNtfLPbV6fOhJbHGL/U2NoyMh/8aEHq32OTW9XLtvHIg0nW+K3Z/3Wpk5jaRrg7f7lY1OzH/ycCKxwTv6cd214eWWyJtF+Pn9YvFgHevKYh7x5Tjkv7FAYOWMHmxhhInonPSoZ19FldaqdQ6QeXVK74nu1U4BTPZJavpXY5Q6x9XP9UqW/mTLZzD9z3rcS54f5Otoljnoq5sSFRvI+CY2IKNC2m4XUTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=M5PixqsA5QQA7A/TruSoyBOLJ81zSazDzW860FjASDM=; b=Vt6jpzBLv1fLwWoqkqmAUQ48nRmNkZM5WfBEORVTyqo/1CDxa/GnLgpzplAcXcTYSTfVPtvBrYuhbZGpJKDqknogICTMrgptEC39FH1wTZOCYCG8YQTgy0gBTnXHRZY53tBwKt1tjqwKfcerzkhbwXSg9U2H1qSfRQCGkY1D+QkhFSdj3wcJacV7RuoUN8JkrhpwmSzBpTgtcHsjpr/rhXWO2SHA3vxjPeQ3ImMuThhJbrlrSBTO5hogZKqBGMpajFK81YII7pRsXBZ5A5w1PNf2YA8VPtC8XKXnvflpMsdOWCWuvA/mU007Mko+P7Za+Bhj8rNNONy5hB//oyBNdQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=M5PixqsA5QQA7A/TruSoyBOLJ81zSazDzW860FjASDM=; b=tvRLduvJeIPeBQi00OeM5XF2DKp//bfS7DSmDW6QverV7JVujMsh1uIM3tVuROwUxMn5IJOJckey94lQGzzjL9ehJsmWo7AIoT7PKx2c8qZyy2aUzIGUEgTIVhTetHZZ7pIbS/ZTCyBIfmwQ2qgt+b1bTAPZmV8MxL7b7vAOhHY= Received: from DM6PR06CA0022.namprd06.prod.outlook.com (2603:10b6:5:120::35) by IA1PR12MB8264.namprd12.prod.outlook.com (2603:10b6:208:3f5::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.29; Mon, 12 Jun 2023 04:35:02 +0000 Received: from DM6NAM11FT042.eop-nam11.prod.protection.outlook.com (2603:10b6:5:120:cafe::db) by DM6PR06CA0022.outlook.office365.com (2603:10b6:5:120::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:35:02 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT042.mail.protection.outlook.com (10.13.173.165) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.29 via Frontend Transport; Mon, 12 Jun 2023 04:35:02 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:35:01 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 22/51] crypto: ccp: Add the SNP_PLATFORM_STATUS command Date: Sun, 11 Jun 2023 23:25:30 -0500 Message-ID: <20230612042559.375660-23-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT042:EE_|IA1PR12MB8264:EE_ X-MS-Office365-Filtering-Correlation-Id: e5162c36-bd59-4262-029b-08db6afe633c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: x/nzRJqVhQegFZIFjd69mC9F6yvWmKohIrk/5lP+hGKK9DKXj1CQZxEj0Juf6Vq5uEnjlBtsxXjX3U6OrXcOrc/3s338NMCgHVdIKfGeYW9uSU1Wm2bbjCFpZGPacre19SJPpqHNsXPzNTbA82fPg0eAUrUczSilmZxUjqdj0q+4hoeK/FvER6JzTxCcZcM9A3GtmbQN8B+7ZqBHYwDubssca0oA37d9d2Ggakl8ptwxa9blznG6XN4YA9/eud/uWyps2078qpKJPv7BUVOMiqaJ+v1smTxbDyiCdSWgs8ETvNVD45EFfx18KlNo9/+3w47HJbNhsKzW+7+W/HD5HtfaCuG4v1pft72dgAIy2UPxPJF4Jp5MeIkaG/CElIsQra0Okul1/UNT5Xew3Eub2drOqw4uG2Qg5uManqT5oK51tWcr1AweI3TzwZqLaFdzdnMnxvlscvgGZJ17mEdlpJeSHlDu21qzUObv/kTPLGtDTHhSe2yAvIFfDAjjdeyF0NhDybsRH0IxSMKgxRywZ6HeepF9LWPVTjd2h7X70ozvICtrJAQR1nWWxZHNmfXL2aJ4dK/o2JpValyF1zFg8bn081GUAXDcVLK5GnpNUKg1O0xMsKPXuQowMNmbTQaBlRyBTBgwx3hEh68wp9e2cXS7rEOyvxqi0bj7porylowKT4Wd71d8bnGHWxkqkjD6effA96Kp1hKrH7JxG3NXVqK+8o2RnRJ0KuCMFk2vz98EHUoazo0XF2P2DrrvjSfNiFGwDErpC3rtgw6NS16+4w== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(346002)(136003)(376002)(39860400002)(396003)(451199021)(36840700001)(46966006)(40470700004)(40460700003)(70586007)(70206006)(316002)(6666004)(4326008)(6916009)(478600001)(54906003)(36756003)(86362001)(47076005)(36860700001)(1076003)(26005)(426003)(336012)(16526019)(186003)(83380400001)(41300700001)(5660300002)(8936002)(8676002)(7416002)(44832011)(82310400005)(2906002)(40480700001)(7406005)(356005)(2616005)(81166007)(82740400003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:35:02.1078 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e5162c36-bd59-4262-029b-08db6afe633c X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT042.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB8264 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh The command can be used by the userspace to query the SNP platform status report. See the SEV-SNP spec for more details. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- Documentation/virt/coco/sev-guest.rst | 27 ++++++++++++++++ drivers/crypto/ccp/sev-dev.c | 45 +++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 1 + 3 files changed, 73 insertions(+) diff --git a/Documentation/virt/coco/sev-guest.rst b/Documentation/virt/coco/sev-guest.rst index bf593e88cfd9..11ea67c944df 100644 --- a/Documentation/virt/coco/sev-guest.rst +++ b/Documentation/virt/coco/sev-guest.rst @@ -61,6 +61,22 @@ counter (e.g. counter overflow), then -EIO will be returned. __u64 fw_err; }; +The host ioctl should be called to /dev/sev device. The ioctl accepts command +id and command input structure. + +:: + struct sev_issue_cmd { + /* Command ID */ + __u32 cmd; + + /* Command request structure */ + __u64 data; + + /* firmware error code on failure (see psp-sev.h) */ + __u32 error; + }; + + 2.1 SNP_GET_REPORT ------------------ @@ -118,6 +134,17 @@ be updated with the expected value. See GHCB specification for further detail on how to parse the certificate blob. +2.4 SNP_PLATFORM_STATUS +----------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_data_snp_platform_status +:Returns (out): 0 on success, -negative on error + +The SNP_PLATFORM_STATUS command is used to query the SNP platform status. The +status includes API major, minor version and more. See the SEV-SNP +specification for further details. + 3. SEV-SNP CPUID Enforcement ============================ diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 10bb0a7dcfd6..0bfe9721c977 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -1767,6 +1767,48 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable) return ret; } +static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) +{ + struct sev_device *sev = psp_master->sev_data; + struct sev_data_snp_addr buf; + struct page *status_page; + void *data; + int ret; + + if (!sev->snp_initialized || !argp->data) + return -EINVAL; + + status_page = alloc_page(GFP_KERNEL_ACCOUNT); + if (!status_page) + return -ENOMEM; + + data = page_address(status_page); + if (rmp_mark_pages_firmware(__pa(data), 1, true)) { + __free_pages(status_page, 0); + return -EFAULT; + } + + buf.gctx_paddr = __psp_pa(data); + ret = __sev_do_cmd_locked(SEV_CMD_SNP_PLATFORM_STATUS, &buf, &argp->error); + + /* Change the page state before accessing it */ + if (snp_reclaim_pages(__pa(data), 1, true)) { + snp_leak_pages(__pa(data) >> PAGE_SHIFT, 1); + return -EFAULT; + } + + if (ret) + goto cleanup; + + if (copy_to_user((void __user *)argp->data, data, + sizeof(struct sev_user_data_snp_status))) + ret = -EFAULT; + +cleanup: + __free_pages(status_page, 0); + return ret; +} + static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) { void __user *argp = (void __user *)arg; @@ -1818,6 +1860,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) case SEV_GET_ID2: ret = sev_ioctl_do_get_id2(&input); break; + case SNP_PLATFORM_STATUS: + ret = sev_ioctl_snp_platform_status(&input); + break; default: ret = -EINVAL; goto out; diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index 7d8a2dd20273..4dc6a3e7b3d5 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -28,6 +28,7 @@ enum { SEV_PEK_CERT_IMPORT, SEV_GET_ID, /* This command is deprecated, use SEV_GET_ID2 */ SEV_GET_ID2, + SNP_PLATFORM_STATUS, SEV_MAX, }; From patchwork Mon Jun 12 04:25:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275493 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 94DD7C7EE25 for ; Mon, 12 Jun 2023 04:36:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234800AbjFLEgV (ORCPT ); Mon, 12 Jun 2023 00:36:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234180AbjFLEfi (ORCPT ); Mon, 12 Jun 2023 00:35:38 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2042.outbound.protection.outlook.com [40.107.95.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89F14E7D; Sun, 11 Jun 2023 21:35:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=D8i5nQwB/HjU+uPR828/aad7kv67DnXqL/PwyIwBcWrwYuqW011fPKDhrBAbJ0fbBakYlEMUgTB5CUJ9AzH5Ch2Ff61AjORrc1GDNaCLq2cM9p2vh6ALr2XlpTQ6hHuBQZJUNPB1+xl9eQ3WW4vBaXzRaVNfeIffq745W9bsXghgHsXy2lV1W3M+IyeFAw8SXvtn3quJQgJZJdz2RrybQO8L3uaxBdfPOZlo91IDvLdzQXIZeZaq2dcU/CY0wqgbpvGdU7rajYVqAnw1xkHhqzZf7UvqzXPk7IXErY6GydLewhUS6MJXIEiIpvYVtzqaIS5awSpmuxrs1taRV3FNeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=d4wfb4qGv5SePlU/T9QnyyuYBpCMuLO/Bu9lKFk7jQ4=; b=mxljwh4NsMm32WvOFbJSJHL0ylcX1d3tdhyo4iCjX9mvNPVQUES/qGa95IZph+lPOVm5KB8vNAoKZSz8wdG6iVbtpnLFOSnqcgdvcfYe/Ebis9sIM2UOsICAoAJp8Vz+9GvN/syIcN7EMLOwdhyQSk53Ce6OKgmXB88AIOAzUTvtxmx/oBh65GOmChmksJJTESK4p797fJ2ZcxldRSj4PNSw3dryF+JimeijhxtOdROrU9mN1dAFmgNyEsEtuV/DB+zQhWGCC6oAf4kbNKrwQCvUW9irZPVJct1YFvEIItwhoVErrVp/NH1N6UVpLicqr//ouv9TxLAtoKgGWimKQA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=d4wfb4qGv5SePlU/T9QnyyuYBpCMuLO/Bu9lKFk7jQ4=; b=eovlZJ5kzFJCKFPxwIPETtb0LXJ4MaCToPGe1OCNAJbctz5UcYNvfG3x4JZe+fH8qKTXYh+RCmGgfuE3CHQSzOVNyLm/tvsczLH/XJWAqqN1j22UTRhMcDGLHb5+4Kc7Xq/dvGFbpYq0adbjC3cudDnFzN9FsOPhWAK6M2MCk8M= Received: from DS7PR03CA0189.namprd03.prod.outlook.com (2603:10b6:5:3b6::14) by BY5PR12MB4225.namprd12.prod.outlook.com (2603:10b6:a03:211::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.46; Mon, 12 Jun 2023 04:35:34 +0000 Received: from DM6NAM11FT093.eop-nam11.prod.protection.outlook.com (2603:10b6:5:3b6:cafe::2c) by DS7PR03CA0189.outlook.office365.com (2603:10b6:5:3b6::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:35:34 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT093.mail.protection.outlook.com (10.13.172.235) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.29 via Frontend Transport; Mon, 12 Jun 2023 04:35:33 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:35:32 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 23/51] KVM: SEV: Select CONFIG_KVM_PROTECTED_VM when CONFIG_KVM_AMD_SEV=y Date: Sun, 11 Jun 2023 23:25:31 -0500 Message-ID: <20230612042559.375660-24-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT093:EE_|BY5PR12MB4225:EE_ X-MS-Office365-Filtering-Correlation-Id: a2b78bd2-671e-412f-81fb-08db6afe7639 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: b8s8DOnfa7UpiLqSFpaMNyRbN2WOU6/wyCDBQpLKX4nofNIqEs3XB20cx68Zj1BO1zofxGhbMrfKc9j4cNq0aIQiHt54BWFwdpIxbbjfBDGUoBrviKT7s2YLkb/qhygc6gzEeB/+5FNqZlqaA0sGXOx5siJ1r/E4ZjiYCPrfyuDLMUi8sF5zFBmQtMjOFOZhAnYMUuZDxgCwqf+W8dnRXQbOoIK8SNJVYbMo4JPd7/EAvVf0uy/bA+dHTNg67YBJIsH5cJTvSqlb3aHLprdOmAH+OIVCx0pyirRYiJBUSv/2BJYmnnZB/RyCASEtJjAGPDCBr6phfLbTU/96js/urRf5xlERlnOoeeZIUBY3IkZPfRdP7GCWh1qnZjS/AH9Rxk6ewM2uTvLybMf33YaYTnQ8hzPX8mSVGGZrgVn6rVLbyEhG2RSi9ID0IM0hvxWAibqcRf7zXjeRoHZsTHCNR7e8fbBhdelLzSxWtMoSBnkN9gomjPfvtqhFYffX9wnw6mmA764kRX4ZlCs4BJdJZHGb6a6Nsqmcfhc9B3VC7fE0FJ0jPCr+ivUMnJJGsS4Do6zM2IqDExMnEP9NTkj9nKjixW+/6M2rIxdytPcmXY7KmLuJVcAh/hxg/Y8dfcAQDXLmoKAuZXQcizKx73IeCgndkq7yp8M7TWi3RS0I3wfSQTd+gCvBrReqOlkKaVG2a9YfZNmOp8G9EqbhmFO1RI6sKf9S0qzCRhnPuAWnlfZLQQxESkHccDbPnrhlByqIVFjETccXriNr6bHmcZcugA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(376002)(39850400004)(396003)(346002)(136003)(451199021)(36840700001)(40470700004)(46966006)(40460700003)(478600001)(36860700001)(47076005)(16526019)(186003)(1076003)(26005)(2616005)(426003)(336012)(36756003)(81166007)(82310400005)(86362001)(356005)(82740400003)(40480700001)(6916009)(4326008)(70586007)(70206006)(41300700001)(316002)(44832011)(7406005)(7416002)(4744005)(2906002)(8676002)(8936002)(54906003)(5660300002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:35:33.9993 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a2b78bd2-671e-412f-81fb-08db6afe7639 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT093.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4225 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org AMD SEV relies on the restricted/protected memory support to run guests in some cases (such as SEV lazy-pinning), so make sure to enable that support with the CONFIG_KVM_PROTECTED_VM build option. Signed-off-by: Michael Roth --- arch/x86/kvm/Kconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index 718010600956..638679a4e5dc 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -126,6 +126,7 @@ config KVM_AMD_SEV bool "AMD Secure Encrypted Virtualization (SEV) support" depends on KVM_AMD && X86_64 depends on CRYPTO_DEV_SP_PSP && !(KVM_AMD=y && CRYPTO_DEV_CCP_DD=m) + select KVM_PROTECTED_VM help Provides support for launching Encrypted VMs (SEV) and Encrypted VMs with Encrypted State (SEV-ES) on AMD processors. From patchwork Mon Jun 12 04:25:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275494 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6628DC7EE25 for ; Mon, 12 Jun 2023 04:36:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234502AbjFLEgz (ORCPT ); Mon, 12 Jun 2023 00:36:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234513AbjFLEgj (ORCPT ); Mon, 12 Jun 2023 00:36:39 -0400 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2084.outbound.protection.outlook.com [40.107.223.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81A661BD1; Sun, 11 Jun 2023 21:36:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PzBuvqVrVgx5QaX4z/cLqNIN5Ug9MJOjvMT5LYhqtgnkvCfNgbaCnmHuwkJMiO6Q+z00ck0FpHcHEgIQ0AIF0bdEpbegyi7AIwU6NPL/sOXZankkiExX7xE7GKKCSbzXpBG9VEujVxti6mk9fzdf8krjYVsSJzxkEkhYFcdFQIJP5J7FFd6QjJiVCWzzftji7ChlVowuTXBt1OPnl+SzNwQjdRdOIikfuLbZhf1zbRlhevdXIIH54LucyH+tHiCg1NlF3BZA7KXktw2PihvAVOo7cq+Yl0u8t/TVTKs+7rX/8RqKr/AjdVp7e6e2+QdhxoHhFZX8Zfnuy1oXktacvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=9nJ5zpVhl4br77hne2iYfznSmBLEzrcXtOigBe9hqVM=; b=M7h+WJ5cI/EXV94N42vs2Bu2lmBv4mofSQjxy0h6GxKRCo7aNPbWgYS3eJg9EG+Z2TK9JT7zkTMhi7y/AeGEAJr5yErI38y4xRptcwN44OcIfiZu6aNp8mlKbrbTrRO26WVKSnFhG2RkCTbEVJ9JLQ3RAQOAnxbxFW8eu7PZmHEZHfz2b4lqY3djia4GLdlqoenLNlLgMvepIe+/1UGXxchi8qkBmHXPXtUm0+rm+yr8RkrfY6B/bbBWX5Mmxez37H22jNlVZVwI2/8Lw3pDE7dgOQEW1h5/JWZG2UhHfBoLqi35SzsmJMhGvK8vi2WCUt5elVG+fAs2jhuUyJIkBg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=9nJ5zpVhl4br77hne2iYfznSmBLEzrcXtOigBe9hqVM=; b=vaLMD9UzS4ZbAyWuRaAwriD9K/MuqNrWCZbgruqca8ZLVdnf66g/sliaPoSoun9qGYJqMgIdkbmIYUfTiU7yypV3PJfxXrb5h+PWwgvDLeIOIwhl6nJTMPMvNvAP9gMAsquV6niZdyQBpjrh4sfvXoYDtpoUy/ewoNOl9doKteM= Received: from DM6PR07CA0097.namprd07.prod.outlook.com (2603:10b6:5:337::30) by CH0PR12MB5074.namprd12.prod.outlook.com (2603:10b6:610:e1::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.46; Mon, 12 Jun 2023 04:35:58 +0000 Received: from DM6NAM11FT010.eop-nam11.prod.protection.outlook.com (2603:10b6:5:337:cafe::b4) by DM6PR07CA0097.outlook.office365.com (2603:10b6:5:337::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:35:58 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT010.mail.protection.outlook.com (10.13.172.222) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.31 via Frontend Transport; Mon, 12 Jun 2023 04:35:58 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:35:57 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 24/51] KVM: SVM: Add support to handle AP reset MSR protocol Date: Sun, 11 Jun 2023 23:25:32 -0500 Message-ID: <20230612042559.375660-25-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT010:EE_|CH0PR12MB5074:EE_ X-MS-Office365-Filtering-Correlation-Id: e5920512-a81a-4d9c-1b6c-08db6afe84bb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: rqK7AFtqnNuXhApJzdERleiUko9HJRWeIj+Stb0wE4TiiBbKMVb8Mk7dJuABz/IDCwvAkfUTMVtMHLSpmcmGkNTi9TyJL0L2xmxMho/ie9B/W6wTXdzGuDW9ujcfhwBgsW+nSMISEukZlRYwsI0zRpAkLnD8/+CLZE022YmLnUBcu6c0LfUgGpdxeIhDFM5GomyCEJnEn6oQXOsPAGB7in8U01EMLOxvQ6FGQzjdC1IQrRlpT8JNXnS5YfFFGbb/o6DdZVez+8lmuQ/k7ueR9UO+lTYOWMct2EXvGLQN3M9LU44kZFKrow5UginpBfKr5/X8wDZtctBrlNeVLqyUqpJuiUjeWCXzsqlrQbLiXZ1agGcXwwJPG+/NWfPi1VpcoZCK4BS+3m+693tLWar/xtqd+5P0a6tHcxKdltUYp5rQN+KkzAtAfuQfl7cshDvCOZFYJImzDx3tYnno+OAv9rR1CIAZXySRCrxJ8/x2/857ztKvLVkQ6+W5sXysGGWY2WFqVF5XsoEQG3a78bZz8/npOqHfWSm+j2buSHjNY9vw33Xh3oTZE8BlaTBlif1tiaprL39l+ZndeouUupitE+a407rmpQq6HJt+BmBha4Put+H2UqLXog4p5x7s+SQJ+qO6wCVTg3amPcOo5gNvKQHpS/I9PbR/PaqVCv15rgR+dGDcpHyAcypY5QjX1Vs5HhNvwQl3O7xf8GqXdB4eQQkzIr0mY2kthNuRQR053c4GNvYpjoAofU5FVNlSL2k1gPwbJ5sTIyOhmJmcOxzK2A== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(136003)(346002)(39860400002)(396003)(376002)(451199021)(36840700001)(46966006)(40470700004)(426003)(336012)(2616005)(47076005)(83380400001)(36860700001)(356005)(82740400003)(40480700001)(86362001)(82310400005)(81166007)(36756003)(40460700003)(478600001)(54906003)(44832011)(4326008)(6666004)(8936002)(8676002)(2906002)(5660300002)(7416002)(7406005)(70206006)(70586007)(6916009)(41300700001)(316002)(16526019)(186003)(1076003)(26005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:35:58.3211 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e5920512-a81a-4d9c-1b6c-08db6afe84bb X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT010.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5074 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Tom Lendacky Add support for AP Reset Hold being invoked using the GHCB MSR protocol, available in version 2 of the GHCB specification. Signed-off-by: Tom Lendacky Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/include/asm/sev-common.h | 2 ++ arch/x86/kvm/svm/sev.c | 56 ++++++++++++++++++++++++++----- arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 9eb20b416251..a4fb53fd15d7 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -56,6 +56,8 @@ /* AP Reset Hold */ #define GHCB_MSR_AP_RESET_HOLD_REQ 0x006 #define GHCB_MSR_AP_RESET_HOLD_RESP 0x007 +#define GHCB_MSR_AP_RESET_HOLD_RESULT_POS 12 +#define GHCB_MSR_AP_RESET_HOLD_RESULT_MASK GENMASK_ULL(51, 0) /* GHCB GPA Register */ #define GHCB_MSR_REG_GPA_REQ 0x012 diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index c25aeb550cd9..b88295aa7124 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -58,6 +58,10 @@ module_param_named(sev_es, sev_es_enabled, bool, 0444); #define sev_es_enabled false #endif /* CONFIG_KVM_AMD_SEV */ +#define AP_RESET_HOLD_NONE 0 +#define AP_RESET_HOLD_NAE_EVENT 1 +#define AP_RESET_HOLD_MSR_PROTO 2 + static u8 sev_enc_bit; static DECLARE_RWSEM(sev_deactivate_lock); static DEFINE_MUTEX(sev_bitmap_lock); @@ -2569,6 +2573,9 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) void sev_es_unmap_ghcb(struct vcpu_svm *svm) { + /* Clear any indication that the vCPU is in a type of AP Reset Hold */ + svm->sev_es.ap_reset_hold_type = AP_RESET_HOLD_NONE; + if (!svm->sev_es.ghcb) return; @@ -2781,6 +2788,22 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_POS); break; } + case GHCB_MSR_AP_RESET_HOLD_REQ: + svm->sev_es.ap_reset_hold_type = AP_RESET_HOLD_MSR_PROTO; + ret = kvm_emulate_ap_reset_hold(&svm->vcpu); + + /* + * Preset the result to a non-SIPI return and then only set + * the result to non-zero when delivering a SIPI. + */ + set_ghcb_msr_bits(svm, 0, + GHCB_MSR_AP_RESET_HOLD_RESULT_MASK, + GHCB_MSR_AP_RESET_HOLD_RESULT_POS); + + set_ghcb_msr_bits(svm, GHCB_MSR_AP_RESET_HOLD_RESP, + GHCB_MSR_INFO_MASK, + GHCB_MSR_INFO_POS); + break; case GHCB_MSR_TERM_REQ: { u64 reason_set, reason_code; @@ -2880,6 +2903,7 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ret = svm_invoke_exit_handler(vcpu, SVM_EXIT_IRET); break; case SVM_VMGEXIT_AP_HLT_LOOP: + svm->sev_es.ap_reset_hold_type = AP_RESET_HOLD_NAE_EVENT; ret = kvm_emulate_ap_reset_hold(vcpu); break; case SVM_VMGEXIT_AP_JUMP_TABLE: { @@ -3040,13 +3064,29 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) return; } - /* - * Subsequent SIPI: Return from an AP Reset Hold VMGEXIT, where - * the guest will set the CS and RIP. Set SW_EXIT_INFO_2 to a - * non-zero value. - */ - if (!svm->sev_es.ghcb) - return; + /* Subsequent SIPI */ + switch (svm->sev_es.ap_reset_hold_type) { + case AP_RESET_HOLD_NAE_EVENT: + /* + * Return from an AP Reset Hold VMGEXIT, where the guest will + * set the CS and RIP. Set SW_EXIT_INFO_2 to a non-zero value. + */ + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, 1); + break; + case AP_RESET_HOLD_MSR_PROTO: + /* + * Return from an AP Reset Hold VMGEXIT, where the guest will + * set the CS and RIP. Set GHCB data field to a non-zero value. + */ + set_ghcb_msr_bits(svm, 1, + GHCB_MSR_AP_RESET_HOLD_RESULT_MASK, + GHCB_MSR_AP_RESET_HOLD_RESULT_POS); - ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, 1); + set_ghcb_msr_bits(svm, GHCB_MSR_AP_RESET_HOLD_RESP, + GHCB_MSR_INFO_MASK, + GHCB_MSR_INFO_POS); + break; + default: + break; + } } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index f44751dd8d5d..50be41fa16a0 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -192,6 +192,7 @@ struct vcpu_sev_es_state { struct ghcb *ghcb; struct kvm_host_map ghcb_map; bool received_first_sipi; + unsigned int ap_reset_hold_type; /* SEV-ES scratch area support */ void *ghcb_sa; From patchwork Mon Jun 12 04:25:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275495 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E951FC7EE23 for ; Mon, 12 Jun 2023 04:37:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234799AbjFLEh1 (ORCPT ); Mon, 12 Jun 2023 00:37:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234564AbjFLEg5 (ORCPT ); Mon, 12 Jun 2023 00:36:57 -0400 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2046.outbound.protection.outlook.com [40.107.100.46]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6E7F1FC6; Sun, 11 Jun 2023 21:36:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aahd68F4z6eqQpVYMaVh/MPQMKXnZj26w881Lwf7SaB75uRx10IY8xu3WA5/FEGSw9ZujGD4lexTM3YT6Ojjo/XSpcLXPTNPMFJporWCTM9jHtNg7/IjCs27kcja0/hHokN0XwjoOvRXeNYldl9L0s/zRpv8857Zw52rDaBevtXqiuh3spAbxHMhIctdjwKbAWQaOMl7Sc7Bmm0keKsM9hX8/k84QgiweANvAJlp3SY9vfIVbeuCrPFm3Qlrz8Pdk5P8kfc7dcjlrPzQPvi1uXu7xG/zDVR3Ov6andOEnMGIU2Ol/vA0Hyub9m/yvktqYVNI/5vVpxlxTlJ0HDoyBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=8zzsL1kp415Jf3ZR08yu8zyJesz2ZChIlRl31hCycZw=; b=a/lPMdcbBRzc4VqfQFS20DGcFuOLpP0uSeDSb1fMhhgprQqbTc28iCpq4CBbas98UmbachZCh67xu9V8IKbrUqO1/vhfl/fslswGCr2XDOri5+TP+BK0gDPB/g6bQBXa7ijEa2Lp2hsKYcsVjuCTixTpxoIY4KawGs72E2FBsilKTxmFxS0eUwLSoXjyGe9hMgb3qWunMpjys7dpx6cZ9NYLdeeaoocaDQUsnwHf2EjzUbivsDVxNhLuIyZU4LQ9umnU0wAEFsSL6VpbaVpHcvo/9r8aMY0gIl5k27FYakIBGc/hH5+/oR1G6HgkpXn9Y06X3bTHWEijmO1IhXiGZw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=8zzsL1kp415Jf3ZR08yu8zyJesz2ZChIlRl31hCycZw=; b=CCLbG9z3lgeW6i036KK2b+Gr/4qgTdYKCS/3bTcpPclaYrO+KVyAzusr2saz/EMG+PX0z5wfCUzKMU27fG72t9NtvLcyC1ozo7gVJ53Dbi8wGYbN1AK+/qL7ZUfKscgQP7MRWjdYpluIENvxp9p0qETLleywb4QY4EakS72cH1Q= Received: from DM6PR06CA0031.namprd06.prod.outlook.com (2603:10b6:5:120::44) by SJ2PR12MB8012.namprd12.prod.outlook.com (2603:10b6:a03:4c7::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.32; Mon, 12 Jun 2023 04:36:21 +0000 Received: from DM6NAM11FT048.eop-nam11.prod.protection.outlook.com (2603:10b6:5:120:cafe::3f) by DM6PR06CA0031.outlook.office365.com (2603:10b6:5:120::44) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:36:20 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT048.mail.protection.outlook.com (10.13.173.114) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:36:20 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:36:19 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 25/51] KVM: SVM: Add GHCB handling for Hypervisor Feature Support requests Date: Sun, 11 Jun 2023 23:25:33 -0500 Message-ID: <20230612042559.375660-26-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT048:EE_|SJ2PR12MB8012:EE_ X-MS-Office365-Filtering-Correlation-Id: 39aef988-d67f-4a0e-fab2-08db6afe9227 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: iBTBvJS9Nb8tlWWoAQnxaDXixIBqez+9IO+HO5ryNUfCBF2WURY5vMhy9JgbF/23bcMk0AfTwEW88A5S5sbysRN4IovXi4gO2GdM15lvoIwI1mbNeA24a728lUOyXzsV+hHotS2g4nJiY1UO/f/0JLabwzp1VmBipz59JA/aoWqJadTdEwoLuriBsrEtq1ZvDNMs+nRAduPQOKS1URtwaqAJiE3ejswPf/D+jPEpsxiDSwNL6uqaPuZNQcJgN9rBgc4nuEwmu1fuAOuyjWTmHL4vSzHlWRkhom56pfQV6QNtahHAqrxb6clq0lXwipp1JPVhPccMw6WrNH1Rx5CQCNjEzxRG80m6cugGj3ayzFTe3nPAXZil3+LmPuYJoxF1aQSWWQ+MdsSp8BskFfOSpLhYp5dOqdO9xqgkFbS2eXt1xzToWFta3hV9A3E+oVhsOyAhThk2iJhBrBVtjILQZ2uX0vvlVU/bq734/b7QKSkxh6kJ0bQ01Up2iDEfI6r0KtrICrgKKWLlhFc+rUzoy91X+pw+gfMc18nqL5gTnDQus3toKW+DXjcFPTS3CVAmCcu4duTUINzchqkxDLuNhh5zHlQomp8d4/RjswOnDbn3jVc4RFmVOOs7Vdi2qjjCSCsMirKNCjW42ABpNrgScgB4oaBEjYFCoroDgPB2Byvh5uLL1mVrpfq0ZEQkmJfxLmBV9DQDrzNjiDo3hhdGB58GW+kZF1F2JreCIQtuc3ITPvIXLzLR9uCrWaKHZbBHVhwoF5Gg/XK/E1vABfvmBg== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(346002)(396003)(376002)(39860400002)(136003)(451199021)(40470700004)(46966006)(36840700001)(316002)(40480700001)(54906003)(36860700001)(40460700003)(82740400003)(356005)(478600001)(81166007)(70586007)(41300700001)(8676002)(8936002)(70206006)(2616005)(4326008)(16526019)(426003)(186003)(336012)(47076005)(6666004)(26005)(83380400001)(1076003)(82310400005)(7406005)(86362001)(7416002)(5660300002)(44832011)(2906002)(6916009)(36756003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:36:20.8606 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 39aef988-d67f-4a0e-fab2-08db6afe9227 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT048.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB8012 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh Version 2 of the GHCB specification introduced advertisement of features that are supported by the Hypervisor. Now that KVM supports version 2 of the GHCB specification, bump the maximum supported protocol version. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/include/asm/sev-common.h | 2 ++ arch/x86/kvm/svm/sev.c | 14 ++++++++++++++ arch/x86/kvm/svm/svm.h | 3 ++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index a4fb53fd15d7..aaea4afcda98 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -101,6 +101,8 @@ enum psc_op { /* GHCB Hypervisor Feature Request/Response */ #define GHCB_MSR_HV_FT_REQ 0x080 #define GHCB_MSR_HV_FT_RESP 0x081 +#define GHCB_MSR_HV_FT_POS 12 +#define GHCB_MSR_HV_FT_MASK GENMASK_ULL(51, 0) #define GHCB_MSR_HV_FT_RESP_VAL(v) \ /* GHCBData[63:12] */ \ (((u64)(v) & GENMASK_ULL(63, 12)) >> 12) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index b88295aa7124..2bceb0060880 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2538,6 +2538,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_HV_FEATURES: break; default: reason = GHCB_ERR_INVALID_EVENT; @@ -2804,6 +2805,13 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS); break; + case GHCB_MSR_HV_FT_REQ: { + set_ghcb_msr_bits(svm, GHCB_HV_FT_SUPPORTED, + GHCB_MSR_HV_FT_MASK, GHCB_MSR_HV_FT_POS); + set_ghcb_msr_bits(svm, GHCB_MSR_HV_FT_RESP, + GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS); + break; + } case GHCB_MSR_TERM_REQ: { u64 reason_set, reason_code; @@ -2928,6 +2936,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ret = 1; break; } + case SVM_VMGEXIT_HV_FEATURES: { + ghcb_set_sw_exit_info_2(ghcb, GHCB_HV_FT_SUPPORTED); + + ret = 1; + break; + } case SVM_VMGEXIT_UNSUPPORTED_EVENT: vcpu_unimpl(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 50be41fa16a0..1ab117daebd9 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -711,9 +711,10 @@ void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu); /* sev.c */ -#define GHCB_VERSION_MAX 1ULL +#define GHCB_VERSION_MAX 2ULL #define GHCB_VERSION_MIN 1ULL +#define GHCB_HV_FT_SUPPORTED GHCB_HV_FT_SNP extern unsigned int max_sev_asid; From patchwork Mon Jun 12 04:25:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275496 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 32C50C7EE23 for ; Mon, 12 Jun 2023 04:38:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234127AbjFLEiD (ORCPT ); Mon, 12 Jun 2023 00:38:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42080 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234564AbjFLEhn (ORCPT ); Mon, 12 Jun 2023 00:37:43 -0400 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2068.outbound.protection.outlook.com [40.107.102.68]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61F5F1BF6; Sun, 11 Jun 2023 21:37:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AnPXLP0mY1IeMJpSHgR3nv6D5WhJcPcVvrpavYsQ9F7g6y1NWdSbJbP5B8PH5BBqMlvJhWIB/+yZYhnyJUle5owBzSc/LK5UqoP5QBE7OpTeGJlXAZbXlslFpciGETcLTh91gdiL9WGU/PO1FYSl3HI6WNun0YPSeSnRzwHcmS/DNCagHi0qP7l+YB7+x0vCRwa3ikiwmf55Qpt8BT5G0W/82yJlahz15j4Xk5WFvPzOCLCamkh10bcT9M1unVdVyN6M/Q4y1XxmNSNXXl0dRrC0X7e4907+BgLDoipkjl2zGOU+l8DbycrOACs2RWIdccg7swdbR7jWgXwXNQgwJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=HAG99mlA6jc200AafFpw4ErvSud5hXObZsDWDpLaHt8=; b=i3blII3YZx3zWIEPgNfJdlH9FuFxY7O2hmnK/6palLgW9uS8Wedo9WO8D421MhX2uyO7CXxCndskqn3rMr8JAycHvQCrTnHpNMJKipjiYHE08ntcP97sTd5wxqQg8TxWFT5spwSObtq1bJn4Tb656X2Ei0TmdZ+rfN4BPjCsEmM8e81c8xR0xStNzo+cjfe4dUqH7d6/ELi34x0lApqhfW0rkQ6GAMhJZRTo57OWPn1v78ifsr/nXt71Eqn12mDH0Ml9vbVTGtGF6TJxWA2DQA5AS4rM70u11PyXLr4RQ1tV57Qw7IZh51JNXt1miUiLwq/kGKjTl9uv2lcVDL6sJw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=HAG99mlA6jc200AafFpw4ErvSud5hXObZsDWDpLaHt8=; b=WQNV7Hua977WTx8KoyO5Q2uQCtsNqbn4NYF8Ms32YN/G4Mw9MADZwtIo2mJQQxqJDhVDCv+MGWVJfpX1KM2IefFQXPx5JPpOwK5axvcENWdvXBtXRNd79KHEfu9GjrXUpHebQ6CdUU085RO6jidmOkbhjwZ6Z+j55SKUH7QaR3Q= Received: from DM6PR04CA0005.namprd04.prod.outlook.com (2603:10b6:5:334::10) by SJ0PR12MB5674.namprd12.prod.outlook.com (2603:10b6:a03:42c::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.33; Mon, 12 Jun 2023 04:37:04 +0000 Received: from DM6NAM11FT083.eop-nam11.prod.protection.outlook.com (2603:10b6:5:334:cafe::41) by DM6PR04CA0005.outlook.office365.com (2603:10b6:5:334::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:37:04 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT083.mail.protection.outlook.com (10.13.173.97) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:37:03 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:37:03 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 26/51] KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe Date: Sun, 11 Jun 2023 23:25:34 -0500 Message-ID: <20230612042559.375660-27-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT083:EE_|SJ0PR12MB5674:EE_ X-MS-Office365-Filtering-Correlation-Id: 923afc12-2499-46bf-bd5a-08db6afeabd7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 5leXYoIZh7AnkZJ9YUvuvDQlcQ73AB41RhQg7fgqFaY/ZFgVZWq3/SWB7aWWydQx5md3WHFh6rFR1bYg7RB06xQinD7mrqnD9mHIHp+RZ2b2LYwZzz8Y2V6R0MWcLmO20210WMF9xQ4hafqmlOlTvhsDk1/hyb8DF1w7Gpo4b3vPY37c5A8zy5xxYmrRm7YA5eLFGeNdzCfykO104SYdvIsD12/n9+C+Sq9UdiZCjB35+yqiLDZYLYfPEPMtImd03i8b8HVoimA9KBtHvFu4dqcaW7Tde6cLswqjHxSNhtD9ezdklPoltv8ilJMoJIxiTCy5TJqNySfnP/KVsFifXoTnTbd3DpLJIhR2m06nibFQTSe+vFqd3ZlD3PgTcWCPJbW4zPS5iRSpaUA8mOBClZcVOXZDZj58SoZKvriWcrpe83ndfoB5avOXvGmWRwSeB5jtOBP9/6MEx5JANbTOUIlTPiG+c3CjP8VrHF0LzdtPBUBzZjXEgBLzgSVVGPodSilHKrvQ0qIXZJCkNKiTjO7Canjt/MVSaQJFOs7n94Z4Ysm/jUh6LS/0F7ITecfK7zmgaW261cfZJnLZ2fe71GUlSTSrYd0QwPQm6eJ+hzllj7nIRXLY/yhZKzcwkSFzRpLLSfAMofC8ZpV+q3RfKMkiP6gjmoljsQU3qCFC1GyiCZMVlQeYeVaTbxrpC80Au4XJp2RCBC1/ap84OJJFX7bDqsB4CSipLqTIBPzIcBVOKL2DZ4oHKiSlajNH57ECidQ4c/QpbgIMu+5JlgV3VQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(346002)(39860400002)(376002)(136003)(396003)(451199021)(40470700004)(36840700001)(46966006)(40460700003)(1076003)(26005)(16526019)(40480700001)(186003)(41300700001)(426003)(5660300002)(82310400005)(47076005)(36756003)(36860700001)(7406005)(7416002)(44832011)(70586007)(4326008)(70206006)(8936002)(6666004)(83380400001)(336012)(478600001)(8676002)(2616005)(54906003)(2906002)(86362001)(6916009)(356005)(316002)(81166007)(82740400003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:37:03.9399 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 923afc12-2499-46bf-bd5a-08db6afeabd7 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT083.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB5674 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh Implement a workaround for an SNP erratum where the CPU will incorrectly signal an RMP violation #PF if a hugepage (2mb or 1gb) collides with the RMP entry of a VMCB, VMSA or AVIC backing page. When SEV-SNP is globally enabled, the CPU marks the VMCB, VMSA, and AVIC backing pages as "in-use" via a reserved bit in the corresponding RMP entry after a successful VMRUN. This is done for _all_ VMs, not just SNP-Active VMs. If the hypervisor accesses an in-use page through a writable translation, the CPU will throw an RMP violation #PF. On early SNP hardware, if an in-use page is 2mb aligned and software accesses any part of the associated 2mb region with a hupage, the CPU will incorrectly treat the entire 2mb region as in-use and signal a spurious RMP violation #PF. The recommended is to not use the hugepage for the VMCB, VMSA or AVIC backing page for similar reasons. Add a generic allocator that will ensure that the page returns is not hugepage (2mb or 1gb) and is safe to be used when SEV-SNP is enabled. Also implement similar handling for the VMCB/VMSA pages of nested guests. Co-developed-by: Marc Orr Signed-off-by: Marc Orr Reported-by: Alper Gun # for nested VMSA case Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Signed-off-by: Brijesh Singh [mdr: squash in nested guest handling from Ashish] Signed-off-by: Michael Roth --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/lapic.c | 5 ++++- arch/x86/kvm/svm/nested.c | 2 +- arch/x86/kvm/svm/sev.c | 33 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 17 ++++++++++++--- arch/x86/kvm/svm/svm.h | 1 + 7 files changed, 55 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index 48f043de2ec0..28456b497198 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -135,6 +135,7 @@ KVM_X86_OP(vcpu_deliver_sipi_vector) KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL_RET0(gmem_prepare) KVM_X86_OP_OPTIONAL(gmem_invalidate) +KVM_X86_OP_OPTIONAL(alloc_apic_backing_page) #undef KVM_X86_OP #undef KVM_X86_OP_OPTIONAL diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index c26f76641121..8d2bb3ff66a2 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1743,6 +1743,7 @@ struct kvm_x86_ops { int (*gmem_prepare)(struct kvm *kvm, struct kvm_memory_slot *slot, kvm_pfn_t pfn, gfn_t gfn, u8 *max_level); void (*gmem_invalidate)(struct kvm *kvm, kvm_pfn_t start, kvm_pfn_t end); + 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 e542cf285b51..94311938651a 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2769,7 +2769,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 = static_call(kvm_x86_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/nested.c b/arch/x86/kvm/svm/nested.c index 96936ddf1b3c..fb981c8b82c4 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -1185,7 +1185,7 @@ int svm_allocate_nested(struct vcpu_svm *svm) if (svm->nested.initialized) return 0; - vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmcb02_page = snp_safe_alloc_page(&svm->vcpu); if (!vmcb02_page) return -ENOMEM; svm->nested.vmcb02.ptr = page_address(vmcb02_page); diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 2bceb0060880..69b57e8f0a7f 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3104,3 +3104,36 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) break; } } + +struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) +{ + unsigned long pfn; + struct page *p; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); + + /* + * Allocate an SNP safe page to workaround the SNP erratum where + * the CPU will incorrectly signal an RMP violation #PF if a + * hugepage (2mb or 1gb) collides with the RMP entry of VMCB, VMSA + * or AVIC backing page. The recommeded workaround is to not use the + * hugepage. + * + * Allocate one extra page, use a page which is not 2mb aligned + * and free the other. + */ + p = alloc_pages(GFP_KERNEL_ACCOUNT | __GFP_ZERO, 1); + if (!p) + return NULL; + + split_page(p, 1); + + pfn = page_to_pfn(p); + if (IS_ALIGNED(pfn, PTRS_PER_PMD)) + __free_page(p++); + else + __free_page(p + 1); + + return p; +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index eb308c9994f9..065167b42f90 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -668,7 +668,7 @@ static int svm_cpu_init(int cpu) int ret = -ENOMEM; memset(sd, 0, sizeof(struct svm_cpu_data)); - sd->save_area = alloc_page(GFP_KERNEL | __GFP_ZERO); + sd->save_area = snp_safe_alloc_page(NULL); if (!sd->save_area) return ret; @@ -1381,7 +1381,7 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu) svm = to_svm(vcpu); err = -ENOMEM; - vmcb01_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmcb01_page = snp_safe_alloc_page(vcpu); if (!vmcb01_page) goto out; @@ -1390,7 +1390,7 @@ static int svm_vcpu_create(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; @@ -4770,6 +4770,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 = { .name = KBUILD_MODNAME, @@ -4900,6 +4910,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, .vcpu_get_apicv_inhibit_reasons = avic_vcpu_get_apicv_inhibit_reasons, + .alloc_apic_backing_page = svm_alloc_apic_backing_page, }; /* diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 1ab117daebd9..e45b54e95495 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -741,6 +741,7 @@ void sev_es_vcpu_reset(struct vcpu_svm *svm); void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector); void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa); void sev_es_unmap_ghcb(struct vcpu_svm *svm); +struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu); /* vmenter.S */ From patchwork Mon Jun 12 04:25:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275497 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 535C9C7EE25 for ; Mon, 12 Jun 2023 04:39:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234867AbjFLEjF (ORCPT ); Mon, 12 Jun 2023 00:39:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234701AbjFLEin (ORCPT ); Mon, 12 Jun 2023 00:38:43 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2078.outbound.protection.outlook.com [40.107.93.78]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7BA83173D; Sun, 11 Jun 2023 21:37:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lfJ4DMH5UeL4MLfR70vtA0ZBekZIPM9Vtivv1eQTZrVm2G1KNpXlh+Hpqbhc3wou4StxEiJFjpJtUCiyLDV0QUHmBlRLpmVHoCxrTkXFF0npzq7lrF8yS7eQG34y3JtYTDXqcBax/L8ji+P2K9JSyASNTmhEbfZ22t29Gpbdqg2xUsCEY7oHIUrV42eAesnTxRTuHKs7TXve8qdb+v3ownwMbE/WqUZqFOgBiG/S6/C09yYF0TbQfzpDgsJ3Otohl4Kmm42I09ourWftwPYGRP9Amvoie0pUZyy2whCQI4Pfxpycp8+3kW08Gy3o+I66j6RQ4CRDN4wAcFiB93+xSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=pOpw3KzGxdOjwJ/TkrcnFihbFz41wbR17m9Hf+7f9aQ=; b=kw9r0XvrmMdG+rFbrGD2NOq4pmQXK0/ozB/oM+UBe2+rg7L/Rp72lRPhL8RSDpSYCVGE9Ps0uWE6sQhlBLSVqr/MG552tp9TryYd4I3ZChIaREQb1NUc7w1zpzHGj/VNUMq/eczCU2iSX97fQKbMG3mWkLMh2fqPw4U+y3tqK0JV1gK1ywlbCVHnhQ7F9U+9y/9Ljs+BcxdtwgViHIYYYSKGShkLTZlKNHlgVOqcVnpyFOVwNnmuxCOq3gVw0hTkzLJ2HYxG2a7gse6VdDhgif66FrUJZU8D2ymSh8zSpL8YmSBkYsxQBPjAZoXyGPl0YJQKBZYIE0Z1/MA39BGFZg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=pOpw3KzGxdOjwJ/TkrcnFihbFz41wbR17m9Hf+7f9aQ=; b=1g1eaNBGzrRncDD0LAr1f9sRtbUZYIoFWbWXpGADs9AoNjVFcJBfFR0b1Hm8aFS+gny5nHSsjfwdbtvZRJpe6PCv9hXRF+5BOfU0E4z+XLXjN2JDV4hZRmZsIutTjy90e6gu86YVfmQhUp19lokKGD+k8chuoCYVYs1TUhWXAMo= Received: from MW4PR04CA0148.namprd04.prod.outlook.com (2603:10b6:303:84::33) by SJ2PR12MB7823.namprd12.prod.outlook.com (2603:10b6:a03:4c9::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.33; Mon, 12 Jun 2023 04:37:52 +0000 Received: from CO1PEPF000042AC.namprd03.prod.outlook.com (2603:10b6:303:84:cafe::cc) by MW4PR04CA0148.outlook.office365.com (2603:10b6:303:84::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:37:52 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1PEPF000042AC.mail.protection.outlook.com (10.167.243.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.13 via Frontend Transport; Mon, 12 Jun 2023 04:37:52 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:37:50 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 27/51] KVM: SVM: Add initial SEV-SNP support Date: Sun, 11 Jun 2023 23:25:35 -0500 Message-ID: <20230612042559.375660-28-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000042AC:EE_|SJ2PR12MB7823:EE_ X-MS-Office365-Filtering-Correlation-Id: 2a304f1d-13d0-4bf6-408d-08db6afec899 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: uLxOKIpZMWOThwXCbr064xUgH6E6BEXvwurUtUUJE6y8Xyh3Ns+MGwSrTLe2xDOBDlNlNpDhqzQM2ox38G2h45Slq50VWTbYTks1w/1NHzNqG2TId4ITzuON1LozCGV7g5A2eLfx6ybroQKMJFHG58oy4B87yx9k2vvCKl2uVIjuT2x9Qi9xYZccGSm+dfi/1kq6LACuFvjvCF6E5kpf7UwWejtJLvMPV1XHw9Wc9Ee5di9Z7Xb3qbgRuCvJjBbGuiy7JoyQZr2nwgl91Iy7e9Es4unsup6Dx2N8LYk5tXgfVHz0vvSZSVv+/lsYs5G2xPmxvBX3ELbp1q3Kv2oPFBsdyl89zfRBXYvRQZI7qX4sOZk+URGizZzTdIcLeljBipQikoCd62+e92Wx3M3B6Z4lvj4zQgtUsk4YlJFPmvA3iYLr0GvfZITr/p5F2lQRbjiMUjOz1lFvkzb3VKtZi7jTOgQMeMd46PE0L4WlfDo1y5PJznczKj4h/5oiBYW7vdshghfPrxgeH394eKuFiRKMrshDcOvZ1aoEde2LKJByWLUrS+YoQ0OKWQpPfqH/oogsqr5O1XVODSZrh0gdAy4XHID+tzKf+EzMQ4Kfz2EyLeuKdPlgcPL2g6bbrEeKK9q8JQuXRjVY/q7wEY4pcYf1al5kYfIOZJ6nBo6NsKKOrVPJ3PbsDXOTgQLl9b8mQJJYKwx3K8o8a/UviNZ4VKPcKAMA4/Ud+50moDtDSzvlcZrzbYpIaXiDQ/aPUv06KeNlyrtCdWdJ0R5bl589hA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(376002)(346002)(136003)(396003)(451199021)(36840700001)(40470700004)(46966006)(70206006)(70586007)(5660300002)(8936002)(8676002)(36756003)(4326008)(6666004)(6916009)(54906003)(478600001)(40460700003)(41300700001)(316002)(81166007)(356005)(40480700001)(82740400003)(336012)(426003)(16526019)(26005)(186003)(44832011)(1076003)(47076005)(7416002)(82310400005)(7406005)(2616005)(83380400001)(86362001)(36860700001)(2906002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:37:52.1218 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2a304f1d-13d0-4bf6-408d-08db6afec899 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1PEPF000042AC.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB7823 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh 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 is added incrementally, the later patches adds a new module parameters that can be used to enabled SEV-SNP in the KVM. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 10 +++++++++- arch/x86/kvm/svm/svm.h | 8 ++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 69b57e8f0a7f..f5fcf6c33583 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -58,6 +58,9 @@ module_param_named(sev_es, sev_es_enabled, bool, 0444); #define sev_es_enabled false #endif /* CONFIG_KVM_AMD_SEV */ +/* enable/disable SEV-SNP support */ +static bool sev_snp_enabled; + #define AP_RESET_HOLD_NONE 0 #define AP_RESET_HOLD_NAE_EVENT 1 #define AP_RESET_HOLD_MSR_PROTO 2 @@ -2169,6 +2172,7 @@ void __init sev_hardware_setup(void) { #ifdef CONFIG_KVM_AMD_SEV unsigned int eax, ebx, ecx, edx, sev_asid_count, sev_es_asid_count; + bool sev_snp_supported = false; bool sev_es_supported = false; bool sev_supported = false; @@ -2248,12 +2252,16 @@ void __init sev_hardware_setup(void) if (misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count)) goto out; - pr_info("SEV-ES supported: %u ASIDs\n", sev_es_asid_count); sev_es_supported = true; + sev_snp_supported = sev_snp_enabled && cpu_feature_enabled(X86_FEATURE_SEV_SNP); + + pr_info("SEV-ES %ssupported: %u ASIDs\n", + sev_snp_supported ? "and SEV-SNP " : "", sev_es_asid_count); out: sev_enabled = sev_supported; sev_es_enabled = sev_es_supported; + sev_snp_enabled = sev_snp_supported; #endif } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index e45b54e95495..6974d63c84f9 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -77,6 +77,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 */ @@ -346,6 +347,13 @@ static __always_inline bool sev_es_guest(struct kvm *kvm) #endif } +static __always_inline bool sev_snp_guest(struct kvm *kvm) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + + return sev_es_guest(kvm) && sev->snp_active; +} + static inline void vmcb_mark_all_dirty(struct vmcb *vmcb) { vmcb->control.clean = 0; From patchwork Mon Jun 12 04:25:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275498 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3B773C7EE25 for ; Mon, 12 Jun 2023 04:39:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234901AbjFLEje (ORCPT ); Mon, 12 Jun 2023 00:39:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41288 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234377AbjFLEjC (ORCPT ); Mon, 12 Jun 2023 00:39:02 -0400 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2051.outbound.protection.outlook.com [40.107.223.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB7B72106; Sun, 11 Jun 2023 21:38:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YHL5ICaabVRjAtFzcxY053uJmWnjEzsrQBW/k5LAsbQ4j+ITZtst/FTpBzGd+8K7p2QGrOIhaxIR2JxSk/IsMHd3BGISdGs51ZdP0sCBBDvaFh0YckBTFLEmEo9yezdUk4iIBgp5JCXW1Ny4ob2kci1rae5qSPEXuonkFPb0MKgNrBBkc3ro2YMkDOppf+I6YOAE1wG3tO6karPwXfa7useTjdmb0xLgnKHHfr4Kh5xLwORCrPXl4CQIGw0wSxshnedPdQ2Xl9l7SmjevqPqZTpHi4dOWKxuvCUIbGEtH0903IioZbMdDJOS4IhfPVLSsvVDnn3UevNezwRkivt2qw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=4q6MewkvTsxk63o0PqCssBYpMxPC1G6Szq/4zLuo80A=; b=cF0+QqqnKsL00Jywi98sTgNIu6kW+9SRx8/MbmocxUL6op/yo1vVhf1DL1ptyR077CmcvigROzJ5l8PkV+LKVHt6pKMcOP6Bj/UiDRPkDMlanj5LDpmmN0mnES6OIPgAxebGk3wyILJuS49qClQ0t5z8VDnQBp0hf++atmiJLM/TpmdsMwSgtrrruUrhqQc3AXjE8L7gchjk8RkA+xO2fwNCxoHiYebS+5wJNd77Rmy+sDjM2SZu3EujOqhahpXt3Vg2Gc6kRRHNIth8VCrzDOX8VKD3Pqd2cCsqwtdEBl5thsuyUAdk1Qqa57FXENsLKCuVvTxBbexB/mz/7BVerw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=4q6MewkvTsxk63o0PqCssBYpMxPC1G6Szq/4zLuo80A=; b=tVvWTGPEeP659g2BnR94YvY6clV1ysEmh3bpN/H79jqHKFPDacn9UDEPtRbdJ9PyydBoNWoa+ZjMZAmIV01QDTiS9ovlFi+n3bd33ZfozMtUO0wcfbRNMbW/GXEH4ap8MWpoftLv/g5TP2ovb4isnYgh8T8tkukv52LaMaWAu+s= Received: from MW2PR16CA0033.namprd16.prod.outlook.com (2603:10b6:907::46) by DS0PR12MB8344.namprd12.prod.outlook.com (2603:10b6:8:fe::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.33; Mon, 12 Jun 2023 04:38:13 +0000 Received: from CO1PEPF000042A8.namprd03.prod.outlook.com (2603:10b6:907:0:cafe::aa) by MW2PR16CA0033.outlook.office365.com (2603:10b6:907::46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:38:13 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1PEPF000042A8.mail.protection.outlook.com (10.167.243.37) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:38:13 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:38:12 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Pavan Kumar Paluri Subject: [PATCH RFC v9 28/51] KVM: SVM: Add KVM_SNP_INIT command Date: Sun, 11 Jun 2023 23:25:36 -0500 Message-ID: <20230612042559.375660-29-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000042A8:EE_|DS0PR12MB8344:EE_ X-MS-Office365-Filtering-Correlation-Id: b2939f41-0cc9-441f-db32-08db6afed560 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 27Vuipl4yrJpshaqF8PFVYj9+w+eXxN+OWiDN6fAAI6dufeGUf5sziAbtcCvJbyHNdjClkNBk+9/D6Wx6m7x0wrGZe4RK7xvfga9ui0AtCCXdiHz8wa2/MbUx7GKeQfiab9CjtH/1EqY0hagvr3y3nWTMaYBbudGK2pNAXiN6VULUR6YZO1SifyjLMcgt66cDsvaNmaoYgIhwl/5Wi/npYUfM6HeSCfjw9rR3VPyENVLIbKm3Hk04m452uuu3g69CoqH2wO/BvhjnPTJzCO7+FrRlViY8brtGL2VeMgxT6ka0uJSLtOfac7ii8jiNLfNWdXBOfe6ruzAdU2a3cyBvS1L9hufVTzxl7FpV2KqijLqgalR3GnQtLkn7lLAs5nKJc/nmXiqYcEgs9RTUedUHNi3uwoOtDS0cq2qBxmg/vW6+YLyM1gkef5+tCkF5hhebjIeTXWGo6ogvb+XqIn334v6goW2caf8o5jOFoKSkpDfuG3Zgam1gvlRdsH3E5G3C0fJ9CgFixR5KyuzwXDKIw++LN2AqZb5jl3H0EXdkgYV1nF7CXc2Fofy3L48NTNhTS05g3FvSNUE3nzqH6r7q/BTpwppI7wrrXfaM/Uv/BeR0A9ywD4L4n3E5UNsZ+5/Cr8UYIaohfDHFoS4K3AWITvMi3zRCORkFIPqzuNG3nOCe6dPtc98xRkJwHI07u1Ftu1MXmSy5E2ytD+GuuJc0ssZ9w7m1sxEiHLm2Chk3jPfdJgnPNjB+1dkviX1mhUlY5P6uUb8WNG0GwiXfn4z4Q== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(346002)(376002)(396003)(39860400002)(136003)(451199021)(36840700001)(46966006)(40470700004)(5660300002)(7406005)(7416002)(70586007)(8936002)(8676002)(316002)(40480700001)(70206006)(6916009)(4326008)(41300700001)(54906003)(44832011)(2906002)(40460700003)(356005)(82740400003)(26005)(86362001)(81166007)(16526019)(1076003)(186003)(36756003)(36860700001)(336012)(426003)(2616005)(478600001)(82310400005)(47076005)(83380400001)(6666004)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:38:13.5782 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b2939f41-0cc9-441f-db32-08db6afed560 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1PEPF000042A8.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8344 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh 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. The flags value must be zero, it will be extended in future SNP support to communicate the optional features (such as restricted INT injection etc). Co-developed-by: Pavan Kumar Paluri Signed-off-by: Pavan Kumar Paluri Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- .../virt/kvm/x86/amd-memory-encryption.rst | 27 +++++++++++++ arch/x86/include/asm/svm.h | 1 + arch/x86/kvm/svm/sev.c | 39 ++++++++++++++++++- arch/x86/kvm/svm/svm.h | 4 ++ include/uapi/linux/kvm.h | 13 +++++++ 5 files changed, 83 insertions(+), 1 deletion(-) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index 487b6328b3e7..1240d28badd6 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -434,6 +434,33 @@ issued by the hypervisor to make the guest ready for execution. Returns: 0 on success, -negative on error +18. KVM_SNP_INIT +---------------- + +The KVM_SNP_INIT command can be used by the hypervisor to initialize SEV-SNP +context. In a typical workflow, this command should be the first command issued. + +Parameters (in/out): struct kvm_snp_init + +Returns: 0 on success, -negative on error + +:: + + struct kvm_snp_init { + __u64 flags; + }; + +The flags bitmap is defined as:: + + /* enable the restricted injection */ + #define KVM_SEV_SNP_RESTRICTED_INJET (1<<0) + + /* enable the restricted injection timer */ + #define KVM_SEV_SNP_RESTRICTED_TIMER_INJET (1<<1) + +If the specified flags is not supported then return -EOPNOTSUPP, and the supported +flags are returned. + References ========== diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index e7c7379d6ac7..ac8edfdd60fa 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -288,6 +288,7 @@ static_assert((X2AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == X2AVIC_ #define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF) +#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) struct vmcb_seg { u16 selector; diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index f5fcf6c33583..70e0576a32d0 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -243,6 +243,25 @@ static void sev_unbind_asid(struct kvm *kvm, unsigned int handle) sev_decommission(handle); } +static int verify_snp_init_flags(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_snp_init params; + int ret = 0; + + if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) + return -EFAULT; + + if (params.flags & ~SEV_SNP_SUPPORTED_FLAGS) + ret = -EOPNOTSUPP; + + params.flags = SEV_SNP_SUPPORTED_FLAGS; + + if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(params))) + ret = -EFAULT; + + return ret; +} + static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) { struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; @@ -256,12 +275,19 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) return ret; sev->active = true; - sev->es_active = argp->id == KVM_SEV_ES_INIT; + sev->es_active = (argp->id == KVM_SEV_ES_INIT || argp->id == KVM_SEV_SNP_INIT); + sev->snp_active = argp->id == KVM_SEV_SNP_INIT; asid = sev_asid_new(sev); if (asid < 0) goto e_no_asid; sev->asid = asid; + if (sev->snp_active) { + ret = verify_snp_init_flags(kvm, argp); + if (ret) + goto e_free; + } + ret = sev_platform_init(&argp->error); if (ret) goto e_free; @@ -277,6 +303,7 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) sev_asid_free(sev); sev->asid = 0; e_no_asid: + sev->snp_active = false; sev->es_active = false; sev->active = false; return ret; @@ -612,6 +639,10 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) save->xss = svm->vcpu.arch.ia32_xss; save->dr6 = svm->vcpu.arch.dr6; + /* Enable the SEV-SNP feature */ + if (sev_snp_guest(svm->vcpu.kvm)) + save->sev_features |= SVM_SEV_FEAT_SNP_ACTIVE; + pr_debug("Virtual Machine Save Area (VMSA):\n"); print_hex_dump_debug("", DUMP_PREFIX_NONE, 16, 1, save, sizeof(*save), false); @@ -1864,6 +1895,12 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) } switch (sev_cmd.id) { + case KVM_SEV_SNP_INIT: + if (!sev_snp_enabled) { + r = -ENOTTY; + goto out; + } + fallthrough; case KVM_SEV_ES_INIT: if (!sev_es_enabled) { r = -ENOTTY; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 6974d63c84f9..4360cf04f53a 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -74,6 +74,9 @@ enum { /* TPR and CR2 are always written before VMRUN */ #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2)) +/* Supported init feature flags */ +#define SEV_SNP_SUPPORTED_FLAGS 0x0 + struct kvm_sev_info { bool active; /* SEV enabled guest */ bool es_active; /* SEV-ES enabled guest */ @@ -89,6 +92,7 @@ struct kvm_sev_info { struct list_head mirror_entry; /* Use as a list entry of mirrors */ struct misc_cg *misc_cg; /* For misc cgroup accounting */ atomic_t migration_in_progress; + u64 snp_init_flags; }; struct kvm_svm { diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 0fa665e8862a..43b6291e3a80 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1926,6 +1926,9 @@ enum sev_cmd_id { /* Guest Migration Extension */ KVM_SEV_SEND_CANCEL, + /* SNP specific commands */ + KVM_SEV_SNP_INIT, + KVM_SEV_NR_MAX, }; @@ -2022,6 +2025,16 @@ struct kvm_sev_receive_update_data { __u32 trans_len; }; +/* enable the restricted injection */ +#define KVM_SEV_SNP_RESTRICTED_INJET (1 << 0) + +/* enable the restricted injection timer */ +#define KVM_SEV_SNP_RESTRICTED_TIMER_INJET (1 << 1) + +struct kvm_snp_init { + __u64 flags; +}; + #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 Mon Jun 12 04:25:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275514 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 98495C7EE2E for ; Mon, 12 Jun 2023 04:40:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229601AbjFLEkp (ORCPT ); Mon, 12 Jun 2023 00:40:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234734AbjFLEjf (ORCPT ); Mon, 12 Jun 2023 00:39:35 -0400 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2068.outbound.protection.outlook.com [40.107.220.68]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25A8510E4; Sun, 11 Jun 2023 21:38:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YEpM1EOxCyk88+trB8HWjrvLcTORIYv+IcP06jJKjEUgFXu4FEgPMa2bgbCi8Co3p54G+20ZSaoU2i5yfLP19lG7zyGhiM/4sGTzw1DHhjzjBnlxIfIAUp16iGHjCIF9hTAcZ1/SvCTfSnY1bJFL/DeA53lJ5+esQbjZALzIzRdPG+7yf4Bby6dchR7Lp8GqBkpz1iSDBwZIj9GptpObDnr09v3PLWqxaCtT6ZOlX7j08yN8Vt9h4o38r46cQ62mVmodAnVb4eI8V0jgh4lraY55/G1zM9LmkIKVhmpBlocWMNxPalMQAK6kPNe6GmBnzORomRJzGpxB/5VFFCbaKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=QK4QZkCIIAqJ+tunV1KOANKN1pjYrjaWRxkGX4t4nd0=; b=ASLk7X9syfErP8FD29sbFJFFnsCMSVR+08/vaD3ZcxD25IBf9tU9h1uqQAl25wtebXnI23MRvJRmoOgVfLtL5TyHHqKv8KU99+/KUDJ+hsESYUjN0jxmyPMZSRE7dFxkPlwYosADwX+FCYNL6ZKQKStvX0BOmZHD42M0brzlbr5v5BwQl/fRw7OdWUylve3gmFuvYg1NJU0t93JG+PxSeg2jmRQrxzJCTDvrarUjjEwUCmtSbtR7kigakfuaWXUGmr5aLZeMYgMLDQBa/6csAdH4hE3RS4v3kTwZFJPaDjwiwoatemC8pj51J1UBzLCj9WRpKIMTpk0cFwBJ9GdXTA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=QK4QZkCIIAqJ+tunV1KOANKN1pjYrjaWRxkGX4t4nd0=; b=afUNk6L31NJMbNfF6fYryZHs+mSM50U3Eok1IJvt56r9mIuCKzs36SKhz7VALt7syEmtIcTSdmMQwPTTBppTfGfEz+pvZlHagckJG3hLSGPIoP+PpmfEvAfvDWhctSXqcx5QpU5c3SdGllzEFmrgnRsYRiP/8xq0YP3/8xgkE3w= Received: from MW4PR04CA0238.namprd04.prod.outlook.com (2603:10b6:303:87::33) by SJ1PR12MB6337.namprd12.prod.outlook.com (2603:10b6:a03:456::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.41; Mon, 12 Jun 2023 04:38:45 +0000 Received: from CO1PEPF000042A7.namprd03.prod.outlook.com (2603:10b6:303:87:cafe::cf) by MW4PR04CA0238.outlook.office365.com (2603:10b6:303:87::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:38:45 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1PEPF000042A7.mail.protection.outlook.com (10.167.243.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:38:45 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:38:40 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 29/51] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_START command Date: Sun, 11 Jun 2023 23:25:37 -0500 Message-ID: <20230612042559.375660-30-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000042A7:EE_|SJ1PR12MB6337:EE_ X-MS-Office365-Filtering-Correlation-Id: 85b63579-e684-40b1-ee7d-08db6afee869 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 0UHqzijJkBtN2Q3L393p5DofWlEQvqPkjdFfojVk4esCIMk4+jpl5FSbMTDcJgVha91KpugKqFcjCdDO0stpM53ATPVmo5xt2/kGJcAPgwLwAaupemD7Pqzf4/Yuz4FG/vBZm8SSGuTO/F+6qgLhxyFyK2HBtA1Q1vFHPMd878O7bJlfCIkmFaOroDByv0R+vWTLyT937u5wV7+gM6Y+RIpdNqWyMJiFfa+UeG7NcMXYiSG+vu11rqXtWsqN1zBoCJ2mPVMpeKreJ+oa4MzoYO5plO6fZFPTMCHs3wXgj+EAosK87cVOAXITlFh12LNyHfeAgL+kazn4HbG01OyRZU84fj+LqPT6p99f48Cp5yO/gTUOliNpatfmCVUJ1li0UlGR8whH6NEJ7opNZzJ4lXFhMtG2npEAPKRosJJxjwaOFhBfJHW0QZtZL1QboZwS6M2OiTH/K8V9fqchzcXTr5RQ+J0d63h1SnxIIrtvaGg5hxFDC6Xzm1w9jumCykEOAopNuD8bg5Wl+4XEQ8TPjq5rT7zXK9brK2ZtW0/Na+WyrlP3RGPYPrckaFVywVtgDnwaRtjCHfnc/vj03yXpM68/YpyKkX8fDa1lEoWopFpi5ierjnzhRp9vq0HoO//ZdEbWh3CmKPAAuBhXtyQjNixkiDLEmBVWCBgBbhkR+BRXS5o1W8FzvTkKJunm1M1QaZ3D4nbCfiIwPtaYcH9BGCsqC7esx17Y3o98SLXy3lxCdHfmCo+JDJSHB4d7S1cCy/ICR+h5BBDdk5jtOJBj3g== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(346002)(396003)(376002)(136003)(39860400002)(451199021)(40470700004)(46966006)(36840700001)(2906002)(7406005)(7416002)(44832011)(8676002)(8936002)(5660300002)(41300700001)(6916009)(54906003)(70586007)(70206006)(316002)(4326008)(6666004)(36860700001)(82310400005)(478600001)(1076003)(36756003)(40460700003)(83380400001)(86362001)(2616005)(16526019)(26005)(47076005)(336012)(426003)(81166007)(186003)(356005)(40480700001)(82740400003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:38:45.4832 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 85b63579-e684-40b1-ee7d-08db6afee869 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1PEPF000042A7.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ1PR12MB6337 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh 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 specification. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra [mdr: hold sev_deactivate_lock when calling SEV_CMD_SNP_DECOMMISSION] Signed-off-by: Michael Roth --- .../virt/kvm/x86/amd-memory-encryption.rst | 24 ++++ arch/x86/kvm/svm/sev.c | 126 +++++++++++++++++- arch/x86/kvm/svm/svm.h | 1 + include/uapi/linux/kvm.h | 10 ++ 4 files changed, 158 insertions(+), 3 deletions(-) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index 1240d28badd6..3293e86f9b8a 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -461,6 +461,30 @@ The flags bitmap is defined as:: If the specified flags is not supported then return -EOPNOTSUPP, and the supported flags are returned. +19. KVM_SNP_LAUNCH_START +------------------------ + +The KVM_SNP_LAUNCH_START command is used for creating the memory encryption +context for the SEV-SNP guest. To create the encryption context, user must +provide a guest policy, migration agent (if any) and guest OS visible +workarounds value as defined SEV-SNP specification. + +Parameters (in): struct kvm_snp_launch_start + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_launch_start { + __u64 policy; /* Guest policy to use. */ + __u64 ma_uaddr; /* userspace address of migration agent */ + __u8 ma_en; /* 1 if the migration agent is enabled */ + __u8 imi_en; /* set IMI to 1. */ + __u8 gosvw[16]; /* guest OS visible workarounds */ + }; + +See the SEV-SNP specification for further detail on the launch input. + References ========== diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 70e0576a32d0..e65f3be67c23 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "mmu.h" #include "x86.h" @@ -75,6 +76,8 @@ static unsigned int nr_asids; static unsigned long *sev_asid_bitmap; static unsigned long *sev_reclaim_asid_bitmap; +static int snp_decommission_context(struct kvm *kvm); + struct enc_region { struct list_head list; unsigned long npages; @@ -100,12 +103,17 @@ static int sev_flush_asids(int min_asid, int max_asid) down_write(&sev_deactivate_lock); wbinvd_on_all_cpus(); - ret = sev_guest_df_flush(&error); + + if (sev_snp_enabled) + ret = sev_do_cmd(SEV_CMD_SNP_DF_FLUSH, NULL, &error); + else + ret = sev_guest_df_flush(&error); up_write(&sev_deactivate_lock); if (ret) - pr_err("SEV: DF_FLUSH failed, ret=%d, error=%#x\n", ret, error); + pr_err("SEV%s: DF_FLUSH failed, ret=%d, error=%#x\n", + sev_snp_enabled ? "-SNP" : "", ret, error); return ret; } @@ -1871,6 +1879,80 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd) return ret; } +/* + * The guest context contains all the information, keys and metadata + * associated with the guest that the firmware tracks to implement SEV + * and SNP features. The firmware stores the guest context in hypervisor + * provide page via the SNP_GCTX_CREATE command. + */ +static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct sev_data_snp_addr data = {}; + void *context; + int rc; + + /* Allocate memory for context page */ + context = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT); + if (!context) + return NULL; + + data.gctx_paddr = __psp_pa(context); + rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error); + if (rc) { + snp_free_firmware_page(context); + return NULL; + } + + 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 = {0}; + + data.gctx_paddr = __psp_pa(sev->snp_context); + data.asid = sev_get_asid(kvm); + return sev_issue_cmd(kvm, SEV_CMD_SNP_ACTIVATE, &data, error); +} + +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 = {0}; + 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; + + sev->snp_context = snp_context_create(kvm, argp); + if (!sev->snp_context) + return -ENOTTY; + + start.gctx_paddr = __psp_pa(sev->snp_context); + start.policy = params.policy; + memcpy(start.gosvw, params.gosvw, sizeof(params.gosvw)); + rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_START, &start, &argp->error); + if (rc) + goto e_free_context; + + sev->fd = argp->sev_fd; + rc = snp_bind_asid(kvm, &argp->error); + if (rc) + goto e_free_context; + + return 0; + +e_free_context: + snp_decommission_context(kvm); + + return rc; +} + int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -1961,6 +2043,9 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) case KVM_SEV_RECEIVE_FINISH: r = sev_receive_finish(kvm, &sev_cmd); break; + case KVM_SEV_SNP_LAUNCH_START: + r = snp_launch_start(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; @@ -2152,6 +2237,33 @@ int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd) 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_addr data = {}; + int ret; + + /* If context is not created then do nothing */ + if (!sev->snp_context) + return 0; + + data.gctx_paddr = __sme_pa(sev->snp_context); + down_write(&sev_deactivate_lock); + ret = sev_do_cmd(SEV_CMD_SNP_DECOMMISSION, &data, NULL); + if (WARN_ONCE(ret, "failed to release guest context")) { + up_write(&sev_deactivate_lock); + return ret; + } + + up_write(&sev_deactivate_lock); + + /* free the context page now */ + snp_free_firmware_page(sev->snp_context); + sev->snp_context = NULL; + + return 0; +} + void sev_vm_destroy(struct kvm *kvm) { struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; @@ -2193,7 +2305,15 @@ void sev_vm_destroy(struct kvm *kvm) } } - sev_unbind_asid(kvm, sev->handle); + if (sev_snp_guest(kvm)) { + if (snp_decommission_context(kvm)) { + WARN_ONCE(1, "Failed to free SNP guest context, leaking asid!\n"); + return; + } + } else { + sev_unbind_asid(kvm, sev->handle); + } + sev_asid_free(sev); } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 4360cf04f53a..9a7cafb018fe 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -93,6 +93,7 @@ struct kvm_sev_info { struct misc_cg *misc_cg; /* For misc cgroup accounting */ atomic_t migration_in_progress; u64 snp_init_flags; + void *snp_context; /* SNP guest context page */ }; struct kvm_svm { diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 43b6291e3a80..b4c7ac9710d3 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1928,6 +1928,7 @@ enum sev_cmd_id { /* SNP specific commands */ KVM_SEV_SNP_INIT, + KVM_SEV_SNP_LAUNCH_START, KVM_SEV_NR_MAX, }; @@ -2035,6 +2036,15 @@ struct kvm_snp_init { __u64 flags; }; +struct kvm_sev_snp_launch_start { + __u64 policy; + __u64 ma_uaddr; + __u8 ma_en; + __u8 imi_en; + __u8 gosvw[16]; + __u8 pad[6]; +}; + #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 Mon Jun 12 04:25:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275515 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AB4F4C7EE2E for ; Mon, 12 Jun 2023 04:41:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234564AbjFLElC (ORCPT ); Mon, 12 Jun 2023 00:41:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235500AbjFLEk2 (ORCPT ); Mon, 12 Jun 2023 00:40:28 -0400 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2080.outbound.protection.outlook.com [40.107.102.80]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D07C19AF; Sun, 11 Jun 2023 21:39:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NPi3DdT2PUo1OlJ+MiySWa7ZyCAQoxHyDwiJLMgT4bFeSQxnVte4gjr0i+o1HDxe0aP+VpSYSRSnwRjnEWPaomvX/JrZ8ScJ25jzj+Z25/0z8kaQijzr+DIJGOVpyjIyWX++k+qX9soDp6REMBGslhbyYbXlA2tw8qnNONScFKkqfX+q9HADKUXXtfNgl9uBncwxM3D5PUiYPlkJxhQs9mSh9dJFU6DrMrtNQHhcQzoQIcgnwhlemqVs4mFs/7WqaqpFIoV1UUzB5p3qHAmBFzkrBaRF7H7dzeMMdnhTHiJ+vetHX1SF3yCaqtUj5ALVEKuw+G4C8DC2UTn95P9f9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=eHNcgb3bW0RGuw2lf/r2QANgbsO2xqcPD/u1M2WbB/M=; b=g5mZMjYIulpTeandK60uHJQtTnOPx2LTYje7ns5VW2kwNvuu1C5J046mFjXWw8NPiVMguJ7i2SCNr0txjAq11j2IjVdmA3ucXOv2iQmrdlIm3/MiqWPeiFUgPum8A2qd5UyCwE4mC++jsKwD7RaVDe+CfiHya7RVFFqB08HXUPH53KTf26fO0TUJORabvWJVCKPYA31HBsSa+wo6WI7kzm9nH1zidZtgTuDip/nppgdiMg7CyY6sI3nI6D05KMrNc4Wk9tXOdbjDh6ge9pQCSSLppkIBEZeFeKhuvwoja2pyE4ORZEDmadhQ6IfCL3czZHC5+5RTUfjJKTQvlNZ6fg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=eHNcgb3bW0RGuw2lf/r2QANgbsO2xqcPD/u1M2WbB/M=; b=mWEiunBIUYm5h3yljexd3mZ6hGY0lEc/viKTVhNo5PUPfPqQfwk/s1oCiuc/ofwqfBh2ULiYFslFfrwX0EH8+5YB9WOXz87uO8ITRRibB2Pep9BLdUVqFqAB78fXypfGuReP3nzCKsvRBG4j9bzqa2LFWJaE0FYauBed7atIAeM= Received: from MW4PR03CA0003.namprd03.prod.outlook.com (2603:10b6:303:8f::8) by CH2PR12MB4873.namprd12.prod.outlook.com (2603:10b6:610:63::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.29; Mon, 12 Jun 2023 04:39:26 +0000 Received: from CO1PEPF000042AE.namprd03.prod.outlook.com (2603:10b6:303:8f:cafe::d3) by MW4PR03CA0003.outlook.office365.com (2603:10b6:303:8f::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:39:26 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1PEPF000042AE.mail.protection.outlook.com (10.167.243.43) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:39:26 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:39:23 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Vishal Annapurve Subject: [PATCH RFC v9 30/51] KVM: Add HVA range operator Date: Sun, 11 Jun 2023 23:25:38 -0500 Message-ID: <20230612042559.375660-31-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000042AE:EE_|CH2PR12MB4873:EE_ X-MS-Office365-Filtering-Correlation-Id: 5e65158f-8517-41a6-9a81-08db6aff00bd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /13cltSALSFlfzZt1R+Xu8figwINoNYALkqfZNNREKs963ny4VOWlyKZ3mv8mVuBHneJDOpqFLJKms8nCy38XsAKGVnKoD4L9UbSGDLwILyT4j0RWsblsM7EU8ffvy3hLterW+n8RK3DMEstvcFWrren5+wQOFMRTrWACQOyCq8FAhghYrfZjS1Cy8AxE1FQ5NFKV0O/qSbC9sblYDk9uwP2NgSd0L4kx+hoA3aRdiQbPLMXaeI+6MfjcOINQjNFr5JI4isHSrRzr5473WZ/uYNEODZiw+DqD05enRuCLTA+WU/A2Ylju/8/rK2P92bs8sTbzCNoxwMwHCo/DH/Mv9XAzgZSwbdV0katiImI2WOdErShobcIinGmWEedAbK+J3U2mDRjcJsuvmMaqtVFi0kGazxkuzaJOp1f7V2fSssJVNfjZlBGKHgqJ+qgR04dtiY2QU6wZ4z+p1CZv5qkwqDG3f5L1mIu59omczQBEw4PArEXaAbExfUQCODGfWEbEoKNutLALoqrcmF5TOMffY6cmCymoGNqrfzLJ48K946x3Nr0hQdK/RxSMlnEaM8CO86e4cGVjlqCpoFWTT46K2lydTkKxuK5IiIX8LiNkxIlA5+2ir8w5FxOYiG5Q2xGmEQeCSrFXeWN2Z+km7g7RrA3dqNMRrV14PntUFZERbwiixKrbYcqlz/GVAIHd1Qr/w5nxZu7NLFwW+0Im7iAFo+GHBu7Tm7/31qenbj9+vmrzuxERln+lXJJPfZpyHpvrzmfTmRW453YZR9IUfnuKQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(376002)(346002)(396003)(136003)(451199021)(36840700001)(46966006)(40470700004)(426003)(336012)(2616005)(47076005)(83380400001)(36860700001)(82740400003)(356005)(40480700001)(86362001)(82310400005)(81166007)(36756003)(40460700003)(478600001)(44832011)(54906003)(4326008)(6666004)(8936002)(8676002)(2906002)(5660300002)(7416002)(7406005)(70206006)(70586007)(6916009)(41300700001)(316002)(16526019)(186003)(1076003)(26005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:39:26.3281 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 5e65158f-8517-41a6-9a81-08db6aff00bd X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1PEPF000042AE.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4873 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Vishal Annapurve Introduce HVA range operator so that other KVM subsystems can operate on HVA range. Signed-off-by: Vishal Annapurve [mdr: minor checkpatch alignment fixups] Signed-off-by: Michael Roth --- include/linux/kvm_host.h | 6 +++++ virt/kvm/kvm_main.c | 49 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 7de06add2235..9a9d4141ba74 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1400,6 +1400,12 @@ void kvm_mmu_invalidate_begin(struct kvm *kvm); void kvm_mmu_invalidate_range_add(struct kvm *kvm, gfn_t start, gfn_t end); void kvm_mmu_invalidate_end(struct kvm *kvm); +typedef int (*kvm_hva_range_op_t)(struct kvm *kvm, + struct kvm_gfn_range *range, void *data); + +int kvm_vm_do_hva_range_op(struct kvm *kvm, unsigned long hva_start, + unsigned long hva_end, kvm_hva_range_op_t handler, void *data); + long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg); long kvm_arch_vcpu_ioctl(struct file *filp, diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 422d49634c56..48beffca6b67 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -642,6 +642,55 @@ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, return (int)ret; } +int kvm_vm_do_hva_range_op(struct kvm *kvm, unsigned long hva_start, + unsigned long hva_end, kvm_hva_range_op_t handler, void *data) +{ + int ret = 0; + struct kvm_gfn_range gfn_range; + struct kvm_memory_slot *slot; + struct kvm_memslots *slots; + int i, idx; + + if (WARN_ON_ONCE(hva_end <= hva_start)) + return -EINVAL; + + idx = srcu_read_lock(&kvm->srcu); + + for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { + struct interval_tree_node *node; + + slots = __kvm_memslots(kvm, i); + kvm_for_each_memslot_in_hva_range(node, slots, + hva_start, hva_end - 1) { + unsigned long start, end; + + slot = container_of(node, struct kvm_memory_slot, + hva_node[slots->node_idx]); + start = max(hva_start, slot->userspace_addr); + end = min(hva_end, slot->userspace_addr + + (slot->npages << PAGE_SHIFT)); + + /* + * {gfn(page) | page intersects with [hva_start, hva_end)} = + * {gfn_start, gfn_start+1, ..., gfn_end-1}. + */ + gfn_range.start = hva_to_gfn_memslot(start, slot); + gfn_range.end = hva_to_gfn_memslot(end + PAGE_SIZE - 1, slot); + gfn_range.slot = slot; + + ret = handler(kvm, &gfn_range, data); + if (ret) + goto e_ret; + } + } + +e_ret: + srcu_read_unlock(&kvm->srcu, idx); + + return ret; +} +EXPORT_SYMBOL_GPL(kvm_vm_do_hva_range_op); + static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn, unsigned long start, unsigned long end, From patchwork Mon Jun 12 04:25:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275517 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 79367C7EE2E for ; Mon, 12 Jun 2023 04:42:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234835AbjFLEmR (ORCPT ); Mon, 12 Jun 2023 00:42:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234822AbjFLElh (ORCPT ); Mon, 12 Jun 2023 00:41:37 -0400 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2053.outbound.protection.outlook.com [40.107.223.53]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F9FF1724; Sun, 11 Jun 2023 21:41:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=U681qMlhTPyX4zBte/1J08WgQdSXtpNRC8GHMG0lZwz6PaTHoR/NtPZddqCxvIKkJNbinV3auUC43G78ahxCQlVAzO4/LaB+MN/JutX4tTrpGoZARZ85v/SlxBMZl0XGGNDzoLqFi+ifT1D7yyqxfaCuF6QJtZlFamF7KG4Gkw+2wtRnh/38Ns3GhRPzX9VIytaxs04PcSOtWQWvz6OGKhiz2oVL7+h1PsFV8onvNjZhL/KnXMAwmG/mO3f8wbsN3cEpqX2dIuqn6oLVrQLbTEcEhGD6/QMDSW8+EB2Xkp4wqyW+1zXHlgNZhirHOegB0fWF6A0B6Nr+OkHr7YqIhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=28cE1nFtQUfuCs5Hi5DEX8uBfUlxeTHiFFKicSfKOZU=; b=TDBUsMiWw0LylSoax6yvRNF1u01EEn+zzdc/m5xR3B8uYHl9U+zBn0I/dUGy/mvxpffJjWx2x9h7BHgf5HdruSoNDP9KgDAUG1mKwFvcoVcMe/Uui1Pdkw/Ym3jKZeMiQku3yJwFdBkUROBDsCnb2dCMkZJHMML4UyynAynRTf9P+26RJCzuvTpS4uUIQnY9g1KCADh3o4HVjTfNrJyuvT6vJzFoAoCPmgqJhIXb3tr6gxs5Z1ntL34lgTE1TjX/5lv+GNdGUeLTywcqevh95zEIz9Xn2SKI+wxqJurANE1AEw0auOlNzWwjnNIkQ/7jS/4Q+v3GmPhYoT2J9TGTYA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=28cE1nFtQUfuCs5Hi5DEX8uBfUlxeTHiFFKicSfKOZU=; b=P47QNxkfRU0oZJ6vNm7Wqx/m0Iu0Vsyv44k5mZOB4QiIDM6s+zu/0iVzPQIIBKAsTtkl08BMIk6V9Yaypy6ZwLoEzI1hX1KlKU63rUZuVqLv76eEoZW9aDKZmespl+2ZAIiI94DpLqCSiM93fLyWvUTP69ARxK3ATUAPGmlca+I= Received: from MW4PR04CA0233.namprd04.prod.outlook.com (2603:10b6:303:87::28) by LV2PR12MB5752.namprd12.prod.outlook.com (2603:10b6:408:14d::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.44; Mon, 12 Jun 2023 04:40:59 +0000 Received: from CO1PEPF000042A7.namprd03.prod.outlook.com (2603:10b6:303:87:cafe::4) by MW4PR04CA0233.outlook.office365.com (2603:10b6:303:87::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:40:58 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1PEPF000042A7.mail.protection.outlook.com (10.167.243.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:40:58 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:40:53 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 31/51] KVM: Split out memory attribute xarray updates to helper function Date: Sun, 11 Jun 2023 23:25:39 -0500 Message-ID: <20230612042559.375660-32-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000042A7:EE_|LV2PR12MB5752:EE_ X-MS-Office365-Filtering-Correlation-Id: 3062944b-838f-4225-c420-08db6aff37b9 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 8k/qp+GwPlSpluzG5rMPQ0v1hvJXRBpRDZBx2Tg5D6MifWTeMcZ2SR3HqrxFSkvLXRA26DJNPHvCbQVwBrBLCGRn50F2N4qLClJA+r286oKWvPnDSXf0tTBXGCA9SUJUu6x97S3xD1CQ+KgRqws2Iu1tdDbZKysOt1tJ9OmsdXf1CUzqKuQLEZgTS95Z4NcblhCPFp3XhxdZyOyblNzTDB9XjEPBJheG7KohvRJhDBrShthEjDDPS8shnIpUpIQ2uYZ3ZhL5d3nPdb3RIF2j9Lvpp3ce7kILcpC48wig6kYAAK2c4H/GH03bnY3r12RXvBJxSPh5coprTwgb5kHZthirdLs85xPW5TNMvZReDmdDedLZsVfKFcNXSRK2XR/5KlUBSEpKZfZ3OKGPAIMUR2XGgyi+SIW5IEgH3cCbfvMYg5OLX9bjM/aEGJ0WgV7X6dLLzovqs6OWhqsYxmwTVKSb4xP9JWKAIXu7UyHpbe1l/ZCDqFp2Gfkp3SFgATcLpIl2vc+9ElnO1n77XQzZotqnQ+MeTSdaV3c0RBPblp8RGVpXuk8XEuGiLkkuCS53C4kfyr0jUkW1BG5zkIgi+hvAPZU1WgND++KqPZthR9dV1PzTLavAqFYoBkrNGxJiFzQAbJF6fK4rzlyjXxMjeH6ahpaQCrL+fQ7pwheAI0tY9UFOCsRySCXCuReBVfHGeMIAf3KAa4OCycgPVGRLTJRFmnaYDx4pCamG+taXZ2Et7pub069/Grv694eiVYgrmgSKJV6jehH+r7BrsWIu2g== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(346002)(376002)(396003)(39860400002)(136003)(451199021)(46966006)(40470700004)(36840700001)(6916009)(4326008)(44832011)(316002)(7406005)(7416002)(70586007)(15650500001)(41300700001)(70206006)(186003)(16526019)(2906002)(54906003)(478600001)(8676002)(8936002)(5660300002)(6666004)(40460700003)(1076003)(82740400003)(356005)(81166007)(40480700001)(36860700001)(26005)(83380400001)(426003)(336012)(47076005)(36756003)(86362001)(82310400005)(2616005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:40:58.5776 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3062944b-838f-4225-c420-08db6aff37b9 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1PEPF000042A7.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV2PR12MB5752 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org This will be useful to other callers that need to update memory attributes for things like setting up the initial private memory payload for a guest. Signed-off-by: Michael Roth --- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 9a9d4141ba74..56055692cdfa 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -991,6 +991,7 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module); void kvm_exit(void); void kvm_get_kvm(struct kvm *kvm); +int kvm_vm_set_region_attr(struct kvm *kvm, gfn_t start, gfn_t end, u64 attributes); bool kvm_get_kvm_safe(struct kvm *kvm); void kvm_put_kvm(struct kvm *kvm); bool file_is_kvm(struct file *file); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 48beffca6b67..0c0a75affab6 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2466,12 +2466,28 @@ static void kvm_mem_attrs_changed(struct kvm *kvm, unsigned long attrs, kvm_flush_remote_tlbs(kvm); } +int kvm_vm_set_region_attr(struct kvm *kvm, gfn_t start, gfn_t end, + u64 attributes) +{ + gfn_t index; + void *entry; + + entry = attributes ? xa_mk_value(attributes) : NULL; + + for (index = start; index < end; index++) + if (xa_err(xa_store(&kvm->mem_attr_array, index, entry, + GFP_KERNEL_ACCOUNT))) + break; + + return index; +} +EXPORT_SYMBOL_GPL(kvm_vm_set_region_attr); + static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, struct kvm_memory_attributes *attrs) { gfn_t start, end; unsigned long i; - void *entry; /* flags is currently not used. */ if (attrs->flags) @@ -2486,8 +2502,6 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, start = attrs->address >> PAGE_SHIFT; end = (attrs->address + attrs->size - 1 + PAGE_SIZE) >> PAGE_SHIFT; - entry = attrs->attributes ? xa_mk_value(attrs->attributes) : NULL; - mutex_lock(&kvm->slots_lock); KVM_MMU_LOCK(kvm); @@ -2495,11 +2509,7 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, kvm_mmu_invalidate_range_add(kvm, start, end); KVM_MMU_UNLOCK(kvm); - for (i = start; i < end; i++) { - if (xa_err(xa_store(&kvm->mem_attr_array, i, entry, - GFP_KERNEL_ACCOUNT))) - break; - } + i = kvm_vm_set_region_attr(kvm, start, end, attrs->attributes); KVM_MMU_LOCK(kvm); if (i > start) From patchwork Mon Jun 12 04:25:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275518 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 57E7EC7EE25 for ; Mon, 12 Jun 2023 04:42:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234937AbjFLEm4 (ORCPT ); Mon, 12 Jun 2023 00:42:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234948AbjFLEmg (ORCPT ); Mon, 12 Jun 2023 00:42:36 -0400 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2073.outbound.protection.outlook.com [40.107.223.73]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03D00E73; Sun, 11 Jun 2023 21:41:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aBI1Lr52kik5DoVeEFjZnJnBcDtWobBQ6DHqA4I8RwXWyZRzY7eAUKN9EnjZECq3dFE4H89zJQ5YyvDpWmYmlm93vhUhlE2D5m0KUhBCdEqL+MG3HSwrGkmIgkWCXqgOjM1g6efA5Ul7nZcu/CwfHPCDkJoO0By/64/B4TftHTgPMpBTkmZUEhfPx91SC3uB2xkWiwvMgmrJnppgZz69q+aKka8QUWvkx5lAgJ3PzAPB0gVkWrHM55SaWjvAUEeOhHanrZyajkYpRqQKaTsY+fMsESXL3+NbZPTVKGKR5ziG2YdCBIt5udvQlb5J7i9XW4lFpsvqRpmrdUhApoGAVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=yAfVn+Tp6NOaS7IR660oZK9mdnFT1BxHWZYI0Sm7SxM=; b=XCHF/bgKSHNCz4Uq+uDQnaAJsCJQlzm4D9baX+OtAOeKC46JeU7JYQe+citZEwY0Kv0bFCdlZH/qn2tNv97MrmLqVRtL6TFbm5GUHpOiEg5cWioxIKA44s9mwHHTSL9YxbwosD3C0BAvf2oMlyxEr4wBmGrmS9uZwR+EsVQH6NNSlkRHP6QEKTN4FqW6lXN0UTTnIUTtWNexoNA7cxx3AeQzYQ+eEsgnoKwEHYCkTGf0CFtSWd1G8Yu+TQ49kaclRwWboLWeFkRFeHDPkyYCYv2mmi9/aL3MvbP4F5uF9DlLWROwKh8wDm8dJJ/yTdGte6xXZsyIpq4EvsCvmQ2fCQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=yAfVn+Tp6NOaS7IR660oZK9mdnFT1BxHWZYI0Sm7SxM=; b=BWWaeu2DQBVs4nicVo2LONo40leKI6SHM09ffbZ9fcbL62f1arEfB2vrnc5SkrtlYHUugnUB0JAxHr8/pqR7E5FTTCKcO+mvjfGuY2+52lmNqgW35obzJrRnGHtaWcGv2OyOEI00ESUzlRSXFJfkBfhe083OZy1VsYP+sRtgzO8= Received: from MW4PR03CA0095.namprd03.prod.outlook.com (2603:10b6:303:b7::10) by PH8PR12MB8605.namprd12.prod.outlook.com (2603:10b6:510:1cc::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.33; Mon, 12 Jun 2023 04:41:39 +0000 Received: from CO1PEPF000042AD.namprd03.prod.outlook.com (2603:10b6:303:b7:cafe::db) by MW4PR03CA0095.outlook.office365.com (2603:10b6:303:b7::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:41:39 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1PEPF000042AD.mail.protection.outlook.com (10.167.243.42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:41:39 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:41:37 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 32/51] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_UPDATE command Date: Sun, 11 Jun 2023 23:25:40 -0500 Message-ID: <20230612042559.375660-33-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000042AD:EE_|PH8PR12MB8605:EE_ X-MS-Office365-Filtering-Correlation-Id: 6d2609bb-3086-457e-c1cb-08db6aff4ff8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: OeXH5NnRpjhKUb+uvMr3z4QnLoj1z7oAd9Zkjk6leQXQK1I1bxHuPR8BuE+Y5xSmvq/CkD0RbKRZILGZ+DUtyYgh3tHogCYKnSgXWBcijrg5w7Na2Gqer5whDZunyBTP4ENlVUZAvZYUScqCoikVA3lGpB26iCy2w51UHBxFuc8OMcXOLGGRI2+J9296SCbQ0/8rg94sZ6NvqJfgy4SWziEgUHFaq26rkldGYTVnaSvIOGhe9qdOSMHad8RRf9wFdLowPFHe/6os1Xz53ni7PKdUe+pIJhpkIBjTKYnos0abNsCVe/3Di+tmPyLhrO3DF4y6FcGWvaCs1GW/B/DndRfwIMD9LcY5G417RRvLm4NIytYUp+GY/9NJOrOskAoH+X7VULSzaQFq4dLs396huAEr5XHPSgCwk8low7k8s7LqL9Lv86lK4mbdVyJlwkKB5Xa0Sh50XD+AIPoL1GLpCSeolcWwyPOP0gESUtkhJhzqfikJvRGDRRnB2vhm3QpJHvo6hBZT5LL2n3E6OWD/7oyiV2KArOaIFA507xwfaBMqOmZGlkvGoboKyd2F/Wx7OROPNT8nATRPB4WfGCbHkLh+kps4FDOZHDTMWgKZ/pBs9iyxjB1FDVZrZUdy78mRv1B3ntYSi0GxThtJjktjRh2sH9qt2feSJGnwmDaOF0GmsoiChBKFhQlcMU5jgyt6EmJKrqnzWNhcAYMnHGoIlTAkTeUoLZfMPLYzM777NkOVTPggAW7eIfl8Ebut5FyUIxvthIgEhpjlpYNsn5Kb/g== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(376002)(39860400002)(136003)(346002)(396003)(451199021)(40470700004)(46966006)(36840700001)(16526019)(186003)(478600001)(426003)(36860700001)(1076003)(26005)(83380400001)(2616005)(336012)(47076005)(40480700001)(6666004)(2906002)(40460700003)(8676002)(4326008)(316002)(8936002)(86362001)(41300700001)(82310400005)(356005)(82740400003)(70586007)(81166007)(36756003)(44832011)(7406005)(54906003)(7416002)(6916009)(70206006)(5660300002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:41:39.2402 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6d2609bb-3086-457e-c1cb-08db6aff4ff8 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1PEPF000042AD.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB8605 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh 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. While terminating the guest, reclaim the guest pages added in the RMP table. If the reclaim fails, then the page is no longer safe to be released back to the system and leak them. For more information see the SEV-SNP specification. Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- .../virt/kvm/x86/amd-memory-encryption.rst | 28 +++ arch/x86/kvm/svm/sev.c | 189 ++++++++++++++++++ include/uapi/linux/kvm.h | 19 ++ 3 files changed, 236 insertions(+) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index 3293e86f9b8a..d8492af09796 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -485,6 +485,34 @@ Returns: 0 on success, -negative on error See the SEV-SNP specification for further detail on the launch input. +20. KVM_SNP_LAUNCH_UPDATE +------------------------- + +The KVM_SNP_LAUNCH_UPDATE is used for encrypting a memory region. It also +calculates a measurement of the memory contents. The measurement is a signature +of the memory contents that can be sent to the guest owner as an attestation +that the memory was encrypted correctly by the firmware. + +Parameters (in): struct kvm_snp_launch_update + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_launch_update { + __u64 start_gfn; /* Guest page number to start from. */ + __u64 uaddr; /* userspace address need to be encrypted */ + __u32 len; /* length of memory region */ + __u8 imi_page; /* 1 if memory is part of the IMI */ + __u8 page_type; /* page type */ + __u8 vmpl3_perms; /* VMPL3 permission mask */ + __u8 vmpl2_perms; /* VMPL2 permission mask */ + __u8 vmpl1_perms; /* VMPL1 permission mask */ + }; + +See the SEV-SNP spec for further details on how to build the VMPL permission +mask and page type. + References ========== diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index e65f3be67c23..6a82767d940f 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -234,6 +234,36 @@ static void sev_decommission(unsigned int handle) sev_guest_decommission(&decommission, NULL); } +static int snp_page_reclaim(u64 pfn) +{ + struct sev_data_snp_page_reclaim data = {0}; + int err, rc; + + data.paddr = __sme_set(pfn << PAGE_SHIFT); + rc = sev_do_cmd(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err); + if (rc) { + /* + * If the reclaim failed, then page is no longer safe + * to use. + */ + snp_leak_pages(pfn, 1); + } + + return rc; +} + +static int host_rmp_make_shared(u64 pfn, enum pg_level level, bool leak) +{ + int rc; + + rc = rmp_make_shared(pfn, level); + if (rc && leak) + snp_leak_pages(pfn, + page_level_size(level) >> PAGE_SHIFT); + + return rc; +} + static void sev_unbind_asid(struct kvm *kvm, unsigned int handle) { struct sev_data_deactivate deactivate; @@ -1953,6 +1983,162 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) return rc; } +static int snp_launch_update_gfn_handler(struct kvm *kvm, + struct kvm_gfn_range *range, + void *opaque) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct kvm_memory_slot *memslot = range->slot; + struct sev_data_snp_launch_update data = {0}; + struct kvm_sev_snp_launch_update params; + struct kvm_sev_cmd *argp = opaque; + int *error = &argp->error; + int i, n = 0, ret = 0; + unsigned long npages; + kvm_pfn_t *pfns; + gfn_t gfn; + + if (!kvm_slot_can_be_private(memslot)) { + pr_err("SEV-SNP requires restricted memory.\n"); + return -EINVAL; + } + + if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) { + pr_err("Failed to copy user parameters for SEV-SNP launch.\n"); + return -EFAULT; + } + + data.gctx_paddr = __psp_pa(sev->snp_context); + + npages = range->end - range->start; + pfns = kvmalloc_array(npages, sizeof(*pfns), GFP_KERNEL_ACCOUNT); + if (!pfns) + return -ENOMEM; + + pr_debug("%s: GFN range 0x%llx-0x%llx, type %d\n", __func__, + range->start, range->end, params.page_type); + + for (gfn = range->start, i = 0; gfn < range->end; gfn++, i++) { + int order, level; + bool assigned; + void *kvaddr; + + ret = kvm_gmem_get_pfn(kvm, memslot, gfn, &pfns[i], &order); + if (ret) + goto e_release; + + n++; + ret = snp_lookup_rmpentry((u64)pfns[i], &assigned, &level); + if (ret || assigned) { + pr_err("Failed to ensure GFN 0x%llx is in initial shared state, ret: %d, assigned: %d\n", + gfn, ret, assigned); + return -EFAULT; + } + + kvaddr = pfn_to_kaddr(pfns[i]); + if (!virt_addr_valid(kvaddr)) { + pr_err("Invalid HVA 0x%llx for GFN 0x%llx\n", (uint64_t)kvaddr, gfn); + ret = -EINVAL; + goto e_release; + } + + ret = kvm_read_guest_page(kvm, gfn, kvaddr, 0, PAGE_SIZE); + if (ret) { + pr_err("Guest read failed, ret: 0x%x\n", ret); + goto e_release; + } + + ret = rmp_make_private(pfns[i], gfn << PAGE_SHIFT, PG_LEVEL_4K, + sev_get_asid(kvm), true); + if (ret) { + ret = -EFAULT; + goto e_release; + } + + data.address = __sme_set(pfns[i] << PAGE_SHIFT); + data.page_size = X86_TO_RMP_PG_LEVEL(PG_LEVEL_4K); + data.page_type = params.page_type; + data.vmpl3_perms = params.vmpl3_perms; + data.vmpl2_perms = params.vmpl2_perms; + data.vmpl1_perms = params.vmpl1_perms; + ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_UPDATE, + &data, error); + if (ret) { + pr_err("SEV-SNP launch update failed, ret: 0x%x, fw_error: 0x%x\n", + ret, *error); + snp_page_reclaim(pfns[i]); + + /* + * When invalid CPUID function entries are detected, the firmware + * corrects these entries for debugging purpose and leaves the + * page unencrypted so it can be provided users for debugging + * and error-reporting. + * + * Copy the corrected CPUID page back to shared memory so + * userpsace can retrieve this information. + */ + if (params.page_type == SNP_PAGE_TYPE_CPUID && + *error == SEV_RET_INVALID_PARAM) { + int ret; + + host_rmp_make_shared(pfns[i], PG_LEVEL_4K, true); + + ret = kvm_write_guest_page(kvm, gfn, kvaddr, 0, PAGE_SIZE); + if (ret) + pr_err("Failed to write CPUID page back to userspace, ret: 0x%x\n", + ret); + } + + goto e_release; + } + } + + /* + * Memory attribute updates via KVM_SET_MEMORY_ATTRIBUTES are serialized + * via kvm->slots_lock, so use the same protocol for updating them here. + */ + mutex_lock(&kvm->slots_lock); + kvm_vm_set_region_attr(kvm, range->start, range->end, KVM_MEMORY_ATTRIBUTE_PRIVATE); + mutex_unlock(&kvm->slots_lock); + +e_release: + /* Content of memory is updated, mark pages dirty */ + for (i = 0; i < n; i++) { + set_page_dirty(pfn_to_page(pfns[i])); + mark_page_accessed(pfn_to_page(pfns[i])); + + /* + * If its an error, then update RMP entry to change page ownership + * to the hypervisor. + */ + if (ret) + host_rmp_make_shared(pfns[i], PG_LEVEL_4K, true); + + put_page(pfn_to_page(pfns[i])); + } + + kvfree(pfns); + return ret; +} + +static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct kvm_sev_snp_launch_update params; + + 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; + + return kvm_vm_do_hva_range_op(kvm, params.uaddr, params.uaddr + params.len, + snp_launch_update_gfn_handler, argp); +} + int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -2046,6 +2232,9 @@ int sev_mem_enc_ioctl(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 b4c7ac9710d3..4961d2e67a4b 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1929,6 +1929,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, }; @@ -2045,6 +2046,24 @@ struct kvm_sev_snp_launch_start { __u8 pad[6]; }; +#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 start_gfn; + __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 Mon Jun 12 04:25:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275519 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4C6CEC7EE2E for ; Mon, 12 Jun 2023 04:43:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234499AbjFLEng (ORCPT ); Mon, 12 Jun 2023 00:43:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46126 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234346AbjFLEnT (ORCPT ); Mon, 12 Jun 2023 00:43:19 -0400 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on20612.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e8d::612]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B9DFE171E; Sun, 11 Jun 2023 21:42:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=catWJ6PST+CGnxINGTq2d5U6JAwDFLk3aq53EDSzwU5OVmlvZUeD2+2PXTYMnrdtzXtKTvwBK4izIVGx1K/JZaZ4rLlJeNbJIsMJRDcP1a0+YOHroEIfW4ueHu2U3+E67CY1fwrfewy1dkGC9UNoLryIfSkWGbngHITbHIi8iI2LJzn8ZO9+FAwmR5xVvmjPuRnhKSFWLd6+DY97EgkzinNk1YH18NE22OYlETw2ExkdUFUtJsCheTdhiK5wDi9DSCwcYfVzEZsz+mfqeiTDeAz1pi5DTGpkk1z5LrXjA0PH3Uw4m8l2cnXhGKOYgMoKPEoE4UBm5rDWRKLoRCS1hQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=FG6QkCjqXiLpNLeaBHakaidVeB3qMARoqImjyr/5kEc=; b=Gwn9+NSgzskG8n4coqckBqN6zLJbNblXN/2MaL3QAs3rIAJs7hsozwK2rh0E0DsfNtFYsiaXQbNmlY0lm443qEY8LSWIH0kCf+p+nZR+mHvQ/W7k3cOEFiTAcuBRLStUe5/t2SxG4poSTD/brlPglC7CIj/QHGEvDG5xm68uuT1UnVzh0jCa4xrt/Bo57RH9k730FoGCkRm1zcfBT9dF2VWp2foAu+v+CwRU1LAuqzYOvr+46VuNtOhWbZbA1M7bStppXE10PVRvajeGvOd61byFOv0+17udoM1s4G7Z51v8UgplmeCEbHqlhVRLjAqD1n9CyidhQacA2mOKZocBqQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=FG6QkCjqXiLpNLeaBHakaidVeB3qMARoqImjyr/5kEc=; b=HoTGlsIlsnI4JrdCtxy+tcL9r9DAXj3Oap3mpA9qtYJrV+QRPVquVfLgLf6JCwkG5QeUPQnPRfT6BaqerF7tySpNmbrjxVmWc7J27fS+sWnKPpoTXZcuvc/CFCNj3BevNGpj/PbZ06awu4/gCIpXc+5Xn5RGOVsowb0ixD9pr1s= Received: from SJ2PR07CA0004.namprd07.prod.outlook.com (2603:10b6:a03:505::10) by DS7PR12MB6048.namprd12.prod.outlook.com (2603:10b6:8:9f::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.29; Mon, 12 Jun 2023 04:42:23 +0000 Received: from CO1PEPF000042AC.namprd03.prod.outlook.com (2603:10b6:a03:505:cafe::28) by SJ2PR07CA0004.outlook.office365.com (2603:10b6:a03:505::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:42:22 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1PEPF000042AC.mail.protection.outlook.com (10.167.243.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.13 via Frontend Transport; Mon, 12 Jun 2023 04:42:22 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:42:19 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Harald Hoyer Subject: [PATCH RFC v9 33/51] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_FINISH command Date: Sun, 11 Jun 2023 23:25:41 -0500 Message-ID: <20230612042559.375660-34-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000042AC:EE_|DS7PR12MB6048:EE_ X-MS-Office365-Filtering-Correlation-Id: 1944eb23-9154-49d7-7573-08db6aff69de X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Ht8gzIVqf1TWuxzF9gRQOPGEWtDUsvncpha3zyEKdL0NL52JDoxgR2+nVa7y2A+oMVE7j/AmKtMTgTSMnL7mv6oMQQ18bcSRGVGE+mz7ZtOk2rPxjLgMNa5taWd1UWtxOa/tvbivlTCsGxSr5hchq5qqBJUpBTmpjTBhrcDJRKkYaSuIKkWCT8rFGrw/VOEuAoQAnA0LcW/QtYheJwDafdbv0HoYsX7oEgpZW86sATpyPS97bD0fWUOmILMvtreIMYupQkjD+Mpf7VvuoWJcTj2tVGy/WFBIJgTJHj3rPANpimvGP0Htkr7pmYBtgCt8cgqTOdkqJEy0mfPFExVkD/hwsVzizH0yERpzmlyf+Ra+KdKXL25JMG0G3jsts0ud9cZ9ihI5SfvyxD0mH3QMEvDDhavA48yU3ig2sbGeg6pjM6/BTf+HPzXahhvY4s8BmClpHNzNtz11Sm1/f4GbQ/rtmZmHnC9heBAAYXDaScoavxM2lrur4T5Z2S1qiVWxCCBF7EA1ni59275zIt3yX0WFBQZ8e+FAvociyVHoQijxl051ufL7qufEUexJFSUAyyBXq5JA+tLQGCka4y22I1an/YrDtn5g109TUi+QKvOdcp6yNta7bxhrljrI8SIUuBohC3FubMukezBDpurINAHlp1mapmZo+k5lXy+hYN+2e5yBHohJ9/6ltEGjQCerL95QLgdIvzQ+u4QLEkgUHa/+XdyaaZdW3hB3mk3shZ5f9nv4o/Dms0IHkKDlcaL8VNIZ/WzZw8hwzGWw1Og5sg== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(396003)(136003)(376002)(346002)(39860400002)(451199021)(46966006)(40470700004)(36840700001)(316002)(40460700003)(41300700001)(336012)(426003)(82310400005)(83380400001)(86362001)(2616005)(36860700001)(26005)(1076003)(186003)(7416002)(7406005)(2906002)(47076005)(44832011)(16526019)(81166007)(356005)(82740400003)(40480700001)(8676002)(36756003)(5660300002)(8936002)(70586007)(70206006)(478600001)(54906003)(6666004)(4326008)(6916009)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:42:22.7042 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1944eb23-9154-49d7-7573-08db6aff69de X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1PEPF000042AC.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6048 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh 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. If its an SNP guest, then VMSA was added in the RMP entry as a guest owned page and also removed from the kernel direct map so flush it later after it is transitioned back to hypervisor state and restored in the direct map. Signed-off-by: Brijesh Singh Signed-off-by: Harald Hoyer Signed-off-by: Ashish Kalra [mdr: always measure BSP first to get consistent launch measurements] Signed-off-by: Michael Roth --- .../virt/kvm/x86/amd-memory-encryption.rst | 24 +++ arch/x86/kvm/svm/sev.c | 146 ++++++++++++++++++ include/uapi/linux/kvm.h | 14 ++ 3 files changed, 184 insertions(+) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index d8492af09796..cd77a19577fe 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -513,6 +513,30 @@ Returns: 0 on success, -negative on error See the SEV-SNP spec for further details on how to build the VMPL permission mask and page type. +21. KVM_SNP_LAUNCH_FINISH +------------------------- + +After completion of the SNP guest launch flow, the KVM_SNP_LAUNCH_FINISH command can be +issued to make the guest ready for the execution. + +Parameters (in): struct kvm_sev_snp_launch_finish + +Returns: 0 on success, -negative on error + +:: + + 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[32]; + __u8 pad[6]; + }; + + +See SEV-SNP specification for further details on launch finish input parameters. + References ========== diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 6a82767d940f..a7cbdc24ccdb 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -66,6 +66,8 @@ static bool sev_snp_enabled; #define AP_RESET_HOLD_NAE_EVENT 1 #define AP_RESET_HOLD_MSR_PROTO 2 +#define INITIAL_VMSA_GPA 0xFFFFFFFFF000 + static u8 sev_enc_bit; static DECLARE_RWSEM(sev_deactivate_lock); static DEFINE_MUTEX(sev_bitmap_lock); @@ -727,7 +729,29 @@ static int sev_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp) if (!sev_es_guest(kvm)) return -ENOTTY; + /* Handle boot vCPU first to ensure consistent measurement of initial state. */ + kvm_for_each_vcpu(i, vcpu, kvm) { + if (vcpu->vcpu_id != 0) + continue; + + ret = mutex_lock_killable(&vcpu->mutex); + if (ret) + return ret; + + ret = __sev_launch_update_vmsa(kvm, vcpu, &argp->error); + + mutex_unlock(&vcpu->mutex); + if (ret) + return ret; + + break; + } + + /* Handle remaining vCPUs. */ kvm_for_each_vcpu(i, vcpu, kvm) { + if (vcpu->vcpu_id == 0) + continue; + ret = mutex_lock_killable(&vcpu->mutex); if (ret) return ret; @@ -2139,6 +2163,109 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) snp_launch_update_gfn_handler, argp); } +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 = {}; + struct kvm_vcpu *vcpu; + unsigned long i; + int ret; + + data.gctx_paddr = __psp_pa(sev->snp_context); + data.page_type = SNP_PAGE_TYPE_VMSA; + + kvm_for_each_vcpu(i, vcpu, kvm) { + struct vcpu_svm *svm = to_svm(vcpu); + u64 pfn = __pa(svm->sev_es.vmsa) >> PAGE_SHIFT; + + /* Perform some pre-encryption checks against the VMSA */ + ret = sev_es_sync_vmsa(svm); + if (ret) + return ret; + + /* Transition the VMSA page to a firmware state. */ + ret = rmp_make_private(pfn, INITIAL_VMSA_GPA, PG_LEVEL_4K, sev->asid, true); + if (ret) + return ret; + + /* Issue the SNP command to encrypt the VMSA */ + data.address = __sme_pa(svm->sev_es.vmsa); + ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_UPDATE, + &data, &argp->error); + if (ret) { + snp_page_reclaim(pfn); + return ret; + } + + svm->vcpu.arch.guest_state_protected = true; + } + + return 0; +} + +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 kvm_sev_snp_launch_finish params; + struct sev_data_snp_launch_finish *data; + void *id_block = NULL, *id_auth = NULL; + 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 finalizing 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); + + 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->id_auth_paddr = __sme_pa(id_auth); + + if (params.auth_key_en) + data->auth_key_en = 1; + } + + memcpy(data->host_data, params.host_data, KVM_SEV_SNP_FINISH_DATA_SIZE); + data->gctx_paddr = __psp_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 sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -2235,6 +2362,9 @@ int sev_mem_enc_ioctl(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; @@ -2695,11 +2825,27 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu) svm = to_svm(vcpu); + /* + * If its an SNP guest, then VMSA was added in the RMP entry as + * a guest owned page. Transition the page to hypervisor state + * before releasing it back to the system. + * Also the page is removed from the kernel direct map, so flush it + * later after it is transitioned back to hypervisor state and + * restored in the direct map. + */ + if (sev_snp_guest(vcpu->kvm)) { + u64 pfn = __pa(svm->sev_es.vmsa) >> PAGE_SHIFT; + + if (host_rmp_make_shared(pfn, PG_LEVEL_4K, true)) + goto skip_vmsa_free; + } + if (vcpu->arch.guest_state_protected) sev_flush_encrypted_page(vcpu, svm->sev_es.vmsa); __free_page(virt_to_page(svm->sev_es.vmsa)); +skip_vmsa_free: if (svm->sev_es.ghcb_sa_free) kvfree(svm->sev_es.ghcb_sa); } diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 4961d2e67a4b..1fb6a6615d09 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1930,6 +1930,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, }; @@ -2064,6 +2065,19 @@ struct kvm_sev_snp_launch_update { __u8 vmpl1_perms; }; +#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]; + __u8 pad[6]; +}; + #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 Mon Jun 12 04:25:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275520 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 27AA2C7EE2E for ; Mon, 12 Jun 2023 04:44:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234991AbjFLEoL (ORCPT ); Mon, 12 Jun 2023 00:44:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234419AbjFLEni (ORCPT ); Mon, 12 Jun 2023 00:43:38 -0400 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2082.outbound.protection.outlook.com [40.107.243.82]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61DD210DC; Sun, 11 Jun 2023 21:43:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=O12/pME0uzjckk/ABw6QRC4tnn3lbuYvnXvz9qqT69vKlBPvuW6B2oDifguCp5TKopURcv+wdNufKXytJQUEWENeyohdiWeDSD3XwTQ0X2Twh56saQON8k8tn3NPgsUoB9+Nyf9KY8h5gjT4qu/QoDgpvDZaHshQPJPoj0+aJhbE2jTTXHlNT+Ngup04a9IK4gSZYf8DENeBaJZJePRlypn7ix7dYhDTcBcnbDciP0rrBPRjfG8gfKR96ZNJB4rtgNjgrcQGdiedTTqGP/YweBonTP/k2oiVtgktydCNXy6o4vg3jfEWkIic1UJW0wynXFAdmAzoUFZy4dJ6/5PGEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=8o93p9mYoqybhgkAygPC0Obm+t8oGE9xTvNcEMl5QmM=; b=APqRrqIBYBUQcXOMV4k/cxzuQ4hSe7cJSBvLqLZEJhjVgp+8T7GdRRRBuB2J66SG78XIvNG38q67t+0TSecOdfnlbbgQ8XGEkoMe0hKW3C7gnafCl+8J+vrZHrxg4jb740KTrFCNLV9DVgzNZXt0EAncem99WHwRFXKzrzJx8iLLeeDyAP8TbViiruWoTAYrLYm4yQL2eLKkqV4H5Nyngutap99tkEbFuKhhJghXwaviHzaywqw16gfeZUESxur8tfjL1TOCp9LueHR2+3UBEJXhCoF8x/iEfyTeDiBKSKz3esPp/gqw6ToHr8bz1TFggYK6ApuwyblWE1mOHEr6BQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=8o93p9mYoqybhgkAygPC0Obm+t8oGE9xTvNcEMl5QmM=; b=sJeOJvOVTtkjsMBo/7ie9TkKrFWihO4tOBUERnY7dAm//EW2LCRV16bQ1/Vuwej4icA2lO2FT7jO/Uw4r53MgKxMPJaXfenB4T9JcxHg7UVMmty4y/tPhZytVaJ8YLC3+h1z6dhYDsSYQUMw5ZmFzlD8xws3WsnyfQaPezoMAn8= Received: from DS7PR05CA0056.namprd05.prod.outlook.com (2603:10b6:8:2f::17) by DM4PR12MB6591.namprd12.prod.outlook.com (2603:10b6:8:8e::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.42; Mon, 12 Jun 2023 04:43:06 +0000 Received: from DM6NAM11FT024.eop-nam11.prod.protection.outlook.com (2603:10b6:8:2f:cafe::2e) by DS7PR05CA0056.outlook.office365.com (2603:10b6:8:2f::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:43:06 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT024.mail.protection.outlook.com (10.13.172.159) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:43:05 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:43:03 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 34/51] KVM: SVM: Add support to handle GHCB GPA register VMGEXIT Date: Sun, 11 Jun 2023 23:25:42 -0500 Message-ID: <20230612042559.375660-35-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT024:EE_|DM4PR12MB6591:EE_ X-MS-Office365-Filtering-Correlation-Id: 46dfd1bf-083e-4c81-62bc-08db6aff838a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xbR7swrgqngFy9fYN9jwKBTPShikayOIVylnXb1sUAyy/9mAA60vMeabHj2cu2WcJPmA70IJn/llAtuqXZi/0LgVJ/UeY8rT/aCf5hP0O9WXmCA4PUvEPKXyvK6LilGDQ6S6tzsus6EJig0Ei2Z9KxJRSoS3ysPlzBDx5Tv9j6VVBU2aInYA6zHR7tsOc0E5AbpM+u5G86pCxCzSjF1ChQgydywnVDuxhi5JRZt+nsuWzW8IU95+Ow0zn6tjkHV02YGOlnV4bzbMY+aUtZ4hytjC4V2O89zEP+Nx6FNcW3Nfwh71EMeuUiXGA/5Q0IBbRYPaklbYy+rfSdLT0o+Em0Q0t2dT3pjLNj3e94cJaaMGMz3RQC+qPe0oteTjDq1PI30n3B/89SDe8tu0FJogbTMmtJEjhoUZBPby2VVL6hh47yM4vdLKjVWQsmM3gtGAO7QHdTceznZfV4/A6Y8/mfEklKHthXWXfIRin/Z4jt/vQL/MnCdr3wrbO7d32iVV1PClT4G8P8Hh5p+Jw1aPpx06RyJy4shN2maASRpGFljZMykbw79EyyzK/QfIPbTCXC3kJ7lHGzCI8uOgiWxaEQOOoOnVhFlYlTb2Y1J5uRqTLbLsITJBegPuTqc0zUhh9/L/L5ZXQzBNSfM0yU6kLERCB7VaGakT8XH1dYkUA8TST4ReWEl6RFGd/m3PmCChUAd/mQBWBE/CV50NuWQQ7p6danGEF/9RECdKSCPBfQEIS5+B4j9Jpg5Ws/JjCWV+m4eIXGOdTI7wDoTYTEGhHA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(136003)(39860400002)(396003)(346002)(376002)(451199021)(46966006)(36840700001)(40470700004)(36756003)(47076005)(478600001)(54906003)(6916009)(4326008)(70586007)(6666004)(316002)(70206006)(86362001)(40480700001)(8676002)(8936002)(5660300002)(41300700001)(2906002)(44832011)(82310400005)(7416002)(356005)(7406005)(81166007)(82740400003)(2616005)(426003)(1076003)(26005)(16526019)(186003)(83380400001)(36860700001)(336012)(40460700003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:43:05.8365 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 46dfd1bf-083e-4c81-62bc-08db6aff838a X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT024.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB6591 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh SEV-SNP guests are required to perform a GHCB GPA registration. 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 VMEXIT, Verify that GHCB GPA matches with the registered value. If a mismatch is detected then abort the guest. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/include/asm/sev-common.h | 8 ++++++++ arch/x86/kvm/svm/sev.c | 27 +++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 7 +++++++ 3 files changed, 42 insertions(+) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index aaea4afcda98..d499730e1f15 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -59,6 +59,14 @@ #define GHCB_MSR_AP_RESET_HOLD_RESULT_POS 12 #define GHCB_MSR_AP_RESET_HOLD_RESULT_MASK GENMASK_ULL(51, 0) +/* Preferred GHCB GPA Request */ +#define GHCB_MSR_PREF_GPA_REQ 0x010 +#define GHCB_MSR_GPA_VALUE_POS 12 +#define GHCB_MSR_GPA_VALUE_MASK GENMASK_ULL(51, 0) + +#define GHCB_MSR_PREF_GPA_RESP 0x011 +#define GHCB_MSR_PREF_GPA_NONE 0xfffffffffffff + /* GHCB GPA Register */ #define GHCB_MSR_REG_GPA_REQ 0x012 #define GHCB_MSR_REG_GPA_REQ_VAL(v) \ diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index a7cbdc24ccdb..44fdcf407759 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3312,6 +3312,27 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS); break; } + case GHCB_MSR_PREF_GPA_REQ: { + set_ghcb_msr_bits(svm, GHCB_MSR_PREF_GPA_NONE, GHCB_MSR_GPA_VALUE_MASK, + GHCB_MSR_GPA_VALUE_POS); + set_ghcb_msr_bits(svm, GHCB_MSR_PREF_GPA_RESP, GHCB_MSR_INFO_MASK, + GHCB_MSR_INFO_POS); + break; + } + case GHCB_MSR_REG_GPA_REQ: { + u64 gfn; + + gfn = get_ghcb_msr_bits(svm, GHCB_MSR_GPA_VALUE_MASK, + GHCB_MSR_GPA_VALUE_POS); + + svm->sev_es.ghcb_registered_gpa = gfn_to_gpa(gfn); + + set_ghcb_msr_bits(svm, gfn, GHCB_MSR_GPA_VALUE_MASK, + GHCB_MSR_GPA_VALUE_POS); + set_ghcb_msr_bits(svm, GHCB_MSR_REG_GPA_RESP, GHCB_MSR_INFO_MASK, + GHCB_MSR_INFO_POS); + break; + } case GHCB_MSR_TERM_REQ: { u64 reason_set, reason_code; @@ -3378,6 +3399,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) exit_code = ghcb_get_sw_exit_code(ghcb); + /* 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; + } + ret = sev_es_validate_vmgexit(svm); if (ret) return ret; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 9a7cafb018fe..02edbdd443e4 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -205,6 +205,8 @@ struct vcpu_sev_es_state { u32 ghcb_sa_len; bool ghcb_sa_sync; bool ghcb_sa_free; + + u64 ghcb_registered_gpa; }; struct vcpu_svm { @@ -359,6 +361,11 @@ static __always_inline bool sev_snp_guest(struct kvm *kvm) return sev_es_guest(kvm) && sev->snp_active; } +static inline bool ghcb_gpa_is_registered(struct vcpu_svm *svm, u64 val) +{ + return svm->sev_es.ghcb_registered_gpa == val; +} + static inline void vmcb_mark_all_dirty(struct vmcb *vmcb) { vmcb->control.clean = 0; From patchwork Mon Jun 12 04:25:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275534 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CBB8EC7EE2E for ; Mon, 12 Jun 2023 04:46:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233298AbjFLEqG (ORCPT ); Mon, 12 Jun 2023 00:46:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235076AbjFLEoi (ORCPT ); Mon, 12 Jun 2023 00:44:38 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 518B910C0; Sun, 11 Jun 2023 21:44:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OoQiZSZJ3evbsB4MiGembIQ7pv8R7U/436c8hr7YAvDO789SmV8AUynOdBah55yBydVju8QIg9Nwibi+WVNrDELrx1dvrWSM+4V5gZcY0A3qhvhpdEATK2A+lzcsyJf2N9Y1o509P+mu5RQ08ybW/vC/3YwM0andkNXQrVwGIt66HMsaRczXs7WzJpYmADjB683HNuS2KQRpIR1BgtmNUdglrGrKZjQXMQeqPUmiOP8s/idnP2dzl12H+4CFJg+gUAJ8sIrh8jPh+yjapmrdwMO5qhjC77JeIJdJ6bKJaSR7oO8YrJkAtfCmaFw/dnza7/lhUynvA9k9KOJs8O6KfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=sWai5t1V65lb7xUaoMrw0zZrJ8os1/1Uqdh6BDEyZfA=; b=bnENNvoCuZ/PBvT8LNOfdIxi8coiDRoNatr7wmXXr07a/NJDqYQXFwNLIBQhMxozTDum44CzfIAtmM2BqAzJPX3hQW/MI/1OzIiW/FY/waAobABIWJxFkZycS15XEk8GMRy+IG3IS4W1xeSUIXJ/D5KejN9F+Wg/vOga49fDzVuFMUZmyedwsSTxGT7p84v2MY3btwnliIrBWcI42lZCOt29lgMCGVUtyO+GyCOjamSIAmVGiOGall7JTanYNt+cloTURfQp8Bdk14GLJYxly5n8ErNBCGNdlrVd5iOaCUAGDqkgfNGvB0GwvawewYW90S6m76Wu5rHzCTP3Nftm9w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=sWai5t1V65lb7xUaoMrw0zZrJ8os1/1Uqdh6BDEyZfA=; b=K8tMeCgFyBOgP4/qhbuVKWfPNXd0OfLuZJUOqYzqac/HGZqjVamSEMmgdE/XpDdCN4GpEYBwRGm80YX4KmUEe1/TdU+TJVoj4kcfnWZtAek6gThR1IGx8t8+EChwCWoM8WxZL3doVHfEfaRIwC43xyQ+mlPcsrxGMUfpjXmPUnI= Received: from DS7PR03CA0238.namprd03.prod.outlook.com (2603:10b6:5:3ba::33) by CY8PR12MB7241.namprd12.prod.outlook.com (2603:10b6:930:5a::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.44; Mon, 12 Jun 2023 04:44:18 +0000 Received: from DM6NAM11FT092.eop-nam11.prod.protection.outlook.com (2603:10b6:5:3ba:cafe::39) by DS7PR03CA0238.outlook.office365.com (2603:10b6:5:3ba::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:44:18 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT092.mail.protection.outlook.com (10.13.173.44) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.34 via Frontend Transport; Mon, 12 Jun 2023 04:44:02 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:43:56 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 35/51] KVM: SVM: Add KVM_EXIT_VMGEXIT Date: Sun, 11 Jun 2023 23:25:43 -0500 Message-ID: <20230612042559.375660-36-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT092:EE_|CY8PR12MB7241:EE_ X-MS-Office365-Filtering-Correlation-Id: eb2150d7-2116-484d-f9f4-08db6affae20 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: b98vSCAvPfVoSDdb4x4k8fKxjgS4WnROpmXnLYSppgfc1XsG7iGt3BP5JsWgnMa3HwjjHhgPfpCvRrTBZOo2tpu7bjFx5xAgFans2DQCuDz4ZCW74W3DZjBp8JcGKaeYhGix0SEs7ugNXjF8rylL+I4aag6pP/rLOtm5cpQ0SfW476D5mZ52GyX+gbcoKJ6pIOFW+qVClK84wJgTHUhziVA3NjqgnkeQRbTuc36TxQK84nuVAfyF4SxpPL56+9XR9sglYudQQA3vOtePlIjYMRatAIV/IkZG+LsSuNgI6P9O18/GwxUnnrokhyQNUMUSrnKbd3T/uYzlTBag9ZhMeww9OpryX9kPDNjb85a2gg4Xnb3glNsTHHtZV8N1WVT0L6mM2BScqnSx2ozaQfMdnRmJW715IVCA3qYII7cXGNi4aSZeJgEKeA9ZTFWwZyXCo9FnSAic4ZHDPdWf1tcRqFmrfJIjq981xnA2GrCyZf8YuheCSsRvTZUetkz6s0yTJ6IX7m3BXxm6ERNyrvwGUMge+rXcCOWSnUIZJQ6Z0SIk3GSTNr4zbPojguEH7/vueyA9CZ5PfH3P6Tqcx7VfhNjNZUQT1/ZJ7hi4ZKBVx04LjhRGRLf8+WgXlIH4mbLu9y7jrD3OtFyFrBZsgPTjeRRnI4IrmsHwRoKl0wnxW4PYhp+DQXqNOB+GzFWsjdwIeaP9yU+rPIvHIaGSMOjqVLRWHV0WDtRBAGm3/25CUnOI6kKcux+5Wxfq/qcuyaW3LikerNZxWFd7pXAofze/yw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(376002)(396003)(346002)(136003)(451199021)(36840700001)(46966006)(40470700004)(426003)(336012)(2616005)(47076005)(83380400001)(36860700001)(356005)(82740400003)(40480700001)(36756003)(86362001)(82310400005)(81166007)(40460700003)(478600001)(54906003)(44832011)(6666004)(8936002)(8676002)(2906002)(5660300002)(7416002)(7406005)(70206006)(70586007)(4326008)(6916009)(41300700001)(316002)(186003)(16526019)(1076003)(26005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:44:02.6940 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: eb2150d7-2116-484d-f9f4-08db6affae20 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT092.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7241 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org For private memslots, GHCB page state change requests will be forwarded to userspace for processing. Define a new KVM_EXIT_VMGEXIT for exits of this type, as well as other potential userspace handling for VMGEXITs in the future. Signed-off-by: Michael Roth --- Documentation/virt/kvm/api.rst | 34 ++++++++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 6 ++++++ 2 files changed, 40 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index df37aa11512d..028fd3fa50a7 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6780,6 +6780,40 @@ Please note that the kernel is allowed to use the kvm_run structure as the primary storage for certain register types. Therefore, the kernel may use the values in kvm_run even if the corresponding bit in kvm_dirty_regs is not set. +:: + + /* KVM_EXIT_VMGEXIT */ + struct { + __u64 ghcb_msr; /* GHCB MSR contents */ + __u64 ret; /* user -> kernel return value */ + } memory; + +If exit reason is KVM_EXIT_VMGEXIT then it indicates that an SEV-SNP guest has +issued a VMGEXIT instruction (as documented by the AMD Architecture +Programmer's Manual (APM)) to the hypervisor that needs to be serviced by +userspace. This is generally handled via the Guest-Hypervisor Communication +Block (GHCB) specification. The value of 'ghcb_msr' will be the contents of +the GHCB MSR register at the time of the VMGEXIT, which can either be the GPA +of the GHCB page for page-based GHCB requests, or an encoding of an MSR-based +GHCB request. The mechanism to distinguish between these two and determine the +type of request is the same as what is documented in the GHCB specification. + +Not all VMGEXITs or GHCB requests will be forwarded to userspace. Currently +this will only be the case for "SNP Page State Change" requests (PSCs), and +only for the subset of these which involve actual shared <-> private +transition. Userspace is expected to process these requests in accordance +with the GHCB specification and issue KVM_SET_MEMORY_ATTRIBUTE ioctls to +perform the shared/private transitions. + +GHCB page-based PSC requests require returning a 64-bit return value to the +guest via the SW_EXITINFO2 field of the vCPU's VMCB structure, as documented +in the GHCB. Userspace must set 'ret' to what the GHCB specification documents +the SW_EXITINFO2 VMCB field should be set to after processing a PSC request. + +For MSR-based PSC requests, userspace must set the value of 'ghcb_msr' to be +the same as what the GHCB specification documents the actual GHCB MSR register +should be set to after processing a PSC request. + 6. Capabilities that can be enabled on vCPUs ============================================ diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 1fb6a6615d09..175b958f103f 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -279,6 +279,7 @@ struct kvm_xen_exit { #define KVM_EXIT_RISCV_CSR 36 #define KVM_EXIT_NOTIFY 37 #define KVM_EXIT_MEMORY_FAULT 38 +#define KVM_EXIT_VMGEXIT 50 /* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */ @@ -527,6 +528,11 @@ struct kvm_run { __u64 gpa; __u64 size; } memory; + /* KVM_EXIT_VMGEXIT */ + struct { + __u64 ghcb_msr; /* GHCB MSR contents */ + __u64 ret; /* user -> kernel */ + } vmgexit; /* Fix the size of the union. */ char padding[256]; }; From patchwork Mon Jun 12 04:25:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275537 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BCC39C7EE2E for ; Mon, 12 Jun 2023 04:46:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234785AbjFLEqp (ORCPT ); Mon, 12 Jun 2023 00:46:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235131AbjFLEom (ORCPT ); Mon, 12 Jun 2023 00:44:42 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2075.outbound.protection.outlook.com [40.107.237.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DBC351BC6; Sun, 11 Jun 2023 21:44:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VOmWeOCa6NkYH7ePV3RsOvkDgBdH4Chk5VCtWI8IYBX/NzqQ+uUg4n747GRM0oCbxzs5+m/xLHZhfv5Atb14ieTbqW2iuBbbPOJr1otAAmScD1Jaz9leomaopWIXZ0NuYVnOMnYUNDGO3g8bzodyh+gS0rw5HuvG8dlP8lkLVT/VKzli5MTnlckbJ/TmqpHKnKGNJ8rvkNx3GcnnEk2LGUgZA9JSsVAYbLf0k0PgEFHnC5gzMDUNAnXIlrzy49YS1Vs+ToXlgcUKHDHSOi3CyjdW53bXx/sEUzDbkK26KTn4q/JdTE57t1O2xzVcHsqEnML6Jj2qVqp5qriTamwwvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=3sDrVPW9K6hr5iqjy87+FTvyv+9ltC0aCsqlHuApz6k=; b=eBbZMCxgr7rn9nKM7H+ydg7gfayNpM8tczu6Ao6pJ9J7wYBK3WB6ngIhpHZrULiOGIpFJDwCwAx/p3H7/5J25H91aQd4+MbB3DQsgVlBJZafwTQDcSnBGPWxBCyxnZ0fD1dS8OT2yvBHKnt4IHDOoh/uNl7FfnZA+zCXPmpHTfv3Ve4JZchQftPun3GpIgn3EaBAVmHgpamnGCBBM76DmAcoqo47AUTrzaHiTIseRqkVXGiAwOP59XnH7Fnr8RhmggAnx2VgGChpas5s4Xbqdse+zaAJ4eHgGaf3EPQ3TnivdRjTkdXVKaeJQZKp0+7TgvI8QI1L/PtVNCTvNfO1NQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=3sDrVPW9K6hr5iqjy87+FTvyv+9ltC0aCsqlHuApz6k=; b=ZFg1dBhIO/GPB96ZYa0CbeIMDY4CVnzwabNCjvbpGdwRsIe5XkfcW4UCW2bzHvNKCam70NLibNEODKA87b1Bl1eHlt9fknedxu5ttOgVgHc8rA4zk3fX67zLSMmBJ6XmGXWEJ6ZRK2Mcn0r7pRnxElMvJLFs5blIVRJrjLih05Q= Received: from DM6PR02CA0113.namprd02.prod.outlook.com (2603:10b6:5:1b4::15) by DM6PR12MB4500.namprd12.prod.outlook.com (2603:10b6:5:28f::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.46; Mon, 12 Jun 2023 04:44:19 +0000 Received: from DM6NAM11FT046.eop-nam11.prod.protection.outlook.com (2603:10b6:5:1b4:cafe::f4) by DM6PR02CA0113.outlook.office365.com (2603:10b6:5:1b4::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:44:19 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT046.mail.protection.outlook.com (10.13.172.121) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:44:19 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:44:18 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 36/51] KVM: SVM: Add support to handle MSR based Page State Change VMGEXIT Date: Sun, 11 Jun 2023 23:25:44 -0500 Message-ID: <20230612042559.375660-37-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT046:EE_|DM6PR12MB4500:EE_ X-MS-Office365-Filtering-Correlation-Id: 7d9c5aea-57bc-4b7d-8f60-08db6affaf32 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YnoRJBIiWIS8VmyL7Q71Yz9l76DEfTekyzl/7U8sJW4ZomDagrcBw4q+7e0B4+ge21/zXE7IgkBmUOpPuIS3Vyq0SBsOTiBWoGiygSDwp8DwZsATzmnJvL93/5eqGfnboNR2AjvgBLErx3m1aNe17uZWOkqL/HJgNUv2clhZIXxj8328s10Mh7hAOvVHBrinjeV3Z1IgMOEJtu80oKpYPnOtRC/DubE1B5cjAzmUe9RYIQTMo0Jdz6Oj3hMxW7/qx+1xMOm9GxK1c6Eh9c1VstOB5jNghJ2e4Ib3Dve4gvIY2q0+Z1Z6IoIdHAzZp7h7CPk3xzArVfoFVe9SGIy6wesCLwVlbSxPHZhguzgHR0bukTnTr7nIzBvZDj5J2JnJ+12KoPdNRfh5tArl5uFXF+DV0TczLiVahUrrwq+eL2uCzwB63f5TenNUnKjE9duvgH6tGRsSSIc+BvVK6aWoBSWJXIMQEnRAqe1x0tcYHyJv8PX0zVl2Gkv8ccm1++a9pyyaYFXWJCV57em8ZrgcVFOlde2O0tM3tfaaQ09/C0B6V0slzxhjL6rMI3oW1eHpUp30MpxSehxglBFhqpBwdUk8APBHKPnvuzZa1w732I4FjgVOawmPzAuiJCULHNGobzFbmoQmE3lOa6EC/b+TciBikkpjjU0zevWu5r2IKmJCplqky14Bd4ppQ3ZvZF/3ivmwHNxsJd01RSoZOIWEp92Oo4CLHDJiFgSFiWAZBEZ9TZyjKyoqkMQRCz5q1e3alXMV0HG5GddoUXS0pGCUBw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(396003)(376002)(136003)(346002)(39860400002)(451199021)(36840700001)(40470700004)(46966006)(7416002)(7406005)(5660300002)(70586007)(70206006)(4326008)(6916009)(44832011)(2906002)(316002)(41300700001)(6666004)(54906003)(8676002)(8936002)(426003)(16526019)(336012)(2616005)(186003)(26005)(1076003)(83380400001)(47076005)(36860700001)(86362001)(82740400003)(356005)(81166007)(478600001)(40460700003)(36756003)(40480700001)(82310400005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:44:19.0807 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7d9c5aea-57bc-4b7d-8f60-08db6affaf32 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT046.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4500 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh 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. When using gmem, private/shared memory is allocated through separate pools, and KVM relies on userspace issuing a KVM_SET_MEMORY_ATTRIBUTES KVM ioctl to tell KVM MMU whether or not a particular GFN should be backed by private memory or not. Forward these page state change requests to userspace so that it can issue the expected KVM ioctls. The KVM MMU will handle updating the RMP entries when it is ready to map a private page into a guest. Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- arch/x86/kvm/svm/sev.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 44fdcf407759..2afc59b86b91 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3233,6 +3233,15 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) svm->vmcb->control.ghcb_gpa = value; } +static int snp_complete_psc_msr_protocol(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + + set_ghcb_msr(svm, vcpu->run->vmgexit.ghcb_msr); + + return 1; /* resume */ +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3333,6 +3342,13 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_POS); break; } + case GHCB_MSR_PSC_REQ: + vcpu->run->exit_reason = KVM_EXIT_VMGEXIT; + vcpu->run->vmgexit.ghcb_msr = control->ghcb_gpa; + vcpu->arch.complete_userspace_io = snp_complete_psc_msr_protocol; + + ret = -1; + break; case GHCB_MSR_TERM_REQ: { u64 reason_set, reason_code; From patchwork Mon Jun 12 04:25:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275535 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7D51BC7EE37 for ; Mon, 12 Jun 2023 04:46:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234180AbjFLEqI (ORCPT ); Mon, 12 Jun 2023 00:46:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235543AbjFLEpX (ORCPT ); Mon, 12 Jun 2023 00:45:23 -0400 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on20621.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e8b::621]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 37B3E171B; Sun, 11 Jun 2023 21:44:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nKaPZ7aEC2jAlLLPmgR+fbjpfjmlpCX/egPf7OZMIX4kw31xOJgtkGlAwddWSbvxNKbcVHahsdP43jI188X/Q9fTckIvO0VspYqm1UoiJUFfOwuZhciHwV/QVic8pqvLkx1nPpp1pgtpd+iK9mfDpJcMVu/pTI3w1eycFe2MRY7KezNWVjxtyK7hnu5bzxJeimllUiy3SB25C3UIRh+8aPYy+prQMei9CcSN8vqyWyuOgBdvgcU9w6OP7bqO/Ov1p01iSif8GFWPRTy6oI068clpeXm0od6ERbosrjmqauqqc2sxBP51iChPekCmnHvlvBLyXaaPaUySPs29e6yuUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=dGa3k8AqLLW1tjtn3WwuRM7764enwizu98BPdPH3+7E=; b=Rrv1aR+LqM+8MfM67HNHDMUDtYSUeT3doTEpcAfQ4DtBb1s7sRWvZxC72nBMC8fIFU+0ccTCKfxwwRqzgRQg9nF2dPsOHKasu0aZeuWjXkWMmeAVcxXEdJFvNviToOMAHVlDrtumbwHUgpqx919/9TVBXkk7IRoI5Pd0BaOpd0ZiPml0fhLfcxqzlH4ytsrFzWjxL2e8mfsLt5HeCy8fps5xMCa4CDZRB2rvhlA0AuWlM+6Q8rikMG2w7PI9BPq1e7TFSxLPXPWSBEvxgTWa2akp/xfAyZi9H+zCPugtVxQD+rtqXNLneERf64t9Cc8dmAmndUGCLHS47I7y9kYIgA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=dGa3k8AqLLW1tjtn3WwuRM7764enwizu98BPdPH3+7E=; b=jTlHICZQsVSzwXB4VGld5ziBwiQpm+spu7W8hU9v21MqeFPT+hOhWl3azHL68az39CIPXPPwVqvyez4jl5L0DFSPUCuYW4fGuHA1svMWlkgiU21rULNIXQXAp3vsvBj6k1VAR/bzT/gwsZx73jLMZKFfoGKC0FyQigg88l3Zdhg= Received: from DM6PR13CA0068.namprd13.prod.outlook.com (2603:10b6:5:134::45) by SA1PR12MB8120.namprd12.prod.outlook.com (2603:10b6:806:331::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.33; Mon, 12 Jun 2023 04:44:43 +0000 Received: from DM6NAM11FT012.eop-nam11.prod.protection.outlook.com (2603:10b6:5:134:cafe::dd) by DM6PR13CA0068.outlook.office365.com (2603:10b6:5:134::45) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:44:43 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT012.mail.protection.outlook.com (10.13.173.109) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.29 via Frontend Transport; Mon, 12 Jun 2023 04:44:43 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:44:42 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 37/51] KVM: SVM: Add support to handle Page State Change VMGEXIT Date: Sun, 11 Jun 2023 23:25:45 -0500 Message-ID: <20230612042559.375660-38-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT012:EE_|SA1PR12MB8120:EE_ X-MS-Office365-Filtering-Correlation-Id: 7a03cd3d-401c-4a77-a599-08db6affbda7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xcB6CwXrHsHJ1KLRi1+GIz3PgRuuOCmHjWLwGIQfSvedys5FVRUjiXor8ghIJR2dmyc58lHkNg5jTMdEBwMebyAiOlkvI0N2HSUd9xMfVu8W8XnUtNhkNgiqvhCjDMm5lGNeoMRh0CXIabBybwvm016jTrtkSD67EnMSlnp/Pm22nGCowaFts0t+ytVdRi1zamnD6pLs51aWiYWSGC7ZupGxMBU+QcUuSAuUhYPcAdHs8K2O3AOvoWT72PsSMw51XegVsxdijOJMZutGgFYZcNvs79teq0o6CoTAaH/du6xomzNQ+IJLrGDBG6o2jCUbV20tSgL8md3VTWybsRbiyNS/1FiZTBEEawGGyS/SVnUczOV8B047BSw/d+nmCJuIA90qK2K/mMTMPYgOtuXn/tQTJr1SmSl9EQHv3dMHjTEDqlTRa9HC96yT16s6zAhCi4GCBAD6txPistJOZvhq0GJDFS+dylcXISAseEluWn277Cha1O42g5T8kO+Qz+9sr8LgjFL7jUxL7fp6QCytPFEu3+RtbHFewl5ydrr4hVa5qnW4equTmCJSuSRZgCSPho36oGjn6paw2fPuCvE8+8YlNq45EEfQWUyBHdNr1W6/1Sb19Nu9HkR+8axZgJeT7VYh4DWzp2c6ezU6h5K+paoo9WxW7ikpl4pS2t2y+ujejTNsx2zX66IVxIRWQGosY6UGC0FYEEzrHZOJAtMDsSJutCBCP3ALSC1lemSmOTCmBcTQoEs0rAXH+LRrOYvc/yNvzLa9iLVylSIQIp0byw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(346002)(39860400002)(376002)(136003)(396003)(451199021)(40470700004)(36840700001)(46966006)(40460700003)(66899021)(1076003)(26005)(16526019)(40480700001)(186003)(5660300002)(41300700001)(426003)(82310400005)(36756003)(47076005)(36860700001)(7406005)(7416002)(44832011)(70586007)(4326008)(70206006)(8936002)(83380400001)(336012)(478600001)(8676002)(2616005)(54906003)(2906002)(86362001)(6916009)(356005)(316002)(81166007)(82740400003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:44:43.3361 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7a03cd3d-401c-4a77-a599-08db6affbda7 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT012.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB8120 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh 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 version 2. Forward these requests to userspace as KVM_EXIT_VMGEXITs, similar to how it is done for requests that don't use a GHCB page. Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- arch/x86/kvm/svm/sev.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 2afc59b86b91..9b9dff7728c8 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3039,6 +3039,7 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) case SVM_VMGEXIT_AP_JUMP_TABLE: case SVM_VMGEXIT_UNSUPPORTED_EVENT: case SVM_VMGEXIT_HV_FEATURES: + case SVM_VMGEXIT_PSC: break; default: reason = GHCB_ERR_INVALID_EVENT; @@ -3242,6 +3243,15 @@ static int snp_complete_psc_msr_protocol(struct kvm_vcpu *vcpu) return 1; /* resume */ } +static int snp_complete_psc(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, vcpu->run->vmgexit.ret); + + return 1; /* resume */ +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3485,6 +3495,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ret = 1; break; } + case SVM_VMGEXIT_PSC: + /* Let userspace handling allocating/deallocating backing pages. */ + vcpu->run->exit_reason = KVM_EXIT_VMGEXIT; + vcpu->run->vmgexit.ghcb_msr = ghcb_gpa; + vcpu->arch.complete_userspace_io = snp_complete_psc; + break; case SVM_VMGEXIT_UNSUPPORTED_EVENT: vcpu_unimpl(vcpu, "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n", From patchwork Mon Jun 12 04:25:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275536 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E910FC87FDE for ; Mon, 12 Jun 2023 04:46:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234296AbjFLEqN (ORCPT ); Mon, 12 Jun 2023 00:46:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235750AbjFLEpu (ORCPT ); Mon, 12 Jun 2023 00:45:50 -0400 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2089.outbound.protection.outlook.com [40.107.92.89]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17067A9; Sun, 11 Jun 2023 21:45:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=niBCB9g78SHtte+VkwNKWSL37ouWuBmblcvgIbkfv9/G3trub3uqMGsp3vsClds4puZocPUxnT2Vkr04kcvnL+qVsTIT4sKk4JO0V/LymMnajmbvGNdAQwsGPktSnCQebKRaxRy0vmojaQc/dP5xVQPUHt56ZB1Wf08zfvpb+cjPuKuy6BdgZWtUcfUxtUrifhoBBVeEfkLwIkZTAQF5vY1nxTdpLxI1AkGgLq+Ss1qR1JMOn8kjZ5zre+63tGW7dYorXXHupk44CcUVVa6SpiMOWGVrrg9eSvHrmkxSPrLwzGkb1Pr5z6WPES9vpcfRjXmmpsS3osB7yzXtmXWOxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=iPne3TgHOrPCdZDAp0qle3bNYwKkBJf0eNd3G8dRABg=; b=UmodQYZB5mERboIo6UtKCjIV0ECrNI2JLFH6HL3NcdvjxOywBBv53wLk7xpdEE9q35AlFJlpnHWLcOTIJqUWpzp8ExnTjXCVoyiS+VG5t63nMLs2su90KbLB/8anIWoOsipFVn5lcetulPR9HHi0Jz4O4E+hQ0n1GrBfFw34hoIaXdJ3BqfMn7wFeO3oNs+KCVzAelZr/PVWk9GC1dLTMebjRhrCQwcP4cGmKI0dI+X3vtYPx/BLHM/zQleXgtHFTENjw2aDkWAdbBVvA3nG0kZhWpYOWh6fNCiJ1lUfw7wg0SnJBCX+QxzE2wIfrA+PDiQLBxA3VO/V68cWK5NLMg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=iPne3TgHOrPCdZDAp0qle3bNYwKkBJf0eNd3G8dRABg=; b=3MbAzWy++c82qom2Jh3QOB3Re0H8ipMe2eDzugUQST5pamcC85Vc14iiD9XL1MwQSJVdTKV2cNK/bLWpDvwUbGztsYPMRQlLwki4+iR6D8IgXyxTJV9lbsGlNcCKQ9spXch3uhJtjOwohumUe70PiS3EO7TioNS+7Daf+ls/RAQ= Received: from BYAPR05CA0068.namprd05.prod.outlook.com (2603:10b6:a03:74::45) by LV2PR12MB5725.namprd12.prod.outlook.com (2603:10b6:408:14c::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.29; Mon, 12 Jun 2023 04:45:46 +0000 Received: from DM6NAM11FT040.eop-nam11.prod.protection.outlook.com (2603:10b6:a03:74:cafe::d7) by BYAPR05CA0068.outlook.office365.com (2603:10b6:a03:74::45) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:45:45 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT040.mail.protection.outlook.com (10.13.173.133) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.29 via Frontend Transport; Mon, 12 Jun 2023 04:45:44 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:45:16 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 38/51] KVM: x86: Export the kvm_zap_gfn_range() for the SNP use Date: Sun, 11 Jun 2023 23:25:46 -0500 Message-ID: <20230612042559.375660-39-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT040:EE_|LV2PR12MB5725:EE_ X-MS-Office365-Filtering-Correlation-Id: f1efeb91-f5a8-485b-f3df-08db6affe24f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: wpywAS+W9P7Vgt28bgPCLwxkXiYKi9rqssu9d6pI/XwesOEAayHV+SNu6XdSKYI8m6p6Qexq44Jqydiw+xwNpKPaZz4duLBp5RllvCIXIj5fVcvx/8/vKcywST3xUF6IiREux46MIQP1nA+PfWJ7ALLiVnXHZ8XruN5/4VKvTt29R+Eva23aaJLUctt5Ck0zGmiErguiE90unoWG8fT36rRewDMSOHQ4SUNe1sUpQyFMSVo+mhyOkmWnd7vNi6x3EhkwxabFL1g9vzqwSDD8Omovm1vo3glcrPn1/zklG7lAPLK3FY0Ommrcf7z1tfcIkSYyK+x/F/l1Wcxo6j0HRyhtHxgNpmmRC1x/pCfiqnXwEjZvOg5QeHcAb8lFn6Mu5XE7vH0JmrpxG39EWO24QNGFPHwDgnoqEwaIYJx2HqGjxls5UP6Y3/ugSA7zrJhkpSQd6KYHkViRpbqPplR53tb1favlBJlyMFQK3mCi9eD8ZmAW+nFSbJc+QmipsOVqSdCaUbVDkDptpiWAfpanlImPPj1hP/0FIOAnKw0p++xuRVBx9Lu0p3Vzqd1AAs7Jwi3almnA+37YorZXDdiTyAZjdX15QBk+VfHm5RTzaPvyHf3nGdEIUN281JuwmiOKE13Dde+zlTbuF4zsNrfutGf7KFMYH7w5rdMYLNmLc3T4smQxarExc4oFeLRH3+cDfHxMb8TQitaxPvY2TqbZgiPr2mPQ0VPJNGhMmxm9NxKq0GiqoExFH+W+FI9opvLXEKA91Sh7dC54WcuX+DkrzA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(396003)(346002)(136003)(376002)(39860400002)(451199021)(46966006)(40470700004)(36840700001)(40460700003)(6666004)(36860700001)(36756003)(47076005)(426003)(336012)(83380400001)(2616005)(82310400005)(86362001)(82740400003)(81166007)(1076003)(26005)(356005)(40480700001)(186003)(16526019)(2906002)(5660300002)(54906003)(44832011)(8676002)(7416002)(8936002)(316002)(7406005)(41300700001)(6916009)(4326008)(70206006)(70586007)(478600001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:45:44.8371 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f1efeb91-f5a8-485b-f3df-08db6affe24f X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT040.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV2PR12MB5725 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh While resolving the RMP page fault, there may be 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, 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. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- 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 8d2bb3ff66a2..026bfc4446ee 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1851,6 +1851,8 @@ void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm, void kvm_mmu_zap_all(struct kvm *kvm); void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, u64 gen); 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, unsigned long cr3); diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index 92d5a1924fc1..963c734642f6 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -235,8 +235,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 0d3983b9aa7e..dff0eb018b27 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -6732,6 +6732,7 @@ static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm, return need_tlb_flush; } +EXPORT_SYMBOL_GPL(kvm_zap_gfn_range); static void kvm_rmap_zap_collapsible_sptes(struct kvm *kvm, const struct kvm_memory_slot *slot) From patchwork Mon Jun 12 04:25:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275538 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6EA61C7EE25 for ; Mon, 12 Jun 2023 04:49:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234701AbjFLEtT (ORCPT ); Mon, 12 Jun 2023 00:49:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235329AbjFLEsD (ORCPT ); Mon, 12 Jun 2023 00:48:03 -0400 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2073.outbound.protection.outlook.com [40.107.92.73]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C4BC4E5E; Sun, 11 Jun 2023 21:47:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CkmBUcULQoz6g5oqyaqFulU/sfr/HbKIBqsafxdviY0z8H7gFDcDzyeXv1gYPKnaxAjPx+BrzHBUc4TVQhSUrK8G78YgaVhDvkKOAjW/ZUQrEK5jdrmUwibxnrzMfbFWwzeURRDMr+hm3AmXu5g+5DY/EJ7Iz8IDv0tp3ZVG3WC/S13VlXi2/7zUpr+0uUHEpFmgfbY1qAviACWAq4yxMBmVvd3O+aPOD5/bfGE0t1/AtKGCQ+SnLzBzYiczWxQnq8KmXVDrwF0/vYIVnA1JUgcxw0ymHmi0DMEK/I1fgZUfYdjV7iNPIxm5EFnuTgRJWJSJWH/X4VFhj26tBrGhag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Rx8POHMeEO6GjBi6mYzRZT5bILI2FMQUM3oFJFtiCLY=; b=lnl5Momd+9jObD0Md5oeQVp7JZWml0r1TO5mm71ArD9MF0rwyW1c/GQ0b4AueL7KJyd9YGaP6eM4rWTqw4XmE0gdIDWyFWpEGu0fhToSltViNTK3DpU6aMabimmoEu3RMP3KURsnxK0z0ZtzcDu5pNdRO+UfSHxQw+Zip3ZWMFIQ5wk1ABjDO4lSfnriU/cqbdNQKycIUv+/2GlM49m7fEtLMme2honU3wJULg/kyATXVzYYMlNzGH7H/Zo0/oPzs1R2EbAZAKrOH0EKvIZguaCxXEBPLtL7DYWu284YQA5cPBvqbniSpChU9l8R9lrqKlrKH3+fGgFAPvCy/KvNmg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=Rx8POHMeEO6GjBi6mYzRZT5bILI2FMQUM3oFJFtiCLY=; b=eU2LCqFbd/NtkXBNhn4oNsZ9LCJbN6tASAK603dcXmOhiS0sSGHGumB+5hFj3vhXkPFLpIM7zE52J/7xXV15h/mn2j0XaNFSGf0vtRkID4AvRhYBZ1rbSyQzGFmNIwoBKiWmqd+U237q/XlHEHhP5Kxha6rAekgCx6qMyYbGSrE= Received: from DM6PR02CA0058.namprd02.prod.outlook.com (2603:10b6:5:177::35) by CH3PR12MB8259.namprd12.prod.outlook.com (2603:10b6:610:124::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.44; Mon, 12 Jun 2023 04:46:53 +0000 Received: from DM6NAM11FT097.eop-nam11.prod.protection.outlook.com (2603:10b6:5:177:cafe::34) by DM6PR02CA0058.outlook.office365.com (2603:10b6:5:177::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.34 via Frontend Transport; Mon, 12 Jun 2023 04:46:53 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT097.mail.protection.outlook.com (10.13.172.72) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.29 via Frontend Transport; Mon, 12 Jun 2023 04:46:53 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:45:51 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 39/51] KVM: x86: Define RMP page fault error bits for #NPF Date: Sun, 11 Jun 2023 23:25:47 -0500 Message-ID: <20230612042559.375660-40-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT097:EE_|CH3PR12MB8259:EE_ X-MS-Office365-Filtering-Correlation-Id: e177e363-8050-4100-9f42-08db6b000b01 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: SWhc41FjYkcwFVb61dw840GlOpjTFgiF60jbUN7hLBmhNskATgqiFMFDybAuLAARZpdBy8OlBTlI6K1RetT+k1+g0Sb7ROIQrDPeVMMnPPaiAFCdndLBGZl8dfH0uAKV35PB7wSBVylNVYoJRgeB4+v6hKmk6q5KlOY7u9GS39cCX1cKgo6690vOgYrkOr95wuZHQw/sIKiCFfIgMZCLyhCYoyc26rqHIrmSLpTy9LvhH7J/vplItX5KSF1ECvQD6pnWB2pHfLPxa/Q9XXZIH0Xz8MBOOL1QI4YAExO0w48QziWZ68xARaNrkeVNLTSFCkME5CrRWQQ1ix3tnaQB5+UzdlhRocKSLHvoWBkqg3nP1ik7LVczWlJHcFmsCai2abVb/4myEHroPTfNW2rVM7hDw30wTn1vY459chdgP6LZ/xNbFeoPYUciEBtkk/XBkLGqjL843atIoJBZcoJA4HAiGi/DH3oE18/lhGG11NdOAmVm9lY/BgGSrLeJljNDjOGJ5/0uYnJHe9u1BhxPo+gB7MriGNUgoPZfo5LCRaY56wbUEeHZ8fYfF9M6sG9jFTte1LeULIcN6t5cYoO2a2LBdGnHDPtT9kskVlgDlKlOTbF+ne2dprVw0EywgPmO/jeZJJlJELauI+J4//4EX5bXXHSJppt05n2IEYTK/I2R8kS/nqjMf0teG8kTtkRsemwMeKPBjdwFCUS3tDB2OHvAb+7Vi6REnyzoScLxg7zdF5pYkYjAk52rpExrhciGIn9uTGvGpUOd2CGeVJx45A== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(396003)(376002)(136003)(346002)(39860400002)(451199021)(46966006)(40470700004)(36840700001)(44832011)(8936002)(8676002)(70586007)(70206006)(2906002)(4326008)(6916009)(7406005)(7416002)(5660300002)(316002)(54906003)(478600001)(6666004)(1076003)(26005)(16526019)(186003)(41300700001)(36860700001)(356005)(82740400003)(426003)(336012)(47076005)(83380400001)(2616005)(40460700003)(40480700001)(36756003)(82310400005)(86362001)(81166007)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:46:53.1103 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e177e363-8050-4100-9f42-08db6b000b01 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT097.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB8259 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh When SEV-SNP is enabled globally, the hardware places restrictions on all memory accesses based on the RMP entry, whether the hypervisor 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. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- 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 026bfc4446ee..2fcd309fd9fb 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -253,9 +253,13 @@ enum x86_intercept_stage; #define PFERR_FETCH_BIT 4 #define PFERR_PK_BIT 5 #define PFERR_SGX_BIT 15 +#define PFERR_GUEST_RMP_BIT 31 #define PFERR_GUEST_FINAL_BIT 32 #define PFERR_GUEST_PAGE_BIT 33 #define PFERR_IMPLICIT_ACCESS_BIT 48 +#define PFERR_GUEST_ENC_BIT 34 +#define PFERR_GUEST_SIZEM_BIT 35 +#define PFERR_GUEST_VMPL_BIT 36 #define PFERR_PRESENT_MASK BIT(PFERR_PRESENT_BIT) #define PFERR_WRITE_MASK BIT(PFERR_WRITE_BIT) @@ -267,6 +271,10 @@ enum x86_intercept_stage; #define PFERR_GUEST_FINAL_MASK BIT_ULL(PFERR_GUEST_FINAL_BIT) #define PFERR_GUEST_PAGE_MASK BIT_ULL(PFERR_GUEST_PAGE_BIT) #define PFERR_IMPLICIT_ACCESS BIT_ULL(PFERR_IMPLICIT_ACCESS_BIT) +#define PFERR_GUEST_RMP_MASK BIT_ULL(PFERR_GUEST_RMP_BIT) +#define PFERR_GUEST_ENC_MASK BIT_ULL(PFERR_GUEST_ENC_BIT) +#define PFERR_GUEST_SIZEM_MASK BIT_ULL(PFERR_GUEST_SIZEM_BIT) +#define PFERR_GUEST_VMPL_MASK BIT_ULL(PFERR_GUEST_VMPL_BIT) #define PFERR_NESTED_GUEST_PAGE (PFERR_GUEST_PAGE_MASK | \ PFERR_WRITE_MASK | \ From patchwork Mon Jun 12 04:25:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275539 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 229F2C7EE2E for ; Mon, 12 Jun 2023 04:49:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235183AbjFLEtV (ORCPT ); Mon, 12 Jun 2023 00:49:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235433AbjFLEsq (ORCPT ); Mon, 12 Jun 2023 00:48:46 -0400 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2079.outbound.protection.outlook.com [40.107.92.79]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B271E1BCD; Sun, 11 Jun 2023 21:48:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ibpd94S9wJ3R8CscOFRtFYoJjT7wdfbFkoi0cpevstqNCy450RJrBDGAfHhF/cbUlVDDCu5GErJNw6Uu2UkK2mShVCJnAmiNxXIwTZ9FiPnRZsxwM5pmoRfkqECtMmRAEv54kDovelxsXNr1qBaZn9hU6fc2nhjufYeq4k8Z/W/ajMvqUKvmD00uUEswBq0rNI2ZjoR3Vz5LeXdf2srrfhd1qkKOUzspKU9ngJAv52TdqOROTovn6IDS9bFu68JUVx/H56DFDLMo6zimZTWlCg8+U/OAh+tDofozEovLLBQ1WKDitzXTylOxzOxdSRX90dJyXIeFhFz7b8RckJV+Ow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=E3y+s7+t3OqbSQoznIHk/l3yDEommf/ftwO+lV/tIro=; b=PhCOCDg9L6nejZyiXnmARwOdXw/k9l9cluYFIP+pgi/AheMatX/WdLzGBcQeBWFUIl3UEdDc4GqaaBXXG20OPa2ANtnUmHVAYxQfUrgT0GQQkJDUU0C5ZJMviLtCzoLCal6O/VIx3riRXyVFWHdH3SEGFzeBBN4wDhQuaZGpPUxq9oNgu9BwEO3yYM+Dgga+1r11O08Lej7xL5RpVV1XMesnm4iL0qy3MG8dT22hDJf92/ugSLLIhn8cRQM+/XB9nQANpb581YmuqBbbWLnMh+COkzIX2fg79bukspyUhFWFpfQsOCCsJb/IDAWrQTTaHGbZSr5uI9W76YKPoutkIQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=E3y+s7+t3OqbSQoznIHk/l3yDEommf/ftwO+lV/tIro=; b=0D7OkJExtOdzx2+c0N0ecrasyFJZsd0eSynonvg2FZCuzI8tuQscvi6cD4mEnZEu3jRO/Ckx2pCJ87I1XEIXv7DIA0VDayflS2zdDB3pl2OHVIW/zsbrPVY4CEsKxauj7ETNNxU/RDEOPHjduyt5Z6gKu48YTuL77C3CDOTskbo= Received: from DM6PR02CA0071.namprd02.prod.outlook.com (2603:10b6:5:177::48) by CY5PR12MB6177.namprd12.prod.outlook.com (2603:10b6:930:26::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.44; Mon, 12 Jun 2023 04:47:17 +0000 Received: from DM6NAM11FT097.eop-nam11.prod.protection.outlook.com (2603:10b6:5:177:cafe::e6) by DM6PR02CA0071.outlook.office365.com (2603:10b6:5:177::48) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:47:17 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT097.mail.protection.outlook.com (10.13.172.72) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.29 via Frontend Transport; Mon, 12 Jun 2023 04:47:16 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:47:11 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 40/51] KVM: SVM: Add support to handle RMP nested page faults Date: Sun, 11 Jun 2023 23:25:48 -0500 Message-ID: <20230612042559.375660-41-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT097:EE_|CY5PR12MB6177:EE_ X-MS-Office365-Filtering-Correlation-Id: 5ccfaa0e-7e76-4413-6a9b-08db6b001935 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: V2Zyvwb47JdoxYXjhACqkbRhZOMMHH4VCfyv07v4gBce+KYaEYYz7PFqP+Kf8m96s9RcCd+Zp/U+4FxCEW54SFw/5u9Rt2mx/dRcRT69PZHpEZK/wCI+r7W8xUTA5QunwYXAE50/p9SA2/j3RZkeObXmkErzudq5v/iWcefZWoZNzY60qageCLyDRukcsB8LwcIwP4L8xAoq0WfxJFyMcaiTeHVFeNnBPXJxoQNVSjWDT3yIXost9ZNV4R2CFrZ4XsqxlaHa/URGmXb9qGBuIprHuEMpsxk77a2jCHfYtO+7JrcbDy+JG6CzWzi54hhEYnRqdNcqE2Cb2EtgQZQ7JFG2JGsADvlHFAYN07F1M+Lu5KB86ivc1+wMpNd5druakJ4vHW0ccjoBnfYr6UTnItPy5KLmcp4kXL9UIYvFnLUe81ZNj6yrJ2VsE+lSZY4trJ3Lk0x/LPzIogpuRSQVf2ueRjcpKagRwOWpjMbePHmqo9VDAtDg5vLQpDE7K52J2Mzmx9V9YWS/qrK7tCrHWTNOizTqxj+9Yf6l3XAtP6bDjnw8+d4V5F+VHb6dkNSlypY8cU6U++CZuKZBKonSMAAbU/Q6uyvHu0sS44dVXfYiyMQ2O3/NhIb/9wKBWK+iIuvPMfRIQvx++7U4dGD/RBVYU9ke5l6dpFwHaI8bqtW98i8Fo8lOxi/ZAYLy8cQwZB/QouwfxvHlq2C0VzFyW+Lu2mmsHCRP+jSjyi7ywRad3JP4d6ZG/cQ8T7kvdn4K6VZnv8hhEOhT/RguuIsw1A== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(346002)(376002)(136003)(39860400002)(396003)(451199021)(40470700004)(46966006)(36840700001)(86362001)(82310400005)(7406005)(7416002)(40460700003)(316002)(8676002)(41300700001)(82740400003)(83380400001)(5660300002)(26005)(40480700001)(81166007)(1076003)(356005)(6666004)(36860700001)(8936002)(44832011)(36756003)(336012)(4326008)(426003)(6916009)(70586007)(47076005)(70206006)(478600001)(186003)(16526019)(2906002)(54906003)(2616005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:47:16.9379 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 5ccfaa0e-7e76-4413-6a9b-08db6b001935 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT097.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR12MB6177 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh When SEV-SNP is enabled in the guest, the hardware places restrictions on all memory accesses based on the contents of the RMP table. When hardware encounters RMP check failure caused by the guest memory access it raises the #NPF. The error code contains additional information on the access type. See the APM volume 2 for additional information. When using gmem, RMP faults resulting from mismatches between the state in the RMP table vs. what the guest expects via its page table result in KVM_EXIT_MEMORY_FAULTs being forwarded to userspace to handle. This means the only expected case that needs to be handled in the kernel is when the page size of the entry in the RMP table is larger than the mapping in the nested page table, in which case a PSMASH instruction needs to be issued to split the large RMP entry into individual 4K entries so that subsequent accesses can succeed. Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- arch/x86/kvm/svm/sev.c | 85 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 21 +++++++++-- arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 103 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 9b9dff7728c8..1ba49c5ebaed 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3234,6 +3234,13 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) svm->vmcb->control.ghcb_gpa = value; } +static int snp_rmptable_psmash(struct kvm *kvm, kvm_pfn_t pfn) +{ + pfn = pfn & ~(KVM_PAGES_PER_HPAGE(PG_LEVEL_2M) - 1); + + return psmash(pfn); +} + static int snp_complete_psc_msr_protocol(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); @@ -3696,3 +3703,81 @@ struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) return p; } + +void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) +{ + struct kvm_memory_slot *slot; + struct kvm *kvm = vcpu->kvm; + int order, rmp_level, ret; + bool assigned; + kvm_pfn_t pfn; + gfn_t gfn; + + /* + * Private memslots forward handling of implicit page state changes + * to userspace, so the only RMP faults expected here are for + * PFERR_GUEST_SIZEM_MASK. Anything else suggests that the RMP table + * has gotten out of sync with the private memslot. Generally... + * + * However, there is a transient case where access to an NPT mapping + * that has just been split/PSMASH'd can generate an RMP fault. In this + * case the PFERR_GUEST_SIZEM bit might not be set. In these cases it + * should be safe to ignore and let the guest retry, but allow for + * these to be optionally logged to diagnose exceptional cases. + */ + if (!(error_code & PFERR_GUEST_SIZEM_MASK)) { + pr_debug_ratelimited("Unexpected RMP fault for GPA 0x%llx, error_code 0x%llx", + gpa, error_code); + return; + } + + gfn = gpa >> PAGE_SHIFT; + + /* + * Only RMPADJUST/PVALIDATE should cause PFERR_GUEST_SIZEM. + * + * For PVALIDATE, this should only happen if a guest PVALIDATEs a 4K GFN + * that is backed by a huge page in the host whose RMP entry has the + * hugepage/assigned bits set. With UPM, that should only ever happen + * for private pages. + * + * For RMPADJUST, this assumption might not hold, in which case handling + * for obtaining the PFN from HVA-backed memory may be needed. For now, + * just print warnings. + */ + if (!kvm_mem_is_private(kvm, gfn)) { + pr_warn_ratelimited("Unexpected RMP fault, size-mismatch for non-private GPA 0x%llx\n", + gpa); + return; + } + + slot = gfn_to_memslot(kvm, gfn); + if (!kvm_slot_can_be_private(slot)) { + pr_warn_ratelimited("Unexpected RMP fault, non-private slot for GPA 0x%llx\n", + gpa); + return; + } + + ret = kvm_gmem_get_pfn(kvm, slot, gfn, &pfn, &order); + if (ret) { + pr_warn_ratelimited("Unexpected RMP fault, no private backing page for GPA 0x%llx\n", + gpa); + return; + } + + ret = snp_lookup_rmpentry(pfn, &assigned, &rmp_level); + if (ret || !assigned) { + pr_warn_ratelimited("Unexpected RMP fault, no assigned RMP entry found for GPA 0x%llx PFN 0x%llx error %d\n", + gpa, pfn, ret); + goto out; + } + + ret = snp_rmptable_psmash(kvm, pfn); + if (ret) + pr_err_ratelimited("Unable to split RMP entries for GPA 0x%llx PFN 0x%llx ret %d\n", + gpa, pfn, ret); + +out: + kvm_zap_gfn_range(kvm, gfn, gfn + PTRS_PER_PMD); + put_page(pfn_to_page(pfn)); +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 065167b42f90..0cff050bf5bb 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1995,15 +1995,28 @@ static int pf_interception(struct kvm_vcpu *vcpu) static int npf_interception(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); + int rc; u64 fault_address = svm->vmcb->control.exit_info_2; u64 error_code = svm->vmcb->control.exit_info_1; trace_kvm_page_fault(vcpu, fault_address, error_code); - return kvm_mmu_page_fault(vcpu, fault_address, error_code, - static_cpu_has(X86_FEATURE_DECODEASSISTS) ? - svm->vmcb->control.insn_bytes : NULL, - svm->vmcb->control.insn_len); + rc = kvm_mmu_page_fault(vcpu, fault_address, error_code, + static_cpu_has(X86_FEATURE_DECODEASSISTS) ? + svm->vmcb->control.insn_bytes : NULL, + svm->vmcb->control.insn_len); + + /* + * rc == 0 indicates a userspace exit is needed to handle page + * transitions, so do that first before updating the RMP table. + */ + if (error_code & PFERR_GUEST_RMP_MASK) { + if (rc == 0) + return rc; + handle_rmp_page_fault(vcpu, fault_address, error_code); + } + + return rc; } static int db_interception(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 02edbdd443e4..4cf9dbc442e9 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -762,6 +762,7 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector); void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa); void sev_es_unmap_ghcb(struct vcpu_svm *svm); struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu); +void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code); /* vmenter.S */ From patchwork Mon Jun 12 04:25:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275550 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id ED3CDC7EE25 for ; Mon, 12 Jun 2023 04:51:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235349AbjFLEvt (ORCPT ); Mon, 12 Jun 2023 00:51:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235411AbjFLEvT (ORCPT ); Mon, 12 Jun 2023 00:51:19 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F249D1985; Sun, 11 Jun 2023 21:51:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gABl3st8+RPuXnOTJ5AIdQuX+inGNFxwqxAYTsWwSisK+Qpo2wMopsUbYyl1UvUUElKefA5rw1mTbLFWhprrfFuH/lFV4qkbG0d/0CsEpIPb6AviqCiZQqeTBwmsbcqOYw8VKwgUGZ6pxZAom3OS77+DA/nE4J31eg1rwhisJ6gFGYkt/111Cx9Q7knlCXzt4yY3LWJ6cSL1fZKgxBIhwLu+LoYtz9r/5AzsT7gDXrz6U+gNmc7fvxAa9mjtJkuUofqJHx2foA4mBnbeklI6zqLx6q4dAtTWZDQi5rHG2v2Z3hQWvG16QNBzOajAiAWt2juaBG2h/Mf3RyVc11Cdsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=zwFcoomX4hZVadAOtXXAh+HWWkMbGG/uK/b829NNo8E=; b=g1mDykJccu9gR+8B7TqgE9ubiSFrNCPn2fBYOGaWZxRqHCRFWQSPsGZ5E/4dn7x/fPq1cVeqG+kNnaxmw3+DwHqNT20J9tJUV0BJgHpQV24klsDhNYowhXhBoNoU+hlpTZpsfAVjxjOqdwR6bsbetMB6fIwWhBBeS4ZsR9sb5wt+GTyyeSn5sYZrEPllVjmOZVqpxU8pi1BG0FXreq8GzUZDm8vNQtLdLlulBe6isiL3B2Y4e2ghGQTOqJ+6vz2Pv8SagytJJQaQIENd1masa8THDgCqvM+u4+Uwglr7g3ClOlYF7AIRmzRpY6B9kAwKVNz6poEzVJEVtafD43c6ow== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=zwFcoomX4hZVadAOtXXAh+HWWkMbGG/uK/b829NNo8E=; b=3GEJ5IexLSvILwWbJ7mTwJpWT9Ef30Bum61UHRfIk7xkhMGR2sweQTWTtqSK5tGGDx4PdD0Oj//3z/gxHYJ50EpkshYiT215VFvDDpis1GhysvvcwqJQ+kWdxMKRi8TOyGrVPZLSkMD43iqS3ajQIwbTsIrM04icHstBY4nNJyg= Received: from SA1P222CA0078.NAMP222.PROD.OUTLOOK.COM (2603:10b6:806:2c1::25) by IA1PR12MB6604.namprd12.prod.outlook.com (2603:10b6:208:3a0::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.44; Mon, 12 Jun 2023 04:51:09 +0000 Received: from SA2PEPF00001507.namprd04.prod.outlook.com (2603:10b6:806:2c1:cafe::4f) by SA1P222CA0078.outlook.office365.com (2603:10b6:806:2c1::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.34 via Frontend Transport; Mon, 12 Jun 2023 04:51:09 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by SA2PEPF00001507.mail.protection.outlook.com (10.167.242.39) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.24 via Frontend Transport; Mon, 12 Jun 2023 04:51:08 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:51:04 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 41/51] KVM: SVM: Use a VMSA physical address variable for populating VMCB Date: Sun, 11 Jun 2023 23:25:49 -0500 Message-ID: <20230612042559.375660-42-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SA2PEPF00001507:EE_|IA1PR12MB6604:EE_ X-MS-Office365-Filtering-Correlation-Id: 2e9a3f11-ad05-4d53-ba08-08db6b00a378 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2S3+ttjoDMZZfxFdU6K9nG5tsU3Pw56v06hAPztdBntfzCtMRLhG4bcnqUZv+hDcoU3FB2h8pclhW+3E2OaYTbBNeApqgeBgoyZ0AuFYzW5CyCINGE7Yf1uOuDaTJRAvJTvOK0ydIweVkOMMMO297gHBXbrk27sj/Fmw8oO27QIUuVj8baKVfMis9I4v5xcs5DzW4msIMK/m5U/9cXtbfCyQipj0FnsxmdFSzv1AzYm9JPGqxIwUk2/nrLHWLVRcAR2OMwjlvgp6juYvHfiLgRwkMuPLudbHpPIj24gOs134WJ7j/CZHszwFQMUQSA7IMwZRbtgDpGB8ty7AeXsR2xnY4c1+Skg4cwdHJHl6JXFJLDCnQYmaQzwcV4UnfGsN1VX+6PbZchd6a/5clj4sKrKzWr4Z5J34BGJ8Ouhsscn9SnVFpoWC8aIJO9Hbp/IzhTACFcY0hU4wTcT2MoUh5IOyG2+Q+voPrnZ94ihSeiz0TQfAfu9G7i8Onq3+qk/siHiWg/l6GZ4dmDwiOyJBeIytV7bR6n2zBPXA9M6LiqfDrj+0bZO+Bd7+WVuq3JvwYrXyRkS9aXBNjWXqDx+TnSrSFfO1xVC2OfEaxAzqm52Gger7FD3RMPpfV/qHFNKObp4+UwioyA3GjvXfgAOd3P/bfOx1BWafi5TF1XFkqfHKZ2hYl1f4JS4yCJxLoU/S0/BRWjnBp2o75w2vVYk7yRpTifQKstKqClrBP16HWPmgDSGKZn+Om7CceXf+0opNGl/Qw8C0YR5aHuRoPWPJ7Q== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(136003)(39860400002)(396003)(376002)(346002)(451199021)(36840700001)(40470700004)(46966006)(86362001)(40480700001)(36756003)(82310400005)(81166007)(356005)(82740400003)(40460700003)(478600001)(6666004)(54906003)(8676002)(8936002)(44832011)(5660300002)(7416002)(7406005)(6916009)(4326008)(70586007)(316002)(70206006)(2906002)(41300700001)(36860700001)(16526019)(426003)(2616005)(336012)(1076003)(186003)(83380400001)(26005)(47076005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:51:08.9202 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2e9a3f11-ad05-4d53-ba08-08db6b00a378 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SA2PEPF00001507.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6604 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Tom Lendacky In preparation to support SEV-SNP AP Creation, use a variable that holds the VMSA physical address rather than converting the virtual address. This will allow SEV-SNP AP Creation to set the new physical address that will be used should the vCPU reset path be taken. Signed-off-by: Tom Lendacky Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 5 ++--- arch/x86/kvm/svm/svm.c | 9 ++++++++- arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 1ba49c5ebaed..111e43eede15 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3551,10 +3551,9 @@ static void sev_es_init_vmcb(struct vcpu_svm *svm) /* * An SEV-ES guest requires a VMSA area that is a separate from the - * VMCB page. Do not include the encryption mask on the VMSA physical - * address since hardware will access it using the guest key. + * VMCB page. */ - svm->vmcb->control.vmsa_pa = __pa(svm->sev_es.vmsa); + svm->vmcb->control.vmsa_pa = svm->sev_es.vmsa_pa; /* Can't intercept CR register access, HV can't modify CR registers */ svm_clr_intercept(svm, INTERCEPT_CR0_READ); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 0cff050bf5bb..77195d8c1aa3 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1419,9 +1419,16 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu) svm->vmcb01.pa = __sme_set(page_to_pfn(vmcb01_page) << PAGE_SHIFT); svm_switch_vmcb(svm, &svm->vmcb01); - if (vmsa_page) + if (vmsa_page) { svm->sev_es.vmsa = page_address(vmsa_page); + /* + * Do not include the encryption mask on the VMSA physical + * address since hardware will access it using the guest key. + */ + svm->sev_es.vmsa_pa = __pa(svm->sev_es.vmsa); + } + svm->guest_state_loaded = false; return 0; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 4cf9dbc442e9..8dc7946ab634 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -197,6 +197,7 @@ struct vcpu_sev_es_state { struct sev_es_save_area *vmsa; struct ghcb *ghcb; struct kvm_host_map ghcb_map; + hpa_t vmsa_pa; bool received_first_sipi; unsigned int ap_reset_hold_type; From patchwork Mon Jun 12 04:25:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275551 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C302CC7EE25 for ; Mon, 12 Jun 2023 04:52:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235152AbjFLEv7 (ORCPT ); Mon, 12 Jun 2023 00:51:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235131AbjFLEvg (ORCPT ); Mon, 12 Jun 2023 00:51:36 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on20619.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe5b::619]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20202E47; Sun, 11 Jun 2023 21:51:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XOIEUdMsOj8hOgySfwQ2RdLKhup8IVFulmYG4yvtApT9ffmG/l6UajfGEb1RrxVl6zWTYOxAbALJcuGjgiCUq0Qtn7xrEtdjjyFJtyX3DBak1NpgVnxU+4/iGFN0/am+JrEI3BPpIT2qGkJC5X4roUO5wBaUr1OjvdHIl7JZfDMckW35DyVw2ETqyzrzgdJd9uh+yfZUE7HOC7xHYV940v/s3qobHEh/08OJ6L6ULDwMQx8dYaAeGq04HYu3H33yFwvMDPzq4xFPTEKcYpOf3l1YvZQ64flcDiMr9LSwFnfJGqgM5NtllGQpcOo4o4lUbNmRT+YOBY+YWEdpkHvznw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Sr6+Hk4gRHkhZVI0+Aeq+AC5gkb36b1EgW2twd01CQE=; b=T/mV2JqVACQ6fXEJ5qT+X3J9J7iGpVcUG6s8lrVffGraGcz49uZJUJOuRwQvZkLxZfcwUWpnQ17Jio51DWRcezFpR9wX9qrxQcfEM2nvqjOZAE/ficnbGlsQTxmdDO7Zr6cTgnc09CKN1sqN6yPCDKNlM/hjGDgbY83E7cd5l0JYiFDnbj15T/Mc59GBJ/Dg6YTgXDv8otczgBNOcHainYkJV4BHzFJYW/Lm6P3jDuzsLuAhPv1MSo99ua/kNC9v3f3BZSLQrU74Jfru/TcbZOWZpZ4/BEGIbiZrDgrEXrfQEDejiazan0JA9M1KX2gV822HUqdd2BHrRxIRmVfZpg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=Sr6+Hk4gRHkhZVI0+Aeq+AC5gkb36b1EgW2twd01CQE=; b=pLdYNgLYg+/X2h1XNhNzHUPLzuOvpJSxrngrfhOmCIfZVfpIajYCtXY0NxR/EeFnSuo1PvSBqc6kigyVODrFQkfJQj5/z6bXWgFOkvA98UJAUgAvHzOdWMRHunIPvYvTrn+4cL5ZDuxAcub/My2C/fpkg78SJ9tE9ZrhJhi29Gw= Received: from SN6PR08CA0015.namprd08.prod.outlook.com (2603:10b6:805:66::28) by DS7PR12MB5933.namprd12.prod.outlook.com (2603:10b6:8:7c::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.29; Mon, 12 Jun 2023 04:51:28 +0000 Received: from SA2PEPF00001509.namprd04.prod.outlook.com (2603:10b6:805:66:cafe::b2) by SN6PR08CA0015.outlook.office365.com (2603:10b6:805:66::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:51:28 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by SA2PEPF00001509.mail.protection.outlook.com (10.167.242.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:51:27 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:51:27 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 42/51] KVM: SVM: Support SEV-SNP AP Creation NAE event Date: Sun, 11 Jun 2023 23:25:50 -0500 Message-ID: <20230612042559.375660-43-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SA2PEPF00001509:EE_|DS7PR12MB5933:EE_ X-MS-Office365-Filtering-Correlation-Id: de353946-87cc-4705-43d2-08db6b00aed2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: A01qHqeaqr7WQHFaxyhfcQuUVQi4v+0mACbGK9FYIM7NgLO03D19Yp4b5+QbUWK+VxSRqmPvhrrHy2tp4FKcocl02gnKrFei+gJj4HJdje8ZpwmjfFeQZQ/f5dT2/22sZMhFUIFN5RH/m+r/vACzduamkSdKdNLYQwGZXzmfhBOHgV3zagLsBPwR1NiOnFNyymvSYAR1SxXyMmHZQZ/I+FtZisKLv72QmMGB5/XMuSuEvT3wy/s0yGTUTPZvxqr/LsI1IoM4IhCInysEgixDJqPtbInkn6by57a2sW5XfhTzsqvGd7FdfnYdvqdELdnHUoMGbMp+dhelbpY1OE6YV5WrANbC0ZVsdLmzFXlba5qo1peUiO86CiOZKFUb2tjwnw+eA4aQogxhftp97ea9q5/ZnhL0UVI9i7DqhGsQmjFaIfImSl8+YZvWjQ/VKp+eYbSia/OftU/tyy7BwU9zb5C6V8TqDpSaCOk3WCQOYgTvHaspY5205D18kAwCc3+7UUI1cuvTjP0VpO4NMsqdj8XmdiXD2sYA5ZTMEjYNbOvH/MzJ6aKegbW+mzqiY6z19o78xTGei4iJ5simbMw3gR7i0j5wVw6+OCS2qxztLNZ8dWuanIwIrGxgC4ii2jLoIchYGo4vqK4VzMVhmH3htXBpai/KqPdSnt4P0I/WX85iLlUsPtuxT+ATz1sGYYVHSRu7cgztMDv99EZ1v22NnrTac1StkrGE5NpurxDQPEohllADAtplOnyKXfkPMyVKI04nGzTvse/RC6m/eGo65A== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(346002)(376002)(39860400002)(396003)(136003)(451199021)(40470700004)(46966006)(36840700001)(86362001)(82310400005)(7406005)(7416002)(40460700003)(316002)(8676002)(41300700001)(82740400003)(83380400001)(5660300002)(26005)(40480700001)(81166007)(1076003)(356005)(6666004)(36860700001)(8936002)(44832011)(36756003)(336012)(4326008)(426003)(6916009)(70586007)(47076005)(70206006)(478600001)(186003)(16526019)(30864003)(2906002)(54906003)(2616005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:51:27.9463 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: de353946-87cc-4705-43d2-08db6b00aed2 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SA2PEPF00001509.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB5933 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Tom Lendacky Add support for the SEV-SNP AP Creation NAE event. This allows SEV-SNP guests to alter the register state of the APs on their own. This allows the guest a way of simulating INIT-SIPI. A new event, KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, is created and used so as to avoid updating the VMSA pointer while the vCPU is running. For CREATE The guest supplies the GPA of the VMSA to be used for the vCPU with the specified APIC ID. The GPA is saved in the svm struct of the target vCPU, the KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event is added to the vCPU and then the vCPU is kicked. For CREATE_ON_INIT: The guest supplies the GPA of the VMSA to be used for the vCPU with the specified APIC ID the next time an INIT is performed. The GPA is saved in the svm struct of the target vCPU. For DESTROY: The guest indicates it wishes to stop the vCPU. The GPA is cleared from the svm struct, the KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event is added to vCPU and then the vCPU is kicked. The KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event handler will be invoked as a result of the event or as a result of an INIT. The handler sets the vCPU to the KVM_MP_STATE_UNINITIALIZED state, so that any errors will leave the vCPU as not runnable. Any previous VMSA pages that were installed as part of an SEV-SNP AP Creation NAE event are un-pinned. If a new VMSA is to be installed, the VMSA guest page is pinned and set as the VMSA in the vCPU VMCB and the vCPU state is set to KVM_MP_STATE_RUNNABLE. If a new VMSA is not to be installed, the VMSA is cleared in the vCPU VMCB and the vCPU state is left as KVM_MP_STATE_UNINITIALIZED to prevent it from being run. Signed-off-by: Tom Lendacky Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra [mdr: add handling for restrictedmem] Signed-off-by: Michael Roth --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/asm/svm.h | 7 +- arch/x86/kvm/svm/sev.c | 240 ++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 3 + arch/x86/kvm/svm/svm.h | 8 +- arch/x86/kvm/x86.c | 11 ++ 6 files changed, 268 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 2fcd309fd9fb..8f515e0386a0 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -113,6 +113,7 @@ KVM_ARCH_REQ_FLAGS(31, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) #define KVM_REQ_HV_TLB_FLUSH \ KVM_ARCH_REQ_FLAGS(32, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) +#define KVM_REQ_UPDATE_PROTECTED_GUEST_STATE KVM_ARCH_REQ(34) #define CR0_RESERVED_BITS \ (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \ diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index ac8edfdd60fa..0deb83ac800b 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -288,7 +288,12 @@ static_assert((X2AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == X2AVIC_ #define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF) -#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +#define SVM_SEV_FEAT_RESTRICTED_INJECTION BIT(3) +#define SVM_SEV_FEAT_ALTERNATE_INJECTION BIT(4) +#define SVM_SEV_FEAT_INT_INJ_MODES \ + (SVM_SEV_FEAT_RESTRICTED_INJECTION | \ + SVM_SEV_FEAT_ALTERNATE_INJECTION) struct vmcb_seg { u16 selector; diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 111e43eede15..ec74ff5e09c7 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -638,6 +638,7 @@ static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) static int sev_es_sync_vmsa(struct vcpu_svm *svm) { + struct kvm_sev_info *sev = &to_kvm_svm(svm->vcpu.kvm)->sev_info; struct sev_es_save_area *save = svm->sev_es.vmsa; /* Check some debug related fields before encrypting the VMSA */ @@ -683,6 +684,12 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) if (sev_snp_guest(svm->vcpu.kvm)) save->sev_features |= SVM_SEV_FEAT_SNP_ACTIVE; + /* + * Save the VMSA synced SEV features. For now, they are the same for + * all vCPUs, so just save each time. + */ + sev->sev_features = save->sev_features; + pr_debug("Virtual Machine Save Area (VMSA):\n"); print_hex_dump_debug("", DUMP_PREFIX_NONE, 16, 1, save, sizeof(*save), false); @@ -3034,6 +3041,11 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) if (!ghcb_sw_scratch_is_valid(ghcb)) goto vmgexit_err; break; + case SVM_VMGEXIT_AP_CREATION: + if (lower_32_bits(ghcb_get_sw_exit_info_1(ghcb)) != SVM_VMGEXIT_AP_DESTROY) + if (!ghcb_rax_is_valid(ghcb)) + goto vmgexit_err; + break; case SVM_VMGEXIT_NMI_COMPLETE: case SVM_VMGEXIT_AP_HLT_LOOP: case SVM_VMGEXIT_AP_JUMP_TABLE: @@ -3259,6 +3271,220 @@ static int snp_complete_psc(struct kvm_vcpu *vcpu) return 1; /* resume */ } +static kvm_pfn_t gfn_to_pfn_gmem(struct kvm *kvm, gfn_t gfn) +{ + struct kvm_memory_slot *slot; + kvm_pfn_t pfn; + int order = 0; + + slot = gfn_to_memslot(kvm, gfn); + if (!kvm_slot_can_be_private(slot)) { + pr_err("SEV: Failure retrieving restricted memslot for GFN 0x%llx, flags 0x%x, userspace_addr: 0x%lx\n", + gfn, slot->flags, slot->userspace_addr); + return INVALID_PAGE; + } + + if (!kvm_mem_is_private(kvm, gfn)) { + pr_err("SEV: Failure retrieving restricted PFN for GFN 0x%llx\n", gfn); + return INVALID_PAGE; + } + + if (kvm_gmem_get_pfn(kvm, slot, gfn, &pfn, &order)) { + pr_err("SEV: Failure retrieving restricted PFN for GFN 0x%llx\n", gfn); + return INVALID_PAGE; + } + + return pfn; +} + +static int __sev_snp_update_protected_guest_state(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + kvm_pfn_t pfn; + hpa_t cur_pa; + + WARN_ON(!mutex_is_locked(&svm->sev_es.snp_vmsa_mutex)); + + /* Save off the current VMSA PA for later checks */ + cur_pa = svm->sev_es.vmsa_pa; + + /* Mark the vCPU as offline and not runnable */ + vcpu->arch.pv.pv_unhalted = false; + vcpu->arch.mp_state = KVM_MP_STATE_HALTED; + + /* Clear use of the VMSA */ + svm->sev_es.vmsa_pa = INVALID_PAGE; + svm->vmcb->control.vmsa_pa = INVALID_PAGE; + + /* + * sev->sev_es.vmsa holds the virtual address of the VMSA initially + * allocated by the host. If the guest specified a new a VMSA via + * AP_CREATION, it will have been pinned to avoid future issues + * with things like page migration support. Make sure to un-pin it + * before switching to a newer guest-specified VMSA. + */ + if (cur_pa != __pa(svm->sev_es.vmsa) && VALID_PAGE(cur_pa)) + kvm_release_pfn_dirty(__phys_to_pfn(cur_pa)); + + if (VALID_PAGE(svm->sev_es.snp_vmsa_gpa)) { + /* + * The VMSA is referenced by the hypervisor physical address, + * so retrieve the PFN and ensure it is restricted memory. + */ + pfn = gfn_to_pfn_gmem(vcpu->kvm, gpa_to_gfn(svm->sev_es.snp_vmsa_gpa)); + if (!VALID_PAGE(pfn) || is_error_pfn(pfn)) + return -EINVAL; + + /* Use the new VMSA */ + svm->sev_es.vmsa_pa = pfn_to_hpa(pfn); + svm->vmcb->control.vmsa_pa = svm->sev_es.vmsa_pa; + + /* Mark the vCPU as runnable */ + vcpu->arch.pv.pv_unhalted = false; + vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; + + svm->sev_es.snp_vmsa_gpa = INVALID_PAGE; + } + + /* + * When replacing the VMSA during SEV-SNP AP creation, + * mark the VMCB dirty so that full state is always reloaded. + */ + vmcb_mark_all_dirty(svm->vmcb); + + return 0; +} + +/* + * Invoked as part of svm_vcpu_reset() processing of an init event. + */ +void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + int ret; + + if (!sev_snp_guest(vcpu->kvm)) + return; + + mutex_lock(&svm->sev_es.snp_vmsa_mutex); + + if (!svm->sev_es.snp_ap_create) + goto unlock; + + svm->sev_es.snp_ap_create = false; + + ret = __sev_snp_update_protected_guest_state(vcpu); + if (ret) + vcpu_unimpl(vcpu, "snp: AP state update on init failed\n"); + +unlock: + mutex_unlock(&svm->sev_es.snp_vmsa_mutex); +} + +static int sev_snp_ap_creation(struct vcpu_svm *svm) +{ + struct kvm_sev_info *sev = &to_kvm_svm(svm->vcpu.kvm)->sev_info; + struct kvm_vcpu *vcpu = &svm->vcpu; + struct kvm_vcpu *target_vcpu; + struct vcpu_svm *target_svm; + unsigned int request; + unsigned int apic_id; + bool kick; + int ret; + + request = lower_32_bits(svm->vmcb->control.exit_info_1); + apic_id = upper_32_bits(svm->vmcb->control.exit_info_1); + + /* Validate the APIC ID */ + target_vcpu = kvm_get_vcpu_by_id(vcpu->kvm, apic_id); + if (!target_vcpu) { + vcpu_unimpl(vcpu, "vmgexit: invalid AP APIC ID [%#x] from guest\n", + apic_id); + return -EINVAL; + } + + ret = 0; + + target_svm = to_svm(target_vcpu); + + /* + * The target vCPU is valid, so the vCPU will be kicked unless the + * request is for CREATE_ON_INIT. For any errors at this stage, the + * kick will place the vCPU in an non-runnable state. + */ + kick = true; + + mutex_lock(&target_svm->sev_es.snp_vmsa_mutex); + + target_svm->sev_es.snp_vmsa_gpa = INVALID_PAGE; + target_svm->sev_es.snp_ap_create = true; + + /* Interrupt injection mode shouldn't change for AP creation */ + if (request < SVM_VMGEXIT_AP_DESTROY) { + u64 sev_features; + + sev_features = vcpu->arch.regs[VCPU_REGS_RAX]; + sev_features ^= sev->sev_features; + if (sev_features & SVM_SEV_FEAT_INT_INJ_MODES) { + vcpu_unimpl(vcpu, "vmgexit: invalid AP injection mode [%#lx] from guest\n", + vcpu->arch.regs[VCPU_REGS_RAX]); + ret = -EINVAL; + goto out; + } + } + + switch (request) { + case SVM_VMGEXIT_AP_CREATE_ON_INIT: + kick = false; + fallthrough; + case SVM_VMGEXIT_AP_CREATE: + if (!page_address_valid(vcpu, svm->vmcb->control.exit_info_2)) { + vcpu_unimpl(vcpu, "vmgexit: invalid AP VMSA address [%#llx] from guest\n", + svm->vmcb->control.exit_info_2); + ret = -EINVAL; + goto out; + } + + /* + * Malicious guest can RMPADJUST a large page into VMSA which + * will hit the SNP erratum where the CPU will incorrectly signal + * an RMP violation #PF if a hugepage collides with the RMP entry + * of VMSA page, reject the AP CREATE request if VMSA address from + * guest is 2M aligned. + */ + if (IS_ALIGNED(svm->vmcb->control.exit_info_2, PMD_SIZE)) { + vcpu_unimpl(vcpu, + "vmgexit: AP VMSA address [%llx] from guest is unsafe as it is 2M aligned\n", + svm->vmcb->control.exit_info_2); + ret = -EINVAL; + goto out; + } + + target_svm->sev_es.snp_vmsa_gpa = svm->vmcb->control.exit_info_2; + break; + case SVM_VMGEXIT_AP_DESTROY: + break; + default: + vcpu_unimpl(vcpu, "vmgexit: invalid AP creation request [%#x] from guest\n", + request); + ret = -EINVAL; + break; + } + +out: + if (kick) { + if (target_vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED) + target_vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; + + kvm_make_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, target_vcpu); + kvm_vcpu_kick(target_vcpu); + } + + mutex_unlock(&target_svm->sev_es.snp_vmsa_mutex); + + return ret; +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3508,6 +3734,18 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) vcpu->run->vmgexit.ghcb_msr = ghcb_gpa; vcpu->arch.complete_userspace_io = snp_complete_psc; break; + case SVM_VMGEXIT_AP_CREATION: + ret = sev_snp_ap_creation(svm); + if (ret) { + ghcb_set_sw_exit_info_1(ghcb, 1); + ghcb_set_sw_exit_info_2(ghcb, + X86_TRAP_GP | + SVM_EVTINJ_TYPE_EXEPT | + SVM_EVTINJ_VALID); + } + + ret = 1; + break; case SVM_VMGEXIT_UNSUPPORTED_EVENT: vcpu_unimpl(vcpu, "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n", @@ -3612,6 +3850,8 @@ void sev_es_vcpu_reset(struct vcpu_svm *svm) set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX, GHCB_VERSION_MIN, sev_enc_bit)); + + mutex_init(&svm->sev_es.snp_vmsa_mutex); } void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 77195d8c1aa3..81b9f4e04a8d 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1358,6 +1358,9 @@ static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) svm->spec_ctrl = 0; svm->virt_spec_ctrl = 0; + if (init_event) + sev_snp_init_protected_guest_state(vcpu); + init_vmcb(vcpu); if (!init_event) diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 8dc7946ab634..e73a58e489c7 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -94,6 +94,7 @@ struct kvm_sev_info { atomic_t migration_in_progress; u64 snp_init_flags; void *snp_context; /* SNP guest context page */ + u64 sev_features; /* Features set at VMSA creation */ }; struct kvm_svm { @@ -208,6 +209,10 @@ struct vcpu_sev_es_state { bool ghcb_sa_free; u64 ghcb_registered_gpa; + + struct mutex snp_vmsa_mutex; /* Used to handle concurrent updates of VMSA. */ + gpa_t snp_vmsa_gpa; + bool snp_ap_create; }; struct vcpu_svm { @@ -735,7 +740,7 @@ void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu); #define GHCB_VERSION_MAX 2ULL #define GHCB_VERSION_MIN 1ULL -#define GHCB_HV_FT_SUPPORTED GHCB_HV_FT_SNP +#define GHCB_HV_FT_SUPPORTED (GHCB_HV_FT_SNP | GHCB_HV_FT_SNP_AP_CREATION) extern unsigned int max_sev_asid; @@ -764,6 +769,7 @@ void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa); void sev_es_unmap_ghcb(struct vcpu_svm *svm); struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu); void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code); +void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu); /* vmenter.S */ diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 10d76afa23d9..9e3c41e2a3ef 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10621,6 +10621,14 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) if (kvm_check_request(KVM_REQ_UPDATE_CPU_DIRTY_LOGGING, vcpu)) static_call(kvm_x86_update_cpu_dirty_logging)(vcpu); + + if (kvm_check_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, vcpu)) { + kvm_vcpu_reset(vcpu, true); + if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE) { + r = 1; + goto out; + } + } } if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win || @@ -12816,6 +12824,9 @@ static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu) return true; #endif + if (kvm_test_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, vcpu)) + return true; + if (kvm_arch_interrupt_allowed(vcpu) && (kvm_cpu_has_interrupt(vcpu) || kvm_guest_apic_has_interrupt(vcpu))) From patchwork Mon Jun 12 04:25:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275552 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EFB24C7EE2E for ; Mon, 12 Jun 2023 04:52:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235281AbjFLEwp (ORCPT ); Mon, 12 Jun 2023 00:52:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235392AbjFLEwZ (ORCPT ); Mon, 12 Jun 2023 00:52:25 -0400 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2056.outbound.protection.outlook.com [40.107.212.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09B201BCD; Sun, 11 Jun 2023 21:52:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LxeWqIGdkhCYuov+4xBX1NWAVaVJCAfokYratb0heeYvUMHzHTF5qI+5ncuR5OhpPRWCMAGSAT6x0mMWE50uYPtu4SG0vTlefL/JFOHTnVNT1I6nAkkEH9NdSmfk8lkH9unQ9KptzrrgKu8D1WuggZmbgffMGFTq4PyJ/rJPIkieOiLr+RdUd3OS2nz+TCvg/3tPtdk4MfsQYa+Kk91xS5vHY60O9YEKoICMOsfSthn9DbfykXEbt/5qV6+utA03Xk0zEUQzlO06gKASTXNr0hv766Gr77R1rveq1p9z0DfRCHEvgTzV6Z2AzZKLBsFP4extTM8TUlpihu8gqr+vBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=sKJx0R4sv5FkQcubDKo/v4xIgCFKS+E772CH7PG+a7A=; b=O3mm5XdQKg5O959KSCIKfa7DPmR8cuoHkQkwM9OS2Tl/tM+VmpmHhWBP07PLCsGpPggqHbjZpuUCA2tq2NQdb69YHc65W10CP3ivVjPiBzKQNeU0XnN9sEq1/pbhIp7ylS0wHSo43RCo0ZFn/JeyKmXHaGefuKIZD9Kx3sMg53fpJF30KBRc0Y7+selMZqPDpsWQe52wH1c8CS77/KgZ72eKqskQmb7mL2ThRf8Tuwoyj31+kDlDE3Lk0at5UHE0IhrBDmeQJAM8XbPjbssBcrYCOcI89pk1A7kqQRFfk0zIEEmdF8cjqT73FAAbdWQeX6dvsrvHm41OmDtgCnBNrQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=sKJx0R4sv5FkQcubDKo/v4xIgCFKS+E772CH7PG+a7A=; b=EfGg3KHUhajVvLRs0Lb2DIVSrMtwI1r38XO3vQidQ8jN5aHTYavFvJw3TRk4FTF9HCHS/fq6W95K5CFIioLtlbDWEpjWd+Ov0b07SRLs/0VSu+ADd+at1OCklAfVnelrlnuuas95u/1LBjs9tkq+uEhyT+Mzkcv0etHOz/NmpFs= Received: from SN6PR08CA0004.namprd08.prod.outlook.com (2603:10b6:805:66::17) by PH0PR12MB7792.namprd12.prod.outlook.com (2603:10b6:510:281::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.19; Mon, 12 Jun 2023 04:52:08 +0000 Received: from SA2PEPF00001509.namprd04.prod.outlook.com (2603:10b6:805:66:cafe::f1) by SN6PR08CA0004.outlook.office365.com (2603:10b6:805:66::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.34 via Frontend Transport; Mon, 12 Jun 2023 04:52:08 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by SA2PEPF00001509.mail.protection.outlook.com (10.167.242.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:52:08 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:52:02 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 43/51] KVM: SEV: Configure MMU to check for private fault flags Date: Sun, 11 Jun 2023 23:25:51 -0500 Message-ID: <20230612042559.375660-44-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SA2PEPF00001509:EE_|PH0PR12MB7792:EE_ X-MS-Office365-Filtering-Correlation-Id: c99a4267-3fa7-436a-e8e3-08db6b00c6fa X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: HyjRnG38AX9bs9UjcbW9XGreVbmQQPpE2V285In5KT1Ed79j7kE+Xm5rXnl8lMChvClUNfaCvP2DSEJtSIglR0ilpyt+/caOv9KLN6UIERaj0s4yCuc4a1QD3CPh7DFbfVkY/JVJDkco6PyqWmA+VYiI+l2ja/1lgW5UBBDEXC0enWCCdeEhyHqzVfx66loCtGk01Gkw0QxvGnHnVWHCyOA9fTszysabp7thQyjBC49n0RIaUNNxQ45QM2ofTc3RSlogrsZPsd/D2LugMKP+og5pM2POU1ab/btlpZubaYaLwq35dd/Zczk5c06eQ8d7HU9Yrf2FPML8junDaueXN+crEnVfW0dmW9CUHSd05ioHCqghJsCx1UVGZohqSiNSAE17BJqQPSQy/8N7WJFW5W4Y2OyTetSbAHS9SgLCtv+76nLaakMwxliG+kQkoOOkpqGt5dZ1Vc0vE/sdqazJAvuF3hTgM6r/qiR2FWYu864AHpfGKHc8UWhUf/tv0Sehdz1wdBUqei+TiESI5jHFrsNactQ4ru+LNvCdOsMhI9Tmbl0AOKntM/TQVM14gHYnh+Lm3OXjIRbqRfg+d3eVNvf1nsdatB3cO92reQOe+kS2KDsNntQu7zsZoaATe6rGq2lByDIJ/3pA78K/AOdVQZ+Wr2ZdW8QF+qONO6IWzQVpgCLODNKDHtPBryApihzHzaq8TEEVFaGJOOHY6IV42XMfGME1TelH32bv1tvG75SkbjbVH2/v2ECBmKgFSrryT+rqtNxC771LK/Np4vYJ9g== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(136003)(396003)(376002)(39860400002)(346002)(451199021)(36840700001)(40470700004)(46966006)(47076005)(16526019)(426003)(336012)(86362001)(186003)(4744005)(82740400003)(36756003)(40480700001)(316002)(81166007)(356005)(7406005)(26005)(1076003)(2906002)(44832011)(2616005)(36860700001)(5660300002)(82310400005)(7416002)(41300700001)(6916009)(54906003)(4326008)(70206006)(6666004)(478600001)(70586007)(8936002)(8676002)(40460700003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:52:08.4780 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c99a4267-3fa7-436a-e8e3-08db6b00c6fa X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SA2PEPF00001509.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB7792 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org SEV-SNP uses a #NPF flag to indicate whether the guest expects a shared or a private page, which in turn dictates whether the KVM MMU should map a normal page or a gmem page. Set the appropriate mask to enable this handling for SEV-SNP guests. Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index ec74ff5e09c7..909ecd90d199 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2006,6 +2006,8 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) if (rc) goto e_free_context; + kvm->arch.mmu_private_fault_mask = PFERR_GUEST_ENC_MASK; + return 0; e_free_context: From patchwork Mon Jun 12 04:25:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275553 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0B6BEC7EE25 for ; Mon, 12 Jun 2023 04:53:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235427AbjFLExo (ORCPT ); Mon, 12 Jun 2023 00:53:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53240 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234346AbjFLExe (ORCPT ); Mon, 12 Jun 2023 00:53:34 -0400 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on20614.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e8a::614]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EFECB1705; Sun, 11 Jun 2023 21:53:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AYWJ+BPq3u4kFEGUa3D7tD9gXtCGgsJc5FwFeLskZXsiqIgMMbxYgwcuO4Zd1Jymaxv1ehNAK7P3RoHwKB9ddeYvtIZWDuuWw2P38lceIIdlRvHYNCIFNdkI/3I9/2MsB/44LCANICqA81axkE9oZ/I/gEAl3JRfvF0Tnl7btkGFA+dLPCbLLSqcZT/ly36tOdrEgbDWBVPC5W7bLhMEhHDBe41GJ+S6y+mlfniBPPs4nauND3N7YZ0JHLvA0oM4h55+2uRqESzr9DuWHzstuCcOivI7OuXvMq/yjU3TdGXpPy8XUIQMgFBMLj16Us9M+YB+EvB4pmRICQNgvgNokg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=WOF0hFPBW7R+0qZTxRk+TtpaZhSYVvpORM/XQ7vmlUo=; b=hUZZv7AFFPuRm4j2wJLb/u9JNf6//pH+SMAC1mdfgFe8loZbolvIUQcIzx48n/caDJg7m2gYiRKg5QbNRXyIz8EbaoA/qPBkerW7bEJ/s0QFvbatRvuWZaCGRmUi2sw3t9wP7c5MGz3Cqa+0YjInIXm5Jyu1g1TKTUTvDkQ3lPj/yvZdQOJh5SseSCL0b3rIeV05eIQwIttaBDZhs2WgvMjoZE4Ad4JP2sQs9lDnqr82sNhal6/RGXF2eS4bs3OWMEak/rVdOEqWb9x5C5QNrNS1A8OOdpRqfXZYgXLAUvHir+qntwNhySVrRAY178KOvzFZ+Q6B5TKVgG+TTU5qLg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=WOF0hFPBW7R+0qZTxRk+TtpaZhSYVvpORM/XQ7vmlUo=; b=3ZLzc0RVys1/wb7SyxYwo0RmL6rRAugPzCp7YFc+lazIax3sYqYbXyJ6Yu+ZYe1Ju1xbz22ri2YfckYMmT8kW/V87N/GOH4NTX/qc5UrV1OIZxOxM67HUcTtNoO+xGEIRuITzphYGTiFPIVx6K/J6Dgo2glqgY4nKie+bfwW6/4= Received: from DM6PR06CA0043.namprd06.prod.outlook.com (2603:10b6:5:54::20) by DS0PR12MB7771.namprd12.prod.outlook.com (2603:10b6:8:138::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.42; Mon, 12 Jun 2023 04:53:17 +0000 Received: from CY4PEPF0000E9DB.namprd05.prod.outlook.com (2603:10b6:5:54:cafe::c5) by DM6PR06CA0043.outlook.office365.com (2603:10b6:5:54::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.34 via Frontend Transport; Mon, 12 Jun 2023 04:53:17 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9DB.mail.protection.outlook.com (10.167.241.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:53:16 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:53:09 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 44/51] KVM: SEV: Implement gmem hook for initializing private pages Date: Sun, 11 Jun 2023 23:25:52 -0500 Message-ID: <20230612042559.375660-45-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9DB:EE_|DS0PR12MB7771:EE_ X-MS-Office365-Filtering-Correlation-Id: fcd15a63-b251-4390-65c3-08db6b00efd8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: dqS0zYY7V6/SXVxsgnGA//ryGTcwnR4TiO8wPG4RvRQbNzIzLJeMo6pkNE2kP4bhaCGYHMtwT1BEclNiqf0CS21osC+D4ag3hGOQllS1GMTBA0DXk/diympqflIZrmRfs4hrutXHit4uRqu8f8nx729JimVp8R7G0CO5OMYP+23hdLakSL0a+cdjys1ak5201JgAuBC7X2ey4Enmkz3jqjRuIuw5NGkEdWKhFBr9s4tZkc07GsP9UeeBQhFthk2rzbeM1cf2lEI8/mOHMrmnorfkF97kj1erwyUUgKtHDcbM4mH4zxeXCsNUIvaN4vYvdhtAdE3gmoPVMY9XQRea6mygrn4WlrDKDck7V8rJNp/ERAOsbf5Xqi9gkRToL7So7JI5kp8adTWwYRUjlOxdc2B/6CTbWqFznejmR0vW8erTl/1gn/9G3rCr6WlkyrGRU7fzQUyye2bM7cJL+vvmWvC3BE+Oi1eshgEjXwJXFQnuKqCvsLVy0oUYkJL7qhAemNdH7vZ5NoLpomRLWDkrtuRLaLxpupOCzyFTZIMKfNSGsplg6LlV230gSF7LvJK2EYN8vn5Xlw/tn2pCQe4H/r2lSb5plEMZ8kcGASNMzlmgntbaACdu4IZAvlN8VrIl2iPhneDaZKgSGrILa6pBjKE4ebVUAEwbA4uiBQt8HhIa0b+6CyCjbBuu9iWk8MkAgdj4VKQT8/1jWrSk05Pq5OOH0L1ncm7Efe3Rof4eZRxaNAfhhaU9VLpSMD+t+LY3Aaw17l84v0ktGzHyI9JKkg== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(376002)(396003)(39860400002)(346002)(136003)(451199021)(36840700001)(46966006)(40470700004)(186003)(2906002)(426003)(44832011)(26005)(1076003)(7406005)(40460700003)(16526019)(7416002)(36756003)(83380400001)(36860700001)(47076005)(82310400005)(86362001)(82740400003)(356005)(81166007)(40480700001)(2616005)(478600001)(41300700001)(54906003)(6666004)(316002)(70586007)(4326008)(70206006)(336012)(6916009)(8676002)(8936002)(5660300002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:53:16.9759 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fcd15a63-b251-4390-65c3-08db6b00efd8 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9DB.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB7771 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org This will handle RMP table updates and direct map changes needed to put a page into a private state before mapping it into an SEV-SNP guest. Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 95 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 2 + arch/x86/kvm/svm/svm.h | 2 + 3 files changed, 99 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 909ecd90d199..c5a1706387bf 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -4022,3 +4022,98 @@ void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) kvm_zap_gfn_range(kvm, gfn, gfn + PTRS_PER_PMD); put_page(pfn_to_page(pfn)); } + +/* Check if GFN range is marked private in the KVM/gmem xarray. */ +static bool is_gfn_range_private(struct kvm *kvm, gfn_t start, gfn_t end) +{ + gfn_t gfn = start; + + while (gfn++ < end) + if (!kvm_mem_is_private(kvm, gfn)) { + pr_debug("%s: overlap detected, GFN 0x%llx start 0x%llx end 0x%llx\n", + __func__, gfn, start, end); + return false; + } + + return true; +} + +/* Check that no pages in PFN range have already been set to private in RMP table. */ +static bool is_pfn_range_shared(kvm_pfn_t start, kvm_pfn_t end) +{ + kvm_pfn_t pfn = start; + + while (pfn++ < end) { + int ret, rmp_level; + bool assigned; + + ret = snp_lookup_rmpentry(pfn, &assigned, &rmp_level); + if (ret) { + pr_debug("%s: failed to retrieve RMP entry, assuming overlap, PFN 0x%llx start 0x%llx end 0x%llx RMP level %d error %d\n", + __func__, pfn, start, end, rmp_level, ret); + return false; + } + + if (assigned == 1) { + pr_debug("%s: overlap detected, PFN 0x%llx start 0x%llx end 0x%llx RMP level %d\n", + __func__, pfn, start, end, rmp_level); + return false; + } + } + + return true; +} + +static int get_supported_rmp_level(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn) +{ + if (!IS_ALIGNED(pfn, PTRS_PER_PMD) || !IS_ALIGNED(gfn, PTRS_PER_PMD)) + return PG_LEVEL_4K; + + /* + * Check that both the desired GFN range states in the xarray, and + * current PFN range states in the RMP table, are conducive to + * creating a 2M private RMP entry. + */ + if (is_gfn_range_private(kvm, gfn, gfn + PTRS_PER_PMD) && + is_pfn_range_shared(pfn, pfn + PTRS_PER_PMD)) + return PG_LEVEL_2M; + + return PG_LEVEL_4K; +} + +int sev_gmem_prepare(struct kvm *kvm, struct kvm_memory_slot *slot, + kvm_pfn_t pfn, gfn_t gfn, u8 *max_level) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + int level, rc = 0; + bool assigned; + + if (!sev_snp_guest(kvm)) + return 0; + + rc = snp_lookup_rmpentry(pfn, &assigned, &level); + if (rc) + return rc; + + /* No conversion needed, just clamp xax_level according to RMP entry. */ + if (assigned) + goto out_adjust_level; + + if (*max_level == PG_LEVEL_4K) + level = PG_LEVEL_4K; + else + level = get_supported_rmp_level(kvm, pfn, gfn); + + rc = rmp_make_private(pfn, gfn_to_gpa(gfn), level, sev->asid, false); + if (rc) + pr_err_ratelimited("%s: failed gfn %llx pfn %llx level %d rc %d\n", + __func__, gfn, pfn, level, rc); + +out_adjust_level: + pr_debug("%s: pfn %llx gfn %llx max_level %d level %d assigned %d\n", + __func__, pfn, gfn, *max_level, level, assigned); + if (*max_level > level) + *max_level = level; + + return rc; +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 81b9f4e04a8d..9085a122907c 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4934,6 +4934,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, .vcpu_get_apicv_inhibit_reasons = avic_vcpu_get_apicv_inhibit_reasons, .alloc_apic_backing_page = svm_alloc_apic_backing_page, + + .gmem_prepare = sev_gmem_prepare, }; /* diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index e73a58e489c7..0438f52e4396 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -770,6 +770,8 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm); struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu); void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code); void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu); +int sev_gmem_prepare(struct kvm *kvm, struct kvm_memory_slot *slot, + kvm_pfn_t pfn, gfn_t gfn, u8 *max_level); /* vmenter.S */ From patchwork Mon Jun 12 04:25:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275554 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9E0D2C7EE2E for ; Mon, 12 Jun 2023 04:54:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235400AbjFLEyN (ORCPT ); Mon, 12 Jun 2023 00:54:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235401AbjFLExi (ORCPT ); Mon, 12 Jun 2023 00:53:38 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2063.outbound.protection.outlook.com [40.107.237.63]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B02E21B8; Sun, 11 Jun 2023 21:53:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YsKC+Tj6vv9K6MyEmihXturAIvX2cRP14OEt/ylsvYSlcrHxSXwfCTnkMpiKea1bAY9oWxnsiw7wmpge0vIVjGD+srUnCCxACy2zlrtMKFYzuUdvBCmB/DbLYVF16GGpUFmKw3cMUlK1DgH4FprQorTONXYu3I9ZF/sbvjRipOpli7y3x/kKEGeiICooqNGKMC9LjehqA1Q4ODjjXjYAk+/VIaYC6SQ79OYDYd8Z7eJ8VKfupt/ZiqCpR5vrIYd06DbcvKQTLElMDznoE0Nh5HVjIT0BVdlGIPxfUwJizI5D9N7Miw3PWfPyT9p1zMzb6bwLobIPnqrFQbVWsBD0yQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=oCqcVUonepiEjlzgOGv6GcVfSdkI7MUY7xviMc0Iq1o=; b=DVzLFdxVG8FYtW6qhAb1qxIcDQfI9abm5VKdLdhOVyxAaTiny5PlNB3A1n1Pdmj0NZDMhOPNc9sETaIwsay5LqRixU+8l//WMdPBAJZevWF2ohr7mSl9Lgb2fXtRqlSIVImIGatTU/hQ3zTaefc6K0KglxwNFMzIhcfdfksZvQl1cfA2p+6wmjxFap/Uwj56HjpMrt96e0uZ/aph2agopveKej9lus0e6nZVOOD5Nv2D6OrC/LNq0+bG+ho7tqp0UaF8lFUkqnZRIBBxeWXu60Iphtu0sQymoQodxLfkJIyHcmZu0iHGXFftGGHoYHdo5VS5EAXSBH2ver5c1rs4Jg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=oCqcVUonepiEjlzgOGv6GcVfSdkI7MUY7xviMc0Iq1o=; b=bB+ToZTHqjgjvUENeAZzDmvV9kQFy8vk+QhA0ogyshgqRzLxPqtJMsPN5qb1Q5f23xtyIp8sbphWG48X6kHvzLF42831z1BOnsy23mp3R1Ft+vBSmVDd8J8//sSw8TyhBNVeIkSGa0qhJ5Ol/0DcPaDvS+s4Ud8HULGtSojEgTc= Received: from DM6PR06CA0051.namprd06.prod.outlook.com (2603:10b6:5:54::28) by BY5PR12MB4033.namprd12.prod.outlook.com (2603:10b6:a03:213::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.38; Mon, 12 Jun 2023 04:53:33 +0000 Received: from CY4PEPF0000E9DB.namprd05.prod.outlook.com (2603:10b6:5:54:cafe::a4) by DM6PR06CA0051.outlook.office365.com (2603:10b6:5:54::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.34 via Frontend Transport; Mon, 12 Jun 2023 04:53:33 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9DB.mail.protection.outlook.com (10.167.241.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:53:32 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:53:32 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 45/51] KVM: SEV: Implement gmem hook for invalidating private pages Date: Sun, 11 Jun 2023 23:25:53 -0500 Message-ID: <20230612042559.375660-46-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9DB:EE_|BY5PR12MB4033:EE_ X-MS-Office365-Filtering-Correlation-Id: e5dfa6b3-63fc-4e73-ad9b-08db6b00f95a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: CcduQt2Zpch0HP3FPh+ToWmsS9MZaye7g2VwFR3+IEo864sV4H0DUKFBuJHsf1RGkzjwo/umEnCy0OnfsojR2Q/c4wES/YOj3o4du7n6b2tLNMkeEaByJz9pYbphkQfHHEAfqa6cLmTPZ+MFtEGKVPvOtLfy3O+/5h58w0+CiyLSFk1BdRBYGn5yktMltMVdFe+THcBkIos1s4u8A4wQ91RJgiKzuFBol1fqxFcrdgc3cEEizrNZGbVCkGsONU8AyLOrqijW35vycipghflrWR4NAxmpw5uYbb30G1yUfjM/3k6Mk+7p8hc62aM0QMZ+qfKPquZpndjGmnpK/ALYodPaQz0bSruT+WOPlfIV+bWMuER97u4K0r0+7VnMz49nC8BnAiC3pLgbi87zv63mmKQdjEbuAEL5Mp5r85IbUe7iy5YwJQAjEjnoN9o6HJSnqGy5CmOwpRgjIVrESAIm7l6KD1j+k6tqMe45tUno9VuoMTtSf/TIZj5iexNWzfLgRvb0XPp5XSkrUbx/rq8VllAKmPPAMbSSkMDDX7oyxEcpgCVXAbjXmqo00Q0kTH6Gr383tBS9lC7rTxSw9svZQYXZYau+DXOzEqx62egIamfi7mcIsO/LmlHBpEJHPc9AdFk3FoJd9qu29aAPhQrpw+tFHIOwumOTrJh64Esl4TxvvdSVw9AIRUvFZ7EBvjyOBDeBQ7T355eKAql+igUNTKNsQYu5tjwcS3TGlfvtbDsI4uAQSDSICB6QoNVU0zeANazb8badDm/qNpone7txuA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(396003)(376002)(136003)(39850400004)(346002)(451199021)(40470700004)(46966006)(36840700001)(36756003)(2906002)(44832011)(82310400005)(5660300002)(7416002)(7406005)(86362001)(40460700003)(83380400001)(40480700001)(6666004)(47076005)(426003)(1076003)(186003)(36860700001)(16526019)(336012)(26005)(356005)(478600001)(82740400003)(81166007)(54906003)(70206006)(6916009)(316002)(2616005)(41300700001)(4326008)(8936002)(70586007)(8676002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:53:32.9292 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e5dfa6b3-63fc-4e73-ad9b-08db6b00f95a X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9DB.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4033 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Implement a platform hook to do the work of restoring the direct map entries of gmem-managed pages and transitioning the corresponding RMP table entries back to the default shared/hypervisor-owned state. Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 43 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 45 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index c5a1706387bf..543926fa3200 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -4117,3 +4117,46 @@ int sev_gmem_prepare(struct kvm *kvm, struct kvm_memory_slot *slot, return rc; } + +void sev_gmem_invalidate(struct kvm *kvm, kvm_pfn_t start, kvm_pfn_t end) +{ + kvm_pfn_t pfn; + + if (!sev_snp_guest(kvm)) + return; + + pr_debug("%s: kvm %p pfn 0x%llx pfn_end 0x%llx\n", + __func__, kvm, start, end); + + for (pfn = start; pfn < end; pfn++) { + int rc, rmp_level; + bool assigned; + + rc = snp_lookup_rmpentry(pfn, &assigned, &rmp_level); + if (rc) { + pr_warn_ratelimited("SEV: Failed to retrieve RMP entry for PFN 0x%llx error %d\n", + pfn, rc); + continue; + } + + if (!assigned) + continue; + + /* + * If PFN is currently assigned as a 2M page, PSMASH it into + * individual 4K RMP entries before attempting to convert a + * 4K sub-page. + */ + if (rmp_level > PG_LEVEL_4K) { + rc = snp_rmptable_psmash(kvm, pfn); + if (rc) + pr_warn_ratelimited("SEV: Failed to PSMASH RMP entry for PFN 0x%llx error %d\n", + pfn, rc); + } + + rc = rmp_make_shared(pfn, PG_LEVEL_4K); + if (rc) + pr_warn_ratelimited("SEV: Failed to update RMP entry for PFN 0x%llx error %d\n", + pfn, rc); + } +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 9085a122907c..1390e47d0aa5 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4936,6 +4936,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .alloc_apic_backing_page = svm_alloc_apic_backing_page, .gmem_prepare = sev_gmem_prepare, + .gmem_invalidate = sev_gmem_invalidate, }; /* diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 0438f52e4396..0d4c29a4300a 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -772,6 +772,7 @@ void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code); void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu); int sev_gmem_prepare(struct kvm *kvm, struct kvm_memory_slot *slot, kvm_pfn_t pfn, gfn_t gfn, u8 *max_level); +void sev_gmem_invalidate(struct kvm *kvm, kvm_pfn_t start, kvm_pfn_t end); /* vmenter.S */ From patchwork Mon Jun 12 04:25:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275555 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0F32FC7EE45 for ; Mon, 12 Jun 2023 04:54:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235669AbjFLEyd (ORCPT ); Mon, 12 Jun 2023 00:54:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235651AbjFLEyC (ORCPT ); Mon, 12 Jun 2023 00:54:02 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2059.outbound.protection.outlook.com [40.107.237.59]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98013172A; Sun, 11 Jun 2023 21:53:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hbK1RVBoLakfJKebKIj2MALNHZzQw1p+CvLKpXQrvPIsWr9eU3Tq5vX+LOgz4uGVGcGkn+wTj9F5QXDRlsADltqd0W9Inak4rJdeJU36Laz4r5uUMs3flHCPYSvHz8PHyp4XjJ9obDPh4F5UL/c3O9sripDINsiCs7XWUNNsVL5PsDBgRgNtnSCR5tEIjccctKrz8lSImgrC+cDBGW2abF/bSTOuF0twR9MDCuawPR/BIxBpiZZ1MVTs6lvNVpkrP+o4kMRzJHJ8WbqK+22hYZSUkzlGny9YhOT9MRq41qZcb9709grEUpxJ2gWFOj1sXtwOG266MqzlJwcSuJpOJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ARsbbAueoW5+HOX2d28yHASiDj/JlfTj9Ohag+P+WKw=; b=JH05SSQTd321eg5s4dht+w72gHVo0XidnWNqhdm+bk/JXUQl9l+L5Qa40Vf2hGOprYLinLEMbHrMHXCcJ1CyIlzH9SbJnCWnEJ/YDhxd+ZGjkm/Aw0MoxyBZCQ5Y3GUV7DBwAHrvqZR7IYoO9SKfa8wFXQ2/6UuROinLsMoMUmvtbSDehMP8HZQDak0tp+GcRfSIsA/UgJhfD8A/rlTDcfWQmtx9ofFyIfcv6ooMv8mVg+HKGLt0kbMc6v3WjmQP759clxKeZBDVS01YNhJHQu8Ebz9H0ckEixUSZ/ut4jzKRvBjCfGRj5UdLRaZGh5Pk4YdHq0U5+KUa1l3xkRxGA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=ARsbbAueoW5+HOX2d28yHASiDj/JlfTj9Ohag+P+WKw=; b=aWz0M+jjPfMVQWsNEAxpSWNSEEyP8OCwkLgtqkb2xCuDpkf/MONxbMolHaFhvmD55pEJJ3rry1se8iEDluYeA2/gUG1nyvAqFwJXhJrfNT0nZBlhPz6cSyTm+yBV+cQkfgxFbF8VH6jjX5YxmYNMTbAsXQU330CStc8BXTUMqr4= Received: from DS7PR03CA0288.namprd03.prod.outlook.com (2603:10b6:5:3ad::23) by DS0PR12MB6581.namprd12.prod.outlook.com (2603:10b6:8:d3::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.36; Mon, 12 Jun 2023 04:53:55 +0000 Received: from CY4PEPF0000E9D9.namprd05.prod.outlook.com (2603:10b6:5:3ad:cafe::6e) by DS7PR03CA0288.outlook.office365.com (2603:10b6:5:3ad::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.34 via Frontend Transport; Mon, 12 Jun 2023 04:53:54 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9D9.mail.protection.outlook.com (10.167.241.77) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.21 via Frontend Transport; Mon, 12 Jun 2023 04:53:54 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:53:54 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 46/51] KVM: SVM: Add module parameter to enable the SEV-SNP Date: Sun, 11 Jun 2023 23:25:54 -0500 Message-ID: <20230612042559.375660-47-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D9:EE_|DS0PR12MB6581:EE_ X-MS-Office365-Filtering-Correlation-Id: 0169d4fa-b07c-4f8b-4d08-08db6b010661 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: TecPksSkmZkpyzqHwDKTafx7h6RP/MFFlXu9cOpeq2Mz2nCzdjJdIukZy3b8j1i8P50hPP+gjbq68Xa+Qecc8aX0/6FwclY+dzJ8es44Xig6ayNdyECOoY3i6QaQosgkUTvlon0U5m8taTtAMWdYWe71aObzQzQ3XaTCqL6EOQEvE6ODIVdbpxCFZ6uCKrnnEk5cG/F3LDA6FbeN9tgyJ5vHgR3va5PXHlIQdCwPOFvRaUm3AjarrXgcsiijz2ncVrWeJOJ3qtqNFufZA7mfDRvzK8ciAziXWJxvLQAMKfGJil1X5366hQRQZNYd5h9gqAUNMj10zk9h4wIcBHy9AgxkH2Y5Rj4oPmoMo6NaFV3VUGzol9cGqG33yA0wo50l6TihXb5jaECxoIR4RZyXEVw7AM45d/CB2Yjq709ipAtrST+G2xW3LqRjeH3O3xdTIoyMoIsdbgZEwcmpjQHKsBwZVvks+4Ix2IKrS0sap5zmocmyX+H+fEbqHQd/CP1yxIWxhh51gyPUeDP0P+MXkxgsFsrSrzcb1tLOCMn46thc3vyvcoTaLsSrnlO1PToy1WH29MYXHfS2+PzUvpEcaGcLL/jC0oVxu0auId4i3hdRaU8UO2QLiwaKtAc9WY3bDVSMSmX81Q4KIwMIgbyjdkFyC6WklESp4IfxxmQt1l2pK1oqNohwBDEhDXSGgNYupQnZ00lTacfnqoDzXy9csXwVWJQdimd6SUd3C8F20VbO7xP1DpqSla8WoNfICsuqnhM9JtBEaqt78sztvbzQtw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(376002)(396003)(346002)(136003)(39860400002)(451199021)(36840700001)(40470700004)(46966006)(8936002)(8676002)(44832011)(5660300002)(7406005)(7416002)(4326008)(6916009)(70586007)(70206006)(316002)(54906003)(2906002)(41300700001)(36860700001)(40460700003)(478600001)(81166007)(40480700001)(356005)(26005)(1076003)(16526019)(36756003)(426003)(336012)(83380400001)(186003)(47076005)(2616005)(82740400003)(82310400005)(86362001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:53:54.7986 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0169d4fa-b07c-4f8b-4d08-08db6b010661 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D9.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB6581 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh Add a module parameter than can be used to enable or disable the SEV-SNP feature. Now that KVM contains the support for the SNP set the GHCB hypervisor feature flag to indicate that SNP is supported. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 543926fa3200..adbe8c242d81 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -54,14 +54,16 @@ module_param_named(sev, sev_enabled, bool, 0444); /* enable/disable SEV-ES support */ static bool sev_es_enabled = true; module_param_named(sev_es, sev_es_enabled, bool, 0444); + +/* enable/disable SEV-SNP support */ +static bool sev_snp_enabled = true; +module_param_named(sev_snp, sev_snp_enabled, bool, 0444); #else #define sev_enabled false #define sev_es_enabled false +#define sev_snp_enabled false #endif /* CONFIG_KVM_AMD_SEV */ -/* enable/disable SEV-SNP support */ -static bool sev_snp_enabled; - #define AP_RESET_HOLD_NONE 0 #define AP_RESET_HOLD_NAE_EVENT 1 #define AP_RESET_HOLD_MSR_PROTO 2 From patchwork Mon Jun 12 04:25:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275556 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E1416C7EE25 for ; Mon, 12 Jun 2023 04:55:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235190AbjFLEz3 (ORCPT ); Mon, 12 Jun 2023 00:55:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235813AbjFLEyu (ORCPT ); Mon, 12 Jun 2023 00:54:50 -0400 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2056.outbound.protection.outlook.com [40.107.92.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 298D1268A; Sun, 11 Jun 2023 21:54:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Lcw/0SkLEgUMhS2rbtls+xYPYwN7YJjzp+1X3+EmQg/ZEmAoOxHF0oOpyBSkYi62h0M/2yjVl7wuz2E4hZ5WQ8KZZFxV0ic98chYIbEBwGFJCi1ns+GEArqSxNIVi6cWmDM4psj7ch46Dl65hqUcGZFXZgNJ99R26B6CmNTTP/7P9UrPl8e3cF5EHx4/wWeTnpPQlFT4O6woOjzgnw06TZnl968geHyu8BkP+oW1alrG3NYDR1Cvy+f7CZSmqjMDoST17uOYUGZQR/XxR/a1Bpy2xzfe11LI2DcrPFGPmZZlk/Y1q7+OSoVt+Ff7x0pjHo7Gqob+nVPOPC1PHxHpXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=W7mzzYO76w209ixfz6AUBtX4bKrAJTU7/Cj1iL+ERcw=; b=Qd0v7n0P7UKHHtbZYiWrCvgx2i2P1Ec1zJ7hpzNQyV0/uahqK4nn0aX+gTq+CPCeQO4LrdvxAaWzKekFdzeUobRe8klT3Fe+K8SDGWOXRcYqR1KJt0Pm0jDMOvJCBF8AfaPcwokEx1uLVmgQ1n0ERTacgLI/qLBrU8Q40XgsDthLfEfNmlPy48Gcj7n+JRhV0iKgcPhHPO/pvjtZG1zIjqpZZMkey7TjEuoEqb8PmaQYEfc30R2PuMbkCgib/8brVlzZz2D3GZZMdsX+cApDs7FA02pcv7bd37RQlm15FF8dcdwDD47pTAiMEu6Q8iQd6tJeKjBiKWjG1EV0OJsTog== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=W7mzzYO76w209ixfz6AUBtX4bKrAJTU7/Cj1iL+ERcw=; b=CyTDyF8dQbEeJP9GphGEJz8lMj+iqLesa4TWDm/RAr7qBfzq7tqJ4P3lOA1D0fUtEZI2H5lVgw8WLam47WT1ynS0uIJOg/ML2RRNe1ahC2kMikER9mDlapWarpAn0DVzL13O9I/8Gn1kTtSkxg7iF8O/I6d03zmq6nBzrHE3Atw= Received: from CYXPR03CA0084.namprd03.prod.outlook.com (2603:10b6:930:d3::8) by CH0PR12MB5330.namprd12.prod.outlook.com (2603:10b6:610:d5::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.29; Mon, 12 Jun 2023 04:54:17 +0000 Received: from CY4PEPF0000E9D6.namprd05.prod.outlook.com (2603:10b6:930:d3:cafe::dd) by CYXPR03CA0084.outlook.office365.com (2603:10b6:930:d3::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:54:17 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9D6.mail.protection.outlook.com (10.167.241.80) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:54:16 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:54:16 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v9 47/51] iommu/amd: Add IOMMU_SNP_SHUTDOWN support Date: Sun, 11 Jun 2023 23:25:55 -0500 Message-ID: <20230612042559.375660-48-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D6:EE_|CH0PR12MB5330:EE_ X-MS-Office365-Filtering-Correlation-Id: 24cc878b-6a36-43ee-53a1-08db6b01139a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: zId6Rx0KWJvdknOLotHWFbZMQccpKFohyIcqZHwOp2tL/AbmvAUWk0CyHdU7xkOi51lfzojxnE9gYdE30fWM5+RAxoAN/h6wY13rUFnFLg/mFQxQK/iG2mUFlRJvCMMIlSDbiszRVkkwGNoIzIO+VXWz9EqHqV0/pKn4aeycwsFbqI0I5uvZZ7dQP1KJhIzWP7ov0rDcFM5auMvQncdtJE6438WsMnWWKGrGNdkBfQywz8QrsjsdlTG/DRONluliUVIqMegWS823JeZ3F5Y8vEE840Blz47ExW22U5Xbwh/OMpnjqTDGvc4xahE9YT0ch9XQBukLvznBRLa1J2NxZZVIk1rDUL4kbcannC695gfBn/zWFzjO5hnXdBdZyO0qFspzmTTiBtEi6EMnZli+qHwNYtYJvD6E8CwiBlWa0Jl+1FZk2LRYph26qvI0T1jbrHQ/zc9LWSMdO5Qx+tLfpcHeUsZHu/CtpIRJvh8ASeQ6uq6vALjjdeMDLSPwdy/3pkhzgzOMKBQYGaQgpDlZKhU1EvZrwr3HLxo914fD1fKbtnFQwPFZIDPLXPtGNvGDm4bWdpnm0LL6UdlfFqSWwCnUYtLNlUOlz6G+yx1FUzAceVlqv7VRsuiSbwNlSLtGL5pnnda843qTR7XKLI4hk+4hQdEyQ/7hTxLRK7dg9heNkAuN7yftmOkK7E0mLXOMjZog16omGZ8eLraFikcNvLYv/5veo47d6zM6+XkoxcFaeMDVWWN1a5aU+8l9SXj4TpNvDB/SSdgM4yme8JbrZw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(376002)(346002)(39860400002)(136003)(396003)(451199021)(46966006)(36840700001)(40470700004)(40460700003)(6666004)(47076005)(36756003)(426003)(336012)(83380400001)(2616005)(86362001)(82310400005)(82740400003)(81166007)(1076003)(26005)(356005)(40480700001)(36860700001)(186003)(16526019)(2906002)(54906003)(44832011)(4326008)(6916009)(316002)(7406005)(7416002)(70206006)(70586007)(41300700001)(5660300002)(478600001)(8936002)(8676002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:54:16.9827 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 24cc878b-6a36-43ee-53a1-08db6b01139a X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D6.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5330 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Ashish Kalra Add a new IOMMU API interface amd_iommu_snp_disable() to transition IOMMU pages to Hypervisor state from Reclaim state after SNP_SHUTDOWN_EX command. Invoke this API from the CCP driver after SNP_SHUTDOWN_EX command. Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 20 +++++++++++++ drivers/iommu/amd/init.c | 55 ++++++++++++++++++++++++++++++++++++ include/linux/amd-iommu.h | 1 + 3 files changed, 76 insertions(+) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 0bfe9721c977..b8e8c4da4025 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -1508,6 +1509,25 @@ static int __sev_snp_shutdown_locked(int *error) return ret; } + /* + * SNP_SHUTDOWN_EX with IOMMU_SNP_SHUTDOWN set to 1 disables SNP + * enforcement by the IOMMU and also transitions all pages + * associated with the IOMMU to the Reclaim state. + * Firmware was transitioning the IOMMU pages to Hypervisor state + * before version 1.53. But, accounting for the number of assigned + * 4kB pages in a 2M page was done incorrectly by not transitioning + * to the Reclaim state. This resulted in RMP #PF when later accessing + * the 2M page containing those pages during kexec boot. Hence, the + * firmware now transitions these pages to Reclaim state and hypervisor + * needs to transition these pages to shared state. SNP Firmware + * version 1.53 and above are needed for kexec boot. + */ + ret = amd_iommu_snp_disable(); + if (ret) { + dev_err(sev->dev, "SNP IOMMU shutdown failed\n"); + return ret; + } + sev->snp_initialized = false; dev_dbg(sev->dev, "SEV-SNP firmware shutdown\n"); diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c index 33ea62d93540..a84ec81cfbb5 100644 --- a/drivers/iommu/amd/init.c +++ b/drivers/iommu/amd/init.c @@ -30,6 +30,7 @@ #include #include #include +#include #include @@ -3701,4 +3702,58 @@ int amd_iommu_snp_enable(void) return 0; } + +static int iommu_page_make_shared(void *page) +{ + unsigned long paddr, pfn; + + paddr = iommu_virt_to_phys(page); + /* Cbit maybe set in the paddr */ + pfn = __sme_clr(paddr) >> PAGE_SHIFT; + return rmp_make_shared(pfn, PG_LEVEL_4K); +} + +static int iommu_make_shared(void *va, size_t size) +{ + void *page; + int ret; + + if (!va) + return 0; + + for (page = va; page < (va + size); page += PAGE_SIZE) { + ret = iommu_page_make_shared(page); + if (ret) + return ret; + } + + return 0; +} + +int amd_iommu_snp_disable(void) +{ + struct amd_iommu *iommu; + int ret; + + if (!amd_iommu_snp_en) + return 0; + + for_each_iommu(iommu) { + ret = iommu_make_shared(iommu->evt_buf, EVT_BUFFER_SIZE); + if (ret) + return ret; + + ret = iommu_make_shared(iommu->ppr_log, PPR_LOG_SIZE); + if (ret) + return ret; + + ret = iommu_make_shared((void *)iommu->cmd_sem, PAGE_SIZE); + if (ret) + return ret; + } + + amd_iommu_snp_en = false; + return 0; +} +EXPORT_SYMBOL_GPL(amd_iommu_snp_disable); #endif diff --git a/include/linux/amd-iommu.h b/include/linux/amd-iommu.h index 8f0cde2d451c..7ba46118d0f1 100644 --- a/include/linux/amd-iommu.h +++ b/include/linux/amd-iommu.h @@ -208,6 +208,7 @@ struct amd_iommu *get_amd_iommu(unsigned int idx); #ifdef CONFIG_KVM_AMD_SEV int amd_iommu_snp_enable(void); +int amd_iommu_snp_disable(void); #endif #endif /* _ASM_X86_AMD_IOMMU_H */ From patchwork Mon Jun 12 04:25:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275575 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A4FA8C7EE25 for ; Mon, 12 Jun 2023 04:56:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235574AbjFLE4R (ORCPT ); Mon, 12 Jun 2023 00:56:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235606AbjFLEzp (ORCPT ); Mon, 12 Jun 2023 00:55:45 -0400 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2060b.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e8c::60b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A644319B5; Sun, 11 Jun 2023 21:55:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gihk28j4nlJGaBEvpAa1Hj3qp6RG9Yd6XHd2k5l+xJPcsMwLNOiocflDSP3twRmZLVakf9J+rQuvNBsul/BftsbVmStAe+Q9G5yF9bfeLmoRF/0F3hObTKwkpFYTqauTh1bQJQwGD/faZYuwLKH7TMnEf2DvR+IJo+ACZkzN4mZTHVuBXzzDosLFNCtHdv2eHgywBi+EAoXtTU7r5Hdk037K7mfmVFiUW/rwvaGXpqSF1mM9rGVpxR1ICl4elXRV2DULB/Y669blLgqQ9699Aa+Iw9fIrd0GVQZ0SvA/UAQYIbj6aGgTuBXMmbXuiuHvaD05kDljZBYCHCZzIwn9IA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Em/p9x32Hb3bLzmcXjcCKuGKc6huNvCGbDR3djc9uMo=; b=kriR/W6+TjqLimipN0d+jkYB5m/bvTZD76GdkRxKzUVX73qLGHkamrNonPxqMYsG4TRrCVAwW/icvEcl9NmzVG7GNzXon4sDw9ChyM9tRXTNxrcloJopxBTOXkFNtt3snmVdkhi0KyKCkq4GdM3HiZwgL3KXzxdaQSZ1sKTjD0YtsjJF/qC4iDZAg07HQk41ADnRrFjA+3jKeUosYoEvN36+AeemWqy9+RUeGu4h5DcnnpzPwnQGQXGHaoIiEIHUrd8LwnGGOGuLTOXU8nY/6dxtZSJUNfMbnBBcCCIrwamdPzxud7bVyixe4TMsfJXvbJ9a3r4c+Go8AdPjaTHyBA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=Em/p9x32Hb3bLzmcXjcCKuGKc6huNvCGbDR3djc9uMo=; b=Fljl715eY0mSF1DjWxiyfEmAbYjMZ46WVo4c4w0YbYEL0pwvBV4wksgqqhZtKP10i3646cyDIWYUmx5xXN9ywBtynQLLKflauh9I/4jjI54YI1xKmBFTYULi+5h4scPZwWXod4fCFi0mRuI9paMhXf+7fAMRAxaEuKiEDA/ihQw= Received: from CYXPR03CA0095.namprd03.prod.outlook.com (2603:10b6:930:d3::6) by DS0PR12MB7557.namprd12.prod.outlook.com (2603:10b6:8:130::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.39; Mon, 12 Jun 2023 04:55:05 +0000 Received: from CY4PEPF0000E9D6.namprd05.prod.outlook.com (2603:10b6:930:d3:cafe::3b) by CYXPR03CA0095.outlook.office365.com (2603:10b6:930:d3::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.34 via Frontend Transport; Mon, 12 Jun 2023 04:55:05 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9D6.mail.protection.outlook.com (10.167.241.80) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:55:04 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:54:58 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Alexey Kardashevskiy , Dionna Glaze Subject: [PATCH RFC v9 48/51] crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command Date: Sun, 11 Jun 2023 23:25:56 -0500 Message-ID: <20230612042559.375660-49-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D6:EE_|DS0PR12MB7557:EE_ X-MS-Office365-Filtering-Correlation-Id: 1439d436-96b9-4ba9-0e0c-08db6b01302d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: IbMju/P4XXqkxJKDm5mlOOpvJe/nMzOQPC/YYKab8FAUWZ8ZdJOnHi6iqBQFESk0Cvlj0UZ+nPThdMXdfGoJNRwvUxPZMLypALZViFjiiF8WN46UKpC5aCJd8xV3/qZAEm+sy8RaAsoSCZM2vrJWV0V0evmame1KCzOb8VlXvkutOl8AeGyHJRJdrJrKb6c+5lnJs7mMXiyoaZ9+b0mjjOpd/8fIu7q/8aXbVMvbcE2hCeA0yxdSHu11LY5XmMxMuJkC3GM4lPsIdKean4PT0zu31IqhOrJpSTbLIgwtKrqKPz5SEALHdMF/gVBEkYngSRzaZ1xYTVKAMM9o5jBSsogNYflKEcmH1x5MCKKW1b93CF42NTiyMnw8j5mJYSS1gwvprTx9bBYBmzNouWS6b0hrRL8upFwjIWTriza1zBaWBSt5cfjJvjPgxFgeVq4s1stOfjvhYDU8sdBbAsqUYRH+hZEHmblaFwnhD8P+P6aoUfph6XdsyI39nhn4Xt3XVtsNiniRRLz0n4RPhh109KLGvHvHM61lpdeMPx/c/kZI5P3t+4qx6jcKTZEMw3wmzhQHwrH/+Zh/chm/lAkRv3Smk10vyKrj//P5KXynl6MzlGvFYR0VRB67VOTMUebY3vemDLyy1GcUumKTsAMTWBGoP4HF0XWAsOnMzkC2lkhF1HtrV4DHNiiUsPFXV38ysHVdNl6lvds2tZS6y6ofawGbELHHvP9o6QBCOUGtG8oU5CjVjKK+1OH95FM5ynuahbrLmhNDv7fC1DqgKWlzkA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(376002)(396003)(346002)(39860400002)(136003)(451199021)(40470700004)(36840700001)(46966006)(40480700001)(40460700003)(81166007)(54906003)(82740400003)(478600001)(356005)(8676002)(70586007)(2616005)(5660300002)(316002)(4326008)(6916009)(8936002)(41300700001)(70206006)(336012)(426003)(16526019)(36860700001)(186003)(47076005)(83380400001)(26005)(1076003)(82310400005)(86362001)(7416002)(44832011)(36756003)(7406005)(2906002)(30864003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:55:04.9205 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1439d436-96b9-4ba9-0e0c-08db6b01302d X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D6.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB7557 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh The SEV-SNP firmware provides the SNP_CONFIG command used to set the system-wide configuration value for SNP guests. The information includes the TCB version string to be reported in guest attestation reports. Version 2 of the GHCB specification adds an NAE (SNP extended guest request) that a guest can use to query the reports that include additional certificates. In both cases, userspace provided additional data is included in the attestation reports. The userspace will use the SNP_SET_EXT_CONFIG command to give the certificate blob and the reported TCB version string at once. Note that the specification defines certificate blob with a specific GUID format; the userspace is responsible for building the proper certificate blob. The ioctl treats it an opaque blob. While it is not defined in the spec, but let's add SNP_GET_EXT_CONFIG command that can be used to obtain the data programmed through the SNP_SET_EXT_CONFIG. Co-developed-by: Alexey Kardashevskiy Signed-off-by: Alexey Kardashevskiy Co-developed-by: Dionna Glaze Signed-off-by: Dionna Glaze Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra [mdr: squash in doc patch from Dionna] Signed-off-by: Michael Roth --- Documentation/virt/coco/sev-guest.rst | 27 ++++ drivers/crypto/ccp/sev-dev.c | 178 ++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.h | 2 + include/linux/psp-sev.h | 10 ++ include/uapi/linux/psp-sev.h | 17 +++ 5 files changed, 234 insertions(+) diff --git a/Documentation/virt/coco/sev-guest.rst b/Documentation/virt/coco/sev-guest.rst index 11ea67c944df..6cad4226c348 100644 --- a/Documentation/virt/coco/sev-guest.rst +++ b/Documentation/virt/coco/sev-guest.rst @@ -145,6 +145,33 @@ The SNP_PLATFORM_STATUS command is used to query the SNP platform status. The status includes API major, minor version and more. See the SEV-SNP specification for further details. +2.5 SNP_SET_EXT_CONFIG +---------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_data_snp_ext_config +:Returns (out): 0 on success, -negative on error + +The SNP_SET_EXT_CONFIG is used to set the system-wide configuration such as +reported TCB version in the attestation report. The command is similar to +SNP_CONFIG command defined in the SEV-SNP spec. The main difference is the +command also accepts an additional certificate blob defined in the GHCB +specification. + +If the certs_address is zero, then the previous certificate blob will deleted. +For more information on the certificate blob layout, see the GHCB spec +(extended guest request message). + +2.6 SNP_GET_EXT_CONFIG +---------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_data_snp_ext_config +:Returns (out): 0 on success, -negative on error + +The SNP_GET_EXT_CONFIG is used to query the system-wide configuration set +through the SNP_SET_EXT_CONFIG. + 3. SEV-SNP CPUID Enforcement ============================ diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index b8e8c4da4025..175c24163ba0 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -1491,6 +1491,10 @@ static int __sev_snp_shutdown_locked(int *error) data.length = sizeof(data); data.iommu_snp_shutdown = 1; + /* Free the memory used for caching the certificate data */ + sev_snp_certs_put(sev->snp_certs); + sev->snp_certs = NULL; + wbinvd_on_all_cpus(); retry: @@ -1829,6 +1833,126 @@ static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) return ret; } +static int sev_ioctl_snp_get_config(struct sev_issue_cmd *argp) +{ + struct sev_device *sev = psp_master->sev_data; + struct sev_user_data_ext_snp_config input; + struct sev_snp_certs *snp_certs; + int ret; + + if (!sev->snp_initialized || !argp->data) + return -EINVAL; + + memset(&input, 0, sizeof(input)); + + if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) + return -EFAULT; + + /* Copy the TCB version programmed through the SET_CONFIG to userspace */ + if (input.config_address) { + if (copy_to_user((void * __user)input.config_address, + &sev->snp_config, sizeof(struct sev_user_data_snp_config))) + return -EFAULT; + } + + snp_certs = sev_snp_certs_get(sev->snp_certs); + + /* Copy the extended certs programmed through the SNP_SET_CONFIG */ + if (input.certs_address && snp_certs) { + if (input.certs_len < snp_certs->len) { + /* Return the certs length to userspace */ + input.certs_len = snp_certs->len; + + ret = -EIO; + goto e_done; + } + + if (copy_to_user((void * __user)input.certs_address, + snp_certs->data, snp_certs->len)) { + ret = -EFAULT; + goto put_exit; + } + } + + ret = 0; + +e_done: + if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) + ret = -EFAULT; + +put_exit: + sev_snp_certs_put(snp_certs); + + return ret; +} + +static int sev_ioctl_snp_set_config(struct sev_issue_cmd *argp, bool writable) +{ + struct sev_device *sev = psp_master->sev_data; + struct sev_user_data_ext_snp_config input; + struct sev_user_data_snp_config config; + struct sev_snp_certs *snp_certs = NULL; + void *certs = NULL; + int ret = 0; + + if (!sev->snp_initialized || !argp->data) + return -EINVAL; + + if (!writable) + return -EPERM; + + memset(&input, 0, sizeof(input)); + + if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) + return -EFAULT; + + /* Copy the certs from userspace */ + if (input.certs_address) { + if (!input.certs_len || !IS_ALIGNED(input.certs_len, PAGE_SIZE)) + return -EINVAL; + + certs = psp_copy_user_blob(input.certs_address, input.certs_len); + if (IS_ERR(certs)) + return PTR_ERR(certs); + } + + /* Issue the PSP command to update the TCB version using the SNP_CONFIG. */ + if (input.config_address) { + memset(&config, 0, sizeof(config)); + if (copy_from_user(&config, + (void __user *)input.config_address, sizeof(config))) { + ret = -EFAULT; + goto e_free; + } + + ret = __sev_do_cmd_locked(SEV_CMD_SNP_CONFIG, &config, &argp->error); + if (ret) + goto e_free; + + memcpy(&sev->snp_config, &config, sizeof(config)); + } + + /* + * If the new certs are passed then cache it else free the old certs. + */ + if (input.certs_len) { + snp_certs = sev_snp_certs_new(certs, input.certs_len); + if (!snp_certs) { + ret = -ENOMEM; + goto e_free; + } + } + + sev_snp_certs_put(sev->snp_certs); + sev->snp_certs = snp_certs; + + return 0; + +e_free: + kfree(certs); + return ret; +} + static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) { void __user *argp = (void __user *)arg; @@ -1883,6 +2007,12 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) case SNP_PLATFORM_STATUS: ret = sev_ioctl_snp_platform_status(&input); break; + case SNP_SET_EXT_CONFIG: + ret = sev_ioctl_snp_set_config(&input, writable); + break; + case SNP_GET_EXT_CONFIG: + ret = sev_ioctl_snp_get_config(&input); + break; default: ret = -EINVAL; goto out; @@ -1931,6 +2061,54 @@ int sev_guest_df_flush(int *error) } EXPORT_SYMBOL_GPL(sev_guest_df_flush); +static void sev_snp_certs_release(struct kref *kref) +{ + struct sev_snp_certs *certs = container_of(kref, struct sev_snp_certs, kref); + + kfree(certs->data); + kfree(certs); +} + +struct sev_snp_certs *sev_snp_certs_new(void *data, u32 len) +{ + struct sev_snp_certs *certs; + + if (!len || !data) + return NULL; + + certs = kzalloc(sizeof(*certs), GFP_KERNEL); + if (!certs) + return NULL; + + certs->data = data; + certs->len = len; + kref_init(&certs->kref); + + return certs; +} +EXPORT_SYMBOL_GPL(sev_snp_certs_new); + +struct sev_snp_certs *sev_snp_certs_get(struct sev_snp_certs *certs) +{ + if (!certs) + return NULL; + + if (!kref_get_unless_zero(&certs->kref)) + return NULL; + + return certs; +} +EXPORT_SYMBOL_GPL(sev_snp_certs_get); + +void sev_snp_certs_put(struct sev_snp_certs *certs) +{ + if (!certs) + return; + + kref_put(&certs->kref, sev_snp_certs_release); +} +EXPORT_SYMBOL_GPL(sev_snp_certs_put); + static void sev_exit(struct kref *ref) { misc_deregister(&misc_dev->misc); diff --git a/drivers/crypto/ccp/sev-dev.h b/drivers/crypto/ccp/sev-dev.h index 19d79f9d4212..22374f3d3e2e 100644 --- a/drivers/crypto/ccp/sev-dev.h +++ b/drivers/crypto/ccp/sev-dev.h @@ -66,6 +66,8 @@ struct sev_device { bool snp_initialized; struct snp_host_map snp_host_map[MAX_SNP_HOST_MAP_BUFS]; + struct sev_snp_certs *snp_certs; + struct sev_user_data_snp_config snp_config; }; int sev_dev_init(struct psp_device *psp); diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 5ae61de96e44..2191d8b5423a 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -24,6 +24,16 @@ #define SEV_FW_BLOB_MAX_SIZE 0x4000 /* 16KB */ +struct sev_snp_certs { + void *data; + u32 len; + struct kref kref; +}; + +struct sev_snp_certs *sev_snp_certs_new(void *data, u32 len); +struct sev_snp_certs *sev_snp_certs_get(struct sev_snp_certs *certs); +void sev_snp_certs_put(struct sev_snp_certs *certs); + /** * SEV platform state */ diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index 4dc6a3e7b3d5..d1e6a0615546 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -29,6 +29,8 @@ enum { SEV_GET_ID, /* This command is deprecated, use SEV_GET_ID2 */ SEV_GET_ID2, SNP_PLATFORM_STATUS, + SNP_SET_EXT_CONFIG, + SNP_GET_EXT_CONFIG, SEV_MAX, }; @@ -201,6 +203,21 @@ struct sev_user_data_snp_config { __u8 rsvd1[52]; } __packed; +/** + * struct sev_data_snp_ext_config - system wide configuration value for SNP. + * + * @config_address: address of the struct sev_user_data_snp_config or 0 when + * reported_tcb does not need to be updated. + * @certs_address: address of extended guest request certificate chain or + * 0 when previous certificate should be removed on SNP_SET_EXT_CONFIG. + * @certs_len: length of the certs + */ +struct sev_user_data_ext_snp_config { + __u64 config_address; /* In */ + __u64 certs_address; /* In */ + __u32 certs_len; /* In */ +}; + /** * struct sev_issue_cmd - SEV ioctl parameters * From patchwork Mon Jun 12 04:25:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275576 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C9082C7EE2E for ; Mon, 12 Jun 2023 04:56:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235758AbjFLE4i (ORCPT ); Mon, 12 Jun 2023 00:56:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234189AbjFLE4J (ORCPT ); Mon, 12 Jun 2023 00:56:09 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2058.outbound.protection.outlook.com [40.107.94.58]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6FA6910E9; Sun, 11 Jun 2023 21:55:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CSD00ODkgqyobCVqRS1vYYyrFCgTP8o4093ZMeDcaibuqeRvmwHumD5vzX6yezdpOO2mzhSfUZ1lrQEeDTktTY0HrEJckoUpzp045985lgbyw4/oQnzYGCz38za+9IX+wOGI5f9N+dR1L/Dvji2q+VNnyPOXpXkGXsT9ib/xBH7hEbKbS/yos7IMXZDqv0eiF8ilI0TUp56iirXN2dupTTqv5fH3o1a1KWB+KP2Za3BaJUCH7edAWV3PN57Fyz5x27GjXiId76NwWkZo7H5VzoSIXZUZg4xvLHvoyLZzHDjAbnmHZzU1ERflzL6JGkTxzPB8iaJMccNtzh7q/nNH3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=lQtuNY0mUB1m5WmhFOnk56WbAk95FDsuLAXhADtmTW0=; b=kr3QIuBLsiU01pkN+Wtq0aN/mHAyP/xQ8lPl8QOnnrqgJ/7IuhwsRjSgqozcoVdwFXdA5fmWpAHnwxM+9mQM4X5a4cZNd64xr4FWFmCPeUmP+UB5+wgEUNYGFRqD0BfuWYd27zxVlTp5gi9e1MU9wWdc5NfQlc1QRIkkR9uHjx8IVQ4YZxH90P0rJEDKOgEPI6+oaCalqYhaC9/qQZK9y9xXHK2ewdVfa+sLap2tR03MOdD/90gz6Xp2coQPZaXbFkN/bhQ+JqO4RDqCpFBcAg/mEw+Q8RFyxLCuFtoGZwRrWL06xPLQvQ82vwQ2kJBbIdePSsMVHO9Oh6CZT/cuIg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=lQtuNY0mUB1m5WmhFOnk56WbAk95FDsuLAXhADtmTW0=; b=lBmrpuM4H3c1DDHbphwQ2dN2HGj1tNbjcqUB1HBVEYhMgSWGf9iJWA1u1R2a/cYSo3nSkwe+S2o4PlwjbrLZ5KUOafgao71AFG3+DkZTNGBUGek4NE5b21D/Gtet9EbE97jtw7oJpkDhgH5DxK+ZRvuzZ0X4LzZdOPj0CkWJcuk= Received: from DM5PR08CA0052.namprd08.prod.outlook.com (2603:10b6:4:60::41) by BN9PR12MB5146.namprd12.prod.outlook.com (2603:10b6:408:137::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.44; Mon, 12 Jun 2023 04:55:32 +0000 Received: from CY4PEPF0000E9DB.namprd05.prod.outlook.com (2603:10b6:4:60:cafe::11) by DM5PR08CA0052.outlook.office365.com (2603:10b6:4:60::41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:55:32 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9DB.mail.protection.outlook.com (10.167.241.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:55:31 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:55:30 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Dionna Glaze , Tom Lendacky , Alexey Kardashevskiy Subject: [PATCH RFC v9 49/51] x86/sev: Add KVM commands for per-instance certs Date: Sun, 11 Jun 2023 23:25:57 -0500 Message-ID: <20230612042559.375660-50-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9DB:EE_|BN9PR12MB5146:EE_ X-MS-Office365-Filtering-Correlation-Id: 81a20d62-19de-453a-ae5a-08db6b014041 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 6gqrwROaIbuwPJnGKntokR4IBQyuRdnfX70ONMTqzwn3+buydpoI8Lfr4bUSvkDhWNj6lc8K127WhiiypW7dQWFvX8MN6sYcFtstLH/jUK8SpcH4RKAxCcpPHl4yqvgh4HNGdSV3Y1dw8qIwVbk6jMwyKSovWmBszo/oBTddu+J9Dq2TM6bwjlBDgZUfS/3JW1TzoZgfwRlDlYTaup1gCz1hqrnkPrSjFf7hw50LMHhO/p49j8vENN867EY+xa1u14fx03xDNAJDeIDBM/VsfAhWEfOgFEiD8K3edUr0KzpAL9Zs43O0yakzXzDkG1ET1nPc4LrNjbIxIRQcKuqExaZ8JcY8nVWum7Nnw/G38ZXu+7jFQT8yzAbQMUajLa5V1Q0/4KPoPZYT+vfW+Q29OIj3xuPN+YfWTwpvijhlVMuItGjAKT0PoZOQk4kv6hWR19/mFn9kne8bvXo+eBBwItxvqPai8be0kiiO3Y1iamrRJv96LSWu1dCDLixW1lFyuMPGdO9lje1GZmRZ8KDrkxrtqzA4m9XjhWh/V2WxGqv1HlN+iixM0ElmjvMuxuIqZH7V20fyyCJVQuX7SOHq0lmaTg3io35uzHksauhxE186DBkILoJ0alhRGipQrBaszMPM3NnOs1mA5pE6DwPxCTRdBoQVXEpTF8px4J8kJ5wr6KwCDCGTs+U2lp7sp87F6lvmngkA+B1UYTDbHWoxtsc4Bp7q1YTbjhzWpNxlEOCEpWsGP1QNXYpug5/6ev4jHVA5OfpcJnW8egjVcE9Mhg== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(376002)(346002)(396003)(136003)(451199021)(36840700001)(40470700004)(46966006)(54906003)(5660300002)(6916009)(4326008)(7406005)(7416002)(8936002)(8676002)(44832011)(41300700001)(316002)(2906002)(186003)(16526019)(478600001)(70586007)(70206006)(6666004)(40460700003)(1076003)(356005)(82740400003)(81166007)(40480700001)(26005)(83380400001)(426003)(336012)(47076005)(36756003)(36860700001)(86362001)(82310400005)(2616005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:55:31.8680 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 81a20d62-19de-453a-ae5a-08db6b014041 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9DB.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5146 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Dionna Glaze The /dev/sev device has the ability to store host-wide certificates for the key used by the AMD-SP for SEV-SNP attestation report signing, but for hosts that want to specify additional certificates that are specific to the image launched in a VM, a different way is needed to communicate those certificates. Add two new KVM ioctl to handle this: KVM_SEV_SNP_{GET,SET}_CERTS The certificates that are set with this command are expected to follow the same format as the host certificates, but that format is opaque to the kernel. The new behavior for custom certificates is that the extended guest request command will now return the overridden certificates if they were installed for the instance. The error condition for a too small data buffer is changed to return the overridden certificate data size if there is an overridden certificate set installed. Setting a 0 length certificate returns the system state to only return the host certificates on an extended guest request. Also increase the SEV_FW_BLOB_MAX_SIZE another 4K page to allow space for an extra certificate. Cc: Tom Lendacky Cc: Paolo Bonzini Signed-off-by: Dionna Glaze Signed-off-by: Ashish Kalra [mdr: remove used of "we" and "this patch" in commit log, squash in documentation patch] Signed-off-by: Michael Roth [aik: snp_handle_ext_guest_request() now uses the CCP's cert object without copying things over, only refcounting needed.] Signed-off-by: Alexey Kardashevskiy --- .../virt/kvm/x86/amd-memory-encryption.rst | 44 +++++++ arch/x86/kvm/svm/sev.c | 115 ++++++++++++++++++ arch/x86/kvm/svm/svm.h | 1 + include/linux/psp-sev.h | 2 +- include/uapi/linux/kvm.h | 12 ++ 5 files changed, 173 insertions(+), 1 deletion(-) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index cd77a19577fe..21c1894d78ef 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -537,6 +537,50 @@ Returns: 0 on success, -negative on error See SEV-SNP specification for further details on launch finish input parameters. +22. KVM_SEV_SNP_GET_CERTS +------------------------- + +After the SNP guest launch flow has started, the KVM_SEV_SNP_GET_CERTS command +can be issued to request the data that has been installed with the +KVM_SEV_SNP_SET_CERTS command. + +Parameters (in/out): struct kvm_sev_snp_get_certs + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_get_certs { + __u64 certs_uaddr; + __u64 certs_len + }; + +If no certs have been installed, then the return value is -ENOENT. +If the buffer specified in the struct is too small, the certs_len field will be +overwritten with the required bytes to receive all the certificate bytes and the +return value will be -EINVAL. + +23. KVM_SEV_SNP_SET_CERTS +------------------------- + +After the SNP guest launch flow has started, the KVM_SEV_SNP_SET_CERTS command +can be issued to override the /dev/sev certs data that is returned when a +guest issues an extended guest request. This is useful for instance-specific +extensions to the host certificates. + +Parameters (in/out): struct kvm_sev_snp_set_certs + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_set_certs { + __u64 certs_uaddr; + __u64 certs_len + }; + +The certs_len field may not exceed SEV_FW_BLOB_MAX_SIZE. + References ========== diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index adbe8c242d81..bdf32aa971d8 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2277,6 +2277,113 @@ static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) return ret; } +static int snp_get_instance_certs(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct kvm_sev_snp_get_certs params; + struct sev_snp_certs *snp_certs; + int rc = 0; + + 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; + + snp_certs = sev_snp_certs_get(sev->snp_certs); + /* No instance certs set. */ + if (!snp_certs) + return -ENOENT; + + if (params.certs_len < sev->snp_certs->len) { + /* Output buffer too small. Return the required size. */ + params.certs_len = sev->snp_certs->len; + + if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, + sizeof(params))) + rc = -EFAULT; + else + rc = -EINVAL; /* May be ENOSPC? */ + } else { + if (copy_to_user((void __user *)(uintptr_t)params.certs_uaddr, + snp_certs->data, snp_certs->len)) + rc = -EFAULT; + } + + sev_snp_certs_put(snp_certs); + + return rc; +} + +static void snp_replace_certs(struct kvm_sev_info *sev, struct sev_snp_certs *snp_certs) +{ + sev_snp_certs_put(sev->snp_certs); + sev->snp_certs = snp_certs; +} + +static int snp_set_instance_certs(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + unsigned long length = SEV_FW_BLOB_MAX_SIZE; + struct kvm_sev_snp_set_certs params; + struct sev_snp_certs *snp_certs; + void *to_certs; + 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; + + if (params.certs_len > SEV_FW_BLOB_MAX_SIZE) + return -EINVAL; + + /* + * Setting a length of 0 is the same as "uninstalling" instance- + * specific certificates. + */ + if (params.certs_len == 0) { + snp_replace_certs(sev, NULL); + return 0; + } + + /* Page-align the length */ + length = ALIGN(params.certs_len, PAGE_SIZE); + + to_certs = kmalloc(length, GFP_KERNEL | __GFP_ZERO); + if (!to_certs) + return -ENOMEM; + + if (copy_from_user(to_certs, + (void __user *)(uintptr_t)params.certs_uaddr, + params.certs_len)) { + ret = -EFAULT; + goto error_exit; + } + + snp_certs = sev_snp_certs_new(to_certs, length); + if (!snp_certs) { + ret = -ENOMEM; + goto error_exit; + } + + snp_replace_certs(sev, snp_certs); + + return 0; +error_exit: + kfree(to_certs); + return ret; +} + int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -2376,6 +2483,12 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) case KVM_SEV_SNP_LAUNCH_FINISH: r = snp_launch_finish(kvm, &sev_cmd); break; + case KVM_SEV_SNP_GET_CERTS: + r = snp_get_instance_certs(kvm, &sev_cmd); + break; + case KVM_SEV_SNP_SET_CERTS: + r = snp_set_instance_certs(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; @@ -2591,6 +2704,8 @@ static int snp_decommission_context(struct kvm *kvm) snp_free_firmware_page(sev->snp_context); sev->snp_context = NULL; + sev_snp_certs_put(sev->snp_certs); + return 0; } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 0d4c29a4300a..72be0c440b16 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -95,6 +95,7 @@ struct kvm_sev_info { u64 snp_init_flags; void *snp_context; /* SNP guest context page */ u64 sev_features; /* Features set at VMSA creation */ + struct sev_snp_certs *snp_certs; }; struct kvm_svm { diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 2191d8b5423a..7b65dd5808a1 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -22,7 +22,7 @@ #define __psp_pa(x) __pa(x) #endif -#define SEV_FW_BLOB_MAX_SIZE 0x4000 /* 16KB */ +#define SEV_FW_BLOB_MAX_SIZE 0x5000 /* 20KB */ struct sev_snp_certs { void *data; diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 175b958f103f..fa1c300303d6 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1937,6 +1937,8 @@ enum sev_cmd_id { KVM_SEV_SNP_LAUNCH_START, KVM_SEV_SNP_LAUNCH_UPDATE, KVM_SEV_SNP_LAUNCH_FINISH, + KVM_SEV_SNP_GET_CERTS, + KVM_SEV_SNP_SET_CERTS, KVM_SEV_NR_MAX, }; @@ -2084,6 +2086,16 @@ struct kvm_sev_snp_launch_finish { __u8 pad[6]; }; +struct kvm_sev_snp_get_certs { + __u64 certs_uaddr; + __u64 certs_len; +}; + +struct kvm_sev_snp_set_certs { + __u64 certs_uaddr; + __u64 certs_len; +}; + #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 Mon Jun 12 04:25:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275577 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 74ABDC7EE2E for ; Mon, 12 Jun 2023 04:58:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233433AbjFLE6M (ORCPT ); Mon, 12 Jun 2023 00:58:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235767AbjFLE5x (ORCPT ); Mon, 12 Jun 2023 00:57:53 -0400 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2064.outbound.protection.outlook.com [40.107.100.64]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C30F3C1B; Sun, 11 Jun 2023 21:56:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=g5ybTzg6orqF1L1+74qH7RXySg/JqLZ9uQrEzI2egNB/A7BpiUKI0oEZf2QDXD7Go0fZjWlI/FBTn7CFeVixVZH/VzBa5trRgchB6fsGaio8BFGgH0wpU90iFN5xCv2Hw2YViAM/E1JpiT4ECD0cSugKnlNNoITQ4y0iM1nxleo/1Y8KY4Npzk2SQ3yA7ghQOne0HcVvFOp5mzEwAxiOYThFcOmgo1WUsvUAqC8INMtQ5jOp8UBkPv2a8PHiJuoJW62b+zJHJLjbugZjzEKLKEtEui0kwPuPYJYwOXVjrug7WLFIrkhBC70aCW440P/9bJf71FDOyUK2jGNG2oK4Ag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=MOjQ9OR6FXBKvOm3ej71JfND9jQgatXW3QKky35AJQE=; b=QmnmY17XM0+Iv+D5RYndHp9UL7cJjFK0wGdcSuNP+qrtoepM/NZ/n5uA8FqMBRJcRg8IgCjaKfNBo+fNjFLtMxBy8VxMfNK4owVsAQ6o1HE+9GBQrm5JfD2rQX+PPX4Cw+J7Usi/5VDkU1E9VlnOsLXYIGlVzvg4gjiiR0hTlI4cL+qaG9qM3YXGfUGak/X15h111DqhKweXSf7kfO9OjN9jCW65GWkxKHhFpO44pblsK+K8EaJcuvAqXwlIvC3ekRRTwuwLLuYOVsVwClzJHCkboBEYj+td53NFb87hm/tDABs7C/zSqAwEbWRVcFVmdK3xrPuOwFyKxrAxKfQYPQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=MOjQ9OR6FXBKvOm3ej71JfND9jQgatXW3QKky35AJQE=; b=LLGvTi+qj7M3SOC8/I9lZ1zOVzIAWlwMggN7gZ3F1UesWm9Ub8njHdEMcxvj0TXJwz4zolVaoniyScV5G1sS3K7rxF+LMt95yeRMdnhfc+uBHa0PVHhtIAptUEj9P1q1Yy4p/n/X1rV/5jjABHUv3icHmZcSPsiRg54dkhZrkbg= Received: from CYXPR03CA0092.namprd03.prod.outlook.com (2603:10b6:930:d3::18) by DM4PR12MB6040.namprd12.prod.outlook.com (2603:10b6:8:af::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.33; Mon, 12 Jun 2023 04:56:45 +0000 Received: from CY4PEPF0000E9D6.namprd05.prod.outlook.com (2603:10b6:930:d3:cafe::1d) by CYXPR03CA0092.outlook.office365.com (2603:10b6:930:d3::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:56:45 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9D6.mail.protection.outlook.com (10.167.241.80) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:56:45 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:56:14 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Alexey Kardashevskiy Subject: [PATCH RFC v9 50/51] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event Date: Sun, 11 Jun 2023 23:25:58 -0500 Message-ID: <20230612042559.375660-51-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D6:EE_|DM4PR12MB6040:EE_ X-MS-Office365-Filtering-Correlation-Id: 39def5c7-7280-43f6-5017-08db6b016c08 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 55sn1+nspxoKvDHHysY6nbWoCVhkRUsJLiUDe7i9mZ8MHZHGw0i2IE17i+h2XTXI2Cd9mJtz/K1UZvBp7NR2EdGfbZqJ7Z19a/raelWFguWJbJGJ3B1VYSc2saWjlQtoL8zxziX5i+4t7PE7fB6MBeLtJCzuz9TMTjBBkcyJYaEX+ZKoVXvAFM0W1cddWWjQd1Q8yRD3SSptLaxH7nsx3yu5poLcfOOtqFz2M3lPdmW16v+XgfzXa16R1L3iaQbWeR/rzhuW/+2z2WfD+QtnRnjoFq+zVHAkGozZqPG5AQuSCaAn23zh2oE/iI+I5z0EOoQp31z/Svtg5imwETInzOGAmg/uaMNypBTyw7mI3uVnUgqkwuT3F/UzrxK43vbt3HOayFuZTwqlfClTl/SAbzC3bpoFTou86uOowBydU+QOWVBSJNmbCM2v5tG5JUxNuHabtgMgWIY6OkWNHQv9zm7gjadZ2B1dHveiPl4zLbXh6BWEsheWk2Y3us6iNoJiKDqfLhhakdTH2f3x7KW1tgilam1+xGaMFRmzey9wHeZeU1wM58kdWiHlBhhYBR/NV/Vlk6jfRiuzkdnU9Tu0XJb5C6Pk/x0HAEHRmFBdXZ5v+xHGqAmZYA6cuVDqoT41llq3QnLcxXVxy/tZlyKdmRPRuUgmL8ftBfKFO5gLXYwS4lPKgVyVgJhgUq1YLKjU5WrEc/ADxF3VRwrKG896VtC1cdCfM2CRpdkSu5xU5dSnNNRB9vXQbQtSNFRf0E2i6cjMeL6oHUwrEQdMarrx/w== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(136003)(346002)(376002)(39860400002)(396003)(451199021)(36840700001)(40470700004)(46966006)(186003)(8676002)(8936002)(54906003)(478600001)(6666004)(5660300002)(41300700001)(316002)(16526019)(26005)(44832011)(1076003)(7416002)(7406005)(70586007)(70206006)(6916009)(4326008)(2616005)(83380400001)(40460700003)(2906002)(426003)(336012)(47076005)(82740400003)(81166007)(40480700001)(356005)(36860700001)(86362001)(82310400005)(36756003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:56:45.3274 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 39def5c7-7280-43f6-5017-08db6b016c08 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D6.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB6040 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the SNP_GUEST_REQUEST API define in the SEV-SNP firmware specification. The SNP_EXT_GUEST_REQUEST is similar to SNP_GUEST_REQUEST with the difference of an additional certificate blob that can be passed through the SNP_SET_CONFIG ioctl defined in the CCP driver. The CCP driver provides snp_guest_ext_guest_request() that is used by the KVM to get both the report and certificate data at once. Co-developed-by: Alexey Kardashevskiy Signed-off-by: Alexey Kardashevskiy Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra [mdr: ensure FW command failures are indicated to guest] Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 175 +++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 1 + drivers/crypto/ccp/sev-dev.c | 15 +++ include/linux/psp-sev.h | 1 + 4 files changed, 192 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index bdf32aa971d8..9f7defce1988 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -328,6 +328,8 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) ret = verify_snp_init_flags(kvm, argp); if (ret) goto e_free; + + mutex_init(&sev->guest_req_lock); } ret = sev_platform_init(&argp->error); @@ -2321,8 +2323,10 @@ static int snp_get_instance_certs(struct kvm *kvm, struct kvm_sev_cmd *argp) static void snp_replace_certs(struct kvm_sev_info *sev, struct sev_snp_certs *snp_certs) { + mutex_lock(&sev->guest_req_lock); sev_snp_certs_put(sev->snp_certs); sev->snp_certs = snp_certs; + mutex_unlock(&sev->guest_req_lock); } static int snp_set_instance_certs(struct kvm *kvm, struct kvm_sev_cmd *argp) @@ -3171,6 +3175,8 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) case SVM_VMGEXIT_UNSUPPORTED_EVENT: case SVM_VMGEXIT_HV_FEATURES: case SVM_VMGEXIT_PSC: + case SVM_VMGEXIT_GUEST_REQUEST: + case SVM_VMGEXIT_EXT_GUEST_REQUEST: break; default: reason = GHCB_ERR_INVALID_EVENT; @@ -3604,6 +3610,163 @@ static int sev_snp_ap_creation(struct vcpu_svm *svm) return ret; } +static unsigned long snp_setup_guest_buf(struct vcpu_svm *svm, + struct sev_data_snp_guest_request *data, + gpa_t req_gpa, gpa_t resp_gpa) +{ + struct kvm_vcpu *vcpu = &svm->vcpu; + struct kvm *kvm = vcpu->kvm; + kvm_pfn_t req_pfn, resp_pfn; + struct kvm_sev_info *sev; + + sev = &to_kvm_svm(kvm)->sev_info; + + if (!IS_ALIGNED(req_gpa, PAGE_SIZE) || !IS_ALIGNED(resp_gpa, PAGE_SIZE)) + return SEV_RET_INVALID_PARAM; + + req_pfn = gfn_to_pfn(kvm, gpa_to_gfn(req_gpa)); + if (is_error_noslot_pfn(req_pfn)) + return SEV_RET_INVALID_ADDRESS; + + resp_pfn = gfn_to_pfn(kvm, gpa_to_gfn(resp_gpa)); + if (is_error_noslot_pfn(resp_pfn)) + return SEV_RET_INVALID_ADDRESS; + + if (rmp_make_private(resp_pfn, 0, PG_LEVEL_4K, 0, true)) + return SEV_RET_INVALID_ADDRESS; + + data->gctx_paddr = __psp_pa(sev->snp_context); + data->req_paddr = __sme_set(req_pfn << PAGE_SHIFT); + data->res_paddr = __sme_set(resp_pfn << PAGE_SHIFT); + + return 0; +} + +static void snp_cleanup_guest_buf(struct sev_data_snp_guest_request *data, unsigned long *rc) +{ + u64 pfn = __sme_clr(data->res_paddr) >> PAGE_SHIFT; + int ret; + + ret = snp_page_reclaim(pfn); + if (ret) + *rc = SEV_RET_INVALID_ADDRESS; + + ret = rmp_make_shared(pfn, PG_LEVEL_4K); + if (ret) + *rc = SEV_RET_INVALID_ADDRESS; +} + +static void snp_handle_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_gpa) +{ + struct sev_data_snp_guest_request data = {0}; + struct kvm_vcpu *vcpu = &svm->vcpu; + struct kvm *kvm = vcpu->kvm; + struct kvm_sev_info *sev; + unsigned long rc; + int err; + + if (!sev_snp_guest(vcpu->kvm)) { + rc = SEV_RET_INVALID_GUEST; + goto e_fail; + } + + sev = &to_kvm_svm(kvm)->sev_info; + + mutex_lock(&sev->guest_req_lock); + + rc = snp_setup_guest_buf(svm, &data, req_gpa, resp_gpa); + if (rc) + goto unlock; + + rc = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &data, &err); + if (rc) + /* Ensure an error value is returned to guest. */ + rc = err ? err : SEV_RET_INVALID_ADDRESS; + + snp_cleanup_guest_buf(&data, &rc); + +unlock: + mutex_unlock(&sev->guest_req_lock); + +e_fail: + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, rc); +} + +static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_gpa) +{ + struct sev_data_snp_guest_request req = {0}; + struct sev_snp_certs *snp_certs = NULL; + struct kvm_vcpu *vcpu = &svm->vcpu; + struct kvm *kvm = vcpu->kvm; + unsigned long data_npages; + struct kvm_sev_info *sev; + unsigned long exitcode = 0; + u64 data_gpa; + int err, rc; + + if (!sev_snp_guest(vcpu->kvm)) { + rc = SEV_RET_INVALID_GUEST; + goto e_fail; + } + + sev = &to_kvm_svm(kvm)->sev_info; + + data_gpa = vcpu->arch.regs[VCPU_REGS_RAX]; + data_npages = vcpu->arch.regs[VCPU_REGS_RBX]; + + if (!IS_ALIGNED(data_gpa, PAGE_SIZE)) { + exitcode = SEV_RET_INVALID_ADDRESS; + goto e_fail; + } + + mutex_lock(&sev->guest_req_lock); + + rc = snp_setup_guest_buf(svm, &req, req_gpa, resp_gpa); + if (rc) + goto unlock; + + /* + * If a VMM-specific certificate blob hasn't been provided, grab the + * host-wide one. + */ + snp_certs = sev_snp_certs_get(sev->snp_certs); + if (!snp_certs) + snp_certs = sev_snp_global_certs_get(); + + /* + * If there is a host-wide or VMM-specific certificate blob available, + * make sure the guest has allocated enough space to store it. + * Otherwise, inform the guest how much space is needed. + */ + if (snp_certs && (data_npages << PAGE_SHIFT) < snp_certs->len) { + vcpu->arch.regs[VCPU_REGS_RBX] = snp_certs->len >> PAGE_SHIFT; + exitcode = SNP_GUEST_REQ_INVALID_LEN; + goto cleanup; + } + + rc = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &req, &err); + if (rc) { + /* pass the firmware error code */ + exitcode = err; + goto cleanup; + } + + /* Copy the certificate blob in the guest memory */ + if (sev->snp_certs && + kvm_write_guest(kvm, data_gpa, sev->snp_certs->data, sev->snp_certs->len)) + exitcode = SEV_RET_INVALID_ADDRESS; + +cleanup: + sev_snp_certs_put(snp_certs); + snp_cleanup_guest_buf(&req, &exitcode); + +unlock: + mutex_unlock(&sev->guest_req_lock); + +e_fail: + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, exitcode); +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3863,6 +4026,18 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) SVM_EVTINJ_VALID); } + ret = 1; + break; + case SVM_VMGEXIT_GUEST_REQUEST: + snp_handle_guest_request(svm, control->exit_info_1, control->exit_info_2); + + ret = 1; + break; + case SVM_VMGEXIT_EXT_GUEST_REQUEST: + snp_handle_ext_guest_request(svm, + control->exit_info_1, + control->exit_info_2); + ret = 1; break; case SVM_VMGEXIT_UNSUPPORTED_EVENT: diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 72be0c440b16..31cd8b3e6877 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -96,6 +96,7 @@ struct kvm_sev_info { void *snp_context; /* SNP guest context page */ u64 sev_features; /* Features set at VMSA creation */ struct sev_snp_certs *snp_certs; + struct mutex guest_req_lock; /* Lock for guest request handling */ }; struct kvm_svm { diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 175c24163ba0..096ba15d0740 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -2109,6 +2109,21 @@ void sev_snp_certs_put(struct sev_snp_certs *certs) } EXPORT_SYMBOL_GPL(sev_snp_certs_put); +struct sev_snp_certs *sev_snp_global_certs_get(void) +{ + struct sev_device *sev; + + if (!psp_master || !psp_master->sev_data) + return NULL; + + sev = psp_master->sev_data; + if (!sev->snp_initialized) + return NULL; + + return sev_snp_certs_get(sev->snp_certs); +} +EXPORT_SYMBOL_GPL(sev_snp_global_certs_get); + 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 7b65dd5808a1..1235eb3110cb 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -33,6 +33,7 @@ struct sev_snp_certs { struct sev_snp_certs *sev_snp_certs_new(void *data, u32 len); struct sev_snp_certs *sev_snp_certs_get(struct sev_snp_certs *certs); void sev_snp_certs_put(struct sev_snp_certs *certs); +struct sev_snp_certs *sev_snp_global_certs_get(void); /** * SEV platform state From patchwork Mon Jun 12 04:25:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13275579 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5A3FFC7EE37 for ; Mon, 12 Jun 2023 04:58:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235628AbjFLE6r (ORCPT ); Mon, 12 Jun 2023 00:58:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235797AbjFLE6N (ORCPT ); Mon, 12 Jun 2023 00:58:13 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2067.outbound.protection.outlook.com [40.107.95.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA7832D6B; Sun, 11 Jun 2023 21:57:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=h7QwnoHsLQAel88XQqN7XTNHE5m4nCWOLANeKr5Qi+aGYKRz5NJNLVdA2YrW2YnWlyb9Vg3glh5KCiCvpJBoRYL/2z5IknPPhSnhiJE+ClY7h+VR3zIXKpOd/VvX+CZTAdciMwTMDFFaxEKn6URJ8q/HBky1KptBKrxevUrP1cbIN8Y9GpsciH403CfvgacW9xCkt6nHpqmXmlefmv74blUCRDJr7X0spG4c9vVaTpl9z6a0cXYL9/npk+ovLE73w+9rplzXwNbx2cMfT7HNmXD3F34IG757DsjqBa3R+R0ztOKiZ9IhXmHVuwXOCZNuZKA4xsM7r0A7AjLHN/4tCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=RjrtDRnX4hUbBvCvwo1i1y/QPZ+dHBYmNvNtfJMhRak=; b=EkDCqPgU43oi3TPjnEN5S+ijm64HMCu/0+rYqzBFReN+N5+Nc31Ff7jy57+Gx2CHolXAfo/yTAq//S35Tn0/5kHDhBItxYZ8OVO44tmSI8Vdjj0sOqcmMQy4KUIxvwPCckTD4uqKFfWTOg7Qo5qLLRzjpL10iCupQfPotJYXrR8TCFBuN/8guNOUrlRq8PJqT90Ly+Em4doy38VOEWk3binJfDKeIceeLLfzv9wZGx4mOGbIr38AW1LLEgixZ3MF80KrHDdzvqOfCBEB7FAbXqe0HY3Na1TALXBBbptiotmjXHw+1z1Q9M5VxSKNILLNMm0HEdITngpQkipgxJAw/w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=RjrtDRnX4hUbBvCvwo1i1y/QPZ+dHBYmNvNtfJMhRak=; b=md+6pjLMK2ykrkM0a/b+89M7rIa2mvsvbVjUWPp4T4lkWySrLx3VYg636nFoMpZFdH2H9xE2PLvnMI2d2rz3SpEpZiwboB9yyPzxF6kXHNg9PYhBzICl1PgQFh9HTx6hpnZtMwWc1hJbhkonz+tl6hH+1V4NMOpMGl6DwJvkxX8= Received: from CYXPR03CA0077.namprd03.prod.outlook.com (2603:10b6:930:d3::19) by CH0PR12MB5249.namprd12.prod.outlook.com (2603:10b6:610:d0::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.42; Mon, 12 Jun 2023 04:57:10 +0000 Received: from CY4PEPF0000E9D6.namprd05.prod.outlook.com (2603:10b6:930:d3:cafe::d5) by CYXPR03CA0077.outlook.office365.com (2603:10b6:930:d3::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.33 via Frontend Transport; Mon, 12 Jun 2023 04:57:09 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9D6.mail.protection.outlook.com (10.167.241.80) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6500.22 via Frontend Transport; Mon, 12 Jun 2023 04:57:09 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 11 Jun 2023 23:57:07 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v9 51/51] crypto: ccp: Add debug support for decrypting pages Date: Sun, 11 Jun 2023 23:25:59 -0500 Message-ID: <20230612042559.375660-52-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612042559.375660-1-michael.roth@amd.com> References: <20230612042559.375660-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D6:EE_|CH0PR12MB5249:EE_ X-MS-Office365-Filtering-Correlation-Id: 499dbd1a-db19-494a-8345-08db6b017a88 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 71t+1axCyvSl/V6S59kONElavhM4jLvegT9GfJXF519MRl0ZhzWRf0RmHqL0U8U4jv/iq52DGbgm13fWzwMeLlhu3mB7SikkcGkuUhUm6l0pDcMcY0eR7hoys9oP3+hHbNtFRJKT63oDbzmDcqq32Qq2UaYQmxx1D4TWtChYzkmqK5A9bSWxQ1r/t7eePnSryFBcNiPxKX3bgP9pb0A0T7+3Gmnveqau5X+QRvhVyESyhzeIRK9kS87j8fyFGgxRKGOj0t+gEpDQ/sZQPxiRiM0xsx0KPSADx+fu+44Yir5r/W6Kot34PCo4TPG733wY3DIFF5P7YlvKiPiu5WyCRumeGXaU5mUHCSqZszyhdVb8RTICGh8le46ezuUebuTom1Xpo9tpDEsTMh9VmM3Rs23jdtXT/NT3QedmaviPX9MMGyl0l6TiDoUIAvAZYw8yYp1cpyD6HC7E5mV8Rul9SjcQneujZ7VwD5MHu7fCvI+0pRD40060pbBvgpOOFTD6ZNPLQKPdHBwpedaED+5+JAzSy8AKLFOLuxL9X1IWDlDWY3c2XR7ChDnMSCZXgOpJ0HkWooKevUmPFiefWi+CZSFTpS9QeyJLwrmrbp76nLwr6XShajkaNUiVpv+RLHD5ixEPFGbJZ9jlCJJSfXGfu6jggqWwAvftDe2pxUO+vzTjzsXPuaP2nNlVgXmeuoNtpHLEcd7C2VlpnvGeCzA0ZaUkGEvJbLsysPwUzbtcOVqh+wULmBvm2PIcJRLvSmicr4UTVjP0yQNS1ijQRcBGDnYo1UPZwr92wlHvbZRk0bI= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(396003)(39860400002)(136003)(346002)(376002)(451199021)(36840700001)(40470700004)(46966006)(2616005)(36756003)(2906002)(7406005)(82310400005)(86362001)(44832011)(7416002)(40480700001)(6666004)(47076005)(16526019)(36860700001)(186003)(83380400001)(40460700003)(5660300002)(336012)(26005)(426003)(1076003)(82740400003)(356005)(81166007)(478600001)(54906003)(70586007)(4326008)(70206006)(6916009)(41300700001)(316002)(8676002)(8936002)(36900700001)(134885004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jun 2023 04:57:09.6400 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 499dbd1a-db19-494a-8345-08db6b017a88 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D6.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5249 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Brijesh Singh Add support to decrypt guest encrypted memory. These API interfaces can be used for example to dump VMCBs on SNP guest exit. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra [mdr: minor commit fixups] Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 32 ++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 19 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 096ba15d0740..3c8cd2d20016 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -2061,6 +2061,38 @@ int sev_guest_df_flush(int *error) } EXPORT_SYMBOL_GPL(sev_guest_df_flush); +int snp_guest_dbg_decrypt_page(u64 gctx_pfn, u64 src_pfn, u64 dst_pfn, int *error) +{ + struct sev_data_snp_dbg data = {0}; + struct sev_device *sev; + int ret; + + if (!psp_master || !psp_master->sev_data) + return -ENODEV; + + sev = psp_master->sev_data; + + if (!sev->snp_initialized) + return -EINVAL; + + data.gctx_paddr = sme_me_mask | (gctx_pfn << PAGE_SHIFT); + data.src_addr = sme_me_mask | (src_pfn << PAGE_SHIFT); + data.dst_addr = sme_me_mask | (dst_pfn << PAGE_SHIFT); + + /* The destination page must be in the firmware state. */ + if (rmp_mark_pages_firmware(data.dst_addr, 1, false)) + return -EIO; + + ret = sev_do_cmd(SEV_CMD_SNP_DBG_DECRYPT, &data, error); + + /* Restore the page state */ + if (snp_reclaim_pages(data.dst_addr, 1, false)) + ret = -EIO; + + return ret; +} +EXPORT_SYMBOL_GPL(snp_guest_dbg_decrypt_page); + static void sev_snp_certs_release(struct kref *kref) { struct sev_snp_certs *certs = container_of(kref, struct sev_snp_certs, kref); diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 1235eb3110cb..55f6dfc2580d 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -916,6 +916,20 @@ int sev_guest_decommission(struct sev_data_decommission *data, int *error); */ int sev_do_cmd(int cmd, void *data, int *psp_ret); +/** + * snp_guest_dbg_decrypt_page - 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 snp_guest_dbg_decrypt_page(u64 gctx_pfn, u64 src_pfn, u64 dst_pfn, int *error); + void *psp_copy_user_blob(u64 uaddr, u32 len); void *snp_alloc_firmware_page(gfp_t mask); void snp_free_firmware_page(void *addr); @@ -946,6 +960,11 @@ 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 snp_guest_dbg_decrypt_page(u64 gctx_pfn, u64 src_pfn, u64 dst_pfn, int *error) +{ + return -ENODEV; +} + static inline void *snp_alloc_firmware_page(gfp_t mask) { return NULL;