From patchwork Thu Apr 18 19:41:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13635307 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 CD024C05023 for ; Thu, 18 Apr 2024 19:44:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 67D066B0085; Thu, 18 Apr 2024 15:44:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 653CA6B0088; Thu, 18 Apr 2024 15:44:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4F4FB6B00A2; Thu, 18 Apr 2024 15:44:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 319806B0085 for ; Thu, 18 Apr 2024 15:44:17 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E602581305 for ; Thu, 18 Apr 2024 19:44:16 +0000 (UTC) X-FDA: 82023678912.14.974DCBC Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2053.outbound.protection.outlook.com [40.107.244.53]) by imf30.hostedemail.com (Postfix) with ESMTP id 0BF848000A for ; Thu, 18 Apr 2024 19:44:13 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=W2NMs6Um; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.53 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1713469454; a=rsa-sha256; cv=pass; b=qvsIxYwIu6BgpfsQbe8/Ka9kTKu81rOKEh+Lp0IeN4EtxD9g3rIbJ1sSVFU+AkJ8WbynsQ bTFx+D/AyATKYBs/RR7fR+wtSwnxabxZTqWQGFKaO80wu1CeeViYvqpP/LFZNyBseo8Ry1 12AnQhnLEB1OTeAB5cyvtY0x+vKAT4Y= ARC-Authentication-Results: i=2; imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=W2NMs6Um; spf=pass (imf30.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.53 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1713469454; 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=c+YMl+OqmHHJKwXYIR17ZTGKqiyBV+wF+b5Kiw15zh4=; b=1nDS3uO7mwWXxUtcaYgsvWWkEqLWOghwsvG9cpGImPN0tXXv0F6rUvb3EzMH4pYmkc02uI amRwL0DSjtwam+G1zL+EsMs0EdFVz4vJsjtSrOCrR7Sce5SQSll4E0vbGfHItqDcP9xqtR gs8SBUP4PtN7/GyeR7rDTCKgLyL5Wzw= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eV5kZykt9jr/mFrNj/tLrPp69XqBMtbEFdZ6Uh405mWq6Zj9otz/zgMKZ0K6BGLAiKjndNa6He+dvcgXu8HTaeXxHed4Ee4JMdapYq2HPkQU96ImvnW7pifsScmVLmL2BzFlJ3IbdpRzWZHg07Nn2sGlsBtzV1ijGnOshNOe+u5zKQ5kMN4dUqzF/zNFehF6+iKiXNjmu6vWtdlVTnuchCTm4S5SsIoMh4zSNCAYNtysgfYlha3hz61VeGz77uC3eTEpkLVFFSz0f5q05zdeibocm6/xjLmRCwQlBLcNs6vvNIpFoPk3u+ttu+eNkDxPP2XV5NlioBVaYXihri365Q== 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=c+YMl+OqmHHJKwXYIR17ZTGKqiyBV+wF+b5Kiw15zh4=; b=cFI5UioEbvUegm9d6BeHQrSEenUacg1pMLYpOqSpbP+oQqbUvA8xOTWQnpx8sIdBMt+FKafoWGIoYbXEpil5QXhwUUtP71SysnWmS1tWJgoYLv5UKFUL85PgK7t38992J1d978vjzuAQCbrERjH9KZLYDQAG51cRSBc+QsYIaB8odZr86sShz/S2l6DvhYRGRIJvI7sm+0BmTp7iQ1+lz+ER/oNCp2wax/ZJV/8+dvaQNQEMc/VoAvGEX1GL8csEpW7+TTZ3hk/b4tynSzksjrOoFeZoV64Vu10scHUNOOG6Za7HSlpx/iKZl2LvcW8+MMNqhicGcQTfoPT/8NsnzQ== 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=c+YMl+OqmHHJKwXYIR17ZTGKqiyBV+wF+b5Kiw15zh4=; b=W2NMs6UmwO3KmFwNiKoMKIg37DM6d1IOahpLQO8LVnrAuFtk92NZTOi8l6sPn5qSJYGEBhVQ3O4B8zB+ccieWC0zMyQT526uSlH+DupmUTyxU1v67X3qzNGY/l42DXYcysCCgPZICIdgXM/x2VSal341z0NTDkIsyqX5Yx+DG5o= Received: from SJ0PR03CA0158.namprd03.prod.outlook.com (2603:10b6:a03:338::13) by PH0PR12MB7472.namprd12.prod.outlook.com (2603:10b6:510:1e9::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.39; Thu, 18 Apr 2024 19:44:10 +0000 Received: from SJ5PEPF000001CA.namprd05.prod.outlook.com (2603:10b6:a03:338:cafe::78) by SJ0PR03CA0158.outlook.office365.com (2603:10b6:a03:338::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7495.23 via Frontend Transport; Thu, 18 Apr 2024 19:44: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 SJ5PEPF000001CA.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.7452.22 via Frontend Transport; Thu, 18 Apr 2024 19:44: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.35; Thu, 18 Apr 2024 14:44:09 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH v13 14/26] KVM: SEV: Add support to handle Page State Change VMGEXIT Date: Thu, 18 Apr 2024 14:41:21 -0500 Message-ID: <20240418194133.1452059-15-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240418194133.1452059-1-michael.roth@amd.com> References: <20240418194133.1452059-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: SJ5PEPF000001CA:EE_|PH0PR12MB7472:EE_ X-MS-Office365-Filtering-Correlation-Id: 9a57e676-5a0f-49a2-8889-08dc5fdfeaf7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: zKYysmAWyC6BnAcDIF+mvOCejiJhyylLPhsblWgT4M+EYqxL16279s/TJDqrlM/TRQVvezBBHsB+QULMmf8Bwa+fIJl7zYnJ6hGtYTiBYhgkNIOAEYoWZkYVBp3RTkhHd6gTUbikXNysyUDEEHBDu/o5INf0yMexJu1R04vKXWB/t8FrZdqdrrLjkXNOLI4msB1zI6IK3ylSM0tU6QbMolI8pBMsTaXOptABPBwiWe47uqBfwKu+Skxfzs1o+KEqYXcZeqwainxTBgHS6POXlQD13JRfZesoKDtkmSdScCLKL0fLO50sJGYj8vKnp3rgMgR4Lsy+X2Ts+DKf8TV7+9yc2SkspvrORHwnt1nTMQDopkyzT0RQkhT/TGJyEV/YehVgUWO354fYJ93B8JrXNawcO8Cx+hgm0GcYBJColiZnaXBY5qXCK+eljeRVmigYaIZ+hffpuDfs5jAJO7lKCBYSDLGr+6BW7fa7z0IIE6aTYDKDWskYheKdwrB2QEiwbGuOZmHIjFrC+X8qmxsMaj8l2m5oo78N9UvuQJIs5rBTO7FJD20Bc9vKww3h5k217H6ZC8RqaiPy278daY2w7LHag9ToJtt31unwcLqvpUtGYUISH6dUwQ/hEQlsYszQMXgcfkUloUI95R+DVHkdUj6QbFePm06OQxq1obSYyAXIf/43qn1tKXVibuFCZjCwM5bxXt7HW5kum+32ee0lbMf4HzmsClIEpOgxmcaP1Bmv7xTiEka8hujAObATe2x8 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)(36860700004)(7416005)(82310400014)(376005)(1800799015);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Apr 2024 19:44:10.2954 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9a57e676-5a0f-49a2-8889-08dc5fdfeaf7 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: SJ5PEPF000001CA.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB7472 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 0BF848000A X-Stat-Signature: 7xmoeshwy7t7sh5hox5o99jft1ur8dio X-Rspam-User: X-HE-Tag: 1713469453-319675 X-HE-Meta: U2FsdGVkX18vztBsSSiQJwbIoi/6fzyAB13sQpP/ha5WTYEReN/1oKQolJXofy7+gpch/ZrOmT85j5GUUVXOPDnqOHkf8ikROCF3dyIgSWGwYvSYi/tjOFEV3aypauNrbCmpXUx5bsGIvzOEWEAd9vxssQlBqTBM95SynF3Fzpw1wRMankA14BhAyacf7fpiW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AG4I55qw9Nu17RzzzUNTc9hYHte1d61fB7BfM= 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: 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 --- Documentation/virt/kvm/api.rst | 14 ++++++++++++++ arch/x86/kvm/svm/sev.c | 16 ++++++++++++++++ include/uapi/linux/kvm.h | 5 +++++ 3 files changed, 35 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 4a7a2945bc78..85099198a10f 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -7065,6 +7065,7 @@ values in kvm_run even if the corresponding bit in kvm_dirty_regs is not set. /* KVM_EXIT_VMGEXIT */ struct kvm_user_vmgexit { #define KVM_USER_VMGEXIT_PSC_MSR 1 + #define KVM_USER_VMGEXIT_PSC 2 __u32 type; /* KVM_USER_VMGEXIT_* type */ union { struct { @@ -7074,9 +7075,14 @@ values in kvm_run even if the corresponding bit in kvm_dirty_regs is not set. __u8 op; __u32 ret; } psc_msr; + struct { + __u64 shared_gpa; + __u64 ret; + } psc; }; }; + 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 @@ -7094,6 +7100,14 @@ update the private/shared state of the GPA using the corresponding KVM_SET_MEMORY_ATTRIBUTES ioctl. The 'ret' field is to be set to 0 by userpace on success, or some non-zero value on failure. +For the KVM_USER_VMGEXIT_PSC type, the psc union type is used. The kernel +will supply the GPA of the Page State Structure defined in the GHCB spec. +Userspace will process this structure as defined by the GHCB, and issue +KVM_SET_MEMORY_ATTRIBUTES ioctls to set the GPAs therein to the expected +private/shared state. Userspace will return a value in 'ret' that is in +agreement with the GHCB-defined return values that the guest will expect +in the SW_EXITINFO2 field of the GHCB in response to these requests. + 6. Capabilities that can be enabled on vCPUs ============================================ diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index e982468554cb..96e24a1e34e3 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3266,6 +3266,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; @@ -3484,6 +3485,15 @@ static int snp_begin_psc_msr(struct kvm_vcpu *vcpu, u64 ghcb_msr) return 0; /* forward request to userspace */ } +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.psc.ret); + + return 1; /* resume guest */ +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3721,6 +3731,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ret = 1; break; + case SVM_VMGEXIT_PSC: + vcpu->run->exit_reason = KVM_EXIT_VMGEXIT; + vcpu->run->vmgexit.type = KVM_USER_VMGEXIT_PSC; + vcpu->run->vmgexit.psc.shared_gpa = svm->sev_es.sw_scratch; + 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", diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 54b81e46a9fa..e33c48bfbd67 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -137,6 +137,7 @@ struct kvm_xen_exit { struct kvm_user_vmgexit { #define KVM_USER_VMGEXIT_PSC_MSR 1 +#define KVM_USER_VMGEXIT_PSC 2 __u32 type; /* KVM_USER_VMGEXIT_* type */ union { struct { @@ -146,6 +147,10 @@ struct kvm_user_vmgexit { __u8 op; __u32 ret; } psc_msr; + struct { + __u64 shared_gpa; + __u64 ret; + } psc; }; };