From patchwork Mon Mar 30 01:34:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 11464407 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0EBE6912 for ; Mon, 30 Mar 2020 01:39:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id CD25220780 for ; Mon, 30 Mar 2020 01:39:11 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="BOnJO6Uv" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728799AbgC3BjH (ORCPT ); Sun, 29 Mar 2020 21:39:07 -0400 Received: from mail-eopbgr750053.outbound.protection.outlook.com ([40.107.75.53]:39901 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727955AbgC3BjH (ORCPT ); Sun, 29 Mar 2020 21:39:07 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EpwaBI9hF7WwFBBHEB/iMiPQnkurgp690LzNFN4t5e3tKIooIgGW22hOzDnolkqc+F2wJdvXV2gXG7En1sUKMOp1DGaDYOkuArxR6FOP4Ag0bsksDomeyVHgbhS6fDqGeQDgJniWAuzh+gyA1MIIKH6wHWw46yooJ1U9HO8oqyf7W2yF8x5miyiSKKN1LJfN8kRnsTukTbkuoEu4M89eLjA3KH+nNRRogVDxRIS42+mcSbdfTehG+ZOvKcJmJBvYGXhI0iKn9xxeaN/8vpUAP644Bb8bnqbUdcMmajEfn5kgvm224bCjodopQCe3ag92Uq4bUEQgqV7TrBnml+42Pw== 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-SenderADCheck; bh=Px3DaSAofM8+jNq45/Y/o5vN7ooFf4B+voinsjKnm1c=; b=cHPKREXuO1bq47Fy7DituRB2m/mOqgA6Cr+vJxcmwwRyhFL8gsrm7nGJMhU/oHY/2ZQBNCU7eK99zHsnOes891W+kqQCAiWvMP2mVjAQfZh3AjlChw/CBR7A9RTPKy3ZID0jSa2wJMR2hE6+6QjOcGXZsxzOrOV7Fp4WNkTQCGpBZyDDD2Lvt6l3edhpiEhyJnN/yM5rFasz7zegd6DjUNZHhP9CObtRgtvFnEWWQRgKwPdjoqewz+U1/ggeGSQotLLewZ3HW62/PYchbbYU7+MgRqL9bneG/7w7YaVXQRERPttFLfZa72UBzLjLOEptN8J5QfBQfXPa0J8kp7+duA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Px3DaSAofM8+jNq45/Y/o5vN7ooFf4B+voinsjKnm1c=; b=BOnJO6Uvsw6ZI/U3X7OKzWdTVBDTVJc3nEymHjI0lOKwbk02Ou3iQD/EdeAXTy9Ho37se4kaEMGEyhec/4BtB3WNHyktStj1RrbGTvz/ySEnFHefjByWtCRWN+iMoYEOJATBZfDQCO/VAaQ3dgLp0yI08zLUABMIYuBw9dgpTeM= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Ashish.Kalra@amd.com; Received: from DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) by DM5PR12MB1387.namprd12.prod.outlook.com (2603:10b6:3:6c::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.20; Mon, 30 Mar 2020 01:34:22 +0000 Received: from DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c]) by DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c%12]) with mapi id 15.20.2856.019; Mon, 30 Mar 2020 01:34:22 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, bp@suse.de, thomas.lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, rientjes@google.com, srutherford@google.com, luto@kernel.org, brijesh.singh@amd.com Subject: [PATCH v5 02/14] KVM: SVM: Add KVM_SEND_UPDATE_DATA command Date: Mon, 30 Mar 2020 01:34:13 +0000 Message-Id: <7ea65c7852543a7cd4fb904db751bed859735a84.1585531159.git.ashish.kalra@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-ClientProxiedBy: DM6PR02CA0040.namprd02.prod.outlook.com (2603:10b6:5:177::17) To DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by DM6PR02CA0040.namprd02.prod.outlook.com (2603:10b6:5:177::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.18 via Frontend Transport; Mon, 30 Mar 2020 01:34:21 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: f9a7f1e8-f147-4139-11b9-08d7d44a7942 X-MS-TrafficTypeDiagnostic: DM5PR12MB1387:|DM5PR12MB1387: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7691; X-Forefront-PRVS: 0358535363 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR12MB1386.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(10009020)(4636009)(39860400002)(376002)(346002)(366004)(136003)(396003)(186003)(66574012)(66556008)(6666004)(66476007)(86362001)(6916009)(66946007)(316002)(4326008)(7696005)(52116002)(5660300002)(6486002)(16526019)(26005)(36756003)(478600001)(7416002)(81166006)(81156014)(8936002)(2616005)(8676002)(2906002)(956004)(136400200001);DIR:OUT;SFP:1101; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PTKITN50Ng8u8mxCA56fkAHbGOS8Mpz4YZkVEvrZNVmxhEYARMXpe4Bfa5dN9hOW6g9Gz004KDnIbSJYtvvtaaCctW8WBse/kaQGTSTdHIFDTFHimryIBtgBc6YGkpjBqB0geGiEgLRoUzUHxTsCxngDs2H8Vv9U7yyzG96uNjHi53MWaNfywNI8yDsXDV9vUvU5c1Z367mE0Q5uBjoblD8eyQwAkoC9fL0a5m2JG9qhvt/Pb13wjO8T0s0ACwm9BJqpPJLUWonDcguHfhaPAyQrigJm2sLwMjLdwHXUyfhHhIxfzle5p1H9rjEZfp4ZVu9IQyN3ZXB90rUxiME2XRkSdResarnuhwNUMd+A4JOTbbZrPrT/pL72aUdDtw6DsNNuzUfWNI/etsMLydNOnyw1kyGnPt2b2BPOjalXTO7DQz90Kl5rHose4jvutfd88SbJCp5wqepGATHgBx2K188BBAvwBOSWz2iG5xavxtT5st6QomdvbT4JQg1Mu6bY X-MS-Exchange-AntiSpam-MessageData: /f49lnhE013ruoHZbd5SrUwDHt0+OLbNVh81n+z5a213PAhTb4CWmU+Rvq8vUc4aYkjvoE4gJg1Lbk2KcZ6rMztYAtpd0X8ZhY9A2+SjlCV/NspIgaAULOGC/NZnUxiJ62nxg2IdoHcLGVDv3VnP9Q== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: f9a7f1e8-f147-4139-11b9-08d7d44a7942 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Mar 2020 01:34:22.5145 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: uCwCuPQ7HG1anYzlVa3uo7MDsJFnrGuwSo5rlvfQrxPWGE3mV2U1533nVXlGHv8nKYJ59I7D37ZKk0EAm85fLA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1387 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Brijesh Singh The command is used for encrypting the guest memory region using the encryption context created with KVM_SEV_SEND_START. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: "H. Peter Anvin" Cc: Paolo Bonzini Cc: "Radim Krčmář" Cc: Joerg Roedel Cc: Borislav Petkov Cc: Tom Lendacky Cc: x86@kernel.org Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org Reviewed-by : Steve Rutherford Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- .../virt/kvm/amd-memory-encryption.rst | 24 ++++ arch/x86/kvm/svm.c | 136 +++++++++++++++++- include/uapi/linux/kvm.h | 9 ++ 3 files changed, 165 insertions(+), 4 deletions(-) diff --git a/Documentation/virt/kvm/amd-memory-encryption.rst b/Documentation/virt/kvm/amd-memory-encryption.rst index 4fd34fc5c7a7..f46817ef7019 100644 --- a/Documentation/virt/kvm/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/amd-memory-encryption.rst @@ -290,6 +290,30 @@ Returns: 0 on success, -negative on error __u32 session_len; }; +11. KVM_SEV_SEND_UPDATE_DATA +---------------------------- + +The KVM_SEV_SEND_UPDATE_DATA command can be used by the hypervisor to encrypt the +outgoing guest memory region with the encryption context creating using +KVM_SEV_SEND_START. + +Parameters (in): struct kvm_sev_send_update_data + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_launch_send_update_data { + __u64 hdr_uaddr; /* userspace address containing the packet header */ + __u32 hdr_len; + + __u64 guest_uaddr; /* the source memory region to be encrypted */ + __u32 guest_len; + + __u64 trans_uaddr; /* the destition memory region */ + __u32 trans_len; + }; + References ========== diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index 63d172e974ad..8561c47cc4f9 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -428,6 +428,7 @@ static DECLARE_RWSEM(sev_deactivate_lock); static DEFINE_MUTEX(sev_bitmap_lock); static unsigned int max_sev_asid; static unsigned int min_sev_asid; +static unsigned long sev_me_mask; static unsigned long *sev_asid_bitmap; static unsigned long *sev_reclaim_asid_bitmap; #define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT) @@ -1232,16 +1233,22 @@ static int avic_ga_log_notifier(u32 ga_tag) static __init int sev_hardware_setup(void) { struct sev_user_data_status *status; + u32 eax, ebx; int rc; - /* Maximum number of encrypted guests supported simultaneously */ - max_sev_asid = cpuid_ecx(0x8000001F); + /* + * Query the memory encryption information. + * EBX: Bit 0:5 Pagetable bit position used to indicate encryption + * (aka Cbit). + * ECX: Maximum number of encrypted guests supported simultaneously. + * EDX: Minimum ASID value that should be used for SEV guest. + */ + cpuid(0x8000001f, &eax, &ebx, &max_sev_asid, &min_sev_asid); if (!max_sev_asid) return 1; - /* Minimum ASID value that should be used for SEV guest */ - min_sev_asid = cpuid_edx(0x8000001F); + sev_me_mask = 1UL << (ebx & 0x3f); /* Initialize SEV ASID bitmaps */ sev_asid_bitmap = bitmap_zalloc(max_sev_asid, GFP_KERNEL); @@ -7274,6 +7281,124 @@ static int sev_send_start(struct kvm *kvm, struct kvm_sev_cmd *argp) return ret; } +/* Userspace wants to query either header or trans length. */ +static int +__sev_send_update_data_query_lengths(struct kvm *kvm, struct kvm_sev_cmd *argp, + struct kvm_sev_send_update_data *params) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_send_update_data *data; + int ret; + + data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); + if (!data) + return -ENOMEM; + + data->handle = sev->handle; + ret = sev_issue_cmd(kvm, SEV_CMD_SEND_UPDATE_DATA, data, &argp->error); + + params->hdr_len = data->hdr_len; + params->trans_len = data->trans_len; + + if (copy_to_user((void __user *)(uintptr_t)argp->data, params, + sizeof(struct kvm_sev_send_update_data))) + ret = -EFAULT; + + kfree(data); + return ret; +} + +static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_send_update_data *data; + struct kvm_sev_send_update_data params; + void *hdr, *trans_data; + struct page **guest_page; + unsigned long n; + int ret, offset; + + if (!sev_guest(kvm)) + return -ENOTTY; + + if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, + sizeof(struct kvm_sev_send_update_data))) + return -EFAULT; + + /* userspace wants to query either header or trans length */ + if (!params.trans_len || !params.hdr_len) + return __sev_send_update_data_query_lengths(kvm, argp, ¶ms); + + if (!params.trans_uaddr || !params.guest_uaddr || + !params.guest_len || !params.hdr_uaddr) + return -EINVAL; + + + /* Check if we are crossing the page boundary */ + offset = params.guest_uaddr & (PAGE_SIZE - 1); + if ((params.guest_len + offset > PAGE_SIZE)) + return -EINVAL; + + /* Pin guest memory */ + guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK, + PAGE_SIZE, &n, 0); + if (!guest_page) + return -EFAULT; + + /* allocate memory for header and transport buffer */ + ret = -ENOMEM; + hdr = kmalloc(params.hdr_len, GFP_KERNEL_ACCOUNT); + if (!hdr) + goto e_unpin; + + trans_data = kmalloc(params.trans_len, GFP_KERNEL_ACCOUNT); + if (!trans_data) + goto e_free_hdr; + + data = kzalloc(sizeof(*data), GFP_KERNEL); + if (!data) + goto e_free_trans_data; + + data->hdr_address = __psp_pa(hdr); + data->hdr_len = params.hdr_len; + data->trans_address = __psp_pa(trans_data); + data->trans_len = params.trans_len; + + /* The SEND_UPDATE_DATA command requires C-bit to be always set. */ + data->guest_address = (page_to_pfn(guest_page[0]) << PAGE_SHIFT) + + offset; + data->guest_address |= sev_me_mask; + data->guest_len = params.guest_len; + data->handle = sev->handle; + + ret = sev_issue_cmd(kvm, SEV_CMD_SEND_UPDATE_DATA, data, &argp->error); + + if (ret) + goto e_free; + + /* copy transport buffer to user space */ + if (copy_to_user((void __user *)(uintptr_t)params.trans_uaddr, + trans_data, params.trans_len)) { + ret = -EFAULT; + goto e_unpin; + } + + /* Copy packet header to userspace. */ + ret = copy_to_user((void __user *)(uintptr_t)params.hdr_uaddr, hdr, + params.hdr_len); + +e_free: + kfree(data); +e_free_trans_data: + kfree(trans_data); +e_free_hdr: + kfree(hdr); +e_unpin: + sev_unpin_memory(kvm, guest_page, n); + + return ret; +} + static int svm_mem_enc_op(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -7321,6 +7446,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp) case KVM_SEV_SEND_START: r = sev_send_start(kvm, &sev_cmd); break; + case KVM_SEV_SEND_UPDATE_DATA: + r = sev_send_update_data(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 17bef4c245e1..d9dc81bb9c55 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1570,6 +1570,15 @@ struct kvm_sev_send_start { __u32 session_len; }; +struct kvm_sev_send_update_data { + __u64 hdr_uaddr; + __u32 hdr_len; + __u64 guest_uaddr; + __u32 guest_len; + __u64 trans_uaddr; + __u32 trans_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 Mar 30 01:34:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 11464383 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 824F481 for ; Mon, 30 Mar 2020 01:35:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5619120780 for ; Mon, 30 Mar 2020 01:35:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="yAE19gfS" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728239AbgC3BfO (ORCPT ); Sun, 29 Mar 2020 21:35:14 -0400 Received: from mail-eopbgr750057.outbound.protection.outlook.com ([40.107.75.57]:6492 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727801AbgC3BfO (ORCPT ); Sun, 29 Mar 2020 21:35:14 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=A38CYZqpTL2yhYiZj9PXuEEPoUsTeCgsCuFj820zRTLkMZIe/A/y6P3aRalq0Umk/u5YeBY0uos1sqPx/IWMSsPwmZbC8dq70Es3uw2HGyHP58ebl9eCrKt7+ss2HLbhuppXoFX7F67adQCTSw2LbyJLqMTmoOUdJ9JQ0Dwc3IFUU4/vlIU6J55kfBojAhSspgDVHSdFDA1AMhKNXzK0gteoXu51IVLJG2n5qDFqfMXBjdbg4JaiAIbLASBCW1X8IQkRCtbgwyrExZL+XxOqEQR25ZISuKXuaQrZpGn5P6wWLsw5qtDU0ZFQu4GLZ0RNtjE0AiPXdrNT3gAlJyHboQ== 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-SenderADCheck; bh=XjtJwepDN2kGis4bfFaCD9krHPW7ehs/wxeUhcqsEqc=; b=COJN+KuMcmWu9W7tX/MDqzE9MU86iM/47Q5vgUI0Uds2GpyVxXmIuD6tWnOpLNbJJfTZV21VW89rSK0Qpitk8mhe8hUS6GQ3coXmEDV2IwTgiVxL9ufoE7TdAICQ+khdGKli+xB5CYoEObUj2ssc8z0l7vRdHvmxXTxC8RrlNsLZZIdMmmgOrTwf+3dnQaSMEz139fqAMjz6M+kDFuSNSIM8Mf7olc5ObdBUIoCoVwnYLLhDRKnsOuhmsjS/QXJRunuAnH9xhYwji0SDGnXfMCnpc7242Sp+MPZrTXXvNVqowgi+6+IBtBrnbeSvynIaprtP0CvNDgEH4eH/q51cCw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=XjtJwepDN2kGis4bfFaCD9krHPW7ehs/wxeUhcqsEqc=; b=yAE19gfSUqNRNJWjwLnXuRo5Gv+OvUiceE8AEc0j6CZVf14Yi5RlWB4JpXZeZX7IJgQaMf7wzPMHQTG2TvlzkGdmtTdf7U8Ow5v5GO7GtMhBQSJwvpD6TwgVywe43h+l3q7iF37d6g+5nHcBxlfyKp9BDsIFirpppv47Qp71M4Q= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Ashish.Kalra@amd.com; Received: from DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) by DM5PR12MB1387.namprd12.prod.outlook.com (2603:10b6:3:6c::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.20; Mon, 30 Mar 2020 01:34:38 +0000 Received: from DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c]) by DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c%12]) with mapi id 15.20.2856.019; Mon, 30 Mar 2020 01:34:38 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, bp@suse.de, thomas.lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, rientjes@google.com, srutherford@google.com, luto@kernel.org, brijesh.singh@amd.com Subject: [PATCH v5 03/14] KVM: SVM: Add KVM_SEV_SEND_FINISH command Date: Mon, 30 Mar 2020 01:34:29 +0000 Message-Id: <798316bc964cef34d2760a87de0fb6dc4e5d9af3.1585531159.git.ashish.kalra@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-ClientProxiedBy: DM6PR02CA0066.namprd02.prod.outlook.com (2603:10b6:5:177::43) To DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by DM6PR02CA0066.namprd02.prod.outlook.com (2603:10b6:5:177::43) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.20 via Frontend Transport; Mon, 30 Mar 2020 01:34:37 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 5cc15f87-e7fb-42db-f71c-08d7d44a82e0 X-MS-TrafficTypeDiagnostic: DM5PR12MB1387:|DM5PR12MB1387: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3826; X-Forefront-PRVS: 0358535363 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR12MB1386.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(10009020)(4636009)(39860400002)(376002)(346002)(366004)(136003)(396003)(186003)(66574012)(66556008)(6666004)(66476007)(86362001)(6916009)(66946007)(316002)(4326008)(7696005)(52116002)(5660300002)(6486002)(16526019)(26005)(36756003)(478600001)(7416002)(81166006)(81156014)(8936002)(2616005)(8676002)(2906002)(956004)(136400200001);DIR:OUT;SFP:1101; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: m8PIE8DnFmOX9H07WuIReiTARTbvmFQNiiWERzSv66f7Oow9x97PUuK9//rzHhFNo/efUkc3tZtOUVFxgkgwWnhsLp9qFyUxtnoaPR00GUX5U/NaK/lq1dkPzNzlgD4ajPPWEEIDbwPAvJ/C8pm5IbCQPisAOYKp8xC67KZAru04UMaRqwe+k+udvMuj8dNWA/yEF7BAKmxHgK0RnBS03ElNizAXtdBxD8mE0FjTbkP813DW+NH+4KrGwnoWCoqawwSSQAc2qzyr0wDg2oiIKBFNECXQuDAYSK0wyl1f1HvOt/FryYVY5TcJ6a+4Fc2H0Dth43DuHvslijweETehv73QXrDG53IRyE0abQgQwOraj+iQjcMU6BiXXZL7uZOqrV6DHnzzXoLzgJuBWXzrjvkkjvMLntiQ81VSJeLpS7FxLdnnHPyokor2yEUtL86aLKo12ooc19XMJeaqK3CieJ0qbL9dDXcz1LEaEiet0L0DUDyYGXReQ2Dcf20vTMjr X-MS-Exchange-AntiSpam-MessageData: itskoVnL7hZCAsmhTrO0jeap5q0Je6/Oo3KWkA/QsCPjPKuEzn2/Kmku0doMSi3L7zcSUzfFSZ7YQ4rRNKZOoOVvkjZr8QkbkB720fA24KLgOAo0J0Qc2hVADap/jc2gldvSmMmMePvCF+9rV238gQ== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5cc15f87-e7fb-42db-f71c-08d7d44a82e0 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Mar 2020 01:34:38.6583 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: bXeISo38cW7MVc4nM9MVAmO6M7p5FSizOLDSGyML7LtFAAOXv6Yfecdt9aYa8i6trmmLUjLxxgUyS4RlbKqiPA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1387 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Brijesh Singh The command is used to finailize the encryption context created with KVM_SEV_SEND_START command. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: "H. Peter Anvin" Cc: Paolo Bonzini Cc: "Radim Krčmář" Cc: Joerg Roedel Cc: Borislav Petkov Cc: Tom Lendacky Cc: x86@kernel.org Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org Reviewed-by: Steve Rutherford Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- .../virt/kvm/amd-memory-encryption.rst | 8 +++++++ arch/x86/kvm/svm.c | 23 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/Documentation/virt/kvm/amd-memory-encryption.rst b/Documentation/virt/kvm/amd-memory-encryption.rst index f46817ef7019..a45dcb5f8687 100644 --- a/Documentation/virt/kvm/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/amd-memory-encryption.rst @@ -314,6 +314,14 @@ Returns: 0 on success, -negative on error __u32 trans_len; }; +12. KVM_SEV_SEND_FINISH +------------------------ + +After completion of the migration flow, the KVM_SEV_SEND_FINISH command can be +issued by the hypervisor to delete the encryption context. + +Returns: 0 on success, -negative on error + References ========== diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index 8561c47cc4f9..71a4cb3b817d 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -7399,6 +7399,26 @@ static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) return ret; } +static int sev_send_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_send_finish *data; + int ret; + + if (!sev_guest(kvm)) + return -ENOTTY; + + data = kzalloc(sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + data->handle = sev->handle; + ret = sev_issue_cmd(kvm, SEV_CMD_SEND_FINISH, data, &argp->error); + + kfree(data); + return ret; +} + static int svm_mem_enc_op(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -7449,6 +7469,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp) case KVM_SEV_SEND_UPDATE_DATA: r = sev_send_update_data(kvm, &sev_cmd); break; + case KVM_SEV_SEND_FINISH: + r = sev_send_finish(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; From patchwork Mon Mar 30 01:34:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 11464385 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id AD6D01805 for ; Mon, 30 Mar 2020 01:35:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8C35F2076A for ; Mon, 30 Mar 2020 01:35:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="ybkrGYvF" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728355AbgC3Bf1 (ORCPT ); Sun, 29 Mar 2020 21:35:27 -0400 Received: from mail-eopbgr750057.outbound.protection.outlook.com ([40.107.75.57]:6492 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727815AbgC3Bf1 (ORCPT ); Sun, 29 Mar 2020 21:35:27 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fzz5C4ruMsQ5UubKX0KU0kkgI8E/jUjBAWFC4k/8j2bncpUvrbo3aSYOemZoch1C4ci9VMxoj3SgclypNCrjGtJOyF+FiSk74s0BAsYLSd9wbqdOjla/7diQS2P8za9iuVQK3W6Bzy81zOxqV98hYJ++YbZENxHXu3t6PKQlh9aR+jbCLO+mquVPFu6wNIkxqUOaiJQNMCaoeJfzrg61eRjV391qibTAu8MXgXeYmaTDyI8qyPAe8bgqV/WlplAFpMKBiaiuodwhhQJYVv9wHmbqgEElMq0d4kb+vutbDNIk5v+D7YL+m3qCn/TQAd/5F7rppD8qDdXvXtAtStIQHg== 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-SenderADCheck; bh=tONNDZE99a8KL+WQna5wYK2bH3j1EgUeZhElYDMWRis=; b=A/TG7wajIYRuccGb3pw4KCx5Puln+Np7Y8R/ZboZUd9/4tGTeljARjNxvav8eE/aSlWxnS/zch0/Xl4HY6Phg8/llU3J8zc/ZmZaHzz/XbGAVEDmQI2NnDpmPf1yIempSaEIXychSAT5eqtpc9t+yh03WhPs9gSalc8eXC2f6zQBJxLRoeTNIiFmg6cU0dGz4FmyjvZhkb75lTVxr37gbYf8DNG5QxZVDTAWE8n7SxKCkoLuQySn5QmeLt+TW0GgLyvkFydJn8k+2IgjG2rDagAUAYpQr0jD216WxTLq6qDSX3KYiMDb0dufnI16K11bjkR/K9VyffdlOuUZ/UF8XA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=tONNDZE99a8KL+WQna5wYK2bH3j1EgUeZhElYDMWRis=; b=ybkrGYvFgWZex8V3Hk2onELDFu4BUAYsH6Pel+OFWgcMDEgIy/f6BbQb8eoMSWK9gprfv6drPGAz8rsGDkVvXfRwK4cT1n/UNi3o0x43pYfns/AzJHEY7frrlpcL755ILKAhm+h905E3azATkBpQ8uKG1P0IaMeWyqwMLQ2WHIs= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Ashish.Kalra@amd.com; Received: from DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) by DM5PR12MB1387.namprd12.prod.outlook.com (2603:10b6:3:6c::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.20; Mon, 30 Mar 2020 01:35:08 +0000 Received: from DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c]) by DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c%12]) with mapi id 15.20.2856.019; Mon, 30 Mar 2020 01:35:08 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, bp@suse.de, thomas.lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, rientjes@google.com, srutherford@google.com, luto@kernel.org, brijesh.singh@amd.com Subject: [PATCH v5 04/14] KVM: SVM: Add support for KVM_SEV_RECEIVE_START command Date: Mon, 30 Mar 2020 01:34:58 +0000 Message-Id: <7753c183e9e571220fffe3663b1139c1f9030fbf.1585531159.git.ashish.kalra@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-ClientProxiedBy: SN1PR12CA0059.namprd12.prod.outlook.com (2603:10b6:802:20::30) To DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by SN1PR12CA0059.namprd12.prod.outlook.com (2603:10b6:802:20::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.18 via Frontend Transport; Mon, 30 Mar 2020 01:35:07 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: ff4e1f6b-fcc4-43de-ef8c-08d7d44a9484 X-MS-TrafficTypeDiagnostic: DM5PR12MB1387:|DM5PR12MB1387: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2331; X-Forefront-PRVS: 0358535363 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR12MB1386.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(10009020)(4636009)(39860400002)(376002)(346002)(366004)(136003)(396003)(186003)(66574012)(66556008)(6666004)(66476007)(86362001)(6916009)(66946007)(316002)(4326008)(7696005)(52116002)(5660300002)(6486002)(16526019)(26005)(36756003)(478600001)(7416002)(81166006)(81156014)(8936002)(2616005)(8676002)(2906002)(956004)(136400200001);DIR:OUT;SFP:1101; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 1pdh9XFQ4EF2IdYQBFqrpv8IqLapH9BNScZXJNxgcs7uYDeI0JlYXXNUxsk51HZPNVWsSEJfNYdw4Z8DtiUByvipP74S2vC4Abj8CogCdsw/lLg9DA3B5hf0QlUxFGAMRq69rU5Y8cKbwI+qu3+o6lrRGipCTsHS5grkL5Wjh7quLTZfQHHx6fABbWPwqZTLnXl0o8/mCjLjHyX+NzGiusT7x6xcFD8CcOE9RTkENExrr70/6GuIYydc4f5YpOPya/ftsmeqhZLWVvABLO09ZVWNR9MDSNK6hotMFJIYndeHBRpEPEDmpZ1Ptx8gE5JQx+DG8pYzv/s2byAm82v87+tu6apklWnJPQyTn5p6GedqlccxUSqJ1StdQYwPPl+UrtKZHHtILn2jrW0oZZMrVMImLClMhdZf4GAZkcWZ0uOnMWDrHA5C3k2o3GqDmnuTbskEsiL1fm2wa6cIKzlog61+BapsOeZfJApkWUptoM6IMPW5d5HgB7GANnJT+MoT X-MS-Exchange-AntiSpam-MessageData: OFPUZBswTZzLBDO+euGJvK3cz2NGtXwCBpNgXCpM3XGB4k2P7a2K22dQuNOqCUnBOCz4Q17MpAFWorLkCJFS+BHzehbuuu2hWy38ZfWj0Wvtvud/o3mRVqwRclxlYM70fCdTePlXELYT0PQ+ZXan2A== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: ff4e1f6b-fcc4-43de-ef8c-08d7d44a9484 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Mar 2020 01:35:08.2502 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: tHHktS/JzH2bTViqo7sybsz2eakF1Wk2H6dqHpniz2QGbs4hcQJ47vLyMzUn0LMqxrzviifth2gyjFqEJ+E/jw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1387 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Brijesh Singh The command is used to create the encryption context for an incoming SEV guest. The encryption context can be later used by the hypervisor to import the incoming data into the SEV guest memory space. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: "H. Peter Anvin" Cc: Paolo Bonzini Cc: "Radim Krčmář" Cc: Joerg Roedel Cc: Borislav Petkov Cc: Tom Lendacky Cc: x86@kernel.org Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org Reviewed-by: Steve Rutherford Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- .../virt/kvm/amd-memory-encryption.rst | 29 +++++++ arch/x86/kvm/svm.c | 81 +++++++++++++++++++ include/uapi/linux/kvm.h | 9 +++ 3 files changed, 119 insertions(+) diff --git a/Documentation/virt/kvm/amd-memory-encryption.rst b/Documentation/virt/kvm/amd-memory-encryption.rst index a45dcb5f8687..ef1f1f3a5b40 100644 --- a/Documentation/virt/kvm/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/amd-memory-encryption.rst @@ -322,6 +322,35 @@ issued by the hypervisor to delete the encryption context. Returns: 0 on success, -negative on error +13. KVM_SEV_RECEIVE_START +------------------------ + +The KVM_SEV_RECEIVE_START command is used for creating the memory encryption +context for an incoming SEV guest. To create the encryption context, the user must +provide a guest policy, the platform public Diffie-Hellman (PDH) key and session +information. + +Parameters: struct kvm_sev_receive_start (in/out) + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_receive_start { + __u32 handle; /* if zero then firmware creates a new handle */ + __u32 policy; /* guest's policy */ + + __u64 pdh_uaddr; /* userspace address pointing to the PDH key */ + __u32 dh_len; + + __u64 session_addr; /* userspace address which points to the guest session information */ + __u32 session_len; + }; + +On success, the 'handle' field contains a new handle and on error, a negative value. + +For more details, see SEV spec Section 6.12. + References ========== diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index 71a4cb3b817d..038b47685733 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -7419,6 +7419,84 @@ static int sev_send_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) return ret; } +static int sev_receive_start(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_receive_start *start; + struct kvm_sev_receive_start params; + int *error = &argp->error; + void *session_data; + void *pdh_data; + int ret; + + if (!sev_guest(kvm)) + return -ENOTTY; + + /* Get parameter from the userspace */ + if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, + sizeof(struct kvm_sev_receive_start))) + return -EFAULT; + + /* some sanity checks */ + if (!params.pdh_uaddr || !params.pdh_len || + !params.session_uaddr || !params.session_len) + return -EINVAL; + + pdh_data = psp_copy_user_blob(params.pdh_uaddr, params.pdh_len); + if (IS_ERR(pdh_data)) + return PTR_ERR(pdh_data); + + session_data = psp_copy_user_blob(params.session_uaddr, + params.session_len); + if (IS_ERR(session_data)) { + ret = PTR_ERR(session_data); + goto e_free_pdh; + } + + ret = -ENOMEM; + start = kzalloc(sizeof(*start), GFP_KERNEL); + if (!start) + goto e_free_session; + + start->handle = params.handle; + start->policy = params.policy; + start->pdh_cert_address = __psp_pa(pdh_data); + start->pdh_cert_len = params.pdh_len; + start->session_address = __psp_pa(session_data); + start->session_len = params.session_len; + + /* create memory encryption context */ + ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_RECEIVE_START, start, + error); + if (ret) + goto e_free; + + /* Bind ASID to this guest */ + ret = sev_bind_asid(kvm, start->handle, error); + if (ret) + goto e_free; + + params.handle = start->handle; + if (copy_to_user((void __user *)(uintptr_t)argp->data, + ¶ms, sizeof(struct kvm_sev_receive_start))) { + ret = -EFAULT; + sev_unbind_asid(kvm, start->handle); + goto e_free; + } + + sev->handle = start->handle; + sev->fd = argp->sev_fd; + +e_free: + kfree(start); +e_free_session: + kfree(session_data); +e_free_pdh: + kfree(pdh_data); + + return ret; +} + static int svm_mem_enc_op(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -7472,6 +7550,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp) case KVM_SEV_SEND_FINISH: r = sev_send_finish(kvm, &sev_cmd); break; + case KVM_SEV_RECEIVE_START: + r = sev_receive_start(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index d9dc81bb9c55..74764b9db5fa 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1579,6 +1579,15 @@ struct kvm_sev_send_update_data { __u32 trans_len; }; +struct kvm_sev_receive_start { + __u32 handle; + __u32 policy; + __u64 pdh_uaddr; + __u32 pdh_len; + __u64 session_uaddr; + __u32 session_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 Mar 30 01:35:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 11464387 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E28FA159A for ; Mon, 30 Mar 2020 01:35:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B77622076A for ; Mon, 30 Mar 2020 01:35:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="WjYdmOhY" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728330AbgC3Bfa (ORCPT ); Sun, 29 Mar 2020 21:35:30 -0400 Received: from mail-eopbgr750057.outbound.protection.outlook.com ([40.107.75.57]:6492 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728333AbgC3Bfa (ORCPT ); Sun, 29 Mar 2020 21:35:30 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ffaQ+cSfgZAJ45uVBCqcIbklMrdV+5Dwj1n7OUV2JNobuVoNNVtojib+2ahk3nTHzO9zGvlYcnjsohuC5Uu8LgXwYusArnQvwQYXMWmzkmMFhLQpMPjr0YbI9jZjZJwBCF26c1z/o6ehxhFZ0M3wQ7d7fkBP3TeDgnn6QfRRTD/UpHSEciMLBkCYqs79m68LXfCC9AuY1pg7PdiDGIW1uKA0rIq8df+L21KqOW7Mi199Lq8I7V8GdBk8gpZlM4iR9QbsQfsKkA1U0kmQ3Sj5EnW8N2sms/rmIDT1EfvwXs9X85bfo0S+8dS1Z+avpXvG1SqD+4JW36k+20YId90/jw== 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-SenderADCheck; bh=rjNjizM2G0T7+C7VEkNBPnrtlTTrAO5qxWvmKQ4fvJQ=; b=ionaYu/+VVyvRVXxMISiBsSTlo646H8d+/VpIP/GrS4Kxh4fY3PCM3rxwzt9gU+uJFRlmAEidj5nvkV/qJL+1m5qBN177pQiJvhR5+AxlPHPeDjXcNg8lmkFwFJ/KH2qRCmCmw+pkzTZAY+XJ3m1SGjYxQJFwSg0ccgYz/R/tGDpJYtPuYR2Y3eWdZ5oACpOXMChVs0cCQgAWjFRx0izZbTc4Hr02SEQUIMq9CgtEJmSTn4qdKtVv7OJcoxnl0Vi1A+inbrKVraxRd47fGZBXNLKhRigd2oENRsyaHUWui5EiBDdFVoLy8hM6b1pAjIa1NnTE4Kq0WrtpXXG73q0Xg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=rjNjizM2G0T7+C7VEkNBPnrtlTTrAO5qxWvmKQ4fvJQ=; b=WjYdmOhY5YmBHJVkAU/01hJctZLapRTDcSx2cPixgeZDEld9A5O/zWWQEzy4UFA/NW5NAQEyxIQEEILvYWxlenHOtWheoAvrz5iLr4jgATtX+aC1sc6UQnFOe4dgXwlHL8RhWamjwektFeg/0oXnPTr7IfF+nTCc5tm06WmTLqg= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Ashish.Kalra@amd.com; Received: from DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) by DM5PR12MB1387.namprd12.prod.outlook.com (2603:10b6:3:6c::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.20; Mon, 30 Mar 2020 01:35:24 +0000 Received: from DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c]) by DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c%12]) with mapi id 15.20.2856.019; Mon, 30 Mar 2020 01:35:24 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, bp@suse.de, thomas.lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, rientjes@google.com, srutherford@google.com, luto@kernel.org, brijesh.singh@amd.com Subject: [PATCH v5 05/14] KVM: SVM: Add KVM_SEV_RECEIVE_UPDATE_DATA command Date: Mon, 30 Mar 2020 01:35:15 +0000 Message-Id: <871a1e89a4dff59f50d9c264c6d9a4cfd0eab50f.1585531159.git.ashish.kalra@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-ClientProxiedBy: SN1PR12CA0065.namprd12.prod.outlook.com (2603:10b6:802:20::36) To DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by SN1PR12CA0065.namprd12.prod.outlook.com (2603:10b6:802:20::36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.18 via Frontend Transport; Mon, 30 Mar 2020 01:35:23 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 89512194-7c45-4c93-a713-08d7d44a9e64 X-MS-TrafficTypeDiagnostic: DM5PR12MB1387:|DM5PR12MB1387: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6108; X-Forefront-PRVS: 0358535363 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR12MB1386.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(10009020)(4636009)(39860400002)(376002)(346002)(366004)(136003)(396003)(186003)(66574012)(66556008)(6666004)(66476007)(86362001)(6916009)(66946007)(316002)(4326008)(7696005)(52116002)(5660300002)(6486002)(16526019)(26005)(36756003)(478600001)(7416002)(81166006)(81156014)(8936002)(2616005)(8676002)(2906002)(956004)(136400200001);DIR:OUT;SFP:1101; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: uhVIrBe0zcxJEPOBgZhnBdVaCTSGsUMM5VydSB7uNSokat/J7JUUi1pb87sXurG5DXSpaGF2aFkzvkVByDbm50gjznk2IfqZoupjp7lsgsMFtiZp3oD4+wa1iCHqI4f+x6lh4SbVge3uygMDv0RMHQCG0zaOwwF9HMiPv6SYzkM3UCrIm6aTpkR5LdSYcPVk36xlmpv/y+252vwODZ5j+OgmurhNws75CvpuX8KJFzdDltAmqctU1Wn/i9VoIZ94kjWQzPsfqpe5ytOb2UDcBP7CUK3L9vS+B5aMlU5Sln5EPjPxcjDSvfC/oM4FxoTED6hQvk3jGgoBFdif0stzE6MCE4Y0NJrtY6WFmwDodwkerNoXo2yAClrOR+8GzKk10dd12Zq6Y2wOOhByiV448RONWpW7eSNz+8cXSOLdP+yDYotXnL3V/XBqfS6QiVC11ph5yn/AiG6zjqUFdTpV6l6y3jeam1jYv1UDyMR63kZ3THGDtCVQKbeNglrv5q52 X-MS-Exchange-AntiSpam-MessageData: SKM8OLeA+BLZL/x7sebTEjb0z1nUjD0l7oeSNnMljLcW7nnp6ouITm6jGlq/2qM+oHQd/74SZv6wOEJfV9KMVWd698YOBCDcUtYHkShHo7QWDzBb0FUuwDaXIKAIWWHasFmMeXwklIEpSDuAnEhrkA== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 89512194-7c45-4c93-a713-08d7d44a9e64 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Mar 2020 01:35:24.7978 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: T/nOYS+nxDlnBgnVz5x30EYtqSc5CVYmIh1WRljwQVO1SatD4T3n3Gss/RN3KDlvUOxT52OBvNI9dp5ljhDMLQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1387 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Brijesh Singh The command is used for copying the incoming buffer into the SEV guest memory space. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: "H. Peter Anvin" Cc: Paolo Bonzini Cc: "Radim Krčmář" Cc: Joerg Roedel Cc: Borislav Petkov Cc: Tom Lendacky Cc: x86@kernel.org Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- .../virt/kvm/amd-memory-encryption.rst | 24 ++++++ arch/x86/kvm/svm.c | 79 +++++++++++++++++++ include/uapi/linux/kvm.h | 9 +++ 3 files changed, 112 insertions(+) diff --git a/Documentation/virt/kvm/amd-memory-encryption.rst b/Documentation/virt/kvm/amd-memory-encryption.rst index ef1f1f3a5b40..554aa33a99cc 100644 --- a/Documentation/virt/kvm/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/amd-memory-encryption.rst @@ -351,6 +351,30 @@ On success, the 'handle' field contains a new handle and on error, a negative va For more details, see SEV spec Section 6.12. +14. KVM_SEV_RECEIVE_UPDATE_DATA +---------------------------- + +The KVM_SEV_RECEIVE_UPDATE_DATA command can be used by the hypervisor to copy +the incoming buffers into the guest memory region with encryption context +created during the KVM_SEV_RECEIVE_START. + +Parameters (in): struct kvm_sev_receive_update_data + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_launch_receive_update_data { + __u64 hdr_uaddr; /* userspace address containing the packet header */ + __u32 hdr_len; + + __u64 guest_uaddr; /* the destination guest memory region */ + __u32 guest_len; + + __u64 trans_uaddr; /* the incoming buffer memory region */ + __u32 trans_len; + }; + References ========== diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index 038b47685733..5fc5355536d7 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -7497,6 +7497,82 @@ static int sev_receive_start(struct kvm *kvm, struct kvm_sev_cmd *argp) return ret; } +static int sev_receive_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct kvm_sev_receive_update_data params; + struct sev_data_receive_update_data *data; + void *hdr = NULL, *trans = NULL; + struct page **guest_page; + unsigned long n; + int ret, offset; + + if (!sev_guest(kvm)) + return -EINVAL; + + if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, + sizeof(struct kvm_sev_receive_update_data))) + return -EFAULT; + + if (!params.hdr_uaddr || !params.hdr_len || + !params.guest_uaddr || !params.guest_len || + !params.trans_uaddr || !params.trans_len) + return -EINVAL; + + /* Check if we are crossing the page boundary */ + offset = params.guest_uaddr & (PAGE_SIZE - 1); + if ((params.guest_len + offset > PAGE_SIZE)) + return -EINVAL; + + hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len); + if (IS_ERR(hdr)) + return PTR_ERR(hdr); + + trans = psp_copy_user_blob(params.trans_uaddr, params.trans_len); + if (IS_ERR(trans)) { + ret = PTR_ERR(trans); + goto e_free_hdr; + } + + ret = -ENOMEM; + data = kzalloc(sizeof(*data), GFP_KERNEL); + if (!data) + goto e_free_trans; + + data->hdr_address = __psp_pa(hdr); + data->hdr_len = params.hdr_len; + data->trans_address = __psp_pa(trans); + data->trans_len = params.trans_len; + + /* Pin guest memory */ + ret = -EFAULT; + guest_page = sev_pin_memory(kvm, params.guest_uaddr & PAGE_MASK, + PAGE_SIZE, &n, 0); + if (!guest_page) + goto e_free; + + /* The RECEIVE_UPDATE_DATA command requires C-bit to be always set. */ + data->guest_address = (page_to_pfn(guest_page[0]) << PAGE_SHIFT) + + offset; + data->guest_address |= sev_me_mask; + data->guest_len = params.guest_len; + data->handle = sev->handle; + + ret = sev_issue_cmd(kvm, SEV_CMD_RECEIVE_UPDATE_DATA, data, + &argp->error); + + sev_unpin_memory(kvm, guest_page, n); + +e_free: + kfree(data); +e_free_trans: + kfree(trans); +e_free_hdr: + kfree(hdr); + + return ret; +} + static int svm_mem_enc_op(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -7553,6 +7629,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp) case KVM_SEV_RECEIVE_START: r = sev_receive_start(kvm, &sev_cmd); break; + case KVM_SEV_RECEIVE_UPDATE_DATA: + r = sev_receive_update_data(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 74764b9db5fa..4e80c57a3182 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1588,6 +1588,15 @@ struct kvm_sev_receive_start { __u32 session_len; }; +struct kvm_sev_receive_update_data { + __u64 hdr_uaddr; + __u32 hdr_len; + __u64 guest_uaddr; + __u32 guest_len; + __u64 trans_uaddr; + __u32 trans_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 Mar 30 01:35:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 11464389 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4D92A912 for ; Mon, 30 Mar 2020 01:35:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 21C2A2076A for ; Mon, 30 Mar 2020 01:35:58 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="XsWgcwxE" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728593AbgC3Bfv (ORCPT ); Sun, 29 Mar 2020 21:35:51 -0400 Received: from mail-eopbgr750052.outbound.protection.outlook.com ([40.107.75.52]:4518 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728324AbgC3Bfu (ORCPT ); Sun, 29 Mar 2020 21:35:50 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QrVHEurZTAgqsJDhkxUtM694+t/xQ81IdZwG/nChSi4c3l47/1ASbOqxv4OHR20AUoKGoMlcnk3HiVW6B835Lmt0RM+91E2avdIuwT/xxgrcj1ysgMKCLBuBRW8flEnZ052Ze4wnqMzUDV10Kazg8a7edB4HkIllxOvwlBhoHCDLIPf6uE+CqVWL9NkrcV619TRkHlEtuH5QXghtpXQrC0w1dWVK6kHw6lJ56nfuidpCm8lI+IZCydt04u/H5BfwKgtq1/Hr28Q0JvO/5ESpP6P65YdC1CA1fWVCqwfR0bV228fYQGPj/3XtwIN1l9tRmGnCpVDAxFjHGPQ9ydcWMA== 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-SenderADCheck; bh=W+0q6Nyh9f1u3yEndkck8hsJTicRQCuicnRxNyF547g=; b=CpBt81T1ubtbIrG/VUBvi+mORmP11qTFidWDgVh0oqECt5fGRG72riYLsGNj6HIij1cbJ6BdZAM6P/Q8cRJM6X6u+VBqJGTtLZEedizFpjDFFDfsK83Ecrpbto8GNOBH+O4/nB1A9VyW+8SOxczJY9pcTfCjv/jVJUyPgjlhD74H46FsDTIp/a0VphkAVgn+0KP9sScR/j7+puLeX7+tQsx9TxKSd2WDvHvuqq3PATjXkQ6MCLQH8qQy72kLVhx5A7R53WQvLcic5g1x5nmYqGi2f0EXqFrIkeqEh07JzgBRvMWFREf8cvUpHayPqycYj3r3d019JWeiVo7Nhl+8AA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=W+0q6Nyh9f1u3yEndkck8hsJTicRQCuicnRxNyF547g=; b=XsWgcwxE0Fe9lYXaxZVvgoDj9smym1RZGNCo+ey5FWCWFUVPWUwhDyoDIQ7elXcucous159H1fOAkfhiHDfqhWFmyeaMId6Dl4gVup4NC+JjfjjtPTKlL5UCYAmQpsAD6vYy5TsPXfvV0+VPYNSsSFSjZrWnUh0CRvW07nqQGr0= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Ashish.Kalra@amd.com; Received: from DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) by DM5PR12MB1387.namprd12.prod.outlook.com (2603:10b6:3:6c::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.20; Mon, 30 Mar 2020 01:35:40 +0000 Received: from DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c]) by DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c%12]) with mapi id 15.20.2856.019; Mon, 30 Mar 2020 01:35:40 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, bp@suse.de, thomas.lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, rientjes@google.com, srutherford@google.com, luto@kernel.org, brijesh.singh@amd.com Subject: [PATCH v5 06/14] KVM: SVM: Add KVM_SEV_RECEIVE_FINISH command Date: Mon, 30 Mar 2020 01:35:30 +0000 Message-Id: <0f8a2125c7acb7b38fc51a044a8088e8baa45e3d.1585531159.git.ashish.kalra@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-ClientProxiedBy: SN1PR12CA0068.namprd12.prod.outlook.com (2603:10b6:802:20::39) To DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by SN1PR12CA0068.namprd12.prod.outlook.com (2603:10b6:802:20::39) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.19 via Frontend Transport; Mon, 30 Mar 2020 01:35:39 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 1e790265-3ce5-435a-2585-08d7d44aa7e2 X-MS-TrafficTypeDiagnostic: DM5PR12MB1387:|DM5PR12MB1387: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-Forefront-PRVS: 0358535363 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR12MB1386.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(10009020)(4636009)(39860400002)(376002)(346002)(366004)(136003)(396003)(186003)(66574012)(66556008)(6666004)(66476007)(86362001)(6916009)(66946007)(316002)(4326008)(7696005)(52116002)(5660300002)(6486002)(16526019)(26005)(36756003)(478600001)(7416002)(81166006)(81156014)(8936002)(2616005)(8676002)(2906002)(956004)(136400200001);DIR:OUT;SFP:1101; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: OUTb1YglYWLvc+faLujq2E+g4l5fONu8aW/6jW5ev0PXnShJ62jsfxbrIa+bv+2Tqjxrn2Tf0SeHQwU5b5Exzk0AkzkT+KeXcMEEG06bezQgp3/wq+kGbnByb0mi5C1TVXdcz6NmwgffRnlLca5iuWJOworUGsKYD8ZqjZR8KA6nccMtsBxWIDoytJ4M3OWsnfK+6Eu4I97IsbnCI6UPVcCiexkitRIJGy+a5TR4vlB1sRtcJGf6g7DFiNESIWaMNbjWEsZH14TnpbhLH3K1cjixNRq54dtTbjxn3TlCSYzVfGmXwi6x5k98paYV0rjkbrcTROTVlHVK6WSAhmVDaY++y5/xhnwlutmJe3B3rGlVL7hoQou2BbUXlE/eWRodaVOfzBSC1O4ivY6DhkkiQREomtVOILipTc1+mFKZi9po3BF04Ic19VyU73CSO3/2kxiBRObxkRjRFWqmDZU5Sw2Rox4rZ+xbVDn6SsWrkjNHZA6cAMX+vm9AWN/SGG7q X-MS-Exchange-AntiSpam-MessageData: H58qb3eQtglsEdv6QuxK/7X4UDBlQ+fH8XhSOM4A0F8n9hqnuWdOHUaBKlSSz0PwdA2N9qhtqLnycgTO9RjJKp26GxwFEuaQcSIYrc2VMVgSp0Epezlz8HUoNsFeNm+XF18jsqvQ7g6SXPFIzb0SsQ== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1e790265-3ce5-435a-2585-08d7d44aa7e2 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Mar 2020 01:35:40.7168 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: WuqOAsNbGkM21Dgj3KyWtIOrkU5MljIA+W8dAdN0ZgQRIWp4yvbNk+nN/h0PLyL5qXvVrIjEPo6IG5Yvm56axg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1387 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Brijesh Singh The command finalize the guest receiving process and make the SEV guest ready for the execution. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: "H. Peter Anvin" Cc: Paolo Bonzini Cc: "Radim Krčmář" Cc: Joerg Roedel Cc: Borislav Petkov Cc: Tom Lendacky Cc: x86@kernel.org Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- .../virt/kvm/amd-memory-encryption.rst | 8 +++++++ arch/x86/kvm/svm.c | 23 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/Documentation/virt/kvm/amd-memory-encryption.rst b/Documentation/virt/kvm/amd-memory-encryption.rst index 554aa33a99cc..93cd95d9a6c0 100644 --- a/Documentation/virt/kvm/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/amd-memory-encryption.rst @@ -375,6 +375,14 @@ Returns: 0 on success, -negative on error __u32 trans_len; }; +15. KVM_SEV_RECEIVE_FINISH +------------------------ + +After completion of the migration flow, the KVM_SEV_RECEIVE_FINISH command can be +issued by the hypervisor to make the guest ready for execution. + +Returns: 0 on success, -negative on error + References ========== diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index 5fc5355536d7..7c2721e18b06 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -7573,6 +7573,26 @@ static int sev_receive_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) return ret; } +static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_receive_finish *data; + int ret; + + if (!sev_guest(kvm)) + return -ENOTTY; + + data = kzalloc(sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + data->handle = sev->handle; + ret = sev_issue_cmd(kvm, SEV_CMD_RECEIVE_FINISH, data, &argp->error); + + kfree(data); + return ret; +} + static int svm_mem_enc_op(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -7632,6 +7652,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp) case KVM_SEV_RECEIVE_UPDATE_DATA: r = sev_receive_update_data(kvm, &sev_cmd); break; + case KVM_SEV_RECEIVE_FINISH: + r = sev_receive_finish(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; From patchwork Mon Mar 30 01:35:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 11464397 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id AEFC481 for ; Mon, 30 Mar 2020 01:36:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 840382082F for ; Mon, 30 Mar 2020 01:36:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="RMdJnJey" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728516AbgC3Bgf (ORCPT ); Sun, 29 Mar 2020 21:36:35 -0400 Received: from mail-eopbgr750077.outbound.protection.outlook.com ([40.107.75.77]:21057 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728176AbgC3Bgf (ORCPT ); Sun, 29 Mar 2020 21:36:35 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EMSb66hNYPOsYx6LsMXGtYoI0NJeC+i0/aMekiLoNjawUzcy+e8UgEnPPV6KMeCr0wcKMZN4xJiDm3pqxZYJzJSSUdF4oBDweltzhAMOezouiDauplQD1CftiYfaPl73/9FsknySurkrPiP8etiS54bc4pXKvCeSFIyikXDZkLN7zSznttayhr2tyX/RxAdDrxIz/u3/4KpnQhAAbqslKaDknpFNPuZD1s/XMMlkxygonReTSB9SrNK2LmPKXJKrfr6pNDyqlFzda8RQMGt5biJU+agvpx6KRlte5H9gBrgANOmMINDxD6+4jRkObEpP2BExFuT4Sh4B/cBW8qeHSg== 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-SenderADCheck; bh=SwLWA9i8h3stz9l2IVcM8HURce+9OCTaQnNc0VFSdHs=; b=TRuRLd4SVoEwIbXi9Idh9fi2c6uCEV2o5d6eNf/iFXadVh+T/vovGXzKjiOL+hSXPoT55PJveMvzK6RproovnRybOPnZHD/KldTI+iY+q6Rkuk7UOeggnSJwSbyvOnWMcq7itk66VVxjwDBIk5+QadCLsTbX2Pn2qou8QvReVzYK9RSVe/+3qVNSJBUZYbD1klsvylUSEmt6UNXYmmOJF4x9ZBvBPFn2oO3uFHS+yClLLhAPjxqmw9HQsRm8EVLoPP81qOkrHB7sqsVJ3eK+xJ7m/s23zG72myhr/5pkgzfYZBIO3hf3ROgbMepGt+tEkhVqShG1cpOKsZVMTN6zOQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=SwLWA9i8h3stz9l2IVcM8HURce+9OCTaQnNc0VFSdHs=; b=RMdJnJeyjDhAf3L4oXub4aXOzV1rRpYwKqJE+dp4RcEx6LEYuJZKpgrn/EJVTPnznrPHgdK3RLNeksfvc15rPGNvqy8hLaGcyN8QP7ecQw4PDuLxu1EvzLshpMt220yjIJSfhf/ozwo98LqvTNfJkGDmYt8RHrPG9IRXVRcOzdU= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Ashish.Kalra@amd.com; Received: from DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) by DM5PR12MB1387.namprd12.prod.outlook.com (2603:10b6:3:6c::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.20; Mon, 30 Mar 2020 01:35:58 +0000 Received: from DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c]) by DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c%12]) with mapi id 15.20.2856.019; Mon, 30 Mar 2020 01:35:58 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, bp@suse.de, thomas.lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, rientjes@google.com, srutherford@google.com, luto@kernel.org, brijesh.singh@amd.com Subject: [PATCH v5 07/14] KVM: x86: Add AMD SEV specific Hypercall3 Date: Mon, 30 Mar 2020 01:35:48 +0000 Message-Id: <6dda7016ab64490ac3d8e74f461f9f3d0ee3fc88.1585531159.git.ashish.kalra@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-ClientProxiedBy: DM5PR06CA0058.namprd06.prod.outlook.com (2603:10b6:3:37::20) To DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by DM5PR06CA0058.namprd06.prod.outlook.com (2603:10b6:3:37::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.18 via Frontend Transport; Mon, 30 Mar 2020 01:35:57 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 41ac7aea-5fbd-4aba-7db0-08d7d44ab233 X-MS-TrafficTypeDiagnostic: DM5PR12MB1387:|DM5PR12MB1387: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3513; X-Forefront-PRVS: 0358535363 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR12MB1386.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(10009020)(4636009)(39860400002)(376002)(346002)(366004)(136003)(396003)(186003)(66556008)(6666004)(66476007)(86362001)(6916009)(66946007)(316002)(4326008)(7696005)(52116002)(5660300002)(6486002)(16526019)(26005)(36756003)(478600001)(7416002)(81166006)(81156014)(8936002)(2616005)(8676002)(2906002)(956004)(136400200001);DIR:OUT;SFP:1101; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: GuB3eHnoVP5143bM7kEXZK9KX3JfghUgkamiuDgkDQSFQ2wr9P2S3ri0rSAundpUtDo5p5KRmhb4c0elxOAbND05mtMXejo3Ks/uytSDyMAjH13Q7o7uDKchcgc+ODdYEJQeOUDTnX+MXqA1y34flr2qQxJUuwSZW+JuhEekoDIZIx0v1gDi/cw+/LGU6vgOLjnBIGKwNBA8HHx2eWeGri4tha6ojaBxWADK7soDzcuHr7kSsm+7g+qqleEGLR3r8PvKrnhofIIidczg1cYET0mbZN0aceIbhnPSDtNPZuIPRJe9Hlm0/CNphvJecz7aqg7AZbOwbbWUxiXucK7Yki/oK6c21A8JOMC2XtyphHmUvkzdi5iNeaez9brKwSphmyHTymzUHK3wL1JzUqTXCo4rn8f+fxBEnBEYgFZ8NfsDYd3GJzwu0ZeV0eqRajlxxhcgdu/db3bJBES5WAr02sbrVVrL5GC45BaB0AfrUtjL/aXd+tSGSi4mGR6SEsUR X-MS-Exchange-AntiSpam-MessageData: v0I7+m00drJ3Ml2O4U5JxNOt8bHIaoo27XM9CArvCLsL+TeZhPOKWLxM0IVI6aLLKwaXjrWVs3p6gS3UJVho3MeAF0VcSd514GDn3odbM15QubJWbNtue3trQ+VC6hawO+ze8fh6RZnKz23in0M5IQ== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 41ac7aea-5fbd-4aba-7db0-08d7d44ab233 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Mar 2020 01:35:58.0511 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 9VscI1GF2fKHg+0zFf7j5IppgdT47KzSsLfYry83WCd+NKmM22Oz2MtxVurDt9L9ow3mzXRLtQ8ZieKvmrRSCA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1387 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Brijesh Singh KVM hypercall framework relies on alternative framework to patch the VMCALL -> VMMCALL on AMD platform. If a hypercall is made before apply_alternative() is called then it defaults to VMCALL. The approach works fine on non SEV guest. A VMCALL would causes #UD, and hypervisor will be able to decode the instruction and do the right things. But when SEV is active, guest memory is encrypted with guest key and hypervisor will not be able to decode the instruction bytes. Add SEV specific hypercall3, it unconditionally uses VMMCALL. The hypercall will be used by the SEV guest to notify encrypted pages to the hypervisor. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: "H. Peter Anvin" Cc: Paolo Bonzini Cc: "Radim Krčmář" Cc: Joerg Roedel Cc: Borislav Petkov Cc: Tom Lendacky Cc: x86@kernel.org Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- arch/x86/include/asm/kvm_para.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/arch/x86/include/asm/kvm_para.h b/arch/x86/include/asm/kvm_para.h index 9b4df6eaa11a..6c09255633a4 100644 --- a/arch/x86/include/asm/kvm_para.h +++ b/arch/x86/include/asm/kvm_para.h @@ -84,6 +84,18 @@ static inline long kvm_hypercall4(unsigned int nr, unsigned long p1, return ret; } +static inline long kvm_sev_hypercall3(unsigned int nr, unsigned long p1, + unsigned long p2, unsigned long p3) +{ + long ret; + + asm volatile("vmmcall" + : "=a"(ret) + : "a"(nr), "b"(p1), "c"(p2), "d"(p3) + : "memory"); + return ret; +} + #ifdef CONFIG_KVM_GUEST bool kvm_para_available(void); unsigned int kvm_arch_para_features(void); From patchwork Mon Mar 30 01:36:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 11464393 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9B5A4912 for ; Mon, 30 Mar 2020 01:36:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6F41F2076A for ; Mon, 30 Mar 2020 01:36:38 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="Sa3vyMi8" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728324AbgC3Bgh (ORCPT ); Sun, 29 Mar 2020 21:36:37 -0400 Received: from mail-eopbgr750077.outbound.protection.outlook.com ([40.107.75.77]:21057 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728124AbgC3Bgg (ORCPT ); Sun, 29 Mar 2020 21:36:36 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FFpeZkBvTxoqSluhVnZxI6TyxX4R8/9FWjCceXGfjFemOoqQoZEUQCyYoxEJi4M2fVq55/q5U0HaVtbG4QvncCIrxBm9VDYujteI4WQwgWJSKROYq6FtnCS4UD5HZjBJn/7F+Nizdo3BOgXMirPYeV+zK2PPqRFt/otnrMKcofJe2DUcGW63S3cbiQxCWLqxzjr3xJKFksEZh6HNN2pvNJHiBDcEb5CAUDp8SxhycRaeK+PdN/qwO1Ufn7KoOm6am3tFHZ7uYPbpOExLWnBKqCLOA7xoe0TdTtBjnLgimC32VODwp/t48OhuER3yfOtGNf+F41zE+6//mhKbqxBd0Q== 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-SenderADCheck; bh=3T7yt/mXrOiz8oubCSe2Ke91Lk7uJ1sie6miTgVNzM8=; b=eo+5iijBrVJV+6Qs2b7JOV9CdJKZKqfKNSoyDdOMoUfD5HKNuxfPixQdmBxBhV7MrMT2zGtk3EfjC4IRjtH4aRw/4IxaBcmeFCWbf3KdvekO32xsnQsC3O/sRZG02nAYG8C6P6xsOxZC7PiUepBqlsGxSf4u6etdVQ+EyI3mRvss/v6N8gCynOwl1JSCDBizeqZDnCoAlvCyA0qHZP1HQLUHyxaT06KCVFjbZP1TUBNIsDwuI6oo4QikCNGoTo2I1PSDcAb5WTJWoYdpZdlwRftrVt7KZmq2BlzD7w4kiFCkOJYM1SIOKsULNzaowVVVMW5sbQEzou57nwq15rnb+w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=3T7yt/mXrOiz8oubCSe2Ke91Lk7uJ1sie6miTgVNzM8=; b=Sa3vyMi8963e2t0/nrjFkqhitfcifEeAtt+tHc6YFZqcDFjVC50Cmd71NQiN3mAsmkhGoXpJcOXh9yd9K3texhsuGXYJ/pstfCpoHNAG4ikipJqvVTv9EAUvIuNc2q/LmgujE4igaPgGN3QIPxmCT3izRKSuzD5sPPaXZfMCR2c= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Ashish.Kalra@amd.com; Received: from DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) by DM5PR12MB1387.namprd12.prod.outlook.com (2603:10b6:3:6c::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.20; Mon, 30 Mar 2020 01:36:15 +0000 Received: from DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c]) by DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c%12]) with mapi id 15.20.2856.019; Mon, 30 Mar 2020 01:36:15 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, bp@suse.de, thomas.lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, rientjes@google.com, srutherford@google.com, luto@kernel.org, brijesh.singh@amd.com Subject: [PATCH v5 08/14] KVM: X86: Introduce KVM_HC_PAGE_ENC_STATUS hypercall Date: Mon, 30 Mar 2020 01:36:06 +0000 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-ClientProxiedBy: DM5PR06CA0060.namprd06.prod.outlook.com (2603:10b6:3:37::22) To DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by DM5PR06CA0060.namprd06.prod.outlook.com (2603:10b6:3:37::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.20 via Frontend Transport; Mon, 30 Mar 2020 01:36:14 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 61de0e7b-8382-4a51-5941-08d7d44abc64 X-MS-TrafficTypeDiagnostic: DM5PR12MB1387:|DM5PR12MB1387: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:323; X-Forefront-PRVS: 0358535363 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR12MB1386.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(10009020)(4636009)(39860400002)(376002)(346002)(366004)(136003)(396003)(186003)(66574012)(66556008)(6666004)(66476007)(86362001)(6916009)(66946007)(316002)(4326008)(7696005)(52116002)(5660300002)(6486002)(16526019)(26005)(36756003)(478600001)(7416002)(81166006)(81156014)(8936002)(2616005)(8676002)(2906002)(956004)(136400200001);DIR:OUT;SFP:1101; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qiqLbbm6n+uU9bvrpba3NABn+mQ5zRYUkJEq4IBxcYXuWbeQmF1iqt6CalgkdXxD8qHBR/UV+BHDep00wwg4iDVUQdPrzO00QVAc5podgy2JAuuz6PcRsrZUVO7Pk11SBhrJLSNQvw/QA3o68gm7d+tljYpSgdm3YhTk40kD1ceyfAO2Xzf4/QNVx+Vbid/5H9lZ23k4FutOw6C7o620EOpjQXArfMiODQup3T3OmttxQ6iCONXZ2UIlaLr49i+6owlBAar2tYxCJFRcmDxvyUpfD6vRxh6UTBBeHfCcXS8Par24XIwN7Q/W+qpUGAFi/qlkGt8Vv/QN4cm3pLfl4W9VoXgxhS7QhzIj5W7VX91u+WleG90mSdY1ojCTNgsD41KMIe5PQ+EOMzMCd1/UuWNuHvnVPnAndUaolaOYNiDeIwzt6tKS0iuFQlZTn7MjRTdYDDQtIeLhKsVGuyA5ySphsBZCjL+urIWwUAep5EfUxAFR5wROWM1mE0e3NFnX X-MS-Exchange-AntiSpam-MessageData: AGnF9L0LRc5TnRaZW3IkOGc/0t/cRpIAyLB9wIbctCrvN/UPPJ1IsdPKE4RxOsoHhsScWk8ASVcD37m3MS355ES0qrxq+Lv21h4n6zXiY1mw9IwbNQuynESkBuQTY0GH9oEtBhwpceO866gnJtHk2A== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 61de0e7b-8382-4a51-5941-08d7d44abc64 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Mar 2020 01:36:15.1525 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: IgXuK/8ZNjO6M0ao+DlAWSbLVgTvmUEaXKgsHbytD4bC7KLnAnCu5UQuXroYDy4Da46stDkVF9mrXj54zBFTjA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1387 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Brijesh Singh This hypercall is used by the SEV guest to notify a change in the page encryption status to the hypervisor. The hypercall should be invoked only when the encryption attribute is changed from encrypted -> decrypted and vice versa. By default all guest pages are considered encrypted. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: "H. Peter Anvin" Cc: Paolo Bonzini Cc: "Radim Krčmář" Cc: Joerg Roedel Cc: Borislav Petkov Cc: Tom Lendacky Cc: x86@kernel.org Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- Documentation/virt/kvm/hypercalls.rst | 15 +++++ arch/x86/include/asm/kvm_host.h | 2 + arch/x86/kvm/svm.c | 94 +++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 1 + arch/x86/kvm/x86.c | 6 ++ include/uapi/linux/kvm_para.h | 1 + 6 files changed, 119 insertions(+) diff --git a/Documentation/virt/kvm/hypercalls.rst b/Documentation/virt/kvm/hypercalls.rst index dbaf207e560d..ff5287e68e81 100644 --- a/Documentation/virt/kvm/hypercalls.rst +++ b/Documentation/virt/kvm/hypercalls.rst @@ -169,3 +169,18 @@ a0: destination APIC ID :Usage example: When sending a call-function IPI-many to vCPUs, yield if any of the IPI target vCPUs was preempted. + + +8. KVM_HC_PAGE_ENC_STATUS +------------------------- +:Architecture: x86 +:Status: active +:Purpose: Notify the encryption status changes in guest page table (SEV guest) + +a0: the guest physical address of the start page +a1: the number of pages +a2: encryption attribute + + Where: + * 1: Encryption attribute is set + * 0: Encryption attribute is cleared diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 98959e8cd448..90718fa3db47 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1267,6 +1267,8 @@ struct kvm_x86_ops { bool (*apic_init_signal_blocked)(struct kvm_vcpu *vcpu); int (*enable_direct_tlbflush)(struct kvm_vcpu *vcpu); + int (*page_enc_status_hc)(struct kvm *kvm, unsigned long gpa, + unsigned long sz, unsigned long mode); }; struct kvm_arch_async_pf { diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index 7c2721e18b06..6c924df1b7b7 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -136,6 +136,8 @@ struct kvm_sev_info { int fd; /* SEV device fd */ unsigned long pages_locked; /* Number of pages locked */ struct list_head regions_list; /* List of registered regions */ + unsigned long *page_enc_bmap; + unsigned long page_enc_bmap_size; }; struct kvm_svm { @@ -1991,6 +1993,9 @@ static void sev_vm_destroy(struct kvm *kvm) sev_unbind_asid(kvm, sev->handle); sev_asid_free(sev->asid); + + kvfree(sev->page_enc_bmap); + sev->page_enc_bmap = NULL; } static void avic_vm_destroy(struct kvm *kvm) @@ -7593,6 +7598,93 @@ static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) return ret; } +static int sev_resize_page_enc_bitmap(struct kvm *kvm, unsigned long new_size) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + unsigned long *map; + unsigned long sz; + + if (sev->page_enc_bmap_size >= new_size) + return 0; + + sz = ALIGN(new_size, BITS_PER_LONG) / 8; + + map = vmalloc(sz); + if (!map) { + pr_err_once("Failed to allocate encrypted bitmap size %lx\n", + sz); + return -ENOMEM; + } + + /* mark the page encrypted (by default) */ + memset(map, 0xff, sz); + + bitmap_copy(map, sev->page_enc_bmap, sev->page_enc_bmap_size); + kvfree(sev->page_enc_bmap); + + sev->page_enc_bmap = map; + sev->page_enc_bmap_size = new_size; + + return 0; +} + +static int svm_page_enc_status_hc(struct kvm *kvm, unsigned long gpa, + unsigned long npages, unsigned long enc) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + gfn_t gfn_start, gfn_end; + int ret; + + if (!sev_guest(kvm)) + return -EINVAL; + + if (!npages) + return 0; + + gfn_start = gpa_to_gfn(gpa); + gfn_end = gfn_start + npages; + + /* out of bound access error check */ + if (gfn_end <= gfn_start) + return -EINVAL; + + /* lets make sure that gpa exist in our memslot */ + pfn_start = gfn_to_pfn(kvm, gfn_start); + pfn_end = gfn_to_pfn(kvm, gfn_end); + + if (is_error_noslot_pfn(pfn_start) && !is_noslot_pfn(pfn_start)) { + /* + * Allow guest MMIO range(s) to be added + * to the page encryption bitmap. + */ + return -EINVAL; + } + + if (is_error_noslot_pfn(pfn_end) && !is_noslot_pfn(pfn_end)) { + /* + * Allow guest MMIO range(s) to be added + * to the page encryption bitmap. + */ + return -EINVAL; + } + + mutex_lock(&kvm->lock); + ret = sev_resize_page_enc_bitmap(kvm, gfn_end); + if (ret) + goto unlock; + + if (enc) + __bitmap_set(sev->page_enc_bmap, gfn_start, + gfn_end - gfn_start); + else + __bitmap_clear(sev->page_enc_bmap, gfn_start, + gfn_end - gfn_start); + +unlock: + mutex_unlock(&kvm->lock); + return ret; +} + static int svm_mem_enc_op(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -7995,6 +8087,8 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .need_emulation_on_page_fault = svm_need_emulation_on_page_fault, .apic_init_signal_blocked = svm_apic_init_signal_blocked, + + .page_enc_status_hc = svm_page_enc_status_hc, }; static int __init svm_init(void) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 079d9fbf278e..f68e76ee7f9c 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -8001,6 +8001,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .nested_get_evmcs_version = NULL, .need_emulation_on_page_fault = vmx_need_emulation_on_page_fault, .apic_init_signal_blocked = vmx_apic_init_signal_blocked, + .page_enc_status_hc = NULL, }; static void vmx_cleanup_l1d_flush(void) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index cf95c36cb4f4..68428eef2dde 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7564,6 +7564,12 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) kvm_sched_yield(vcpu->kvm, a0); ret = 0; break; + case KVM_HC_PAGE_ENC_STATUS: + ret = -KVM_ENOSYS; + if (kvm_x86_ops->page_enc_status_hc) + ret = kvm_x86_ops->page_enc_status_hc(vcpu->kvm, + a0, a1, a2); + break; default: ret = -KVM_ENOSYS; break; diff --git a/include/uapi/linux/kvm_para.h b/include/uapi/linux/kvm_para.h index 8b86609849b9..847b83b75dc8 100644 --- a/include/uapi/linux/kvm_para.h +++ b/include/uapi/linux/kvm_para.h @@ -29,6 +29,7 @@ #define KVM_HC_CLOCK_PAIRING 9 #define KVM_HC_SEND_IPI 10 #define KVM_HC_SCHED_YIELD 11 +#define KVM_HC_PAGE_ENC_STATUS 12 /* * hypercalls use architecture specific From patchwork Mon Mar 30 01:36:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 11464395 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E6FA181 for ; Mon, 30 Mar 2020 01:36:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B218D20780 for ; Mon, 30 Mar 2020 01:36:45 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="C4Q7BFlA" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728742AbgC3Bgl (ORCPT ); Sun, 29 Mar 2020 21:36:41 -0400 Received: from mail-eopbgr750077.outbound.protection.outlook.com ([40.107.75.77]:21057 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728077AbgC3Bgh (ORCPT ); Sun, 29 Mar 2020 21:36:37 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Af6WXzvwWG+lK+nyTY4g8yUt8n2hE7lQB4vYsQGRjHy9pTOrZEEg/Rvg2gd3VTSROFL6gNRgohjtG7I9hiops0XJIpLkD3ild6VJzZ3TY6gqpusP4zHS5QU8qMUuROKBET+G2aCT/+Vr7hMZ3x230w6CKicsraN6zGnWtSoBvz1YY4H7wPC+m2zwaawTFeWY4aW1O+PkJCsB2CUWX0LT2kc5LEp7KNGMOpU0CymNYWfrC8q8Sp4oNxEF5/O9gN5XjesxXQ88ZQuKVSPq0Hko6FyMJ+HQ9WWKlLqOT98/ReGo9GEYFAqhQ1UcrggS/h7NDt7wbnSaSeEe6QG1skzaxg== 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-SenderADCheck; bh=lT1aEHavMmh47S9ptLBco7SCMd7oQdb73C0QUcz2Eyo=; b=B6oz6YWOt45FAZYbnUDR2x8c/NDEeo5ni1iOZXh/RnyEnCCOOuYAjS8aYEt6wZmx/S4huxfM4q9Y4hCPqS8QK1rW6ZrDTj/VI+OQs2IKQ0mFzHPRSVeUkwOJJ4f3bf1KuV1sBbnUiVWJSVBawvbzqkzN3gbWKEuFBvafT6IedrQJylZ/2qKQ/Lh0QaTevXqQUrpUkne7rPHMulaoZ3oYaHzXJKxLn7ihf6agvolF5lYS4q7fkZVL1cHv9eundH8pq3nzWMyWDFAr9heSj5OZ12I7U56Jw7fYWRpSmjRofoAmOX1EJnOcW6sJxtr9B3j+/ZXlNLVQjoLZHQ/pItnvPA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=lT1aEHavMmh47S9ptLBco7SCMd7oQdb73C0QUcz2Eyo=; b=C4Q7BFlAQiSsaWnRS7hzqhpQ0UC2B6hGT5r9uBKSEmfgHsWHmZ9PEMV6ZOqwdfgde1iqq9L+fDdgZBH7AN70U1sNsvv2IEqgCaNy5mk7fdAgRKb9hWDQEjo/fySwnG1J8Kiw70JSZjOdJnCHYMQoxhS2+UctbnB7413YWwkuvMM= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Ashish.Kalra@amd.com; Received: from DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) by DM5PR12MB1387.namprd12.prod.outlook.com (2603:10b6:3:6c::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.20; Mon, 30 Mar 2020 01:36:31 +0000 Received: from DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c]) by DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c%12]) with mapi id 15.20.2856.019; Mon, 30 Mar 2020 01:36:31 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, bp@suse.de, thomas.lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, rientjes@google.com, srutherford@google.com, luto@kernel.org, brijesh.singh@amd.com Subject: [PATCH v5 09/14] KVM: x86: Introduce KVM_GET_PAGE_ENC_BITMAP ioctl Date: Mon, 30 Mar 2020 01:36:21 +0000 Message-Id: <98b423c48531e1a816731a761e168dacbd3e4802.1585531159.git.ashish.kalra@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-ClientProxiedBy: DM5PR06CA0055.namprd06.prod.outlook.com (2603:10b6:3:37::17) To DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by DM5PR06CA0055.namprd06.prod.outlook.com (2603:10b6:3:37::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.20 via Frontend Transport; Mon, 30 Mar 2020 01:36:30 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: fbeff93a-ab7c-4f94-1be7-08d7d44ac601 X-MS-TrafficTypeDiagnostic: DM5PR12MB1387:|DM5PR12MB1387: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3173; X-Forefront-PRVS: 0358535363 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR12MB1386.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(10009020)(4636009)(39860400002)(376002)(346002)(366004)(136003)(396003)(186003)(66574012)(66556008)(6666004)(66476007)(86362001)(6916009)(66946007)(316002)(4326008)(7696005)(52116002)(5660300002)(6486002)(16526019)(26005)(36756003)(478600001)(7416002)(81166006)(81156014)(8936002)(2616005)(8676002)(2906002)(956004)(136400200001);DIR:OUT;SFP:1101; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bT9+w2qqkoz0BU8O5ju8aNlcWGXoD0pNDy1B25oMR5ozUXVI8d7ED+ir/n794ePBQxO694oCG+509n5OTHU+nxFC5SjPi+VWnJeBehSQaAaaK+q6xbWu88i6yxCUcM7GUcz1EzSy7ZfAhQDxb+hotuia9bud2alR9kANOACj/QG3FyMLXSdudfr25BWafGTqbWmVc7r600LpHeIiSPuKsqBrRg0InCRv5cN5oIqbJ7xfa+aJB+FQ+eNhLOApnTfvitueBf3z+SXic0AFkpUxfn33R9Erz0+pM/31odazlgF0OCUGJTFoJMTFcmrWcYVji3u6q8Osy7u6Up/f4CF8E4NaOurrC5957co/Hkk8V71VMYqGY5JLm1rssjfurpY8dLKpcga/z9CU3DHiWmGVJaKKch2EGQ7EC/BN1GwRLqbdhDZsapOAYeZUMScq3zUtxYJ80H45dGTWJKe8znHIULOpByBLKJj1Otp8HUYr5Bp2MemVibLKlBRdXQXHpu/A X-MS-Exchange-AntiSpam-MessageData: vTF1L0z+TQ+gvnYIxX+7A+weMAP1i/kMITQZBnP3iOgEW5xlTYVSRdP674ifEGFfK0xgvRGoK+zr5djjbUlF7UA4Fwr4PXNeDjNhUOQ0BBtkpg4XvggxI5fg7y7J+NdUfqH1YfslFrWpHRGufqPOUw== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: fbeff93a-ab7c-4f94-1be7-08d7d44ac601 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Mar 2020 01:36:31.2673 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: ye438Zgj3TdaFG5JXjHtI6WICEBBsh+skuF0qHuYod3AyPGQmjTy4FcwirDoPgL/83lu+CYaSsgwDNj5jOoNlw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1387 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Brijesh Singh The ioctl can be used to retrieve page encryption bitmap for a given gfn range. Return the correct bitmap as per the number of pages being requested by the user. Ensure that we only copy bmap->num_pages bytes in the userspace buffer, if bmap->num_pages is not byte aligned we read the trailing bits from the userspace and copy those bits as is. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: "H. Peter Anvin" Cc: Paolo Bonzini Cc: "Radim Krčmář" Cc: Joerg Roedel Cc: Borislav Petkov Cc: Tom Lendacky Cc: x86@kernel.org Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- Documentation/virt/kvm/api.rst | 27 +++++++++++++ arch/x86/include/asm/kvm_host.h | 2 + arch/x86/kvm/svm.c | 71 +++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 12 ++++++ include/uapi/linux/kvm.h | 12 ++++++ 5 files changed, 124 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index ebd383fba939..8ad800ebb54f 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -4648,6 +4648,33 @@ This ioctl resets VCPU registers and control structures according to the clear cpu reset definition in the POP. However, the cpu is not put into ESA mode. This reset is a superset of the initial reset. +4.125 KVM_GET_PAGE_ENC_BITMAP (vm ioctl) +--------------------------------------- + +:Capability: basic +:Architectures: x86 +:Type: vm ioctl +:Parameters: struct kvm_page_enc_bitmap (in/out) +:Returns: 0 on success, -1 on error + +/* for KVM_GET_PAGE_ENC_BITMAP */ +struct kvm_page_enc_bitmap { + __u64 start_gfn; + __u64 num_pages; + union { + void __user *enc_bitmap; /* one bit per page */ + __u64 padding2; + }; +}; + +The encrypted VMs have concept of private and shared pages. The private +page is encrypted with the guest-specific key, while shared page may +be encrypted with the hypervisor key. The KVM_GET_PAGE_ENC_BITMAP can +be used to get the bitmap indicating whether the guest page is private +or shared. The bitmap can be used during the guest migration, if the page +is private then userspace need to use SEV migration commands to transmit +the page. + 5. The kvm_run structure ======================== diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 90718fa3db47..27e43e3ec9d8 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1269,6 +1269,8 @@ struct kvm_x86_ops { int (*enable_direct_tlbflush)(struct kvm_vcpu *vcpu); int (*page_enc_status_hc)(struct kvm *kvm, unsigned long gpa, unsigned long sz, unsigned long mode); + int (*get_page_enc_bitmap)(struct kvm *kvm, + struct kvm_page_enc_bitmap *bmap); }; struct kvm_arch_async_pf { diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index 6c924df1b7b7..c417b1bfb5bf 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -7685,6 +7685,76 @@ static int svm_page_enc_status_hc(struct kvm *kvm, unsigned long gpa, return ret; } +static int svm_get_page_enc_bitmap(struct kvm *kvm, + struct kvm_page_enc_bitmap *bmap) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + unsigned long gfn_start, gfn_end; + unsigned long sz, i, sz_bytes; + unsigned long *bitmap; + int ret, n; + + if (!sev_guest(kvm)) + return -ENOTTY; + + gfn_start = bmap->start_gfn; + gfn_end = gfn_start + bmap->num_pages; + + sz = ALIGN(bmap->num_pages, BITS_PER_LONG) / BITS_PER_BYTE; + bitmap = kmalloc(sz, GFP_KERNEL); + if (!bitmap) + return -ENOMEM; + + /* by default all pages are marked encrypted */ + memset(bitmap, 0xff, sz); + + mutex_lock(&kvm->lock); + if (sev->page_enc_bmap) { + i = gfn_start; + for_each_clear_bit_from(i, sev->page_enc_bmap, + min(sev->page_enc_bmap_size, gfn_end)) + clear_bit(i - gfn_start, bitmap); + } + mutex_unlock(&kvm->lock); + + ret = -EFAULT; + + n = bmap->num_pages % BITS_PER_BYTE; + sz_bytes = ALIGN(bmap->num_pages, BITS_PER_BYTE) / BITS_PER_BYTE; + + /* + * Return the correct bitmap as per the number of pages being + * requested by the user. Ensure that we only copy bmap->num_pages + * bytes in the userspace buffer, if bmap->num_pages is not byte + * aligned we read the trailing bits from the userspace and copy + * those bits as is. + */ + + if (n) { + unsigned char *bitmap_kernel = (unsigned char *)bitmap; + unsigned char bitmap_user; + unsigned long offset, mask; + + offset = bmap->num_pages / BITS_PER_BYTE; + if (copy_from_user(&bitmap_user, bmap->enc_bitmap + offset, + sizeof(unsigned char))) + goto out; + + mask = GENMASK(n - 1, 0); + bitmap_user &= ~mask; + bitmap_kernel[offset] &= mask; + bitmap_kernel[offset] |= bitmap_user; + } + + if (copy_to_user(bmap->enc_bitmap, bitmap, sz_bytes)) + goto out; + + ret = 0; +out: + kfree(bitmap); + return ret; +} + static int svm_mem_enc_op(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -8089,6 +8159,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .apic_init_signal_blocked = svm_apic_init_signal_blocked, .page_enc_status_hc = svm_page_enc_status_hc, + .get_page_enc_bitmap = svm_get_page_enc_bitmap, }; static int __init svm_init(void) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 68428eef2dde..3c3fea4e20b5 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -5226,6 +5226,18 @@ long kvm_arch_vm_ioctl(struct file *filp, case KVM_SET_PMU_EVENT_FILTER: r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp); break; + case KVM_GET_PAGE_ENC_BITMAP: { + struct kvm_page_enc_bitmap bitmap; + + r = -EFAULT; + if (copy_from_user(&bitmap, argp, sizeof(bitmap))) + goto out; + + r = -ENOTTY; + if (kvm_x86_ops->get_page_enc_bitmap) + r = kvm_x86_ops->get_page_enc_bitmap(kvm, &bitmap); + break; + } default: r = -ENOTTY; } diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 4e80c57a3182..db1ebf85e177 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -500,6 +500,16 @@ struct kvm_dirty_log { }; }; +/* for KVM_GET_PAGE_ENC_BITMAP */ +struct kvm_page_enc_bitmap { + __u64 start_gfn; + __u64 num_pages; + union { + void __user *enc_bitmap; /* one bit per page */ + __u64 padding2; + }; +}; + /* for KVM_CLEAR_DIRTY_LOG */ struct kvm_clear_dirty_log { __u32 slot; @@ -1478,6 +1488,8 @@ struct kvm_enc_region { #define KVM_S390_NORMAL_RESET _IO(KVMIO, 0xc3) #define KVM_S390_CLEAR_RESET _IO(KVMIO, 0xc4) +#define KVM_GET_PAGE_ENC_BITMAP _IOW(KVMIO, 0xc5, struct kvm_page_enc_bitmap) + /* Secure Encrypted Virtualization command */ enum sev_cmd_id { /* Guest initialization commands */ From patchwork Mon Mar 30 01:36:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 11464405 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 407A8912 for ; Mon, 30 Mar 2020 01:39:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0AD8020780 for ; Mon, 30 Mar 2020 01:39:08 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="WC/VTATP" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728531AbgC3BjA (ORCPT ); Sun, 29 Mar 2020 21:39:00 -0400 Received: from mail-eopbgr750048.outbound.protection.outlook.com ([40.107.75.48]:39303 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727815AbgC3BjA (ORCPT ); Sun, 29 Mar 2020 21:39:00 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZBW4zeGYREk2KsucA3GtyLzy1MrT+MFM+NNkpj62/A3Y61uVMpE0ogkLEC0Xf1LG5fQydPnnFdUWUcHgGYwkgKeRiDK4MZnGq6Jf+asRpCERe8OrPgeCBefM8QdcLshuy1U77BmG9yhnoWvWELAjBdUhjitTikIdVU32JmQqwBxTlsWCjZA0lgydGr3Age5mb5KVgn7BR1JUs/8LWmDV8Ii6qJzCYVhXCrcXs+s7rxjzNtu4QbK039iCsr7RcCjgyVp2fvodUEUEzyZlTsvrrG4nQ+0erlpWYaZAicup7YJoOLQ4El4igCz0zUCaxGE3XCWWUJ5icMIBzA8tc9QBVA== 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-SenderADCheck; bh=5mtUHk17blOSg2a6eSqI1/FBFC0Cg73y4oTIurroTLA=; b=W+jZ0rMdtQ9xrMP9c529/Mzc0ox4I7K0ypw6dw1vhJZhzxC+jXnPKYK/kMF1h44XONVH/t+SPbJmPV6YNIdNUbbi4OF8G+6CVEiXkl8M8hNVj3SFOA+RDjNFemXIqmp/AQ1wxtx+NkbpFYdc0GHthYJrjUK2qN7a4yZzZxTrkE8ruUZjtLaKRuwzKlSRNZU2zsQQ/xi9eUF3wgS59/7wd/guWdbX84ZAJPYhJsuo2cQRR43gsrdJyRcAo9vFrktFG8zc82+3S2AFRHFEyw19SJHVGEDuzI0YpdAUsLltMtpH3Sf2GP4nj5yLBQQNSjxoQMj1EkHu80YzuKAVQ33b6Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=5mtUHk17blOSg2a6eSqI1/FBFC0Cg73y4oTIurroTLA=; b=WC/VTATPEQJ4mG+D1PbbIKRKKSVQMmtQV/nBiwjDVprMUPBwxfaqRALqRRj7fL4xxPHbUuGpuAzSeF85JEW8EpqnELbayNBImFG5T/0F/Dtcu7AOm3A2zky6TohWKvaS5yn77XadfkFxn6kGuiGorigd9RdHM4RdXAK8i7dPS0Y= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Ashish.Kalra@amd.com; Received: from DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) by DM5PR12MB1387.namprd12.prod.outlook.com (2603:10b6:3:6c::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.20; Mon, 30 Mar 2020 01:36:46 +0000 Received: from DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c]) by DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c%12]) with mapi id 15.20.2856.019; Mon, 30 Mar 2020 01:36:46 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, bp@suse.de, thomas.lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, rientjes@google.com, srutherford@google.com, luto@kernel.org, brijesh.singh@amd.com Subject: [PATCH v5 10/14] mm: x86: Invoke hypercall when page encryption status is changed Date: Mon, 30 Mar 2020 01:36:37 +0000 Message-Id: <7de8c7748914b9511175b5a8b252332d1c00d33f.1585531159.git.ashish.kalra@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-ClientProxiedBy: SN4PR0801CA0023.namprd08.prod.outlook.com (2603:10b6:803:29::33) To DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by SN4PR0801CA0023.namprd08.prod.outlook.com (2603:10b6:803:29::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.20 via Frontend Transport; Mon, 30 Mar 2020 01:36:45 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: e4e95d80-9108-4127-a224-08d7d44acf22 X-MS-TrafficTypeDiagnostic: DM5PR12MB1387:|DM5PR12MB1387: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:5516; X-Forefront-PRVS: 0358535363 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR12MB1386.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(10009020)(4636009)(39860400002)(376002)(346002)(366004)(136003)(396003)(186003)(66574012)(66556008)(6666004)(66476007)(86362001)(6916009)(66946007)(316002)(4326008)(7696005)(52116002)(5660300002)(6486002)(16526019)(26005)(36756003)(478600001)(7416002)(81166006)(81156014)(8936002)(2616005)(8676002)(2906002)(956004)(136400200001);DIR:OUT;SFP:1101; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: MXt2I5hHfu7UcvvOq89UWjqt1pbvf06guiu3+PtGHJmpME0QZ3V0hyhh+hFd4AcChvjw3VeCZWiEbq8hV00haRjuv5wQsWrle7TFN0znIIzYCd3zufC3bQt/z3JL1F5gWVkZGBemiMKD9yGBEn4RixamXeHU46UcZsQcvKGKiOS3MEKB7svAjYWsmpkkiTXnkpRUF5xu3ONFwdV3kOqkD7js/rEnSRt4u4X7rQuYgp0HEB8QOVoTWKXkEM4LqIJN6L0cTrPhnwWbf+p8itikqepOCBTR7WWLqaNL/HJvPFiBvIS9jVMp/d19zjujzujJh7+wEQphCONa+OnJxYsP2537pTgFG3Zer6THgZJwBaFhtXGQJ/GOihbEC0qsorNc3zt8HqhEue0SS8vq402dtf+iebhbeMBNC8D3EOO81LOr2azQHCwFIKoyyYz24acm2wRDPTM8e1X39G2yEm3FRyQUXxCaPYowwfi0bw75NpFBRkAyGNErGJBKZp2OQ24r X-MS-Exchange-AntiSpam-MessageData: enxBITf6EyBdKAg9GAwemrhhS+3dEwizkyXMEeM+Ahbb09P6ZCBkEQNo9PbicXjtl759ciugl8jQvbWcdT9aLtYrFFM6nuceLliXRVwJ+FVqAMq8ZrCAYmDUAhD614i0q8nrvOrXLQyLc+9ZDmYXDA== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: e4e95d80-9108-4127-a224-08d7d44acf22 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Mar 2020 01:36:46.5705 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: PFDkl5eNR4nlZO5GWztb6+FqAGBBeDzGKYTdz9FJpb6HOik8QMQOZtX9M3WpNCWdZGnWnWp3Cf8wFKywwtBp1w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1387 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Brijesh Singh Invoke a hypercall when a memory region is changed from encrypted -> decrypted and vice versa. Hypervisor need to know the page encryption status during the guest migration. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: "H. Peter Anvin" Cc: Paolo Bonzini Cc: "Radim Krčmář" Cc: Joerg Roedel Cc: Borislav Petkov Cc: Tom Lendacky Cc: x86@kernel.org Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Reported-by: kbuild test robot --- arch/x86/include/asm/paravirt.h | 6 +++ arch/x86/include/asm/paravirt_types.h | 2 + arch/x86/kernel/paravirt.c | 1 + arch/x86/mm/mem_encrypt.c | 57 ++++++++++++++++++++++++++- arch/x86/mm/pat/set_memory.c | 7 ++++ 5 files changed, 72 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/paravirt.h b/arch/x86/include/asm/paravirt.h index 694d8daf4983..aa296af5d393 100644 --- a/arch/x86/include/asm/paravirt.h +++ b/arch/x86/include/asm/paravirt.h @@ -78,6 +78,12 @@ static inline void paravirt_arch_exit_mmap(struct mm_struct *mm) PVOP_VCALL1(mmu.exit_mmap, mm); } +static inline void page_encryption_changed(unsigned long vaddr, int npages, + bool enc) +{ + PVOP_VCALL3(mmu.page_encryption_changed, vaddr, npages, enc); +} + #ifdef CONFIG_PARAVIRT_XXL static inline void load_sp0(unsigned long sp0) { diff --git a/arch/x86/include/asm/paravirt_types.h b/arch/x86/include/asm/paravirt_types.h index 732f62e04ddb..03bfd515c59c 100644 --- a/arch/x86/include/asm/paravirt_types.h +++ b/arch/x86/include/asm/paravirt_types.h @@ -215,6 +215,8 @@ struct pv_mmu_ops { /* Hook for intercepting the destruction of an mm_struct. */ void (*exit_mmap)(struct mm_struct *mm); + void (*page_encryption_changed)(unsigned long vaddr, int npages, + bool enc); #ifdef CONFIG_PARAVIRT_XXL struct paravirt_callee_save read_cr2; diff --git a/arch/x86/kernel/paravirt.c b/arch/x86/kernel/paravirt.c index c131ba4e70ef..840c02b23aeb 100644 --- a/arch/x86/kernel/paravirt.c +++ b/arch/x86/kernel/paravirt.c @@ -367,6 +367,7 @@ struct paravirt_patch_template pv_ops = { (void (*)(struct mmu_gather *, void *))tlb_remove_page, .mmu.exit_mmap = paravirt_nop, + .mmu.page_encryption_changed = paravirt_nop, #ifdef CONFIG_PARAVIRT_XXL .mmu.read_cr2 = __PV_IS_CALLEE_SAVE(native_read_cr2), diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index f4bd4b431ba1..c9800fa811f6 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -29,6 +30,7 @@ #include #include #include +#include #include "mm_internal.h" @@ -196,6 +198,47 @@ void __init sme_early_init(void) swiotlb_force = SWIOTLB_FORCE; } +static void set_memory_enc_dec_hypercall(unsigned long vaddr, int npages, + bool enc) +{ + unsigned long sz = npages << PAGE_SHIFT; + unsigned long vaddr_end, vaddr_next; + + vaddr_end = vaddr + sz; + + for (; vaddr < vaddr_end; vaddr = vaddr_next) { + int psize, pmask, level; + unsigned long pfn; + pte_t *kpte; + + kpte = lookup_address(vaddr, &level); + if (!kpte || pte_none(*kpte)) + return; + + switch (level) { + case PG_LEVEL_4K: + pfn = pte_pfn(*kpte); + break; + case PG_LEVEL_2M: + pfn = pmd_pfn(*(pmd_t *)kpte); + break; + case PG_LEVEL_1G: + pfn = pud_pfn(*(pud_t *)kpte); + break; + default: + return; + } + + psize = page_level_size(level); + pmask = page_level_mask(level); + + kvm_sev_hypercall3(KVM_HC_PAGE_ENC_STATUS, + pfn << PAGE_SHIFT, psize >> PAGE_SHIFT, enc); + + vaddr_next = (vaddr & pmask) + psize; + } +} + static void __init __set_clr_pte_enc(pte_t *kpte, int level, bool enc) { pgprot_t old_prot, new_prot; @@ -253,12 +296,13 @@ static void __init __set_clr_pte_enc(pte_t *kpte, int level, bool enc) static int __init early_set_memory_enc_dec(unsigned long vaddr, unsigned long size, bool enc) { - unsigned long vaddr_end, vaddr_next; + unsigned long vaddr_end, vaddr_next, start; unsigned long psize, pmask; int split_page_size_mask; int level, ret; pte_t *kpte; + start = vaddr; vaddr_next = vaddr; vaddr_end = vaddr + size; @@ -313,6 +357,8 @@ static int __init early_set_memory_enc_dec(unsigned long vaddr, ret = 0; + set_memory_enc_dec_hypercall(start, PAGE_ALIGN(size) >> PAGE_SHIFT, + enc); out: __flush_tlb_all(); return ret; @@ -451,6 +497,15 @@ void __init mem_encrypt_init(void) if (sev_active()) static_branch_enable(&sev_enable_key); +#ifdef CONFIG_PARAVIRT + /* + * With SEV, we need to make a hypercall when page encryption state is + * changed. + */ + if (sev_active()) + pv_ops.mmu.page_encryption_changed = set_memory_enc_dec_hypercall; +#endif + pr_info("AMD %s active\n", sev_active() ? "Secure Encrypted Virtualization (SEV)" : "Secure Memory Encryption (SME)"); diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index c4aedd00c1ba..86b7804129fc 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "../mm_internal.h" @@ -1987,6 +1988,12 @@ static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc) */ cpa_flush(&cpa, 0); + /* Notify hypervisor that a given memory range is mapped encrypted + * or decrypted. The hypervisor will use this information during the + * VM migration. + */ + page_encryption_changed(addr, numpages, enc); + return ret; } From patchwork Mon Mar 30 01:37:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 11464399 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 93C7481 for ; Mon, 30 Mar 2020 01:38:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5D1F32076A for ; Mon, 30 Mar 2020 01:38:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="h8leF/kP" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728333AbgC3BiK (ORCPT ); Sun, 29 Mar 2020 21:38:10 -0400 Received: from mail-eopbgr750041.outbound.protection.outlook.com ([40.107.75.41]:37124 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728077AbgC3BiK (ORCPT ); Sun, 29 Mar 2020 21:38:10 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ao1sMX19/Otpl51X5Y0gErsQyguqAlYyZx86EoMBLWS5Zc4b4dsWEJoetnCD8Th6EbUDuCvukB578bHaQSOeXZ+Ol20yql6amvLM263UUWKfR/GbJllX11ZzRnEIYSMWnALdCfC0Vg3bJDQORP6r4T1PpWM6S7IkXXiQlRSEpY6TkTtT7VDFPqCJ/XxE/XMVT16ZrlbpI8zSm/x0Zn/9vwnyCwhVFAQmcuPvh5/hRKDRPer/gUXoufHJP7k4MSlM8v+MgTF2fkaH3spxQ+eVqPy85rwI+NgfR4qGIv5Aj5knfsgNQCsS4IWNvzohMkBVbwcbU7kCVrqknYtnTTosAA== 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-SenderADCheck; bh=T+rutv8RNZ9J9zGk1iW3DhPnA3eOyfwyJx1isTHf35I=; b=TbzN03bF2DMJXHj7F1VtB2jcCubOVKtd12+R2jVL+r/VCXlGfZiU6SlTqrL0FfCg7RVc95rDKNu8yh6u8X6nRxMZWZO0coCSM1uuvesJtaAW7pbK0apuNA/By8nP4gMG44PaQm/SGSf6voM/xQd6BQv+wmNF70Lf5vvvBcydDbjTAl58CK9bG8odMkPB0VwfUOyfYJvK9hS+whRWvMRuAI3dmkomUX35iewocfehzd8z5I5UOhfr0IXPVKfImUjXA+q8vB0pVGScUMMXkY8jH5nIsTqngHDszoA5ZqJ75+MTr4MnyNTcVPHqgxvKOnmhMA42THqPrpcOPeA4wv9eRg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=T+rutv8RNZ9J9zGk1iW3DhPnA3eOyfwyJx1isTHf35I=; b=h8leF/kPS+OGuW2dHVXrBbnf8+fj2Rum5MpwT9Oc4eteW8g1TUHcugdQHy4H6q07cR0rnp3BP3DqKtwN1BO74eGwLzrozE1Dp4wsKN1AAO1s9DkAZ0jx3DpNghQ/m+nB7H8XniWRI4Pd+g0ukFt8gA0XtDoUpkB3B9OoOeHxyko= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Ashish.Kalra@amd.com; Received: from DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) by DM5PR12MB1387.namprd12.prod.outlook.com (2603:10b6:3:6c::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.20; Mon, 30 Mar 2020 01:37:32 +0000 Received: from DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c]) by DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c%12]) with mapi id 15.20.2856.019; Mon, 30 Mar 2020 01:37:32 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, bp@suse.de, thomas.lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, rientjes@google.com, srutherford@google.com, luto@kernel.org, brijesh.singh@amd.com Subject: [PATCH v5 11/14] KVM: x86: Introduce KVM_SET_PAGE_ENC_BITMAP ioctl Date: Mon, 30 Mar 2020 01:37:21 +0000 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-ClientProxiedBy: SN4PR0801CA0022.namprd08.prod.outlook.com (2603:10b6:803:29::32) To DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by SN4PR0801CA0022.namprd08.prod.outlook.com (2603:10b6:803:29::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.20 via Frontend Transport; Mon, 30 Mar 2020 01:37:31 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: dbc3d5d6-0f4a-483b-d16a-08d7d44aeaab X-MS-TrafficTypeDiagnostic: DM5PR12MB1387:|DM5PR12MB1387: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4303; X-Forefront-PRVS: 0358535363 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR12MB1386.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(10009020)(4636009)(39860400002)(376002)(346002)(366004)(136003)(396003)(186003)(66574012)(66556008)(6666004)(66476007)(86362001)(6916009)(66946007)(316002)(4326008)(7696005)(52116002)(5660300002)(6486002)(16526019)(26005)(36756003)(478600001)(7416002)(81166006)(81156014)(8936002)(2616005)(8676002)(2906002)(956004)(136400200001);DIR:OUT;SFP:1101; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /vhpTj95Fh+c0T0876c+Pv75GuMMUCgxCp59p5v87fqU/mB/FIp8Pvo4QPn/U7jwlcThL9EdUUcu8x8hXNoqzrz0HvIGoDtZBwlqFybV+YJXJphaIeWrA6v7NsR/cAIbqE8fOUA6hOoZoYxe5+nQQUFfAOGnnHg35lW/jrDN9P5DFJZiEQ7+E/Aw4n99C/jvAHv6eeBBvFv1CWH7ZJmbnEEacNyShPogjxGniCyN7IgPtWO3TiFD5BFdowkCVF7XXhom/GsZNJsWmlxVzfWti8Pp6RNfZyTCif1NZIT+d996Ambrv8lNfucx/IymXM/vb3MF4t5uJmLNewtq/ffbe06vqKraRPzx3M7PnSptzvSP35mlgED0XdB6ADS/QJM0S95oD8lNOL1eaxXcro+BXDIPX3TjuHpNV6GTcZz8AlQHRy4CLIrmvQyXn8ubWHCC9oG3WCZE7Xl/dQs+a4BEHQis3yLPlHbZJCdQ46UPtqrLrPEYFKGTxMvl2DVsYjGM X-MS-Exchange-AntiSpam-MessageData: xkT3rTMRocpI3PLKSvwWNBLMcyEpFZcyxfkpdrowxJ7+2oUuGvI7OlWNtynmvjTry8chB2sgokrffUJdIw47TZobT/DefiIhm03ELYt8tns8fO6+foRrd8IvvIhfLPl2sbDo5z7wCUUkibq1bj4V+A== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: dbc3d5d6-0f4a-483b-d16a-08d7d44aeaab X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Mar 2020 01:37:32.7731 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: y52NFL86Tq+fysuGhueqgXvylS717lJHfM/ozgBK7flmUc/DL+jjQIBjQXwbgjTRmajcXqGJ8oBhE9OPoL6i1w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1387 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Brijesh Singh The ioctl can be used to set page encryption bitmap for an incoming guest. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: "H. Peter Anvin" Cc: Paolo Bonzini Cc: "Radim Krčmář" Cc: Joerg Roedel Cc: Borislav Petkov Cc: Tom Lendacky Cc: x86@kernel.org Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- Documentation/virt/kvm/api.rst | 22 +++++++++++++++++ arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/svm.c | 42 +++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 12 ++++++++++ include/uapi/linux/kvm.h | 1 + 5 files changed, 79 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 8ad800ebb54f..4d1004a154f6 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -4675,6 +4675,28 @@ or shared. The bitmap can be used during the guest migration, if the page is private then userspace need to use SEV migration commands to transmit the page. +4.126 KVM_SET_PAGE_ENC_BITMAP (vm ioctl) +--------------------------------------- + +:Capability: basic +:Architectures: x86 +:Type: vm ioctl +:Parameters: struct kvm_page_enc_bitmap (in/out) +:Returns: 0 on success, -1 on error + +/* for KVM_SET_PAGE_ENC_BITMAP */ +struct kvm_page_enc_bitmap { + __u64 start_gfn; + __u64 num_pages; + union { + void __user *enc_bitmap; /* one bit per page */ + __u64 padding2; + }; +}; + +During the guest live migration the outgoing guest exports its page encryption +bitmap, the KVM_SET_PAGE_ENC_BITMAP can be used to build the page encryption +bitmap for an incoming guest. 5. The kvm_run structure ======================== diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 27e43e3ec9d8..d30f770aaaea 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1271,6 +1271,8 @@ struct kvm_x86_ops { unsigned long sz, unsigned long mode); int (*get_page_enc_bitmap)(struct kvm *kvm, struct kvm_page_enc_bitmap *bmap); + int (*set_page_enc_bitmap)(struct kvm *kvm, + struct kvm_page_enc_bitmap *bmap); }; struct kvm_arch_async_pf { diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index c417b1bfb5bf..e7fa196f3de1 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -7755,6 +7755,47 @@ static int svm_get_page_enc_bitmap(struct kvm *kvm, return ret; } +static int svm_set_page_enc_bitmap(struct kvm *kvm, + struct kvm_page_enc_bitmap *bmap) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + unsigned long gfn_start, gfn_end; + unsigned long *bitmap; + unsigned long sz, i; + int ret; + + if (!sev_guest(kvm)) + return -ENOTTY; + + gfn_start = bmap->start_gfn; + gfn_end = gfn_start + bmap->num_pages; + + sz = ALIGN(bmap->num_pages, BITS_PER_LONG) / 8; + bitmap = kmalloc(sz, GFP_KERNEL); + if (!bitmap) + return -ENOMEM; + + ret = -EFAULT; + if (copy_from_user(bitmap, bmap->enc_bitmap, sz)) + goto out; + + mutex_lock(&kvm->lock); + ret = sev_resize_page_enc_bitmap(kvm, gfn_end); + if (ret) + goto unlock; + + i = gfn_start; + for_each_clear_bit_from(i, bitmap, (gfn_end - gfn_start)) + clear_bit(i + gfn_start, sev->page_enc_bmap); + + ret = 0; +unlock: + mutex_unlock(&kvm->lock); +out: + kfree(bitmap); + return ret; +} + static int svm_mem_enc_op(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -8160,6 +8201,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .page_enc_status_hc = svm_page_enc_status_hc, .get_page_enc_bitmap = svm_get_page_enc_bitmap, + .set_page_enc_bitmap = svm_set_page_enc_bitmap, }; static int __init svm_init(void) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 3c3fea4e20b5..05e953b2ec61 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -5238,6 +5238,18 @@ long kvm_arch_vm_ioctl(struct file *filp, r = kvm_x86_ops->get_page_enc_bitmap(kvm, &bitmap); break; } + case KVM_SET_PAGE_ENC_BITMAP: { + struct kvm_page_enc_bitmap bitmap; + + r = -EFAULT; + if (copy_from_user(&bitmap, argp, sizeof(bitmap))) + goto out; + + r = -ENOTTY; + if (kvm_x86_ops->set_page_enc_bitmap) + r = kvm_x86_ops->set_page_enc_bitmap(kvm, &bitmap); + break; + } default: r = -ENOTTY; } diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index db1ebf85e177..b4b01d47e568 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1489,6 +1489,7 @@ struct kvm_enc_region { #define KVM_S390_CLEAR_RESET _IO(KVMIO, 0xc4) #define KVM_GET_PAGE_ENC_BITMAP _IOW(KVMIO, 0xc5, struct kvm_page_enc_bitmap) +#define KVM_SET_PAGE_ENC_BITMAP _IOW(KVMIO, 0xc6, struct kvm_page_enc_bitmap) /* Secure Encrypted Virtualization command */ enum sev_cmd_id { From patchwork Mon Mar 30 01:37:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 11464409 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5A0E981 for ; Mon, 30 Mar 2020 01:39:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2D95C20780 for ; Mon, 30 Mar 2020 01:39:51 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="IvGYtCKJ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728346AbgC3Bjr (ORCPT ); Sun, 29 Mar 2020 21:39:47 -0400 Received: from mail-eopbgr750041.outbound.protection.outlook.com ([40.107.75.41]:37124 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727801AbgC3Bjr (ORCPT ); Sun, 29 Mar 2020 21:39:47 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CKgCuS++UOjwBK7BCTuQ31njNZAzLVzebPpLhxexQGRuXoA1hcn1JrxZKl3h1PgCskfSicGzgtqdR1TVca1ffcb4HKEuwe2eswuMVBDPfaumq9luvU1gT4vUkaHDPOwYoGAJYhs+rbIUG7h5dZ5veND1EEtnY8ja72tnC1AStFTgu+U+NCuc73WaomSbiz5kSRLotncUNFXz5ZXyb7ZzH7TxJLzgczboBpoB/OFKuugOrS4F5hAybx8UVCmS7nVW2QeXrZcRXSPtbg9w/n4ENV4zLVyysZmwI2nIZ/cCY21gj7f5zrC0Rb4VV2zGbLRQTeqLpjSp/J6s+aiH7V+wlA== 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-SenderADCheck; bh=coEXQKta0i8hjS/TsVIf2MiaJHX/DIbg2a4S2sQ4Vx8=; b=DvuFmWfTperzvEFc6TOim8ffvHmkbNAbGlSkkLiU4Lf5ojg/mM0IEEy+ok0XNJKteC6sLLw3Od8WesW6RFnRfyancCB5OeKhuG/Q4px5B6SD5fqQDI3KhT8wrN+7ls1/LXBgazGZTPiYErfnauRC52LODoONmSzcwTM/s74psyG4HxWQJjzzicwguhoS7snBlBr9rWsR6gKa2TsTbhO8wk32cNPGk8rWq1gl/Nm7lItqtiurS97PdEp+7xr2YjnRQ2XM5GrRp0/YA53EiXUqgEeGu6ZNuzAarzzttvTG9oKt7HWkPW5RMS3c80ZkMVYFQjaNwtzhZ6odgGGZKJl6KA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=coEXQKta0i8hjS/TsVIf2MiaJHX/DIbg2a4S2sQ4Vx8=; b=IvGYtCKJxbVX9+K1ATfdf8OlUk00UC2EHcaN7uu0zsTPa1ZdDDpzbs7N9hLEBb14pkxxSW5r5fnjXi8kQO6JuqxDVOQkAOm24cdcrNTGdXczGL9ZvbsVtQ4cU057LKiGkjsd79ioW2KYlVYqD8KcXX/4N5msbiqB/YKt2eQI7SI= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Ashish.Kalra@amd.com; Received: from DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) by DM5PR12MB1387.namprd12.prod.outlook.com (2603:10b6:3:6c::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.20; Mon, 30 Mar 2020 01:37:49 +0000 Received: from DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c]) by DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c%12]) with mapi id 15.20.2856.019; Mon, 30 Mar 2020 01:37:49 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, bp@suse.de, thomas.lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, rientjes@google.com, srutherford@google.com, luto@kernel.org, brijesh.singh@amd.com Subject: [PATCH v5 12/14] KVM: x86: Introduce KVM_PAGE_ENC_BITMAP_RESET ioctl Date: Mon, 30 Mar 2020 01:37:39 +0000 Message-Id: <3a6b8429065e7d8367ee225775a31293da87dd58.1585531159.git.ashish.kalra@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-ClientProxiedBy: SN4PR0501CA0150.namprd05.prod.outlook.com (2603:10b6:803:2c::28) To DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by SN4PR0501CA0150.namprd05.prod.outlook.com (2603:10b6:803:2c::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2878.9 via Frontend Transport; Mon, 30 Mar 2020 01:37:48 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 2ddf5b56-c1d6-43fd-a354-08d7d44af4b8 X-MS-TrafficTypeDiagnostic: DM5PR12MB1387:|DM5PR12MB1387: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6108; X-Forefront-PRVS: 0358535363 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR12MB1386.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(10009020)(4636009)(39860400002)(376002)(346002)(366004)(136003)(396003)(186003)(66556008)(6666004)(66476007)(86362001)(6916009)(66946007)(316002)(4326008)(7696005)(52116002)(5660300002)(6486002)(16526019)(26005)(36756003)(478600001)(7416002)(81166006)(81156014)(8936002)(2616005)(8676002)(2906002)(956004)(136400200001);DIR:OUT;SFP:1101; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: +HnJ/BOndkpTw9vZ2HRo3yMJrkb+JZKp+kiE5oXOD/KB/SaeMtIploMLfB2UHRARfrqtv4IFXu5wEPuYsRQW3hnRdhLoDYMzlSWm5oqLy6QflwHyGu9VvcWbvDmV7uub9+Prx6AGcMLLbipqmVwuFB5S9mcxjHx7RsbhPtCJQ9SKdnUcreiwz/C05M5DQdktcrQRgdy2Kvesg03A7Gor93/iqosnCCLupDcNALVkr+I68YBrYAFG13P4IdmEk3qwqzDLL8hWN0DdkLdD/j2DWlw433LED5Zz39eJ3NmCIYNi38bIfx5gSmx5pRcJv4Ughl5sp1JKF7Ktnj+7KwzJjm+qsHCpdTqWDa6f5pu7vpIGYszpLmK+QS841BGVziiohdcdmT5rZAbdu1BwN9jkCT0vwj1CmEBKWWXje4jHEkhA8FS6s3+DGQIruUmKrDoBdG5Z0k6SZWN5ojPbp+vjmVCvQp9ywjkyiI3+CWWDbmBP6S5EnCqT0V+g6C2chYPL X-MS-Exchange-AntiSpam-MessageData: 40Kq2aDyDB2dnhqd6GXu9TKF2uFZxPmXJCpH4O1hiCHwqhj2CeqlO5zQgsKHrPzcrNNuFbg+grhLi9XuKHq888s7ftpXv1AdE14rF8WS5uZfON1gJoiS1uGj8/mfqQzbTLsishMCRB3/YWHGKlxslA== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2ddf5b56-c1d6-43fd-a354-08d7d44af4b8 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Mar 2020 01:37:49.6203 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 8JGxUn8gnDcJSOhjv7fXLYQEp6D8/gTI/7LY/B48wjmjCRCjuwpOqRdE7huXaS6U7VTAhjaLbdAaALZvo+dc4Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1387 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Ashish Kalra This ioctl can be used by the application to reset the page encryption bitmap managed by the KVM driver. A typical usage for this ioctl is on VM reboot, on reboot, we must reinitialize the bitmap. Signed-off-by: Ashish Kalra --- Documentation/virt/kvm/api.rst | 13 +++++++++++++ arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm.c | 16 ++++++++++++++++ arch/x86/kvm/x86.c | 6 ++++++ include/uapi/linux/kvm.h | 1 + 5 files changed, 37 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 4d1004a154f6..a11326ccc51d 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -4698,6 +4698,19 @@ During the guest live migration the outgoing guest exports its page encryption bitmap, the KVM_SET_PAGE_ENC_BITMAP can be used to build the page encryption bitmap for an incoming guest. +4.127 KVM_PAGE_ENC_BITMAP_RESET (vm ioctl) +----------------------------------------- + +:Capability: basic +:Architectures: x86 +:Type: vm ioctl +:Parameters: none +:Returns: 0 on success, -1 on error + +The KVM_PAGE_ENC_BITMAP_RESET is used to reset the guest's page encryption +bitmap during guest reboot and this is only done on the guest's boot vCPU. + + 5. The kvm_run structure ======================== diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index d30f770aaaea..a96ef6338cd2 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1273,6 +1273,7 @@ struct kvm_x86_ops { struct kvm_page_enc_bitmap *bmap); int (*set_page_enc_bitmap)(struct kvm *kvm, struct kvm_page_enc_bitmap *bmap); + int (*reset_page_enc_bitmap)(struct kvm *kvm); }; struct kvm_arch_async_pf { diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index e7fa196f3de1..d74773573811 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -7796,6 +7796,21 @@ static int svm_set_page_enc_bitmap(struct kvm *kvm, return ret; } +static int svm_reset_page_enc_bitmap(struct kvm *kvm) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + + if (!sev_guest(kvm)) + return -ENOTTY; + + mutex_lock(&kvm->lock); + /* by default all pages should be marked encrypted */ + if (sev->page_enc_bmap_size) + bitmap_fill(sev->page_enc_bmap, sev->page_enc_bmap_size); + mutex_unlock(&kvm->lock); + return 0; +} + static int svm_mem_enc_op(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -8202,6 +8217,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .page_enc_status_hc = svm_page_enc_status_hc, .get_page_enc_bitmap = svm_get_page_enc_bitmap, .set_page_enc_bitmap = svm_set_page_enc_bitmap, + .reset_page_enc_bitmap = svm_reset_page_enc_bitmap, }; static int __init svm_init(void) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 05e953b2ec61..2127ed937f53 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -5250,6 +5250,12 @@ long kvm_arch_vm_ioctl(struct file *filp, r = kvm_x86_ops->set_page_enc_bitmap(kvm, &bitmap); break; } + case KVM_PAGE_ENC_BITMAP_RESET: { + r = -ENOTTY; + if (kvm_x86_ops->reset_page_enc_bitmap) + r = kvm_x86_ops->reset_page_enc_bitmap(kvm); + break; + } default: r = -ENOTTY; } diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index b4b01d47e568..0884a581fc37 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1490,6 +1490,7 @@ struct kvm_enc_region { #define KVM_GET_PAGE_ENC_BITMAP _IOW(KVMIO, 0xc5, struct kvm_page_enc_bitmap) #define KVM_SET_PAGE_ENC_BITMAP _IOW(KVMIO, 0xc6, struct kvm_page_enc_bitmap) +#define KVM_PAGE_ENC_BITMAP_RESET _IO(KVMIO, 0xc7) /* Secure Encrypted Virtualization command */ enum sev_cmd_id { From patchwork Mon Mar 30 01:37:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 11464401 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1486681 for ; Mon, 30 Mar 2020 01:38:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D2C7820780 for ; Mon, 30 Mar 2020 01:38:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="3A6XlOvB" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728468AbgC3Bip (ORCPT ); Sun, 29 Mar 2020 21:38:45 -0400 Received: from mail-eopbgr750043.outbound.protection.outlook.com ([40.107.75.43]:19870 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728297AbgC3Bip (ORCPT ); Sun, 29 Mar 2020 21:38:45 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=l1Bd9wRwx9T7uF5FUVTjS4aPX+eFYTQHyK1faHvVCc98kzpINLEHqHwW9PMgrSPcb/ml/olE65r2VnEninXeiS6yhogAzltpc5cnyRHPgA+9kkD7jSphjd7PkQmuU51LuhSVfWg50N1qGsukfckztZVoMoAjpu+7leITcdSCHieF9+v1JGvnNbRmLUolnQxmW/o/blRpWRKszlwFj94bMSKjXlwic8Re+Z6M+XT+/cRi1BQRvcV9A8NduAw5XpfioPCh+W+tRB/JBIGfKRhbJSeqyo7Q1tG36F8+u1q6GcYPIrg8d8AsjVfNbD5SR/V6Itw8CAN+961FsPPwtCznLA== 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-SenderADCheck; bh=o676g9aBnVUbDNobCs1Bakfbgep3HSLh9e8GPmRqNlM=; b=KyvCpD/sNlqh4sDtRDX3+OKjkrRasSU4ZNFE1JdqtNY6ZJVIazDlwfZrs2O0FVOLHmjBUujmhpF0s3BgTrx9jGW9O9gtqKR6uyH7U7JkIAHxYW3j+dYoWT8ttpV3w4SXjBCwIPUS/4ACh0h0X/a1zyQcWuVMLSGMK+q5cyVn2WX/Zq0aS/Uzks43LeGHsbiCDCT47rVKYgl4oEmm9sT6mT6zNwaGaJy4/LlzXahJ9v8vxQqJJN7Kh7TLw4bJJWLtcDlQ9j5Ez0YnIUEUcioBDwGq1/os6hJf3bmibSj6viH0TOcnhn38xCcr/E6l405PqUgVsz6D4FATTumr6n2MDg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=o676g9aBnVUbDNobCs1Bakfbgep3HSLh9e8GPmRqNlM=; b=3A6XlOvBFVYLX+M7lfaXSu+YGxBDT9zXE5H6Kipabx+4CwDe+2e/D3hP7obk9ORZ1uiY5MHOMOPcdbvCs6z3wu1pFoZP0v2C7gNG+Zc1j3kOVmJtc2KZdmKoV0mzTOT44YVLK4tXAHPnAQ/KI3RAdY7Lx60IYRMuf7AG9QsQ9uw= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Ashish.Kalra@amd.com; Received: from DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) by DM5PR12MB1387.namprd12.prod.outlook.com (2603:10b6:3:6c::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.20; Mon, 30 Mar 2020 01:38:05 +0000 Received: from DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c]) by DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c%12]) with mapi id 15.20.2856.019; Mon, 30 Mar 2020 01:38:05 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, bp@suse.de, thomas.lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, rientjes@google.com, srutherford@google.com, luto@kernel.org, brijesh.singh@amd.com Subject: [PATCH v5 13/14] KVM: x86: Introduce new KVM_FEATURE_SEV_LIVE_MIGRATION feature & Custom MSR. Date: Mon, 30 Mar 2020 01:37:56 +0000 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-ClientProxiedBy: SN4PR0501CA0019.namprd05.prod.outlook.com (2603:10b6:803:40::32) To DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by SN4PR0501CA0019.namprd05.prod.outlook.com (2603:10b6:803:40::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2878.9 via Frontend Transport; Mon, 30 Mar 2020 01:38:04 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 2d314986-d87a-4f18-6256-08d7d44afe1b X-MS-TrafficTypeDiagnostic: DM5PR12MB1387:|DM5PR12MB1387: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6430; X-Forefront-PRVS: 0358535363 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR12MB1386.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(10009020)(4636009)(39860400002)(376002)(346002)(366004)(136003)(396003)(186003)(66556008)(6666004)(66476007)(86362001)(6916009)(66946007)(316002)(4326008)(7696005)(52116002)(5660300002)(6486002)(16526019)(26005)(36756003)(478600001)(7416002)(81166006)(81156014)(8936002)(2616005)(8676002)(2906002)(956004)(136400200001);DIR:OUT;SFP:1101; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: XPK0FxuzJthtVwRCrx7wwCxQssMikYyeJn2ISfg/vnTzGUZ0xK8Wk/NrPVhiQS2Nan+C8A4guKuM8nBipiJ6ANJXbTxCyHEDz2/tcSurKp3tXjHF/cWmFzdKVYhy4d+rhV0afKW6iXxNbvDEU5kzMZf75vKGeBjP+/mqxy7N5r6ZbOEPQjmBtXY986jCy4zGpHav9mOhT72ZGc4rTHBODnpxVUsoG9RuzSVo5bmZ8wnzeOuueHBq4B3+TXhZJvjK7poV0Bk8J4jD/E0CruZ4G9+ef7ji0hNifIftehJgsj6azAl2eQrbSGeLBtpqQRX8KiRutHVN/c5cqpQ8kW8F+Dawiuu2gxM6emXCbR0fsMe8FjTv33VMnPCuQoxTSXi0kflCXgZ1lnC9hy1+IwgKMrsMnVb3+0C4E09xguz/IfSYqCnIgDFFxN1g2S5XvqzV1FgQKiPf83JXUik2Vqrne+D66DfAWqptkjQ9dQTln3p2HV6hJgOkeu/ZlhlGxQn4 X-MS-Exchange-AntiSpam-MessageData: BfxoRhZBQcdf9uRrws128kOFZ9Ic3+D/rSfNL+YKfKoKQfFRLu03S0ROOWG4z5L++TnA7hqTEXyHbvhhaCRxEGMloOXAUZIrSzMKKSvg4NeNwldUzxXWtOvpm+cyUemTJxEcHo59FDwjlXYwfi9uQA== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2d314986-d87a-4f18-6256-08d7d44afe1b X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Mar 2020 01:38:05.3739 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: Up6nXld4Re5y4A9ZLbreAfAtl2J70jE5GGAixQ/9hQifCQ+I5/yx1eaInXG6FwLz10wgPftcu53OOZe3UjhEHw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1387 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Ashish Kalra Add new KVM_FEATURE_SEV_LIVE_MIGRATION feature for guest to check for host-side support for SEV live migration. Also add a new custom MSR_KVM_SEV_LIVE_MIG_EN for guest to enable the SEV live migration feature. Also, ensure that _bss_decrypted section is marked as decrypted in the page encryption bitmap. Signed-off-by: Ashish Kalra --- Documentation/virt/kvm/cpuid.rst | 4 ++++ Documentation/virt/kvm/msr.rst | 10 ++++++++++ arch/x86/include/asm/kvm_host.h | 3 +++ arch/x86/include/uapi/asm/kvm_para.h | 5 +++++ arch/x86/kernel/kvm.c | 4 ++++ arch/x86/kvm/cpuid.c | 3 ++- arch/x86/kvm/svm.c | 5 +++++ arch/x86/kvm/x86.c | 7 +++++++ arch/x86/mm/mem_encrypt.c | 14 +++++++++++++- 9 files changed, 53 insertions(+), 2 deletions(-) diff --git a/Documentation/virt/kvm/cpuid.rst b/Documentation/virt/kvm/cpuid.rst index 01b081f6e7ea..fcb191bb3016 100644 --- a/Documentation/virt/kvm/cpuid.rst +++ b/Documentation/virt/kvm/cpuid.rst @@ -86,6 +86,10 @@ KVM_FEATURE_PV_SCHED_YIELD 13 guest checks this feature bit before using paravirtualized sched yield. +KVM_FEATURE_SEV_LIVE_MIGRATION 14 guest checks this feature bit + before enabling SEV live + migration feature. + KVM_FEATURE_CLOCSOURCE_STABLE_BIT 24 host will warn if no guest-side per-cpu warps are expeced in kvmclock diff --git a/Documentation/virt/kvm/msr.rst b/Documentation/virt/kvm/msr.rst index 33892036672d..7cd7786bbb03 100644 --- a/Documentation/virt/kvm/msr.rst +++ b/Documentation/virt/kvm/msr.rst @@ -319,3 +319,13 @@ data: KVM guests can request the host not to poll on HLT, for example if they are performing polling themselves. + +MSR_KVM_SEV_LIVE_MIG_EN: + 0x4b564d06 + + Control SEV Live Migration features. + +data: + Bit 0 enables (1) or disables (0) host-side SEV Live Migration feature. + Bit 1 enables (1) or disables (0) support for SEV Live Migration extensions. + All other bits are reserved. diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index a96ef6338cd2..ad5faaed43c0 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -780,6 +780,9 @@ struct kvm_vcpu_arch { u64 msr_kvm_poll_control; + /* SEV Live Migration MSR (AMD only) */ + u64 msr_kvm_sev_live_migration_flag; + /* * Indicates the guest is trying to write a gfn that contains one or * more of the PTEs used to translate the write itself, i.e. the access diff --git a/arch/x86/include/uapi/asm/kvm_para.h b/arch/x86/include/uapi/asm/kvm_para.h index 2a8e0b6b9805..d9d4953b42ad 100644 --- a/arch/x86/include/uapi/asm/kvm_para.h +++ b/arch/x86/include/uapi/asm/kvm_para.h @@ -31,6 +31,7 @@ #define KVM_FEATURE_PV_SEND_IPI 11 #define KVM_FEATURE_POLL_CONTROL 12 #define KVM_FEATURE_PV_SCHED_YIELD 13 +#define KVM_FEATURE_SEV_LIVE_MIGRATION 14 #define KVM_HINTS_REALTIME 0 @@ -50,6 +51,7 @@ #define MSR_KVM_STEAL_TIME 0x4b564d03 #define MSR_KVM_PV_EOI_EN 0x4b564d04 #define MSR_KVM_POLL_CONTROL 0x4b564d05 +#define MSR_KVM_SEV_LIVE_MIG_EN 0x4b564d06 struct kvm_steal_time { __u64 steal; @@ -122,4 +124,7 @@ struct kvm_vcpu_pv_apf_data { #define KVM_PV_EOI_ENABLED KVM_PV_EOI_MASK #define KVM_PV_EOI_DISABLED 0x0 +#define KVM_SEV_LIVE_MIGRATION_ENABLED (1 << 0) +#define KVM_SEV_LIVE_MIGRATION_EXTENSIONS_SUPPORTED (1 << 1) + #endif /* _UAPI_ASM_X86_KVM_PARA_H */ diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c index 6efe0410fb72..8fcee0b45231 100644 --- a/arch/x86/kernel/kvm.c +++ b/arch/x86/kernel/kvm.c @@ -418,6 +418,10 @@ static void __init sev_map_percpu_data(void) if (!sev_active()) return; + if (kvm_para_has_feature(KVM_FEATURE_SEV_LIVE_MIGRATION)) { + wrmsrl(MSR_KVM_SEV_LIVE_MIG_EN, KVM_SEV_LIVE_MIGRATION_ENABLED); + } + for_each_possible_cpu(cpu) { __set_percpu_decrypted(&per_cpu(apf_reason, cpu), sizeof(apf_reason)); __set_percpu_decrypted(&per_cpu(steal_time, cpu), sizeof(steal_time)); diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index b1c469446b07..74c8b2a7270c 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -716,7 +716,8 @@ static inline int __do_cpuid_func(struct kvm_cpuid_entry2 *entry, u32 function, (1 << KVM_FEATURE_ASYNC_PF_VMEXIT) | (1 << KVM_FEATURE_PV_SEND_IPI) | (1 << KVM_FEATURE_POLL_CONTROL) | - (1 << KVM_FEATURE_PV_SCHED_YIELD); + (1 << KVM_FEATURE_PV_SCHED_YIELD) | + (1 << KVM_FEATURE_SEV_LIVE_MIGRATION); if (sched_info_on()) entry->eax |= (1 << KVM_FEATURE_STEAL_TIME); diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index d74773573811..8c6839a97974 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -7632,12 +7632,17 @@ static int svm_page_enc_status_hc(struct kvm *kvm, unsigned long gpa, unsigned long npages, unsigned long enc) { struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct kvm_vcpu *vcpu = kvm->vcpus[0]; gfn_t gfn_start, gfn_end; int ret; if (!sev_guest(kvm)) return -EINVAL; + if (!(vcpu->arch.msr_kvm_sev_live_migration_flag & + KVM_SEV_LIVE_MIGRATION_ENABLED)) + return -ENOTTY; + if (!npages) return 0; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 2127ed937f53..82867b8798f8 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2880,6 +2880,10 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) vcpu->arch.msr_kvm_poll_control = data; break; + case MSR_KVM_SEV_LIVE_MIG_EN: + vcpu->arch.msr_kvm_sev_live_migration_flag = data; + break; + case MSR_IA32_MCG_CTL: case MSR_IA32_MCG_STATUS: case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: @@ -3126,6 +3130,9 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) case MSR_KVM_POLL_CONTROL: msr_info->data = vcpu->arch.msr_kvm_poll_control; break; + case MSR_KVM_SEV_LIVE_MIG_EN: + msr_info->data = vcpu->arch.msr_kvm_sev_live_migration_flag; + break; case MSR_IA32_P5_MC_ADDR: case MSR_IA32_P5_MC_TYPE: case MSR_IA32_MCG_CAP: diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index c9800fa811f6..f6a841494845 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -502,8 +502,20 @@ void __init mem_encrypt_init(void) * With SEV, we need to make a hypercall when page encryption state is * changed. */ - if (sev_active()) + if (sev_active()) { + unsigned long nr_pages; + pv_ops.mmu.page_encryption_changed = set_memory_enc_dec_hypercall; + + /* + * Ensure that _bss_decrypted section is marked as decrypted in the + * page encryption bitmap. + */ + nr_pages = DIV_ROUND_UP(__end_bss_decrypted - __start_bss_decrypted, + PAGE_SIZE); + set_memory_enc_dec_hypercall((unsigned long)__start_bss_decrypted, + nr_pages, 0); + } #endif pr_info("AMD %s active\n", From patchwork Mon Mar 30 01:38:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 11464403 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2AE8781 for ; Mon, 30 Mar 2020 01:39:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id F391920842 for ; Mon, 30 Mar 2020 01:39:06 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="YmQPxrzo" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728696AbgC3BjD (ORCPT ); Sun, 29 Mar 2020 21:39:03 -0400 Received: from mail-eopbgr750048.outbound.protection.outlook.com ([40.107.75.48]:39303 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727954AbgC3BjC (ORCPT ); Sun, 29 Mar 2020 21:39:02 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=devBj8pIJaIdRUVpZz+W3ivaf55TxBK17QFF5XuTQMsGyBgqDI99EaMC6HsLKB/la+4Y4wgweNS1oG09Jv4rt/HiKeEEpRDDg3qrhJbUj9M/ff3RbgMnXClVMGfOhAqCZIIpom++ieTku4Hz9I/dZd+b2pckbeaSYhe5qhi/lCKUhfmpx6nH2Pfq+uigVfOGIMJOY5uKmfywrySwPZ4uQinWUPcrEAk25Ftjd3YcMUuNrX+q/qe2MLgokLgtSYEdhRlMqWxrFnHjiyo5XfunBCv7d02oMCeidSFXzlnWE0I+6589V5EPdC+Q+90IpW7GmfTO7lKAM8/3R+wp81DCEw== 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-SenderADCheck; bh=aodWKOdHZfBvB6+8w8EpuzzbFUtA2r4yfbVD9G2bT3Y=; b=L+/uvD+dcjn/101fwCcFeXhkrKJ9SZb664elBp/WyFM98cclgs/SEnFYkisWmSO7I90QmZvvswb7xJN98CfVURvtSMHcVB0cf1TWuQ8w2LZo2Bz6vJnThrhyckbEdjaNKBbRECyPs2f+a6FlINbZ+3go+NqR7vlC6/5kVB4PJGTq9/7gaIjVpF4Yt/Lc4CeEeR+cmXexnFnwZJyl2jCh8GVEntxLSkr5upnSgJfaaOIVl91jM653R89DZ8KgGvTwpH0QovXkQ65XJIP9yrEB3TeNgxgfpSum6kyDLg5Qs65ldU3N+EbxbfE7cX+YtXhy3x5F5YlW7eqZsd0pdXd2Jw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=aodWKOdHZfBvB6+8w8EpuzzbFUtA2r4yfbVD9G2bT3Y=; b=YmQPxrzo7PVJ+rgqQMBAxdKe3GDygRhDP6EG9YDD1YO1Jk6DkJEQj3yIvvL6ZmxJ1Ye1SARLlGCo8dUT09ylAaLeR+Wdi29JRoPdfGM20VmfS8RZ5YsPr4JXxL9Sr0zngONez0NyqHWvoo2ajFujGYpZzKqUb+sE+C1ycgymtOA= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Ashish.Kalra@amd.com; Received: from DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) by DM5PR12MB1387.namprd12.prod.outlook.com (2603:10b6:3:6c::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.20; Mon, 30 Mar 2020 01:38:22 +0000 Received: from DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c]) by DM5PR12MB1386.namprd12.prod.outlook.com ([fe80::969:3d4e:6f37:c33c%12]) with mapi id 15.20.2856.019; Mon, 30 Mar 2020 01:38:22 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, bp@suse.de, thomas.lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, rientjes@google.com, srutherford@google.com, luto@kernel.org, brijesh.singh@amd.com Subject: [PATCH v5 14/14] KVM: x86: Add kexec support for SEV Live Migration. Date: Mon, 30 Mar 2020 01:38:12 +0000 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-ClientProxiedBy: SN6PR08CA0023.namprd08.prod.outlook.com (2603:10b6:805:66::36) To DM5PR12MB1386.namprd12.prod.outlook.com (2603:10b6:3:77::9) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by SN6PR08CA0023.namprd08.prod.outlook.com (2603:10b6:805:66::36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2856.19 via Frontend Transport; Mon, 30 Mar 2020 01:38:21 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: cc387a04-eb4e-4265-7db5-08d7d44b0855 X-MS-TrafficTypeDiagnostic: DM5PR12MB1387:|DM5PR12MB1387: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2512; X-Forefront-PRVS: 0358535363 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR12MB1386.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(10009020)(4636009)(39860400002)(376002)(346002)(366004)(136003)(396003)(186003)(66556008)(6666004)(66476007)(86362001)(6916009)(66946007)(316002)(4326008)(7696005)(52116002)(5660300002)(6486002)(16526019)(26005)(36756003)(478600001)(7416002)(81166006)(81156014)(8936002)(2616005)(8676002)(2906002)(956004)(136400200001);DIR:OUT;SFP:1101; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xoeVFpd86DOsrB8t9uDMsSSBYFM8u6obIAmhhm/0quWGUDQV/l4tScEmjj3sOoOnEAN88tD2AUS4V3hq89fNsEJ4K8mzuVOL2QsnRWUf69FF3dq9EyrvoyYjhRYyySQJhML8835Q7g1EzcdGvBNHQcunQAk3mXcBddi1YM1UYxTKAEAShL9wk3EqARCXXoUtTI2LI1U+XWt7ov5zq8RWaoUJod34i7cwppsRozn738cB8zyQJAapc8t401mM5WclhTsu3e1++CusyibMJvY7h3WTSDVPpM5TbQGR+Gr/NDXuazAJZOpHh/VM2tN7wcfI9/fDBaQpsezZ6JioFmJ/wF3LQHQrbd/IXqkjJMgcqlViAwhkac7Uc1gD4QaBRy2c23gmk6P3x3+51UAeSRj+igsA81T/B8viA+cTxsi3pWxvNXp5Z2Xfv3GCZbEQvQW2488rkIgxc3sqF6g7spbeZ7NPBgyrj1SRDLgv9ED9b0JL98lpnOYaTtFXd68jPTJ2 X-MS-Exchange-AntiSpam-MessageData: 5Y5CERaaduFaaKIx0+jO2D1aKkA3qyk7eU+6LSV0FhjQ85S4UkhCcMzLVDNKDa1xXxEBtc2ns2bATDdgBlNyK049yd3cY3ZNlxcbgtOs6P+OsHNhOpNf8g3FU66fCu9zml/tA6/MGVk4unnjSk164A== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: cc387a04-eb4e-4265-7db5-08d7d44b0855 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Mar 2020 01:38:22.5290 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: Jbs2Hb3udl0jB4KiKXGf3INsnQrerWtjww45vSBVVirpVurtiTanW31rGX/N0UxVGu7cPojBS/aiybSsPcnHWg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1387 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Ashish Kalra Reset the host's page encryption bitmap related to kernel specific page encryption status settings before we load a new kernel by kexec. We cannot reset the complete page encryption bitmap here as we need to retain the UEFI/OVMF firmware specific settings. Signed-off-by: Ashish Kalra --- arch/x86/kernel/kvm.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c index 8fcee0b45231..ba6cce3c84af 100644 --- a/arch/x86/kernel/kvm.c +++ b/arch/x86/kernel/kvm.c @@ -34,6 +34,7 @@ #include #include #include +#include static int kvmapf = 1; @@ -357,6 +358,33 @@ static void kvm_pv_guest_cpu_reboot(void *unused) */ if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) wrmsrl(MSR_KVM_PV_EOI_EN, 0); + /* + * Reset the host's page encryption bitmap related to kernel + * specific page encryption status settings before we load a + * new kernel by kexec. NOTE: We cannot reset the complete + * page encryption bitmap here as we need to retain the + * UEFI/OVMF firmware specific settings. + */ + if (kvm_para_has_feature(KVM_FEATURE_SEV_LIVE_MIGRATION) && + (smp_processor_id() == 0)) { + unsigned long nr_pages; + int i; + + for (i = 0; i < e820_table->nr_entries; i++) { + struct e820_entry *entry = &e820_table->entries[i]; + unsigned long start_pfn, end_pfn; + + if (entry->type != E820_TYPE_RAM) + continue; + + start_pfn = entry->addr >> PAGE_SHIFT; + end_pfn = (entry->addr + entry->size) >> PAGE_SHIFT; + nr_pages = DIV_ROUND_UP(entry->size, PAGE_SIZE); + + kvm_sev_hypercall3(KVM_HC_PAGE_ENC_STATUS, + entry->addr, nr_pages, 1); + } + } kvm_pv_disable_apf(); kvm_disable_steal_time(); }