From patchwork Wed Mar 7 16:50:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 10264495 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id CE0BD602BD for ; Wed, 7 Mar 2018 17:01:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BBFA629716 for ; Wed, 7 Mar 2018 17:01:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B9F3B29774; Wed, 7 Mar 2018 17:01:11 +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=-6.8 required=2.0 tests=BAD_ENC_HEADER,BAYES_00, DKIM_SIGNED,RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B72992974D for ; Wed, 7 Mar 2018 17:01:05 +0000 (UTC) Received: from localhost ([::1]:34399 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1etcR6-00069i-Rh for patchwork-qemu-devel@patchwork.kernel.org; Wed, 07 Mar 2018 12:01:04 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50318) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1etcHg-0004jm-Rn for qemu-devel@nongnu.org; Wed, 07 Mar 2018 11:51:23 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1etcHc-0008Hm-Sg for qemu-devel@nongnu.org; Wed, 07 Mar 2018 11:51:20 -0500 Received: from mail-by2nam03on0062.outbound.protection.outlook.com ([104.47.42.62]:59840 helo=NAM03-BY2-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 1etcHc-0008Fi-FU for qemu-devel@nongnu.org; Wed, 07 Mar 2018 11:51:16 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amd-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=yOiFCfGTN1p3p95Ddw2f7CryuL3dx6CDoqbIRlT8WHc=; b=VxyzW5p9o4PqqehNdDjAqMfwTz5zsri8ADwSIbI1+iEtYh6vTA6z4Zs8NIgpmF6QPbr189C5+6KiJyq84xAT0vliBSLC15b8AUboNLHrM8DDkS0vPgcwvuERgLeV23We19s/zvaiTUj3sbxJaDL5MlHH5NkwsBIIsaZdRQHU0Sw= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=brijesh.singh@amd.com; Received: from wsp141597wss.amd.com (165.204.78.1) by SN1PR12MB0158.namprd12.prod.outlook.com (2a01:111:e400:5144::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.548.13; Wed, 7 Mar 2018 16:51:11 +0000 From: Brijesh Singh To: qemu-devel@nongnu.org Date: Wed, 7 Mar 2018 10:50:21 -0600 Message-Id: <20180307165038.88640-12-brijesh.singh@amd.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180307165038.88640-1-brijesh.singh@amd.com> References: <20180307165038.88640-1-brijesh.singh@amd.com> MIME-Version: 1.0 X-Originating-IP: [165.204.78.1] X-ClientProxiedBy: SN4PR0501CA0095.namprd05.prod.outlook.com (2603:10b6:803:22::33) To SN1PR12MB0158.namprd12.prod.outlook.com (2a01:111:e400:5144::17) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: c6cc6405-fe69-4aa9-a77b-08d5844ba24b X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(7020095)(4652020)(48565401081)(5600026)(4604075)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603328)(7153060)(7193020); SRVR:SN1PR12MB0158; X-Microsoft-Exchange-Diagnostics: 1; SN1PR12MB0158; 3:8++zuVkVm0DnusAcHxfrEEMTRk2ei+gY6gjcrEP+pbILTQSQ/twOd+1wndA6ncg9rHuzIgWuSwYZHpjNaq+iZ3ulpwUQCamvlYrd2PANyV5TzBS1BcMpjxewZwBHlykYuUmEpAF1dsHtmWuSMwojHHzbW6m/3QQR7+/EOjqDRri366VwnJYSYTAJatfsCaN7xz8RWaHhO3fJn+IIFT1BXJ9ueiuUD52jcsCkK40Gi+zZ9QuBqDsIccCAKC5KTCWq; 25:y96YHwdZZ3i+Lh20Fq2OKsSWHRMWf40iSq65uuqbGb6aMp8bX0kL/kpT0LXJ4QpOlq7RRwlPUUfoNKyxsIIt82ZqGeb7zbOPWYabKi9wHDBB9N6YJ0Fl8UGGRIB+2HNYNw6jysqxAMtgPF6NF0VBlCuZzPN471z5gZIbAjtsBW8gSX7UAs3inJJjMH6QglDOBmuiv0hp0TMQUivKMylQkZzRz+UP6z6GxIBm1JobSfycBbiLlTDBQRSzzIeAnJD5Tp+rtJUrXP90I9P2ql37icxMsBPx5kp78AcsdBMDh1r2qE/CTK8xJOwCsHjT0zFj95PcVWsCRp3FvjCYzSoIeg==; 31:xCR5EoR2M4yS9YEGdOna2WkKZAd+f4MbrjcHMNlCi9VA8SL4X+rrWuPKzOAWRUnU+TqA18cgQenqWCozpsyeNMnt9a1KbOIoBJRkugYnntKPX0lNKpes8yJ+qDmmgbA2tZ9c1xN4LC6oLqy+QQ8RIOfmoikLNP7wsTMlJnKECBxavi0DqyXdJYITzeKzQt+atrJNkiw+q8jcdLkk3G3MAM/1azAxDZgcaSMQwNk+WIU= X-MS-TrafficTypeDiagnostic: SN1PR12MB0158: X-Microsoft-Exchange-Diagnostics: 1; SN1PR12MB0158; 20:1tgQDYMEYpxCmOX5S7xcpMZFOL5VAo7LNBqvpdWNAMJhqtyqSWo7uZrbOOVPnHY4hsnRjKj2DVe3rXsk5L5QtqSS7kfNjLOxTZkrWOjDe+43q2ZTQbO/T9vqFkFggW97BUu1myHvg2IaAzgTZ4Hz/SX8rtFqqu8rpzBQ3davia6r4X5aw9uRqB0PE+eK438fxQg1MyvRvDw1+rfudPXXblnVFUdkqOBQqL9DwCf8GnWkSz2Wm4MaH2LnmZ1Cc1rg251QD5zYnZpBS8ApTaa3uZnl4oJs2UDfEhHTo38h6foONaYr7cdCpQTKIRaUpD8LrHlgbbel42B6Fuo4cR2jM47Vbr5NUxOpn6Cl6ZIVa9VAsWmzpfGoHS/tZNAAXgrK863B+DDulJLJTrLcAR2fUT876H/mWuA134WD9veV7my+ZYsh5AnHDHStDT4EJoXVdcOwEP/8sZ9tQKPvRHnv5QyO1CjBe692u4n476fP2iNgmQjsvkmoYYkXRNZL8iCO; 4:vL1CExrySBvzhCOmiSwkNtnsgS+3RpiUQCNALpqhcdN9r26IyWafMIbQPL4Oo6XTgGy0obTER7ztPfIoSwF+kHhy7LyhO0t3rgY73gpCwH5JgDeHCl0lWjcXz8vZ5hlo7rhEw6VL+2ZuzX38XGwlBd6ZASFj6P4YvcYD8l3GCLiRedxdk2ykZAo1syfWWp75Byz/xc8R7taffvywhx17XNUzLdsr0Tnbsv89JfuItvHdylv2Gbj9KDrz4o7fh9ZiHXhuuS6mE2VakLFsy7bhr223frKODvdhO7VrF2Vd0lAYAQoRmBmS5s7FdHPCU45W X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(8211001083)(6040501)(2401047)(5005006)(8121501046)(93006095)(93001095)(10201501046)(3231220)(944501244)(52105095)(3002001)(6055026)(6041288)(20161123562045)(20161123564045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123558120)(20161123560045)(6072148)(201708071742011); SRVR:SN1PR12MB0158; BCL:0; PCL:0; RULEID:; SRVR:SN1PR12MB0158; X-Forefront-PRVS: 0604AFA86B X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(1496009)(39380400002)(39860400002)(376002)(346002)(396003)(366004)(189003)(199004)(53936002)(8666007)(2950100002)(6486002)(6666003)(6916009)(53416004)(3846002)(1076002)(6116002)(4326008)(54906003)(8656006)(2906002)(25786009)(59450400001)(386003)(68736007)(478600001)(48376002)(50466002)(105586002)(2361001)(2351001)(86362001)(66066001)(106356001)(47776003)(186003)(16526019)(51416003)(76176011)(52116002)(7696005)(26005)(16586007)(316002)(50226002)(8936002)(8676002)(81156014)(81166006)(36756003)(7416002)(97736004)(305945005)(7736002)(39060400002)(5660300001); DIR:OUT; SFP:1101; SCL:1; SRVR:SN1PR12MB0158; H:wsp141597wss.amd.com; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; SN1PR12MB0158; 23:krwemhhnTwZBr//cqYQEK3N2G8brFT2S6kBpxANzL?= =?us-ascii?Q?6AHjSNEzVpuYvaINrDCKvJ1RCdHNCu/i0ugYhwcHRs9LLwKBC2nxiMfbEBMY?= =?us-ascii?Q?DZIgJxZKi2bpKFyZJ+b8njbq5QmPsLhKxtkeJOb2WakcZD7TPkkvXKMj+2+v?= =?us-ascii?Q?Wp5rCSzqwbcxYmRrt/iVVprzgNngWUe5I2YDRQ4hGy25ukR0dsvP5HccPuUu?= =?us-ascii?Q?h48cxY9Nfnx4PwRBu2vmttrZDlpKS26NDo03LEyxzV9Rt4AwgUaJvUtsrkxf?= =?us-ascii?Q?SfbCZTcOOPWMONKC6BYDXsqOZnFS6Xhvmxcr/9TwmEA+0S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0V4pULr7vzsWZbNOPov84JKxNKPqATSY7/23/H5ikqtu3FmzmWPScSK48WGMJgh+i8ngSSYo0Xf5kO9F1aoYQZBWpgxLc5uHeqMbZ4dvi9Q2FHp6jZCCN1L0aLW/kOmjKdwPKlI5gG2xCnbJfrCiK0qcISYb/F2Z5/jV7Gt0dJvb4Ol0FPQXQsLbsvz5WYbwQrCL63udVE1gUofBVcDFEhq+9YBjiRq SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; SN1PR12MB0158; 20:565h1if6zFkAJ8g6sa7V+jw18qp3oljB9Y9MsvMzut5yqHONT91EUWVhj/sESG39W0V0ZgW21TO5VmgXpbk1hR5VNf4PTCJQe25AMIPQ5U6+8SDA383XWbq+gazFfpSCpHhlvJhC4JYGbpWWz9oYbvDPmyPgak2ny4hs8AV+Bqx7M7FFuMDQbaRp9oniJgMowp77EKUGF2e8Pe7cJGW4/pmANft7P0AJ4d2WtZoo7CVMx0lHsiSdcVGPpQTz7WEe X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Mar 2018 16:51:11.6667 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c6cc6405-fe69-4aa9-a77b-08d5844ba24b X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB0158 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 104.47.42.62 Subject: [Qemu-devel] [PATCH v11 11/28] sev/i386: add command to initialize the memory encryption context X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , Brijesh Singh , kvm@vger.kernel.org, "Michael S. Tsirkin" , Stefan Hajnoczi , Alexander Graf , "Edgar E. Iglesias" , Markus Armbruster , Bruce Rogers , Christian Borntraeger , Marcel Apfelbaum , Borislav Petkov , Thomas Lendacky , Eduardo Habkost , Richard Henderson , "Dr. David Alan Gilbert" , Alistair Francis , Cornelia Huck , Richard Henderson , Peter Crosthwaite , Paolo Bonzini Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP When memory encryption is enabled, KVM_SEV_INIT command is used to initialize the platform. The command loads the SEV related persistent data from non-volatile storage and initializes the platform context. This command should be first issued before invoking any other guest commands provided by the SEV firmware. Cc: Paolo Bonzini Cc: Richard Henderson Cc: Eduardo Habkost Signed-off-by: Brijesh Singh --- accel/kvm/kvm-all.c | 16 ++++ include/sysemu/sev.h | 22 +++++ stubs/Makefile.objs | 1 + stubs/sev.c | 21 +++++ target/i386/Makefile.objs | 2 +- target/i386/monitor.c | 11 ++- target/i386/sev-stub.c | 41 +++++++++ target/i386/sev.c | 219 ++++++++++++++++++++++++++++++++++++++++++++++ target/i386/sev_i386.h | 29 ++++++ target/i386/trace-events | 3 + 10 files changed, 362 insertions(+), 3 deletions(-) create mode 100644 include/sysemu/sev.h create mode 100644 stubs/sev.c create mode 100644 target/i386/sev-stub.c diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index b91fcb7160d3..a6473522be11 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -38,6 +38,7 @@ #include "qemu/event_notifier.h" #include "trace.h" #include "hw/irq.h" +#include "sysemu/sev.h" #include "hw/boards.h" @@ -103,6 +104,9 @@ struct KVMState #endif KVMMemoryListener memory_listener; QLIST_HEAD(, KVMParkedVcpu) kvm_parked_vcpus; + + /* memory encryption */ + void *memcrypt_handle; }; KVMState *kvm_state; @@ -1636,6 +1640,18 @@ static int kvm_init(MachineState *ms) kvm_state = s; + /* + * if memory encryption object is specified then initialize the memory + * encryption context. + */ + if (ms->memory_encryption) { + kvm_state->memcrypt_handle = sev_guest_init(ms->memory_encryption); + if (!kvm_state->memcrypt_handle) { + ret = -1; + goto err; + } + } + ret = kvm_arch_init(ms, s); if (ret < 0) { goto err; diff --git a/include/sysemu/sev.h b/include/sysemu/sev.h new file mode 100644 index 000000000000..3f6a26e92789 --- /dev/null +++ b/include/sysemu/sev.h @@ -0,0 +1,22 @@ +/* + * QEMU Secure Encrypted Virutualization (SEV) support + * + * Copyright: Advanced Micro Devices, 2016-2018 + * + * Authors: + * Brijesh Singh + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ + +#ifndef QEMU_SEV_H +#define QEMU_SEV_H + +#include "sysemu/kvm.h" + +void *sev_guest_init(const char *id); +int sev_encrypt_data(void *handle, uint8_t *ptr, uint64_t len); +void sev_set_debug_ops(void *handle, MemoryRegion *mr); +#endif diff --git a/stubs/Makefile.objs b/stubs/Makefile.objs index 2d59d8409162..31b36fdfdb88 100644 --- a/stubs/Makefile.objs +++ b/stubs/Makefile.objs @@ -43,3 +43,4 @@ stub-obj-y += xen-common.o stub-obj-y += xen-hvm.o stub-obj-y += pci-host-piix.o stub-obj-y += ram-block.o +stub-obj-y += sev.o diff --git a/stubs/sev.c b/stubs/sev.c new file mode 100644 index 000000000000..4a5cc5569e5f --- /dev/null +++ b/stubs/sev.c @@ -0,0 +1,21 @@ +/* + * QEMU SEV stub + * + * Copyright Advanced Micro Devices 2018 + * + * Authors: + * Brijesh Singh + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ + +#include "qemu/osdep.h" +#include "qemu-common.h" +#include "sysemu/sev.h" + +void *sev_guest_init(const char *id) +{ + return NULL; +} diff --git a/target/i386/Makefile.objs b/target/i386/Makefile.objs index 76aeaeae2750..741cb080eb17 100644 --- a/target/i386/Makefile.objs +++ b/target/i386/Makefile.objs @@ -5,7 +5,7 @@ obj-$(CONFIG_TCG) += int_helper.o mem_helper.o misc_helper.o mpx_helper.o obj-$(CONFIG_TCG) += seg_helper.o smm_helper.o svm_helper.o obj-$(CONFIG_SOFTMMU) += machine.o arch_memory_mapping.o arch_dump.o monitor.o obj-$(CONFIG_KVM) += kvm.o hyperv.o sev.o -obj-$(call lnot,$(CONFIG_KVM)) += kvm-stub.o +obj-$(call lnot,$(CONFIG_KVM)) += kvm-stub.o sev-stub.o # HAX support ifdef CONFIG_WIN32 obj-$(CONFIG_HAX) += hax-all.o hax-mem.o hax-windows.o diff --git a/target/i386/monitor.c b/target/i386/monitor.c index 27b99adf395b..29de61996371 100644 --- a/target/i386/monitor.c +++ b/target/i386/monitor.c @@ -670,6 +670,13 @@ void hmp_info_io_apic(Monitor *mon, const QDict *qdict) SevInfo *qmp_query_sev(Error **errp) { - error_setg(errp, "SEV feature is not available"); - return NULL; + SevInfo *info; + + info = sev_get_info(); + if (!info) { + error_setg(errp, "SEV feature is not available"); + return NULL; + } + + return info; } diff --git a/target/i386/sev-stub.c b/target/i386/sev-stub.c new file mode 100644 index 000000000000..c86d8c139237 --- /dev/null +++ b/target/i386/sev-stub.c @@ -0,0 +1,41 @@ +/* + * QEMU SEV stub + * + * Copyright Advanced Micro Devices 2018 + * + * Authors: + * Brijesh Singh + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ + +#include "qemu/osdep.h" +#include "qemu-common.h" +#include "sev_i386.h" + +SevInfo *sev_get_info(void) +{ + return NULL; +} + +bool sev_enabled(void) +{ + return false; +} + +uint64_t sev_get_me_mask(void) +{ + return ~0; +} + +uint32_t sev_get_cbit_position(void) +{ + return 0; +} + +uint32_t sev_get_reduced_phys_bits(void) +{ + return 0; +} diff --git a/target/i386/sev.c b/target/i386/sev.c index ab42e4a456d2..2c9fd67eaaec 100644 --- a/target/i386/sev.c +++ b/target/i386/sev.c @@ -18,10 +18,88 @@ #include "sysemu/kvm.h" #include "sev_i386.h" #include "sysemu/sysemu.h" +#include "trace.h" #define DEFAULT_GUEST_POLICY 0x1 /* disable debug */ #define DEFAULT_SEV_DEVICE "/dev/sev" +static SEVState *sev_state; + +static const char *const sev_fw_errlist[] = { + "", + "Platform state is invalid", + "Guest state is invalid", + "Platform configuration is invalid", + "Buffer too small", + "Platform is already owned", + "Certificate is invalid", + "Policy is not allowed", + "Guest is not active", + "Invalid address", + "Bad signature", + "Bad measurement", + "Asid is already owned", + "Invalid ASID", + "WBINVD is required", + "DF_FLUSH is required", + "Guest handle is invalid", + "Invalid command", + "Guest is active", + "Hardware error", + "Hardware unsafe", + "Feature not supported", + "Invalid parameter" +}; + +#define SEV_FW_MAX_ERROR ARRAY_SIZE(sev_fw_errlist) + +static int +sev_ioctl(int fd, int cmd, void *data, int *error) +{ + int r; + struct kvm_sev_cmd input; + + memset(&input, 0x0, sizeof(input)); + + input.id = cmd; + input.sev_fd = fd; + input.data = (__u64)data; + + r = kvm_vm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_OP, &input); + + if (error) { + *error = input.error; + } + + return r; +} + +static int +sev_platform_ioctl(int fd, int cmd, void *data, int *error) +{ + int r; + struct sev_issue_cmd arg; + + arg.cmd = cmd; + arg.data = (unsigned long)data; + r = ioctl(fd, SEV_ISSUE_CMD, &arg); + if (error) { + *error = arg.error; + } + + return r; +} + +static const char * +fw_error_to_str(int code) +{ + if (code >= SEV_FW_MAX_ERROR) { + return "unknown error"; + } + + return sev_fw_errlist[code]; +} + static void qsev_guest_finalize(Object *obj) { @@ -219,6 +297,147 @@ static const TypeInfo qsev_guest_info = { } }; +static QSevGuestInfo * +lookup_sev_guest_info(const char *id) +{ + Object *obj; + QSevGuestInfo *info; + + obj = object_resolve_path_component(object_get_objects_root(), id); + if (!obj) { + return NULL; + } + + info = (QSevGuestInfo *) + object_dynamic_cast(obj, TYPE_QSEV_GUEST_INFO); + if (!info) { + return NULL; + } + + return info; +} + +bool +sev_enabled(void) +{ + return sev_state ? true : false; +} + +uint64_t +sev_get_me_mask(void) +{ + return sev_state ? sev_state->me_mask : ~0; +} + +uint32_t +sev_get_cbit_position(void) +{ + return sev_state ? sev_state->cbitpos : 0; +} + +uint32_t +sev_get_reduced_phys_bits(void) +{ + return sev_state ? sev_state->reduced_phys_bits : 0; +} + +SevInfo * +sev_get_info(void) +{ + SevInfo *info; + + info = g_new0(SevInfo, 1); + info->enabled = sev_state ? true : false; + + if (info->enabled) { + info->api_major = sev_state->api_major; + info->api_minor = sev_state->api_minor; + info->build_id = sev_state->build_id; + info->policy = sev_state->policy; + info->state = sev_state->state; + info->handle = sev_state->handle; + } + + return info; +} + +void * +sev_guest_init(const char *id) +{ + SEVState *s; + char *devname; + int ret, fw_error; + uint32_t ebx; + uint32_t host_cbitpos; + struct sev_user_data_status status = {}; + + s = g_new0(SEVState, 1); + s->sev_info = lookup_sev_guest_info(id); + if (!s->sev_info) { + error_report("%s: '%s' is not a valid '%s' object", + __func__, id, TYPE_QSEV_GUEST_INFO); + goto err; + } + + sev_state = s; + s->state = SEV_STATE_UNINIT; + + host_cpuid(0x8000001F, 0, NULL, &ebx, NULL, NULL); + host_cbitpos = ebx & 0x3f; + + s->cbitpos = object_property_get_int(OBJECT(s->sev_info), "cbitpos", NULL); + if (host_cbitpos != s->cbitpos) { + error_report("%s: cbitpos check failed, host '%d' requested '%d'", + __func__, host_cbitpos, s->cbitpos); + goto err; + } + + s->reduced_phys_bits = object_property_get_int(OBJECT(s->sev_info), + "reduced-phys-bits", NULL); + if (s->reduced_phys_bits < 1) { + error_report("%s: reduced_phys_bits check failed, it should be >=1," + "' requested '%d'", __func__, s->reduced_phys_bits); + goto err; + } + + s->me_mask = ~(1UL << s->cbitpos); + + devname = object_property_get_str(OBJECT(s->sev_info), "sev-device", NULL); + s->sev_fd = open(devname, O_RDWR); + if (s->sev_fd < 0) { + error_report("%s: Failed to open %s '%s'", __func__, + devname, strerror(errno)); + goto err; + } + g_free(devname); + + ret = sev_platform_ioctl(s->sev_fd, SEV_PLATFORM_STATUS, &status, + &fw_error); + if (ret) { + error_report("%s: failed to get platform status ret=%d" + "fw_error='%d: %s'", __func__, ret, fw_error, + fw_error_to_str(fw_error)); + goto err; + } + s->build_id = status.build; + s->api_major = status.api_major; + s->api_minor = status.api_minor; + + trace_kvm_sev_init(); + ret = sev_ioctl(s->sev_fd, KVM_SEV_INIT, NULL, &fw_error); + if (ret) { + error_report("%s: failed to initialize ret=%d fw_error=%d '%s'", + __func__, ret, fw_error, fw_error_to_str(fw_error)); + goto err; + } + + return s; +err: + g_free(sev_state); + sev_state = NULL; + return NULL; +} + static void sev_register_types(void) { diff --git a/target/i386/sev_i386.h b/target/i386/sev_i386.h index caf879c3b874..c06be12c8079 100644 --- a/target/i386/sev_i386.h +++ b/target/i386/sev_i386.h @@ -14,10 +14,17 @@ #ifndef QEMU_SEV_I386_H #define QEMU_SEV_I386_H +#include +#include + +#include + #include "qom/object.h" #include "qapi/error.h" #include "sysemu/kvm.h" +#include "sysemu/sev.h" #include "qemu/error-report.h" +#include "qapi/qapi-commands-misc.h" #define SEV_POLICY_NODBG 0x1 #define SEV_POLICY_NOKS 0x2 @@ -30,6 +37,12 @@ #define QSEV_GUEST_INFO(obj) \ OBJECT_CHECK(QSevGuestInfo, (obj), TYPE_QSEV_GUEST_INFO) +extern bool sev_enabled(void); +extern uint64_t sev_get_me_mask(void); +extern SevInfo *sev_get_info(void); +extern uint32_t sev_get_cbit_position(void); +extern uint32_t sev_get_reduced_phys_bits(void); + typedef struct QSevGuestInfo QSevGuestInfo; typedef struct QSevGuestInfoClass QSevGuestInfoClass; @@ -58,4 +71,20 @@ struct QSevGuestInfoClass { ObjectClass parent_class; }; +struct SEVState { + QSevGuestInfo *sev_info; + uint8_t api_major; + uint8_t api_minor; + uint8_t build_id; + uint32_t policy; + uint64_t me_mask; + uint32_t cbitpos; + uint32_t reduced_phys_bits; + uint32_t handle; + int sev_fd; + SevState state; +}; + +typedef struct SEVState SEVState; + #endif diff --git a/target/i386/trace-events b/target/i386/trace-events index 3153fd445488..797b716751b7 100644 --- a/target/i386/trace-events +++ b/target/i386/trace-events @@ -5,3 +5,6 @@ kvm_x86_fixup_msi_error(uint32_t gsi) "VT-d failed to remap interrupt for GSI %" kvm_x86_add_msi_route(int virq) "Adding route entry for virq %d" kvm_x86_remove_msi_route(int virq) "Removing route entry for virq %d" kvm_x86_update_msi_routes(int num) "Updated %d MSI routes" + +# target/i386/sev.c +kvm_sev_init(void) ""