From patchwork Thu Feb 4 00:37:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 12065849 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5E7D2C433E0 for ; Thu, 4 Feb 2021 00:39:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0209E614A5 for ; Thu, 4 Feb 2021 00:39:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233789AbhBDAis (ORCPT ); Wed, 3 Feb 2021 19:38:48 -0500 Received: from mail-dm6nam12on2045.outbound.protection.outlook.com ([40.107.243.45]:57185 "EHLO NAM12-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S234122AbhBDAim (ORCPT ); Wed, 3 Feb 2021 19:38:42 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=l4hgKWDfV4YL7uPBCuj4Puaog7b8yAjZiZaBN0khxBv2wQKThLs78MrAmIxtekL27916RTWuwy30q2ZdOgC82gtr8LUlelaES0EIYRBRl5gRsNXX00qCEXbdaLrh6bu66eZH6NJmPAXrAMGj7GdHCV3MBSYoA2JnxvU5L3jmsT2G/KEHKpJv3AtMaBR+16oaZfHRHPczj/abhdzrmE3wBpb+SnQ99754OxNhHPqkAT+lEZXTuhDl5uofIunOAQboZZqVrvEAPjUUJveJBH/jhGi3jlrU7KKzffGsz5lsk57Lk4ILDCtN/rA1d8Tpb/hZTLs+lqmuPvWZcXWuXb+tKw== 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=caa+099V3/AL46PMO/Kd15N1J3cf0E9F3xaBsazqjns=; b=eezr7EkbaRlXWWwbCChAk0xr1rl6IF/r7StqYGmDltR6jaiNBseD6bHoeHX/KkySPx47bOpVsGb8DvPkiPfL34WI6q0FzTvuGPt1+dBNOGJROwSpaTZ/4rYZrmF3csouxyXEJaMj/6+n/pOAJUamzaxhEFxCLteVNTSoyEfu9pzu+2XaqwENVZnZYkPA6ePBvDUZpgY2RFee/lqiRgE1ni83lkGzW3H7MTUb7WPfcPWKOyJNzaa6h8NG6j1mwH4DjHc2Hz5XfLMVXACM8lh7wYMEKXZW4AdojXK5xaKkS5BUg/ks9wvXYXKATFzU00BuWUFHmuBdFRKh1Y9ynW0kOA== 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=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=caa+099V3/AL46PMO/Kd15N1J3cf0E9F3xaBsazqjns=; b=aAzaEsAb5vFxJskfQoJ9apgC4gx50+xvgZqVq8L0xA6/VNE6EWpt3xQR5YGJkJnuPfIsgWqWqUor2bJ5vM/VP2tdT1BpjaUEonAcqeAeJGKPxVTvUxIbnDnkkRvNZHYVOiApTMXinoEjKggJrHOfXTTGTXZDO64ieo+h7HqEocQ= Authentication-Results: redhat.com; dkim=none (message not signed) header.d=none;redhat.com; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2767.namprd12.prod.outlook.com (2603:10b6:805:75::23) by SA0PR12MB4384.namprd12.prod.outlook.com (2603:10b6:806:9f::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3805.16; Thu, 4 Feb 2021 00:37:39 +0000 Received: from SN6PR12MB2767.namprd12.prod.outlook.com ([fe80::24bb:3e53:c95e:cb8e]) by SN6PR12MB2767.namprd12.prod.outlook.com ([fe80::24bb:3e53:c95e:cb8e%7]) with mapi id 15.20.3805.028; Thu, 4 Feb 2021 00:37:39 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, rkrcmar@redhat.com, joro@8bytes.org, bp@suse.de, thomas.lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, srutherford@google.com, seanjc@google.com, venu.busireddy@oracle.com, brijesh.singh@amd.com Subject: [PATCH v10 04/16] KVM: SVM: Add support for KVM_SEV_RECEIVE_START command Date: Thu, 4 Feb 2021 00:37:30 +0000 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR12CA0028.namprd12.prod.outlook.com (2603:10b6:806:6f::33) To SN6PR12MB2767.namprd12.prod.outlook.com (2603:10b6:805:75::23) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by SA0PR12CA0028.namprd12.prod.outlook.com (2603:10b6:806:6f::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3825.20 via Frontend Transport; Thu, 4 Feb 2021 00:37:38 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: f8767e1f-1192-4abe-140e-08d8c8a51330 X-MS-TrafficTypeDiagnostic: SA0PR12MB4384: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2331; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: autB7i8NW+VCQ3MUK7wpimds+RM2Y6ky0prAiMbQiv12W6/yVl7h/uHDmqBr9Ej56VwdnwiyQNpsgzfrjff/5rcdwrIKOwj9G3Vughro6WNGpdMgsNh0Yf12QYcd6adIol94eke2Ysqm+McVnhUX0FfUHdg0Yr/Aw1qdT66tC2qwA0P1b/fQPjyWf6R/RU+4UzH9O01p9xheV3nUk7NhI5szmuvmZOrMjwumOTRHe/qQOrVfx8XYgMmH/Pl+Joq5GWlMseXSmPncht9GrmzBCVAk162IyDfOLXJp02urlYupOnhD9sC1aE4rp3YexHRzMK1gRnlcu/Yg5HJC9G3ifPGfjTLTz02hDtL5BUiEuYSf7QW5I3p0wWlxxRs+OQ4S8gyvo7DkuBbOQ4ngqExH2YQpWtN8s6IjawTUZg8DtyIziG1HKQhNg4TZs5hcHLtx6raks3uz7t+CUoTeJIQ2n35pAnG3Qtbg5qL/d2rZ2rgpD2II/bi/oXIZndvY65ZVaxbt+atK1nFyuFL0HaCVRA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2767.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(39850400004)(366004)(346002)(376002)(396003)(8936002)(4326008)(66556008)(86362001)(6666004)(66476007)(2906002)(186003)(478600001)(26005)(6916009)(6486002)(5660300002)(52116002)(16526019)(36756003)(7696005)(7416002)(66946007)(83380400001)(8676002)(956004)(2616005)(66574015)(316002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: =?utf-8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WxqLt+b2/H+8Z5TsY9?= =?utf-8?q?e49Js2P/RoNd9b5QRA1Pt1Orsh+OHwsmUY88pqeF7qTjict83xm8cF3Rhfuoze9lc?= =?utf-8?q?L/1H1rRGyADuOibwC2rp9AmKWAbVCNfG8rtLg6nAT17bcOjfmldpgcss2vH++K/nu?= =?utf-8?q?pybaFLVWYM6O3VSE1YjbWMDdyhqTax+FvBlIT9XcuLKDROKsnptudn+C6zrGlhcuQ?= =?utf-8?q?SoZReRrgASQsmOwqCVzex4vjsoDOZr/LHfD93t/SoyTD7sbJx/TxCBRotgY5hAQz8?= =?utf-8?q?ih54hkAW47MYUnS4+lAUqPIoB9LK3HLRJZuBxhhwHcxIg0NAev2vvuDW11AUB8rvf?= =?utf-8?q?8Yquw4V1E0WLEpDmNt7z1Nm3O5OR0wVCbej7n10o2M2NethmnFrb6ksUjtNCBIK8m?= =?utf-8?q?I+BKgP+8uENU9E?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: f8767e1f-1192-4abe-140e-08d8c8a51330 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2767.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Feb 2021 00:37:39.1711 (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: 4TKWoO/beSKB7UK7hUd5p+lfVDe3r9u9HV2PECj45sGmeI+nyy8o5O3wahfDX4ya9u5i2lOpoBqnyW5cdd3NQA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4384 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/sev.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 0da0c199efa8..079ac5ac2459 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 pdh_len; + + __u64 session_uaddr; /* 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/sev.c b/arch/x86/kvm/svm/sev.c index 0d117b1e6491..ec0d573cb09a 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -1299,6 +1299,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; +} + int svm_mem_enc_op(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -1358,6 +1436,9 @@ 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 0ff7bed508fc..d2eea75de8b3 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1666,6 +1666,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)