From nobody Sat Nov 23 23:41:48 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1730789269; cv=none; d=zohomail.com; s=zohoarc; b=DfK3ILL9otFDHl2pOXvdqOk0IxAkbK0U+iAdxcUZHe0w4NHyRpClmdIrH8WxNut413au+ZpP2n0mZMpEOnIYZD06r5+chNIZLXNedH4uBQL19fdQ43D8vTGSd7zULY/ML+CFkEAkPMtwnVNFV1y29G8JoIpGvRANp4axgGAddxY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1730789269; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=lssVsLuNC4nya/Nc2kAckX+IDhzoUEe2F8xi643+m50=; b=fby6Ku7h/HUWPW0/pQaFoAbBrw+2ShLcqzB0WfS1HLW5Y3JZ7VtRVRODKbNsFs0PQmFFsP0nz9+AABlIZRgGL5lsiEZMfVbybhEu5+ZoNR5EDlwe6Eb78eMDp/OiF4PILUd0YVRqybFyLXmB+f6Sh5eptaGgR4Xpkb9bghG0cQ0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1730789269476268.0301996003884; Mon, 4 Nov 2024 22:47:49 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t8DD5-0007S7-4N; Tue, 05 Nov 2024 01:38:39 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1t8DCm-0007CR-BZ for qemu-devel@nongnu.org; Tue, 05 Nov 2024 01:38:21 -0500 Received: from mgamail.intel.com ([198.175.65.18]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1t8DCk-0001w0-4q for qemu-devel@nongnu.org; Tue, 05 Nov 2024 01:38:20 -0500 Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by orvoesa110.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Nov 2024 22:38:06 -0800 Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:38:02 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788698; x=1762324698; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ztCvbGM6vH5Ut/+6plgKBcbU9yS14b6PXdQLMgJMQR8=; b=ScoH3BaMkLJg85KwQY/NzxI2Y7wVo6xIMgemXyUkTgpQfXx5mP86h9nq f6vreLbbi9+7oTeZcniCGetReC7yvRCjzxzL6I9eQFToBmdBs3Axq8ZdE n+k2mURsp/PQp1XqZwxVHAqGIwD6UGTaTfSMHnL1cf8DgHfPWLvjMh4Rw xhfQGnvGlbSgrx+mjTtaTN8RJHioKXM6TSF78xMGR6F9rpP0TD8N8rS+H aWKtlVdV1rCO2wLWbXA3UNXe8Bq9PK07hg4RlT3Uxuzm3pOLwGEToXwRP 2qRAbW93blAQjfagKuvravfSkI3xnfVLRa7ZoVl+xazSGS6vwOeVHVeyI Q==; X-CSE-ConnectionGUID: fITmyElfSVy1fAQ+gynuug== X-CSE-MsgGUID: lKeC3msQQtqPNRw08BnlhQ== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689562" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689562" X-CSE-ConnectionGUID: ZLFkEOq6TdaE8lHC8X/acQ== X-CSE-MsgGUID: 3H/zKqOtTnK0X2RtBtjvWA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989023" From: Xiaoyao Li To: Paolo Bonzini , Riku Voipio , Richard Henderson , Zhao Liu , "Michael S. Tsirkin" , Marcel Apfelbaum , Igor Mammedov , Ani Sinha Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Eric Blake , Markus Armbruster , Marcelo Tosatti , rick.p.edgecombe@intel.com, kvm@vger.kernel.org, qemu-devel@nongnu.org, xiaoyao.li@intel.com Subject: [PATCH v6 24/60] i386/tdx: Setup the TD HOB list Date: Tue, 5 Nov 2024 01:23:32 -0500 Message-Id: <20241105062408.3533704-25-xiaoyao.li@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241105062408.3533704-1-xiaoyao.li@intel.com> References: <20241105062408.3533704-1-xiaoyao.li@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=198.175.65.18; envelope-from=xiaoyao.li@intel.com; helo=mgamail.intel.com X-Spam_score_int: -39 X-Spam_score: -4.0 X-Spam_bar: ---- X-Spam_report: (-4.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.34, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.781, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1730789271257116600 Content-Type: text/plain; charset="utf-8" The TD HOB list is used to pass the information from VMM to TDVF. The TD HOB must include PHIT HOB and Resource Descriptor HOB. More details can be found in TDVF specification and PI specification. Build the TD HOB in TDX's machine_init_done callback. Co-developed-by: Isaku Yamahata Signed-off-by: Isaku Yamahata Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann --- Changes in v1: - drop the code of adding mmio resources since OVMF prepares all the MMIO hob itself. --- hw/i386/meson.build | 2 +- hw/i386/tdvf-hob.c | 147 ++++++++++++++++++++++++++++++++++++++++++ hw/i386/tdvf-hob.h | 24 +++++++ target/i386/kvm/tdx.c | 16 +++++ 4 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 hw/i386/tdvf-hob.c create mode 100644 hw/i386/tdvf-hob.h diff --git a/hw/i386/meson.build b/hw/i386/meson.build index 3bc1da2b6eb4..7896f348cff8 100644 --- a/hw/i386/meson.build +++ b/hw/i386/meson.build @@ -32,7 +32,7 @@ i386_ss.add(when: 'CONFIG_PC', if_true: files( 'port92.c')) i386_ss.add(when: 'CONFIG_X86_FW_OVMF', if_true: files('pc_sysfw_ovmf.c'), if_false: files('pc_sysfw_ovmf-stu= bs.c')) -i386_ss.add(when: 'CONFIG_TDX', if_true: files('tdvf.c')) +i386_ss.add(when: 'CONFIG_TDX', if_true: files('tdvf.c', 'tdvf-hob.c')) =20 subdir('kvm') subdir('xen') diff --git a/hw/i386/tdvf-hob.c b/hw/i386/tdvf-hob.c new file mode 100644 index 000000000000..e00de256ea8c --- /dev/null +++ b/hw/i386/tdvf-hob.c @@ -0,0 +1,147 @@ +/* + * SPDX-License-Identifier: GPL-2.0-or-later + + * Copyright (c) 2020 Intel Corporation + * Author: Isaku Yamahata + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License along + * with this program; if not, see . + */ + +#include "qemu/osdep.h" +#include "qemu/log.h" +#include "qemu/error-report.h" +#include "e820_memory_layout.h" +#include "hw/i386/pc.h" +#include "hw/i386/x86.h" +#include "hw/pci/pcie_host.h" +#include "sysemu/kvm.h" +#include "standard-headers/uefi/uefi.h" +#include "tdvf-hob.h" + +typedef struct TdvfHob { + hwaddr hob_addr; + void *ptr; + int size; + + /* working area */ + void *current; + void *end; +} TdvfHob; + +static uint64_t tdvf_current_guest_addr(const TdvfHob *hob) +{ + return hob->hob_addr + (hob->current - hob->ptr); +} + +static void tdvf_align(TdvfHob *hob, size_t align) +{ + hob->current =3D QEMU_ALIGN_PTR_UP(hob->current, align); +} + +static void *tdvf_get_area(TdvfHob *hob, uint64_t size) +{ + void *ret; + + if (hob->current + size > hob->end) { + error_report("TD_HOB overrun, size =3D 0x%" PRIx64, size); + exit(1); + } + + ret =3D hob->current; + hob->current +=3D size; + tdvf_align(hob, 8); + return ret; +} + +static void tdvf_hob_add_memory_resources(TdxGuest *tdx, TdvfHob *hob) +{ + EFI_HOB_RESOURCE_DESCRIPTOR *region; + EFI_RESOURCE_ATTRIBUTE_TYPE attr; + EFI_RESOURCE_TYPE resource_type; + + TdxRamEntry *e; + int i; + + for (i =3D 0; i < tdx->nr_ram_entries; i++) { + e =3D &tdx->ram_entries[i]; + + if (e->type =3D=3D TDX_RAM_UNACCEPTED) { + resource_type =3D EFI_RESOURCE_MEMORY_UNACCEPTED; + attr =3D EFI_RESOURCE_ATTRIBUTE_TDVF_UNACCEPTED; + } else if (e->type =3D=3D TDX_RAM_ADDED) { + resource_type =3D EFI_RESOURCE_SYSTEM_MEMORY; + attr =3D EFI_RESOURCE_ATTRIBUTE_TDVF_PRIVATE; + } else { + error_report("unknown TDX_RAM_ENTRY type %d", e->type); + exit(1); + } + + region =3D tdvf_get_area(hob, sizeof(*region)); + *region =3D (EFI_HOB_RESOURCE_DESCRIPTOR) { + .Header =3D { + .HobType =3D EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, + .HobLength =3D cpu_to_le16(sizeof(*region)), + .Reserved =3D cpu_to_le32(0), + }, + .Owner =3D EFI_HOB_OWNER_ZERO, + .ResourceType =3D cpu_to_le32(resource_type), + .ResourceAttribute =3D cpu_to_le32(attr), + .PhysicalStart =3D cpu_to_le64(e->address), + .ResourceLength =3D cpu_to_le64(e->length), + }; + } +} + +void tdvf_hob_create(TdxGuest *tdx, TdxFirmwareEntry *td_hob) +{ + TdvfHob hob =3D { + .hob_addr =3D td_hob->address, + .size =3D td_hob->size, + .ptr =3D td_hob->mem_ptr, + + .current =3D td_hob->mem_ptr, + .end =3D td_hob->mem_ptr + td_hob->size, + }; + + EFI_HOB_GENERIC_HEADER *last_hob; + EFI_HOB_HANDOFF_INFO_TABLE *hit; + + /* Note, Efi{Free}Memory{Bottom,Top} are ignored, leave 'em zeroed. */ + hit =3D tdvf_get_area(&hob, sizeof(*hit)); + *hit =3D (EFI_HOB_HANDOFF_INFO_TABLE) { + .Header =3D { + .HobType =3D EFI_HOB_TYPE_HANDOFF, + .HobLength =3D cpu_to_le16(sizeof(*hit)), + .Reserved =3D cpu_to_le32(0), + }, + .Version =3D cpu_to_le32(EFI_HOB_HANDOFF_TABLE_VERSION), + .BootMode =3D cpu_to_le32(0), + .EfiMemoryTop =3D cpu_to_le64(0), + .EfiMemoryBottom =3D cpu_to_le64(0), + .EfiFreeMemoryTop =3D cpu_to_le64(0), + .EfiFreeMemoryBottom =3D cpu_to_le64(0), + .EfiEndOfHobList =3D cpu_to_le64(0), /* initialized later */ + }; + + tdvf_hob_add_memory_resources(tdx, &hob); + + last_hob =3D tdvf_get_area(&hob, sizeof(*last_hob)); + *last_hob =3D (EFI_HOB_GENERIC_HEADER) { + .HobType =3D EFI_HOB_TYPE_END_OF_HOB_LIST, + .HobLength =3D cpu_to_le16(sizeof(*last_hob)), + .Reserved =3D cpu_to_le32(0), + }; + hit->EfiEndOfHobList =3D tdvf_current_guest_addr(&hob); +} diff --git a/hw/i386/tdvf-hob.h b/hw/i386/tdvf-hob.h new file mode 100644 index 000000000000..1b737e946a8d --- /dev/null +++ b/hw/i386/tdvf-hob.h @@ -0,0 +1,24 @@ +#ifndef HW_I386_TD_HOB_H +#define HW_I386_TD_HOB_H + +#include "hw/i386/tdvf.h" +#include "target/i386/kvm/tdx.h" + +void tdvf_hob_create(TdxGuest *tdx, TdxFirmwareEntry *td_hob); + +#define EFI_RESOURCE_ATTRIBUTE_TDVF_PRIVATE \ + (EFI_RESOURCE_ATTRIBUTE_PRESENT | \ + EFI_RESOURCE_ATTRIBUTE_INITIALIZED | \ + EFI_RESOURCE_ATTRIBUTE_TESTED) + +#define EFI_RESOURCE_ATTRIBUTE_TDVF_UNACCEPTED \ + (EFI_RESOURCE_ATTRIBUTE_PRESENT | \ + EFI_RESOURCE_ATTRIBUTE_INITIALIZED | \ + EFI_RESOURCE_ATTRIBUTE_TESTED) + +#define EFI_RESOURCE_ATTRIBUTE_TDVF_MMIO \ + (EFI_RESOURCE_ATTRIBUTE_PRESENT | \ + EFI_RESOURCE_ATTRIBUTE_INITIALIZED | \ + EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE) + +#endif diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 76b40f278dd4..6720c785a4ad 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -23,6 +23,7 @@ #include "hw/i386/x86.h" #include "hw/i386/tdvf.h" #include "hw/i386/x86.h" +#include "hw/i386/tdvf-hob.h" #include "kvm_i386.h" #include "tdx.h" =20 @@ -131,6 +132,19 @@ void tdx_set_tdvf_region(MemoryRegion *tdvf_mr) tdx_guest->tdvf_mr =3D tdvf_mr; } =20 +static TdxFirmwareEntry *tdx_get_hob_entry(TdxGuest *tdx) +{ + TdxFirmwareEntry *entry; + + for_each_tdx_fw_entry(&tdx->tdvf, entry) { + if (entry->type =3D=3D TDVF_SECTION_TYPE_TD_HOB) { + return entry; + } + } + error_report("TDVF metadata doesn't specify TD_HOB location."); + exit(1); +} + static void tdx_add_ram_entry(uint64_t address, uint64_t length, enum TdxRamType type) { @@ -262,6 +276,8 @@ static void tdx_finalize_vm(Notifier *notifier, void *u= nused) =20 qsort(tdx_guest->ram_entries, tdx_guest->nr_ram_entries, sizeof(TdxRamEntry), &tdx_ram_entry_compare); + + tdvf_hob_create(tdx_guest, tdx_get_hob_entry(tdx_guest)); } =20 static Notifier tdx_machine_done_notify =3D { --=20 2.34.1