From patchwork Thu Apr 18 19:41:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13635353 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 A9EFEC4345F for ; Thu, 18 Apr 2024 19:48:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 439666B00B6; Thu, 18 Apr 2024 15:48:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3BF9C6B00B7; Thu, 18 Apr 2024 15:48:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1EC306B00B8; Thu, 18 Apr 2024 15:48:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id EF87F6B00B6 for ; Thu, 18 Apr 2024 15:47:59 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 5C363C07B5 for ; Thu, 18 Apr 2024 19:47:59 +0000 (UTC) X-FDA: 82023688278.25.3FC407F Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2044.outbound.protection.outlook.com [40.107.101.44]) by imf25.hostedemail.com (Postfix) with ESMTP id 68C17A000C for ; Thu, 18 Apr 2024 19:47:56 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Zqxn9OPh; spf=pass (imf25.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.101.44 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=1713469676; 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=pHAilU5wIuTeY2DKyABNnsoR9pEpGZ0pNOq2pX1Qd9g=; b=CrdretFYHQDNrw/4TcWGZx1vHOdsqOmFTVfJR6uF+aRiusTlc/UOZB7M6+jcMi7aOr0OkP dYpF3lyn14Iwx92yOZBDdZzWaeSBNA5b6m7OWQqmFGnRYIvjCgYfgLJ4YcvSTGEJY8KWgT MlaQnxGOZREVc1hYbX66Z7DFjugjFgQ= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1713469676; a=rsa-sha256; cv=pass; b=20qr4oOOW5PYnmhMwxkkPfRAl5GqO2U96U/vbtPnauM+LQFPDFu/uSUMMtgLLBo4/HcQHC VAl5dGKgnEp4Im4I+zlsKzv8cPeKWpbD04myjBFIXAu4mMr9JCPviEZpvdj2dFhGuGlALs 60JTE8nEkrdk3QMJXVkyzAD5BhOF49g= ARC-Authentication-Results: i=2; imf25.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Zqxn9OPh; spf=pass (imf25.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.101.44 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=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=A1v3ytFCkgJopFZdrBNevafsZKxJOWjs4xo9v1sckPWJNHEQ0lyASzfIG8Qljo1dsV7YKWf4u+MBE+zRM6qGzVrzKFYUtokTkv+6dASb+xeao2jvMxF+mMtBsvMKSxrX8tazOfcx/sAWyNOPyUmPhOrlzaX4gxJYsxfa1n4UFccyye4PbjaxIWLC80KnrMP+u5pVYCES5z9ESx6RR6QEtilfkn6Bl7w7oYSc9jLc1ZBMwp3Jjl1j9nfGiIX3bSno/orsmETmu8LXQ42p/np0T7pClWW7/1tiZbRg8wzNxYp3nCnbk8Oi+lLJPcoKIOHFD7655+5L/9TNEtno1zWGuw== 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=pHAilU5wIuTeY2DKyABNnsoR9pEpGZ0pNOq2pX1Qd9g=; b=ew3/jAeE+4IeaL+oaTDDfvOpavID3cRWvpu3JKaDnIohUc4apHIFaYuQClPxsLPrifnvaJ6/xE/Ozj7mkh2Rxcfk2fZoihQtf8t5+FLYRQ519ckoDEha35JppX631OPncRHu9u0/Lr/NFUIOh4kQMKkq24oUGuYJrIMuaikzQeHG0r29Vx+dh94uIjRhxrVOsq8LNIwVBKCtklNx/TW9LIqJZnIqA71B4odGC11dkCBRJf3yWVNVkyRpV77p3hYO9aWEJOOO/wS761ivraFTcfmoUpKg2/mqeEk+71T5+IlRm8UhuVUSuIm33W9+uBcs1BVo6ER8nYCFVm0CX3WyHg== 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=pHAilU5wIuTeY2DKyABNnsoR9pEpGZ0pNOq2pX1Qd9g=; b=Zqxn9OPhJUTvvrHOPXcepKehqdHuwuSOy5uQsql9xeoDKYXZm9kcpxu25ddqUKYiiB1OLZttX3pa8AtvxBlWHgbSIpsruZOqbLy2h3sx/1F41d5T4jE4T4SFQUPN2D0DD6Ve/RFyfdCKTS77az5UCfxUZLd3ohcUHvT5eBhxtTA= Received: from PH0PR07CA0074.namprd07.prod.outlook.com (2603:10b6:510:f::19) by MW4PR12MB7429.namprd12.prod.outlook.com (2603:10b6:303:21b::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.41; Thu, 18 Apr 2024 19:47:52 +0000 Received: from SN1PEPF0002BA50.namprd03.prod.outlook.com (2603:10b6:510:f:cafe::3a) by PH0PR07CA0074.outlook.office365.com (2603:10b6:510:f::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.33 via Frontend Transport; Thu, 18 Apr 2024 19:47:51 +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 SN1PEPF0002BA50.mail.protection.outlook.com (10.167.242.73) 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:47:51 +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:47:50 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Alexey Kardashevskiy Subject: [PATCH v13 23/26] KVM: SEV: Provide support for SNP_GUEST_REQUEST NAE event Date: Thu, 18 Apr 2024 14:41:30 -0500 Message-ID: <20240418194133.1452059-24-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: SN1PEPF0002BA50:EE_|MW4PR12MB7429:EE_ X-MS-Office365-Filtering-Correlation-Id: fc2000d2-817b-4dfb-65bd-08dc5fe06edb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: J9AFZ6jNFUwNOHDqPHVMZWByIdbNtvh6ko+rdCHOeXzPpQIQ195kxG6DmWGzK8d/nnxTXFnvFBeiHUDjdHuDjLR4aqTGPSJKfLU+3W/rcG07c9oEV03DSSWxyZeE3boAd8JUVT68yzCFABbPmRCo9V63suUKvznqDJ5WAli0uFBvXNhyjVRlOuq4Ca4/z6F/8VHWZzo5i6FVhJBPbKeusyI20e0m8vVlOZInr4/HSLWamg0kJEp40BGqfdf/mFEd2Co0PbDLfIiTeJshMQm9Zu4KE4bD6gZZtpG1S3bOzOF2zrvwbEstGaWEt4y4Du7YIp3CN7LTUx8Lk8JehIo4StYPuOLrW3v4BrCg2Pc1hakKctDM960n77dQAI5Qme+RX1b2pOJULGp9uEGO89QDtoD9cRQcGJiUJ6EJKyVeCPacJQGtCYjyyPdf2FEubp8fts8QOqStbsShan2OvqiQ2xF8wwzfspXpmpbDer6i7hymaYN7MSloi7LKTjwNocMYrvSVcVeBVpLHq3nwz/WuUOqJwU4eQ2yAu2Y+v+pefqYbU6XSdMVMl8SrJF6i+E83xFrhzofaUxo6APxjQAkBAcoKMei1f4gMQWHBlPKhbS6BBPWEGEH3ZHBTAzr/ekI09mbKGibxN6HdDXQOqU4EpzwsqBZK/Y08ierLdoVFo2P0H+OQcpll4an7kTxmPMpAYct+0bDk4BUsns7V8Vjd3/k6CU0VgcICT/Wq2k3DYUURY7dHELp4hVfQBxuuqHUZ 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)(82310400014)(1800799015)(7416005)(376005);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Apr 2024 19:47:51.5390 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fc2000d2-817b-4dfb-65bd-08dc5fe06edb 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: SN1PEPF0002BA50.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB7429 X-Stat-Signature: 8sep5ydwnaow44tkkpfnnoe7za6t9g58 X-Rspamd-Queue-Id: 68C17A000C X-Rspamd-Server: rspam06 X-Rspam-User: X-HE-Tag: 1713469676-852014 X-HE-Meta: U2FsdGVkX19JE9qi0BCU7TMb0jNzylnTVk/Ojn7Lznn75st0dFeSOgSNcu+pb301kogX+r5/ftsVu8fKvPLPNVD542eRrM8kbdivqtFi/LgBq3jxWy48BqtXGskbwBdUd4XoWMZyqueZB/S4bXsFNEFg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wqjF2EKYsixdjwEkuT0k7+EhvenlI9Pa+qr3kc7/7pkoB7t2Jk90uWint+loI3ppCoU3bs6c8VPmDmxEGAqiUAlGaKQMB4DoJJrJVJsIELE0PGVslGA/Qc6fneIxQ7FDCGzufr5UdRoHsAr408d/eArGk9 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 Version 2 of GHCB specification added support for the SNP Guest Request Message NAE event. The event allows for an SEV-SNP guest to make requests to the SEV-SNP firmware through hypervisor using the SNP_GUEST_REQUEST API defined in the SEV-SNP firmware specification. This is used by guests primarily to request attestation reports from firmware. There are other request types are available as well, but the specifics of what guest requests are being made are opaque to the hypervisor, which only serves as a proxy for the guest requests and firmware responses. Implement handling for these events. Signed-off-by: Brijesh Singh Co-developed-by: Alexey Kardashevskiy Signed-off-by: Alexey Kardashevskiy Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Reviewed-by: Tom Lendacky [mdr: ensure FW command failures are indicated to guest, drop extended request handling to be re-written as separate patch, massage commit] Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 83 ++++++++++++++++++++++++++++++++++ include/uapi/linux/sev-guest.h | 9 ++++ 2 files changed, 92 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index de51c3aa0040..953f00ddf31b 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -3281,6 +3282,7 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) case SVM_VMGEXIT_HV_FEATURES: case SVM_VMGEXIT_PSC: case SVM_VMGEXIT_TERM_REQUEST: + case SVM_VMGEXIT_GUEST_REQUEST: break; default: reason = GHCB_ERR_INVALID_EVENT; @@ -3724,6 +3726,83 @@ static int sev_snp_ap_creation(struct vcpu_svm *svm) return ret; } +static bool snp_setup_guest_buf(struct kvm *kvm, struct sev_data_snp_guest_request *data, + gpa_t req_gpa, gpa_t resp_gpa) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + kvm_pfn_t req_pfn, resp_pfn; + + if (!PAGE_ALIGNED(req_gpa) || !PAGE_ALIGNED(resp_gpa)) + return false; + + req_pfn = gfn_to_pfn(kvm, gpa_to_gfn(req_gpa)); + if (is_error_noslot_pfn(req_pfn)) + return false; + + resp_pfn = gfn_to_pfn(kvm, gpa_to_gfn(resp_gpa)); + if (is_error_noslot_pfn(resp_pfn)) + return false; + + if (rmp_make_private(resp_pfn, 0, PG_LEVEL_4K, 0, true)) + return false; + + 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 true; +} + +static bool snp_cleanup_guest_buf(struct sev_data_snp_guest_request *data) +{ + u64 pfn = __sme_clr(data->res_paddr) >> PAGE_SHIFT; + + if (snp_page_reclaim(pfn)) + return false; + + if (rmp_make_shared(pfn, PG_LEVEL_4K)) + return false; + + return true; +} + +static bool __snp_handle_guest_req(struct kvm *kvm, gpa_t req_gpa, gpa_t resp_gpa, + sev_ret_code *fw_err) +{ + struct sev_data_snp_guest_request data = {0}; + struct kvm_sev_info *sev; + bool ret = true; + + if (!sev_snp_guest(kvm)) + return false; + + sev = &to_kvm_svm(kvm)->sev_info; + + if (!snp_setup_guest_buf(kvm, &data, req_gpa, resp_gpa)) + return false; + + if (sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &data, fw_err)) + ret = false; + + if (!snp_cleanup_guest_buf(&data)) + ret = false; + + return ret; +} + +static void snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_gpa) +{ + struct kvm_vcpu *vcpu = &svm->vcpu; + struct kvm *kvm = vcpu->kvm; + sev_ret_code fw_err = 0; + int vmm_ret = 0; + + if (!__snp_handle_guest_req(kvm, req_gpa, resp_gpa, &fw_err)) + vmm_ret = SNP_GUEST_VMM_ERR_GENERIC; + + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, SNP_GUEST_ERR(vmm_ret, fw_err)); +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3984,6 +4063,10 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) vcpu->run->system_event.ndata = 1; vcpu->run->system_event.data[0] = control->ghcb_gpa; break; + case SVM_VMGEXIT_GUEST_REQUEST: + snp_handle_guest_req(svm, control->exit_info_1, control->exit_info_2); + 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/include/uapi/linux/sev-guest.h b/include/uapi/linux/sev-guest.h index 154a87a1eca9..7bd78e258569 100644 --- a/include/uapi/linux/sev-guest.h +++ b/include/uapi/linux/sev-guest.h @@ -89,8 +89,17 @@ struct snp_ext_report_req { #define SNP_GUEST_FW_ERR_MASK GENMASK_ULL(31, 0) #define SNP_GUEST_VMM_ERR_SHIFT 32 #define SNP_GUEST_VMM_ERR(x) (((u64)x) << SNP_GUEST_VMM_ERR_SHIFT) +#define SNP_GUEST_FW_ERR(x) ((x) & SNP_GUEST_FW_ERR_MASK) +#define SNP_GUEST_ERR(vmm_err, fw_err) (SNP_GUEST_VMM_ERR(vmm_err) | \ + SNP_GUEST_FW_ERR(fw_err)) +/* + * The GHCB spec only formally defines INVALID_LEN/BUSY VMM errors, but define + * a GENERIC error code such that it won't ever conflict with GHCB-defined + * errors if any get added in the future. + */ #define SNP_GUEST_VMM_ERR_INVALID_LEN 1 #define SNP_GUEST_VMM_ERR_BUSY 2 +#define SNP_GUEST_VMM_ERR_GENERIC BIT(31) #endif /* __UAPI_LINUX_SEV_GUEST_H_ */