From patchwork Wed May 1 08:51:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650311 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 39655C4345F for ; Wed, 1 May 2024 09:02:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C6B4A6B0098; Wed, 1 May 2024 05:02:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C1B606B009A; Wed, 1 May 2024 05:02:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A6DFD6B0099; Wed, 1 May 2024 05:02:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 7E5DB6B0085 for ; Wed, 1 May 2024 05:02:52 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 5955F1C0E95 for ; Wed, 1 May 2024 09:02:28 +0000 (UTC) X-FDA: 82069235976.27.E309571 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2050.outbound.protection.outlook.com [40.107.93.50]) by imf15.hostedemail.com (Postfix) with ESMTP id 6FB90A0004 for ; Wed, 1 May 2024 09:02:24 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=uxy8Olep; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf15.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.50 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1714554144; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=bK20vcWucIreIaSDh3sNBwKjRQE7GxXQ728dKk/YFZA=; b=0Ii4GtaTN08z2xmZ+0TPCzWWlZ8xj7bnP2LSBfYK6EP/FtDc1VaieO1v5U0jvn1UUW2sij XeVpjoKUqAWAT3SgaMpz47kO79I7RihX6w9O+xXuDnvsKeSs2WjFUgNxoqmsFjz1MG8Tzd QE2gqiESJQqyjGCruwpD5ASi/RKwTT0= ARC-Authentication-Results: i=2; imf15.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=uxy8Olep; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf15.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.50 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1714554144; a=rsa-sha256; cv=pass; b=EVjjtEn9wPPURrxUS/IfY8ym/ECHAGAsqLvEtUjw5gusbgj/p0ARID1jRfwe9ATj2j5NuU 1aMdeP0OI7tqCCdI3CKxAtFoplYlt80MQKbvALkX7cnztZ3xC2QzEH+QHbyRRPFkIvdEgU Tnafh+Recsj1FGbgWOIVGNAdD+yfWUo= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=G4ryxOPEzp3+sS5EGNwVMD5wDnXIz1Rm3MsBAEM/7gj/MqeybA9fT+EgCl/6+2quVjNQEh739Wx0aGEM6xchcTgJorzo6bVY/oOZswXngVOPkBYdPkcbYJoKZJ+rI7L7xwhOP944VQ91RGSTkD1EDNEEVIXvYhxHaucXjx/DL5Q4r1Ly3WiOydnyxQccVqdVZmZ/sPDqzIfHnyggeexkPFoFt8FzmSqQgEX8NdHBcsFy+Fo7peg3xMqtVA4ylTyNohrSexVPr1DnedGCFXUgvkDjJrQvWQqH+6KUaVn3WrZQMkn/WISrLJTo3WPCbeRgIJiYB+jLbpavzNTkmskh3Q== 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=bK20vcWucIreIaSDh3sNBwKjRQE7GxXQ728dKk/YFZA=; b=D2bz4TxCz0J2PktU/C7cWimAfVsdH8Mcdl0HFVO5qxQ7BFC63x7DT5APGRqE8FYPdh8i7RQd0QNi0LlVND6/Kf2xw5pwyexAgf6e9uEq+XbrFoaYjXhQ+V/+59ZazgTdWveOwIOV7VMIdAWqu88YS38LkzHzptUMBn6qa1tIUvaufzUs/KIPztENL/S6cug93lJITtg69mv/+ivGGoeqDvxdG0+GI/FZCjfvdSLTS2daux4BkCeuGSJNBGUVyQUgGeDaH/2Vcz/GE4fIQKyNjA/QTSmzQiTb00IyFyOTjdxs3zt9+HcvBZzq1dAxnl7jJDlJK0GMbjcH4ncObq3OQA== 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 (0) 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=bK20vcWucIreIaSDh3sNBwKjRQE7GxXQ728dKk/YFZA=; b=uxy8OleptnA5XhKkoZXWbEBz+BDGiDOt0sME/G37M2DsU/vKG4/R93IoNMt0GBssh7rkAF3W23dsp6fErLwPRNNINoGBTKnZLYrYZJSFkHx2v0yJJm3qJ+vlctLWHHaXadVVBkTp1PgX89kjdgu7DArvBPqELHLlYJejdMpztLE= Received: from BY3PR10CA0021.namprd10.prod.outlook.com (2603:10b6:a03:255::26) by PH7PR12MB7794.namprd12.prod.outlook.com (2603:10b6:510:276::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.34; Wed, 1 May 2024 09:02:15 +0000 Received: from CO1PEPF000044F3.namprd05.prod.outlook.com (2603:10b6:a03:255:cafe::a1) by BY3PR10CA0021.outlook.office365.com (2603:10b6:a03:255::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.28 via Frontend Transport; Wed, 1 May 2024 09:02:15 +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 CO1PEPF000044F3.mail.protection.outlook.com (10.167.241.73) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7544.18 via Frontend Transport; Wed, 1 May 2024 09:02:15 +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.35; Wed, 1 May 2024 04:02:14 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH v15 09/20] KVM: SEV: Add support to handle MSR based Page State Change VMGEXIT Date: Wed, 1 May 2024 03:51:59 -0500 Message-ID: <20240501085210.2213060-10-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240501085210.2213060-1-michael.roth@amd.com> References: <20240501085210.2213060-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: CO1PEPF000044F3:EE_|PH7PR12MB7794:EE_ X-MS-Office365-Filtering-Correlation-Id: 65ed4a01-1b45-4bfc-24f2-08dc69bd65af X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|82310400014|1800799015|376005|7416005|36860700004; X-Microsoft-Antispam-Message-Info: ls4uveIgbbjXe+xVN9H8hYDP49MkXr8mngw4l+xatvlPXX0Hgg3W3bfbVhNQ/kA5uo62F6gk/55N6bHbw3VnUwO0e0KyNtvRJXyN3mvi4jqdixMMwX/Yopf1tk9ZhTKokWSXsDH4tuYtylRLbFzgSqM3oOr8PFYSDVUOo3bO0IP67aaNrKFnT7BujQOaZAywsRcFdOEyixzqyw5SfQP30dtke0+prINgOi9xHRZqFy5oTSNmrTfoVtX+OenaIz6vy7rI3x0SRdCT6JKWIpbNOobAiiUU38LhdcFTYN3i9o5uB3OtJNinTYHnXtKRAbvJWVkiJPoVusMgiqsLWc4Y9clLjMF98FSvdyhq+wjrwT6UzBNBrbbO9mQMt3C2i6Np2tpv6Dv02wL4qD6zXXejxe5QI/25dUaxKesl9aizP9cIhu8cGvD9HbrpfAKVWW5Kar7OA2NB4swOouC6ToRjh5PFEX1bfE6OFa54aSJsIH1HWvIqZd41et0+Ftq1tO+w4AT5FxlBffENg3QpUjmBf6HjC12BvbQlIWy+biTn1ILKdNG/dAVpwqCaBN9Di7bH16fMACiZxqES0U3QF0sElVAFvl6rfhVuV8CyaC6/nOxMDjlpU7G4wvfN6FH0pq7Vp+SquxKNFA6yR16KeC0tavUTNv+dxDsZPPJ0IyYMI4tLQr8heAyLL26q6MqYQtyqNMqEk2+LAKycFomeOTGfFUJyCGZ7pFL9OEnoOFsHhkI59x25ATQNGFVILbsQPpMjPZR8Zw+X1oO7oWvjYrwJbFTaO8sWZqTtaI20iehkB7hAYMv9+nY5eOa2xRs0ZkLpP3Dqrv+HFs9JTQIGCBXBcHFTfNkxzB/QfcasfwjqoHylWWx0oRKq+8sawG/9hc8vejvMaZqk3nDvC3OyucJA8BjKa0BPnBw7CyXrQqAsJC7+E4DjHvxMOO0Gn8w34m2xSg+CY6kyHphOdcgDzAFxisjxLlB5XZzXGWn+PGyY5Lc15Ude2pn5jEqWs/dN41v8I+fwc6IYFSk3s9LTY6bA0QBBWJRhWdLy/t/7lJSozQQ/iJTN0DjmlIxUeYOCCxvW3Rn6hvBgv0dO1u6ZSxLkczHrofkvmqs9OnMeATKnrzrtIDgouA0B0URiSDfarcgN7d8goIslL53REvpc9bhnWWs4sed1Ay0SpKtSIlup2iZalHptBIY6YgCzvchm3Io/cXNDFLYYWJnZmo4xxDSz0DAo0DFYKK8KwkFmv+vYpHu+M32Y8RW/vsvZjGZwOW0+sDURmcumULYSs1R+4T4WAMa5s8qinu/qF9uiPs9VdPzRVyhkrxRwPCFClU2UaJKM19YIwHN28c2sVfRoQNQH5Z37xzYfjM9tOxuGPW1r6ubgg+lMyybeN3kWGE7WBTaG 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:(13230031)(82310400014)(1800799015)(376005)(7416005)(36860700004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 May 2024 09:02:15.3761 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 65ed4a01-1b45-4bfc-24f2-08dc69bd65af 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: CO1PEPF000044F3.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7794 X-Rspamd-Server: rspam01 X-Stat-Signature: zncc3f1nif6iwoc3j8mdazy5ebpwbycr X-Rspam-User: X-Rspamd-Queue-Id: 6FB90A0004 X-HE-Tag: 1714554144-631526 X-HE-Meta: U2FsdGVkX18t55meUxjwYrn5fMD//bHKqza7HN27Cb8oc31poqemz1J00KBJlRiXWA15DwF/wDz7O1EBNm2wuLPjPUxUwA+Ht7Xned8qEwnrQ4nBSMfGUNXSQSkmxI9Z46vmpIu9H5SBypYefCuOf539pfGgQ5bTKw8rGm15CfZn8OYv9pGtBr+AfUmY+4UtHsmsJOzfHb4cIFw1KaXScck2tnrSkpgYVz5BXyTttrEVy/mFuxBLxXVc1mUCw5sdAXHsGPwo/Zs7paY54KWitF57MoyaGrUAYaSLr9qaGxhifz0OD10KXYNHLPgqO4Bu+BRyM0wbEBYfRb+fsau/e39RXACRvj9eFVRKKHG+xYub5gb6vVbLUztF2lGhlsfSYLDEuLd6WAaqY0aLCTh47ou6LrPSg1Xzw6lbTOnxVojiLM9xe464oT84IbInp7JO4jwyQPUM3HHplvFepc65X/rd095nMI87i8i9qhBSUp3jMFc95nrih8RfKROSvE+/+24L3sTqFf90q0UurMObJ0mWGFPIS0lrcjR2fMTGaooAHi0b4DT5wrjmWQctzEFh0v4PAJg35a6fSpxtf9gDeNNwDGiR8xYyR2mhI5B0ZgK1WT4WW0O++1tNWTFlXMwnnaRaLcxV1vi+T3+suWfEjKcy0YnNmaeC3CbN4jsgKSHAwKdYD2zFpB8nxu+9KfMjzqiEwGLwWoLtK9KOYsJx6VDN/uvPFsPbIweSm5CLeBp0mRSCSxsT6fZPVQYzid3eXZv+mYD/LdhtmUQNCT9J/0GkEn5fJFYNEv33Oiz7l4uelHinFBoerpHulJPxlOokAkRgOJ5NSrL7g+tYW86DP1veF0asGQPtyu6Q7LuMs8yRNxAIXYTiC0PIfzRn+dwmS2Tx+zVjhJvsTX10CT9hwaZm+S88j8XgkkHsW4C7jBTyAKWpHzBtaVdhJGk3ZkXdwFJbNDyQoiytJ1o9R0U 8hWut8yF fpCup+jWm9cR0WfIbp4jspQ5zlQXb+ommqNXGV6elMOS/4Ga6K3PwRpj5sG6KcTaNRKvpPzWHqX8p0UtA8EZT1FoQGp9umFGRisdAg5H5/gV1M4HvwoJZi9iJedkT5VN6pSbky6pRjp5siAtzYNsNj9KlY54ROZ3SuKChI0hRezx5N/9jwI3eebjUkIh3roWcX5r2TCzWRS8bYm2Zlh28hS5DvGR2ZaOlb7YAtdWDBZvXk2MjjVY0nzpupNagYoYjtoXxwvLdnoqiLpBxxITeeWuIbYa7btrB8/5QRPxsfkzSFsoqOjpMtgHXUWuSmR5J0IU4Tg8SZbSH6xM= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 the 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. Use the existing KVM_HC_MAP_GPA_RANGE hypercall format to deliver these requests to userspace via KVM_EXIT_HYPERCALL. Signed-off-by: Michael Roth Co-developed-by: Brijesh Singh Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- arch/x86/include/asm/sev-common.h | 6 ++++ arch/x86/kvm/svm/sev.c | 48 +++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 1006bfffe07a..6d68db812de1 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -101,11 +101,17 @@ enum psc_op { /* GHCBData[11:0] */ \ GHCB_MSR_PSC_REQ) +#define GHCB_MSR_PSC_REQ_TO_GFN(msr) (((msr) & GENMASK_ULL(51, 12)) >> 12) +#define GHCB_MSR_PSC_REQ_TO_OP(msr) (((msr) & GENMASK_ULL(55, 52)) >> 52) + #define GHCB_MSR_PSC_RESP 0x015 #define GHCB_MSR_PSC_RESP_VAL(val) \ /* GHCBData[63:32] */ \ (((u64)(val) & GENMASK_ULL(63, 32)) >> 32) +/* Set highest bit as a generic error response */ +#define GHCB_MSR_PSC_RESP_ERROR (BIT_ULL(63) | GHCB_MSR_PSC_RESP) + /* GHCB Hypervisor Feature Request/Response */ #define GHCB_MSR_HV_FT_REQ 0x080 #define GHCB_MSR_HV_FT_RESP 0x081 diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index e1ac5af4cb74..720775c9d0b8 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3461,6 +3461,48 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) svm->vmcb->control.ghcb_gpa = value; } +static int snp_complete_psc_msr(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + + if (vcpu->run->hypercall.ret) + set_ghcb_msr(svm, GHCB_MSR_PSC_RESP_ERROR); + else + set_ghcb_msr(svm, GHCB_MSR_PSC_RESP); + + return 1; /* resume guest */ +} + +static int snp_begin_psc_msr(struct vcpu_svm *svm, u64 ghcb_msr) +{ + u64 gpa = gfn_to_gpa(GHCB_MSR_PSC_REQ_TO_GFN(ghcb_msr)); + u8 op = GHCB_MSR_PSC_REQ_TO_OP(ghcb_msr); + struct kvm_vcpu *vcpu = &svm->vcpu; + + if (op != SNP_PAGE_STATE_PRIVATE && op != SNP_PAGE_STATE_SHARED) { + set_ghcb_msr(svm, GHCB_MSR_PSC_RESP_ERROR); + return 1; /* resume guest */ + } + + if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE))) { + set_ghcb_msr(svm, GHCB_MSR_PSC_RESP_ERROR); + return 1; /* resume guest */ + } + + vcpu->run->exit_reason = KVM_EXIT_HYPERCALL; + vcpu->run->hypercall.nr = KVM_HC_MAP_GPA_RANGE; + vcpu->run->hypercall.args[0] = gpa; + vcpu->run->hypercall.args[1] = 1; + vcpu->run->hypercall.args[2] = (op == SNP_PAGE_STATE_PRIVATE) + ? KVM_MAP_GPA_RANGE_ENCRYPTED + : KVM_MAP_GPA_RANGE_DECRYPTED; + vcpu->run->hypercall.args[2] |= KVM_MAP_GPA_RANGE_PAGE_SZ_4K; + + vcpu->arch.complete_userspace_io = snp_complete_psc_msr; + + return 0; /* forward request to userspace */ +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3566,6 +3608,12 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_POS); break; } + case GHCB_MSR_PSC_REQ: + if (!sev_snp_guest(vcpu->kvm)) + goto out_terminate; + + ret = snp_begin_psc_msr(svm, control->ghcb_gpa); + break; case GHCB_MSR_TERM_REQ: { u64 reason_set, reason_code;