From patchwork Mon Oct 16 13:28: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: 13423393 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 78C3ACDB465 for ; Mon, 16 Oct 2023 13:47:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2105A8D009A; Mon, 16 Oct 2023 09:47:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1997D8D0001; Mon, 16 Oct 2023 09:47:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 03A958D009A; Mon, 16 Oct 2023 09:47:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id E26208D0001 for ; Mon, 16 Oct 2023 09:47:10 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 8BB86B5AF6 for ; Mon, 16 Oct 2023 13:47:10 +0000 (UTC) X-FDA: 81351451020.03.0EC4856 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by imf16.hostedemail.com (Postfix) with ESMTP id 2EA17180021 for ; Mon, 16 Oct 2023 13:47:07 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="JP/yrh7V"; spf=pass (imf16.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.40 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=1697464027; a=rsa-sha256; cv=pass; b=zOGZDuQ1Yk/GMGpCLSyNFz2iTKNSDdOTAcLIhHju1kYltQWqD5Q9a2DfECsDHAY7dzCgFS C7j9ci0WJLqpp8QrtjYsAoUXZr3CdoM9w7e7yBZyeDfGtuDpE2mEcKzv4ujoMYOCNce0bK 41veTqEuFKSpv2mvsf1LGUR8rQu44SM= ARC-Authentication-Results: i=2; imf16.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="JP/yrh7V"; spf=pass (imf16.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.40 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=1697464027; 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=O16UDz6bg6SnYcZCZVeleJclaR2rL+C/MJ+u7rMewKY=; b=51A/93L7mi/d62lR1MyU4MYO8Bstm4tMKplARyJOIqgUZsUatQdU6nAiiO0Cd0ES/wnE4I 2LCPWb6lS8H2cQU7zW2B7FKSdxZQECewhPrbngeNCa3HkcjzWHq2RQlch6x2HyZ7hxRPuM X7/P9HpD/D1hU23epBj6m1UtQ8cnViE= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RZtnVYIkRKpklQIhXmFzOKzhYGwnBGBX66T2PvTid/5q+mEzwHM/gi5WWDZF0R/E2lYX00VneqJQZyPFkYKecBiO3ES9nB+FwIQfyADs4KjGpL5Z4ipOod/L0QU+RlqR4sR2B98wRcYirVvXSPcTuS+aPxCHEwxrQe9GpQ3DoDGzKYed58zgo3Ht9HD6QgKw2+5Le/hpm5cxMRSG6F/SlNE/H2OjHy2GJkvPNPyzumoQpnZ+Je3VsFaD4Bz2ttH3fiz+AdagBYYNpqaBN5YEt/1jBUGhgs+9TT5D8e4pkFLhNc9stEe4CXDwfwHZ6sjxtyFsrwW0C86GIyhblfTmag== 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=O16UDz6bg6SnYcZCZVeleJclaR2rL+C/MJ+u7rMewKY=; b=A/27t0eVBPWpkXJy1LCqENcpt/KHjsEs00fAeAJ5/Yevu0Qt8vRpcPv6ZLNIPyOmQXwEayON/qeyuPNKTNB5LgoTW/6PWmPsEo4lhWqLzaR7CCD7wJkkary2uDk0iuP9x4OHFnvOqq+klS1PomiBtj6fC+cxuHQ4QNI/f4UWvdoT/yi3A9txjx/tviZTMQdTXVtUi/TuAMeZR67vYpor3P0YoG2Wqyk8D04nVDx0q1Bq0wlggsJ/iWY3Ns+7OjfgrfRkAOEBqIoZ7kenh1IvpdRk7EikRHSyC/Vey8I7MKqQUvry5VgCOtMy4nTFri92/l9jLt2L1udapDxkIUEiYg== 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=O16UDz6bg6SnYcZCZVeleJclaR2rL+C/MJ+u7rMewKY=; b=JP/yrh7VDpdzkVIFb4yRyvB5OFbElJatssH7QOQoUuT7ZfrCGEzV7jj5M/SV63zYfzi8ORJ5upgxAOGkdhI/lAlQUEnud7VHyw7Lym+IN8dQp+8C1v4NnBojAtgeerWKgC0C/QbQf+2f1HmMvGlrABQ2eSHwZOHI3ZeVZM7NCc8= Received: from BL0PR05CA0008.namprd05.prod.outlook.com (2603:10b6:208:91::18) by LV8PR12MB9407.namprd12.prod.outlook.com (2603:10b6:408:1f9::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6886.35; Mon, 16 Oct 2023 13:47:03 +0000 Received: from MN1PEPF0000F0DF.namprd04.prod.outlook.com (2603:10b6:208:91:cafe::d4) by BL0PR05CA0008.outlook.office365.com (2603:10b6:208:91::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6907.17 via Frontend Transport; Mon, 16 Oct 2023 13:47: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 MN1PEPF0000F0DF.mail.protection.outlook.com (10.167.242.37) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6838.22 via Frontend Transport; Mon, 16 Oct 2023 13:47: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.27; Mon, 16 Oct 2023 08:47:01 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Alexey Kardashevskiy Subject: [PATCH v10 48/50] KVM: SEV: Provide support for SNP_GUEST_REQUEST NAE event Date: Mon, 16 Oct 2023 08:28:17 -0500 Message-ID: <20231016132819.1002933-49-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231016132819.1002933-1-michael.roth@amd.com> References: <20231016132819.1002933-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MN1PEPF0000F0DF:EE_|LV8PR12MB9407:EE_ X-MS-Office365-Filtering-Correlation-Id: 607a1e43-8e69-4273-02ea-08dbce4e6141 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: U3Z/ElY34s2JHCJj1JhEWSsP+K/1GgJgFpqtlUvmELYzvXlYqnwZRu/oRObMD0UWwlQhqCpZD2JMq/3Zh6ivk9mEPTHzcPqp55jXmxJfLXI0xTueMWFhg4pD98sv+6NUSjXToAGNDX9IFESvvsSaGGvUzbGPEHguPz0tUiLiMsdZbEguNnOjFaVYDgaq7g/ID+AFozpLzGp1mTRSVBnFztJGUwoi3clvU1CIVAn8YP2R9RkHk2RrqLDvgK5zjSYH0cWPjyVS6od1YqnByx1ScgwpJ513u5B/p6j4Ft6DghVwqz8QZUJeSsdw0nSJ9CVTnJ+Z8qZw0myp275meXWNrTgNV8L9jrSF8Zy2I7e59nnxIZlRuPJx7mDqIO/TjktC5Nca8fOt1m35hXU1U5WRqC74YVU9s3PgBis2zeexrEHW0rx3CfXG/dzCcGAObK6WkAi743kvK2my0Feq8ySJcFBgb6pTUW9tPuyhhZFo7n66wizUSlEJpzDjsTgG2zxHY7XqXJaOfqZkWR0lzoUwlbNiM+HLKJ7fosnq3mQfGA86kuOgcmqW4eXB3RmFJVpKhmjPGhqbVtkwLg3+ifCMla0I/5AGsES750pavMflGOqpUDVsol4AzRocypKllyl3e1wEerguiSQm/vWBN4TfCKQK+79gN8FdxzqvjX/kJIEEqQSpTQObWcNsyfNBintmOZzcLUx+tFNNzxipteW0+L5IP+pBCOkaH79hvyumRW01wGEpuiu2vxspjq/y/GhvVqRLLsXoCfH3Dt0iPyRtCw== 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)(4636009)(39860400002)(346002)(376002)(396003)(136003)(230922051799003)(1800799009)(64100799003)(82310400011)(451199024)(186009)(36840700001)(46966006)(40470700004)(41300700001)(478600001)(54906003)(70206006)(70586007)(6666004)(6916009)(16526019)(26005)(1076003)(426003)(316002)(336012)(2616005)(7416002)(8936002)(7406005)(4326008)(8676002)(2906002)(5660300002)(36756003)(44832011)(81166007)(86362001)(356005)(47076005)(36860700001)(83380400001)(82740400003)(40460700003)(40480700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Oct 2023 13:47:03.6697 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 607a1e43-8e69-4273-02ea-08dbce4e6141 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: MN1PEPF0000F0DF.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV8PR12MB9407 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 2EA17180021 X-Stat-Signature: 31xffi1j5dg3kqoa8h3aijf6j4zp4fg7 X-Rspam-User: X-HE-Tag: 1697464027-843162 X-HE-Meta: U2FsdGVkX1+N4XzcA1DRFfDr7WiU2VsmLKhKhgZB/IalwcWcQD/wnh38ryXwbggRW34qfDZB4q5E96ajZJaH09CkrpRLCkBflnfUXf9SlYFUahAiKGegyyhHQqNNDqwNWFZlqiRi4NBDPo++MbtD++sO4t8s4oY8tsPZxL9piNw+Co0Q71U0C458jbqxVR7eDL688MFh2GSWzu2rYlaz4rtLgAWO/X2fjEcNsMVSP47467Mj1Mh66S/S4NAp+G1ZgYNOzi2MzIxq8/lk64hLd0/I+033FY/kCGi+7ISDDDMFL3CRAXcHtNs/8FjNfo0NkTzTzYacptF7zf8OniqwVULqm56X+cudhRYePPDSyY3OSbgt+zc/8uMLim++0jqJBX/nbcZVxwE5LdMpifQOuijDR3PZE27aEoT3MbcAhtGcJz2rwVPv1mIjQj0ZZs3Zza6bt5y5k41DurW8IP4n3Dvip+zas9PWcyoXqh/KvZoKmtW4Pb6cWuFfRzLQmSeyyo5te+ZkLtjcockUStxY3yCTtFsyjEDJl//RNikbyOyRdkdxHfZieqBFf7TiwkSN1+QGF95e9WjZ34DkYkdvxQKLPnEMpzZXQ+Cc/EXzrpM8q7EQcbulYKqHEom4/Q0j/uvApVo5+4jYly30PVrIhW/ZZhooK12MGTbIHhg2bJyH9RyGtfhMg2ShmZEyKuJgvbHAhDihnd0GzJgfE4tc/0JLS3EQEYhlqVmfJ4m1rj6zYnuz8FpPefbsokdctJ9LDsSjOOGRb6ZCvlbYx7Y8zTEkZyXDxqyNs2tkXdjsOhtRQohH0KUQv8VINvfbrBc2gO/XVpkIdgchCQnMgX9N2Zkej6woG6zwZX83Dgmb5QMefB2iVxVLOplZcAILsBLK9pGzsREvksgqoLx4C1z7nCdUNtwXA6mIGv8gm7qjzYerzZntIpaPxXq8Brz3y1l2D0aZrpbK/F91+ZhlxHg vTe+y9y7 x/Lb16yArXA5ke+N0lD4RGh+pB/4nITbKR49iWbAbv3efYSP0DVewNMvDENeKrGlSqfzqt4k1yG2yWeq9WJfq8Bgc497TxF9Th3I7o6sNNta0SAdfk3UyYOGnkBHrnokcniKlBCzvmVHq1w+wwQCwy4TXR7p9MCioZxGbtEBWvn0Vsek4ArxfBsryS8GaAYGGxFQ3+lmQemSXWHZRbp+YZHIONl64pbqj26amT5pyRgPpeuvC02olW/nipgL34k1iUi9drOgNK4P+bYTJpSlJcHjSnlxfsZpdBLb3mUY9L4HT5EXgOJSpM2e8lF937gT7nEqX1RK9HbNI5V6/3w7MpKmj6QuFvZ2Z4vgV6v1xMoOQ0mcKGLPcEGbCyHSz2fr5vlNR 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: 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 | 176 +++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 1 + drivers/crypto/ccp/sev-dev.c | 15 +++ include/linux/psp-sev.h | 1 + 4 files changed, 193 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 602aaf82eef3..d71ec257debb 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 @@ -339,6 +340,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); @@ -2345,8 +2348,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) @@ -3218,6 +3223,8 @@ 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: + case SVM_VMGEXIT_EXT_GUEST_REQUEST: break; default: reason = GHCB_ERR_INVALID_EVENT; @@ -3627,6 +3634,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_VMM_ERR(SNP_GUEST_VMM_ERR_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 (snp_certs && + kvm_write_guest(kvm, data_gpa, snp_certs->data, 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; @@ -3894,6 +4058,18 @@ 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_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: 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 bdf792ba06e1..3673a6e4e22e 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -98,6 +98,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 4807ddd6ec52..f9c75c561c4e 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 722e26d28d2f..3b294ccbbec9 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -25,6 +25,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