From patchwork Wed Jul 10 20:23:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 11038937 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E0EEB13B1 for ; Wed, 10 Jul 2019 20:33:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D3F1D28988 for ; Wed, 10 Jul 2019 20:33:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C60AF2899E; Wed, 10 Jul 2019 20:33:29 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.0 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 3E51828988 for ; Wed, 10 Jul 2019 20:33:29 +0000 (UTC) Received: from localhost ([::1]:36908 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hlJHM-0005Iz-Ix for patchwork-qemu-devel@patchwork.kernel.org; Wed, 10 Jul 2019 16:33:28 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57218) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hlJ7S-0001cg-01 for qemu-devel@nongnu.org; Wed, 10 Jul 2019 16:23:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hlJ7Q-0002XR-7s for qemu-devel@nongnu.org; Wed, 10 Jul 2019 16:23:13 -0400 Received: from mail-eopbgr790080.outbound.protection.outlook.com ([40.107.79.80]:1472 helo=NAM03-CO1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hlJ7Q-0002MZ-0B for qemu-devel@nongnu.org; Wed, 10 Jul 2019 16:23:12 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=hgwBKSesx8nNb/GpifaMtqM9wXT9opm01pUp8M3qvcY=; b=NnVMfFyr0QIjLH7h1BjFw5sFuUO0OkkFf5yR0iGZC8ImdZhDmUD5csyvPj2f8z/w1gJmsOMQK1JvgdP50L7mYVRob3g141LXTJi/NHgOdZkeZm2wxEPn/wEvuwoWHcmTbip0dw6kdjCxeSLG7TsLP4sUAhvZSvsZi2Xp9AbWeMU= Received: from DM6PR12MB2682.namprd12.prod.outlook.com (20.176.116.31) by DM6PR12MB3820.namprd12.prod.outlook.com (10.255.173.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2052.18; Wed, 10 Jul 2019 20:23:09 +0000 Received: from DM6PR12MB2682.namprd12.prod.outlook.com ([fe80::bc1a:a30d:9da2:1cdd]) by DM6PR12MB2682.namprd12.prod.outlook.com ([fe80::bc1a:a30d:9da2:1cdd%6]) with mapi id 15.20.2073.008; Wed, 10 Jul 2019 20:23:09 +0000 From: "Singh, Brijesh" To: "qemu-devel@nongnu.org" Thread-Topic: [PATCH v2 10/13] target/i386: sev: add support to load incoming encrypted page Thread-Index: AQHVN11Ji+wliLaTF0elg6lb5gX0sg== Date: Wed, 10 Jul 2019 20:23:07 +0000 Message-ID: <20190710202219.25939-11-brijesh.singh@amd.com> References: <20190710202219.25939-1-brijesh.singh@amd.com> In-Reply-To: <20190710202219.25939-1-brijesh.singh@amd.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: DM3PR11CA0020.namprd11.prod.outlook.com (2603:10b6:0:54::30) To DM6PR12MB2682.namprd12.prod.outlook.com (2603:10b6:5:4a::31) authentication-results: spf=none (sender IP is ) smtp.mailfrom=brijesh.singh@amd.com; x-ms-exchange-messagesentrepresentingtype: 1 x-mailer: git-send-email 2.17.1 x-originating-ip: [165.204.77.1] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 32fd1439-a55b-404f-c238-08d705746bb4 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600148)(711020)(4605104)(1401327)(4618075)(2017052603328)(7193020); SRVR:DM6PR12MB3820; x-ms-traffictypediagnostic: DM6PR12MB3820: x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:1186; x-forefront-prvs: 0094E3478A x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(4636009)(366004)(346002)(39860400002)(136003)(376002)(396003)(189003)(199004)(256004)(8936002)(2616005)(1076003)(316002)(14444005)(476003)(81166006)(478600001)(54906003)(81156014)(2351001)(8676002)(486006)(25786009)(6436002)(6486002)(4326008)(66476007)(66946007)(446003)(53936002)(50226002)(71190400001)(66446008)(68736007)(6506007)(14454004)(386003)(5660300002)(36756003)(66066001)(186003)(6512007)(99286004)(305945005)(102836004)(26005)(2501003)(11346002)(64756008)(52116002)(5640700003)(66556008)(76176011)(86362001)(6116002)(6916009)(3846002)(7736002)(71200400001)(2906002); DIR:OUT; SFP:1101; SCL:1; SRVR:DM6PR12MB3820; H:DM6PR12MB2682.namprd12.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; MX:1; A:1; received-spf: None (protection.outlook.com: amd.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: 08gmRQfJp2ynmBEA8ZmI/1Jq1FWCBQ6PYNeJTEiWJF16b8fO42tOMVX8fKnSp7E8dI7xLx4VW4gkxEnk7medwLzpuAIrZ4moLxSnMsvJK3iKKtqHmfEWykIewLpQHy8m+28fTmzBl4+7Gpa8fs4+yhwxkR2D+kBg2znzvXbefV/Cmg1Cj9ic/phL9iNfEdx4L0cTv2fbDRHaQLplTfUgO3oTaUclDXymd5PFWzJ3nBgMJDyykNcwX1PsuilBX7ROD9VhO1besqtuh6ojKBLyTNw3rBDHYiJ9j5nHvrvGA2xIZ9u+1NheNGtA2yM1CF5ZDKc/BOSRIA8e2/pJzYBX4DL2XEjLLzvF6d2y2Zv5KHoixaF1VnQEWjIaeukD4AtnS0djoUsMbMkgbaabxAZa75Sx4iJmNzXzevRac1uZpWM= MIME-Version: 1.0 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 32fd1439-a55b-404f-c238-08d705746bb4 X-MS-Exchange-CrossTenant-originalarrivaltime: 10 Jul 2019 20:23:07.9127 (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: sbrijesh@amd.com X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB3820 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 40.107.79.80 Subject: [Qemu-devel] [PATCH v2 10/13] target/i386: sev: add support to load incoming encrypted page X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "pbonzini@redhat.com" , "Lendacky, Thomas" , "Singh, Brijesh" , "dgilbert@redhat.com" , "ehabkost@redhat.com" Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP The sev_load_incoming_page() provide the implementation to read the incoming guest private pages from the socket and load it into the guest memory. The routines uses the RECEIVE_START command to create the incoming encryption context on the first call then uses the RECEIEVE_UPDATE_DATA command to load the encrypted pages into the guest memory. After migration is completed, we issue the RECEIVE_FINISH command to transition the SEV guest to the runnable state so that it can be executed. Signed-off-by: Brijesh Singh --- accel/kvm/kvm-all.c | 1 + target/i386/sev.c | 126 ++++++++++++++++++++++++++++++++++++++- target/i386/trace-events | 3 + 3 files changed, 129 insertions(+), 1 deletion(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index a9fb447248..7f94dba6f9 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -1793,6 +1793,7 @@ static int kvm_init(MachineState *ms) kvm_state->memcrypt_encrypt_data = sev_encrypt_data; kvm_state->memcrypt_save_outgoing_page = sev_save_outgoing_page; + kvm_state->memcrypt_load_incoming_page = sev_load_incoming_page; } ret = kvm_arch_init(ms, s); diff --git a/target/i386/sev.c b/target/i386/sev.c index 28b36c8035..09a62d6f88 100644 --- a/target/i386/sev.c +++ b/target/i386/sev.c @@ -708,13 +708,34 @@ sev_launch_finish(SEVState *s) } } +static int +sev_receive_finish(SEVState *s) +{ + int error, ret = 1; + + trace_kvm_sev_receive_finish(); + ret = sev_ioctl(s->sev_fd, KVM_SEV_RECEIVE_FINISH, 0, &error); + if (ret) { + error_report("%s: RECEIVE_FINISH ret=%d fw_error=%d '%s'", + __func__, ret, error, fw_error_to_str(error)); + goto err; + } + + sev_set_guest_state(SEV_STATE_RUNNING); +err: + return ret; +} + + static void sev_vm_state_change(void *opaque, int running, RunState state) { SEVState *s = opaque; if (running) { - if (!sev_check_state(SEV_STATE_RUNNING)) { + if (sev_check_state(SEV_STATE_RECEIVE_UPDATE)) { + sev_receive_finish(s); + } else if (!sev_check_state(SEV_STATE_RUNNING)) { sev_launch_finish(s); } } @@ -1065,6 +1086,109 @@ int sev_save_outgoing_page(void *handle, QEMUFile *f, uint8_t *ptr, return sev_send_update_data(s, f, ptr, sz, bytes_sent); } +static int +sev_receive_start(QSevGuestInfo *sev, QEMUFile *f) +{ + int ret = 1; + int fw_error; + struct kvm_sev_receive_start *start; + gchar *session = NULL, *pdh_cert = NULL; + + start = g_new0(struct kvm_sev_receive_start, 1); + + /* get SEV guest handle */ + start->handle = object_property_get_int(OBJECT(sev), "handle", + &error_abort); + + /* get the source policy */ + start->policy = qemu_get_be32(f); + + /* get source PDH key */ + start->pdh_len = qemu_get_be32(f); + pdh_cert = g_new(gchar, start->pdh_len); + qemu_get_buffer(f, (uint8_t *)pdh_cert, start->pdh_len); + start->pdh_uaddr = (unsigned long)pdh_cert; + + /* get source session data */ + start->session_len = qemu_get_be32(f); + session = g_new(gchar, start->session_len); + qemu_get_buffer(f, (uint8_t *)session, start->session_len); + start->session_uaddr = (unsigned long)session; + + trace_kvm_sev_receive_start(start->policy, session, pdh_cert); + + ret = sev_ioctl(sev_state->sev_fd, KVM_SEV_RECEIVE_START, start, &fw_error); + if (ret < 0) { + error_report("Error RECEIVE_START ret=%d fw_error=%d '%s'", + ret, fw_error, fw_error_to_str(fw_error)); + goto err; + } + + object_property_set_int(OBJECT(sev), start->handle, "handle", &error_abort); + sev_set_guest_state(SEV_STATE_RECEIVE_UPDATE); +err: + g_free(start); + g_free(session); + g_free(pdh_cert); + + return ret; +} + +static int sev_receive_update_data(QEMUFile *f, uint8_t *ptr) +{ + int ret = 1, fw_error = 0; + gchar *hdr = NULL, *trans = NULL; + struct kvm_sev_receive_update_data *update; + + update = g_new0(struct kvm_sev_receive_update_data, 1); + + /* get packet header */ + update->hdr_len = qemu_get_be32(f); + hdr = g_new(gchar, update->hdr_len); + qemu_get_buffer(f, (uint8_t *)hdr, update->hdr_len); + update->hdr_uaddr = (unsigned long)hdr; + + /* get transport buffer */ + update->trans_len = qemu_get_be32(f); + trans = g_new(gchar, update->trans_len); + update->trans_uaddr = (unsigned long)trans; + qemu_get_buffer(f, (uint8_t *)update->trans_uaddr, update->trans_len); + + update->guest_uaddr = (unsigned long) ptr; + update->guest_len = update->trans_len; + + trace_kvm_sev_receive_update_data(trans, ptr, update->guest_len, + hdr, update->hdr_len); + + ret = sev_ioctl(sev_state->sev_fd, KVM_SEV_RECEIVE_UPDATE_DATA, + update, &fw_error); + if (ret) { + error_report("Error RECEIVE_UPDATE_DATA ret=%d fw_error=%d '%s'", + ret, fw_error, fw_error_to_str(fw_error)); + goto err; + } +err: + g_free(trans); + g_free(update); + g_free(hdr); + return ret; +} + +int sev_load_incoming_page(void *handle, QEMUFile *f, uint8_t *ptr) +{ + SEVState *s = (SEVState *)handle; + + /* If this is first buffer and SEV is not in recieiving state then + * use RECEIVE_START command to create a encryption context. + */ + if (!sev_check_state(SEV_STATE_RECEIVE_UPDATE) && + sev_receive_start(s->sev_info, f)) { + return 1; + } + + return sev_receive_update_data(f, ptr); +} + static void sev_register_types(void) { diff --git a/target/i386/trace-events b/target/i386/trace-events index b41516cf9f..609752cca7 100644 --- a/target/i386/trace-events +++ b/target/i386/trace-events @@ -18,3 +18,6 @@ kvm_sev_launch_finish(void) "" kvm_sev_send_start(uint64_t pdh, int l1, uint64_t plat, int l2, uint64_t amd, int l3) "pdh 0x%" PRIx64 " len %d plat 0x%" PRIx64 " len %d amd 0x%" PRIx64 " len %d" kvm_sev_send_update_data(void *src, void *dst, int len) "guest %p trans %p len %d" kvm_sev_send_finish(void) "" +kvm_sev_receive_start(int policy, void *session, void *pdh) "policy 0x%x session %p pdh %p" +kvm_sev_receive_update_data(void *src, void *dst, int len, void *hdr, int hdr_len) "guest %p trans %p len %d hdr %p hdr_len %d" +kvm_sev_receive_finish(void) ""