From patchwork Tue Nov 5 06:23:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862390 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1324E1F16B for ; Tue, 5 Nov 2024 06:36:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788588; cv=none; b=S9VPfEaDSQW18C2WXy5/o+u+geA+5Q0qDpoyvnhdft107kpacQQRW3A9OBvpD7e+g8wPg/MhDvvTJMmJ/Iol0L4YnilhkbtLB1Wg9Ym9/gUCV40D0qwNR4VbYHddhhD0saXIC+WiUUkyC1iJx1BZVbnQ1kDMn9vWUu+3h3h3k+8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788588; c=relaxed/simple; bh=g+h4bo1XqzDmMZ0LZa6Xv9bp3PCsN0+TI6VIrKIylhs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GKL51iBwCOCG2B/+x+6rCfxX6M8FkdO2gYv05sAzuAhoLA3DS1qtb/nxDAVmaSQ9Zy7mKIDbOHw0gtdNDOUU8NPEJjcDJlGYobsJZVzoVfS3nT3dPNlze7oBYnaSfN8ht3pDjUX2C/ojPjgj8BUEtofF/EO//B3FQB5ltSlWqa0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=UxdSy+uS; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="UxdSy+uS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788586; x=1762324586; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=g+h4bo1XqzDmMZ0LZa6Xv9bp3PCsN0+TI6VIrKIylhs=; b=UxdSy+uSi8GX93IN7nTpAmT/9uOAwA+oTO5TYaQVQgFDknSWuQP5h8TG VHFhBQkJEaUHPM8RBykQmWb/cjL2cZNNSyUFtepXDaxl+FXTUo0WMA7RW 2PGvASE7gskUF6zaIjbUPAeOS3GV++0F42JXwa6OJWb5j2WEHsQh8dvjK qMcf4nUz6Fhd9hgwnxh02cyukIU4TxnqEd/tClPqAVOQdMqhqsi+XoWAH MG5xv8cZW6ALRJD/m9UbShVw0x/LRuzGZkyg194auRcDuLVcq2jHGlYnI IYPYp8lnOxdZTbfFnJOIhSiomR33tWUwYIOyu41EKDHmw0JKef/ie3ij0 Q==; X-CSE-ConnectionGUID: jSexo5CuRuSXbuWw/+EtPQ== X-CSE-MsgGUID: JC3u0LjrQKSyiYjsp+PbAA== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689212" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689212" 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:36:26 -0800 X-CSE-ConnectionGUID: dzGKxjZRRju1K3R3NN9uKw== X-CSE-MsgGUID: B+woiuUfTM68IGRpM3+trQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83988551" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:36:22 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 01/60] *** HACK *** linux-headers: Update headers to pull in TDX API changes Date: Tue, 5 Nov 2024 01:23:09 -0500 Message-Id: <20241105062408.3533704-2-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Pull in recent TDX updates, which are not backwards compatible. It's just to make this series runnable. It will be updated by script scripts/update-linux-headers.sh once TDX support is upstreamed in linux kernel Signed-off-by: Xiaoyao Li --- linux-headers/asm-x86/kvm.h | 70 +++++++++++++++++++++++++++++++++++++ linux-headers/linux/kvm.h | 1 + 2 files changed, 71 insertions(+) diff --git a/linux-headers/asm-x86/kvm.h b/linux-headers/asm-x86/kvm.h index 4711ef2c3d01..719b9d121dbe 100644 --- a/linux-headers/asm-x86/kvm.h +++ b/linux-headers/asm-x86/kvm.h @@ -922,5 +922,75 @@ struct kvm_hyperv_eventfd { #define KVM_X86_SEV_VM 2 #define KVM_X86_SEV_ES_VM 3 #define KVM_X86_SNP_VM 4 +#define KVM_X86_TDX_VM 5 + +/* Trust Domain eXtension sub-ioctl() commands. */ +enum kvm_tdx_cmd_id { + KVM_TDX_CAPABILITIES = 0, + KVM_TDX_INIT_VM, + KVM_TDX_INIT_VCPU, + KVM_TDX_INIT_MEM_REGION, + KVM_TDX_FINALIZE_VM, + KVM_TDX_GET_CPUID, + + KVM_TDX_CMD_NR_MAX, +}; + +struct kvm_tdx_cmd { + /* enum kvm_tdx_cmd_id */ + __u32 id; + /* flags for sub-commend. If sub-command doesn't use this, set zero. */ + __u32 flags; + /* + * data for each sub-command. An immediate or a pointer to the actual + * data in process virtual address. If sub-command doesn't use it, + * set zero. + */ + __u64 data; + /* + * Auxiliary error code. The sub-command may return TDX SEAMCALL + * status code in addition to -Exxx. + * Defined for consistency with struct kvm_sev_cmd. + */ + __u64 hw_error; +}; + +struct kvm_tdx_capabilities { + __u64 supported_attrs; + __u64 supported_xfam; + __u64 reserved[254]; + struct kvm_cpuid2 cpuid; +}; + +struct kvm_tdx_init_vm { + __u64 attributes; + __u64 xfam; + __u64 mrconfigid[6]; /* sha384 digest */ + __u64 mrowner[6]; /* sha384 digest */ + __u64 mrownerconfig[6]; /* sha384 digest */ + + /* The total space for TD_PARAMS before the CPUIDs is 256 bytes */ + __u64 reserved[12]; + + /* + * Call KVM_TDX_INIT_VM before vcpu creation, thus before + * KVM_SET_CPUID2. + * This configuration supersedes KVM_SET_CPUID2s for VCPUs because the + * TDX module directly virtualizes those CPUIDs without VMM. The user + * space VMM, e.g. qemu, should make KVM_SET_CPUID2 consistent with + * those values. If it doesn't, KVM may have wrong idea of vCPUIDs of + * the guest, and KVM may wrongly emulate CPUIDs or MSRs that the TDX + * module doesn't virtualize. + */ + struct kvm_cpuid2 cpuid; +}; + +#define KVM_TDX_MEASURE_MEMORY_REGION _BITULL(0) + +struct kvm_tdx_init_mem_region { + __u64 source_addr; + __u64 gpa; + __u64 nr_pages; +}; #endif /* _ASM_X86_KVM_H */ diff --git a/linux-headers/linux/kvm.h b/linux-headers/linux/kvm.h index 49dd1b30ce9e..ebad8e0d10c5 100644 --- a/linux-headers/linux/kvm.h +++ b/linux-headers/linux/kvm.h @@ -369,6 +369,7 @@ struct kvm_run { #define KVM_SYSTEM_EVENT_WAKEUP 4 #define KVM_SYSTEM_EVENT_SUSPEND 5 #define KVM_SYSTEM_EVENT_SEV_TERM 6 +#define KVM_SYSTEM_EVENT_TDX_FATAL 7 __u32 type; __u32 ndata; union { From patchwork Tue Nov 5 06:23:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862391 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 642FC1F16B for ; Tue, 5 Nov 2024 06:36:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788592; cv=none; b=Qz0inysRl6TgtSJ1LV1UBKgA1W5be57ODc9NPXQdfttIKcjzLrilS36oWWMhcruwFVkMQ3IvC7B+kdmSY8frYlabrZvmKR8Sc7kB4gxXkgXewLnsZSUxESU47iLk81ASmk1oyByNGRa8RIrufMwdhEmICr+SmaY10RbMelfHycA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788592; c=relaxed/simple; bh=DWbz/FsnKhNwDGpwgnzLBuoDZpAUd28LILimRe0bFmc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=X/rOqbqF71DXhaN53GJIFNJz0uJdWkCsmKiqjRY/abfFyI1+qLALSDFAhofuHklQAf5HOycEFqkppTQ38uYo2EagIvsxZc0phCetF7zefK5oqmkjr1gNUlbbOsapAgB1cYM2sMe/EfE5XhZRniSsscunDVUGWyTnE23l4czTMW8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=nqaBC32f; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="nqaBC32f" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788590; x=1762324590; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DWbz/FsnKhNwDGpwgnzLBuoDZpAUd28LILimRe0bFmc=; b=nqaBC32fezrDxquFKSxU3v1ApeO1cFHQik7IwsnhqdNAj6VFVWly2SRz 57ygjwhrGZvbh6kLX0/wro5cK9WvZMOjltWiKciGm3KDi64BJSOOsaFFM 8978gAuoZEDmeLytFq28iczNUhSAP2wdTboBwdXqoQR0xnEfWDsymG0Wj oMWuLgt5iuMy7CUbi2eb+TYigiQbS+hasyJ3xZmXTv4MhGpeJx6eZ5n77 JHwahISSCQks/Kv9qFbRMa2DzUx9cyH5ytBiPZHXqD0KZbN2m/kuwSYqD w5f1Frcqskm0TzfKZPKo0Td63yrZpeRS6fbx3NnEB7H9AjIUe0WN7wbIj w==; X-CSE-ConnectionGUID: GrBLPHPxQoun513gOeu75Q== X-CSE-MsgGUID: w5h0LW2jQh6wHRedB4gEPg== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689220" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689220" 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:36:30 -0800 X-CSE-ConnectionGUID: Tt4I96uARDOpGhbeK3+VRQ== X-CSE-MsgGUID: 17l9xoadQhiPBHnWfFfj5A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83988564" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:36:26 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 02/60] i386: Introduce tdx-guest object Date: Tue, 5 Nov 2024 01:23:10 -0500 Message-Id: <20241105062408.3533704-3-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Introduce tdx-guest object which inherits X86_CONFIDENTIAL_GUEST, and will be used to create TDX VMs (TDs) by qemu -machine ...,confidential-guest-support=tdx0 \ -object tdx-guest,id=tdx0 It has one QAPI member 'attributes' defined, which allows user to set TD's attributes directly. Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann Acked-by: Markus Armbruster --- Chanegs in v6: - Make tdx-guest inherits X86_CONFIDENTIAL_GUEST; - set cgs->require_guest_memfd; - allow attributes settable via QAPI; - update QAPI version to since 9.2; Changes in v4: - update the new qapi `since` filed from 8.2 to 9.0 Changes in v1 - make @attributes not user-settable --- configs/devices/i386-softmmu/default.mak | 1 + hw/i386/Kconfig | 5 +++ qapi/qom.json | 15 ++++++++ target/i386/kvm/meson.build | 2 ++ target/i386/kvm/tdx.c | 45 ++++++++++++++++++++++++ target/i386/kvm/tdx.h | 19 ++++++++++ 6 files changed, 87 insertions(+) create mode 100644 target/i386/kvm/tdx.c create mode 100644 target/i386/kvm/tdx.h diff --git a/configs/devices/i386-softmmu/default.mak b/configs/devices/i386-softmmu/default.mak index 4faf2f0315e2..bc0479a7e0a3 100644 --- a/configs/devices/i386-softmmu/default.mak +++ b/configs/devices/i386-softmmu/default.mak @@ -18,6 +18,7 @@ #CONFIG_QXL=n #CONFIG_SEV=n #CONFIG_SGA=n +#CONFIG_TDX=n #CONFIG_TEST_DEVICES=n #CONFIG_TPM_CRB=n #CONFIG_TPM_TIS_ISA=n diff --git a/hw/i386/Kconfig b/hw/i386/Kconfig index 32818480d263..86bc10377c4f 100644 --- a/hw/i386/Kconfig +++ b/hw/i386/Kconfig @@ -10,6 +10,10 @@ config SGX bool depends on KVM +config TDX + bool + depends on KVM + config PC bool imply APPLESMC @@ -26,6 +30,7 @@ config PC imply QXL imply SEV imply SGX + imply TDX imply TEST_DEVICES imply TPM_CRB imply TPM_TIS_ISA diff --git a/qapi/qom.json b/qapi/qom.json index 321ccd708ad1..129b25edf495 100644 --- a/qapi/qom.json +++ b/qapi/qom.json @@ -1008,6 +1008,19 @@ '*host-data': 'str', '*vcek-disabled': 'bool' } } +## +# @TdxGuestProperties: +# +# Properties for tdx-guest objects. +# +# @attributes: The 'attributes' of a TD guest that is passed to +# KVM_TDX_INIT_VM +# +# Since: 9.2 +## +{ 'struct': 'TdxGuestProperties', + 'data': { '*attributes': 'uint64' } } + ## # @ThreadContextProperties: # @@ -1092,6 +1105,7 @@ 'sev-snp-guest', 'thread-context', 's390-pv-guest', + 'tdx-guest', 'throttle-group', 'tls-creds-anon', 'tls-creds-psk', @@ -1163,6 +1177,7 @@ 'if': 'CONFIG_SECRET_KEYRING' }, 'sev-guest': 'SevGuestProperties', 'sev-snp-guest': 'SevSnpGuestProperties', + 'tdx-guest': 'TdxGuestProperties', 'thread-context': 'ThreadContextProperties', 'throttle-group': 'ThrottleGroupProperties', 'tls-creds-anon': 'TlsCredsAnonProperties', diff --git a/target/i386/kvm/meson.build b/target/i386/kvm/meson.build index 3996cafaf29f..466bccb9cb17 100644 --- a/target/i386/kvm/meson.build +++ b/target/i386/kvm/meson.build @@ -8,6 +8,8 @@ i386_kvm_ss.add(files( i386_kvm_ss.add(when: 'CONFIG_XEN_EMU', if_true: files('xen-emu.c')) +i386_kvm_ss.add(when: 'CONFIG_TDX', if_true: files('tdx.c')) + i386_system_ss.add(when: 'CONFIG_HYPERV', if_true: files('hyperv.c'), if_false: files('hyperv-stub.c')) i386_system_ss.add_all(when: 'CONFIG_KVM', if_true: i386_kvm_ss) diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c new file mode 100644 index 000000000000..166f53d2b9e3 --- /dev/null +++ b/target/i386/kvm/tdx.c @@ -0,0 +1,45 @@ +/* + * QEMU TDX support + * + * Copyright Intel + * + * Author: + * Xiaoyao Li + * + * 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 "qom/object_interfaces.h" + +#include "tdx.h" + +/* tdx guest */ +OBJECT_DEFINE_TYPE_WITH_INTERFACES(TdxGuest, + tdx_guest, + TDX_GUEST, + X86_CONFIDENTIAL_GUEST, + { TYPE_USER_CREATABLE }, + { NULL }) + +static void tdx_guest_init(Object *obj) +{ + ConfidentialGuestSupport *cgs = CONFIDENTIAL_GUEST_SUPPORT(obj); + TdxGuest *tdx = TDX_GUEST(obj); + + cgs->require_guest_memfd = true; + tdx->attributes = 0; + + object_property_add_uint64_ptr(obj, "attributes", &tdx->attributes, + OBJ_PROP_FLAG_READWRITE); +} + +static void tdx_guest_finalize(Object *obj) +{ +} + +static void tdx_guest_class_init(ObjectClass *oc, void *data) +{ +} diff --git a/target/i386/kvm/tdx.h b/target/i386/kvm/tdx.h new file mode 100644 index 000000000000..de687457cae6 --- /dev/null +++ b/target/i386/kvm/tdx.h @@ -0,0 +1,19 @@ +#ifndef QEMU_I386_TDX_H +#define QEMU_I386_TDX_H + +#include "confidential-guest.h" + +#define TYPE_TDX_GUEST "tdx-guest" +#define TDX_GUEST(obj) OBJECT_CHECK(TdxGuest, (obj), TYPE_TDX_GUEST) + +typedef struct TdxGuestClass { + X86ConfidentialGuestClass parent_class; +} TdxGuestClass; + +typedef struct TdxGuest { + X86ConfidentialGuest parent_obj; + + uint64_t attributes; /* TD attributes */ +} TdxGuest; + +#endif /* QEMU_I386_TDX_H */ From patchwork Tue Nov 5 06:23:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862392 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 89B791F16B for ; Tue, 5 Nov 2024 06:36:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788596; cv=none; b=XL4TMbUK0XlRh7swQQU5378+/5WNNvrm9T3xN3JaKEv0NE/e+iPpaaPQF84SLhFNn51xNDRq/2FcLxanX8vGgEyPl1+Bu1v2cTorkq27mr+7yJyqrU1LzLIpIFQLBc2TQHy5LjDwxVhNmvPy8lS1qA3RBuU4KGTVGP1csrsTw6U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788596; c=relaxed/simple; bh=OubJ0frbbHV7MSdZrZwfSjPq/m9f7s+BIewp1R/ME4Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ffkY7IyX+z+jpbr7psu13Vo5IG2Y2cDih0ghfEUIRWXUUwfMzYNVCIOpn9z3vucCTwC9cMJjUi2zcFpnncyJb+L1V53V5oI7E5SKyNl5aF9qDMdFdf29uOe4ezWZDN81vviT1G6POpDR3Up6IKSYZsQGCut/NUXSVBZaFylzMrw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=kUVH4UCi; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="kUVH4UCi" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788594; x=1762324594; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=OubJ0frbbHV7MSdZrZwfSjPq/m9f7s+BIewp1R/ME4Y=; b=kUVH4UCiOhidEEGjsh5PYQPanbuiqGmNBSvowIOzWn2H0WICphbeN0Pd 0H0xoMGPoo0neR5r1xWpe/EN2bOdNHXSQwwmuZUsjxFO+RXpATv/Aj/77 5ynG2FR1U8LmDiNUwdI8JkzJKCxJjPD+HaXIyMm8yO5H7pv8xfTxSJJ48 CGLR8l4rw/NZrzSvj6KiO8MEjPJYyJQwvS8PWtKkHT8JZxl0fmsyIFA2g MqgO2ZLQE433eqSRFo7Npe0V/VmTpcbl+9/xik5pz14CNecK5WqlxOyjJ z418EY+xi5Xl3tqZS/puPHWL+gaC2s6ztuhkhE6KdhuPP8fumvmr3aVx1 Q==; X-CSE-ConnectionGUID: Of3QTW5VTb2kmBICIRhK1A== X-CSE-MsgGUID: +AvS7N7HSkG9FA/ZqT0Izg== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689237" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689237" 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:36:34 -0800 X-CSE-ConnectionGUID: xfz50WFbT2mM6g0uRJBg8A== X-CSE-MsgGUID: 23LnzFrqQVehOjvo+qmMZw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83988589" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:36:30 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 03/60] i386/tdx: Implement tdx_kvm_type() for TDX Date: Tue, 5 Nov 2024 01:23:11 -0500 Message-Id: <20241105062408.3533704-4-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 TDX VM requires VM type to be KVM_X86_TDX_VM. Implement tdx_kvm_type() as X86ConfidentialGuestClass->kvm_type. Signed-off-by: Xiaoyao Li --- Changes in v6: - new added patch; --- target/i386/kvm/kvm.c | 1 + target/i386/kvm/tdx.c | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index 8e17942c3ba1..ed2e89946c44 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -194,6 +194,7 @@ static const char *vm_type_name[] = { [KVM_X86_SEV_VM] = "SEV", [KVM_X86_SEV_ES_VM] = "SEV-ES", [KVM_X86_SNP_VM] = "SEV-SNP", + [KVM_X86_TDX_VM] = "TDX", }; bool kvm_is_vm_type_supported(int type) diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 166f53d2b9e3..bf8947549a96 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -14,8 +14,17 @@ #include "qemu/osdep.h" #include "qom/object_interfaces.h" +#include "kvm_i386.h" #include "tdx.h" +static int tdx_kvm_type(X86ConfidentialGuest *cg) +{ + /* Do the object check */ + TDX_GUEST(cg); + + return KVM_X86_TDX_VM; +} + /* tdx guest */ OBJECT_DEFINE_TYPE_WITH_INTERFACES(TdxGuest, tdx_guest, @@ -42,4 +51,7 @@ static void tdx_guest_finalize(Object *obj) static void tdx_guest_class_init(ObjectClass *oc, void *data) { + X86ConfidentialGuestClass *x86_klass = X86_CONFIDENTIAL_GUEST_CLASS(oc); + + x86_klass->kvm_type = tdx_kvm_type; } From patchwork Tue Nov 5 06:23:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862393 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BACCF1F16B for ; Tue, 5 Nov 2024 06:36:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788600; cv=none; b=qyYXKlJiTrTmEq4wgRRn3Ts4SQeIZNzYfQWYpI+1Ibp6rjfQ/C5paqmm7TNf8CnhP2BYqQWYIJeoo3TgvSdZE46V3uUhM63mgJQmJ2fOTpZD+sJv1+VobamtOiIwGBoLNf1awWQJPwQHXPxdicHc7epaNkUiPYXwAbOoWgKuZGU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788600; c=relaxed/simple; bh=ou+Mdobmm1tQq2ArTm7wdCK4exn2TLAYJgjckbytn0A=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hmi4RcnnztUTNAFiGj1bep+uvKvsRKCmFPdJRCoGYUobilS18KK1JcPYRV6QDEKqwPLBoaoC5lbXI4D6d0/ErBU5VgZuwQ9D1PvqteUd5Szl0xb60/XCFJucOm1XRGSB9bWDsNFlSSojBesgPA366EkmRY88pLRf4ztfWGTmiuw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=A+LvCwdU; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="A+LvCwdU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788599; x=1762324599; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ou+Mdobmm1tQq2ArTm7wdCK4exn2TLAYJgjckbytn0A=; b=A+LvCwdUJl5Wt4Xq02ub6C8tEdUDfCBVG7eW6sp0H0emya/Vl85l7fRr DwQVpvih/VqEg9tOlsq3EUZewuE2K47XRzn4ri0UeqyY98OJRqi5MFQCt opQmZI0FqR5agWMXiYrDDsgUscv94CVYTt089SSAj77ab8xuuXVdlairj Z9YtgDC1PWtx6zRqEfdq/bYQ2Qq3Z2FOfATjqBMGOMpF6Y7Bn/C325Dg2 lISfNAQ6Bj5wF5uiLFa81faUy8OccApheM1Ma73NArIwNFTMzf0ttJ4wB jyDi14zhzvHTcqe4laKhzjBW96xd8g7FfBuLKJkGRvsFfJettH08jJ3Fq w==; X-CSE-ConnectionGUID: YoxPk2X7TV+isH0qGGWGtg== X-CSE-MsgGUID: 6zX7bMqERa6AmP0cEqxY3A== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689256" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689256" 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:36:39 -0800 X-CSE-ConnectionGUID: r1o3UWJtR+OrzWAUBbZ4Gw== X-CSE-MsgGUID: /2AI0GzRRKefGwjrd6onTg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83988607" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:36:34 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 04/60] i386/tdx: Implement tdx_kvm_init() to initialize TDX VM context Date: Tue, 5 Nov 2024 01:23:12 -0500 Message-Id: <20241105062408.3533704-5-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Implement TDX specific ConfidentialGuestSupportClass::kvm_init() callback, tdx_kvm_init(). Mark guest state is proctected for TDX VM. More TDX specific initialization will be added later. Signed-off-by: Xiaoyao Li --- Changes in v6: - remove Acked-by from Gerd since the patch changed due to use ConfidentialGuestSupportClass::kvm_init(); --- target/i386/kvm/kvm.c | 11 +---------- target/i386/kvm/tdx.c | 10 ++++++++++ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index ed2e89946c44..2bbac603da70 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -3204,16 +3204,7 @@ int kvm_arch_init(MachineState *ms, KVMState *s) Error *local_err = NULL; /* - * Initialize SEV context, if required - * - * If no memory encryption is requested (ms->cgs == NULL) this is - * a no-op. - * - * It's also a no-op if a non-SEV confidential guest support - * mechanism is selected. SEV is the only mechanism available to - * select on x86 at present, so this doesn't arise, but if new - * mechanisms are supported in future (e.g. TDX), they'll need - * their own initialization either here or elsewhere. + * Initialize confidential guest (SEV/TDX) context, if required */ if (ms->cgs) { ret = confidential_guest_kvm_init(ms->cgs, &local_err); diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index bf8947549a96..85f006c1d6b4 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -14,9 +14,17 @@ #include "qemu/osdep.h" #include "qom/object_interfaces.h" +#include "hw/i386/x86.h" #include "kvm_i386.h" #include "tdx.h" +static int tdx_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) +{ + kvm_mark_guest_state_protected(); + + return 0; +} + static int tdx_kvm_type(X86ConfidentialGuest *cg) { /* Do the object check */ @@ -51,7 +59,9 @@ static void tdx_guest_finalize(Object *obj) static void tdx_guest_class_init(ObjectClass *oc, void *data) { + ConfidentialGuestSupportClass *klass = CONFIDENTIAL_GUEST_SUPPORT_CLASS(oc); X86ConfidentialGuestClass *x86_klass = X86_CONFIDENTIAL_GUEST_CLASS(oc); + klass->kvm_init = tdx_kvm_init; x86_klass->kvm_type = tdx_kvm_type; } From patchwork Tue Nov 5 06:23:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862394 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 133E01F16B for ; Tue, 5 Nov 2024 06:36:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788604; cv=none; b=R7pbejagWLzk9bLHLD9WNyYGVyh26LXO61/1wEs5K/BbqQ+enWctfmQyiCjv8syuKCpCg3N7nVdtvbFEaIzM0XbVyByM6zfPUKKjKGFaN+lUsBed2Lc8jsuuNfmYm5m/3ujBoQssoomF6AyvdL4McYtt1d67BmwMEP+LqVHu7RY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788604; c=relaxed/simple; bh=0vj+lC4sTbpC830FiXMO0t8VM8X6DjsBEWbC13OZKiM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ca2I5Y8GvnzlMudtqM+UVHeJimBAlMS+TltECLAPoDmM+2/W5uGDOyoJynEHDfxgY4AG8a8dfQAgCcM2G6y1CjxEmgIg811oLi3TA/4HVQbgVAeJhxd7J3VhKL1TNuZ5IQbgUr3rpGM/MOt0KFvKX7WRpWOwnKd+I1o6stotCP8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=QfcUpqq7; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="QfcUpqq7" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788603; x=1762324603; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0vj+lC4sTbpC830FiXMO0t8VM8X6DjsBEWbC13OZKiM=; b=QfcUpqq7BYNtb3EkBCl08xVhDq+PBjH06xpupoY9/bds2xXvOSr47TNF pt16IN09iDs+kO/HL2jdGja+FpF4Gpoat3ZVoYlRNwVgItsi6wyDeHi+z jc1Ob+bVLQiIfm02RyXN0hlP+YTmLORGXtq62JkW+ivsHV1GZQwDrl9Mn JgU4SZwk1/uuKuLMxDCCoqYplXVSRLdLSfdINjKcOwZGX7u98bSpiomNi jx4nYlOSLb/UEyw3MO5AGwPJ0XocGyUOhgsa9f5mjZZZn9aqvwk/dFE0y Sw6BqNRhMTjaNzS+iyHx7/yzdun5hssKmBEvt40RbWb3fcoXYz9y8C5Wk g==; X-CSE-ConnectionGUID: 4Fdr5HQgR9mZKvSMPg2z8g== X-CSE-MsgGUID: HP0JiSxfR4yhtNkLJHP3Hw== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689274" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689274" 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:36:43 -0800 X-CSE-ConnectionGUID: 3W+DpMZgTHWMuO4ZzHvdzA== X-CSE-MsgGUID: 6O5LboUeRWak91gSXFK5fQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83988621" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:36:39 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 05/60] i386/tdx: Get tdx_capabilities via KVM_TDX_CAPABILITIES Date: Tue, 5 Nov 2024 01:23:13 -0500 Message-Id: <20241105062408.3533704-6-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 KVM provides TDX capabilities via sub command KVM_TDX_CAPABILITIES of IOCTL(KVM_MEMORY_ENCRYPT_OP). Get the capabilities when initializing TDX context. It will be used to validate user's setting later. Since there is no interface reporting how many cpuid configs contains in KVM_TDX_CAPABILITIES, QEMU chooses to try starting with a known number and abort when it exceeds KVM_MAX_CPUID_ENTRIES. Besides, introduce the interfaces to invoke TDX "ioctls" at VCPU scope in preparation. Signed-off-by: Xiaoyao Li --- Changes in v6: - Pass CPUState * to tdx_vcpu_ioctl(); - update commit message to remove platform scope thing; - dump hw_error when it's non-zero to help debug; Changes in v4: - use {} to initialize struct kvm_tdx_cmd, to avoid memset(); - remove tdx_platform_ioctl() because no user; Changes in v3: - rename __tdx_ioctl() to tdx_ioctl_internal() - Pass errp in get_tdx_capabilities(); changes in v2: - Make the error message more clear; changes in v1: - start from nr_cpuid_configs = 6 for the loop; - stop the loop when nr_cpuid_configs exceeds KVM_MAX_CPUID_ENTRIES; --- target/i386/kvm/kvm.c | 2 - target/i386/kvm/kvm_i386.h | 2 + target/i386/kvm/tdx.c | 93 +++++++++++++++++++++++++++++++++++++- 3 files changed, 94 insertions(+), 3 deletions(-) diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index 2bbac603da70..b843de7f2379 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -1782,8 +1782,6 @@ static int hyperv_init_vcpu(X86CPU *cpu) static Error *invtsc_mig_blocker; -#define KVM_MAX_CPUID_ENTRIES 100 - static void kvm_init_xsave(CPUX86State *env) { if (has_xsave2) { diff --git a/target/i386/kvm/kvm_i386.h b/target/i386/kvm/kvm_i386.h index 9de9c0d30388..7ac4c3a91171 100644 --- a/target/i386/kvm/kvm_i386.h +++ b/target/i386/kvm/kvm_i386.h @@ -13,6 +13,8 @@ #include "sysemu/kvm.h" +#define KVM_MAX_CPUID_ENTRIES 100 + #ifdef CONFIG_KVM #define kvm_pit_in_kernel() \ diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 85f006c1d6b4..907044910fec 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -12,17 +12,108 @@ */ #include "qemu/osdep.h" +#include "qemu/error-report.h" +#include "qapi/error.h" #include "qom/object_interfaces.h" #include "hw/i386/x86.h" #include "kvm_i386.h" #include "tdx.h" +static struct kvm_tdx_capabilities *tdx_caps; + +enum tdx_ioctl_level { + TDX_VM_IOCTL, + TDX_VCPU_IOCTL, +}; + +static int tdx_ioctl_internal(enum tdx_ioctl_level level, void *state, + int cmd_id, __u32 flags, void *data) +{ + struct kvm_tdx_cmd tdx_cmd = {}; + int r; + + tdx_cmd.id = cmd_id; + tdx_cmd.flags = flags; + tdx_cmd.data = (__u64)(unsigned long)data; + + switch (level) { + case TDX_VM_IOCTL: + r = kvm_vm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_OP, &tdx_cmd); + break; + case TDX_VCPU_IOCTL: + r = kvm_vcpu_ioctl(state, KVM_MEMORY_ENCRYPT_OP, &tdx_cmd); + break; + default: + error_report("Invalid tdx_ioctl_level %d", level); + exit(1); + } + + if (r && tdx_cmd.hw_error) { + error_report("TDX ioctl %d return with %d, hw_errors: 0x%llx", + cmd_id, r, tdx_cmd.hw_error); + } + return r; +} + +static inline int tdx_vm_ioctl(int cmd_id, __u32 flags, void *data) +{ + return tdx_ioctl_internal(TDX_VM_IOCTL, NULL, cmd_id, flags, data); +} + +static inline int tdx_vcpu_ioctl(CPUState *cpu, int cmd_id, __u32 flags, + void *data) +{ + return tdx_ioctl_internal(TDX_VCPU_IOCTL, cpu, cmd_id, flags, data); +} + +static int get_tdx_capabilities(Error **errp) +{ + struct kvm_tdx_capabilities *caps; + /* 1st generation of TDX reports 6 cpuid configs */ + int nr_cpuid_configs = 6; + size_t size; + int r; + + do { + size = sizeof(struct kvm_tdx_capabilities) + + nr_cpuid_configs * sizeof(struct kvm_cpuid_entry2); + caps = g_malloc0(size); + caps->cpuid.nent = nr_cpuid_configs; + + r = tdx_vm_ioctl(KVM_TDX_CAPABILITIES, 0, caps); + if (r == -E2BIG) { + g_free(caps); + nr_cpuid_configs *= 2; + if (nr_cpuid_configs > KVM_MAX_CPUID_ENTRIES) { + error_setg(errp, "%s: KVM TDX seems broken that number of CPUID" + " entries in kvm_tdx_capabilities exceeds limit %d", + __func__, KVM_MAX_CPUID_ENTRIES); + return r; + } + } else if (r < 0) { + g_free(caps); + error_setg_errno(errp, -r, "%s: KVM_TDX_CAPABILITIES failed", __func__); + return r; + } + } while (r == -E2BIG); + + tdx_caps = caps; + + return 0; +} + static int tdx_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) { + int r = 0; + kvm_mark_guest_state_protected(); - return 0; + if (!tdx_caps) { + r = get_tdx_capabilities(errp); + } + + return r; } static int tdx_kvm_type(X86ConfidentialGuest *cg) From patchwork Tue Nov 5 06:23:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862395 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 260891FCF45 for ; Tue, 5 Nov 2024 06:36:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788608; cv=none; b=otg5RjJZwq+CsJSEClyEXpx/I5ONE6t5li1ZbQ5wH7Lwk3dNWB26JLiYJxmbiVLRDLvFWu7AF/EEuaCV6AYsN4X/KJCq7/d4AMgCCaAQYffZjj7C5gZqWIDbOOgbT+kWmkKsiCRipfgfNi6acyd8btFeyAhlxL5KWgx8Q65oyI0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788608; c=relaxed/simple; bh=7SB8aGVRHq3cVcRCjG983qVZg5U5xF0U5N0nc3brUHY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=N9KLFSKlh932wVYVYdHipmhp1QlJbDmNMXLhj6n7lHJdE5N2+PyJZh7Pc9AxzCiysk0Mm5haq9hYmhH7y96IX04eG3JTGlmj3fhH9ZRONBwSIDAmx/CO/whdwnrJGredd/6Vnfukui99pUnBoT1bpSn/Qo/dlz3I+oThqUXxtBI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=nBLE+p1a; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="nBLE+p1a" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788607; x=1762324607; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7SB8aGVRHq3cVcRCjG983qVZg5U5xF0U5N0nc3brUHY=; b=nBLE+p1aURZts4J4eHcJdF7GuV+0vzHV5NVRe6zItpZqNvP1ZMtFDm9K 0JRuAMf2JEpojnAO7GmclJ5WFizFjG18CmItIjrIA+ayn+4fxPBBSippK 5nusTn9b8N6gHnE3+A0vkSVuyNFvV6a5i7KE4jYtFAfioAlMGsBSxfOJE UVPIOYvgvFDjC3Mz98uxtbsyw3eMfT+rke1iFFDaYgeS8u3WqgTuiMeN6 gfcwdi3qQ5i5+nmKxz3kjKTta37951YO86ZCb757ZdPMSG2zJru/jDD0z BIiInwPG5LxbUHsm5cJWzvLVwMDm2LrC6TrAPgp2rHyAGurkHsOo6uZAZ g==; X-CSE-ConnectionGUID: SVxvBjLnQua68VWTXlsofw== X-CSE-MsgGUID: 32Zmdd2CTdiz71sAQd0U8A== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689287" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689287" 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:36:47 -0800 X-CSE-ConnectionGUID: CGPXOm3fS/i5ivZLDU97xA== X-CSE-MsgGUID: d6NqJKe+RIiI+IpNuhCdLA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83988631" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:36:43 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 06/60] i386/tdx: Introduce is_tdx_vm() helper and cache tdx_guest object Date: Tue, 5 Nov 2024 01:23:14 -0500 Message-Id: <20241105062408.3533704-7-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 It will need special handling for TDX VMs all around the QEMU. Introduce is_tdx_vm() helper to query if it's a TDX VM. Cache tdx_guest object thus no need to cast from ms->cgs every time. Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann Reviewed-by: Isaku Yamahata --- changes in v3: - replace object_dynamic_cast with TDX_GUEST(); --- target/i386/kvm/tdx.c | 15 ++++++++++++++- target/i386/kvm/tdx.h | 10 ++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 907044910fec..ff3ef9bd8657 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -20,8 +20,16 @@ #include "kvm_i386.h" #include "tdx.h" +static TdxGuest *tdx_guest; + static struct kvm_tdx_capabilities *tdx_caps; +/* Valid after kvm_arch_init()->confidential_guest_kvm_init()->tdx_kvm_init() */ +bool is_tdx_vm(void) +{ + return !!tdx_guest; +} + enum tdx_ioctl_level { TDX_VM_IOCTL, TDX_VCPU_IOCTL, @@ -105,15 +113,20 @@ static int get_tdx_capabilities(Error **errp) static int tdx_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) { + TdxGuest *tdx = TDX_GUEST(cgs); int r = 0; kvm_mark_guest_state_protected(); if (!tdx_caps) { r = get_tdx_capabilities(errp); + if (r) { + return r; + } } - return r; + tdx_guest = tdx; + return 0; } static int tdx_kvm_type(X86ConfidentialGuest *cg) diff --git a/target/i386/kvm/tdx.h b/target/i386/kvm/tdx.h index de687457cae6..bca19c833e18 100644 --- a/target/i386/kvm/tdx.h +++ b/target/i386/kvm/tdx.h @@ -1,6 +1,10 @@ #ifndef QEMU_I386_TDX_H #define QEMU_I386_TDX_H +#ifndef CONFIG_USER_ONLY +#include CONFIG_DEVICES /* CONFIG_TDX */ +#endif + #include "confidential-guest.h" #define TYPE_TDX_GUEST "tdx-guest" @@ -16,4 +20,10 @@ typedef struct TdxGuest { uint64_t attributes; /* TD attributes */ } TdxGuest; +#ifdef CONFIG_TDX +bool is_tdx_vm(void); +#else +#define is_tdx_vm() 0 +#endif /* CONFIG_TDX */ + #endif /* QEMU_I386_TDX_H */ From patchwork Tue Nov 5 06:23:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862396 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 75AE21F708B for ; Tue, 5 Nov 2024 06:36:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788613; cv=none; b=nprM0fyg/wpo300shuTSCsZeAqMI3o/cUMfPJqCPIdQt04G5a3l/hmsMOcL+jIEbLXFgYMjP1Xkq6zvsqwHbugaFYmiM05wY8YonQrvQ/5SZsrrZkUYHrc6sI7wuZsXcc1gIl0DJyxTL+OCn8SUbMwPfE3CgXefqu/AixnvH0Xo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788613; c=relaxed/simple; bh=A6C88ZryYnZ5BmBKN8njc0C6s0vxQGqEOftEOyDrLG0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=C/hTQ9mv9rUxInnnJNXBA9mc3Jm7+QUS+qAEKbM3HW63g4b+3B8D8BrkZi/SA7uhvgoKPUZCjK8B50/tiEgYJELqMwXM1UM9+diTw1sSb67rOsAnVV9Ve5MaWCdG4Uwf7KCZpzgye9XE//MW03DsCR6zZjblw0PEiv2p9BLlVJM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=lEcwwHWs; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="lEcwwHWs" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788611; x=1762324611; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=A6C88ZryYnZ5BmBKN8njc0C6s0vxQGqEOftEOyDrLG0=; b=lEcwwHWsd8uvqVi7Cj8HmQ0A7j7MSY3gGeaFRK/lcxN6P9+Vyh1Rd0g6 4Fy9QGtaTTh4mY7cjKm9JavBAp500TbeO0Nzueh4kboJ3itZBNVz0sYFv +7zJP1O2RJS/e3Q0e2jDKVqZwuewBWFgmna3OP6PY5BHLzfMXOw4OB7j/ jqc6iLuswBraRhsvBYYtvsp+jBIMDDqjiZVhzeqz6fu4sB0Dx35aDED4r ed82TErjI99eUvDsiu+BgCnV7t4o4wKZWT7CPdFaBeM3MVu33HOodBrYP 4NcVkrlmOi/kOYKYikyNHVaFsrc5oMYXNNhQqLbiK9o6YmO0e/RD95igR A==; X-CSE-ConnectionGUID: 91ck1+aqTR+xenOrlDoAaQ== X-CSE-MsgGUID: 0z3RjDDoSPWPdWg4W9Esuw== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689317" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689317" 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:36:51 -0800 X-CSE-ConnectionGUID: W+heuwoAQZOjmJ+rdV79Pw== X-CSE-MsgGUID: sN9Xd87dRTyBfJlJhz4auA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83988636" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:36:47 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 07/60] kvm: Introduce kvm_arch_pre_create_vcpu() Date: Tue, 5 Nov 2024 01:23:15 -0500 Message-Id: <20241105062408.3533704-8-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Introduce kvm_arch_pre_create_vcpu(), to perform arch-dependent work prior to create any vcpu. This is for i386 TDX because it needs call TDX_INIT_VM before creating any vcpu. Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann --- Changes in v3: - pass @errp to kvm_arch_pre_create_vcpu(); (Per Daniel) --- accel/kvm/kvm-all.c | 10 ++++++++++ include/sysemu/kvm.h | 1 + 2 files changed, 11 insertions(+) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 930a5bfed58f..1732fa1adecd 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -523,6 +523,11 @@ void kvm_destroy_vcpu(CPUState *cpu) } } +int __attribute__ ((weak)) kvm_arch_pre_create_vcpu(CPUState *cpu, Error **errp) +{ + return 0; +} + int kvm_init_vcpu(CPUState *cpu, Error **errp) { KVMState *s = kvm_state; @@ -531,6 +536,11 @@ int kvm_init_vcpu(CPUState *cpu, Error **errp) trace_kvm_init_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu)); + ret = kvm_arch_pre_create_vcpu(cpu, errp); + if (ret < 0) { + goto err; + } + ret = kvm_create_vcpu(cpu); if (ret < 0) { error_setg_errno(errp, -ret, diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h index c3a60b28909a..643ca4950543 100644 --- a/include/sysemu/kvm.h +++ b/include/sysemu/kvm.h @@ -374,6 +374,7 @@ int kvm_arch_get_default_type(MachineState *ms); int kvm_arch_init(MachineState *ms, KVMState *s); +int kvm_arch_pre_create_vcpu(CPUState *cpu, Error **errp); int kvm_arch_init_vcpu(CPUState *cpu); int kvm_arch_destroy_vcpu(CPUState *cpu); From patchwork Tue Nov 5 06:23:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862397 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 88A2D1F708B for ; Tue, 5 Nov 2024 06:36:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788617; cv=none; b=tEN17LjbycMeZ1nlPDdmX3BFcvCa5B/G49v6sFzSDL3cdhTQjn9LAb9UQ7lLVbPEuXWY+BnvKpqnPDvP7NUmNw4wUFepb/wU8Qbu2w+0sG39iATINidmMTgq0pd9SO/FzNlm2C12WrAX/p3ewxYLI+L4TNMPHDjMjZEJwOVjaBM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788617; c=relaxed/simple; bh=EMCLkioz6PKqWrBweYilnMeVfCok800taR5KSHSziyM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hp+svL6OGb75//59kHExxtUSZecsDU41sPCHjTsG6wd9fj9UkgQoQLxOgQHzPaVHWAk1QotnS/dPiKuI+ZQehUDmOgvGesnpiTdg3TRTh4Gb7HR31iqYAq0eJGKg5BJebDGEkudBc1LshrCa8M+v4lg/GuhtuE4kRFM0pQ65zZw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=X7IG5YSI; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="X7IG5YSI" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788615; x=1762324615; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=EMCLkioz6PKqWrBweYilnMeVfCok800taR5KSHSziyM=; b=X7IG5YSIzNhS2AXrQB7B/5JKKPhI55b1wUe+lZK9JVTucUcRKXOmCYhG ICjt/qWU+Zti9wuu2qtszfDD/M4MfWXKR4CT2U7YW6zcJPKadNVKD1AHr /9HwBkj3FCYExiiaTUF+MK95iwtV/wUrQYj8LZIeiVJxyPU8o3OjmzORs rcDjoJKQ8XRg4IMhVXMlnWkDgfEM343v0MBYBWxwv93Guox5bG0m7iTGR 4ukEffxgLv8IA1yZH1Pl0ECGBazOOXM5zB2BkPWDEg4aAx8QT64LyUD9w I6pfNqTNL/+u8IYDJb2gK12uC6jPaaAzWK39hrORUnvplb2nItO57q66z g==; X-CSE-ConnectionGUID: M1uUwq0WRIKh+uJQ7CUjzg== X-CSE-MsgGUID: q75HXP8hSm2tuNuwf5Pjvg== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689336" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689336" 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:36:55 -0800 X-CSE-ConnectionGUID: /mqITyi7TM+2Y+yCpFQ9lQ== X-CSE-MsgGUID: mzsjznpqSTuIPYeKU0/kIQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83988646" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:36:51 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 08/60] i386/kvm: Export cpuid_entry_get_reg() and cpuid_find_entry() Date: Tue, 5 Nov 2024 01:23:16 -0500 Message-Id: <20241105062408.3533704-9-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Signed-off-by: Xiaoyao Li --- target/i386/kvm/kvm.c | 8 ++++---- target/i386/kvm/kvm_i386.h | 4 ++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index b843de7f2379..afbf67a7fdaa 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -395,7 +395,7 @@ static bool host_tsx_broken(void) /* Returns the value for a specific register on the cpuid entry */ -static uint32_t cpuid_entry_get_reg(struct kvm_cpuid_entry2 *entry, int reg) +uint32_t cpuid_entry_get_reg(struct kvm_cpuid_entry2 *entry, int reg) { uint32_t ret = 0; switch (reg) { @@ -417,9 +417,9 @@ static uint32_t cpuid_entry_get_reg(struct kvm_cpuid_entry2 *entry, int reg) /* Find matching entry for function/index on kvm_cpuid2 struct */ -static struct kvm_cpuid_entry2 *cpuid_find_entry(struct kvm_cpuid2 *cpuid, - uint32_t function, - uint32_t index) +struct kvm_cpuid_entry2 *cpuid_find_entry(struct kvm_cpuid2 *cpuid, + uint32_t function, + uint32_t index) { int i; for (i = 0; i < cpuid->nent; ++i) { diff --git a/target/i386/kvm/kvm_i386.h b/target/i386/kvm/kvm_i386.h index 7ac4c3a91171..efb0883bd968 100644 --- a/target/i386/kvm/kvm_i386.h +++ b/target/i386/kvm/kvm_i386.h @@ -67,6 +67,10 @@ bool kvm_has_waitpkg(void); uint64_t kvm_swizzle_msi_ext_dest_id(uint64_t address); void kvm_update_msi_routes_all(void *private, bool global, uint32_t index, uint32_t mask); +uint32_t cpuid_entry_get_reg(struct kvm_cpuid_entry2 *entry, int reg); +struct kvm_cpuid_entry2 *cpuid_find_entry(struct kvm_cpuid2 *cpuid, + uint32_t function, + uint32_t index); #endif /* CONFIG_KVM */ From patchwork Tue Nov 5 06:23:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862398 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 97D6A189B80 for ; Tue, 5 Nov 2024 06:37:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788626; cv=none; b=QUgZJv6e9lAmyteQG72NkxA9VKMhUUs8yTBpJNOnJFTseEyXvalIEAiT6IUH+NRLygr+S47cXznNXbLLapU5c4peterCdJ+1E7R5YrRVFHub2rKZ2qw6FSHmAHIG7gD3tnGa5aYpRBNCcSWzNzvgNZQ6UlePG34ZpFvyxM87FI0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788626; c=relaxed/simple; bh=yQIhC8cfgzUM44TPusIfHMFUJd+lfWQalAFJnpz6Jag=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=u+LcesR7nr0ABRbeG2++na0wI8RtUiYfHaxUQJYrnryObL6SNfa9Pd8clXuXeqtxTuD/d1a7Z6h0sTye5aBk97u09cwq205wW3pzc0XGto6yvAZcHhDXLxCqeogYyjBt2PngTWhj+r4uBEuKx7IfEi3tr7zX8b9n5FD1ByjDm54= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=HmwW75py; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="HmwW75py" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788625; x=1762324625; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yQIhC8cfgzUM44TPusIfHMFUJd+lfWQalAFJnpz6Jag=; b=HmwW75pyCLXmLX6+ttiXHu8LErny2NO78YlLq9TI+p7tbgMp45Ms9ZG6 aFRGjDoc+XvDqTy8dlP6ww4SfJ4YZGf+fa5sX/9hdPxCUpI1PpSGpRmSG LBH7EJa4b4cf20I73CvvCyJz0jhY0WAjDr4XjkN6xwl/jnA9oopz4NSdL fsU5m3aJ7e1D/I+xGN1GmVN/QHGmdoZEVuKXMBJ5v+EIUVxVfiJN/trvU 6+J6u/471DwK+tUXTcSMYkBfGVCJrzzQN8E7hD2Hcru06BDh1Xd9rsCT0 vYUZeVNfgSoyELCxnc9vp+K7vpDlhZ3OIFzBd5g3HUMLxvQs4yyH6tTsH g==; X-CSE-ConnectionGUID: 2OO+t/riRVGQkAQo5lOS0Q== X-CSE-MsgGUID: BYZiFWHCShSncXU3o88lXA== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689369" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689369" 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:37:04 -0800 X-CSE-ConnectionGUID: vvoBuMURRum5DlaKfdWiIg== X-CSE-MsgGUID: nWmSZL4zTd2VmMz+z6IAgA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83988677" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:36:55 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 09/60] i386/tdx: Initialize TDX before creating TD vcpus Date: Tue, 5 Nov 2024 01:23:17 -0500 Message-Id: <20241105062408.3533704-10-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Invoke KVM_TDX_INIT in kvm_arch_pre_create_vcpu() that KVM_TDX_INIT configures global TD configurations, e.g. the canonical CPUID config, and must be executed prior to creating vCPUs. Use kvm_x86_arch_cpuid() to setup the CPUID settings for TDX VM. Note, this doesn't address the fact that QEMU may change the CPUID configuration when creating vCPUs, i.e. punts on refactoring QEMU to provide a stable CPUID config prior to kvm_arch_init(). Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann Acked-by: Markus Armbruster --- Changes in v6: - setup xfam explicitly to fit with new uapi; - use tdx_caps->cpuid to filter the input of cpuids because now KVM only allows the leafs that reported via KVM_TDX_GET_CAPABILITIES; Changes in v4: - mark init_vm with g_autofree() and use QEMU_LOCK_GUARD() to eliminate the goto labels; (Daniel) Changes in v3: - Pass @errp in tdx_pre_create_vcpu() and pass error info to it. (Daniel) --- accel/kvm/kvm-all.c | 8 ++++ target/i386/kvm/kvm.c | 15 +++++-- target/i386/kvm/kvm_i386.h | 3 ++ target/i386/kvm/meson.build | 2 +- target/i386/kvm/tdx-stub.c | 8 ++++ target/i386/kvm/tdx.c | 87 +++++++++++++++++++++++++++++++++++++ target/i386/kvm/tdx.h | 6 +++ 7 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 target/i386/kvm/tdx-stub.c diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 1732fa1adecd..4a1c9950894c 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -536,8 +536,15 @@ int kvm_init_vcpu(CPUState *cpu, Error **errp) trace_kvm_init_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu)); + /* + * tdx_pre_create_vcpu() may call cpu_x86_cpuid(). It in turn may call + * kvm_vm_ioctl(). Set cpu->kvm_state in advance to avoid NULL pointer + * dereference. + */ + cpu->kvm_state = s; ret = kvm_arch_pre_create_vcpu(cpu, errp); if (ret < 0) { + cpu->kvm_state = NULL; goto err; } @@ -546,6 +553,7 @@ int kvm_init_vcpu(CPUState *cpu, Error **errp) error_setg_errno(errp, -ret, "kvm_init_vcpu: kvm_create_vcpu failed (%lu)", kvm_arch_vcpu_id(cpu)); + cpu->kvm_state = NULL; goto err; } diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index afbf67a7fdaa..db676c1336ab 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -38,6 +38,7 @@ #include "kvm_i386.h" #include "../confidential-guest.h" #include "sev.h" +#include "tdx.h" #include "xen-emu.h" #include "hyperv.h" #include "hyperv-proto.h" @@ -1824,9 +1825,8 @@ static void kvm_init_nested_state(CPUX86State *env) } } -static uint32_t kvm_x86_build_cpuid(CPUX86State *env, - struct kvm_cpuid_entry2 *entries, - uint32_t cpuid_i) +uint32_t kvm_x86_build_cpuid(CPUX86State *env, struct kvm_cpuid_entry2 *entries, + uint32_t cpuid_i) { uint32_t limit, i, j; uint32_t unused; @@ -2358,6 +2358,15 @@ int kvm_arch_init_vcpu(CPUState *cs) return r; } +int kvm_arch_pre_create_vcpu(CPUState *cpu, Error **errp) +{ + if (is_tdx_vm()) { + return tdx_pre_create_vcpu(cpu, errp); + } + + return 0; +} + int kvm_arch_destroy_vcpu(CPUState *cs) { X86CPU *cpu = X86_CPU(cs); diff --git a/target/i386/kvm/kvm_i386.h b/target/i386/kvm/kvm_i386.h index efb0883bd968..b1baf9e7f910 100644 --- a/target/i386/kvm/kvm_i386.h +++ b/target/i386/kvm/kvm_i386.h @@ -24,6 +24,9 @@ #define kvm_ioapic_in_kernel() \ (kvm_irqchip_in_kernel() && !kvm_irqchip_is_split()) +uint32_t kvm_x86_build_cpuid(CPUX86State *env, struct kvm_cpuid_entry2 *entries, + uint32_t cpuid_i); + #else #define kvm_pit_in_kernel() 0 diff --git a/target/i386/kvm/meson.build b/target/i386/kvm/meson.build index 466bccb9cb17..3f44cdedb758 100644 --- a/target/i386/kvm/meson.build +++ b/target/i386/kvm/meson.build @@ -8,7 +8,7 @@ i386_kvm_ss.add(files( i386_kvm_ss.add(when: 'CONFIG_XEN_EMU', if_true: files('xen-emu.c')) -i386_kvm_ss.add(when: 'CONFIG_TDX', if_true: files('tdx.c')) +i386_kvm_ss.add(when: 'CONFIG_TDX', if_true: files('tdx.c'), if_false: files('tdx-stub.c')) i386_system_ss.add(when: 'CONFIG_HYPERV', if_true: files('hyperv.c'), if_false: files('hyperv-stub.c')) diff --git a/target/i386/kvm/tdx-stub.c b/target/i386/kvm/tdx-stub.c new file mode 100644 index 000000000000..b614b46d3f4a --- /dev/null +++ b/target/i386/kvm/tdx-stub.c @@ -0,0 +1,8 @@ +#include "qemu/osdep.h" + +#include "tdx.h" + +int tdx_pre_create_vcpu(CPUState *cpu, Error **errp) +{ + return -EINVAL; +} diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index ff3ef9bd8657..1b7894e43c6f 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -137,6 +137,91 @@ static int tdx_kvm_type(X86ConfidentialGuest *cg) return KVM_X86_TDX_VM; } +static int setup_td_xfam(X86CPU *x86cpu, Error **errp) +{ + CPUX86State *env = &x86cpu->env; + uint64_t xfam; + + xfam = env->features[FEAT_XSAVE_XCR0_LO] | + env->features[FEAT_XSAVE_XCR0_HI] | + env->features[FEAT_XSAVE_XSS_LO] | + env->features[FEAT_XSAVE_XSS_HI]; + + if (xfam & ~tdx_caps->supported_xfam) { + error_setg(errp, "Invalid XFAM 0x%lx for TDX VM (supported: 0x%llx))", + xfam, tdx_caps->supported_xfam); + return -1; + } + + tdx_guest->xfam = xfam; + return 0; +} + +static void tdx_filter_cpuid(struct kvm_cpuid2 *cpuids) +{ + int i, dest_cnt = 0; + struct kvm_cpuid_entry2 *src, *dest, *conf; + + for (i = 0; i < cpuids->nent; i++) { + src = cpuids->entries + i; + conf = cpuid_find_entry(&tdx_caps->cpuid, src->function, src->index); + if (!conf) { + continue; + } + dest = cpuids->entries + dest_cnt; + + dest->function = src->function; + dest->index = src->index; + dest->flags = src->flags; + dest->eax = src->eax & conf->eax; + dest->ebx = src->ebx & conf->ebx; + dest->ecx = src->ecx & conf->ecx; + dest->edx = src->edx & conf->edx; + + dest_cnt++; + } + cpuids->nent = dest_cnt++; +} + +int tdx_pre_create_vcpu(CPUState *cpu, Error **errp) +{ + X86CPU *x86cpu = X86_CPU(cpu); + CPUX86State *env = &x86cpu->env; + g_autofree struct kvm_tdx_init_vm *init_vm = NULL; + int r = 0; + + QEMU_LOCK_GUARD(&tdx_guest->lock); + if (tdx_guest->initialized) { + return r; + } + + init_vm = g_malloc0(sizeof(struct kvm_tdx_init_vm) + + sizeof(struct kvm_cpuid_entry2) * KVM_MAX_CPUID_ENTRIES); + + r = setup_td_xfam(x86cpu, errp); + if (r) { + return r; + } + + init_vm->cpuid.nent = kvm_x86_build_cpuid(env, init_vm->cpuid.entries, 0); + tdx_filter_cpuid(&init_vm->cpuid); + + init_vm->attributes = tdx_guest->attributes; + init_vm->xfam = tdx_guest->xfam; + + do { + r = tdx_vm_ioctl(KVM_TDX_INIT_VM, 0, init_vm); + } while (r == -EAGAIN); + if (r < 0) { + error_setg_errno(errp, -r, "KVM_TDX_INIT_VM failed"); + return r; + } + + tdx_guest->initialized = true; + + return 0; +} + /* tdx guest */ OBJECT_DEFINE_TYPE_WITH_INTERFACES(TdxGuest, tdx_guest, @@ -150,6 +235,8 @@ static void tdx_guest_init(Object *obj) ConfidentialGuestSupport *cgs = CONFIDENTIAL_GUEST_SUPPORT(obj); TdxGuest *tdx = TDX_GUEST(obj); + qemu_mutex_init(&tdx->lock); + cgs->require_guest_memfd = true; tdx->attributes = 0; diff --git a/target/i386/kvm/tdx.h b/target/i386/kvm/tdx.h index bca19c833e18..e077fd7d1653 100644 --- a/target/i386/kvm/tdx.h +++ b/target/i386/kvm/tdx.h @@ -17,7 +17,11 @@ typedef struct TdxGuestClass { typedef struct TdxGuest { X86ConfidentialGuest parent_obj; + QemuMutex lock; + + bool initialized; uint64_t attributes; /* TD attributes */ + uint64_t xfam; } TdxGuest; #ifdef CONFIG_TDX @@ -26,4 +30,6 @@ bool is_tdx_vm(void); #define is_tdx_vm() 0 #endif /* CONFIG_TDX */ +int tdx_pre_create_vcpu(CPUState *cpu, Error **errp); + #endif /* QEMU_I386_TDX_H */ From patchwork Tue Nov 5 06:23:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862399 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 769A81DB54C for ; Tue, 5 Nov 2024 06:37:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788628; cv=none; b=WPZ9TKmcFpzpwJxw3onUHopAnR8mwJg1xHq78mOBru4b7+J939hu4LxRsObuLSLdNl4nIDbv7ctMp3lvq3c7hSAeroguAMmZTKw5DHWYHtGkIAfhMRqGdH5UFkUqQ9NFsTFw2Mi6kpDDSVSnqRyGpyqceuer5tyHMb5GPiHxSr0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788628; c=relaxed/simple; bh=o/sLK02aQtaM8p5hBGZNXbfp2X2hSnsOmbdz3IT3nfg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OWptFZCm05WaKCxY6Qzhd1CGJ5GUae1kcyvJM10ie5f3RixdOvMMtDLQJWdJ49SfsHcdvdxEcSfYoSUYEeA2tPgNjeWdnB/3+/ZTKcaDtqXDVskzaWIfk66b4TyFSS7Gir9+XsSJr/f0X9p9Wjw1W2hYrFjSwp52yIwLgefWhKU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=FG5qnWlN; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="FG5qnWlN" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788626; x=1762324626; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=o/sLK02aQtaM8p5hBGZNXbfp2X2hSnsOmbdz3IT3nfg=; b=FG5qnWlNYtxIHPn1Mf//Fb4mPaoYfM4MsEVPOg7STD/ZDznAQOyamSPu 88MksN03Z2n+koJOeJkTTfU8nKtimtb8R7yCW7KF7H40PEdrKAhEaJxbO lhBbox4W94lYR/tnIe5NE6HqmnvyPoZAm39SoBqa2jUHjYiPrKeh9gvN/ 8wWrWK7ORi6sjqS4dYfiSnqeKN4rf0sZxLc+REoTE+zwI9XdZk7dq+WOH P9cGwReZnmoFz/YNSnEZB8n19oDOXl5WYLqqphMtg11tJNPIiAWgY6Umy CBZmF6/Zd6/Fh+1FgJIRIrozpFZqExd5a4Ctb6avwq9a86/MUfu0PZWg2 Q==; X-CSE-ConnectionGUID: ntzbRrQVQBqmR2JIjQwrjA== X-CSE-MsgGUID: EcWNT/4dTE+ESc+BNOkCBA== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689379" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689379" 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:37:04 -0800 X-CSE-ConnectionGUID: YRSFmewLQMe+eiJQaD2GmQ== X-CSE-MsgGUID: zpxUO4cAQnGtI73ucRqEtw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83988701" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:37:00 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 10/60] i386/tdx: Add property sept-ve-disable for tdx-guest object Date: Tue, 5 Nov 2024 01:23:18 -0500 Message-Id: <20241105062408.3533704-11-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Bit 28 of TD attribute, named SEPT_VE_DISABLE. When set to 1, it disables EPT violation conversion to #VE on guest TD access of PENDING pages. Some guest OS (e.g., Linux TD guest) may require this bit as 1. Otherwise refuse to boot. Add sept-ve-disable property for tdx-guest object, for user to configure this bit. Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann Acked-by: Markus Armbruster --- Changes in v4: - collect Acked-by from Markus Changes in v3: - update the comment of property @sept-ve-disable to make it more descriptive and use new format. (Daniel and Markus) --- qapi/qom.json | 8 +++++++- target/i386/kvm/tdx.c | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/qapi/qom.json b/qapi/qom.json index 129b25edf495..b3dc0cfa2641 100644 --- a/qapi/qom.json +++ b/qapi/qom.json @@ -1016,10 +1016,16 @@ # @attributes: The 'attributes' of a TD guest that is passed to # KVM_TDX_INIT_VM # +# @sept-ve-disable: toggle bit 28 of TD attributes to control disabling +# of EPT violation conversion to #VE on guest TD access of PENDING +# pages. Some guest OS (e.g., Linux TD guest) may require this to +# be set, otherwise they refuse to boot. +# # Since: 9.2 ## { 'struct': 'TdxGuestProperties', - 'data': { '*attributes': 'uint64' } } + 'data': { '*attributes': 'uint64', + '*sept-ve-disable': 'bool' } } ## # @ThreadContextProperties: diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 1b7894e43c6f..faac05ef630f 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -20,6 +20,8 @@ #include "kvm_i386.h" #include "tdx.h" +#define TDX_TD_ATTRIBUTES_SEPT_VE_DISABLE BIT_ULL(28) + static TdxGuest *tdx_guest; static struct kvm_tdx_capabilities *tdx_caps; @@ -222,6 +224,24 @@ int tdx_pre_create_vcpu(CPUState *cpu, Error **errp) return 0; } +static bool tdx_guest_get_sept_ve_disable(Object *obj, Error **errp) +{ + TdxGuest *tdx = TDX_GUEST(obj); + + return !!(tdx->attributes & TDX_TD_ATTRIBUTES_SEPT_VE_DISABLE); +} + +static void tdx_guest_set_sept_ve_disable(Object *obj, bool value, Error **errp) +{ + TdxGuest *tdx = TDX_GUEST(obj); + + if (value) { + tdx->attributes |= TDX_TD_ATTRIBUTES_SEPT_VE_DISABLE; + } else { + tdx->attributes &= ~TDX_TD_ATTRIBUTES_SEPT_VE_DISABLE; + } +} + /* tdx guest */ OBJECT_DEFINE_TYPE_WITH_INTERFACES(TdxGuest, tdx_guest, @@ -242,6 +262,9 @@ static void tdx_guest_init(Object *obj) object_property_add_uint64_ptr(obj, "attributes", &tdx->attributes, OBJ_PROP_FLAG_READWRITE); + object_property_add_bool(obj, "sept-ve-disable", + tdx_guest_get_sept_ve_disable, + tdx_guest_set_sept_ve_disable); } static void tdx_guest_finalize(Object *obj) From patchwork Tue Nov 5 06:23:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862400 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 29E761FE0E5 for ; Tue, 5 Nov 2024 06:37:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788630; cv=none; b=Dup5cf+VEPOpi7CpXzoE2csAIBsyRSvwHco8S0M2k05O5voqgYAk4utXL2L0KnF15hk1KiBxCVRiRzr+hhQtMfzSyEr7ANTN2PN+6aIJpPiI7WOS+wC6AKLXpX87TdHbLysFfvG2zoqYnjTPRHRYFNjDqI0MEYjtTWNSzGuUKxY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788630; c=relaxed/simple; bh=3vrjucC0HxJukGg8dTLCBBUFcd1deUmBY7cHA/rzJ28=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=PyuRBN1rphI1SX/J4dZpRiSIzLnOhO51W2UxBiaweGs/ypsoDRPkCTpXMpu9RlyuLjfI8rQSSqRefAELGAAyy1kw6gnKWJAMh8ar5Fo5s3YITzLpdyn4/VjhxJ+SIU1f62SDmjwYEHXwrmlVq6JbMiF161rIJ/Yv256sNNHpOyc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=hbHR2tUk; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="hbHR2tUk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788629; x=1762324629; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=3vrjucC0HxJukGg8dTLCBBUFcd1deUmBY7cHA/rzJ28=; b=hbHR2tUkk2hGy13NWdcugBFuRrFbkhdqa+NJVAGuHm9fgJIX/C1qCzq7 dtxMIpVVMWWsY//qJ9MK5pqRf++WA9Mf9prF0hX12uKrs/i1J18T0JvWK 3S2fAWY2J7T1wWrB+GjPwSRg/W3nm9PCrqg7hc+hJk/Yb1btVeK86GG24 v9VBIdIiIHnboeaOovYkJpgjKOclkwYeo8gi3yIEthMRRlwIkhuM55y8v juhqUssb/C7x3ApS1kCln/y1IFm+tIyJw2LYtLX9yid39nbcXZjk2EmuN VB2H9Fv/nyffg6AtyIWWkt7Qct49dq4MVO205J4GxXmREe2HjR0nrMVTi A==; X-CSE-ConnectionGUID: ivybeuNQTKqa7nK6sTuQ8A== X-CSE-MsgGUID: gU9rHaaaRyqJJjX9qGz9EQ== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689399" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689399" 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:37:09 -0800 X-CSE-ConnectionGUID: Zm2pPz90SC68qOWzD6MiDQ== X-CSE-MsgGUID: UIm5o8N/TvOypSmczHpCoA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83988743" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:37:04 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 11/60] i386/tdx: Make sept_ve_disable set by default Date: Tue, 5 Nov 2024 01:23:19 -0500 Message-Id: <20241105062408.3533704-12-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Isaku Yamahata For TDX KVM use case, Linux guest is the most major one. It requires sept_ve_disable set. Make it default for the main use case. For other use case, it can be enabled/disabled via qemu command line. Signed-off-by: Isaku Yamahata --- target/i386/kvm/tdx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index faac05ef630f..e8fd5c7d49e7 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -258,7 +258,7 @@ static void tdx_guest_init(Object *obj) qemu_mutex_init(&tdx->lock); cgs->require_guest_memfd = true; - tdx->attributes = 0; + tdx->attributes = TDX_TD_ATTRIBUTES_SEPT_VE_DISABLE; object_property_add_uint64_ptr(obj, "attributes", &tdx->attributes, OBJ_PROP_FLAG_READWRITE); From patchwork Tue Nov 5 06:23:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862401 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 03DEB1F6667 for ; Tue, 5 Nov 2024 06:37:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788634; cv=none; b=s2kxX1K5qVfxP8+JP5wtR3jOe/OQoJDt7imGGubphfP5BPZVgsJNPlJhzh/xuEtrjMYfc7EZdO92HWoqnkWL3W4LVRB3UnnIiJanbMvLasWQgDUHYkn9ShL+vfyb3/Y24q0MIyg6RCbYJnsskvt/0YpXhAFoENDKN2d/MLAbW38= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788634; c=relaxed/simple; bh=oOcXPFYjwedzTDjXBfJ22esNNNhA5wHUGcb1V46w02g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ColYE6u4DsXYk6s3GHTNOmfyWhvhI1+u3Q/MiLw+eCRTkR28i8n1vYBmBZaFXv1X6jPavtpeAZI+bbgwfYw9tniAcBcKPTHuS+t96YGXoHuGGbC9ICFwUWl4Suah0Jgz+S/il/jAoYnaDE/9YFOFEjSXNmowevGwocehGQFn9hA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=NNLqft3w; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="NNLqft3w" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788633; x=1762324633; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=oOcXPFYjwedzTDjXBfJ22esNNNhA5wHUGcb1V46w02g=; b=NNLqft3wSOPvZ+0JeRXtHtiXVegc9C96x9fd9S44w4xRh60jiXKwUQcw ApAOFEAAiw39/W0SScskIAq2kW9sSpy2xKNFW5vXhLmXiaWn1Y/9NeawU 3XOmRzwA+EeyvjaDMpBnJ6lOj3NufVzXaUeQw2nLtI0S2zIh+gtIwNGQ5 11HMGprWwwB1gRrsT5ba4CQ5nVveyf1fkk4u+d60QVT0iYGhL/GMqDUpF hHRrcfSBbZEvTislm99Yv+teqa1kX+uDuaoP6SGA7Ilid66vxizWlKb86 RauTc3htClcYquSb3/kHRHPlMB2cr4zWaN8tQOPLzCm7hCxtLW/FSK+cQ g==; X-CSE-ConnectionGUID: crnOrcC0QYSIg2WaR5oo+A== X-CSE-MsgGUID: Tkor2lEpStuwEcQ446kAsg== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689407" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689407" 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:37:13 -0800 X-CSE-ConnectionGUID: S/SrZVnbRye6tYey39q1gQ== X-CSE-MsgGUID: gPwWQciUSUSPcF3ezm7Hdw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83988765" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:37:09 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 12/60] i386/tdx: Wire CPU features up with attributes of TD guest Date: Tue, 5 Nov 2024 01:23:20 -0500 Message-Id: <20241105062408.3533704-13-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 For QEMU VMs, - PKS is configured via CPUID_7_0_ECX_PKS, e.g., -cpu xxx,+pks and - PMU is configured by x86cpu->enable_pmu, e.g., -cpu xxx,pmu=on While the bit 30 (PKS) and bit 63 (PERFMON) of TD's attributes are also used to configure the PKS and PERFMON/PMU of TD, reuse the existing configuration interfaces of 'cpu' for TD's attributes. Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann --- target/i386/kvm/tdx.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index e8fd5c7d49e7..6cf81f788fe0 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -21,6 +21,8 @@ #include "tdx.h" #define TDX_TD_ATTRIBUTES_SEPT_VE_DISABLE BIT_ULL(28) +#define TDX_TD_ATTRIBUTES_PKS BIT_ULL(30) +#define TDX_TD_ATTRIBUTES_PERFMON BIT_ULL(63) static TdxGuest *tdx_guest; @@ -139,6 +141,15 @@ static int tdx_kvm_type(X86ConfidentialGuest *cg) return KVM_X86_TDX_VM; } +static void setup_td_guest_attributes(X86CPU *x86cpu) +{ + CPUX86State *env = &x86cpu->env; + + tdx_guest->attributes |= (env->features[FEAT_7_0_ECX] & CPUID_7_0_ECX_PKS) ? + TDX_TD_ATTRIBUTES_PKS : 0; + tdx_guest->attributes |= x86cpu->enable_pmu ? TDX_TD_ATTRIBUTES_PERFMON : 0; +} + static int setup_td_xfam(X86CPU *x86cpu, Error **errp) { CPUX86State *env = &x86cpu->env; @@ -200,6 +211,8 @@ int tdx_pre_create_vcpu(CPUState *cpu, Error **errp) init_vm = g_malloc0(sizeof(struct kvm_tdx_init_vm) + sizeof(struct kvm_cpuid_entry2) * KVM_MAX_CPUID_ENTRIES); + setup_td_guest_attributes(x86cpu); + r = setup_td_xfam(x86cpu, errp); if (r) { return r; From patchwork Tue Nov 5 06:23:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862402 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 282481F6667 for ; Tue, 5 Nov 2024 06:37:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788638; cv=none; b=GvlG3zgeALYXlRwnKSD58/TnGO15IkRiBcZWwp1A+nZMk6Fn5ilpEILtKw7rVHPzZ1v3SghI4SngmgikIui7BoOK1o865l+7flSmtV5DS+9MJksikioliws56HzcnknbbDUbhvMXuXhTU31d9GxDhD98EOAHlGJo5E8OuLDhlgA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788638; c=relaxed/simple; bh=/c6iKYuArYsGCvrbLnQoYffE/JqYul3IweXytxDmZNY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=me1zQ6LbvBmXyRqpupa7KTujjHfLLV9n/1BU6b0LcylgN9xWjSniZ8ssmvTBv7GblOTF/OYcnACEMIGsOmKE9Ze4L9GlZzrBY8wEisPukU9IFUZNjTwnl/x7JRCJEy7Az9GgAY9hQttjrwj+39iyU964a/R+65KOUN98VnL60es= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=I9MB9OGO; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="I9MB9OGO" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788637; x=1762324637; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/c6iKYuArYsGCvrbLnQoYffE/JqYul3IweXytxDmZNY=; b=I9MB9OGOJK+imERA6/Y6lPiu6y3nGwQ1JsCB9S1wLAZ8NID1Lzs7adul 41vm7RCY9sr7TJlk68fITb9IvcEvUJYFrGztxa7dgPRMNy3UOs+s0oH02 N5kecnY8xGkZ4fjwZM3F2s+H7tdntzw9I265Vix4aHTTtIN8vt4OW2BvX Bj/KsD+r4MiBxwwY/LxjfZmInvtbwekv9cg+VayizJvOEOlW7q2LxaiAA a7z6LMOmewm51z655KY82zQogCM6B3fZG6URClAueJEI+mpcZ0AbyMOHo acqN7ZU6N4mAZemowEVNSXwt/dpTTZtAxBCijmJKBLeuNZsLuDTP2rwQT Q==; X-CSE-ConnectionGUID: insP5wpNTYyX3XHeIj2rMQ== X-CSE-MsgGUID: EdpovNl0TpW1uKvd5yUrPA== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689425" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689425" 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:37:17 -0800 X-CSE-ConnectionGUID: L6eSPwevRTCTpTItmtpCDQ== X-CSE-MsgGUID: bP+aULKLSei0fDyQJ6ZDzA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83988794" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:37:13 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 13/60] i386/tdx: Validate TD attributes Date: Tue, 5 Nov 2024 01:23:21 -0500 Message-Id: <20241105062408.3533704-14-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Validate TD attributes with tdx_caps that fixed-0 bits must be zero and fixed-1 bits must be set. Besides, sanity check the attribute bits that have not been supported by QEMU yet. e.g., debug bit, it will be allowed in the future when debug TD support lands in QEMU. Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann --- Changes in v3: - using error_setg() for error report; (Daniel) --- target/i386/kvm/tdx.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 6cf81f788fe0..5a9ce2ada89d 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -20,6 +20,7 @@ #include "kvm_i386.h" #include "tdx.h" +#define TDX_TD_ATTRIBUTES_DEBUG BIT_ULL(0) #define TDX_TD_ATTRIBUTES_SEPT_VE_DISABLE BIT_ULL(28) #define TDX_TD_ATTRIBUTES_PKS BIT_ULL(30) #define TDX_TD_ATTRIBUTES_PERFMON BIT_ULL(63) @@ -141,13 +142,33 @@ static int tdx_kvm_type(X86ConfidentialGuest *cg) return KVM_X86_TDX_VM; } -static void setup_td_guest_attributes(X86CPU *x86cpu) +static int tdx_validate_attributes(TdxGuest *tdx, Error **errp) +{ + if ((tdx->attributes & ~tdx_caps->supported_attrs)) { + error_setg(errp, "Invalid attributes 0x%lx for TDX VM " + "(supported: 0x%llx)", + tdx->attributes, tdx_caps->supported_attrs); + return -1; + } + + if (tdx->attributes & TDX_TD_ATTRIBUTES_DEBUG) { + error_setg(errp, "Current QEMU doesn't support attributes.debug[bit 0] " + "for TDX VM"); + return -1; + } + + return 0; +} + +static int setup_td_guest_attributes(X86CPU *x86cpu, Error **errp) { CPUX86State *env = &x86cpu->env; tdx_guest->attributes |= (env->features[FEAT_7_0_ECX] & CPUID_7_0_ECX_PKS) ? TDX_TD_ATTRIBUTES_PKS : 0; tdx_guest->attributes |= x86cpu->enable_pmu ? TDX_TD_ATTRIBUTES_PERFMON : 0; + + return tdx_validate_attributes(tdx_guest, errp); } static int setup_td_xfam(X86CPU *x86cpu, Error **errp) @@ -211,7 +232,10 @@ int tdx_pre_create_vcpu(CPUState *cpu, Error **errp) init_vm = g_malloc0(sizeof(struct kvm_tdx_init_vm) + sizeof(struct kvm_cpuid_entry2) * KVM_MAX_CPUID_ENTRIES); - setup_td_guest_attributes(x86cpu); + r = setup_td_guest_attributes(x86cpu, errp); + if (r) { + return r; + } r = setup_td_xfam(x86cpu, errp); if (r) { From patchwork Tue Nov 5 06:23:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862403 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 24FFB1FCC61 for ; Tue, 5 Nov 2024 06:37:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788643; cv=none; b=YUPxxPjENrNACp9jLsLnBglyPMtl//h9H8/jVMU5GofiPdpvJxJEwh6ch6Z6MHXzhLzMmAk69CEhAEoMp1BPe/52FNZOFHH3DwvxrxeN5nPqQyRLD5NNq8iyoufvG7eFePJI7JsE1ZEWSjPpudHXwcHyNJso1ZGiE2mSo+xp0eI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788643; c=relaxed/simple; bh=u2uKohq3DcafzpQYHK3WlKK6kJ5fDktVKOc7Gy0BK4Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=joDjxqnPyuvdHetpENxfi49kpJwAPbIhoX1pfg2k/HOsVNYX77CnqQkRDYbslr8+khSlqTF9hPBo5qS0TRSaTDEesy4F5IIfS8n98/gNiTfLHYTMnPIa8LgkYVpsxmS3ZdRoj5eFdphf4GIhGhFtPJNYN65H8hTQ5meLtdfXfmo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=HS/8u9K/; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="HS/8u9K/" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788642; x=1762324642; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=u2uKohq3DcafzpQYHK3WlKK6kJ5fDktVKOc7Gy0BK4Y=; b=HS/8u9K/BpjiLjcFuJChcMSqn4/O1TQzFO9GiZQFbHM7gXiCDVPRIBEn fzc8UEd4p/jfAfAAHjQYLHDROfmBI7bzSsnl9z97oOsXz+/j9AWLRpYq9 h2vx/OvGrr6OAV6H2h9qkMAEStFnU+tEK6ZU8zv4MBkzPXWu9oH+vnKHy Tkbtqh5giAAGM8gsN+rUvgDTrJU+1MSfAgRXLJYK5hSTl5UfKFuqiesJ7 lOlNv3Zm7l/urCQwMhrA/unjAiNy8prxsgTnyeGtdHM0THrDGXP/3Oqo/ UmaBthw7fG8l6dDjVvbMA8AmXV3C+NR+u9ti06WkZA/JFFZdCOYi8TDGz w==; X-CSE-ConnectionGUID: g+Lj99FkRi6PpCPFb6PsXQ== X-CSE-MsgGUID: Di6G+XZ4TfyAbuBbvJdoBQ== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689433" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689433" 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:37:22 -0800 X-CSE-ConnectionGUID: 24IZZRMiRmuB5po5TzwMsg== X-CSE-MsgGUID: YO1KTPRyQN+CWj/2JK6wtg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83988820" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:37:17 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 14/60] i386/tdx: Support user configurable mrconfigid/mrowner/mrownerconfig Date: Tue, 5 Nov 2024 01:23:22 -0500 Message-Id: <20241105062408.3533704-15-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Isaku Yamahata Three sha384 hash values, mrconfigid, mrowner and mrownerconfig, of a TD can be provided for TDX attestation. Detailed meaning of them can be found: https://lore.kernel.org/qemu-devel/31d6dbc1-f453-4cef-ab08-4813f4e0ff92@intel.com/ Allow user to specify those values via property mrconfigid, mrowner and mrownerconfig. They are all in base64 format. example -object tdx-guest, \ mrconfigid=ASNFZ4mrze8BI0VniavN7wEjRWeJq83vASNFZ4mrze8BI0VniavN7wEjRWeJq83v,\ mrowner=ASNFZ4mrze8BI0VniavN7wEjRWeJq83vASNFZ4mrze8BI0VniavN7wEjRWeJq83v,\ mrownerconfig=ASNFZ4mrze8BI0VniavN7wEjRWeJq83vASNFZ4mrze8BI0VniavN7wEjRWeJq83v Signed-off-by: Isaku Yamahata Co-developed-by: Xiaoyao Li Signed-off-by: Xiaoyao Li --- Changes in v6: - refine the doc comment of QAPI properties; Changes in v5: - refine the description of QAPI properties and add description of default value when not specified; Changes in v4: - describe more of there fields in qom.json - free the old value before set new value to avoid memory leak in _setter(); (Daniel) Changes in v3: - use base64 encoding instread of hex-string; --- qapi/qom.json | 16 +++++++- target/i386/kvm/tdx.c | 86 +++++++++++++++++++++++++++++++++++++++++++ target/i386/kvm/tdx.h | 3 ++ 3 files changed, 104 insertions(+), 1 deletion(-) diff --git a/qapi/qom.json b/qapi/qom.json index b3dc0cfa2641..477bbaa86a68 100644 --- a/qapi/qom.json +++ b/qapi/qom.json @@ -1021,11 +1021,25 @@ # pages. Some guest OS (e.g., Linux TD guest) may require this to # be set, otherwise they refuse to boot. # +# @mrconfigid: ID for non-owner-defined configuration of the guest TD, +# e.g., run-time or OS configuration (base64 encoded SHA384 digest). +# Defaults to all zeros. +# +# @mrowner: ID for the guest TD’s owner (base64 encoded SHA384 digest). +# Defaults to all zeros. +# +# @mrownerconfig: ID for owner-defined configuration of the guest TD, +# e.g., specific to the workload rather than the run-time or OS +# (base64 encoded SHA384 digest). Defaults to all zeros. +# # Since: 9.2 ## { 'struct': 'TdxGuestProperties', 'data': { '*attributes': 'uint64', - '*sept-ve-disable': 'bool' } } + '*sept-ve-disable': 'bool', + '*mrconfigid': 'str', + '*mrowner': 'str', + '*mrownerconfig': 'str' } } ## # @ThreadContextProperties: diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 5a9ce2ada89d..887a5324b439 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -13,6 +13,7 @@ #include "qemu/osdep.h" #include "qemu/error-report.h" +#include "qemu/base64.h" #include "qapi/error.h" #include "qom/object_interfaces.h" @@ -222,6 +223,7 @@ int tdx_pre_create_vcpu(CPUState *cpu, Error **errp) X86CPU *x86cpu = X86_CPU(cpu); CPUX86State *env = &x86cpu->env; g_autofree struct kvm_tdx_init_vm *init_vm = NULL; + size_t data_len; int r = 0; QEMU_LOCK_GUARD(&tdx_guest->lock); @@ -232,6 +234,37 @@ int tdx_pre_create_vcpu(CPUState *cpu, Error **errp) init_vm = g_malloc0(sizeof(struct kvm_tdx_init_vm) + sizeof(struct kvm_cpuid_entry2) * KVM_MAX_CPUID_ENTRIES); +#define SHA384_DIGEST_SIZE 48 + if (tdx_guest->mrconfigid) { + g_autofree uint8_t *data = qbase64_decode(tdx_guest->mrconfigid, + strlen(tdx_guest->mrconfigid), &data_len, errp); + if (!data || data_len != SHA384_DIGEST_SIZE) { + error_setg(errp, "TDX: failed to decode mrconfigid"); + return -1; + } + memcpy(init_vm->mrconfigid, data, data_len); + } + + if (tdx_guest->mrowner) { + g_autofree uint8_t *data = qbase64_decode(tdx_guest->mrowner, + strlen(tdx_guest->mrowner), &data_len, errp); + if (!data || data_len != SHA384_DIGEST_SIZE) { + error_setg(errp, "TDX: failed to decode mrowner"); + return -1; + } + memcpy(init_vm->mrowner, data, data_len); + } + + if (tdx_guest->mrownerconfig) { + g_autofree uint8_t *data = qbase64_decode(tdx_guest->mrownerconfig, + strlen(tdx_guest->mrownerconfig), &data_len, errp); + if (!data || data_len != SHA384_DIGEST_SIZE) { + error_setg(errp, "TDX: failed to decode mrownerconfig"); + return -1; + } + memcpy(init_vm->mrownerconfig, data, data_len); + } + r = setup_td_guest_attributes(x86cpu, errp); if (r) { return r; @@ -279,6 +312,51 @@ static void tdx_guest_set_sept_ve_disable(Object *obj, bool value, Error **errp) } } +static char *tdx_guest_get_mrconfigid(Object *obj, Error **errp) +{ + TdxGuest *tdx = TDX_GUEST(obj); + + return g_strdup(tdx->mrconfigid); +} + +static void tdx_guest_set_mrconfigid(Object *obj, const char *value, Error **errp) +{ + TdxGuest *tdx = TDX_GUEST(obj); + + g_free(tdx->mrconfigid); + tdx->mrconfigid = g_strdup(value); +} + +static char *tdx_guest_get_mrowner(Object *obj, Error **errp) +{ + TdxGuest *tdx = TDX_GUEST(obj); + + return g_strdup(tdx->mrowner); +} + +static void tdx_guest_set_mrowner(Object *obj, const char *value, Error **errp) +{ + TdxGuest *tdx = TDX_GUEST(obj); + + g_free(tdx->mrowner); + tdx->mrowner = g_strdup(value); +} + +static char *tdx_guest_get_mrownerconfig(Object *obj, Error **errp) +{ + TdxGuest *tdx = TDX_GUEST(obj); + + return g_strdup(tdx->mrownerconfig); +} + +static void tdx_guest_set_mrownerconfig(Object *obj, const char *value, Error **errp) +{ + TdxGuest *tdx = TDX_GUEST(obj); + + g_free(tdx->mrownerconfig); + tdx->mrownerconfig = g_strdup(value); +} + /* tdx guest */ OBJECT_DEFINE_TYPE_WITH_INTERFACES(TdxGuest, tdx_guest, @@ -302,6 +380,14 @@ static void tdx_guest_init(Object *obj) object_property_add_bool(obj, "sept-ve-disable", tdx_guest_get_sept_ve_disable, tdx_guest_set_sept_ve_disable); + object_property_add_str(obj, "mrconfigid", + tdx_guest_get_mrconfigid, + tdx_guest_set_mrconfigid); + object_property_add_str(obj, "mrowner", + tdx_guest_get_mrowner, tdx_guest_set_mrowner); + object_property_add_str(obj, "mrownerconfig", + tdx_guest_get_mrownerconfig, + tdx_guest_set_mrownerconfig); } static void tdx_guest_finalize(Object *obj) diff --git a/target/i386/kvm/tdx.h b/target/i386/kvm/tdx.h index e077fd7d1653..bc26e24eb9ac 100644 --- a/target/i386/kvm/tdx.h +++ b/target/i386/kvm/tdx.h @@ -22,6 +22,9 @@ typedef struct TdxGuest { bool initialized; uint64_t attributes; /* TD attributes */ uint64_t xfam; + char *mrconfigid; /* base64 encoded sha348 digest */ + char *mrowner; /* base64 encoded sha348 digest */ + char *mrownerconfig; /* base64 encoded sha348 digest */ } TdxGuest; #ifdef CONFIG_TDX From patchwork Tue Nov 5 06:23:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862404 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AD29A1D88DB for ; Tue, 5 Nov 2024 06:37:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788647; cv=none; b=iNro0avevfWMpEPrqfOt/k6vyzs8qc4e3z17mkySyCXhDoInXk8qsUTJfYYB/dFmAdKcaWZg82UfVGdBHd0C2P62R7OdST2ToDRVPE5FemVHgRfseJV+ET8D/8rGtmt8k07S99OSCsS2MwuqV2cC3ZJIMcv8WY7zDkzKE0lxXM8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788647; c=relaxed/simple; bh=VOjci9p7Zb1n81rBNztaLxcXzrYXbaFL+FZxHq6NkWo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=F0omoz6emsivf0B1k5fo5Ye2hErIDngy9xK/pHYW//o1+GbuEOeE9zc+4mUE6AuNRBwWo5Yy1LrxXdaNdT90y6IFGUJKfLKcX6TppXDdgaagZIDi/sKGGefqkgAs8dZwxADAhNacXwqnCBJP/5fZ+4J1HdySrOFZeeN73QuklR4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=lj4Vic8Y; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="lj4Vic8Y" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788646; x=1762324646; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=VOjci9p7Zb1n81rBNztaLxcXzrYXbaFL+FZxHq6NkWo=; b=lj4Vic8YfZwfCLstfDxfrsDDPvsPWL3W/kHiude85VQ048Uhgx/0c/GS R0ztmLuQUxTIZ30EMvJ2lK/tfZelvkEFmQKz8owHjmulQi8IeQegZuMnZ Psm19GkGSNMk+8MnEwxRmCZ/syCT6gbeDGhBvmt7gUnbmEv1LumKW503M UjqYEoHMSWuv5eNW8ncfV4U+2tOkP3LBYJA7GS97W7ASIpPduRbT5lYdM fkrgaZTNv3SI/R7aAqFmZbvAnKIeRXVMCMCxwkOGLynVSgOM8HnIuJ1t1 BskYF/VlPktp/RjqOFwC9xA9Bc7K3qx/Dz3Ebnj9LR4TVQfW6+/kECe96 w==; X-CSE-ConnectionGUID: LmpmXTaCQvCqcTbz1dOP6w== X-CSE-MsgGUID: HaeQOKz6QkGkgda1ET+3TA== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689444" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689444" 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:37:25 -0800 X-CSE-ConnectionGUID: N4Omogx0ShuO9H1a47PWkQ== X-CSE-MsgGUID: /XLDrWUYQfqYb+xEQj4Gkw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83988843" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:37:21 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 15/60] i386/tdx: Set APIC bus rate to match with what TDX module enforces Date: Tue, 5 Nov 2024 01:23:23 -0500 Message-Id: <20241105062408.3533704-16-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 TDX advertises core crystal clock with cpuid[0x15] as 25MHz for TD guests and it's unchangeable from VMM. As a result, TDX guest reads the APIC timer as the same frequency, 25MHz. While KVM's default emulated frequency for APIC bus is 1GHz, set the APIC bus rate to match with TDX explicitly to ensure KVM provide correct emulated APIC timer for TD guest. Signed-off-by: Xiaoyao Li --- Changes in v6: - new patch; --- target/i386/kvm/tdx.c | 13 +++++++++++++ target/i386/kvm/tdx.h | 3 +++ 2 files changed, 16 insertions(+) diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 887a5324b439..94b9be62c5dd 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -234,6 +234,19 @@ int tdx_pre_create_vcpu(CPUState *cpu, Error **errp) init_vm = g_malloc0(sizeof(struct kvm_tdx_init_vm) + sizeof(struct kvm_cpuid_entry2) * KVM_MAX_CPUID_ENTRIES); + if (!kvm_check_extension(kvm_state, KVM_CAP_X86_APIC_BUS_CYCLES_NS)) { + error_setg(errp, "KVM doesn't support KVM_CAP_X86_APIC_BUS_CYCLES_NS"); + return -EOPNOTSUPP; + } + + r = kvm_vm_enable_cap(kvm_state, KVM_CAP_X86_APIC_BUS_CYCLES_NS, + 0, TDX_APIC_BUS_CYCLES_NS); + if (r < 0) { + error_setg_errno(errp, -r, + "Unable to set core crystal clock frequency to 25MHz"); + return r; + } + #define SHA384_DIGEST_SIZE 48 if (tdx_guest->mrconfigid) { g_autofree uint8_t *data = qbase64_decode(tdx_guest->mrconfigid, diff --git a/target/i386/kvm/tdx.h b/target/i386/kvm/tdx.h index bc26e24eb9ac..0aebc7e3f6c9 100644 --- a/target/i386/kvm/tdx.h +++ b/target/i386/kvm/tdx.h @@ -14,6 +14,9 @@ typedef struct TdxGuestClass { X86ConfidentialGuestClass parent_class; } TdxGuestClass; +/* TDX requires bus frequency 25MHz */ +#define TDX_APIC_BUS_CYCLES_NS 40 + typedef struct TdxGuest { X86ConfidentialGuest parent_obj; From patchwork Tue Nov 5 06:23:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862405 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0A9211D5CC9 for ; Tue, 5 Nov 2024 06:37:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788651; cv=none; b=ioxRcRmYbG+LQzBs6lIVRUYP7QeSV0E62CK/Fq687GsArN94HPGB3UYDWpZYWJ/fKtdqaHb5JbNc9qr6ZiTBgdH+ku25aYz19f0lnJYJVyGYn7fEkPDcjvLxfBM4Fc1ye4LvXYPMuEcmih0x6WuDGaTKVtnjN7j4PjoDU8Ua7R0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788651; c=relaxed/simple; bh=pMLKK6RqyOLgwiaGzV9tdw+bbfVlPoHbIbNhAVqpC40=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tUhmwuQhBJu3/sEtqnDDOESrjH/cV2H1rPaGAIv9Pc2hbOthvNXed1LObSekPlV5qTeowv41MbeKrsvrBTNfgDELXZRF+tDDMA2DCG3pSzC2/W5Tk99nUZNRCbj6emXqmKSs1cUxAs+G6BShj3xWc1YFIZtqnjIkPr18kl6VCDw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Z914yTo2; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Z914yTo2" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788650; x=1762324650; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pMLKK6RqyOLgwiaGzV9tdw+bbfVlPoHbIbNhAVqpC40=; b=Z914yTo2x9hvuAKQ/HKV9bXIYgjq7516tdBM3uRD/ruGayzlyswIW9z4 6oK1f8yhBBIJDm6PFOutSOgrD/1ctNJWnj+M7UFFDDAMwmnoEugmWsEH8 WAddwgkn5m+lR3BQR2+DkYoUe1Hs2tvoPN4yE0Jx24pj42Ly4HrGBKFk8 JL/9EtONG++TksZU963IbHUzGt6U9iTi+ztEcUqsooXyCx4B9pb08TOyV sCgWe7j5ON88n7uVgfcYeGs5VLPOXYmGuhqYaZotqkXDPKeaA50wRcFcC 4xE0/ZdXV0qat7+xrIaIu0jw2DShSlgfpd/EjM1fN2dpszYpcE0ldLHLe g==; X-CSE-ConnectionGUID: 6jHDS1AARoqOCdzzIJi2QQ== X-CSE-MsgGUID: 0oyZ74pEQwag5cMnxTaGpA== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689469" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689469" 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:37:30 -0800 X-CSE-ConnectionGUID: LmwtOO5fT8i33Nqv7yvPQw== X-CSE-MsgGUID: 2V+gOx61QkK0Zyv5GT/b3g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83988865" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:37:25 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 16/60] i386/tdx: Implement user specified tsc frequency Date: Tue, 5 Nov 2024 01:23:24 -0500 Message-Id: <20241105062408.3533704-17-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Reuse "-cpu,tsc-frequency=" to get user wanted tsc frequency and call VM scope VM_SET_TSC_KHZ to set the tsc frequency of TD before KVM_TDX_INIT_VM. Besides, sanity check the tsc frequency to be in the legal range and legal granularity (required by TDX module). Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann --- Changes in v3: - use @errp to report error info; (Daniel) Changes in v1: - Use VM scope VM_SET_TSC_KHZ to set the TSC frequency of TD since KVM side drop the @tsc_khz field in struct kvm_tdx_init_vm --- target/i386/kvm/kvm.c | 9 +++++++++ target/i386/kvm/tdx.c | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index db676c1336ab..4fafc003e9a7 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -872,6 +872,15 @@ static int kvm_arch_set_tsc_khz(CPUState *cs) int r, cur_freq; bool set_ioctl = false; + /* + * TSC of TD vcpu is immutable, it cannot be set/changed via vcpu scope + * VM_SET_TSC_KHZ, but only be initialized via VM scope VM_SET_TSC_KHZ + * before ioctl KVM_TDX_INIT_VM in tdx_pre_create_vcpu() + */ + if (is_tdx_vm()) { + return 0; + } + if (!env->tsc_khz) { return 0; } diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 94b9be62c5dd..4193211c3190 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -21,6 +21,9 @@ #include "kvm_i386.h" #include "tdx.h" +#define TDX_MIN_TSC_FREQUENCY_KHZ (100 * 1000) +#define TDX_MAX_TSC_FREQUENCY_KHZ (10 * 1000 * 1000) + #define TDX_TD_ATTRIBUTES_DEBUG BIT_ULL(0) #define TDX_TD_ATTRIBUTES_SEPT_VE_DISABLE BIT_ULL(28) #define TDX_TD_ATTRIBUTES_PKS BIT_ULL(30) @@ -247,6 +250,28 @@ int tdx_pre_create_vcpu(CPUState *cpu, Error **errp) return r; } + if (env->tsc_khz && (env->tsc_khz < TDX_MIN_TSC_FREQUENCY_KHZ || + env->tsc_khz > TDX_MAX_TSC_FREQUENCY_KHZ)) { + error_setg(errp, "Invalid TSC %ld KHz, must specify cpu_frequency " + "between [%d, %d] kHz", env->tsc_khz, + TDX_MIN_TSC_FREQUENCY_KHZ, TDX_MAX_TSC_FREQUENCY_KHZ); + return -EINVAL; + } + + if (env->tsc_khz % (25 * 1000)) { + error_setg(errp, "Invalid TSC %ld KHz, it must be multiple of 25MHz", + env->tsc_khz); + return -EINVAL; + } + + /* it's safe even env->tsc_khz is 0. KVM uses host's tsc_khz in this case */ + r = kvm_vm_ioctl(kvm_state, KVM_SET_TSC_KHZ, env->tsc_khz); + if (r < 0) { + error_setg_errno(errp, -r, "Unable to set TSC frequency to %ld kHz", + env->tsc_khz); + return r; + } + #define SHA384_DIGEST_SIZE 48 if (tdx_guest->mrconfigid) { g_autofree uint8_t *data = qbase64_decode(tdx_guest->mrconfigid, From patchwork Tue Nov 5 06:23:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862406 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 529261714A0 for ; Tue, 5 Nov 2024 06:37:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788659; cv=none; b=X9EP8NNew7ckdm7/GOGUT0UGdKDT483EynwB/MSf0uHF/ykSUo915FN+jNOPSnmyidPyr0ytq69vBmpFBQdfcR3lgRmEGNRzIpGRGaOnwJoi0xUsKBVtGi8YeDBmwXyJ1e18PEaRPbotiPKAGR3Bd5vlB+tgGTV5hAs/sahBPnA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788659; c=relaxed/simple; bh=eviZxt2uHCaBFHxS9OQDjWkbBKzkgFdGvefQVCQtPgg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WwytQQOsQTlwsfkPu4BkNrLJ7ODi5ZhvJU8AF1EByBiM8epMaKlqFyoyw6+MTC2YcAyc11Cowc036GcHsg/FDQemcYGd9XXhd64STfIDsNqYJGU5HjEs5bpVd7dsDoOZZzjLeQm5NCdt4kvWSevW5e5imT2G/y92LVUj1MciJaw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=W+oC5gGa; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="W+oC5gGa" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788658; x=1762324658; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=eviZxt2uHCaBFHxS9OQDjWkbBKzkgFdGvefQVCQtPgg=; b=W+oC5gGadj3TAryv4aB0abMcvjBRacnTQ5egb6bnlT2EsJ9W1EJnAME2 LoVD9B/lg2/BgZiz44u3OkfAwK4hEnZt6a/pVzI0BtPRTXlHbQF2j7hSM ZX5FtUCJ+m4eXvY+ILnmRimoc8aHBjInf/npEKMka5UBJj2yDQObjRC0c gWJ2PueEKW4zj7XxKdoAA+n8cTqGOwzxaflm8YpwVIX+9XpdcEZPJ5Sym rUkMyStnpAHikvw9Aky/7v6AqYvG7TKK7+wIX1j1/GsVbNzHnxy4UlIf3 gb/oqiCJfnfMGBQykuLK8y/b0rbuOgAOFb3Um1DA9PprfcC9sK0FQRlnu A==; X-CSE-ConnectionGUID: wL2W0e8LRsu/VbR8vGlp0g== X-CSE-MsgGUID: 0NE4ExfOQh2zpPFrp1hxtg== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689484" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689484" 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:37:38 -0800 X-CSE-ConnectionGUID: h4M+rUgqSuWhYI8H31oyhw== X-CSE-MsgGUID: rtUp98ElRPurPn+fT+1nsg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83988899" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:37:30 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 17/60] i386/tdx: load TDVF for TD guest Date: Tue, 5 Nov 2024 01:23:25 -0500 Message-Id: <20241105062408.3533704-18-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Chao Peng TDVF(OVMF) needs to run at private memory for TD guest. TDX cannot support pflash device since it doesn't support read-only private memory. Thus load TDVF(OVMF) with -bios option for TDs. Use memory_region_init_ram_guest_memfd() to allocate the MemoryRegion for TDVF because it needs to be located at private memory. Also store the MemoryRegion pointer of TDVF since the shared ramblock of it can be discared after it gets copied to private ramblock. Signed-off-by: Chao Peng Co-developed-by: Xiaoyao Li Signed-off-by: Xiaoyao Li --- hw/i386/x86-common.c | 6 +++++- target/i386/kvm/tdx.c | 6 ++++++ target/i386/kvm/tdx.h | 3 +++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/hw/i386/x86-common.c b/hw/i386/x86-common.c index b86c38212eab..1df496a15eff 100644 --- a/hw/i386/x86-common.c +++ b/hw/i386/x86-common.c @@ -44,6 +44,7 @@ #include "standard-headers/asm-x86/bootparam.h" #include CONFIG_DEVICES #include "kvm/kvm_i386.h" +#include "kvm/tdx.h" #ifdef CONFIG_XEN_EMU #include "hw/xen/xen.h" @@ -1007,11 +1008,14 @@ void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware, if (machine_require_guest_memfd(MACHINE(x86ms))) { memory_region_init_ram_guest_memfd(&x86ms->bios, NULL, "pc.bios", bios_size, &error_fatal); + if (is_tdx_vm()) { + tdx_set_tdvf_region(&x86ms->bios); + } } else { memory_region_init_ram(&x86ms->bios, NULL, "pc.bios", bios_size, &error_fatal); } - if (sev_enabled()) { + if (sev_enabled() || is_tdx_vm()) { /* * The concept of a "reset" simply doesn't exist for * confidential computing guests, we have to destroy and diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 4193211c3190..d5ebc2430fd1 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -120,6 +120,12 @@ static int get_tdx_capabilities(Error **errp) return 0; } +void tdx_set_tdvf_region(MemoryRegion *tdvf_mr) +{ + assert(!tdx_guest->tdvf_mr); + tdx_guest->tdvf_mr = tdvf_mr; +} + static int tdx_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) { TdxGuest *tdx = TDX_GUEST(cgs); diff --git a/target/i386/kvm/tdx.h b/target/i386/kvm/tdx.h index 0aebc7e3f6c9..e5d836805385 100644 --- a/target/i386/kvm/tdx.h +++ b/target/i386/kvm/tdx.h @@ -28,6 +28,8 @@ typedef struct TdxGuest { char *mrconfigid; /* base64 encoded sha348 digest */ char *mrowner; /* base64 encoded sha348 digest */ char *mrownerconfig; /* base64 encoded sha348 digest */ + + MemoryRegion *tdvf_mr; } TdxGuest; #ifdef CONFIG_TDX @@ -37,5 +39,6 @@ bool is_tdx_vm(void); #endif /* CONFIG_TDX */ int tdx_pre_create_vcpu(CPUState *cpu, Error **errp); +void tdx_set_tdvf_region(MemoryRegion *tdvf_mr); #endif /* QEMU_I386_TDX_H */ From patchwork Tue Nov 5 06:23:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862407 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BCF4F1FDFA6 for ; Tue, 5 Nov 2024 06:37:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788664; cv=none; b=ZBWY8lENs9oVZq+cKIkj8Z6awCLN3oIBvdd8C0Xu2YBWKSnjy9ZVIIuubKv7TH6mifZZwXpPIUIqtpaYvHacGtAhiHI0I/XKxIzt0rBzBW0PHiQzXKyp90y/9CisF7HkuzpWCfto/30jHychYEkJ+wWBw1AzKh7asukeNi0oVG8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788664; c=relaxed/simple; bh=MB450M7DhV+Mw3GU//ZlpYPh2anAB1c4R48Atz8u8+E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=T4FGjKtanHNkOb025RFU/lZaXKdD5fwOU8JqO1ZLplQeU3Ye+Zt/oso4vibPx/NpJqaVDVeRV5c3TmZseCuXgQyJ36W+nBpMr+ylKTotqTGaMJ1Rtkz4lKIwEStA1XjgQAdEhJ+u2kdtzqhC0VucSavkl7uXm3pJc2sQMu4doeg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=HPHNTDfF; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="HPHNTDfF" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788663; x=1762324663; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=MB450M7DhV+Mw3GU//ZlpYPh2anAB1c4R48Atz8u8+E=; b=HPHNTDfFtvAIw2B8rjaMFhZnhiKXc86wNYmpegdsetpmuU2cnj4p2PWh NM0oVjwj9xiQDJEKT5dur4SO/7icVUl8B5rjo6HyZHnvWwPXV0jJki6yD f7f/c9VQxGNGEOchxIwa02cEdW/lpVsJfk/8tfkP9Lxu4Sx5miVEowFw1 bDAHLbkKj9u3elvGyIPv8PlHLL58Yz6O0MAJy1DuOnCDL19vlZOC7w5yF Dg1Vhcm0EDREO0W1M4E6cbR9APhvZDwG4V6OjSrTIJVGC4GllVQW5JrYe ImMcj5BQxNLJHwIp/iz+MBiskp9cMsXj9AQKNi+SvsvgZ+j0gfnglwiSv w==; X-CSE-ConnectionGUID: iKP6ho4OQQOCkxowZa4JzQ== X-CSE-MsgGUID: aedz8nSkShSg+71NlwmY4Q== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689491" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689491" 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:37:42 -0800 X-CSE-ConnectionGUID: rbEn1p/vQZSeVaXsKkoaBQ== X-CSE-MsgGUID: QJiZgFZnTRCR08aV8dJgjQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83988911" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:37:35 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 18/60] i386/tdvf: Introduce function to parse TDVF metadata Date: Tue, 5 Nov 2024 01:23:26 -0500 Message-Id: <20241105062408.3533704-19-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Isaku Yamahata TDX VM needs to boot with its specialized firmware, Trusted Domain Virtual Firmware (TDVF). QEMU needs to parse TDVF and map it in TD guest memory prior to running the TDX VM. A TDVF Metadata in TDVF image describes the structure of firmware. QEMU refers to it to setup memory for TDVF. Introduce function tdvf_parse_metadata() to parse the metadata from TDVF image and store the info of each TDVF section. TDX metadata is located by a TDX metadata offset block, which is a GUID-ed structure. The data portion of the GUID structure contains only an 4-byte field that is the offset of TDX metadata to the end of firmware file. Select X86_FW_OVMF when TDX is enable to leverage existing functions to parse and search OVMF's GUID-ed structures. Signed-off-by: Isaku Yamahata Co-developed-by: Xiaoyao Li Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann --- Changes in v6: - Drop the the data endianness change for metadata->Length; Changes in v1: - rename tdvf_parse_section_entry() to tdvf_parse_and_check_section_entry() Changes in RFC v4: - rename TDX_METADATA_GUID to TDX_METADATA_OFFSET_GUID --- hw/i386/Kconfig | 1 + hw/i386/meson.build | 1 + hw/i386/tdvf.c | 200 +++++++++++++++++++++++++++++++++++++++++ include/hw/i386/tdvf.h | 51 +++++++++++ 4 files changed, 253 insertions(+) create mode 100644 hw/i386/tdvf.c create mode 100644 include/hw/i386/tdvf.h diff --git a/hw/i386/Kconfig b/hw/i386/Kconfig index 86bc10377c4f..555a000037bc 100644 --- a/hw/i386/Kconfig +++ b/hw/i386/Kconfig @@ -12,6 +12,7 @@ config SGX config TDX bool + select X86_FW_OVMF depends on KVM config PC diff --git a/hw/i386/meson.build b/hw/i386/meson.build index 10bdfde27c69..3bc1da2b6eb4 100644 --- a/hw/i386/meson.build +++ b/hw/i386/meson.build @@ -32,6 +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-stubs.c')) +i386_ss.add(when: 'CONFIG_TDX', if_true: files('tdvf.c')) subdir('kvm') subdir('xen') diff --git a/hw/i386/tdvf.c b/hw/i386/tdvf.c new file mode 100644 index 000000000000..4afa636bfa0e --- /dev/null +++ b/hw/i386/tdvf.c @@ -0,0 +1,200 @@ +/* + * 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/error-report.h" + +#include "hw/i386/pc.h" +#include "hw/i386/tdvf.h" +#include "sysemu/kvm.h" + +#define TDX_METADATA_OFFSET_GUID "e47a6535-984a-4798-865e-4685a7bf8ec2" +#define TDX_METADATA_VERSION 1 +#define TDVF_SIGNATURE 0x46564454 /* TDVF as little endian */ + +typedef struct { + uint32_t DataOffset; + uint32_t RawDataSize; + uint64_t MemoryAddress; + uint64_t MemoryDataSize; + uint32_t Type; + uint32_t Attributes; +} TdvfSectionEntry; + +typedef struct { + uint32_t Signature; + uint32_t Length; + uint32_t Version; + uint32_t NumberOfSectionEntries; + TdvfSectionEntry SectionEntries[]; +} TdvfMetadata; + +struct tdx_metadata_offset { + uint32_t offset; +}; + +static TdvfMetadata *tdvf_get_metadata(void *flash_ptr, int size) +{ + TdvfMetadata *metadata; + uint32_t offset = 0; + uint8_t *data; + + if ((uint32_t) size != size) { + return NULL; + } + + if (pc_system_ovmf_table_find(TDX_METADATA_OFFSET_GUID, &data, NULL)) { + offset = size - le32_to_cpu(((struct tdx_metadata_offset *)data)->offset); + + if (offset + sizeof(*metadata) > size) { + return NULL; + } + } else { + error_report("Cannot find TDX_METADATA_OFFSET_GUID"); + return NULL; + } + + metadata = flash_ptr + offset; + + /* Finally, verify the signature to determine if this is a TDVF image. */ + metadata->Signature = le32_to_cpu(metadata->Signature); + if (metadata->Signature != TDVF_SIGNATURE) { + error_report("Invalid TDVF signature in metadata!"); + return NULL; + } + + /* Sanity check that the TDVF doesn't overlap its own metadata. */ + metadata->Length = le32_to_cpu(metadata->Length); + if (offset + metadata->Length > size) { + return NULL; + } + + /* Only version 1 is supported/defined. */ + metadata->Version = le32_to_cpu(metadata->Version); + if (metadata->Version != TDX_METADATA_VERSION) { + return NULL; + } + + return metadata; +} + +static int tdvf_parse_and_check_section_entry(const TdvfSectionEntry *src, + TdxFirmwareEntry *entry) +{ + entry->data_offset = le32_to_cpu(src->DataOffset); + entry->data_len = le32_to_cpu(src->RawDataSize); + entry->address = le64_to_cpu(src->MemoryAddress); + entry->size = le64_to_cpu(src->MemoryDataSize); + entry->type = le32_to_cpu(src->Type); + entry->attributes = le32_to_cpu(src->Attributes); + + /* sanity check */ + if (entry->size < entry->data_len) { + error_report("Broken metadata RawDataSize 0x%x MemoryDataSize 0x%lx", + entry->data_len, entry->size); + return -1; + } + if (!QEMU_IS_ALIGNED(entry->address, TARGET_PAGE_SIZE)) { + error_report("MemoryAddress 0x%lx not page aligned", entry->address); + return -1; + } + if (!QEMU_IS_ALIGNED(entry->size, TARGET_PAGE_SIZE)) { + error_report("MemoryDataSize 0x%lx not page aligned", entry->size); + return -1; + } + + switch (entry->type) { + case TDVF_SECTION_TYPE_BFV: + case TDVF_SECTION_TYPE_CFV: + /* The sections that must be copied from firmware image to TD memory */ + if (entry->data_len == 0) { + error_report("%d section with RawDataSize == 0", entry->type); + return -1; + } + break; + case TDVF_SECTION_TYPE_TD_HOB: + case TDVF_SECTION_TYPE_TEMP_MEM: + /* The sections that no need to be copied from firmware image */ + if (entry->data_len != 0) { + error_report("%d section with RawDataSize 0x%x != 0", + entry->type, entry->data_len); + return -1; + } + break; + default: + error_report("TDVF contains unsupported section type %d", entry->type); + return -1; + } + + return 0; +} + +int tdvf_parse_metadata(TdxFirmware *fw, void *flash_ptr, int size) +{ + TdvfSectionEntry *sections; + TdvfMetadata *metadata; + ssize_t entries_size; + int i; + + metadata = tdvf_get_metadata(flash_ptr, size); + if (!metadata) { + return -EINVAL; + } + + /* load and parse metadata entries */ + fw->nr_entries = le32_to_cpu(metadata->NumberOfSectionEntries); + if (fw->nr_entries < 2) { + error_report("Invalid number of fw entries (%u) in TDVF Metadata", + fw->nr_entries); + return -EINVAL; + } + + entries_size = fw->nr_entries * sizeof(TdvfSectionEntry); + if (metadata->Length != sizeof(*metadata) + entries_size) { + error_report("TDVF metadata len (0x%x) mismatch, expected (0x%x)", + metadata->Length, + (uint32_t)(sizeof(*metadata) + entries_size)); + return -EINVAL; + } + + fw->entries = g_new(TdxFirmwareEntry, fw->nr_entries); + sections = g_new(TdvfSectionEntry, fw->nr_entries); + + if (!memcpy(sections, (void *)metadata + sizeof(*metadata), entries_size)) { + error_report("Failed to read TDVF section entries"); + goto err; + } + + for (i = 0; i < fw->nr_entries; i++) { + if (tdvf_parse_and_check_section_entry(§ions[i], &fw->entries[i])) { + goto err; + } + } + g_free(sections); + + return 0; + +err: + g_free(sections); + fw->entries = 0; + g_free(fw->entries); + return -EINVAL; +} diff --git a/include/hw/i386/tdvf.h b/include/hw/i386/tdvf.h new file mode 100644 index 000000000000..593341eb2e93 --- /dev/null +++ b/include/hw/i386/tdvf.h @@ -0,0 +1,51 @@ +/* + * 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 . + */ + +#ifndef HW_I386_TDVF_H +#define HW_I386_TDVF_H + +#include "qemu/osdep.h" + +#define TDVF_SECTION_TYPE_BFV 0 +#define TDVF_SECTION_TYPE_CFV 1 +#define TDVF_SECTION_TYPE_TD_HOB 2 +#define TDVF_SECTION_TYPE_TEMP_MEM 3 + +#define TDVF_SECTION_ATTRIBUTES_MR_EXTEND (1U << 0) +#define TDVF_SECTION_ATTRIBUTES_PAGE_AUG (1U << 1) + +typedef struct TdxFirmwareEntry { + uint32_t data_offset; + uint32_t data_len; + uint64_t address; + uint64_t size; + uint32_t type; + uint32_t attributes; +} TdxFirmwareEntry; + +typedef struct TdxFirmware { + uint32_t nr_entries; + TdxFirmwareEntry *entries; +} TdxFirmware; + +int tdvf_parse_metadata(TdxFirmware *fw, void *flash_ptr, int size); + +#endif /* HW_I386_TDVF_H */ From patchwork Tue Nov 5 06:23:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862408 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A1F231F76A8 for ; Tue, 5 Nov 2024 06:37:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788666; cv=none; b=o3dX5A67htUUfqekm5oyCT2LQyTppVEpc8HoZnB17kQ3KSw9bG764cUfPg6WkTved3lP9pHAUeWRoxIQ9by24GXx//bSbtd+yA/0piibURTR0R7Wyw1k+CgYIlaPWBmYsSTUTBKau9/BUT1lqFMaDPtUZemU1qBOCaeSSIL07/o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788666; c=relaxed/simple; bh=Uv4z8mL1bvbBNMAeIE2zAJ1MiSnqSKEkHnSLrWnImks=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EpHylEQ5EnMFo+cfDuTou4MV9uFlF1ao+yZ9+RwENda224Q6MmwgYUvQXcm7LbUM8DCWBqBUY05Y9m2iB4dwLARA1aIUAe5Grm9sCJxJZ2tEudmv49R0z2vmJRLuTb5rTyXR5kzFyhQT8XuWiEb4gg9y52Rz73a5VM2hQNYIUjc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=MdplqzDA; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="MdplqzDA" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788665; x=1762324665; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Uv4z8mL1bvbBNMAeIE2zAJ1MiSnqSKEkHnSLrWnImks=; b=MdplqzDAG8W48StCPV6A5jKemD6qbdXgL/Pi0RTvVY5tk0HfoAxXshvu 6tT9DPCZgq3xH2E0ZiPfRpi+pjp0X96qFY423D9iUuZvaVxs8IqANZK0v pi36Ptg36Y7AFfb4EV3p7eeUps2jhC3ab6Chw8zGyhcN0Hnu28A4c1lMj RH/KBEeJHZbP0e8kzM9rkNYmVxZtj/56KKhjCVPpCx/LBqKPlBV3agcE/ RiAcwGik5wenjb6TPesuDa/VNpLs6Epv0H4lUqsPgVEzdc8hQI2/ft2uN Dk4MVOBTGCHlMsZcdWT61VElxOUshzUcal/iYEITQY0TN/pAUH3sOB8lF w==; X-CSE-ConnectionGUID: 5C8d6NlDRa2a4wInGIVfdw== X-CSE-MsgGUID: 25lPTFEzSgm38WrTACa8nQ== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689499" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689499" 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:37:44 -0800 X-CSE-ConnectionGUID: uLi/5UJcRZuoX2fSx3LoXg== X-CSE-MsgGUID: uLUYXO3NRLOALI68fRxdFg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83988934" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:37:40 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 19/60] i386/tdx: Parse TDVF metadata for TDX VM Date: Tue, 5 Nov 2024 01:23:27 -0500 Message-Id: <20241105062408.3533704-20-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 After TDVF is loaded to bios MemoryRegion, it needs parse TDVF metadata. Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann --- hw/i386/pc_sysfw.c | 7 +++++++ target/i386/kvm/tdx-stub.c | 5 +++++ target/i386/kvm/tdx.c | 5 +++++ target/i386/kvm/tdx.h | 3 +++ 4 files changed, 20 insertions(+) diff --git a/hw/i386/pc_sysfw.c b/hw/i386/pc_sysfw.c index ef80281d28bb..5a373bf129a1 100644 --- a/hw/i386/pc_sysfw.c +++ b/hw/i386/pc_sysfw.c @@ -37,6 +37,7 @@ #include "hw/block/flash.h" #include "sysemu/kvm.h" #include "sev.h" +#include "kvm/tdx.h" #define FLASH_SECTOR_SIZE 4096 @@ -280,5 +281,11 @@ void x86_firmware_configure(hwaddr gpa, void *ptr, int size) } sev_encrypt_flash(gpa, ptr, size, &error_fatal); + } else if (is_tdx_vm()) { + ret = tdx_parse_tdvf(ptr, size); + if (ret) { + error_report("failed to parse TDVF for TDX VM"); + exit(1); + } } } diff --git a/target/i386/kvm/tdx-stub.c b/target/i386/kvm/tdx-stub.c index b614b46d3f4a..a064d583d393 100644 --- a/target/i386/kvm/tdx-stub.c +++ b/target/i386/kvm/tdx-stub.c @@ -6,3 +6,8 @@ int tdx_pre_create_vcpu(CPUState *cpu, Error **errp) { return -EINVAL; } + +int tdx_parse_tdvf(void *flash_ptr, int size) +{ + return -EINVAL; +} diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index d5ebc2430fd1..334dbe95cc77 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -338,6 +338,11 @@ int tdx_pre_create_vcpu(CPUState *cpu, Error **errp) return 0; } +int tdx_parse_tdvf(void *flash_ptr, int size) +{ + return tdvf_parse_metadata(&tdx_guest->tdvf, flash_ptr, size); +} + static bool tdx_guest_get_sept_ve_disable(Object *obj, Error **errp) { TdxGuest *tdx = TDX_GUEST(obj); diff --git a/target/i386/kvm/tdx.h b/target/i386/kvm/tdx.h index e5d836805385..6b7926be3efe 100644 --- a/target/i386/kvm/tdx.h +++ b/target/i386/kvm/tdx.h @@ -6,6 +6,7 @@ #endif #include "confidential-guest.h" +#include "hw/i386/tdvf.h" #define TYPE_TDX_GUEST "tdx-guest" #define TDX_GUEST(obj) OBJECT_CHECK(TdxGuest, (obj), TYPE_TDX_GUEST) @@ -30,6 +31,7 @@ typedef struct TdxGuest { char *mrownerconfig; /* base64 encoded sha348 digest */ MemoryRegion *tdvf_mr; + TdxFirmware tdvf; } TdxGuest; #ifdef CONFIG_TDX @@ -40,5 +42,6 @@ bool is_tdx_vm(void); int tdx_pre_create_vcpu(CPUState *cpu, Error **errp); void tdx_set_tdvf_region(MemoryRegion *tdvf_mr); +int tdx_parse_tdvf(void *flash_ptr, int size); #endif /* QEMU_I386_TDX_H */ From patchwork Tue Nov 5 06:23:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862409 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 386531F76A8 for ; Tue, 5 Nov 2024 06:37:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788670; cv=none; b=iLF3Tv9jBvXq8IPofCpkSsiyoq1LKtWZHRh1a6waVsYFZeRBdY6Xh/llqfe6ZEG0Uk8eTXzERRgytZ/2Tnwn6ZVAkKKKyWH3/EpWVHRwAMi+da4QJUOhCi98f/OnN7DzuGv7GZWNtF0DTiL2mBbQOXPTEsrA9rZil2mL4+oDkxA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788670; c=relaxed/simple; bh=UOhXpmceQkEx8s4Oa9vrgi40TyS418oe+WG7+rZTz0Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Il+cIh+NvKVJZ0l5CcpZ2LggU+inFxg6uBupoCGD0zeEfdkKzqp7lHofqLMVJ3bjtKDClNPDeEerJ706BAb/yguZnFh1WRPccXMgFz3rfePwpSoUSH43qNkNlAU0o3MiUjY8+nIKQ+yqy8Wnyzu7Rq2xvsjxyWLzVdbRGI6OeQU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=hFsfEmKo; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="hFsfEmKo" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788669; x=1762324669; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=UOhXpmceQkEx8s4Oa9vrgi40TyS418oe+WG7+rZTz0Q=; b=hFsfEmKocVYLApeQbANtWOmeNcb7grCBYFFkxCzeg6T3Y18KLrbwwMZE tytRTohJjAzpTD0ILYneMle6CUHlfEuFaXwpRWnqQE6Qo190Oh5hxbBeM n7AB6bIjNqUocVqudtizkp5Nmv38Vu7TOKSWwyL+hPmGUqQVie4Vb+F84 h0QEJxkA0+K4ZBJZ1fzigqT+uzXdBWpAql1yuEIibZKp5cok3PBBZChvr eMewA8fKVuT+MyJ+jZGOrZlW01DonhJVxH0KMQlSod/Zjqh60Lva/FGPe CYTV3+v5jF4Gj3GsmjRc1ZJ3Pa4I8elLsch5NyjlcHrgrgUeg14cmHXlW A==; X-CSE-ConnectionGUID: aK9xtO2GRq2X73lGEx9ang== X-CSE-MsgGUID: ifY7kEwYTRaIk0blvxKv1g== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689520" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689520" 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:37:49 -0800 X-CSE-ConnectionGUID: EWYM0303ShenTTql/6GbMg== X-CSE-MsgGUID: Nr3+H8eZQYORr8CJ9auVUg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83988959" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:37:44 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 20/60] i386/tdx: Don't initialize pc.rom for TDX VMs Date: Tue, 5 Nov 2024 01:23:28 -0500 Message-Id: <20241105062408.3533704-21-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 For TDX, the address below 1MB are entirely general RAM. No need to initialize pc.rom memory region for TDs. Signed-off-by: Xiaoyao Li --- This is more as a workaround of the issue that for q35 machine type, the real memslot update (which requires memslot deletion )for pc.rom happens after tdx_init_memory_region. It leads to the private memory ADD'ed before get lost. I haven't work out a good solution to resolve the order issue. So just skip the pc.rom setup to avoid memslot deletion. --- hw/i386/pc.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/hw/i386/pc.c b/hw/i386/pc.c index 2047633e4cf7..4a23856aed47 100644 --- a/hw/i386/pc.c +++ b/hw/i386/pc.c @@ -43,6 +43,7 @@ #include "sysemu/xen.h" #include "sysemu/reset.h" #include "kvm/kvm_i386.h" +#include "kvm/tdx.h" #include "hw/xen/xen.h" #include "qapi/qmp/qlist.h" #include "qemu/error-report.h" @@ -966,21 +967,23 @@ void pc_memory_init(PCMachineState *pcms, /* Initialize PC system firmware */ pc_system_firmware_init(pcms, rom_memory); - option_rom_mr = g_malloc(sizeof(*option_rom_mr)); - if (machine_require_guest_memfd(machine)) { - memory_region_init_ram_guest_memfd(option_rom_mr, NULL, "pc.rom", - PC_ROM_SIZE, &error_fatal); - } else { - memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE, - &error_fatal); - if (pcmc->pci_enabled) { - memory_region_set_readonly(option_rom_mr, true); + if (!is_tdx_vm()) { + option_rom_mr = g_malloc(sizeof(*option_rom_mr)); + if (machine_require_guest_memfd(machine)) { + memory_region_init_ram_guest_memfd(option_rom_mr, NULL, "pc.rom", + PC_ROM_SIZE, &error_fatal); + } else { + memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE, + &error_fatal); + if (pcmc->pci_enabled) { + memory_region_set_readonly(option_rom_mr, true); + } } + memory_region_add_subregion_overlap(rom_memory, + PC_ROM_MIN_VGA, + option_rom_mr, + 1); } - memory_region_add_subregion_overlap(rom_memory, - PC_ROM_MIN_VGA, - option_rom_mr, - 1); fw_cfg = fw_cfg_arch_create(machine, x86ms->boot_cpus, x86ms->apic_id_limit); From patchwork Tue Nov 5 06:23:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862410 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 633191FCC63 for ; Tue, 5 Nov 2024 06:37:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788674; cv=none; b=rgLyfGwip+U712C0/z0K0pCjHwp6gyp/1TTc6xoNJD+tB7EH33sliNdI12rx6iItbk7UHe2gEN67kMgCY3IC4o6X290EGNg1irMSMBGsciTPS2HDbh354d9NOusjBJnmR4q+m1/9HERjUKyTI3nv7fcZBeerePvP/PF1NFSPVHI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788674; c=relaxed/simple; bh=O+bsng0OpqwNAjJu2viZcM1uSHuVeOnU2MBD79mzkgE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=V7VxryzV0jxSKJ87bnTFChvCJBasOdjeG04dKjXB3fAI2rkwDgiOdb0zQDENo9tCgfSkjx6ovHGO63ykG8oD+Nqi3PX7ZREUu5AFSCgxHb5YnrSP6+vo6K7nxB0euFWiyjiT9fvTxOnVhY2UYSkXMWhBXctfZ0WJuDZilpaKzcc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=lJ6sZ5xR; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="lJ6sZ5xR" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788673; x=1762324673; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=O+bsng0OpqwNAjJu2viZcM1uSHuVeOnU2MBD79mzkgE=; b=lJ6sZ5xR07xGHLj+D14HMhKGMmckab+syj4n/Wp/8gtQe3zEohALtHvb Spj14lRjjpXd9jLpFGM9wcV/Ue0uDErSVIXGk9xITT62Fblu3AdFWZQrN JjVnRuyzJUFCbydmZcKhAMa+PDdw3f7uoAW+JOve3MMkJmjRTQngEVIpH Vmg/y8y5WonRwllV8rcczLfIGemkT7d+84AjQAlk88rsydPKOmA6prHMq ezqv8K6sXq3WVTLaSizjVNHSNQI6D/4fSzjdZ7WZ2XcLILRwOM2dJG7aM h7FGw3NtxqGM1VIc4HI0HyYo+nWZgXFD9DdRLjSP1Ndflj5b4wNit+e15 w==; X-CSE-ConnectionGUID: 8EV8Y5elRkq05/MBIhjdAA== X-CSE-MsgGUID: PQTl/QAhSd+J/2omuAGgSw== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689535" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689535" 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:37:53 -0800 X-CSE-ConnectionGUID: igX+T3kWQlmGCs/qvn4FXA== X-CSE-MsgGUID: flkki1dAQsOAxyMp0EcImg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83988983" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:37:49 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 21/60] i386/tdx: Track mem_ptr for each firmware entry of TDVF Date: Tue, 5 Nov 2024 01:23:29 -0500 Message-Id: <20241105062408.3533704-22-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 For each TDVF sections, QEMU needs to copy the content to guest private memory via KVM API (KVM_TDX_INIT_MEM_REGION). Introduce a field @mem_ptr for TdxFirmwareEntry to track the memory pointer of each TDVF sections. So that QEMU can add/copy them to guest private memory later. TDVF sections can be classified into two groups: - Firmware itself, e.g., TDVF BFV and CFV, that located separately from guest RAM. Its memory pointer is the bios pointer. - Sections located at guest RAM, e.g., TEMP_MEM and TD_HOB. mmap a new memory range for them. Register a machine_init_done callback to do the stuff. Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann --- hw/i386/tdvf.c | 1 + include/hw/i386/tdvf.h | 7 +++++++ target/i386/kvm/tdx.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/hw/i386/tdvf.c b/hw/i386/tdvf.c index 4afa636bfa0e..535409f34b41 100644 --- a/hw/i386/tdvf.c +++ b/hw/i386/tdvf.c @@ -190,6 +190,7 @@ int tdvf_parse_metadata(TdxFirmware *fw, void *flash_ptr, int size) } g_free(sections); + fw->mem_ptr = flash_ptr; return 0; err: diff --git a/include/hw/i386/tdvf.h b/include/hw/i386/tdvf.h index 593341eb2e93..d880af245a73 100644 --- a/include/hw/i386/tdvf.h +++ b/include/hw/i386/tdvf.h @@ -39,13 +39,20 @@ typedef struct TdxFirmwareEntry { uint64_t size; uint32_t type; uint32_t attributes; + + void *mem_ptr; } TdxFirmwareEntry; typedef struct TdxFirmware { + void *mem_ptr; + uint32_t nr_entries; TdxFirmwareEntry *entries; } TdxFirmware; +#define for_each_tdx_fw_entry(fw, e) \ + for (e = (fw)->entries; e != (fw)->entries + (fw)->nr_entries; e++) + int tdvf_parse_metadata(TdxFirmware *fw, void *flash_ptr, int size); #endif /* HW_I386_TDVF_H */ diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 334dbe95cc77..6777f66a6451 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -14,9 +14,13 @@ #include "qemu/osdep.h" #include "qemu/error-report.h" #include "qemu/base64.h" +#include "qemu/mmap-alloc.h" #include "qapi/error.h" #include "qom/object_interfaces.h" +#include "sysemu/sysemu.h" +#include "hw/i386/x86.h" +#include "hw/i386/tdvf.h" #include "hw/i386/x86.h" #include "kvm_i386.h" #include "tdx.h" @@ -126,6 +130,33 @@ void tdx_set_tdvf_region(MemoryRegion *tdvf_mr) tdx_guest->tdvf_mr = tdvf_mr; } +static void tdx_finalize_vm(Notifier *notifier, void *unused) +{ + TdxFirmware *tdvf = &tdx_guest->tdvf; + TdxFirmwareEntry *entry; + + for_each_tdx_fw_entry(tdvf, entry) { + switch (entry->type) { + case TDVF_SECTION_TYPE_BFV: + case TDVF_SECTION_TYPE_CFV: + entry->mem_ptr = tdvf->mem_ptr + entry->data_offset; + break; + case TDVF_SECTION_TYPE_TD_HOB: + case TDVF_SECTION_TYPE_TEMP_MEM: + entry->mem_ptr = qemu_ram_mmap(-1, entry->size, + qemu_real_host_page_size(), 0, 0); + break; + default: + error_report("Unsupported TDVF section %d", entry->type); + exit(1); + } + } +} + +static Notifier tdx_machine_done_notify = { + .notify = tdx_finalize_vm, +}; + static int tdx_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) { TdxGuest *tdx = TDX_GUEST(cgs); @@ -140,6 +171,8 @@ static int tdx_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) } } + qemu_add_machine_init_done_notifier(&tdx_machine_done_notify); + tdx_guest = tdx; return 0; } From patchwork Tue Nov 5 06:23:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862411 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A8C721FCF45 for ; Tue, 5 Nov 2024 06:37:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788679; cv=none; b=teapMB00y5jup0EORmMif/MEdIhaBpTGZns9UlyVL6YoP6TY41PK9z+tv/cip0gDtoJ3ccwo/ZpZH9Uw8v2f/rnfum3C/n5T31SBgJXC1csnMPn0+hwFPJvnZV7C/efTaESgI6dsyEW1D4mJ5DErF78knq8Rcg52ouIg/mVXqDk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788679; c=relaxed/simple; bh=tiZmHs4SbtcRLxGmTdYVWdzKXeiosxgYMBJw0d6ZN1Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EtFol4EB4yJIQLfKfB18pqvdEgfpyOiV6SF3sgceXNMHcUMuJ7JLW0SQyxDoxxFlnrp9arOtNIQIHYv1Org7oIEy/gwBwFFRaNWdTRHgtIsTCqD3xWKBb3eDjQGL+y4VJDXN6jSUVmpbZt+QR26FDm44x5+wHZ31PiXfgqgSvDc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=LXpQAEIP; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="LXpQAEIP" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788678; x=1762324678; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=tiZmHs4SbtcRLxGmTdYVWdzKXeiosxgYMBJw0d6ZN1Y=; b=LXpQAEIPjvb7vh6Aew/nkbSOEGpNy3aLAnS9X7kJmsjzlvzLfjsEym2e zuWheKB7WOKHJuWa6HfmElhMgt+6Z7mlJxVZ/ts75jgM17dXXU1bcViwe yRw+43KMkPHsksj/K0O2LtBBP6es+lvNZ+n38SntLWRoAuhl3qt2wUyU+ WK2BLi5Ms2Wz5FU4GnJIfqjIw/QmfRxVassPUC+3rSkFWboJQ5EIwqCW3 wlH4FbENZXT80r9+w2mn6dlC3yrCOTIFf5ok02FU10txhCk8ncAchfChr 7JaD22XrpWSCM/S0+nM0HcVfUSVharuUqL0VWFJK3aifbMS5z5i4LO1dT A==; X-CSE-ConnectionGUID: POKVADIGSkmTS+/uCEoyAA== X-CSE-MsgGUID: z2fyhAPRT3ae8y6+0jmP5Q== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689545" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689545" 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:37:57 -0800 X-CSE-ConnectionGUID: 32Lx/LQZQMqakH3q9ZtDCA== X-CSE-MsgGUID: VWRxPBIxTIWZtwHpbO/mfQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83988996" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:37:53 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 22/60] i386/tdx: Track RAM entries for TDX VM Date: Tue, 5 Nov 2024 01:23:30 -0500 Message-Id: <20241105062408.3533704-23-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The RAM of TDX VM can be classified into two types: - TDX_RAM_UNACCEPTED: default type of TDX memory, which needs to be accepted by TDX guest before it can be used and will be all-zeros after being accepted. - TDX_RAM_ADDED: the RAM that is ADD'ed to TD guest before running, and can be used directly. E.g., TD HOB and TEMP MEM that needed by TDVF. Maintain TdxRamEntries[] which grabs the initial RAM info from e820 table and mark each RAM range as default type TDX_RAM_UNACCEPTED. Then turn the range of TD HOB and TEMP MEM to TDX_RAM_ADDED since these ranges will be ADD'ed before TD runs and no need to be accepted runtime. The TdxRamEntries[] are later used to setup the memory TD resource HOB that passes memory info from QEMU to TDVF. Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann --- Changes in v3: - use enum TdxRamType in struct TdxRamEntry; (Isaku) - Fix the indention; (Daniel) Changes in v1: - simplify the algorithm of tdx_accept_ram_range() (Suggested-by: Gerd Hoffman) (1) Change the existing entry to cover the accepted ram range. (2) If there is room before the accepted ram range add a TDX_RAM_UNACCEPTED entry for that. (3) If there is room after the accepted ram range add a TDX_RAM_UNACCEPTED entry for that. --- target/i386/kvm/tdx.c | 111 ++++++++++++++++++++++++++++++++++++++++++ target/i386/kvm/tdx.h | 14 ++++++ 2 files changed, 125 insertions(+) diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 6777f66a6451..76b40f278dd4 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -19,6 +19,7 @@ #include "qom/object_interfaces.h" #include "sysemu/sysemu.h" +#include "hw/i386/e820_memory_layout.h" #include "hw/i386/x86.h" #include "hw/i386/tdvf.h" #include "hw/i386/x86.h" @@ -130,11 +131,117 @@ void tdx_set_tdvf_region(MemoryRegion *tdvf_mr) tdx_guest->tdvf_mr = tdvf_mr; } +static void tdx_add_ram_entry(uint64_t address, uint64_t length, + enum TdxRamType type) +{ + uint32_t nr_entries = tdx_guest->nr_ram_entries; + tdx_guest->ram_entries = g_renew(TdxRamEntry, tdx_guest->ram_entries, + nr_entries + 1); + + tdx_guest->ram_entries[nr_entries].address = address; + tdx_guest->ram_entries[nr_entries].length = length; + tdx_guest->ram_entries[nr_entries].type = type; + tdx_guest->nr_ram_entries++; +} + +static int tdx_accept_ram_range(uint64_t address, uint64_t length) +{ + uint64_t head_start, tail_start, head_length, tail_length; + uint64_t tmp_address, tmp_length; + TdxRamEntry *e; + int i; + + for (i = 0; i < tdx_guest->nr_ram_entries; i++) { + e = &tdx_guest->ram_entries[i]; + + if (address + length <= e->address || + e->address + e->length <= address) { + continue; + } + + /* + * The to-be-accepted ram range must be fully contained by one + * RAM entry. + */ + if (e->address > address || + e->address + e->length < address + length) { + return -EINVAL; + } + + if (e->type == TDX_RAM_ADDED) { + return -EINVAL; + } + + break; + } + + if (i == tdx_guest->nr_ram_entries) { + return -1; + } + + tmp_address = e->address; + tmp_length = e->length; + + e->address = address; + e->length = length; + e->type = TDX_RAM_ADDED; + + head_length = address - tmp_address; + if (head_length > 0) { + head_start = tmp_address; + tdx_add_ram_entry(head_start, head_length, TDX_RAM_UNACCEPTED); + } + + tail_start = address + length; + if (tail_start < tmp_address + tmp_length) { + tail_length = tmp_address + tmp_length - tail_start; + tdx_add_ram_entry(tail_start, tail_length, TDX_RAM_UNACCEPTED); + } + + return 0; +} + +static int tdx_ram_entry_compare(const void *lhs_, const void* rhs_) +{ + const TdxRamEntry *lhs = lhs_; + const TdxRamEntry *rhs = rhs_; + + if (lhs->address == rhs->address) { + return 0; + } + if (le64_to_cpu(lhs->address) > le64_to_cpu(rhs->address)) { + return 1; + } + return -1; +} + +static void tdx_init_ram_entries(void) +{ + unsigned i, j, nr_e820_entries; + + nr_e820_entries = e820_get_table(NULL); + tdx_guest->ram_entries = g_new(TdxRamEntry, nr_e820_entries); + + for (i = 0, j = 0; i < nr_e820_entries; i++) { + uint64_t addr, len; + + if (e820_get_entry(i, E820_RAM, &addr, &len)) { + tdx_guest->ram_entries[j].address = addr; + tdx_guest->ram_entries[j].length = len; + tdx_guest->ram_entries[j].type = TDX_RAM_UNACCEPTED; + j++; + } + } + tdx_guest->nr_ram_entries = j; +} + static void tdx_finalize_vm(Notifier *notifier, void *unused) { TdxFirmware *tdvf = &tdx_guest->tdvf; TdxFirmwareEntry *entry; + tdx_init_ram_entries(); + for_each_tdx_fw_entry(tdvf, entry) { switch (entry->type) { case TDVF_SECTION_TYPE_BFV: @@ -145,12 +252,16 @@ static void tdx_finalize_vm(Notifier *notifier, void *unused) case TDVF_SECTION_TYPE_TEMP_MEM: entry->mem_ptr = qemu_ram_mmap(-1, entry->size, qemu_real_host_page_size(), 0, 0); + tdx_accept_ram_range(entry->address, entry->size); break; default: error_report("Unsupported TDVF section %d", entry->type); exit(1); } } + + qsort(tdx_guest->ram_entries, tdx_guest->nr_ram_entries, + sizeof(TdxRamEntry), &tdx_ram_entry_compare); } static Notifier tdx_machine_done_notify = { diff --git a/target/i386/kvm/tdx.h b/target/i386/kvm/tdx.h index 6b7926be3efe..c669e0d0daca 100644 --- a/target/i386/kvm/tdx.h +++ b/target/i386/kvm/tdx.h @@ -18,6 +18,17 @@ typedef struct TdxGuestClass { /* TDX requires bus frequency 25MHz */ #define TDX_APIC_BUS_CYCLES_NS 40 +enum TdxRamType { + TDX_RAM_UNACCEPTED, + TDX_RAM_ADDED, +}; + +typedef struct TdxRamEntry { + uint64_t address; + uint64_t length; + enum TdxRamType type; +} TdxRamEntry; + typedef struct TdxGuest { X86ConfidentialGuest parent_obj; @@ -32,6 +43,9 @@ typedef struct TdxGuest { MemoryRegion *tdvf_mr; TdxFirmware tdvf; + + uint32_t nr_ram_entries; + TdxRamEntry *ram_entries; } TdxGuest; #ifdef CONFIG_TDX From patchwork Tue Nov 5 06:23:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862412 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1B7C61FCF45 for ; Tue, 5 Nov 2024 06:38:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788683; cv=none; b=erae7vegO7ZryTGeWKsFWXAWeG0uQPK5wi2IiDpeP8r24pR3zKuby8iVfFn7N2H7ZPUGS8Dza83q+YBBK41u/h/Jube25CVZ3Ddka3aCuxG6IGV8jSqNKbqIVB/Z5y5v1YOKQL5/IAbShRv0s9sge42yXJSYQ2ImVWzubSCo/Kc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788683; c=relaxed/simple; bh=pw70+E9f7yP0q/kg6tok3Z3RGqPvOBZoP8fDG+f0630=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fOd1FgjylbENq6pCVsEJbir25tgytSmnliLyUhIU7+PFLqIsKahn4EVG+NtejyMli5Xo4wX6VQj4s0bELAKEFAGfCBa/FfS7ugmkLJ287v/cu11ZJGsWWVAhWTTMmtsiwEpl3N9K3AK/WevbmEpmWGwMrtMbV0AtSbjmQ1itt4s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=fufVmIAe; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="fufVmIAe" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788682; x=1762324682; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pw70+E9f7yP0q/kg6tok3Z3RGqPvOBZoP8fDG+f0630=; b=fufVmIAeXyiqHyJMpTilxBOy2DwOyX6ZYyRa6mvmWFTSUaPfr+6dFUmD CHUVNO7VDKvRyrZZAHJ1aMmk+YNzIWeQ79Q0tpghwd+axUnIOSRO/fwRQ ibWwoycwu/dRXfKakJPfgAqtHXGllxBgq5wyrQdpjo3+CB2igC+RA/Ye8 8B1lYhXq0gkbMTrkO1rwJjtU7DfT/GY++aMk1v3dRyJV/vGFt3/7TZHFN 0zj7QGa7DesN8Ej+dJEcmmb7vr51OblMY6vEiK1nTcuQBHwyu1XOLERxq 9ztZl60CaxQA4pDcRa2snwL6agm5pQbb/Xz23Ip/v35EtEFiD5Cex7Syw Q==; X-CSE-ConnectionGUID: /jgO6yB/RJKvbt0sudp8Lw== X-CSE-MsgGUID: SCUdduFoTkGzOKYpxaqlMQ== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689554" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689554" 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:02 -0800 X-CSE-ConnectionGUID: RVGuAhgLRpqW3JT0yZtiFg== X-CSE-MsgGUID: vTO8ntceRhecz3VMf69SWw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989010" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:37:58 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 23/60] headers: Add definitions from UEFI spec for volumes, resources, etc... Date: Tue, 5 Nov 2024 01:23:31 -0500 Message-Id: <20241105062408.3533704-24-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add UEFI definitions for literals, enums, structs, GUIDs, etc... that will be used by TDX to build the UEFI Hand-Off Block (HOB) that is passed to the Trusted Domain Virtual Firmware (TDVF). All values come from the UEFI specification [1], PI spec [2] and TDVF design guide[3]. [1] UEFI Specification v2.1.0 https://uefi.org/sites/default/files/resources/UEFI_Spec_2_10_Aug29.pdf [2] UEFI PI spec v1.8 https://uefi.org/sites/default/files/resources/UEFI_PI_Spec_1_8_March3.pdf [3] https://software.intel.com/content/dam/develop/external/us/en/documents/tdx-virtual-firmware-design-guide-rev-1.pdf Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann --- include/standard-headers/uefi/uefi.h | 198 +++++++++++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 include/standard-headers/uefi/uefi.h diff --git a/include/standard-headers/uefi/uefi.h b/include/standard-headers/uefi/uefi.h new file mode 100644 index 000000000000..b15aba796156 --- /dev/null +++ b/include/standard-headers/uefi/uefi.h @@ -0,0 +1,198 @@ +/* + * 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 . + * + */ + +#ifndef HW_I386_UEFI_H +#define HW_I386_UEFI_H + +/***************************************************************************/ +/* + * basic EFI definitions + * supplemented with UEFI Specification Version 2.8 (Errata A) + * released February 2020 + */ +/* UEFI integer is little endian */ + +typedef struct { + uint32_t Data1; + uint16_t Data2; + uint16_t Data3; + uint8_t Data4[8]; +} EFI_GUID; + +typedef enum { + EfiReservedMemoryType, + EfiLoaderCode, + EfiLoaderData, + EfiBootServicesCode, + EfiBootServicesData, + EfiRuntimeServicesCode, + EfiRuntimeServicesData, + EfiConventionalMemory, + EfiUnusableMemory, + EfiACPIReclaimMemory, + EfiACPIMemoryNVS, + EfiMemoryMappedIO, + EfiMemoryMappedIOPortSpace, + EfiPalCode, + EfiPersistentMemory, + EfiUnacceptedMemoryType, + EfiMaxMemoryType +} EFI_MEMORY_TYPE; + +#define EFI_HOB_HANDOFF_TABLE_VERSION 0x0009 + +#define EFI_HOB_TYPE_HANDOFF 0x0001 +#define EFI_HOB_TYPE_MEMORY_ALLOCATION 0x0002 +#define EFI_HOB_TYPE_RESOURCE_DESCRIPTOR 0x0003 +#define EFI_HOB_TYPE_GUID_EXTENSION 0x0004 +#define EFI_HOB_TYPE_FV 0x0005 +#define EFI_HOB_TYPE_CPU 0x0006 +#define EFI_HOB_TYPE_MEMORY_POOL 0x0007 +#define EFI_HOB_TYPE_FV2 0x0009 +#define EFI_HOB_TYPE_LOAD_PEIM_UNUSED 0x000A +#define EFI_HOB_TYPE_UEFI_CAPSULE 0x000B +#define EFI_HOB_TYPE_FV3 0x000C +#define EFI_HOB_TYPE_UNUSED 0xFFFE +#define EFI_HOB_TYPE_END_OF_HOB_LIST 0xFFFF + +typedef struct { + uint16_t HobType; + uint16_t HobLength; + uint32_t Reserved; +} EFI_HOB_GENERIC_HEADER; + +typedef uint64_t EFI_PHYSICAL_ADDRESS; +typedef uint32_t EFI_BOOT_MODE; + +typedef struct { + EFI_HOB_GENERIC_HEADER Header; + uint32_t Version; + EFI_BOOT_MODE BootMode; + EFI_PHYSICAL_ADDRESS EfiMemoryTop; + EFI_PHYSICAL_ADDRESS EfiMemoryBottom; + EFI_PHYSICAL_ADDRESS EfiFreeMemoryTop; + EFI_PHYSICAL_ADDRESS EfiFreeMemoryBottom; + EFI_PHYSICAL_ADDRESS EfiEndOfHobList; +} EFI_HOB_HANDOFF_INFO_TABLE; + +#define EFI_RESOURCE_SYSTEM_MEMORY 0x00000000 +#define EFI_RESOURCE_MEMORY_MAPPED_IO 0x00000001 +#define EFI_RESOURCE_IO 0x00000002 +#define EFI_RESOURCE_FIRMWARE_DEVICE 0x00000003 +#define EFI_RESOURCE_MEMORY_MAPPED_IO_PORT 0x00000004 +#define EFI_RESOURCE_MEMORY_RESERVED 0x00000005 +#define EFI_RESOURCE_IO_RESERVED 0x00000006 +#define EFI_RESOURCE_MEMORY_UNACCEPTED 0x00000007 +#define EFI_RESOURCE_MAX_MEMORY_TYPE 0x00000008 + +#define EFI_RESOURCE_ATTRIBUTE_PRESENT 0x00000001 +#define EFI_RESOURCE_ATTRIBUTE_INITIALIZED 0x00000002 +#define EFI_RESOURCE_ATTRIBUTE_TESTED 0x00000004 +#define EFI_RESOURCE_ATTRIBUTE_SINGLE_BIT_ECC 0x00000008 +#define EFI_RESOURCE_ATTRIBUTE_MULTIPLE_BIT_ECC 0x00000010 +#define EFI_RESOURCE_ATTRIBUTE_ECC_RESERVED_1 0x00000020 +#define EFI_RESOURCE_ATTRIBUTE_ECC_RESERVED_2 0x00000040 +#define EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED 0x00000080 +#define EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED 0x00000100 +#define EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED 0x00000200 +#define EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE 0x00000400 +#define EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE 0x00000800 +#define EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE 0x00001000 +#define EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE 0x00002000 +#define EFI_RESOURCE_ATTRIBUTE_16_BIT_IO 0x00004000 +#define EFI_RESOURCE_ATTRIBUTE_32_BIT_IO 0x00008000 +#define EFI_RESOURCE_ATTRIBUTE_64_BIT_IO 0x00010000 +#define EFI_RESOURCE_ATTRIBUTE_UNCACHED_EXPORTED 0x00020000 +#define EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTED 0x00040000 +#define EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTABLE 0x00080000 +#define EFI_RESOURCE_ATTRIBUTE_READ_PROTECTABLE 0x00100000 +#define EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTABLE 0x00200000 +#define EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTABLE 0x00400000 +#define EFI_RESOURCE_ATTRIBUTE_PERSISTENT 0x00800000 +#define EFI_RESOURCE_ATTRIBUTE_PERSISTABLE 0x01000000 +#define EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE 0x02000000 + +typedef uint32_t EFI_RESOURCE_TYPE; +typedef uint32_t EFI_RESOURCE_ATTRIBUTE_TYPE; + +typedef struct { + EFI_HOB_GENERIC_HEADER Header; + EFI_GUID Owner; + EFI_RESOURCE_TYPE ResourceType; + EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute; + EFI_PHYSICAL_ADDRESS PhysicalStart; + uint64_t ResourceLength; +} EFI_HOB_RESOURCE_DESCRIPTOR; + +typedef struct { + EFI_HOB_GENERIC_HEADER Header; + EFI_GUID Name; + + /* guid specific data follows */ +} EFI_HOB_GUID_TYPE; + +typedef struct { + EFI_HOB_GENERIC_HEADER Header; + EFI_PHYSICAL_ADDRESS BaseAddress; + uint64_t Length; +} EFI_HOB_FIRMWARE_VOLUME; + +typedef struct { + EFI_HOB_GENERIC_HEADER Header; + EFI_PHYSICAL_ADDRESS BaseAddress; + uint64_t Length; + EFI_GUID FvName; + EFI_GUID FileName; +} EFI_HOB_FIRMWARE_VOLUME2; + +typedef struct { + EFI_HOB_GENERIC_HEADER Header; + EFI_PHYSICAL_ADDRESS BaseAddress; + uint64_t Length; + uint32_t AuthenticationStatus; + bool ExtractedFv; + EFI_GUID FvName; + EFI_GUID FileName; +} EFI_HOB_FIRMWARE_VOLUME3; + +typedef struct { + EFI_HOB_GENERIC_HEADER Header; + uint8_t SizeOfMemorySpace; + uint8_t SizeOfIoSpace; + uint8_t Reserved[6]; +} EFI_HOB_CPU; + +typedef struct { + EFI_HOB_GENERIC_HEADER Header; +} EFI_HOB_MEMORY_POOL; + +typedef struct { + EFI_HOB_GENERIC_HEADER Header; + + EFI_PHYSICAL_ADDRESS BaseAddress; + uint64_t Length; +} EFI_HOB_UEFI_CAPSULE; + +#define EFI_HOB_OWNER_ZERO \ + ((EFI_GUID){ 0x00000000, 0x0000, 0x0000, \ + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }) + +#endif From patchwork Tue Nov 5 06:23:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862413 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 86AC21F16B for ; Tue, 5 Nov 2024 06:38:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788689; cv=none; b=J7pHAmFZzfMrg6ABgz/hp3tAuIiGPBDPylS7D4/xncDmF91nE5tbxR+OShpHJXwlDZGNPpg6D+82+uUu7l+FrThuAfGog9rZWQFsYJgrwUJN1GEnjD/XKXPYGS2/j4MzNEPYW0yMTtjia8Ycx3XE6JpzSkaxfGX+WTu87v5T+3o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788689; c=relaxed/simple; bh=ztCvbGM6vH5Ut/+6plgKBcbU9yS14b6PXdQLMgJMQR8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=J3tsr65stsaACMJDmXZbQGweeIdNB1QXh3dG4gyYnYrE6XiOjrinICHoUrOes2mvvIz7jnm0Krl2MRuX4L10E85tGXVrmnOXDl8rcFgBh31MfMx9wtfxrBbAjBjDBx1M0JK8R+o9WTx5EufQ3PyslaJ+NOearBg5mR6g1R8e9VY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=dZ5y9sCN; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="dZ5y9sCN" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788688; x=1762324688; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ztCvbGM6vH5Ut/+6plgKBcbU9yS14b6PXdQLMgJMQR8=; b=dZ5y9sCNsq1Po81zUJWEBs0m+KzLZojxql0mELXIe8qp4qWtxv5oVvfK B5Bh0RzoXZP9iSnADwtDoO/m7sy25vD4Y+m95RCx7ysPztF3ia3pvgVky qXSJ6TGO/I5vciRjko+LKpQJI7v5qJ8qaZ84/ZHtbXs0d/8qTVPtAt6iY hOlISvcyTvbgpq7ZFMDIQH3Hnlj+QGUqAY4EiiUQxuzk/fv82M6nFrDc0 PR5dtFD7kVfPRz34dUBG60fhEYZRdf6Qb7Y1lCBTlgC91y+7exGvFpsuP MP4laZZkm4y3iZVf4sTN87mxrGp6GX4raYvPos0CVCCflq7fAYRUqGXsx Q==; X-CSE-ConnectionGUID: NCZJ75HNTH6L370aic05IQ== X-CSE-MsgGUID: UQmwQWYjQbapF2ZeA3Xdow== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689564" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689564" 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 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" 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 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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-stubs.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')) 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 = 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 = 0x%" PRIx64, size); + exit(1); + } + + ret = hob->current; + hob->current += 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 = 0; i < tdx->nr_ram_entries; i++) { + e = &tdx->ram_entries[i]; + + if (e->type == TDX_RAM_UNACCEPTED) { + resource_type = EFI_RESOURCE_MEMORY_UNACCEPTED; + attr = EFI_RESOURCE_ATTRIBUTE_TDVF_UNACCEPTED; + } else if (e->type == TDX_RAM_ADDED) { + resource_type = EFI_RESOURCE_SYSTEM_MEMORY; + attr = EFI_RESOURCE_ATTRIBUTE_TDVF_PRIVATE; + } else { + error_report("unknown TDX_RAM_ENTRY type %d", e->type); + exit(1); + } + + region = tdvf_get_area(hob, sizeof(*region)); + *region = (EFI_HOB_RESOURCE_DESCRIPTOR) { + .Header = { + .HobType = EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, + .HobLength = cpu_to_le16(sizeof(*region)), + .Reserved = cpu_to_le32(0), + }, + .Owner = EFI_HOB_OWNER_ZERO, + .ResourceType = cpu_to_le32(resource_type), + .ResourceAttribute = cpu_to_le32(attr), + .PhysicalStart = cpu_to_le64(e->address), + .ResourceLength = cpu_to_le64(e->length), + }; + } +} + +void tdvf_hob_create(TdxGuest *tdx, TdxFirmwareEntry *td_hob) +{ + TdvfHob hob = { + .hob_addr = td_hob->address, + .size = td_hob->size, + .ptr = td_hob->mem_ptr, + + .current = td_hob->mem_ptr, + .end = 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 = tdvf_get_area(&hob, sizeof(*hit)); + *hit = (EFI_HOB_HANDOFF_INFO_TABLE) { + .Header = { + .HobType = EFI_HOB_TYPE_HANDOFF, + .HobLength = cpu_to_le16(sizeof(*hit)), + .Reserved = cpu_to_le32(0), + }, + .Version = cpu_to_le32(EFI_HOB_HANDOFF_TABLE_VERSION), + .BootMode = cpu_to_le32(0), + .EfiMemoryTop = cpu_to_le64(0), + .EfiMemoryBottom = cpu_to_le64(0), + .EfiFreeMemoryTop = cpu_to_le64(0), + .EfiFreeMemoryBottom = cpu_to_le64(0), + .EfiEndOfHobList = cpu_to_le64(0), /* initialized later */ + }; + + tdvf_hob_add_memory_resources(tdx, &hob); + + last_hob = tdvf_get_area(&hob, sizeof(*last_hob)); + *last_hob = (EFI_HOB_GENERIC_HEADER) { + .HobType = EFI_HOB_TYPE_END_OF_HOB_LIST, + .HobLength = cpu_to_le16(sizeof(*last_hob)), + .Reserved = cpu_to_le32(0), + }; + hit->EfiEndOfHobList = 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" @@ -131,6 +132,19 @@ void tdx_set_tdvf_region(MemoryRegion *tdvf_mr) tdx_guest->tdvf_mr = tdvf_mr; } +static TdxFirmwareEntry *tdx_get_hob_entry(TdxGuest *tdx) +{ + TdxFirmwareEntry *entry; + + for_each_tdx_fw_entry(&tdx->tdvf, entry) { + if (entry->type == 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 *unused) 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)); } static Notifier tdx_machine_done_notify = { From patchwork Tue Nov 5 06:23:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862433 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 672601F16B for ; Tue, 5 Nov 2024 06:38:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788694; cv=none; b=e08ZnbWfl25IqRX7qOzzT5m+hsf/xvm7V2VgP3qkE/ZwQWMWTOKrFNZH2FLI/ECXsoWzRMbv0oQpbxnRBtq8NQe4b735c9uCghHVs6KUe1D4607Y1scYp9o8K5MMb9jNB9w3Ai5QT/yyj9nR8Xlxg0lyBCMLhlcXIECL21U5Amk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788694; c=relaxed/simple; bh=rvObCK3RI5s5qGyDuVPMqNnDGspWafj/j5UMlEUdJUA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LXJwU3bHQUbBHLoa7SusnryTXdre2/LOe857swM3H5rGa7vjv2mvA5+CztNoDbvI30urHchxoCL2gj3Q75CcEgOgHRdOff2isoFH2xAgabTFYU5W9LWkZ9dvAHNWfuvmbOxriKTbn7onxVeuwcZCJDxMKwl3tOTJrrO+V2zhiLg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=W5Lupe+5; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="W5Lupe+5" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788692; x=1762324692; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rvObCK3RI5s5qGyDuVPMqNnDGspWafj/j5UMlEUdJUA=; b=W5Lupe+57xANXsdG93Gq8HFS6IfT3xLqoMw0a1tdK+x47/QGoK6jt0cL r9AG8G2LTDVZwI/J4XfXmIWfESFubh1zup7DZ4OYvTZ9WkLySJ+uCFR/C 3KfH7T8hHT8+qAVgxTrCNLjKudONhT2NEjC5AuubIJHxWW9i025PTnPc4 n7HGx19dpvNnUfdlpZXKShIyX764Azkpceh3CorRIm/qgerzPmLKjbvW7 hnDGzKjIpIdiypNw3rI3WGADAB1kRf0tupd9mWnU9VwrVga1iYXxDhJ9C oxHaETdD43aQl+VuP+wRG/H0F+DSfxPFmdszZitWxcebt7XkS3LxVuIdo A==; X-CSE-ConnectionGUID: 1W5tzAhFTeKIm1RenTtduw== X-CSE-MsgGUID: OcDaAO9VTiOxIPVagvxtCA== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689578" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689578" 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:12 -0800 X-CSE-ConnectionGUID: O+gOavi8QUaJkXjcuFvZWw== X-CSE-MsgGUID: f4LDZ/alR22cvlK2YFWB8w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989038" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:38:06 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 25/60] i386/tdx: Add TDVF memory via KVM_TDX_INIT_MEM_REGION Date: Tue, 5 Nov 2024 01:23:33 -0500 Message-Id: <20241105062408.3533704-26-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Isaku Yamahata TDVF firmware (CODE and VARS) needs to be copied to TD's private memory via KVM_TDX_INIT_MEM_REGION, as well as TD HOB and TEMP memory. If the TDVF section has TDVF_SECTION_ATTRIBUTES_MR_EXTEND set in the flag, calling KVM_TDX_EXTEND_MEMORY to extend the measurement. After populating the TDVF memory, the original image located in shared ramblock can be discarded. Signed-off-by: Isaku Yamahata Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann --- Changes in v6: - switch back to use KVM_TDX_INIT_MEM_REGION according to KVM's change; --- target/i386/kvm/tdx.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 6720c785a4ad..0a6ac62de7ff 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -18,6 +18,7 @@ #include "qapi/error.h" #include "qom/object_interfaces.h" #include "sysemu/sysemu.h" +#include "exec/ramblock.h" #include "hw/i386/e820_memory_layout.h" #include "hw/i386/x86.h" @@ -253,6 +254,8 @@ static void tdx_finalize_vm(Notifier *notifier, void *unused) { TdxFirmware *tdvf = &tdx_guest->tdvf; TdxFirmwareEntry *entry; + RAMBlock *ram_block; + int r; tdx_init_ram_entries(); @@ -278,6 +281,42 @@ static void tdx_finalize_vm(Notifier *notifier, void *unused) sizeof(TdxRamEntry), &tdx_ram_entry_compare); tdvf_hob_create(tdx_guest, tdx_get_hob_entry(tdx_guest)); + + for_each_tdx_fw_entry(tdvf, entry) { + struct kvm_tdx_init_mem_region region; + uint32_t flags; + + region = (struct kvm_tdx_init_mem_region) { + .source_addr = (uint64_t)entry->mem_ptr, + .gpa = entry->address, + .nr_pages = entry->size >> 12, + }; + + flags = entry->attributes & TDVF_SECTION_ATTRIBUTES_MR_EXTEND ? + KVM_TDX_MEASURE_MEMORY_REGION : 0; + + do { + r = tdx_vcpu_ioctl(first_cpu, KVM_TDX_INIT_MEM_REGION, flags, + ®ion); + } while (r == -EAGAIN || r == -EINTR); + if (r < 0) { + error_report("KVM_TDX_INIT_MEM_REGION failed %s", strerror(-r)); + exit(1); + } + + if (entry->type == TDVF_SECTION_TYPE_TD_HOB || + entry->type == TDVF_SECTION_TYPE_TEMP_MEM) { + qemu_ram_munmap(-1, entry->mem_ptr, entry->size); + entry->mem_ptr = NULL; + } + } + + /* + * TDVF image has been copied into private region above via + * KVM_MEMORY_MAPPING. It becomes useless. + */ + ram_block = tdx_guest->tdvf_mr->ram_block; + ram_block_discard_range(ram_block, 0, ram_block->max_length); } static Notifier tdx_machine_done_notify = { From patchwork Tue Nov 5 06:23:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862434 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1AB8D1FE109 for ; Tue, 5 Nov 2024 06:38:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788696; cv=none; b=U0Hn8nSE3opy8DKnI+MEvolyikXEy2QbGbQDhVmAjJUK4AaXQlt17SzlLycqFSkOUPNXHn+qqUcFlDUdGFn6uPf+AzgHINjrVbgZ2Zmp5zqLoomgVOaK0uGfdhurphb3Op9lzl0mAVEtndiJY792VwarK6ACUBhfq2+N75tpzaA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788696; c=relaxed/simple; bh=lKT4R+z384ZnSwuZplcbwrI7VIz8YVlbaPogqTQylkI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IbtjOlOi2pd7TW6HbvZiNhuQwv1breaugyjmstJAvHZTF70bj9cetOu8+kg7h8V1sdgobFCgXT6arVEnkowxtyrYUGlvXumaqg9yPyEQzh6BH1q+Y7R7EPZHwOQhoEZhUiZEDu85r63GmCQ9fFcWSRbVgnzDFDDtP7Bt6wirDMI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Y6OLUQ4s; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Y6OLUQ4s" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788695; x=1762324695; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=lKT4R+z384ZnSwuZplcbwrI7VIz8YVlbaPogqTQylkI=; b=Y6OLUQ4sc9CzBSTwewK7zbm7tsgL+oC0xhvI84+qWxebab0gYoQLVWbV Dms0NTqMeFTd4Fq8Qj2S8iV7u0ayo5aQHlZIDRskBlmcFhVgJya7tCG+Y DulWoAFX/MtreGNbBWtlV3zasMYPyGRONpimfjEDuR0RnFg63qar9etWG Um5kJOETultcjN50tB4i/RQJTWlaKt92YRZiKpGklDAVB4i+pvb1g2vPV s+iPUa/+KnZm5WFhF+e9ipdAAJ1HG3T+2F9oayKLwMYGTuWgF6LW7CwdU jgfJIotaCBmllsa8teETkw8ol4z0R07kWjwtuos4mvLe7GfYkhN7csgpf g==; X-CSE-ConnectionGUID: I6SX2NRvRkOGES2nHqeRhA== X-CSE-MsgGUID: LM/vXntPSqWqqWbvbS+4bA== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689590" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689590" 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:15 -0800 X-CSE-ConnectionGUID: fMPbZ0l5SYCOMqH44oNg1w== X-CSE-MsgGUID: ghpoWLs8SMOi/kGwz41ALQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989055" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:38:11 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 26/60] i386/tdx: Call KVM_TDX_INIT_VCPU to initialize TDX vcpu Date: Tue, 5 Nov 2024 01:23:34 -0500 Message-Id: <20241105062408.3533704-27-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 TDX vcpu needs to be initialized by SEAMCALL(TDH.VP.INIT) and KVM provides vcpu level IOCTL KVM_TDX_INIT_VCPU for it. KVM_TDX_INIT_VCPU needs the address of the HOB as input. Invoke it for each vcpu after HOB list is created. Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann --- target/i386/kvm/tdx.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 0a6ac62de7ff..1abca7a5be6d 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -250,6 +250,22 @@ static void tdx_init_ram_entries(void) tdx_guest->nr_ram_entries = j; } +static void tdx_post_init_vcpus(void) +{ + TdxFirmwareEntry *hob; + CPUState *cpu; + int r; + + hob = tdx_get_hob_entry(tdx_guest); + CPU_FOREACH(cpu) { + r = tdx_vcpu_ioctl(cpu, KVM_TDX_INIT_VCPU, 0, (void *)hob->address); + if (r < 0) { + error_report("KVM_TDX_INIT_VCPU failed %s", strerror(-r)); + exit(1); + } + } +} + static void tdx_finalize_vm(Notifier *notifier, void *unused) { TdxFirmware *tdvf = &tdx_guest->tdvf; @@ -282,6 +298,8 @@ static void tdx_finalize_vm(Notifier *notifier, void *unused) tdvf_hob_create(tdx_guest, tdx_get_hob_entry(tdx_guest)); + tdx_post_init_vcpus(); + for_each_tdx_fw_entry(tdvf, entry) { struct kvm_tdx_init_mem_region region; uint32_t flags; From patchwork Tue Nov 5 06:23:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862435 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 28A8C1FCF71 for ; Tue, 5 Nov 2024 06:38:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788701; cv=none; b=gb2bakMNBxCxVn94xhMyJP/RJuFii7pdRWXETt+LqGzA8VtN2V1qRrCIBH9jD7B3sZJsmftpHo/OdnRbfXHiFFA9lOriBZq40nlQOMnZacdFz0ahZJuSxo8M13a2U6z/7TxLA9+QTGdKQkWOpJNtrPNtxd3UAfeDPbcOpvrmBgI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788701; c=relaxed/simple; bh=CqV8pOHF0zado4AcM+LoYPzs2/IDXl5pQDk/iq626Nc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OvGcFmLNN/10GxIsL9y7XS3qsICNWXCGvbclS98HhYVXsu83TsVcoES5GUbQitRwLw1Jlserog+n9xwbWLsvYk1ho6j+jS157Fe0i5uKa6gqwHxhCEchbuWMSWQEtRvIV9o7RFZHfMbPmLmCZygG/TZVI9Lpn9+sIrc4PxAupgA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=K3jYleZz; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="K3jYleZz" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788699; x=1762324699; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=CqV8pOHF0zado4AcM+LoYPzs2/IDXl5pQDk/iq626Nc=; b=K3jYleZzqSmRSHg3O02l38jJEZP0NdRPJJM9Rrhflk1qOxQX3mqWENLe O14TwPkO5bwU3zv5nx3cdFXO12zQzkLE31Ncm+QXaMAGe+xOzPJe0fdVW HbHRPKXzdeUGqPyot9mCmvMpahcKssYU33saVRddf0BA1U+qpdKourKyJ gzYluotQXnZQydw6UJLXqjEqc4eR+cnCGZZQfwPepdPro1wcgWgCNCazY DfToTV4HQLHEt9F4JPDvk7Cg+qXB0RQFL+OAydvlO7KVRJg5SkEviW+XH uFBk6MlutZCbLzSfxgu8ugUP+tWUO1NNm1r6xKNSsUjFyeAP63i63kOSB Q==; X-CSE-ConnectionGUID: j1eTEZC/Q16t4D83RtYxpg== X-CSE-MsgGUID: YKld6ZtdQpmrbWgLyFCqqg== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689604" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689604" 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:19 -0800 X-CSE-ConnectionGUID: Kz4s4N8uRXmCfX4vxeZZNw== X-CSE-MsgGUID: gzLPe+K2QjucAkeJFr6zoQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989076" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:38:15 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 27/60] i386/tdx: Finalize TDX VM Date: Tue, 5 Nov 2024 01:23:35 -0500 Message-Id: <20241105062408.3533704-28-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Invoke KVM_TDX_FINALIZE_VM to finalize the TD's measurement and make the TD vCPUs runnable once machine initialization is complete. Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann --- target/i386/kvm/tdx.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 1abca7a5be6d..33d7ed039051 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -335,6 +335,13 @@ static void tdx_finalize_vm(Notifier *notifier, void *unused) */ ram_block = tdx_guest->tdvf_mr->ram_block; ram_block_discard_range(ram_block, 0, ram_block->max_length); + + r = tdx_vm_ioctl(KVM_TDX_FINALIZE_VM, 0, NULL); + if (r < 0) { + error_report("KVM_TDX_FINALIZE_VM failed %s", strerror(-r)); + exit(0); + } + CONFIDENTIAL_GUEST_SUPPORT(tdx_guest)->ready = true; } static Notifier tdx_machine_done_notify = { From patchwork Tue Nov 5 06:23:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862436 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4874F1FCF71 for ; Tue, 5 Nov 2024 06:38:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788704; cv=none; b=ufm95Thv/7YxhyM5p1MRGvFEUyc3PjCNLO8x91o81eacJZ+u4gcTXh3YVGkThFPdF+hMpf23h4yU3nPTYHUcLn1VnEiKosP5tBDvS6qunVYaNAFkGyRCXgLyWzV/N3VOvIKBOsmv0x2gPEnPjjXd6A1yh6Gn/TJzctGF1zYr1gQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788704; c=relaxed/simple; bh=yTZNX+NQ5Q3yptqAX7/efQmRoNrRURBtZaK/2Teq53U=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HnFDuxrmLf9EDgbV57t+SMuJTzpkDXViHYkXfDuCWnrha0lHUATebsE+qxmf8CybtvHnUh9aaIXJhdoHsQ5CjKbBuwI/WGTKlWZr3nbtVkvicPXKML0lp/0UYvh/xWFrZmTNifVeuoUKw/zoPqp74JPSl8pKweYssKSE0fEsl6M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=eoplg+BF; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="eoplg+BF" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788703; x=1762324703; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yTZNX+NQ5Q3yptqAX7/efQmRoNrRURBtZaK/2Teq53U=; b=eoplg+BFCHEirWZ7zobYMtfhqY+p9DygR9zjl6afpUBvhEw/mdKP3sBs s461kOqSIBXJl2v7JUrtUMxBvN4FFD3npW3xEJHKfsVc2X2feFkWpfj1Z KgSFuipoZO9TvtRwHg40AhHBtKASpYEwxLn3IiqHv7iBx3xqL3LUyXCzQ P0rOLCtdObc+niQCgAqlvWw8eajdSYjIht679WsXFnvzytzew5S8uP5zP UYZiB2pXUPW9+5Ovy7Z9Qr8kVmDWXQZ2OlzC5POctvmAIZpJwyP4lIm9g OoT2+iFUsJoGl0gEDTmrD6e4OUXU9Sh0GoUgOpGwzg/mwv0kynscBgJx1 Q==; X-CSE-ConnectionGUID: bn/kwFCgQ324J3AqbK9RDA== X-CSE-MsgGUID: TQ2SibEPQ3SbyhjcquxQeQ== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689615" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689615" 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:23 -0800 X-CSE-ConnectionGUID: BJdYU1grQeKHRWNJj1QRwA== X-CSE-MsgGUID: M0YwmNanRkGKH2IyrQ+pEw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989093" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:38:19 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 28/60] i386/tdx: Enable user exit on KVM_HC_MAP_GPA_RANGE Date: Tue, 5 Nov 2024 01:23:36 -0500 Message-Id: <20241105062408.3533704-29-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 KVM translates TDG.VP.VMCALL to KVM_HC_MAP_GPA_RANGE, and QEMU needs to enable user exit on KVM_HC_MAP_GPA_RANGE in order to handle the memory conversion requested by TD guest. Signed-off-by: Xiaoyao Li --- changes in v6: - new patch; --- target/i386/kvm/tdx.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 33d7ed039051..b34707e93f4d 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -20,6 +20,8 @@ #include "sysemu/sysemu.h" #include "exec/ramblock.h" +#include + #include "hw/i386/e820_memory_layout.h" #include "hw/i386/x86.h" #include "hw/i386/tdvf.h" @@ -362,6 +364,11 @@ static int tdx_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) } } + /* TDX relies on KVM_HC_MAP_GPA_RANGE to handle TDG.VP.VMCALL */ + if (!kvm_enable_hypercall(BIT_ULL(KVM_HC_MAP_GPA_RANGE))) { + return -EOPNOTSUPP; + } + qemu_add_machine_init_done_notifier(&tdx_machine_done_notify); tdx_guest = tdx; From patchwork Tue Nov 5 06:23:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862437 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 89E3B1FCF71 for ; Tue, 5 Nov 2024 06:38:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788709; cv=none; b=ljQAH8WAVRhyY/+MhjCG6PXC/ThNjjJ7o5AGHYMWVVqzBQOzB010hM4EnFe/0awZBqZOiN7HJr9FhB9rgQ+S5DjB+21dvfCjKS4cOE6XkJepPxTE2jgYemSTxwyXGPQvL3JyAUS8J/EyEz1hDHGJLnl2TLtKVJGeXAn1EXCNJxU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788709; c=relaxed/simple; bh=hTO3AoGf1nt//dmP+tTmzwZEz/P9eM+x0h2usNJWysY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sgT86NbS0nr42ROMvofwOWWAQQUKNCB0dc2JKapUIAd/UO20F78FYyfNkqBsnm9IludYByhhXpKVqwUvh1naDfrA7NyioQLNPJHJYxMSh2JIPHYWGp/86EbHlgzU7hWArtY8giT78czq1HeZnRIO7y7qV3EC1K9YG4Hbo+Kc1Bc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=IaC2yiIM; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="IaC2yiIM" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788708; x=1762324708; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=hTO3AoGf1nt//dmP+tTmzwZEz/P9eM+x0h2usNJWysY=; b=IaC2yiIM4D/To+hIxfY8Pj3bCHo0xgMdJ+9KAsph4m4O3/DWaF9xdlMS QBAbi73OBY04FubFH0tkV5a0CDXN0K4fleRj3FUvmjPKnII2Ym4gO265x 7xMq1avRn2rFkxpg2otN52A85tqwiJLtSdZJQXZ6NNMgMtvN8VxXtE02t Ek/hZO2t0HCrBb6qdreKCSN6tJVjdLHlCfiW/lP9MRRTdO/60+p2uRZPg rmUxtKInJKd1eMb5KuKYJ3T7q5MdkSMebUyZQzUO3ASiqfQyhTpPymvE3 9LJnkkPXKKjoEx94FNn480Re6fBm5xrf1lLNaRVtKru9LwlL0KrDPq4j9 w==; X-CSE-ConnectionGUID: VjMS+1XxS22psOaqXA6I9A== X-CSE-MsgGUID: QvCtgYofRtaQYvXgNDWnZQ== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689636" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689636" 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:27 -0800 X-CSE-ConnectionGUID: 6+S6bBOERHqqKH31mEtJoA== X-CSE-MsgGUID: 0k0dK/97R56/U3RTPqjZig== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989108" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:38:23 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 29/60] i386/tdx: Handle KVM_SYSTEM_EVENT_TDX_FATAL Date: Tue, 5 Nov 2024 01:23:37 -0500 Message-Id: <20241105062408.3533704-30-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 TD guest can use TDG.VP.VMCALL to request termination. KVM translates such request into KVM_EXIT_SYSTEM_EVENT with type of KVM_SYSTEM_EVENT_TDX_FATAL. Add hanlder for such exit. Parse and print the error message, and terminate the TD guest in the handler. Signed-off-by: Xiaoyao Li --- Changes in v6: - replace the patch " i386/tdx: Handle TDG.VP.VMCALL" in v5; --- target/i386/kvm/kvm.c | 10 ++++++++++ target/i386/kvm/tdx-stub.c | 5 +++++ target/i386/kvm/tdx.c | 24 ++++++++++++++++++++++++ target/i386/kvm/tdx.h | 2 ++ 4 files changed, 41 insertions(+) diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index 4fafc003e9a7..dea0f83370d5 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -6116,6 +6116,16 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) case KVM_EXIT_HYPERCALL: ret = kvm_handle_hypercall(run); break; + case KVM_EXIT_SYSTEM_EVENT: + switch (run->system_event.type) { + case KVM_SYSTEM_EVENT_TDX_FATAL: + ret = tdx_handle_report_fatal_error(cpu, run); + break; + default: + ret = -1; + break; + } + break; default: fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason); ret = -1; diff --git a/target/i386/kvm/tdx-stub.c b/target/i386/kvm/tdx-stub.c index a064d583d393..b5cb6d56c46a 100644 --- a/target/i386/kvm/tdx-stub.c +++ b/target/i386/kvm/tdx-stub.c @@ -11,3 +11,8 @@ int tdx_parse_tdvf(void *flash_ptr, int size) { return -EINVAL; } + +int tdx_handle_report_fatal_error(X86CPU *cpu, struct kvm_run *run) +{ + return -EINVAL; +} diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index b34707e93f4d..3f44dfbf6585 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -574,6 +574,30 @@ int tdx_parse_tdvf(void *flash_ptr, int size) return tdvf_parse_metadata(&tdx_guest->tdvf, flash_ptr, size); } +int tdx_handle_report_fatal_error(X86CPU *cpu, struct kvm_run *run) +{ + uint64_t error_code = run->system_event.data[0]; + char *message = NULL; + + if (error_code & 0xffff) { + error_report("TDX: REPORT_FATAL_ERROR: invalid error code: 0x%lx", + error_code); + return -1; + } + + /* It has optional message */ + if (run->system_event.data[2]) { +#define TDX_FATAL_MESSAGE_MAX 64 + message = g_malloc0(TDX_FATAL_MESSAGE_MAX + 1); + + memcpy(message, &run->system_event.data[2], TDX_FATAL_MESSAGE_MAX); + message[TDX_FATAL_MESSAGE_MAX] = '\0'; + } + + error_report("TD guest reports fatal error. %s", message ? : ""); + return -1; +} + static bool tdx_guest_get_sept_ve_disable(Object *obj, Error **errp) { TdxGuest *tdx = TDX_GUEST(obj); diff --git a/target/i386/kvm/tdx.h b/target/i386/kvm/tdx.h index c669e0d0daca..7222a5d31359 100644 --- a/target/i386/kvm/tdx.h +++ b/target/i386/kvm/tdx.h @@ -6,6 +6,7 @@ #endif #include "confidential-guest.h" +#include "cpu.h" #include "hw/i386/tdvf.h" #define TYPE_TDX_GUEST "tdx-guest" @@ -57,5 +58,6 @@ bool is_tdx_vm(void); int tdx_pre_create_vcpu(CPUState *cpu, Error **errp); void tdx_set_tdvf_region(MemoryRegion *tdvf_mr); int tdx_parse_tdvf(void *flash_ptr, int size); +int tdx_handle_report_fatal_error(X86CPU *cpu, struct kvm_run *run); #endif /* QEMU_I386_TDX_H */ From patchwork Tue Nov 5 06:23:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862438 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F2D701FCF71 for ; Tue, 5 Nov 2024 06:38:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788713; cv=none; b=MUBBd+4y6esLxGcluE576Ll/iWboMNZoBJ/DKZ7xcUszX+PyIkEGA2ttWYBTEkUr5VILKj5jX5OAYIJP4zHdYkshv559BF8NkbR6sanPzxb9RiQ8W5vbR9WyRSsemKNDLg7BrltEdiP+YRUimBu4nDu1aVFwmqj2kG0Yz+WXGr0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788713; c=relaxed/simple; bh=6uInE5yDbMnbDX6d9fyJjDXgIoMCCobDyV6ItNz4Zao=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=DqSWSKiJYjNPHtbZcidQvQRzlMqqLYlR6ZO21nj5tY3u1oVdJnA8hpDg6HfQqnlX04DrVBi0d+hrMStlezcUwdRB/+GuXydM+FfUDGdGVPudWSE/SfOofaUoX+K61ul8dQ5XqTsw4UxW+VU9QHPgtJAs6AAswDAo6PVRlxj95Wk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=VOijBmT8; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="VOijBmT8" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788712; x=1762324712; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=6uInE5yDbMnbDX6d9fyJjDXgIoMCCobDyV6ItNz4Zao=; b=VOijBmT8oPvx6qy6cUkzkWH+tgqHAITsDz02Bywq5yo78ntHd1qKbuw3 gleGIqKzuiAw1OvHNN+qzsmcakbjl1NRijxgjzirE5+qD3320BgE/7lhr VTe4g75KeZHy2428YMhb4VfBdK9yScbXr1CHf9wvCzk8h9SLoLlDAw076 YGZWV4uqyiHE5JebwUIO69CrPhbn4M5G9AxLDKCHrv81dhf5oHaAV9D4s 8HUWuw+nVG79od92ybH6bTPLyBZZ4tbgtEqMIdbydzyWwnymvebdw91La AKK3+KzfhK9BcfmbvLCgr31ccFH5deaziIrGmgO+PP6kfhXT0tcjKwSRC g==; X-CSE-ConnectionGUID: m5WPYOSLTva/+U7++CqUuA== X-CSE-MsgGUID: Od9OBX7rTum9X/24NiCadw== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689654" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689654" 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:32 -0800 X-CSE-ConnectionGUID: svLfrtNRRe2nzq6dLY2BYA== X-CSE-MsgGUID: iUPoEBAiSDGKObyzPN4S6A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989128" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:38:27 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 30/60] i386/tdx: Wire TDX_REPORT_FATAL_ERROR with GuestPanic facility Date: Tue, 5 Nov 2024 01:23:38 -0500 Message-Id: <20241105062408.3533704-31-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Integrate TDX's TDX_REPORT_FATAL_ERROR into QEMU GuestPanic facility Originated-from: Isaku Yamahata Signed-off-by: Xiaoyao Li --- Changes in v6: - change error_code of GuestPanicInformationTdx from uint64_t to uint32_t, to only contains the bit 31:0 returned in r12. Changes in v5: - mention additional error information in gpa when it presents; - refine the documentation; (Markus) Changes in v4: - refine the documentation; (Markus) Changes in v3: - Add docmentation of new type and struct; (Daniel) - refine the error message handling; (Daniel) --- qapi/run-state.json | 31 +++++++++++++++++++++-- system/runstate.c | 58 +++++++++++++++++++++++++++++++++++++++++++ target/i386/kvm/tdx.c | 24 +++++++++++++++++- 3 files changed, 110 insertions(+), 3 deletions(-) diff --git a/qapi/run-state.json b/qapi/run-state.json index ce95cfa46b73..c5b0b747b30d 100644 --- a/qapi/run-state.json +++ b/qapi/run-state.json @@ -501,10 +501,12 @@ # # @s390: s390 guest panic information type (Since: 2.12) # +# @tdx: tdx guest panic information type (Since: 9.0) +# # Since: 2.9 ## { 'enum': 'GuestPanicInformationType', - 'data': [ 'hyper-v', 's390' ] } + 'data': [ 'hyper-v', 's390', 'tdx' ] } ## # @GuestPanicInformation: @@ -519,7 +521,8 @@ 'base': {'type': 'GuestPanicInformationType'}, 'discriminator': 'type', 'data': {'hyper-v': 'GuestPanicInformationHyperV', - 's390': 'GuestPanicInformationS390'}} + 's390': 'GuestPanicInformationS390', + 'tdx' : 'GuestPanicInformationTdx'}} ## # @GuestPanicInformationHyperV: @@ -598,6 +601,30 @@ 'psw-addr': 'uint64', 'reason': 'S390CrashReason'}} +## +# @GuestPanicInformationTdx: +# +# TDX Guest panic information specific to TDX, as specified in the +# "Guest-Hypervisor Communication Interface (GHCI) Specification", +# section TDG.VP.VMCALL. +# +# @error-code: TD-specific error code +# +# @message: Human-readable error message provided by the guest. Not +# to be trusted. +# +# @gpa: guest-physical address of a page that contains more verbose +# error information, as zero-terminated string. Present when the +# "GPA valid" bit (bit 63) is set in @error-code. +# +# +# Since: 9.0 +## +{'struct': 'GuestPanicInformationTdx', + 'data': {'error-code': 'uint32', + 'message': 'str', + '*gpa': 'uint64'}} + ## # @MEMORY_FAILURE: # diff --git a/system/runstate.c b/system/runstate.c index c2c9afa905a6..9bb8162eb28f 100644 --- a/system/runstate.c +++ b/system/runstate.c @@ -565,6 +565,52 @@ static void qemu_system_wakeup(void) } } +static char *tdx_parse_panic_message(char *message) +{ + bool printable = false; + char *buf = NULL; + int len = 0, i; + + /* + * Although message is defined as a json string, we shouldn't + * unconditionally treat it as is because the guest generated it and + * it's not necessarily trustable. + */ + if (message) { + /* The caller guarantees the NUL-terminated string. */ + len = strlen(message); + + printable = len > 0; + for (i = 0; i < len; i++) { + if (!(0x20 <= message[i] && message[i] <= 0x7e)) { + printable = false; + break; + } + } + } + + if (!printable && len) { + /* 3 = length of "%02x " */ + buf = g_malloc(len * 3); + for (i = 0; i < len; i++) { + if (message[i] == '\0') { + break; + } else { + sprintf(buf + 3 * i, "%02x ", message[i]); + } + } + if (i > 0) + /* replace the last ' '(space) to NUL */ + buf[i * 3 - 1] = '\0'; + else + buf[0] = '\0'; + + return buf; + } + + return message; +} + void qemu_system_guest_panicked(GuestPanicInformation *info) { qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed"); @@ -606,7 +652,19 @@ void qemu_system_guest_panicked(GuestPanicInformation *info) S390CrashReason_str(info->u.s390.reason), info->u.s390.psw_mask, info->u.s390.psw_addr); + } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_TDX) { + qemu_log_mask(LOG_GUEST_ERROR, + "\nTDX guest reports fatal error:" + " error code: 0x%" PRIx32 " error message:\"%s\"\n", + info->u.tdx.error_code, + tdx_parse_panic_message(info->u.tdx.message)); + if (info->u.tdx.gpa != -1ull) { + qemu_log_mask(LOG_GUEST_ERROR, "Additional error information " + "can be found at gpa page: 0x%" PRIx64 "\n", + info->u.tdx.gpa); + } } + qapi_free_GuestPanicInformation(info); } } diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 3f44dfbf6585..394f1d75dc0d 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -17,6 +17,7 @@ #include "qemu/mmap-alloc.h" #include "qapi/error.h" #include "qom/object_interfaces.h" +#include "sysemu/runstate.h" #include "sysemu/sysemu.h" #include "exec/ramblock.h" @@ -574,10 +575,25 @@ int tdx_parse_tdvf(void *flash_ptr, int size) return tdvf_parse_metadata(&tdx_guest->tdvf, flash_ptr, size); } +static void tdx_panicked_on_fatal_error(X86CPU *cpu, uint64_t error_code, + char *message, uint64_t gpa) +{ + GuestPanicInformation *panic_info; + + panic_info = g_new0(GuestPanicInformation, 1); + panic_info->type = GUEST_PANIC_INFORMATION_TYPE_TDX; + panic_info->u.tdx.error_code = (uint32_t) error_code; + panic_info->u.tdx.message = message; + panic_info->u.tdx.gpa = gpa; + + qemu_system_guest_panicked(panic_info); +} + int tdx_handle_report_fatal_error(X86CPU *cpu, struct kvm_run *run) { uint64_t error_code = run->system_event.data[0]; char *message = NULL; + uint64_t gpa = -1ull; if (error_code & 0xffff) { error_report("TDX: REPORT_FATAL_ERROR: invalid error code: 0x%lx", @@ -594,7 +610,13 @@ int tdx_handle_report_fatal_error(X86CPU *cpu, struct kvm_run *run) message[TDX_FATAL_MESSAGE_MAX] = '\0'; } - error_report("TD guest reports fatal error. %s", message ? : ""); +#define TDX_REPORT_FATAL_ERROR_GPA_VALID BIT_ULL(63) + if (error_code & TDX_REPORT_FATAL_ERROR_GPA_VALID) { + gpa = run->system_event.data[1]; + } + + tdx_panicked_on_fatal_error(cpu, error_code, message, gpa); + return -1; } From patchwork Tue Nov 5 06:23:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862439 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1015C1FDFAF for ; Tue, 5 Nov 2024 06:38:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788717; cv=none; b=jBQfjAcFF1Bs7xbOroTyW56spWvPmlPnx7KfKMrVJMppe+R2dYbLkKw/KkYhSqTuXL0BNujFExEhMAGlNt5uM8a81skaYuX0TIFx4QBL04cKi2UIYI1gZkQ7PLB6KMMbrc7K8MHbvKItOLKf38CZI+sof8VRFWKoo56EnTDGIqE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788717; c=relaxed/simple; bh=PjQf7e/17C3WWdlE56nodz232TMn447UvRVBPAlFKX4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LAJgXCtdyQZM7uIyAs3ZDKwOLQlt7+QSSaLLrcr73uyTklBe51xb+XqfpWPmkObndGTufG9YQ7AKjClISs2S6CV+D4t824K9TdAlhKxlaYSW1DttnDKo6rEIoO0t7Cx6anwfOz96AS+PtOeRHJBvxVlf3agd/120TG/10zZmTPY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=DouHB9CI; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="DouHB9CI" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788716; x=1762324716; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=PjQf7e/17C3WWdlE56nodz232TMn447UvRVBPAlFKX4=; b=DouHB9CIvGcUI3sBUK1qmXkLwCW0dBgG50wNUiFePEJLbSzedCCAWzRx EIqreKe8vvAzkxcCmJkohm428t1+1GISF7xN9ZAjVp8ad+K7EiLwM7b2p h/g1bcJIs1f2/TdAHiaEZlW3X9Gf5Ohn1lQKlAxqEJ5xI+/FoyJByHIJR CtgUxY+jWVE8gReGDVqqHSOMNF5IFJnKs6wjqnAguhvqlI6Vj1ccid5g1 Y6ggGHxZhOHVtVgweyCzLrxi3UDYrFJumFx54T49cRhybi7vSKyXYAGaG 4dgat3Fz2N34i6AKNRgqNDtlaOOfcFitPgYyx560+ViCaFJM/9nMSN5ZF A==; X-CSE-ConnectionGUID: hWZrcXj7S4eexpEFUf6jXQ== X-CSE-MsgGUID: Nt1RhzJ8SgKRi0K8PGERBg== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689669" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689669" 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:36 -0800 X-CSE-ConnectionGUID: HBu2ooFlQVmCgAlxAclJfw== X-CSE-MsgGUID: YoO5oAvySVuB4CPn/mfq7w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989162" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:38:32 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 31/60] i386/cpu: introduce x86_confidential_guest_cpu_instance_init() Date: Tue, 5 Nov 2024 01:23:39 -0500 Message-Id: <20241105062408.3533704-32-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To allow execute confidential guest specific cpu init operations. Signed-off-by: Xiaoyao Li --- Changes in v6: - new patch; --- target/i386/confidential-guest.h | 11 +++++++++++ target/i386/cpu.c | 10 ++++++++++ 2 files changed, 21 insertions(+) diff --git a/target/i386/confidential-guest.h b/target/i386/confidential-guest.h index 7342d2843aa5..38169ed68e06 100644 --- a/target/i386/confidential-guest.h +++ b/target/i386/confidential-guest.h @@ -39,6 +39,7 @@ struct X86ConfidentialGuestClass { /* */ int (*kvm_type)(X86ConfidentialGuest *cg); + void (*cpu_instance_init)(X86ConfidentialGuest *cg, CPUState *cpu); uint32_t (*mask_cpuid_features)(X86ConfidentialGuest *cg, uint32_t feature, uint32_t index, int reg, uint32_t value); }; @@ -59,6 +60,16 @@ static inline int x86_confidential_guest_kvm_type(X86ConfidentialGuest *cg) } } +static inline void x86_confidential_guest_cpu_instance_init(X86ConfidentialGuest *cg, + CPUState *cpu) +{ + X86ConfidentialGuestClass *klass = X86_CONFIDENTIAL_GUEST_GET_CLASS(cg); + + if (klass->cpu_instance_init) { + klass->cpu_instance_init(cg, cpu); + } +} + /** * x86_confidential_guest_mask_cpuid_features: * diff --git a/target/i386/cpu.c b/target/i386/cpu.c index 3baa95481fbc..c7d65bbeab9b 100644 --- a/target/i386/cpu.c +++ b/target/i386/cpu.c @@ -35,6 +35,7 @@ #include "hw/qdev-properties.h" #include "hw/i386/topology.h" #ifndef CONFIG_USER_ONLY +#include "confidential-guest.h" #include "sysemu/reset.h" #include "qapi/qapi-commands-machine-target.h" #include "exec/address-spaces.h" @@ -8157,6 +8158,15 @@ static void x86_cpu_post_initfn(Object *obj) } accel_cpu_instance_init(CPU(obj)); + +#ifndef CONFIG_USER_ONLY + MachineState *ms = MACHINE(object_dynamic_cast(qdev_get_machine(), + TYPE_MACHINE)); + if (ms && ms->cgs) { + x86_confidential_guest_cpu_instance_init(X86_CONFIDENTIAL_GUEST(ms->cgs), + (CPU(obj))); + } +#endif } static void x86_cpu_init_default_topo(X86CPU *cpu) From patchwork Tue Nov 5 06:23:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862440 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 26E161FDFAD for ; Tue, 5 Nov 2024 06:38:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788721; cv=none; b=oQe50v2u5ZAv3ec8rLz4aZ4q3VPaKvQgj5M8Nk1Jos1naxDC0jlBOW6GGJVmjHB6n5l3kNAr5Fj6X0e1wt8tPzNVsTZc4Yrr4aeuffJRXb5XoQEW03U6tLpkf90yw5KVrUPcwumhVgcucA0UU0O50iW3X72zjBm6vnfAMviNVeY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788721; c=relaxed/simple; bh=IFyldM/wkLLjkzgvXirqmq3JCBeSDG+HIbY0Aejj2GY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oXG9cL1iTXxTR3ax48HpADRqGO0n52OmqRI0lsrj7hBkaQRZF23oOl0uZXBe7+33M7Pb4Y0XZ14FVPjZ9K3B5696eslKUcw703+wQrl9VmlQ3KDLpHtDGjvRAa5IRMBxk0ntofm2GsdAvHOPA+GVgW7eWYqXgr/Apro+7K0JnZ0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=X1xE4RW3; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="X1xE4RW3" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788720; x=1762324720; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=IFyldM/wkLLjkzgvXirqmq3JCBeSDG+HIbY0Aejj2GY=; b=X1xE4RW3BYRmw9ONODniHEFnPcXaokxnV2pKBZk4eziWkbB9I7JRAJi6 ICjVoKr94sa2K6ovacFfqpJCIoErp8En7+A1q2AYGAdRl3z/q3/f6ZV7F psVT5JNzp+GKRZlgobwuMCEe8QSyS3VwyJx5pJDg0eTjsNcfnw71h4CBm hXyoI8gTOGOx9uwKOgpEbFFqUYMD526aodkb5/9gjTe+nVnYKbXPibX9D vbFOF4RxGwO7UuPgEY5bq9j4QoL24JWS9tcvPczlcbitufRiMJyhCu+9J uJ0d0+Rlh4mLCsVWvmBlhRcb4Dble3I/lw/wQ5dBigWjA1eHaM/bHWtpj g==; X-CSE-ConnectionGUID: x7B367ezT0efSLnn51GDnw== X-CSE-MsgGUID: 59eTzcWrS6GFw4E23DPbvg== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689682" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689682" 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:40 -0800 X-CSE-ConnectionGUID: iuT24egDTnOHJU8nk9k9hQ== X-CSE-MsgGUID: 35Pm2IzXRcuowvndLGKt2A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989208" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:38:36 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 32/60] i386/tdx: implement tdx_cpu_instance_init() Date: Tue, 5 Nov 2024 01:23:40 -0500 Message-Id: <20241105062408.3533704-33-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Currently, pmu is not supported for TDX by KVM. Signed-off-by: Xiaoyao Li --- chanegs in v6: - new patch; --- target/i386/kvm/tdx.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 394f1d75dc0d..61fb1f184149 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -384,6 +384,11 @@ static int tdx_kvm_type(X86ConfidentialGuest *cg) return KVM_X86_TDX_VM; } +static void tdx_cpu_instance_init(X86ConfidentialGuest *cg, CPUState *cpu) +{ + object_property_set_bool(OBJECT(cpu), "pmu", false, &error_abort); +} + static int tdx_validate_attributes(TdxGuest *tdx, Error **errp) { if ((tdx->attributes & ~tdx_caps->supported_attrs)) { @@ -727,4 +732,5 @@ static void tdx_guest_class_init(ObjectClass *oc, void *data) klass->kvm_init = tdx_kvm_init; x86_klass->kvm_type = tdx_kvm_type; + x86_klass->cpu_instance_init = tdx_cpu_instance_init; } From patchwork Tue Nov 5 06:23:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862441 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D217E1FA25C for ; Tue, 5 Nov 2024 06:38:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788726; cv=none; b=BBOCCk9DZ3z17QFHtakSctrnuugLo+4zW1wU67f5prtD8ynTSkWAdkVWlwZUKqe8gwzGclLxHCsHYZZheJ9zo/F8VVx3/VCFyUkc0BuMQGvkPyyC96CU/7urpBo+kkb14s1g0IecQq3uNw55BzXt5/s8n+GbalIqksEEwFP6TDg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788726; c=relaxed/simple; bh=63P5g04tq2r6PcAIPsWb8iPHQuE0MH+gEuTLFzYddZI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Wle3t6NOivKzNiJ7WRnRH2AavMdIkEr9jqUOXR5vOgHeIrGMNGBf4PtkkGIRoYYoHRIvhYs5yWoTuODLpSRMo8uRe1DtW5675BxkTQ4UqhlLS8kVkcp8B2QD5CgEBiijjvr7QD0RwBvtlW/1PWqZhCOqR3nlyzNctUDcuPhcsGo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=PwP8qhxB; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="PwP8qhxB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788725; x=1762324725; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=63P5g04tq2r6PcAIPsWb8iPHQuE0MH+gEuTLFzYddZI=; b=PwP8qhxB7ZCiuq1qGruUdI3EPQ5VZ704WF+BWgbwhA+bDy3PdKDD6CGf K1hGtekkeEHgT6AK0cQOu/OxfUqjTBTbPj+crw6WI9RSrwfRoTNEOTNht xV7olNevQcSeTgARIiqCApgU0MiswUnBNCPE71d9FLF9xmkKOf1yI/Ane CLC0A41kTVX3zhIuXPkoBDqGmBsZ6O1/1zHFgKLu7eccWqlGARDpXpOMD +Iqm4ZEEpe2tQh0kavR+psufRIHr9We3wzD9Lxt7tJqB16TztyAgq/N7S 5+4L51Sh6PwSzGBs/AreibbkN8qI5aSCDyKv/r/ziWBmubP5qNQGlL9rl Q==; X-CSE-ConnectionGUID: 0I5egfcKSKSkDNNswzwblg== X-CSE-MsgGUID: RaxrW4OZTRKPjvPqKllOpg== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689691" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689691" 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:45 -0800 X-CSE-ConnectionGUID: MdCsSwMrQqKD4Co1gmDzvA== X-CSE-MsgGUID: KOGn8PVPRDGFMXhDPQsGvQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989240" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:38:40 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 33/60] i386/cpu: introduce x86_confidenetial_guest_cpu_realizefn() Date: Tue, 5 Nov 2024 01:23:41 -0500 Message-Id: <20241105062408.3533704-34-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To execute confidential guest specific cpu realize operations. Signed-off-by: Xiaoyao Li --- changes in v6: - new patch; --- target/i386/confidential-guest.h | 12 ++++++++++++ target/i386/cpu.c | 13 ++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/target/i386/confidential-guest.h b/target/i386/confidential-guest.h index 38169ed68e06..4b7ea91023dc 100644 --- a/target/i386/confidential-guest.h +++ b/target/i386/confidential-guest.h @@ -40,6 +40,7 @@ struct X86ConfidentialGuestClass { /* */ int (*kvm_type)(X86ConfidentialGuest *cg); void (*cpu_instance_init)(X86ConfidentialGuest *cg, CPUState *cpu); + void (*cpu_realizefn)(X86ConfidentialGuest *cg, CPUState *cpu, Error **errp); uint32_t (*mask_cpuid_features)(X86ConfidentialGuest *cg, uint32_t feature, uint32_t index, int reg, uint32_t value); }; @@ -70,6 +71,17 @@ static inline void x86_confidential_guest_cpu_instance_init(X86ConfidentialGuest } } +static inline void x86_confidenetial_guest_cpu_realizefn(X86ConfidentialGuest *cg, + CPUState *cpu, + Error **errp) +{ + X86ConfidentialGuestClass *klass = X86_CONFIDENTIAL_GUEST_GET_CLASS(cg); + + if (klass->cpu_realizefn) { + klass->cpu_realizefn(cg, cpu, errp); + } +} + /** * x86_confidential_guest_mask_cpuid_features: * diff --git a/target/i386/cpu.c b/target/i386/cpu.c index c7d65bbeab9b..1ffbafef03e7 100644 --- a/target/i386/cpu.c +++ b/target/i386/cpu.c @@ -7848,6 +7848,18 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp) return; } +#ifndef CONFIG_USER_ONLY + MachineState *ms = MACHINE(qdev_get_machine()); + + if (ms->cgs) { + x86_confidenetial_guest_cpu_realizefn(X86_CONFIDENTIAL_GUEST(ms->cgs), + cs, &local_err); + if (local_err != NULL) { + goto out; + } + } +#endif + if (xcc->host_cpuid_required && !accel_uses_host_cpuid()) { g_autofree char *name = x86_cpu_class_get_model_name(xcc); error_setg(&local_err, "CPU model '%s' requires KVM or HVF", name); @@ -7972,7 +7984,6 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp) } #ifndef CONFIG_USER_ONLY - MachineState *ms = MACHINE(qdev_get_machine()); qemu_register_reset(x86_cpu_machine_reset_cb, cpu); if (cpu->env.features[FEAT_1_EDX] & CPUID_APIC || ms->smp.cpus > 1) { From patchwork Tue Nov 5 06:23:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862442 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BDD741FCC63 for ; Tue, 5 Nov 2024 06:38:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788730; cv=none; b=rUudOncCXiPWe4hDPqdKWbpfsYdc+zhv5Cv28AXsQ3JiyVKz7J2MhwhVD84mVf7SyZH13JPdobfhSOuL/rRwRXeGBSngjqjowj9lRiRbTnBjX5vaDy2b3TJd4AARzt74SIDo0QXYWqKeS3zE/CYIxw0YVIUc+FWrLpW5Hd3B+uU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788730; c=relaxed/simple; bh=nkZUZZFxnGCMa4wnPLMg+4QnO1QTfXvDpRrYii+EGk0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BRobEZ1JbzhUNWz2kLWTO4AudX2mDh81lQN78SxCS3SGLOdTkknmlH6HHX5yCVPrysh92z1r3cxCp1RXqU7PQGYXnUgPUaEApRL9oBRHqAjQkoD+AdJVrtNDvQaXADvbf4NZyaiht2eQAUww4zmdH2KB+w8+q0mx12vYd8vtOzA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=UlnwIWiW; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="UlnwIWiW" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788729; x=1762324729; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=nkZUZZFxnGCMa4wnPLMg+4QnO1QTfXvDpRrYii+EGk0=; b=UlnwIWiWx8qVUdgBbwuOlUHPhJlmbuSle0raEy4zI9Kip/yqU04AhoF3 XhqwXM4Pbg5xitPIzps+egixalq78MC0lbwW9a6NcQRNeO5YkntyznnUK mCe+5XJIhI3jl27DcI6DTf9QZa/nTzEYwKwhih+7E18yhDfqb2pw+Dkxb eb1pcLtTUYBaM1UcKTFM4nJ65wFpxY09Q9lcfgqjUxxWdjdqA1d8/fFel vHrGTanIubo/2a2a97Nj70Swr1JW21iQBwonVPsnzRVIR5kgw8nYJCw1d 67zDqZPX6sBoCLUuahofnPduVy2XkJ5C/BNSiFHrimE2Ew5PXPsAiE/YR g==; X-CSE-ConnectionGUID: B8qzzp6cTMGcJmp1SFS4mA== X-CSE-MsgGUID: PmA/w4PbQAureUh/nMuxPQ== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689702" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689702" 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:49 -0800 X-CSE-ConnectionGUID: tBqCUn3tQC6VO3jLGAIGQg== X-CSE-MsgGUID: Ion8spsGRbqXdvTLwF6aBA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989310" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:38:44 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 34/60] i386/tdx: implement tdx_cpu_realizefn() Date: Tue, 5 Nov 2024 01:23:42 -0500 Message-Id: <20241105062408.3533704-35-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 For TDX guest, KVM doesn't allow phys_bits configuration and the phys_bits can only be native/host value. Add the logic to set cpu->phys_bits to host value when user doesn't give a explicit one and error out when user desires a different one than host value. Signed-off-by: Xiaoyao Li --- Changes in v6: - new patches; --- target/i386/host-cpu.c | 2 +- target/i386/host-cpu.h | 1 + target/i386/kvm/tdx.c | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/target/i386/host-cpu.c b/target/i386/host-cpu.c index 03b9d1b169a5..e2c59e5ae288 100644 --- a/target/i386/host-cpu.c +++ b/target/i386/host-cpu.c @@ -15,7 +15,7 @@ #include "sysemu/sysemu.h" /* Note: Only safe for use on x86(-64) hosts */ -static uint32_t host_cpu_phys_bits(void) +uint32_t host_cpu_phys_bits(void) { uint32_t eax; uint32_t host_phys_bits; diff --git a/target/i386/host-cpu.h b/target/i386/host-cpu.h index 6a9bc918baa4..b97ec01c9bec 100644 --- a/target/i386/host-cpu.h +++ b/target/i386/host-cpu.h @@ -10,6 +10,7 @@ #ifndef HOST_CPU_H #define HOST_CPU_H +uint32_t host_cpu_phys_bits(void); void host_cpu_instance_init(X86CPU *cpu); void host_cpu_max_instance_init(X86CPU *cpu); bool host_cpu_realizefn(CPUState *cs, Error **errp); diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 61fb1f184149..289722a129ce 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -23,6 +23,8 @@ #include +#include "cpu.h" +#include "host-cpu.h" #include "hw/i386/e820_memory_layout.h" #include "hw/i386/x86.h" #include "hw/i386/tdvf.h" @@ -389,6 +391,20 @@ static void tdx_cpu_instance_init(X86ConfidentialGuest *cg, CPUState *cpu) object_property_set_bool(OBJECT(cpu), "pmu", false, &error_abort); } +static void tdx_cpu_realizefn(X86ConfidentialGuest *cg, CPUState *cs, + Error **errp) +{ + X86CPU *cpu = X86_CPU(cs); + uint32_t host_phys_bits = host_cpu_phys_bits(); + + if (!cpu->phys_bits) { + cpu->phys_bits = host_phys_bits; + } else if (cpu->phys_bits != host_phys_bits) { + error_setg(errp, "TDX only supports host physical bits (%u)", + host_phys_bits); + } +} + static int tdx_validate_attributes(TdxGuest *tdx, Error **errp) { if ((tdx->attributes & ~tdx_caps->supported_attrs)) { @@ -733,4 +749,5 @@ static void tdx_guest_class_init(ObjectClass *oc, void *data) klass->kvm_init = tdx_kvm_init; x86_klass->kvm_type = tdx_kvm_type; x86_klass->cpu_instance_init = tdx_cpu_instance_init; + x86_klass->cpu_realizefn = tdx_cpu_realizefn; } From patchwork Tue Nov 5 06:23:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862443 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 353DF1FCC63 for ; Tue, 5 Nov 2024 06:38:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788734; cv=none; b=nscqvnOU7D6yFCPtlmQYtqbdLZ+PdoSCqhraUIYl3d+0wKhhaOc+garnvh03U18j5lKjMAOuBd4xCb/gA7hlmi915+0OUrrXY9MfdVe97fFvwcTcRfybB7YYKmUEJFI0aqPDY31uojTpm4LS1K5iaJo9LbmO+Oe35h/bX3hNL+k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788734; c=relaxed/simple; bh=kqZuabcZCeMEwxC/J0aE7gtHFhAE9jCqlqQdO5TnUDc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=n3wvrtGtfj+wdER2ILGE80Fam84isoJ7e4BUUH4Ck1WyGXA2RpctTWPmu5g4L4JUZWshj4300UKztQVINtKmZGi75zK8zxUUqpIgtmFuuYX+8TJU1qU3wwoAnMHrKasnqRDDAMndUcYATP5Utle3sag1UPrGwfZoUWmHQNQ9SnY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=FQ492mP7; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="FQ492mP7" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788733; x=1762324733; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=kqZuabcZCeMEwxC/J0aE7gtHFhAE9jCqlqQdO5TnUDc=; b=FQ492mP7UvUXd5bN9Er4VwW9oEL1WQ/qeWwy06mb0JZK9mGSyrisBBE0 N7c4IrFF6k6b2/U2utAJWcKj3Ir+8R/leh7qNei6Ei2prdMoeqiqKPBhl U4o6Cy7F4CErt/NsxUKHKHNxr9jCzaBEVx2X89TjosiDkKSVXJUN6+1n2 r0PwZI7WjthuowQ4DT892ZKAxA2BgyiPZE4q0IShHYAIyXiWRYl6YoFnH Whq3m9MTa1tn+xiNnW3bTWpo3zhzn2Lr+TbLl71cg7zpVSVBzxdmSAD03 5wTlEEoFjfz1tMbhY1im4Q/ZtI81jY/m93QHBNyK40QnwCvtplnWXbNfe w==; X-CSE-ConnectionGUID: auUeGuYwRkqS+z4+hLUixQ== X-CSE-MsgGUID: Ob5a8J51SaiO3dcpvY9sYQ== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689711" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689711" 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:53 -0800 X-CSE-ConnectionGUID: djoKb6XrSbaa0uJJNx7iyQ== X-CSE-MsgGUID: Y6K1GjKyTai+AQIU8Wqo/Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989366" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:38:49 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 35/60] i386/cpu: Introduce enable_cpuid_0x1f to force exposing CPUID 0x1f Date: Tue, 5 Nov 2024 01:23:43 -0500 Message-Id: <20241105062408.3533704-36-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Currently, QEMU exposes CPUID 0x1f to guest only when necessary, i.e., when topology level that cannot be enumerated by leaf 0xB, e.g., die or module level, are configured for the guest, e.g., -smp xx,dies=2. However, TDX architecture forces to require CPUID 0x1f to configure CPU topology. Introduce a bool flag, enable_cpuid_0x1f, in CPU for the case that requires CPUID leaf 0x1f to be exposed to guest. Introduce a new function x86_has_cpuid_0x1f(), which is the warpper of cpu->enable_cpuid_0x1f and x86_has_extended_topo() to check if it needs to enable cpuid leaf 0x1f for the guest. Signed-off-by: Xiaoyao Li --- target/i386/cpu.c | 4 ++-- target/i386/cpu.h | 9 +++++++++ target/i386/kvm/kvm.c | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/target/i386/cpu.c b/target/i386/cpu.c index 1ffbafef03e7..119b38bcb0c1 100644 --- a/target/i386/cpu.c +++ b/target/i386/cpu.c @@ -6731,7 +6731,7 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, break; case 0x1F: /* V2 Extended Topology Enumeration Leaf */ - if (!x86_has_extended_topo(env->avail_cpu_topo)) { + if (!x86_has_cpuid_0x1f(cpu)) { *eax = *ebx = *ecx = *edx = 0; break; } @@ -7588,7 +7588,7 @@ void x86_cpu_expand_features(X86CPU *cpu, Error **errp) * cpu->vendor_cpuid_only has been unset for compatibility with older * machine types. */ - if (x86_has_extended_topo(env->avail_cpu_topo) && + if (x86_has_cpuid_0x1f(cpu) && (IS_INTEL_CPU(env) || !cpu->vendor_cpuid_only)) { x86_cpu_adjust_level(cpu, &env->cpuid_min_level, 0x1F); } diff --git a/target/i386/cpu.h b/target/i386/cpu.h index 59959b8b7a4d..dcc673262c06 100644 --- a/target/i386/cpu.h +++ b/target/i386/cpu.h @@ -2171,6 +2171,9 @@ struct ArchCPU { /* Compatibility bits for old machine types: */ bool enable_cpuid_0xb; + /* Force to enable cpuid 0x1f */ + bool enable_cpuid_0x1f; + /* Enable auto level-increase for all CPUID leaves */ bool full_cpuid_auto_level; @@ -2431,6 +2434,12 @@ void host_cpuid(uint32_t function, uint32_t count, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx); bool cpu_has_x2apic_feature(CPUX86State *env); +static inline bool x86_has_cpuid_0x1f(X86CPU *cpu) +{ + return cpu->enable_cpuid_0x1f || + x86_has_extended_topo(cpu->env.avail_cpu_topo); +} + /* helper.c */ void x86_cpu_set_a20(X86CPU *cpu, int a20_state); void cpu_sync_avx_hflag(CPUX86State *env); diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index dea0f83370d5..022809bad36e 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -1874,7 +1874,7 @@ uint32_t kvm_x86_build_cpuid(CPUX86State *env, struct kvm_cpuid_entry2 *entries, break; } case 0x1f: - if (!x86_has_extended_topo(env->avail_cpu_topo)) { + if (!x86_has_cpuid_0x1f(env_archcpu(env))) { cpuid_i--; break; } From patchwork Tue Nov 5 06:23:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862444 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5D7C81DB551 for ; Tue, 5 Nov 2024 06:38:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788738; cv=none; b=oSd5rJsZZS9u4c0qt6HnbXPWb5lHZegEWi+T2Qje4eQphGwGhfxmuLkeATeEdhl6aaErQPMHiowTDfWOE/15/yfclpCPj+ZAQ6rXjB0out0POEtfrZjj7TvvHDeXhT6z6nV0CkMU896EIPFVTtbKaMStnaWY4LmiJaPt09SERTU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788738; c=relaxed/simple; bh=dyUD9Nwe8FsbgpcTWs2XG6l6cCFEI7AD/O96ORbLEfg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=m7hRVaMKiyuIhMgCWym66j0xTojJZRpF2ttS8FSY15dJEpQDAsQ9gTfFhMvTZHr7bPYC2rcUia28dRTX2bveJngQmqWX/cuye0ZP6SRJ20VNf5DK3BqDkmlZdHY43PAWp0REBtGXUyVsKxF7YMleDnxFLYXmzsz80hRpd/6SC1E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=XeCuNuiY; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="XeCuNuiY" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788737; x=1762324737; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=dyUD9Nwe8FsbgpcTWs2XG6l6cCFEI7AD/O96ORbLEfg=; b=XeCuNuiYL5oNeXJ8cz5gJDTBIZSGYwVcniEABPVdjoT08/DE1mrEiX96 7M/OPfmS4H1n0RKMrdwp2Tt4K6CenvxI8jcHhKItiGWCDjDkH7uf3ZJKz cESBXvxjBe8rQn1BZeugnTcdeijJUo5qKz8Iug+GbeuVGRooQE3tP/xAq RokdypCCdBMup429wg8HJkRlKD/k4/AtNfIf6q2d/+EZp0eOx4FbsUCr+ YRw07ja5oGzGaevNJQn6NObrreRK8tqXaeYzl+KGaKvAPxVPtseaHaDhH zPftX7dyzQxXgSHAf69aQglmEXHHz+5Ezw2W7pXSQopavH8EOU9N6vkbx Q==; X-CSE-ConnectionGUID: xkDHaZeiQFWLdzsC46RXyA== X-CSE-MsgGUID: M0jUYzygTo+6jSkFrZR35w== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689720" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689720" 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:57 -0800 X-CSE-ConnectionGUID: nBw1klSbQS65rq8tQKhmxQ== X-CSE-MsgGUID: 0zbx6/jETtKRuCVPbc4Ycg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989408" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:38:53 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 36/60] i386/tdx: Force exposing CPUID 0x1f Date: Tue, 5 Nov 2024 01:23:44 -0500 Message-Id: <20241105062408.3533704-37-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 TDX uses CPUID 0x1f to configure TD guest's CPU topology. So set enable_cpuid_0x1f for TDs. Signed-off-by: Xiaoyao Li --- target/i386/kvm/tdx.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 289722a129ce..19ce90df4143 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -388,7 +388,11 @@ static int tdx_kvm_type(X86ConfidentialGuest *cg) static void tdx_cpu_instance_init(X86ConfidentialGuest *cg, CPUState *cpu) { + X86CPU *x86cpu = X86_CPU(cpu); + object_property_set_bool(OBJECT(cpu), "pmu", false, &error_abort); + + x86cpu->enable_cpuid_0x1f = true; } static void tdx_cpu_realizefn(X86ConfidentialGuest *cg, CPUState *cs, From patchwork Tue Nov 5 06:23:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862445 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9AAF81FDF96 for ; Tue, 5 Nov 2024 06:39:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788743; cv=none; b=XuKtjYW94IxWnM4dWyDL4A4+S7G52vZPU2edJIunoI/oRht3Wb82rG7FxFa7IRTS6zo+g/F8zmuK1uyF8oyyNsxgDVD8cJfBvDfe5Z2XUdaNZSi+zMsQrnZ/FlVrgjBbrykvEVBRvXK3ML6LM7fvQvFBlbnLPtlvvc8K7+AEIuY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788743; c=relaxed/simple; bh=mgYnaE32reSGuV7hHoP0EJIpwxMG8v5/dHOATY3+4xU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=PZ4knF2y5F2w0l6UpbMQxFpLH17LThH5fJufL94uOGL735HuhWjLWmEz9Yu+QxbQp4Jo4rawnJDbFg68iQKVFpdFvJQVkiJWzXjuUOLk/k3GsE5TSARUAnOXMyibfWatHcSpoZgImL2uD0AFb68pVYPpJ/LmDPLtQrrWKBvTzpE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=GTkkfB6i; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="GTkkfB6i" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788742; x=1762324742; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mgYnaE32reSGuV7hHoP0EJIpwxMG8v5/dHOATY3+4xU=; b=GTkkfB6ih45rGuyu/NYjCZp26wSKro2/VSu9TBniUV3fe8Mx5aBrzPOU p1UZgJietI6RHeSV/Raml9sKhn8BtXHQVf5pVtMKnKErTJc+tZ7HElBw3 DS8nITa1W1HYqJMhft2tsCNmv3Vsoi3WYRPojg/lQqj3zve8wVNIde3nl OPo5J0TJIOXQCgIreRu8Oe79NwRNK4iHDt0ogS1HcK17uEf1orRE7eqTD Adqz3/8WB9b6aBvrVfpiUWefwdvoixPevpOEXosb/Xah4aiQkaD1XbOQn JM8dNlGQGCbiqU3+7ZqP+YAwlBSM64cUIq70e6+MnZgpgbv6wKKQ/+ucq A==; X-CSE-ConnectionGUID: 0+55poALRGCuKLSQcuRtVg== X-CSE-MsgGUID: ySKmbfx9SDONc04wVycKGA== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689727" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689727" 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:39:01 -0800 X-CSE-ConnectionGUID: OxDsKalzR/SUCZQoWpLyNQ== X-CSE-MsgGUID: PurmE+u2Sx2SqBVZbB/RpQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989457" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:38:57 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 37/60] i386/tdx: Set kvm_readonly_mem_enabled to false for TDX VM Date: Tue, 5 Nov 2024 01:23:45 -0500 Message-Id: <20241105062408.3533704-38-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 TDX only supports readonly for shared memory but not for private memory. In the view of QEMU, it has no idea whether a memslot is used as shared memory of private. Thus just mark kvm_readonly_mem_enabled to false to TDX VM for simplicity. Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann --- target/i386/kvm/tdx.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 19ce90df4143..00faffa891e4 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -372,6 +372,15 @@ static int tdx_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) return -EOPNOTSUPP; } + /* + * Set kvm_readonly_mem_allowed to false, because TDX only supports readonly + * memory for shared memory but not for private memory. Besides, whether a + * memslot is private or shared is not determined by QEMU. + * + * Thus, just mark readonly memory not supported for simplicity. + */ + kvm_readonly_mem_allowed = false; + qemu_add_machine_init_done_notifier(&tdx_machine_done_notify); tdx_guest = tdx; From patchwork Tue Nov 5 06:23:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862446 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D3CE31FDF96 for ; Tue, 5 Nov 2024 06:39:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788747; cv=none; b=R//xVYhXQTVQlFuqqj0JZ/xAQKj1vW7MnVakjDsUuUKR3dcS0s9dgygSCkoHGN53DR0hrE44G6RCI2793PpH0iwlxjeq/uwp/iewQJ0+xHy93zqj2//r3igptXpUPIzUqdUbJ4tUPNCtg9Vt/a1Ql/KVI4vpBwS/AZLkW2z8NM4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788747; c=relaxed/simple; bh=oaeUYxWxoTsbbEf4Af9Nw3NbJLsiof0AXokRyvVuUyo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RmwrxFDvoyiW5OeHPQtgjCbPev2LvXOmaR3VxdCRcUKitMjwKefpsJ0W/Svx03MLE0bi6IVbC4tpx6K9kzp8Qj01OweVYN9TILQ+lfoi0tzTnlvjO9LUudgS/sKnm/LRVFxpUC7Cj3btfMiowTiEwDLXxZHQtl/nnWXbuMYQSnU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=e7bw9pGT; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="e7bw9pGT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788746; x=1762324746; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=oaeUYxWxoTsbbEf4Af9Nw3NbJLsiof0AXokRyvVuUyo=; b=e7bw9pGTQdIwqwcc9v9FZDuUPIZ0GbWdBDx2lveytNuNQR1GVZfrHpB9 zqps8ZM9pHTxZgFwY6Jcupf9/syGVd13Cqa6LmrMyFnS0OMMC47hsvKmo iulk3RlKC0D7unxuNwDr8XHv/gFqGjBLt6gv3jNpkC/UtAc4kcv11VclD hhcDuG/jhayOaJVAWUjiARsAYWU06SEQFLWlEkWTMMvqJ6pQRz7iWov4v pofpqndVlp5ubYk3NMTurpibPfbT5+fZ+BeJ6ebPBub5dG/gnxtUaNLBb NJIZYVDY6Z71mPDMFyqJVR4UPFHXuVlQX+a81Qed/PysYVazeiDbPeiwV g==; X-CSE-ConnectionGUID: kglpdMlfQpiiSm3MNyA5HA== X-CSE-MsgGUID: S6BiPMUiS02HQUd4905yEg== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689735" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689735" 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:39:06 -0800 X-CSE-ConnectionGUID: EI5xNTcOT4uw11Kv11bggg== X-CSE-MsgGUID: 2ryCymgaT3yCWbd2Jfig5A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989510" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:39:01 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 38/60] i386/tdx: Disable SMM for TDX VMs Date: Tue, 5 Nov 2024 01:23:46 -0500 Message-Id: <20241105062408.3533704-39-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 TDX doesn't support SMM and VMM cannot emulate SMM for TDX VMs because VMM cannot manipulate TDX VM's memory. Disable SMM for TDX VMs and error out if user requests to enable SMM. Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann --- target/i386/kvm/tdx.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 00faffa891e4..68d90a180db7 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -355,11 +355,20 @@ static Notifier tdx_machine_done_notify = { static int tdx_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) { + MachineState *ms = MACHINE(qdev_get_machine()); + X86MachineState *x86ms = X86_MACHINE(ms); TdxGuest *tdx = TDX_GUEST(cgs); int r = 0; kvm_mark_guest_state_protected(); + if (x86ms->smm == ON_OFF_AUTO_AUTO) { + x86ms->smm = ON_OFF_AUTO_OFF; + } else if (x86ms->smm == ON_OFF_AUTO_ON) { + error_setg(errp, "TDX VM doesn't support SMM"); + return -EINVAL; + } + if (!tdx_caps) { r = get_tdx_capabilities(errp); if (r) { From patchwork Tue Nov 5 06:23:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862447 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EEF061FDFA3 for ; Tue, 5 Nov 2024 06:39:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788751; cv=none; b=pmgKGdZLurB07F+GE03mkkxWBw/LvT6daSKZzwCPEY/ymYt0CG2wVk6XAZUpRSIUnW3D3X/KkMdMvedcZRJVOcCxvpleg25SEem69f+rA456zMBAVyAgrTXIvxx26VNIrLt0KD8btOSy4mpc0515tyDJA6HvV/bIgcWFiDrqNs0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788751; c=relaxed/simple; bh=ETTKI5+hacWoJJE2C+uh6AGfusJHWOqz7xoddYnIUoE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hQT3fyawgKAxpQYKLdTYD0I0Gbe/ARm5IDlpdERm+pc9FVknTsaCrxB/d33Amc1Z/JgpAKBgU+r89+lrmTF5kKQzuk6qjMeLl+q+Yu5ExZP8IL1OrvSx/OrAE96UVxblSu0zphivUQzLYNmJjKNnVfY5Ym/IBJArVXecC3/3pro= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Dc+6YsvU; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Dc+6YsvU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788750; x=1762324750; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ETTKI5+hacWoJJE2C+uh6AGfusJHWOqz7xoddYnIUoE=; b=Dc+6YsvUflwWrzCC3KHTnckWUUjzLl+gqmFKxxiDIozvGbYyoGlkNR5F bkPcVT4VVQsWt8t/EP109SYQOG0zQ1zkURlZceWOeEx/FHDRgfAZ0uoyO iCPipHMf7x3Z3toyDmbIjLuJe9RSMc0CJXlqjBnGsIkcLB+BcvJ8/pgpi 4vt4puUXy1mNYUNLQhnQULBARYuSOivUI1HA91UN433n6PowKc2Av+Fvf 2NZsdcUUtbrSXVJhAJ9He/cu4XdiuZ5vl2J52+EN5ndVMeJK28fZ+xbLR +HZWlpmEWxjsNQhx6uiqbCkE9xh+noOJLAQ1yDOV4rgSmAsH+KPT8Q5M6 g==; X-CSE-ConnectionGUID: RrmD8pycQSyP3Tpo1JW98g== X-CSE-MsgGUID: VKID7HB4R8acpjR+z1+6cg== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689750" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689750" 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:39:10 -0800 X-CSE-ConnectionGUID: aHXP1H8NQpSpWutRAMliPQ== X-CSE-MsgGUID: 8S17/192T7WLzQnztIR5qQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989534" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:39:06 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 39/60] i386/tdx: Disable PIC for TDX VMs Date: Tue, 5 Nov 2024 01:23:47 -0500 Message-Id: <20241105062408.3533704-40-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Legacy PIC (8259) cannot be supported for TDX VMs since TDX module doesn't allow directly interrupt injection. Using posted interrupts for the PIC is not a viable option as the guest BIOS/kernel will not do EOI for PIC IRQs, i.e. will leave the vIRR bit set. Hence disable PIC for TDX VMs and error out if user wants PIC. Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann --- target/i386/kvm/tdx.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 68d90a180db7..9ab4e911f78a 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -369,6 +369,13 @@ static int tdx_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) return -EINVAL; } + if (x86ms->pic == ON_OFF_AUTO_AUTO) { + x86ms->pic = ON_OFF_AUTO_OFF; + } else if (x86ms->pic == ON_OFF_AUTO_ON) { + error_setg(errp, "TDX VM doesn't support PIC"); + return -EINVAL; + } + if (!tdx_caps) { r = get_tdx_capabilities(errp); if (r) { From patchwork Tue Nov 5 06:23:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862448 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 396771FDF9B for ; Tue, 5 Nov 2024 06:39:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788755; cv=none; b=T3w+JOOClUJ2reP8ZsGX//GJkhGzB+4gYjjO99jtT7WGNrcA8TXnwqXHW7EgekHGohqqVyqXOs/5/tM5C1auRPL5MrQ8w6BKOosScFiEV82PoWj+tNa6iKuawq+qjUWKndfEN3vKjZjsp9mbxW4XNrwZvNWBN1E3orP+2bNv3CE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788755; c=relaxed/simple; bh=BHXWCaOd3UyAcZOJUgpgw+yI5VVHsxLUYqIIcqCHqr8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KeDsL++Z0emyvhKwkuboE8BZVjPYnc8y7lmmLAXc8Zb8Ym0Tq3xtvznJi1j6LW+nrbK99atwbYxNH1ALStPiqJIpzhINXpAwPWDjKLlm+r212Vz35Lii4Pw2DHv1m8cPdZKOm1uzq1hQjhBWTnGulrm9EYD+rED2C9YowcmTRrs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=dPbwa/oO; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="dPbwa/oO" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788754; x=1762324754; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=BHXWCaOd3UyAcZOJUgpgw+yI5VVHsxLUYqIIcqCHqr8=; b=dPbwa/oOvolGgx0fV/HFbprQJAo8XMhzGlbrVjQgxyTqmmJ94CD6TSLN y+rg+TInAjjqwKHLrdasksULC/LmBdyKwVV8T1JiYAX357KG+tbjCk71V FJFb7mBcMyFxAm4pKPc1q56EJATXfyZbSndRSNUttsUMcJi7c99R8RX8C gviBitb42V7hRGtpIt/u53RUEXy9Ry/nzBC88IsFgGdLzG0o87yXIpFxd jD+Z1sN4cLThjHK4DWbEt7mn98ypuDdfvVyCDhyNJyMWw2Z0zi8ipVKZW C3Wig6/dx86W4/5QyIz87C+rP4RmQ2phnjL+7YPqk/m61R2qGyttqLWo8 Q==; X-CSE-ConnectionGUID: +4Pxi/LbQQmTcUavgglVsQ== X-CSE-MsgGUID: hUMBSCP6Sry5Rpb0DHmNEA== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689761" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689761" 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:39:14 -0800 X-CSE-ConnectionGUID: AYU/sAKLS2q3DkWuAe4Fxw== X-CSE-MsgGUID: VBH/u/1NTEmaD6xbyYj7Gw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989566" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:39:10 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 40/60] hw/i386: add eoi_intercept_unsupported member to X86MachineState Date: Tue, 5 Nov 2024 01:23:48 -0500 Message-Id: <20241105062408.3533704-41-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add a new bool member, eoi_intercept_unsupported, to X86MachineState with default value false. Set true for TDX VM. Inability to intercept eoi causes impossibility to emulate level triggered interrupt to be re-injected when level is still kept active. which affects interrupt controller emulation. Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann --- hw/i386/x86.c | 1 + include/hw/i386/x86.h | 1 + target/i386/kvm/tdx.c | 2 ++ 3 files changed, 4 insertions(+) diff --git a/hw/i386/x86.c b/hw/i386/x86.c index 01fc5e656272..82faeed24ff9 100644 --- a/hw/i386/x86.c +++ b/hw/i386/x86.c @@ -370,6 +370,7 @@ static void x86_machine_initfn(Object *obj) x86ms->oem_table_id = g_strndup(ACPI_BUILD_APPNAME8, 8); x86ms->bus_lock_ratelimit = 0; x86ms->above_4g_mem_start = 4 * GiB; + x86ms->eoi_intercept_unsupported = false; } static void x86_machine_class_init(ObjectClass *oc, void *data) diff --git a/include/hw/i386/x86.h b/include/hw/i386/x86.h index d43cb3908e65..fd9a30391755 100644 --- a/include/hw/i386/x86.h +++ b/include/hw/i386/x86.h @@ -73,6 +73,7 @@ struct X86MachineState { uint64_t above_4g_mem_start; /* CPU and apic information: */ + bool eoi_intercept_unsupported; unsigned pci_irq_mask; unsigned apic_id_limit; uint16_t boot_cpus; diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 9ab4e911f78a..9dcb77e011bd 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -388,6 +388,8 @@ static int tdx_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) return -EOPNOTSUPP; } + x86ms->eoi_intercept_unsupported = true; + /* * Set kvm_readonly_mem_allowed to false, because TDX only supports readonly * memory for shared memory but not for private memory. Besides, whether a From patchwork Tue Nov 5 06:23:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862449 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F31741FDFBE for ; Tue, 5 Nov 2024 06:39:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788760; cv=none; b=dF7J9zaGv6CdyV1VotUhybfPvn9MOJoRN08rBZvzDBv2CEGJOKNnNRg1fgo0MEqdBXuSo3Fq3B0F8OsdITUElG3q70oeQ8Q3lEXQzpXV4HT7sJb8Z/FqXm3pam1Gkxj9z6sUXgID1C3XFuzb/P9kaKVAz5ObjGidSnXXqaBEwCA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788760; c=relaxed/simple; bh=8uEuNvmf9noH27+CtakzBXFvtETe/Vb0IM6iwSDYV+w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XWIS1qg2HjHM6Yupm21YEKPMdX580S5sQMAa1ZV7j0JQTe7oKvEHvOZFiOLzvogGIpTVZ0n2o7wzI67oHU466IuDX/UHgY/dGtkCrIdlBxk6oWuZkuumsx2guQdcGPq9ftdyM2zZ2RcOMB8GPvQTwMfctwEtTJuPyBMKgCVPHlc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=nLnfeyz5; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="nLnfeyz5" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788759; x=1762324759; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=8uEuNvmf9noH27+CtakzBXFvtETe/Vb0IM6iwSDYV+w=; b=nLnfeyz5K/vlE17gJhWyubTCCkobmYCgJ3feZAGY6eg5y2SoiLWSLLY/ D2302+8kuhwFrzadLlAXXxLhcwqO0ObMrpuqrCFPdYmeYgWI75aBgOzWN NRqr8aPmiaUy7d43OB0eofcxfnLulnt5OA/GhFhpfJGgeft4pvEr55CiA /JEXLUhpBCtXG9XUHCS268Ji8saDCzpz7yc5GjPRCjXuQVFfJaCycYJKT bAJkDxL4ypJJGMLYZ/OZKrGCm3LZLE8245cNpsHglKWN9HmFjfmfSazQG dHexJUvu2bv+6hJkgyxUVXhKQRcJvAMWka60HH0rpYzwLHwTVGuwqObGk Q==; X-CSE-ConnectionGUID: cXUUs+YrQO6FgjoFuHziuA== X-CSE-MsgGUID: bFh6emA5TN2D14lvrnmd3w== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689768" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689768" 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:39:19 -0800 X-CSE-ConnectionGUID: DcXkl4xzR2q/ZU3Wc71OxQ== X-CSE-MsgGUID: EsjtOYi6QOKwTZwDwlyv1g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989595" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:39:14 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 41/60] hw/i386: add option to forcibly report edge trigger in acpi tables Date: Tue, 5 Nov 2024 01:23:49 -0500 Message-Id: <20241105062408.3533704-42-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Isaku Yamahata When level trigger isn't supported on x86 platform, forcibly report edge trigger in acpi tables. Signed-off-by: Isaku Yamahata Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann --- hw/i386/acpi-build.c | 99 ++++++++++++++++++++++++++++--------------- hw/i386/acpi-common.c | 45 +++++++++++++++----- 2 files changed, 101 insertions(+), 43 deletions(-) diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c index 4967aa745902..d0a5bfc69e9a 100644 --- a/hw/i386/acpi-build.c +++ b/hw/i386/acpi-build.c @@ -888,7 +888,8 @@ static void build_dbg_aml(Aml *table) aml_append(table, scope); } -static Aml *build_link_dev(const char *name, uint8_t uid, Aml *reg) +static Aml *build_link_dev(const char *name, uint8_t uid, Aml *reg, + bool level_trigger_unsupported) { Aml *dev; Aml *crs; @@ -900,7 +901,10 @@ static Aml *build_link_dev(const char *name, uint8_t uid, Aml *reg) aml_append(dev, aml_name_decl("_UID", aml_int(uid))); crs = aml_resource_template(); - aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH, + aml_append(crs, aml_interrupt(AML_CONSUMER, + level_trigger_unsupported ? + AML_EDGE : AML_LEVEL, + AML_ACTIVE_HIGH, AML_SHARED, irqs, ARRAY_SIZE(irqs))); aml_append(dev, aml_name_decl("_PRS", crs)); @@ -924,7 +928,8 @@ static Aml *build_link_dev(const char *name, uint8_t uid, Aml *reg) return dev; } -static Aml *build_gsi_link_dev(const char *name, uint8_t uid, uint8_t gsi) +static Aml *build_gsi_link_dev(const char *name, uint8_t uid, + uint8_t gsi, bool level_trigger_unsupported) { Aml *dev; Aml *crs; @@ -937,7 +942,10 @@ static Aml *build_gsi_link_dev(const char *name, uint8_t uid, uint8_t gsi) crs = aml_resource_template(); irqs = gsi; - aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH, + aml_append(crs, aml_interrupt(AML_CONSUMER, + level_trigger_unsupported ? + AML_EDGE : AML_LEVEL, + AML_ACTIVE_HIGH, AML_SHARED, &irqs, 1)); aml_append(dev, aml_name_decl("_PRS", crs)); @@ -956,7 +964,7 @@ static Aml *build_gsi_link_dev(const char *name, uint8_t uid, uint8_t gsi) } /* _CRS method - get current settings */ -static Aml *build_iqcr_method(bool is_piix4) +static Aml *build_iqcr_method(bool is_piix4, bool level_trigger_unsupported) { Aml *if_ctx; uint32_t irqs; @@ -964,7 +972,9 @@ static Aml *build_iqcr_method(bool is_piix4) Aml *crs = aml_resource_template(); irqs = 0; - aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, + aml_append(crs, aml_interrupt(AML_CONSUMER, + level_trigger_unsupported ? + AML_EDGE : AML_LEVEL, AML_ACTIVE_HIGH, AML_SHARED, &irqs, 1)); aml_append(method, aml_name_decl("PRR0", crs)); @@ -998,7 +1008,7 @@ static Aml *build_irq_status_method(void) return method; } -static void build_piix4_pci0_int(Aml *table) +static void build_piix4_pci0_int(Aml *table, bool level_trigger_unsupported) { Aml *dev; Aml *crs; @@ -1011,12 +1021,16 @@ static void build_piix4_pci0_int(Aml *table) aml_append(sb_scope, pci0_scope); aml_append(sb_scope, build_irq_status_method()); - aml_append(sb_scope, build_iqcr_method(true)); + aml_append(sb_scope, build_iqcr_method(true, level_trigger_unsupported)); - aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQ0"))); - aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQ1"))); - aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQ2"))); - aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQ3"))); + aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQ0"), + level_trigger_unsupported)); + aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQ1"), + level_trigger_unsupported)); + aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQ2"), + level_trigger_unsupported)); + aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQ3"), + level_trigger_unsupported)); dev = aml_device("LNKS"); { @@ -1025,7 +1039,9 @@ static void build_piix4_pci0_int(Aml *table) crs = aml_resource_template(); irqs = 9; - aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, + aml_append(crs, aml_interrupt(AML_CONSUMER, + level_trigger_unsupported ? + AML_EDGE : AML_LEVEL, AML_ACTIVE_HIGH, AML_SHARED, &irqs, 1)); aml_append(dev, aml_name_decl("_PRS", crs)); @@ -1111,7 +1127,7 @@ static Aml *build_q35_routing_table(const char *str) return pkg; } -static void build_q35_pci0_int(Aml *table) +static void build_q35_pci0_int(Aml *table, bool level_trigger_unsupported) { Aml *method; Aml *sb_scope = aml_scope("_SB"); @@ -1150,25 +1166,41 @@ static void build_q35_pci0_int(Aml *table) aml_append(sb_scope, pci0_scope); aml_append(sb_scope, build_irq_status_method()); - aml_append(sb_scope, build_iqcr_method(false)); + aml_append(sb_scope, build_iqcr_method(false, level_trigger_unsupported)); - aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQA"))); - aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQB"))); - aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQC"))); - aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQD"))); - aml_append(sb_scope, build_link_dev("LNKE", 4, aml_name("PRQE"))); - aml_append(sb_scope, build_link_dev("LNKF", 5, aml_name("PRQF"))); - aml_append(sb_scope, build_link_dev("LNKG", 6, aml_name("PRQG"))); - aml_append(sb_scope, build_link_dev("LNKH", 7, aml_name("PRQH"))); + aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQA"), + level_trigger_unsupported)); + aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQB"), + level_trigger_unsupported)); + aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQC"), + level_trigger_unsupported)); + aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQD"), + level_trigger_unsupported)); + aml_append(sb_scope, build_link_dev("LNKE", 4, aml_name("PRQE"), + level_trigger_unsupported)); + aml_append(sb_scope, build_link_dev("LNKF", 5, aml_name("PRQF"), + level_trigger_unsupported)); + aml_append(sb_scope, build_link_dev("LNKG", 6, aml_name("PRQG"), + level_trigger_unsupported)); + aml_append(sb_scope, build_link_dev("LNKH", 7, aml_name("PRQH"), + level_trigger_unsupported)); - aml_append(sb_scope, build_gsi_link_dev("GSIA", 0x10, 0x10)); - aml_append(sb_scope, build_gsi_link_dev("GSIB", 0x11, 0x11)); - aml_append(sb_scope, build_gsi_link_dev("GSIC", 0x12, 0x12)); - aml_append(sb_scope, build_gsi_link_dev("GSID", 0x13, 0x13)); - aml_append(sb_scope, build_gsi_link_dev("GSIE", 0x14, 0x14)); - aml_append(sb_scope, build_gsi_link_dev("GSIF", 0x15, 0x15)); - aml_append(sb_scope, build_gsi_link_dev("GSIG", 0x16, 0x16)); - aml_append(sb_scope, build_gsi_link_dev("GSIH", 0x17, 0x17)); + aml_append(sb_scope, build_gsi_link_dev("GSIA", 0x10, 0x10, + level_trigger_unsupported)); + aml_append(sb_scope, build_gsi_link_dev("GSIB", 0x11, 0x11, + level_trigger_unsupported)); + aml_append(sb_scope, build_gsi_link_dev("GSIC", 0x12, 0x12, + level_trigger_unsupported)); + aml_append(sb_scope, build_gsi_link_dev("GSID", 0x13, 0x13, + level_trigger_unsupported)); + aml_append(sb_scope, build_gsi_link_dev("GSIE", 0x14, 0x14, + level_trigger_unsupported)); + aml_append(sb_scope, build_gsi_link_dev("GSIF", 0x15, 0x15, + level_trigger_unsupported)); + aml_append(sb_scope, build_gsi_link_dev("GSIG", 0x16, 0x16, + level_trigger_unsupported)); + aml_append(sb_scope, build_gsi_link_dev("GSIH", 0x17, 0x17, + level_trigger_unsupported)); aml_append(table, sb_scope); } @@ -1350,6 +1382,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker, PCMachineState *pcms = PC_MACHINE(machine); PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(machine); X86MachineState *x86ms = X86_MACHINE(machine); + bool level_trigger_unsupported = x86ms->eoi_intercept_unsupported; AcpiMcfgInfo mcfg; bool mcfg_valid = !!acpi_get_mcfg(&mcfg); uint32_t nr_mem = machine->ram_slots; @@ -1382,7 +1415,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker, if (pm->pcihp_bridge_en || pm->pcihp_root_en) { build_x86_acpi_pci_hotplug(dsdt, pm->pcihp_io_base); } - build_piix4_pci0_int(dsdt); + build_piix4_pci0_int(dsdt, level_trigger_unsupported); } else if (q35) { sb_scope = aml_scope("_SB"); dev = aml_device("PCI0"); @@ -1426,7 +1459,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker, if (pm->pcihp_bridge_en) { build_x86_acpi_pci_hotplug(dsdt, pm->pcihp_io_base); } - build_q35_pci0_int(dsdt); + build_q35_pci0_int(dsdt, level_trigger_unsupported); } if (misc->has_hpet) { diff --git a/hw/i386/acpi-common.c b/hw/i386/acpi-common.c index 0cc2919bb851..ad38a6b31162 100644 --- a/hw/i386/acpi-common.c +++ b/hw/i386/acpi-common.c @@ -103,6 +103,7 @@ void acpi_build_madt(GArray *table_data, BIOSLinker *linker, const CPUArchIdList *apic_ids = mc->possible_cpu_arch_ids(MACHINE(x86ms)); AcpiTable table = { .sig = "APIC", .rev = 3, .oem_id = oem_id, .oem_table_id = oem_table_id }; + bool level_trigger_unsupported = x86ms->eoi_intercept_unsupported; acpi_table_begin(&table, table_data); /* Local APIC Address */ @@ -124,18 +125,42 @@ void acpi_build_madt(GArray *table_data, BIOSLinker *linker, IO_APIC_SECONDARY_ADDRESS, IO_APIC_SECONDARY_IRQBASE); } - if (x86mc->apic_xrupt_override) { - build_xrupt_override(table_data, 0, 2, - 0 /* Flags: Conforms to the specifications of the bus */); - } + if (level_trigger_unsupported) { + /* Force edge trigger */ + if (x86mc->apic_xrupt_override) { + build_xrupt_override(table_data, 0, 2, + /* Flags: active high, edge triggered */ + 1 | (1 << 2)); + } + + for (i = x86mc->apic_xrupt_override ? 1 : 0; i < 16; i++) { + build_xrupt_override(table_data, i, i, + /* Flags: active high, edge triggered */ + 1 | (1 << 2)); + } + + if (x86ms->ioapic2) { + for (i = 0; i < 16; i++) { + build_xrupt_override(table_data, IO_APIC_SECONDARY_IRQBASE + i, + IO_APIC_SECONDARY_IRQBASE + i, + /* Flags: active high, edge triggered */ + 1 | (1 << 2)); + } + } + } else { + if (x86mc->apic_xrupt_override) { + build_xrupt_override(table_data, 0, 2, + 0 /* Flags: Conforms to the specifications of the bus */); + } - for (i = 1; i < 16; i++) { - if (!(x86ms->pci_irq_mask & (1 << i))) { - /* No need for a INT source override structure. */ - continue; + for (i = 1; i < 16; i++) { + if (!(x86ms->pci_irq_mask & (1 << i))) { + /* No need for a INT source override structure. */ + continue; + } + build_xrupt_override(table_data, i, i, + 0xd /* Flags: Active high, Level Triggered */); } - build_xrupt_override(table_data, i, i, - 0xd /* Flags: Active high, Level Triggered */); } if (x2apic_mode) { From patchwork Tue Nov 5 06:23:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862450 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 165691FDFBE for ; Tue, 5 Nov 2024 06:39:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788764; cv=none; b=MdjMVQV32ourKSAI22ydX0mFAtkU+uY2admyrXIGll1Ri9ZL/BqK8Ss2JIRvVfJO5jUYiE+WwnZ3jvP3o7E3UPt/o3N/0L4QrEJyuke0aDaLKAZkXE++3ClhaeoVoJtqMXKQg+WmiA45nc4IJCcxnOMIaVAQ/QyZ2cOX8SATq0U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788764; c=relaxed/simple; bh=uFxOU0S0tIUsyV9rR7mBF54/Xh/koV03EYrPligZB2A=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=A22M8/OeHxK1IJcV0UThPVaupj78lPSWV5ZZAlqemeKNjoMDdhgw7KWYePAPEpe0R/rEu0fJSzqmhRh6t5GBzZYdngfhF8lfWrmi0OpUZUFEh38OxD892N9l3h0m6VJ6Z89h0kJNJVt2YGz4q8bzcyIEEo6dXk8hsRjECS2BIPc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=EoepP8IK; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="EoepP8IK" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788763; x=1762324763; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=uFxOU0S0tIUsyV9rR7mBF54/Xh/koV03EYrPligZB2A=; b=EoepP8IKrpaNd0mLm7GhGUjBCiTeZr1XL9MiaKTqV5D9e8VMDI4m4bYN axMHTy6std/apAbLcO33H9IcPhoUsjDlJTmPRLeDP9n2OUiP52S96X3kO 5QK3ufWE6y6VtpTVaJGH8nSu4nGEtYz69N7cOABZW41qt/QJOCigQ6E69 aIQ1yKeiChDY0gda2H/EtEL+QoOx4HjffAHDhxBpSMN4p40cHRrzAJHnE GJrkqUFW/7hqzElPcz0RM/EdugeCi/YzCjdx9nNkPvi8gdhCEUyQAch/5 ITQ6kZV7jPJAMUjdF8obg1a4cr3Vv50cgVYnjJEdW50w+tu/+sJvb63F5 g==; X-CSE-ConnectionGUID: r2LaZIntQ5S0+3YxlX6CDg== X-CSE-MsgGUID: KS7CnF4NQdWOuq6RjI2i8w== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689776" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689776" 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:39:23 -0800 X-CSE-ConnectionGUID: fZqnB8jWQYyh3dtw65a9ag== X-CSE-MsgGUID: UaR6lv4fRS+2sLaYX0hujA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989612" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:39:19 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 42/60] i386/tdx: Don't synchronize guest tsc for TDs Date: Tue, 5 Nov 2024 01:23:50 -0500 Message-Id: <20241105062408.3533704-43-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Isaku Yamahata TSC of TDs is not accessible and KVM doesn't allow access of MSR_IA32_TSC for TDs. To avoid the assert() in kvm_get_tsc, make kvm_synchronize_all_tsc() noop for TDs, Signed-off-by: Isaku Yamahata Reviewed-by: Connor Kuehl Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann --- target/i386/kvm/kvm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index 022809bad36e..595439f4a4d6 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -330,7 +330,7 @@ void kvm_synchronize_all_tsc(void) { CPUState *cpu; - if (kvm_enabled()) { + if (kvm_enabled() && !is_tdx_vm()) { CPU_FOREACH(cpu) { run_on_cpu(cpu, do_kvm_synchronize_tsc, RUN_ON_CPU_NULL); } From patchwork Tue Nov 5 06:23:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862451 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E76A1714A0 for ; Tue, 5 Nov 2024 06:39:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788768; cv=none; b=oxREWdCqOKHReqLsqTpMyHQHkVP/adwFsGJ5j5QnuieFM3V8bc9RTzvwWwnnFlo9+FUhh3RiFZ0DWGzQmt8DDGm3Ko6F+o8IGP7PbVIv2Czi+C/CDwYWyIyyhq8N2XXGxOdkaxUYujGypDh8PLqsBKKjxucN14NWBO3+n4HFCsg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788768; c=relaxed/simple; bh=sfRTE3KuC3TUYSe9/IzIJIb3zhuizbpOCanDRw/1dzs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=plpzNp4NRsKwFLq1h8okRtslADHYke/6+DNBHNBJC4IsnOMs8ozM2jXZHK+rZ+mymMnAXRPgDGsfB/Bhark02qi0s6uDCuJ4oDPwvotX8TZDK4CT3wnUFZ54IeR0oOTxKYEpVsaUrp938Mm9VcawKoknTCtmKU0lVbRadTfG++k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=ayNpDRHi; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ayNpDRHi" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788767; x=1762324767; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=sfRTE3KuC3TUYSe9/IzIJIb3zhuizbpOCanDRw/1dzs=; b=ayNpDRHitFZyYktdm1Jews9jicwpuXhaqaBqr4Bq8VbGnwG0qaGxDHXr gaJCcEKeK5ouWilRoKqRF9QA4TddmyaR8YZ0jBfsBx8mVBv7CrvXaJ22v 0IwRvkGeiaEuhFbfBl+WRlEr2HFvsTXkJeusf6JrGHJ4wHglUNpmf1Nj6 P/UNz2dzA8suFCWvgGWf5fdlc/55AMXVGwGvPIf4oOI9V5O+ythVnTiTC XY2/QsBzL4yrNxy0WU9Do6sT73RKZ0aFYJG9foF89Wu21YeSRj9kJPw3Y zxXyt0ehiSnyGL1NkkYwDJT606whkcy0kZEEHhmSaz2nzHMW5TU8XHV99 w==; X-CSE-ConnectionGUID: g0JY8aYxR/yu6OIIZ7gE5Q== X-CSE-MsgGUID: EXOD6vL1SmuJnpWYhOYmiw== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689789" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689789" 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:39:27 -0800 X-CSE-ConnectionGUID: wooCqLHeS6GiI0bPYQB6Sg== X-CSE-MsgGUID: Nssb6yFVSEmGQHaQCO+BPQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989635" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:39:23 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 43/60] i386/tdx: Only configure MSR_IA32_UCODE_REV in kvm_init_msrs() for TDs Date: Tue, 5 Nov 2024 01:23:51 -0500 Message-Id: <20241105062408.3533704-44-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 For TDs, only MSR_IA32_UCODE_REV in kvm_init_msrs() can be configured by VMM, while the features enumerated/controlled by other MSRs except MSR_IA32_UCODE_REV in kvm_init_msrs() are not under control of VMM. Only configure MSR_IA32_UCODE_REV for TDs. Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann --- target/i386/kvm/kvm.c | 44 ++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index 595439f4a4d6..8909fce14909 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -3852,32 +3852,34 @@ static void kvm_init_msrs(X86CPU *cpu) CPUX86State *env = &cpu->env; kvm_msr_buf_reset(cpu); - if (has_msr_arch_capabs) { - kvm_msr_entry_add(cpu, MSR_IA32_ARCH_CAPABILITIES, - env->features[FEAT_ARCH_CAPABILITIES]); - } - - if (has_msr_core_capabs) { - kvm_msr_entry_add(cpu, MSR_IA32_CORE_CAPABILITY, - env->features[FEAT_CORE_CAPABILITY]); - } - - if (has_msr_perf_capabs && cpu->enable_pmu) { - kvm_msr_entry_add_perf(cpu, env->features); + + if (!is_tdx_vm()) { + if (has_msr_arch_capabs) { + kvm_msr_entry_add(cpu, MSR_IA32_ARCH_CAPABILITIES, + env->features[FEAT_ARCH_CAPABILITIES]); + } + + if (has_msr_core_capabs) { + kvm_msr_entry_add(cpu, MSR_IA32_CORE_CAPABILITY, + env->features[FEAT_CORE_CAPABILITY]); + } + + if (has_msr_perf_capabs && cpu->enable_pmu) { + kvm_msr_entry_add_perf(cpu, env->features); + } + + /* + * Older kernels do not include VMX MSRs in KVM_GET_MSR_INDEX_LIST, but + * all kernels with MSR features should have them. + */ + if (kvm_feature_msrs && cpu_has_vmx(env)) { + kvm_msr_entry_add_vmx(cpu, env->features); + } } if (has_msr_ucode_rev) { kvm_msr_entry_add(cpu, MSR_IA32_UCODE_REV, cpu->ucode_rev); } - - /* - * Older kernels do not include VMX MSRs in KVM_GET_MSR_INDEX_LIST, but - * all kernels with MSR features should have them. - */ - if (kvm_feature_msrs && cpu_has_vmx(env)) { - kvm_msr_entry_add_vmx(cpu, env->features); - } - assert(kvm_buf_set_msrs(cpu) == 0); } From patchwork Tue Nov 5 06:23:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862452 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1EF071714A0 for ; Tue, 5 Nov 2024 06:39:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788772; cv=none; b=Omz1Ddya5qWrdefnhpk6/Aqe9gbqeuUvkwr03HldZoggbVkUNf8EtK6mvQDLcLLLotBJHnzvd7NFOm3peWKwnE7gnEh9uE/2OtFIw6uUsIwLtSVxtOT08sNqIsEqZSEZEe+1Lx/+j+lAHS/gHIF/w4oCgBJbD1ryQOIwrSRvDnI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788772; c=relaxed/simple; bh=+XE3AzklwMx8qhJJ+4Ok+j4KRAHEYu5njiiWc5OMr0s=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NMEyCY/7Ecmv9F3fSA6cDmBIX82kFdrtQU6o+sm3Bq0TaL13GqHNgQcfOC3y4A398hUcF0xdHFAj/Q9qPS515o++1unUzRsIjMxnenJw8yoGkrnjUKD7U/jYawMYirMdYqAKjXO2oHaed1m2dwgauL2nbrqjn5XAGWmAfBcGPuY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=mQ+AH8qE; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="mQ+AH8qE" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788771; x=1762324771; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=+XE3AzklwMx8qhJJ+4Ok+j4KRAHEYu5njiiWc5OMr0s=; b=mQ+AH8qEAmnP6VhPUWRHKD0/i8zQcIbkQyM2NMTRFln9OO7TXQAIXS95 a4KYj5LAmEnvV11tIP9loTb+JK95OwTKSF2iDsd8DlE2/5aC0Ljhh9e/S q0iN7LUD5ZZEQxLS6VaMMdMVRnKQzsC7McgOh6Npar5nq7Wp0CZPhGDl2 xaSO7/WeR2GOw1deQYj5UXxKrsVpn6tCr/lz3mWdImOw4L2sFavBI0Vbk Px3p20g/xfITlOvdxBLFFjVBZca/WRUqhzrN6tYRcMrBzDJ7xzVgVMMFX BEG5J2fJ/rU+FfakpHYU7M8go4qB7/mTOiQWLzZDbz058I7VitEUgvBFE Q==; X-CSE-ConnectionGUID: su2NBJtDRyeKmkk1y+j3cA== X-CSE-MsgGUID: 6PbwBzueRQmTxqAGh9uiRQ== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689803" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689803" 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:39:31 -0800 X-CSE-ConnectionGUID: /q6YE+zjQ2S+qPxXDNLUQw== X-CSE-MsgGUID: 8D/bpe6kSS61Uxuq3srC+w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989664" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:39:27 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 44/60] i386/tdx: Skip kvm_put_apicbase() for TDs Date: Tue, 5 Nov 2024 01:23:52 -0500 Message-Id: <20241105062408.3533704-45-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 KVM doesn't allow wirting to MSR_IA32_APICBASE for TDs. Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann --- target/i386/kvm/kvm.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index 8909fce14909..c39e879a77e9 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -3627,6 +3627,11 @@ void kvm_put_apicbase(X86CPU *cpu, uint64_t value) { int ret; + /* TODO: Allow accessing guest state for debug TDs. */ + if (is_tdx_vm()) { + return; + } + ret = kvm_put_one_msr(cpu, MSR_IA32_APICBASE, value); assert(ret == 1); } From patchwork Tue Nov 5 06:23:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862453 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A96A11714A0 for ; Tue, 5 Nov 2024 06:39:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788777; cv=none; b=Y458ZWt7M3uUwp1WthNZqWvcHYsYB2SNH+q/DIU1oMePxibbvB2u6rjCHzSA+OYsavC+KRARLu8yru9P8P6K/cMhX3Xz3s/eXEWpR3KO69a/6YQ/IhI/kCVGm7UKHWxLYILvqLwLBzSAsPjf/mP11NvPFrLh5P2bpIhKu7DTSI4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788777; c=relaxed/simple; bh=C2RlAuENX7AQKgB6fkf8PxOsDfhUa1Oh2lPmCd3nnHY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZJkYEdn6hLqvZPeX9kAcdAdEeJqOHiul39tL2K0NTOXtfbRfrP3BhfP12gASblmdRwClvbOinzEjE/+RkEGdPtJ9mzyqj7vgCLDIQ4mLukqqoZphka6TjJImJ4JONT4fnHVpoguqhjNPLGYRbatQY3vcSXr00rpsJyBUVGSm+ac= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=ipL4Yx5z; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ipL4Yx5z" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788776; x=1762324776; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=C2RlAuENX7AQKgB6fkf8PxOsDfhUa1Oh2lPmCd3nnHY=; b=ipL4Yx5zwjFJsYeYktSvZnrfcHBv6U4ijt0dj8XXosfeGgNyrJUGOYD6 HMK3xBjzikA2ROIe+xkMEAXitVd2kXE8ApgNX8OqC8TFv45jswiundQjV 7PVtKy6y60HVDVwj/D6yJIxXe8Tms5yee7CUIpuxsQEVYOBPhZg71La35 RsGHV9tS7eMrNv4mfBfeQtIcxyIvq+vJT0lj/QWCk2rDD78YmdrMAkQXX SaFhi5xWRntDGbDWr0YMULpWHmAE77HoO72femjGbmvJ+a3GzLWTQy1Vn QC8IyUSu/HNUdUZHyOXGWiXeEL3ru7CO50CHBsdlfDTC0PEmm/AVJgYko A==; X-CSE-ConnectionGUID: vOyDuBkDTvqvRhbav1R1Pg== X-CSE-MsgGUID: r9MckzrLQdSpt74N4vumPw== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689812" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689812" 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:39:35 -0800 X-CSE-ConnectionGUID: nQHxqnLjRaOvI8sC+/HFoA== X-CSE-MsgGUID: w5YMLsrFQWCEEsV4cIXAAw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989690" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:39:31 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 45/60] i386/tdx: Don't get/put guest state for TDX VMs Date: Tue, 5 Nov 2024 01:23:53 -0500 Message-Id: <20241105062408.3533704-46-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Sean Christopherson Don't get/put state of TDX VMs since accessing/mutating guest state of production TDs is not supported. Note, it will be allowed for a debug TD. Corresponding support will be introduced when debug TD support is implemented in the future. Signed-off-by: Sean Christopherson Signed-off-by: Xiaoyao Li Acked-by: Gerd Hoffmann --- target/i386/kvm/kvm.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index c39e879a77e9..e47aa32233e6 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -5254,6 +5254,11 @@ int kvm_arch_put_registers(CPUState *cpu, int level, Error **errp) assert(cpu_is_stopped(cpu) || qemu_cpu_is_self(cpu)); + /* TODO: Allow accessing guest state for debug TDs. */ + if (is_tdx_vm()) { + return 0; + } + /* * Put MSR_IA32_FEATURE_CONTROL first, this ensures the VM gets out of VMX * root operation upon vCPU reset. kvm_put_msr_feature_control() should also @@ -5368,6 +5373,12 @@ int kvm_arch_get_registers(CPUState *cs, Error **errp) error_setg_errno(errp, -ret, "Failed to get MP state"); goto out; } + + /* TODO: Allow accessing guest state for debug TDs. */ + if (is_tdx_vm()) { + return 0; + } + ret = kvm_getput_regs(cpu, 0); if (ret < 0) { error_setg_errno(errp, -ret, "Failed to get general purpose registers"); From patchwork Tue Nov 5 06:23:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862454 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B09D41FE0FF for ; Tue, 5 Nov 2024 06:39:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788781; cv=none; b=GaWrqkdX0BE3BTuJHrWLh3f6xQccSjjkHkWDw1kcXWh+B7qqBkOD8gnaiQw0r6HCYOrxO9OvfGbRMUZ0AWFf5aX97lwwDtEyZQ1Q/XGQjXH73Bk+DivogEXRwhF5zD0uLwlWh5uvYsj6TQMBGaMDC5XiIe4NYUmggmpbEn60ZoY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788781; c=relaxed/simple; bh=8SBCXjeKuMoMULo/dUKnjmMZB22IpUgmloGXn6p/MT4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=f8/QRpRkStDDtv/LLM3vjbpL2N7Y5ssVErryjBAPrSfGACDfkfGbWgA+O6ZL8PCHCsfjAvZA/UXUF4LrnQ1YISEHTwNnYWRmJotaTP7IwCxZ+K/NPVye8Ns3uVkWq8UZf6UL8QuHvMXFxQIe3LoUlN1L025T+UwWClAjH1t76Kw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=QmeqQ16D; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="QmeqQ16D" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788780; x=1762324780; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=8SBCXjeKuMoMULo/dUKnjmMZB22IpUgmloGXn6p/MT4=; b=QmeqQ16D98QuB6m+bSJbDiLkFGdm9br9UPGseGxd1rkYmp9WXYEpqZPD SvCWG3WF/Yz1lrtkaA1ptV/Y8YDbk0udvleEe86ESLF2wWcxgtrFZZs0U ovD5NAxnu9dmd3fDcvwNJl96HHsmSF5ACpzCps+YfeYSZvL80ZCAYVnJu I/gS8Y4CncD9ReTKaRx87P4UlhoDBsPTWm+5f/9MYWdh8rP5xLvRZovkG 8RxZ3YbAM9uMZAGtGogzAClLJXxqK95QnQGNdWQ7wuh7Hrin3zKvWVAzc 5RDL4PX6MhqCORYU3rYXpwARO5Z6zh6sWThH/+y2O2Vpe0sdrP37CnACo g==; X-CSE-ConnectionGUID: Mx+h9YCxRQyubkqe1Xs9+Q== X-CSE-MsgGUID: sjl1WRpwTiO1Kg+hDAU4CQ== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689823" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689823" 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:39:40 -0800 X-CSE-ConnectionGUID: 02jwTyEjT4W7WgQAzjU8Tg== X-CSE-MsgGUID: bd629gL+QAag3jRWFrdaqw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989718" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:39:35 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 46/60] i386/cgs: Rename *mask_cpuid_features() to *adjust_cpuid_features() Date: Tue, 5 Nov 2024 01:23:54 -0500 Message-Id: <20241105062408.3533704-47-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Because for TDX case, there are also fixed-1 bits that enfored by TDX module. Signed-off-by: Xiaoyao Li --- target/i386/confidential-guest.h | 20 ++++++++++---------- target/i386/kvm/kvm.c | 2 +- target/i386/sev.c | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/target/i386/confidential-guest.h b/target/i386/confidential-guest.h index 4b7ea91023dc..2dde29889c23 100644 --- a/target/i386/confidential-guest.h +++ b/target/i386/confidential-guest.h @@ -41,8 +41,8 @@ struct X86ConfidentialGuestClass { int (*kvm_type)(X86ConfidentialGuest *cg); void (*cpu_instance_init)(X86ConfidentialGuest *cg, CPUState *cpu); void (*cpu_realizefn)(X86ConfidentialGuest *cg, CPUState *cpu, Error **errp); - uint32_t (*mask_cpuid_features)(X86ConfidentialGuest *cg, uint32_t feature, uint32_t index, - int reg, uint32_t value); + uint32_t (*adjust_cpuid_features)(X86ConfidentialGuest *cg, uint32_t feature, + uint32_t index, int reg, uint32_t value); }; /** @@ -83,21 +83,21 @@ static inline void x86_confidenetial_guest_cpu_realizefn(X86ConfidentialGuest *c } /** - * x86_confidential_guest_mask_cpuid_features: + * x86_confidential_guest_adjust_cpuid_features: * - * Removes unsupported features from a confidential guest's CPUID values, returns - * the value with the bits removed. The bits removed should be those that KVM - * provides independent of host-supported CPUID features, but are not supported by - * the confidential computing firmware. + * Adjust the supported features from a confidential guest's CPUID values, + * returns the adjusted value. There are bits being removed that are not + * supported by the confidential computing firmware or bits being added that + * are forcibly exposed to guest by the confidential computing firmware. */ -static inline int x86_confidential_guest_mask_cpuid_features(X86ConfidentialGuest *cg, +static inline int x86_confidential_guest_adjust_cpuid_features(X86ConfidentialGuest *cg, uint32_t feature, uint32_t index, int reg, uint32_t value) { X86ConfidentialGuestClass *klass = X86_CONFIDENTIAL_GUEST_GET_CLASS(cg); - if (klass->mask_cpuid_features) { - return klass->mask_cpuid_features(cg, feature, index, reg, value); + if (klass->adjust_cpuid_features) { + return klass->adjust_cpuid_features(cg, feature, index, reg, value); } else { return value; } diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index e47aa32233e6..f067961fba43 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -576,7 +576,7 @@ uint32_t kvm_arch_get_supported_cpuid(KVMState *s, uint32_t function, } if (current_machine->cgs) { - ret = x86_confidential_guest_mask_cpuid_features( + ret = x86_confidential_guest_adjust_cpuid_features( X86_CONFIDENTIAL_GUEST(current_machine->cgs), function, index, reg, ret); } diff --git a/target/i386/sev.c b/target/i386/sev.c index 1a4eb1ada624..4e6582c6a666 100644 --- a/target/i386/sev.c +++ b/target/i386/sev.c @@ -946,7 +946,7 @@ out: } static uint32_t -sev_snp_mask_cpuid_features(X86ConfidentialGuest *cg, uint32_t feature, uint32_t index, +sev_snp_adjust_cpuid_features(X86ConfidentialGuest *cg, uint32_t feature, uint32_t index, int reg, uint32_t value) { switch (feature) { @@ -2404,7 +2404,7 @@ sev_snp_guest_class_init(ObjectClass *oc, void *data) klass->launch_finish = sev_snp_launch_finish; klass->launch_update_data = sev_snp_launch_update_data; klass->kvm_init = sev_snp_kvm_init; - x86_klass->mask_cpuid_features = sev_snp_mask_cpuid_features; + x86_klass->adjust_cpuid_features = sev_snp_adjust_cpuid_features; x86_klass->kvm_type = sev_snp_kvm_type; object_class_property_add(oc, "policy", "uint64", From patchwork Tue Nov 5 06:23:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862455 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0D14B1FDFAF for ; Tue, 5 Nov 2024 06:39:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788785; cv=none; b=Ax0doqHSfPYOlLhO29CI5EHsQYakWLbL6Q0qFKK9u+26553pLYDcGvGx6SrnCW7yIQX1KJZxb21VbLRHHsGEweTqmP3jsWcVBszBs4/jXeGbvpHiYJu5Vj9GA+uWQZGeGBx3xBSbvM3M9tKnKxCDd9uAgrhQwuYaywGx00ceV+w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788785; c=relaxed/simple; bh=xbrsyTH8bvg8xsrUxtQa3cOVPKG1OlwWAfdK7z7v1Zw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nhp4cnLY0ssB8S4XDXneGcjLWXMt2HdSdewhDzTHXsZTdbgaMsPSdA9KgFgGu7dR38P+lHXoAM2IEBYFdWp+mt34TOO9xC4k0sT3uBr9ZSEB25HquCefrlxKPjSmAPla35/leZRgebE+9KebtQ8+jkMYIPim8tKGhTQUe+L1prw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=RavGQxO3; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="RavGQxO3" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788784; x=1762324784; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xbrsyTH8bvg8xsrUxtQa3cOVPKG1OlwWAfdK7z7v1Zw=; b=RavGQxO3pCZaQZiEvB/S3PbCEaUX1svLfSYNKQk4yP0IwxKYDMNzFtFK SZCGLEejusp+ypkvp+fydKOfsbFLVH27zASR9m1OXd9Js4kvBLkJ4cDVi NaK3MSHd2/ADqMK52Bys0LP+hx1MeWQRGWmC1hQS53FFVe3Eka/d6+WQ7 aFnBLowfPhZ77rq+Enil6dxcpGpRjh6fx2giCRBXsxmiHg6XxbaQiuPzw dzUvcrnFYinbnGUCaxom8HS1O1zMWRjF3RHHA+zCHbJuDXRzhADRWbT0n 7mVdD1usLyuJhvr0QfONVS0iLeunA6PZVAffPuj+NkmFCA5XPdjIUbpif g==; X-CSE-ConnectionGUID: ucXqjhK8RlKXxId5ULrXyQ== X-CSE-MsgGUID: 6BNzZuKfQ5SyuKtGUs0tQw== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689836" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689836" 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:39:44 -0800 X-CSE-ConnectionGUID: I1dHo+kSQGaDnrUYcs9Eow== X-CSE-MsgGUID: xUQf/ZneTdyrNfoRVh6f7g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989754" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:39:40 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 47/60] i386/tdx: Implement adjust_cpuid_features() for TDX Date: Tue, 5 Nov 2024 01:23:55 -0500 Message-Id: <20241105062408.3533704-48-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 1. QEMU's support for Intel PT is borken in general, thus doesn't support for TDX. 2. Only limited KVM PV features are supported for TD guest. 3. Drop the AMD specific bits that are reserved on Intel platform. Signed-off-by: Xiaoyao Li --- target/i386/kvm/tdx.c | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 9dcb77e011bd..ba723db92bfe 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -33,6 +33,8 @@ #include "kvm_i386.h" #include "tdx.h" +#include "standard-headers/asm-x86/kvm_para.h" + #define TDX_MIN_TSC_FREQUENCY_KHZ (100 * 1000) #define TDX_MAX_TSC_FREQUENCY_KHZ (10 * 1000 * 1000) @@ -41,6 +43,14 @@ #define TDX_TD_ATTRIBUTES_PKS BIT_ULL(30) #define TDX_TD_ATTRIBUTES_PERFMON BIT_ULL(63) +#define TDX_SUPPORTED_KVM_FEATURES ((1U << KVM_FEATURE_NOP_IO_DELAY) | \ + (1U << KVM_FEATURE_PV_UNHALT) | \ + (1U << KVM_FEATURE_PV_TLB_FLUSH) | \ + (1U << KVM_FEATURE_PV_SEND_IPI) | \ + (1U << KVM_FEATURE_POLL_CONTROL) | \ + (1U << KVM_FEATURE_PV_SCHED_YIELD) | \ + (1U << KVM_FEATURE_MSI_EXT_DEST_ID)) + static TdxGuest *tdx_guest; static struct kvm_tdx_capabilities *tdx_caps; @@ -436,6 +446,39 @@ static void tdx_cpu_realizefn(X86ConfidentialGuest *cg, CPUState *cs, } } +static uint32_t tdx_adjust_cpuid_features(X86ConfidentialGuest *cg, + uint32_t feature, uint32_t index, + int reg, uint32_t value) +{ + switch (feature) { + case 0x7: + if (index == 0 && reg == R_EBX) { + /* QEMU Intel PT support is broken */ + value &= ~CPUID_7_0_EBX_INTEL_PT; + } + break; + case 0x40000001: + if (reg == R_EAX) { + value &= TDX_SUPPORTED_KVM_FEATURES; + } + break; + case 0x80000001: + if (reg == R_EDX) { + value &= ~CPUID_EXT2_AMD_ALIASES; + } + break; + case 0x80000008: + if (reg == R_EBX) { + value &= CPUID_8000_0008_EBX_WBNOINVD; + } + break; + default: + break; + } + + return value; +} + static int tdx_validate_attributes(TdxGuest *tdx, Error **errp) { if ((tdx->attributes & ~tdx_caps->supported_attrs)) { @@ -781,4 +824,5 @@ static void tdx_guest_class_init(ObjectClass *oc, void *data) x86_klass->kvm_type = tdx_kvm_type; x86_klass->cpu_instance_init = tdx_cpu_instance_init; x86_klass->cpu_realizefn = tdx_cpu_realizefn; + x86_klass->adjust_cpuid_features = tdx_adjust_cpuid_features; } From patchwork Tue Nov 5 06:23:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862456 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 50D6E1FE11E for ; Tue, 5 Nov 2024 06:39:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788790; cv=none; b=r5q0JNqkgbVv8KcWOqvCHuyvftl9/MUp3eEHJzuS3ojz0FYfWUI9ur7JbC/v2gVvOF+/IvR+eekNqrGKdjn2KTBvHVf6bYs5sIOW4L+6UlrNjktaO9H+PIx3oP+DHX5znQia/gBdz+BqgxzpQEgEGQ4J/Azds2t8QVyMd5V3eOk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788790; c=relaxed/simple; bh=deJjyJ9mt80ns5ej0bXUJMHjNBnYjzuUgaD7RaYuZHg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mO9r5akyN6bZ6A44df+rMq07FrAscN/wx8ABQp+cb3xFO+49E77lHdhaphfwqyUx1iN8easwkgV/54xPB3Rg0x0F0Du939pAQCfKnpKX1XJ7RAxFHqESPwMK8190uuO3ngNWtWN1RnaLN7zfyphPv9dDi/0XwFr0y8aAKTtVIdA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=bJBmy6Fh; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="bJBmy6Fh" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788788; x=1762324788; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=deJjyJ9mt80ns5ej0bXUJMHjNBnYjzuUgaD7RaYuZHg=; b=bJBmy6FhU3ZeaozG/0bh801nVVf5ZZzzqzlhRHl0kPbH+mN84rneQisC IpEFOPU4PktaEb1GhLwE8NFmnE4DgMyaqw9qKoegrPJ3Ey1fDIWGk2wLC x/GSsmnwGqNvCCX+EDKFX4Iqn1MBy8L3iPhklVv1QUiYugKFSM9CgIq/e KatxsnGuuAOLZVKqTwdPFIcSPi11R8JJZ9Y1Cw6VudPXocQZhgI7ZUG7G aHDe8fau1y9yXrpzC5G7IsaKIRB8HgrQ2y3og7euZw2J/M2Xd8ryb613V jieDCK2rhKkUi75Jy+f/+xcmnjnr8rckeJ8fRoBodfr/jKbFu3HpBPVnQ A==; X-CSE-ConnectionGUID: vuARhwu7R6+nD99reMlyWA== X-CSE-MsgGUID: a3dI9DiHQJqg77KXRIeruA== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689846" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689846" 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:39:48 -0800 X-CSE-ConnectionGUID: DRI0gvHuR1iEyNYSoW/T8w== X-CSE-MsgGUID: XNjENJeDQkSChrVYaaBSCA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989792" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:39:44 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 48/60] i386/tdx: Apply TDX fixed0 and fixed1 information to supported CPUIDs Date: Tue, 5 Nov 2024 01:23:56 -0500 Message-Id: <20241105062408.3533704-49-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 TDX architecture forcibly sets some CPUID bits for TD guest that VMM cannot disable it. It also disallows some CPUID bits though they might be supported for VMX VMs. The fixed0 and fixed1 bits may vary on different TDX module and on different host. It's a huge burden to maintain all combination. To simplify it, hardcode the fixed0 and fixed1 CPUID bits that irrelative with host in QEMU based on a new enough TDX module version. Ideally, future TDX module can expose such fixed0 and fixed1 information via some interface, then KVM can reported them to QEMU. Signed-off-by: Xiaoyao Li --- target/i386/cpu.h | 2 + target/i386/kvm/kvm_i386.h | 6 +++ target/i386/kvm/tdx.c | 102 +++++++++++++++++++++++++++++++++++++ target/i386/sev.c | 5 -- 4 files changed, 110 insertions(+), 5 deletions(-) diff --git a/target/i386/cpu.h b/target/i386/cpu.h index dcc673262c06..8118356af4fc 100644 --- a/target/i386/cpu.h +++ b/target/i386/cpu.h @@ -854,6 +854,8 @@ uint64_t x86_cpu_get_supported_feature_word(X86CPU *cpu, FeatureWord w); #define CPUID_7_0_EBX_SMAP (1U << 20) /* AVX-512 Integer Fused Multiply Add */ #define CPUID_7_0_EBX_AVX512IFMA (1U << 21) +/* PCOMMIT instruction */ +#define CPUID_7_0_EBX_PCOMMIT (1U << 22) /* Flush a Cache Line Optimized */ #define CPUID_7_0_EBX_CLFLUSHOPT (1U << 23) /* Cache Line Write Back */ diff --git a/target/i386/kvm/kvm_i386.h b/target/i386/kvm/kvm_i386.h index b1baf9e7f910..0b8240266dd9 100644 --- a/target/i386/kvm/kvm_i386.h +++ b/target/i386/kvm/kvm_i386.h @@ -12,9 +12,15 @@ #define QEMU_KVM_I386_H #include "sysemu/kvm.h" +#include #define KVM_MAX_CPUID_ENTRIES 100 +typedef struct KvmCpuidInfo { + struct kvm_cpuid2 cpuid; + struct kvm_cpuid_entry2 entries[KVM_MAX_CPUID_ENTRIES]; +} KvmCpuidInfo; + #ifdef CONFIG_KVM #define kvm_pit_in_kernel() \ diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index ba723db92bfe..bc1581d1e43b 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -446,10 +446,100 @@ static void tdx_cpu_realizefn(X86ConfidentialGuest *cg, CPUState *cs, } } +/* + * Fixed0 and Fixed1 bits info are grabbed from TDX 1.5.06 spec. + */ +KvmCpuidInfo tdx_fixed0_bits = { + .cpuid.nent = 3, + .entries[0] = { + .function = 0x1, + .index = 0x0, + .ecx = CPUID_EXT_VMX | CPUID_EXT_SMX, + .edx = CPUID_PSE36 | CPUID_IA64, + }, + .entries[1] = { + .function = 0x7, + .index = 0x0, + .flags = KVM_CPUID_FLAG_SIGNIFCANT_INDEX, + .ebx = CPUID_7_0_EBX_TSC_ADJUST | CPUID_7_0_EBX_SGX | + CPUID_7_0_EBX_PCOMMIT, + .ecx = CPUID_7_0_ECX_SGX_LC | (1U << 15) | (0x1fU << 17) | (1U << 26) | + (1U << 29), + .edx = (1U << 0) | (1U << 1) | (1U << 7) | (1U << 9) | (1U << 11) | + (1U << 12) | (1U << 13) | (1U << 15) | (1U << 17) | (1U << 21), + }, + .entries[2] = { + .function = 0x80000001, + .index = 0x0, + .ecx = 0xFFFFFEDE, + .edx = 0xD3EFF7FF, + }, +}; + +KvmCpuidInfo tdx_fixed1_bits = { + .cpuid.nent = 6, + .entries[0] = { + .function = 0x1, + .index = 0, + .ecx = CPUID_EXT_SSE3 | CPUID_EXT_PCLMULQDQ | CPUID_EXT_DTES64 | + CPUID_EXT_DSCPL | CPUID_EXT_SSE3 | CPUID_EXT_CX16 | + CPUID_EXT_PDCM | CPUID_EXT_PCID | CPUID_EXT_SSE41 | + CPUID_EXT_SSE42 | CPUID_EXT_X2APIC | CPUID_EXT_MOVBE | + CPUID_EXT_POPCNT | CPUID_EXT_AES | CPUID_EXT_XSAVE | + CPUID_EXT_RDRAND | CPUID_EXT_HYPERVISOR, + .edx = CPUID_FP87 | CPUID_VME | CPUID_DE | CPUID_PSE | CPUID_TSC | + CPUID_MSR | CPUID_PAE | CPUID_MCE | CPUID_CX8 | CPUID_APIC | + CPUID_SEP | CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | + CPUID_PAT | CPUID_CLFLUSH | CPUID_DTS | CPUID_MMX | CPUID_FXSR | + CPUID_SSE | CPUID_SSE2, + }, + .entries[1] = { + .function = 0x6, + .index = 0, + .eax = CPUID_6_EAX_ARAT, + }, + .entries[2] = { + .function = 0x7, + .index = 0, + .flags = KVM_CPUID_FLAG_SIGNIFCANT_INDEX, + .ebx = CPUID_7_0_EBX_FSGSBASE | CPUID_7_0_EBX_FDP_EXCPTN_ONLY | + CPUID_7_0_EBX_SMEP | CPUID_7_0_EBX_INVPCID | + CPUID_7_0_EBX_ZERO_FCS_FDS | CPUID_7_0_EBX_RDSEED | + CPUID_7_0_EBX_SMAP | CPUID_7_0_EBX_CLFLUSHOPT | + CPUID_7_0_EBX_CLWB | CPUID_7_0_EBX_SHA_NI, + .ecx = CPUID_7_0_ECX_BUS_LOCK_DETECT | CPUID_7_0_ECX_MOVDIRI | + CPUID_7_0_ECX_MOVDIR64B, + .edx = (1U << 10) | CPUID_7_0_EDX_SPEC_CTRL | CPUID_7_0_EDX_STIBP | + CPUID_7_0_EDX_FLUSH_L1D | CPUID_7_0_EDX_ARCH_CAPABILITIES | + CPUID_7_0_EDX_CORE_CAPABILITY | CPUID_7_0_EDX_SPEC_CTRL_SSBD, + }, + .entries[3] = { + .function = 0x7, + .index = 2, + .flags = KVM_CPUID_FLAG_SIGNIFCANT_INDEX, + .edx = (1U << 0) | (1U << 1) | (1U << 2) | (1U << 4), + }, + .entries[4] = { + .function = 0x80000001, + .index = 0, + .ecx = CPUID_EXT3_LAHF_LM | CPUID_EXT3_ABM | CPUID_EXT3_3DNOWPREFETCH, + .edx = CPUID_EXT2_NX | CPUID_EXT2_PDPE1GB | CPUID_EXT2_RDTSCP | + CPUID_EXT2_LM, + }, + .entries[5] = { + .function = 0x80000007, + .index = 0, + .edx = CPUID_APM_INVTSC, + }, +}; + static uint32_t tdx_adjust_cpuid_features(X86ConfidentialGuest *cg, uint32_t feature, uint32_t index, int reg, uint32_t value) { + struct kvm_cpuid_entry2 *e; + uint32_t fixed0, fixed1; + switch (feature) { case 0x7: if (index == 0 && reg == R_EBX) { @@ -476,6 +566,18 @@ static uint32_t tdx_adjust_cpuid_features(X86ConfidentialGuest *cg, break; } + e = cpuid_find_entry(&tdx_fixed0_bits.cpuid, feature, index); + if (e) { + fixed0 = cpuid_entry_get_reg(e, reg); + value &= ~fixed0; + } + + e = cpuid_find_entry(&tdx_fixed1_bits.cpuid, feature, index); + if (e) { + fixed1 = cpuid_entry_get_reg(e, reg); + value |= fixed1; + } + return value; } diff --git a/target/i386/sev.c b/target/i386/sev.c index 4e6582c6a666..6982a957a6f7 100644 --- a/target/i386/sev.c +++ b/target/i386/sev.c @@ -214,11 +214,6 @@ static const char *const sev_fw_errlist[] = { /* doesn't expose this, so re-use the max from kvm.c */ #define KVM_MAX_CPUID_ENTRIES 100 -typedef struct KvmCpuidInfo { - struct kvm_cpuid2 cpuid; - struct kvm_cpuid_entry2 entries[KVM_MAX_CPUID_ENTRIES]; -} KvmCpuidInfo; - #define SNP_CPUID_FUNCTION_MAXCOUNT 64 #define SNP_CPUID_FUNCTION_UNKNOWN 0xFFFFFFFF From patchwork Tue Nov 5 06:23:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862457 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DC0151FDFAF for ; Tue, 5 Nov 2024 06:39:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788794; cv=none; b=tXK0SfpoQbALvSaDO0akGNa5ILSfemON33ahb4hoYN2X9lhtfQ1nd2iVXgR794Dic04NLEcrdG1BiaHVrlVI1bq8E3kZy8Xq1CP35T9s1o2MHPtn0pso7OlCE8d7+dCnu/R1D9cxfXVwKLpv0Yzcgx+oCOJ/8S/SuhWGgEhfeX4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788794; c=relaxed/simple; bh=st5/vyvnaPY+w7rPqzvPYRhQDfLRvMD4QU7tirPy57w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bXSZqVICH8OKbXzpX4afXjVU1HsapH7bwpSTrEB3ZIwFZsC+sSkUbZ7IPhDtzqVoy680gRDVC/fNipPGAlCItJHXU76FDRgi9Ix4JzEaEftx432xeAwIcBcM4Cggctp0hDeZzQV1B1awyVkKiu3/lAkOUY3V4JQTfv+yDKXSCJY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=RPtoZOi6; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="RPtoZOi6" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788793; x=1762324793; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=st5/vyvnaPY+w7rPqzvPYRhQDfLRvMD4QU7tirPy57w=; b=RPtoZOi6812G48VKY9WxMqPcDohJ4oUh6rfqFlAa9ikuVq5NUILnT7aB 34YLXQtjdRhPKfkx6Y2GAEuyPmOXUhlaIOhrkrylTv7hvbXvHwMBBAYkP 7z8g0rJ8Tk3Tn3OPkH6GVw1FQONmRMTKw85IjzRQD0nUxM2Q9rUpAcLZk acQCuQbXMb9f8mL62LfF2tCu5yP5AWi/pxNdDPRw/zm/XEBVg7EwNZK9i 9/BDMRonEiEwhq9Wf88jAt5drQwjtbILNuh/ns2L1iZ3CEkRm/uEAEe1W i64cneQKZdy/KSzc16tYTi8hYUWtSsW83ii+k70LH28U6Q4i+IJ9r8lfQ g==; X-CSE-ConnectionGUID: vRd2jjP5Szy/xUmrub6oRA== X-CSE-MsgGUID: oFZDQL06QXi2V15lQqTTXw== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689856" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689856" 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:39:53 -0800 X-CSE-ConnectionGUID: 5WJ5xED5SVGuZQVUr6MnSA== X-CSE-MsgGUID: 5It1nMLzSeKHTxiykp0Vsg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989825" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:39:48 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 49/60] i386/tdx: Mask off CPUID bits by unsupported TD Attributes Date: Tue, 5 Nov 2024 01:23:57 -0500 Message-Id: <20241105062408.3533704-50-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 For TDX, some CPUID feature bit is configured via TD attributes. Adjust the supported CPUID to mask off the bit if its matched attribute is unsupported. Signed-off-by: Xiaoyao Li --- target/i386/cpu.h | 4 ++++ target/i386/kvm/tdx.c | 54 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/target/i386/cpu.h b/target/i386/cpu.h index 8118356af4fc..e02e23d972a0 100644 --- a/target/i386/cpu.h +++ b/target/i386/cpu.h @@ -903,6 +903,8 @@ uint64_t x86_cpu_get_supported_feature_word(X86CPU *cpu, FeatureWord w); #define CPUID_7_0_ECX_LA57 (1U << 16) /* Read Processor ID */ #define CPUID_7_0_ECX_RDPID (1U << 22) +/* KeyLocker */ +#define CPUID_7_0_ECX_KeyLocker (1U << 23) /* Bus Lock Debug Exception */ #define CPUID_7_0_ECX_BUS_LOCK_DETECT (1U << 24) /* Cache Line Demote Instruction */ @@ -955,6 +957,8 @@ uint64_t x86_cpu_get_supported_feature_word(X86CPU *cpu, FeatureWord w); #define CPUID_7_1_EAX_AVX_VNNI (1U << 4) /* AVX512 BFloat16 Instruction */ #define CPUID_7_1_EAX_AVX512_BF16 (1U << 5) +/* Linear address space separation */ +#define CPUID_7_1_EAX_LASS (1U << 6) /* CMPCCXADD Instructions */ #define CPUID_7_1_EAX_CMPCCXADD (1U << 7) /* Fast Zero REP MOVS */ diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index bc1581d1e43b..5ac5f93907ca 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -533,6 +533,58 @@ KvmCpuidInfo tdx_fixed1_bits = { }, }; +typedef struct TdxAttrsMap { + uint32_t attr_index; + uint32_t cpuid_leaf; + uint32_t cpuid_subleaf; + int cpuid_reg; + uint32_t feat_mask; +} TdxAttrsMap; + +static TdxAttrsMap tdx_attrs_maps[] = { + {.attr_index = 27, + .cpuid_leaf = 7, + .cpuid_subleaf = 1, + .cpuid_reg = R_EAX, + .feat_mask = CPUID_7_1_EAX_LASS}, + {.attr_index = 30, + .cpuid_leaf = 7, + .cpuid_subleaf = 0, + .cpuid_reg = R_ECX, + .feat_mask = CPUID_7_0_ECX_PKS,}, + {.attr_index = 31, + .cpuid_leaf = 7, + .cpuid_subleaf = 0, + .cpuid_reg = R_ECX, + .feat_mask = CPUID_7_0_ECX_KeyLocker, + }, +}; + +static void tdx_mask_cpuid_by_attrs(uint32_t feature, uint32_t index, + int reg, uint32_t *value) +{ + TdxAttrsMap *map; + uint64_t unavail = 0; + int i; + + for (i = 0; i < ARRAY_SIZE(tdx_attrs_maps); i++) { + map = &tdx_attrs_maps[i]; + + if (feature != map->cpuid_leaf || index != map->cpuid_subleaf || + reg != map->cpuid_reg) { + continue; + } + + if (!((1ULL << map->attr_index) & tdx_caps->supported_attrs)) { + unavail |= map->feat_mask; + } + } + + if (unavail) { + *value &= ~unavail; + } +} + static uint32_t tdx_adjust_cpuid_features(X86ConfidentialGuest *cg, uint32_t feature, uint32_t index, int reg, uint32_t value) @@ -566,6 +618,8 @@ static uint32_t tdx_adjust_cpuid_features(X86ConfidentialGuest *cg, break; } + tdx_mask_cpuid_by_attrs(feature, index, reg, &value); + e = cpuid_find_entry(&tdx_fixed0_bits.cpuid, feature, index); if (e) { fixed0 = cpuid_entry_get_reg(e, reg); From patchwork Tue Nov 5 06:23:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862458 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0B9C41FA275 for ; Tue, 5 Nov 2024 06:39:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788798; cv=none; b=MvWfReVJ1Bpz4jYdd5ZnWL2cJM5SQGl5jkbGg8t8rjt9+vcF6QHTlEIMINL4WaYt3ntyMlm7j+x/zGfehEXbx5ZpBby1G9+Fv6fHnKdAF6rf5Dluq3AwdFiy7p+b5bNYArrEyTQcN1SBTM0KjdEa2AH32tlg6wYEQGXrrhVH1mM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788798; c=relaxed/simple; bh=f1vBUeMjW4fPDV+AM3GOU/JcU0MWDw60zLwDbytS/8c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=uPZ9Pgkvft5WZ6t2jrw772eLmCXfUvKogN0o/CVCKCKdB4VoefUiav1L0G3HBfBMnJk1AM47vf6tJB5StJOKpTY8z28l3LDrumUj7+/JDUgZMZ0HvDPSlWMzMlK1l3HrZXTtoEUIL9Ic4Rz+UzGe11haT4zuhld83HOWJEQrsvI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=WC4Gcjui; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="WC4Gcjui" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788797; x=1762324797; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=f1vBUeMjW4fPDV+AM3GOU/JcU0MWDw60zLwDbytS/8c=; b=WC4Gcjui4C21xZDTB8z9qX3QfEvOTB6riVEBn6B8n76JT1FyoF9Q0BTM UDQ+6R76apUB2BG33kKBNTR8lHXabA9YPB3rlDknyVzSrBpbQgbBwqzUR VL1pFE7jrawbjmxg56mGLss/COTfWWBRZH5XIJPZu98Mcft2DGpBDPnoh gzT82qnr68uVlwFPP7lZ3ipYJY4sHoFk6qbdbNbh6XRu9cdcpWvV5/2Zz xBAyxyAa4cFMvTOS3Ld/2hgqAsNaztopuQODLBj633qeOFGgeO5Or460F vDqxAnrEkDxb8qhl1NvY4+hiDdBSzpzBFCZlSGAlguxYeDwOHMdLYLJwV g==; X-CSE-ConnectionGUID: taZ255foSTaQXWMlC6LCgw== X-CSE-MsgGUID: 9NVXLMWyTXSKHm3FtCCBvg== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689865" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689865" 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:39:57 -0800 X-CSE-ConnectionGUID: AyZ+kzahRb2rPCJpujgR5g== X-CSE-MsgGUID: YIvu0W5xTNuM1XUDVf3m0A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989850" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:39:53 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 50/60] i386/cpu: Move CPUID_XSTATE_XSS_MASK to header file and introduce CPUID_XSTATE_MASK Date: Tue, 5 Nov 2024 01:23:58 -0500 Message-Id: <20241105062408.3533704-51-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 They will be used by TDX. Signed-off-by: Xiaoyao Li --- target/i386/cpu.c | 3 --- target/i386/cpu.h | 5 +++++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/target/i386/cpu.c b/target/i386/cpu.c index 119b38bcb0c1..8c507ad406e7 100644 --- a/target/i386/cpu.c +++ b/target/i386/cpu.c @@ -1827,9 +1827,6 @@ static const X86RegisterInfo32 x86_reg_info_32[CPU_NB_REGS32] = { }; #undef REGISTER -/* CPUID feature bits available in XSS */ -#define CPUID_XSTATE_XSS_MASK (XSTATE_ARCH_LBR_MASK) - ExtSaveArea x86_ext_save_areas[XSAVE_STATE_AREA_COUNT] = { [XSTATE_FP_BIT] = { /* x87 FP state component is always enabled if XSAVE is supported */ diff --git a/target/i386/cpu.h b/target/i386/cpu.h index e02e23d972a0..0cc88c470dfb 100644 --- a/target/i386/cpu.h +++ b/target/i386/cpu.h @@ -621,6 +621,11 @@ typedef enum X86Seg { XSTATE_Hi16_ZMM_MASK | XSTATE_PKRU_MASK | \ XSTATE_XTILE_CFG_MASK | XSTATE_XTILE_DATA_MASK) +/* CPUID feature bits available in XSS */ +#define CPUID_XSTATE_XSS_MASK (XSTATE_ARCH_LBR_MASK) + +#define CPUID_XSTATE_MASK (CPUID_XSTATE_XCR0_MASK | CPUID_XSTATE_XSS_MASK) + /* CPUID feature words */ typedef enum FeatureWord { FEAT_1_EDX, /* CPUID[1].EDX */ From patchwork Tue Nov 5 06:23:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862459 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 42EC81FCF41 for ; Tue, 5 Nov 2024 06:40:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788802; cv=none; b=W60YtSe1nl9+zBzNYsQVR9LHOLClymGqeekVMWeJ2U5pyB8GvGCzU42W4QZlNVhrcQW8NOVcr5LEyqh0y7Zvc6AEowF+/3e+whb3xNMznfHMCFYxoFiNn0CB9nWFpreQRpDdPbNZIrIoOFo5LA2Fl6L6h0Oi/vOTmGeqmo0vqj4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788802; c=relaxed/simple; bh=+RyN2ozpNF9/qWmSSJyynxhc2IsEJ+Gu8thAMTAlGko=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gx5maBoL9EhMy43hA12Ykj+uQcz7Uf03n7+MyhoUYMeVUZCWGNclM6s3L3/bMVgAY803YCtcgA26XpQBBQDpVR/Xk9C6zyJT+IRkrxudnDmp0uKakekGDti3mdzO25MXDnQEZTxHEPoYoJ46GDCXgvgNcSwqUgcoNYYX8gK8XsY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=kaLww7oC; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="kaLww7oC" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788801; x=1762324801; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=+RyN2ozpNF9/qWmSSJyynxhc2IsEJ+Gu8thAMTAlGko=; b=kaLww7oCtsg03d03Wp5U+tIZ3V+opuu9Mwg/MwhrBqAopjWsIwQqowIu Pb9UsknU17peyW2nk+mjVSrbgBJ00GJlSzcRaVazBhfxNx9cxmqOp0h2h VorYgcFKG6WLA++OBnTJ9G7TyiqgZN2MMoKUMlUiaZh5kLe3X4QZ7ECuq RwmeGSefmY/+LfQwLwxdAXRclXkIwkGtBGao68tJ8KBKT27ci7jB2ImC7 m0kgCf9NPSmp6mlonDFePplOQNg8/8dNIbJFvF5zZd5v++3GqJwgmc8ji w880bI4BgxE4/omHDzpDqeJZJKlcXvy5wcY0a1R08EBNETBAXmg9gY5be w==; X-CSE-ConnectionGUID: Br5UUMXoTYKynU+8zphzmA== X-CSE-MsgGUID: RywWLtQHTjS92dxeJbQGWw== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689874" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689874" 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:40:01 -0800 X-CSE-ConnectionGUID: 5jqpsLxmSO+hY5dQHtlrcg== X-CSE-MsgGUID: fhbU8dw3QlKkdGhUX6WkBg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989877" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:39:57 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 51/60] i386/tdx: Mask off CPUID bits by unsupported XFAM Date: Tue, 5 Nov 2024 01:23:59 -0500 Message-Id: <20241105062408.3533704-52-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Mask off the CPUID bits as unsupported if its matched XFAM bit is not supported. Otherwise, it might fail the check in setup_td_xfam() as unsupported XFAM being requested. Signed-off-by: Xiaoyao Li --- target/i386/kvm/tdx.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 5ac5f93907ca..e7e0f073dfc9 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -24,6 +24,7 @@ #include #include "cpu.h" +#include "cpu-internal.h" #include "host-cpu.h" #include "hw/i386/e820_memory_layout.h" #include "hw/i386/x86.h" @@ -585,6 +586,42 @@ static void tdx_mask_cpuid_by_attrs(uint32_t feature, uint32_t index, } } +static void tdx_mask_cpuid_by_xfam(uint32_t feature, uint32_t index, + int reg, uint32_t *value) +{ + const FeatureWordInfo *f; + const ExtSaveArea *esa; + uint64_t unavail = 0; + int i; + + assert(tdx_caps); + + for (i = 0; i < ARRAY_SIZE(x86_ext_save_areas); i++) { + if ((1ULL << i) & tdx_caps->supported_xfam) { + continue; + } + + if (!((1ULL << i) & CPUID_XSTATE_MASK)) { + continue; + } + + esa = &x86_ext_save_areas[i]; + f = &feature_word_info[esa->feature]; + assert(f->type == CPUID_FEATURE_WORD); + if (f->cpuid.eax != feature || + (f->cpuid.needs_ecx && f->cpuid.ecx != index) || + f->cpuid.reg != reg) { + continue; + } + + unavail |= esa->bits; + } + + if (unavail) { + *value &= ~unavail; + } +} + static uint32_t tdx_adjust_cpuid_features(X86ConfidentialGuest *cg, uint32_t feature, uint32_t index, int reg, uint32_t value) @@ -619,6 +656,7 @@ static uint32_t tdx_adjust_cpuid_features(X86ConfidentialGuest *cg, } tdx_mask_cpuid_by_attrs(feature, index, reg, &value); + tdx_mask_cpuid_by_xfam(feature, index, reg, &value); e = cpuid_find_entry(&tdx_fixed0_bits.cpuid, feature, index); if (e) { From patchwork Tue Nov 5 06:24:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862460 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5500C1FCC63 for ; Tue, 5 Nov 2024 06:40:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788806; cv=none; b=Sd/6Mx9naIU29Tl2Pgj/kf5BwsZC/uCMXwNxb683YKbjzeN/cFqvnrfOu2CenKU4O6ceX9MEqRutmOEjspsIpEBO50teq111ViRAL17sn+vq0YDiJjtvmZ91tzCLxo5ZIkKvSdH/Sv+08ziwXEaEbG4bxgDO/uBPRNe3LdX8j/U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788806; c=relaxed/simple; bh=P5Dpgnlq8pIkngj4xnDuBRfdAfvVDs1AjzLSgYZekNU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MLKFmiQDIRicHpTFwfiA3Idd56Y1WeNTzVthcvebnsIxMYKyH75Y7x2TPZjBNUKU3q74Psya+/mZ8VSm3xGTX/+TkbFJsbf06yjEceZdyFXYW6k8KvWAS39HT8ci4KbokRcchWkFzvrtwRlG/D1m6AIuLY+v0nQczadBy+KETMk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=O9LaLOdX; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="O9LaLOdX" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788805; x=1762324805; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=P5Dpgnlq8pIkngj4xnDuBRfdAfvVDs1AjzLSgYZekNU=; b=O9LaLOdXX+os+p0sQX33rYz0KO8C28GhlBHTSZ+1DjFue2TPStiDGmwo PeWB9l4T91Z8QplS+UCWfPPBI8oPkb99t5ut52RtDgzMOwwa8OTdbyele ucBSXl0SShdYARGGeutLMU4ygC01Y22LB43J1YK8AEoGFmnuKwYaIwRdQ I52RGBQK0s7NRQx9Kp8NBhM5qGsZ2+629ACPoMJGePgMR7QLxkPosh2Dm xN6+RAszML0LGOkgRr9ak7gRAE+u1Uyx23ADamctYI17I6xSBr1seEGz3 JNdnSJX3F/kncE+jNMPg/6YUObQuxN7mn/KiEjIIQmeoaGue0xEeqHE8t g==; X-CSE-ConnectionGUID: HXMNOdPHQkauczZRrdDsCg== X-CSE-MsgGUID: zCyF67HgQ/2HkhNKITX3LQ== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689886" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689886" 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:40:05 -0800 X-CSE-ConnectionGUID: c9TSTQY7QYKD76jrtTi9Dw== X-CSE-MsgGUID: zpSR5Yn5SvKva1MN49xPSA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989906" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:40:01 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 52/60] i386/cpu: Expose mark_unavailable_features() for TDX Date: Tue, 5 Nov 2024 01:24:00 -0500 Message-Id: <20241105062408.3533704-53-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Expose mark_unavailable_features() out of cpu.c so that it can be used by TDX when features are masked off. Signed-off-by: Xiaoyao Li --- target/i386/cpu.c | 4 ++-- target/i386/cpu.h | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/target/i386/cpu.c b/target/i386/cpu.c index 8c507ad406e7..e728fb6b9f10 100644 --- a/target/i386/cpu.c +++ b/target/i386/cpu.c @@ -5479,8 +5479,8 @@ static bool x86_cpu_have_filtered_features(X86CPU *cpu) return false; } -static void mark_unavailable_features(X86CPU *cpu, FeatureWord w, uint64_t mask, - const char *verbose_prefix) +void mark_unavailable_features(X86CPU *cpu, FeatureWord w, uint64_t mask, + const char *verbose_prefix) { CPUX86State *env = &cpu->env; FeatureWordInfo *f = &feature_word_info[w]; diff --git a/target/i386/cpu.h b/target/i386/cpu.h index 0cc88c470dfb..e70e7f5ced4b 100644 --- a/target/i386/cpu.h +++ b/target/i386/cpu.h @@ -2444,6 +2444,8 @@ void cpu_set_apic_feature(CPUX86State *env); void host_cpuid(uint32_t function, uint32_t count, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx); bool cpu_has_x2apic_feature(CPUX86State *env); +void mark_unavailable_features(X86CPU *cpu, FeatureWord w, uint64_t mask, + const char *verbose_prefix); static inline bool x86_has_cpuid_0x1f(X86CPU *cpu) { From patchwork Tue Nov 5 06:24:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862461 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6E38119047D for ; Tue, 5 Nov 2024 06:40:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788810; cv=none; b=kfDNjSIzWZzp+obvs5+9tlhHM9M7kuWga6BoNmJAzi9M35fgCDYawOY1smbd5etHRXhWTRgqu6JIsjWRzLLEF3uDXOMx++MxPxUkP84rq+VlWsZE2Zfz05QO725KMqIT5xXt4b8LrXzD+G4y8wzFR5ipWohVEkuaxELnnf40B+s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788810; c=relaxed/simple; bh=Cd6UQJeNLnYBe3u2FXzM6iswu2YcXGcY/0+XpTAWb5M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=l+uVmCczFwj9VOeWrxiROX2PSKoVW+o5n09OfZ7++0SKEW51S7uN+K7Rqn6ybllDr//zb8bOUi6J35QiDaPGO0iCLFehgdDpycwYa/Lohs80pFR8Y2teM6AZCG7JQVo5R6DiRNtDpC9NjjZO61TqYb9uq705uQMbW0m8YtUF5AY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=OweGKFtz; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="OweGKFtz" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788809; x=1762324809; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Cd6UQJeNLnYBe3u2FXzM6iswu2YcXGcY/0+XpTAWb5M=; b=OweGKFtz9olkZSs9FXw8mN9a59SOgvzlwOh4WEbT/CbbzZPrgz2KDti8 13H2rroWiZ/sbEqZsecYKGwJ7upXx9/lWFzZ7igxihg5OyUzbbkhbOE16 +XRYsqFCUgqmEEFMPhB3BzAbEVNXzoio0SK8tJKtnfGGLRYi1bJEZo/Ez Qv6abnY0t5yuniE/fW96Rno5HKDacd0EJXvSQxixykxFR5GIwpOydZBRg WIRV3g+neXZBt/c9CRWLdArtMNhlK3kSXI10nwFC55jBoLMbLut0Ulp2V A/YVIOWoWeNmulmrZ0M1/OXsUdoztPm3FPBIRNbcYv6eETH1Ie9yoFRYG Q==; X-CSE-ConnectionGUID: ywJ03oHARlSUsK5SqaDRQg== X-CSE-MsgGUID: 1+G/l8s7Sy6W0Q/9IgjU5w== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689898" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689898" 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:40:09 -0800 X-CSE-ConnectionGUID: rHLLn7tpR9W0Ru67Q1UL8w== X-CSE-MsgGUID: ttu57Bs2RkOUlOsMPVmdKQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989941" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:40:05 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 53/60] i386/cpu: introduce mark_forced_on_features() Date: Tue, 5 Nov 2024 01:24:01 -0500 Message-Id: <20241105062408.3533704-54-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Signed-off-by: Xiaoyao Li --- target/i386/cpu.c | 29 +++++++++++++++++++++++++++++ target/i386/cpu.h | 5 +++++ 2 files changed, 34 insertions(+) diff --git a/target/i386/cpu.c b/target/i386/cpu.c index e728fb6b9f10..472ab206d8fe 100644 --- a/target/i386/cpu.c +++ b/target/i386/cpu.c @@ -5507,6 +5507,35 @@ void mark_unavailable_features(X86CPU *cpu, FeatureWord w, uint64_t mask, } } +void mark_forced_on_features(X86CPU *cpu, FeatureWord w, uint64_t mask, + const char *verbose_prefix) +{ + CPUX86State *env = &cpu->env; + FeatureWordInfo *f = &feature_word_info[w]; + int i; + + if (!cpu->force_features) { + env->features[w] |= mask; + } + + cpu->forced_on_features[w] |= mask; + + if (!verbose_prefix) { + return; + } + + for (i = 0; i < 64; ++i) { + if ((1ULL << i) & mask) { + g_autofree char *feat_word_str = feature_word_description(f, i); + warn_report("%s: %s%s%s [bit %d]", + verbose_prefix, + feat_word_str, + f->feat_names[i] ? "." : "", + f->feat_names[i] ? f->feat_names[i] : "", i); + } + } +} + static void x86_cpuid_version_get_family(Object *obj, Visitor *v, const char *name, void *opaque, Error **errp) diff --git a/target/i386/cpu.h b/target/i386/cpu.h index e70e7f5ced4b..b5b1c3917427 100644 --- a/target/i386/cpu.h +++ b/target/i386/cpu.h @@ -2135,6 +2135,9 @@ struct ArchCPU { /* Features that were filtered out because of missing host capabilities */ FeatureWordArray filtered_features; + /* Features that are forced enabled by underlying hypervisor, e.g., TDX */ + FeatureWordArray forced_on_features; + /* Enable PMU CPUID bits. This can't be enabled by default yet because * it doesn't have ABI stability guarantees, as it passes all PMU CPUID * bits returned by GET_SUPPORTED_CPUID (that depend on host CPU and kernel @@ -2446,6 +2449,8 @@ void host_cpuid(uint32_t function, uint32_t count, bool cpu_has_x2apic_feature(CPUX86State *env); void mark_unavailable_features(X86CPU *cpu, FeatureWord w, uint64_t mask, const char *verbose_prefix); +void mark_forced_on_features(X86CPU *cpu, FeatureWord w, uint64_t mask, + const char *verbose_prefix); static inline bool x86_has_cpuid_0x1f(X86CPU *cpu) { From patchwork Tue Nov 5 06:24:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862462 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA32719047D for ; Tue, 5 Nov 2024 06:40:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788815; cv=none; b=YjKKQoDiR0Du1XUG884BtcJcMhca5HJtuCxR4QqGEOYXBLMXuZpsfh+yRRpOzA78S5zgCxKbzCmYDtW47KJrHd7mv683krrOFXGKYeNfjFlJZFTt9juHLYxuTPtJKBtttJzDhB2/CFSL+n0puvYMcteYqo2slZg9xFHnUcueCp0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788815; c=relaxed/simple; bh=xs0CRmXKoLP85ntSNgfzBFyenj5am27Jk7rZikXTb+4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Q5H20Gj/6y/JGpkeAyRPoi4Jvu1zMHFYx+5n1QwvKQfWBXaQ2t5F8le9GOkHbQSJ4bQBUd1BzWaLeUNhztGKUGCu8RMqDVCax7bwdFvvnx5kwTc2SrYUnYBgRCd7k6nagzIKyPkRFvOiw2S1geCGL22LDW17qUszqO7scjSkc9E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=DwUKqCtK; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="DwUKqCtK" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788814; x=1762324814; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xs0CRmXKoLP85ntSNgfzBFyenj5am27Jk7rZikXTb+4=; b=DwUKqCtKrdkFqYT6hbYBcAxn9yAdzG5ivZ+IWH5syJgzHUNcCwbqNzzh YvI+MGxtWdmyxhxWZPJuSfGeZ3+3sj15e/v6vFuOoh3SUJSdqOw0s1JbZ EvvzgjqeR0MZg5KUf7zmlpsMYVjbsXe3Fm2XE5b9HyfzvAFB+qxHeV33t R1Xin665fzvYaRGoelmKmvSgd3AzynqF9cgN19Eu8OLHjaq/KqxhENRfx FDQRBNdLoL45mSDrR09H/dY1N2aRIxcgErtl+fX8ToxgE2+2aFp5aF/Rm WDZIKlgOiKUX3iWl32zpURuHG281MDCj/qT6C3xWSNNVh4ite7GjHl9GU g==; X-CSE-ConnectionGUID: wCiV7yJEQV2OJcKNHAcuhA== X-CSE-MsgGUID: DtOL7uDmRCafbJHfPY0yBQ== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689909" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689909" 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:40:14 -0800 X-CSE-ConnectionGUID: +AXT2kK8SDWa5RYfBzoJxw== X-CSE-MsgGUID: xLmUcX+pSb2KsI+RjIq5Og== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83989984" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:40:09 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 54/60] i386/cgs: Introduce x86_confidential_guest_check_features() Date: Tue, 5 Nov 2024 01:24:02 -0500 Message-Id: <20241105062408.3533704-55-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To do cgs specific feature checking. Note the feature checking in x86_cpu_filter_features() is valid for non-cgs VMs. For cgs VMs like TDX, what features can be supported has more restrictions. Signed-off-by: Xiaoyao Li --- target/i386/confidential-guest.h | 13 +++++++++++++ target/i386/kvm/kvm.c | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/target/i386/confidential-guest.h b/target/i386/confidential-guest.h index 2dde29889c23..3018f38e18bf 100644 --- a/target/i386/confidential-guest.h +++ b/target/i386/confidential-guest.h @@ -43,6 +43,7 @@ struct X86ConfidentialGuestClass { void (*cpu_realizefn)(X86ConfidentialGuest *cg, CPUState *cpu, Error **errp); uint32_t (*adjust_cpuid_features)(X86ConfidentialGuest *cg, uint32_t feature, uint32_t index, int reg, uint32_t value); + int (*check_features)(X86ConfidentialGuest *cg, CPUState *cs); }; /** @@ -103,4 +104,16 @@ static inline int x86_confidential_guest_adjust_cpuid_features(X86ConfidentialGu } } +static inline int x86_confidential_guest_check_features(X86ConfidentialGuest *cg, + CPUState *cs) +{ + X86ConfidentialGuestClass *klass = X86_CONFIDENTIAL_GUEST_GET_CLASS(cg); + + if (klass->check_features) { + return klass->check_features(cg, cs); + } + + return 0; +} + #endif diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index f067961fba43..42dc5b78faf0 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -2086,6 +2086,14 @@ int kvm_arch_init_vcpu(CPUState *cs) int r; Error *local_err = NULL; + if (current_machine->cgs) { + r = x86_confidential_guest_check_features( + X86_CONFIDENTIAL_GUEST(current_machine->cgs), cs); + if (r < 0) { + return r; + } + } + memset(&cpuid_data, 0, sizeof(cpuid_data)); cpuid_i = 0; From patchwork Tue Nov 5 06:24:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862463 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F2A5619047D for ; Tue, 5 Nov 2024 06:40:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788819; cv=none; b=FJ32HzMNpLekH9bDLr2K/Lgl2QrOi5sAh5+I9r6r1wSEHE8fEM8dY4ypdTH0rboftMg+zEKNhH4oKUZzqyOm8duKRTyNvJS25QgtCrcuxKCR20+94T57wGoDMfRBYec+L+k15QUd//8O5IMgrI6PaD7JOhHPlzal4r3WdnH3h7Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788819; c=relaxed/simple; bh=R3qGJveZ2fmuHjYuT93ENGzexAuyNwuq1EZaJ0+cqc0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RfD3H3T5YMfjvxG3WU4IecUqV7wDx5HbMNU8tYhDuzvIANxAfSZc2rddziU9wpc2JCRXePjLDA42EKQ9MvkO6z0GilNTlqaLtpWkrvTRanXRldZTPgoB2cAN2Im/7R57VS2/ThiP9w1UYrSPwwSN0SJQ6JqkPL/NERtf8Diigf8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=nU4BAzbw; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="nU4BAzbw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788818; x=1762324818; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=R3qGJveZ2fmuHjYuT93ENGzexAuyNwuq1EZaJ0+cqc0=; b=nU4BAzbwUw6Dijc2oR1g8ZxpUtnnU5RiUx/ogKM6jIldaO9Zgs0hR6gJ wjzQecShgsMeHhpDPqmzYDTFxvShj5VLaAmZIGA0i/EPiCEPL5Kwf775e HRjMFj+ZUnYkF9uKhosUVLD6768Qy2qDfGlpJDpgUJTR8jSe3t6Pmgn3Q b+Lr63iz99SsULfaulFkUEtLXSDnx+nFAqcIx2TSqmHbYByQFJd4947hw MhySCLJcSa14+F8WDOEfKrhIk5/YsHPzu3tWPqi1URjjzUz5dsV9gkVlC MEhe29SFr2+Wnz97RaGq5jBWSkV9nczrBZeCAf0RIRqRtsovJD/D4cd2G Q==; X-CSE-ConnectionGUID: dd2wNkVEQJq6qAIviLXmCQ== X-CSE-MsgGUID: uHlpKDPpRL2noHRwlQQc7g== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689919" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689919" 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:40:18 -0800 X-CSE-ConnectionGUID: cCHE70fzS4uPY/tjfHAGjA== X-CSE-MsgGUID: 4cVcz/YWTguoW1CHzdYfFA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83990026" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:40:14 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 55/60] i386/tdx: Fetch and validate CPUID of TD guest Date: Tue, 5 Nov 2024 01:24:03 -0500 Message-Id: <20241105062408.3533704-56-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Use KVM_TDX_GET_CPUID to get the CPUIDs that are managed and enfored by TDX module for TD guest. Check QEMU's configuration against the fetched data. Print wanring message when 1. a feature is not supported but requested by QEMU or 2. QEMU doesn't want to expose a feature while it is enforced enabled. - If cpu->enforced_cpuid is not set, prints the warning message of both 1) and 2) and tweak QEMU's configuration. - If cpu->enforced_cpuid is set, quit if any case of 1) or 2). Signed-off-by: Xiaoyao Li --- target/i386/kvm/tdx.c | 81 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index e7e0f073dfc9..9cb099e160e4 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -673,6 +673,86 @@ static uint32_t tdx_adjust_cpuid_features(X86ConfidentialGuest *cg, return value; } + +static void tdx_fetch_cpuid(CPUState *cpu, struct kvm_cpuid2 *fetch_cpuid) +{ + int r; + + r = tdx_vcpu_ioctl(cpu, KVM_TDX_GET_CPUID, 0, fetch_cpuid); + if (r) { + error_report("KVM_TDX_GET_CPUID failed %s", strerror(-r)); + exit(1); + } +} + +static int tdx_check_features(X86ConfidentialGuest *cg, CPUState *cs) +{ + uint64_t actual, requested, unavailable, forced_on; + g_autofree struct kvm_cpuid2 *fetch_cpuid; + const char *forced_on_prefix = NULL; + const char *unav_prefix = NULL; + struct kvm_cpuid_entry2 *entry; + X86CPU *cpu = X86_CPU(cs); + CPUX86State *env = &cpu->env; + FeatureWordInfo *wi; + FeatureWord w; + bool mismatch = false; + + fetch_cpuid = g_malloc0(sizeof(*fetch_cpuid) + + sizeof(struct kvm_cpuid_entry2) * KVM_MAX_CPUID_ENTRIES); + tdx_fetch_cpuid(cs, fetch_cpuid); + + if (cpu->check_cpuid || cpu->enforce_cpuid) { + unav_prefix = "TDX doesn't support requested feature"; + forced_on_prefix = "TDX forcibly sets the feature"; + } + + for (w = 0; w < FEATURE_WORDS; w++) { + wi = &feature_word_info[w]; + actual = 0; + + switch (wi->type) { + case CPUID_FEATURE_WORD: + entry = cpuid_find_entry(fetch_cpuid, wi->cpuid.eax, wi->cpuid.ecx); + if (!entry) { + /* + * If KVM doesn't report it means it's totally configurable + * by QEMU + */ + continue; + } + + actual = cpuid_entry_get_reg(entry, wi->cpuid.reg); + break; + case MSR_FEATURE_WORD: + /* + * TODO: + * validate MSR features when KVM has interface report them. + */ + continue; + } + + requested = env->features[w]; + unavailable = requested & ~actual; + mark_unavailable_features(cpu, w, unavailable, unav_prefix); + if (unavailable) { + mismatch = true; + } + + forced_on = actual & ~requested; + mark_forced_on_features(cpu, w, forced_on, forced_on_prefix); + if (forced_on) { + mismatch = true; + } + } + + if (cpu->enforce_cpuid && mismatch) { + return -1; + } + + return 0; +} + static int tdx_validate_attributes(TdxGuest *tdx, Error **errp) { if ((tdx->attributes & ~tdx_caps->supported_attrs)) { @@ -1019,4 +1099,5 @@ static void tdx_guest_class_init(ObjectClass *oc, void *data) x86_klass->cpu_instance_init = tdx_cpu_instance_init; x86_klass->cpu_realizefn = tdx_cpu_realizefn; x86_klass->adjust_cpuid_features = tdx_adjust_cpuid_features; + x86_klass->check_features = tdx_check_features; } From patchwork Tue Nov 5 06:24:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862464 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F35C1FDF9B for ; Tue, 5 Nov 2024 06:40:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788823; cv=none; b=NOa/Jfcricq11xxwYkndru48EulOuL2yZJob8XSXJZzvnu9Sfwq4pcoyKTlq/HfadDJy8yLm4jwD0OJ/XEnrEaNSeyt9rE64Pza2A8/BgotIwL8C0MrhnNOUxgvLtjpd926kwcog6FLT861mWbDGqiiKSU1WWM3clykuSdH5/Jc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788823; c=relaxed/simple; bh=N6wZGF3LbJuiZwh2h+P0qnuOeIsvhZtITe8MucmhR3Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NwZm2LhlSXsNqZbQKIBmJBBGmGxP19/ppyo04pBAH9uszNL4fZcucyHOS2ED+pHPFsLZ8teNw9pCC3BPQDH0hzBMdOR+mIMwP7uoj957sAtsNydx2fsP0IRYIpGeFIqBAh57SawFYuzgmjpi22FAdQPBIbO4pCL0MVCZ1qZo3cs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=KRdNBOA+; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="KRdNBOA+" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788822; x=1762324822; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=N6wZGF3LbJuiZwh2h+P0qnuOeIsvhZtITe8MucmhR3Y=; b=KRdNBOA+jId0z3h/xuhv3xGTPZzgwx8CYvagqpCcbuCFakjmlGnXp9dW qL+VSI5QRjA4kSe6fak6faGtC+KY0OKvS1xR0x3j2efWEtpfuvZ3HLPp3 VZaKjrtHumHPmAKTISn6QA9JG1QO+/yNyZnwU5mqeJjvlOQNi5LEjcQkn LGFsiTsKQ2W2alfR0xlMjP6DhPFAA/nJYGA0zEexgqPlmNnF5a/GLniUO xJpya1VshM2+XNH0VRh4x/+otVJ74YGpu5KLXH4mEDvlmpZf8GE2DULSw vHEmpZ2O0MraTUW3IWP5niUOAgnefqWQLD6iwi7VK34dtBsZQcJEfQ8op Q==; X-CSE-ConnectionGUID: zQtDtrbQT16KEBIP56BcFA== X-CSE-MsgGUID: pecEVBq2QXOcelc6wqsdIw== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689927" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689927" 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:40:22 -0800 X-CSE-ConnectionGUID: cui/g9wYSTW/R2q/F+xxag== X-CSE-MsgGUID: BO/sHFRmRSK/ivSD8JW1yA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83990064" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:40:18 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 56/60] i386/tdx: Don't treat SYSCALL as unavailable Date: Tue, 5 Nov 2024 01:24:04 -0500 Message-Id: <20241105062408.3533704-57-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Signed-off-by: Xiaoyao Li --- target/i386/kvm/tdx.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 9cb099e160e4..05475edf72bd 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -734,6 +734,13 @@ static int tdx_check_features(X86ConfidentialGuest *cg, CPUState *cs) requested = env->features[w]; unavailable = requested & ~actual; + /* + * Intel enumerates SYSCALL bit as 1 only when processor in 64-bit + * mode and before vcpu running it's not in 64-bit mode. + */ + if (w == FEAT_8000_0001_EDX && unavailable & CPUID_EXT2_SYSCALL) { + unavailable &= ~CPUID_EXT2_SYSCALL; + } mark_unavailable_features(cpu, w, unavailable, unav_prefix); if (unavailable) { mismatch = true; From patchwork Tue Nov 5 06:24:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862465 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 74C731FDFBB for ; Tue, 5 Nov 2024 06:40:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788827; cv=none; b=JfUx1Jl72DtRFiBmSy5BVPw8ohKGzCZd2A/qsSOuwcOkajYQy5B5pP9QRr51DavyflZOMdfW8086nfv1tZfVzYiodGlaeGd2N0h3spNE2+aiZvQpeqxIChYDdWc+vsgsSn6XZVmQG6ZDWyC50MmjwVjcw2UllSVj8rfZYIHID6g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788827; c=relaxed/simple; bh=NKlDvzzDpP7N59sc27fzM1CZSPqI8zTcm+9UeKAwP8w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fSC0wrGJSfzEHls+aGvynG2/gdmM6s0ER4VSoIx0Myc3Frs06Ov+4LmGAOXAklJ+pq2CeZZwwYIHG1bDmTV/IqsOfO5xTH83wuVpCkGmoURIbuc0UMqJ9oJcS7xFtEPFBajN/Mx7bgppTGpI3g14IkDD93PGBRsI2eIOC8st8sA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=hQteR0xV; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="hQteR0xV" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788826; x=1762324826; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=NKlDvzzDpP7N59sc27fzM1CZSPqI8zTcm+9UeKAwP8w=; b=hQteR0xV+v/PX5w7rjfNCQxUR5dXghCGA18Ma0973F6ZpUBUlR4AAW98 YsWx3Z/CHZLYWetLE5/lb0avWbO1QyCpHjf0UmADstRr4vAOmL3HlYF41 nV0kVg4DwyL95RUFW9+/9W4XxdaGdU2jYWa5d+inYdHNEUf7ZN9DlSo0b 57LqmqNB4QwsYyhN0z0J4kVXIEMTDn7K7d+UYMTb7V3OdHRIpvjoOJ7u8 qv1qdJ7JgzFGM1mVvYBR4A0Gut6XhgaVv7w8B9fdt2iQWMvx4vRwGFdaL hfgDhG37CKKfkXTxooNx2Ieq+nh6NZkSc0XGZXS1ZRwZq9CLl7zD3M0Rk w==; X-CSE-ConnectionGUID: P6yX3spURx+clIjiSlNtRg== X-CSE-MsgGUID: HPQFBOxiSECXJSUM1IetFQ== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689938" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689938" 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:40:26 -0800 X-CSE-ConnectionGUID: fMooDLw8TVGkCNYMWGss5g== X-CSE-MsgGUID: Is1f+4fSQ9Kp08bHbbfQaw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83990085" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:40:22 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 57/60] i386/tdx: Make invtsc default on Date: Tue, 5 Nov 2024 01:24:05 -0500 Message-Id: <20241105062408.3533704-58-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Because it's fixed1 bit that enforced by TDX module. Signed-off-by: Xiaoyao Li --- target/i386/kvm/tdx.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c index 05475edf72bd..4cb1f4ac3479 100644 --- a/target/i386/kvm/tdx.c +++ b/target/i386/kvm/tdx.c @@ -430,6 +430,9 @@ static void tdx_cpu_instance_init(X86ConfidentialGuest *cg, CPUState *cpu) object_property_set_bool(OBJECT(cpu), "pmu", false, &error_abort); + /* invtsc is fixed1 for TD guest */ + object_property_set_bool(OBJECT(cpu), "invtsc", true, &error_abort); + x86cpu->enable_cpuid_0x1f = true; } From patchwork Tue Nov 5 06:24:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862466 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C24941FE0F1 for ; Tue, 5 Nov 2024 06:40:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788832; cv=none; b=F4ovdWgiM3zttEv+2Py7tYYGVXe6Jkw080SWmlp3qYKuDzEe/lH4eMd5FgY0Obv0iC26X+hoP2uBTihLpRqYdn5K1wv+1vVZ6XOanjLpFFGdM6xFl40qRL2W13IPV86x/ISrV7e/+uQAMhE6SzQYAYqueWG0X7HDaO/WX31aPqg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788832; c=relaxed/simple; bh=Pfq6Mcq11nNik95S/q8D/VnbY9hYdtshEgdTuCa1i80=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QRM/OpUZOry0xEYoY3zDkLX1yC5GifUtqfi6+HL6xgmd82zmQJNf4deLX/8+osCAvnceK3aXk1X5+g0Tm1a8icsBvqgxHBLuVwxuH77FF5ZrGpT4+MsGpTLdO/R+RoaqB8aZPyruiSDW6VjeaRNKCIFTfwEyJWUmjA33HGOkSTM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=KPA27s+Z; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="KPA27s+Z" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788831; x=1762324831; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Pfq6Mcq11nNik95S/q8D/VnbY9hYdtshEgdTuCa1i80=; b=KPA27s+Zs6pjKv7J8BiSeSaYLOmioxj8R2L/OgXCjpyChneRlVHoOl9V cW16UteI1WRahhxBjETxXv+DphfnS/KealIybN5unuUKioKPyKQtqcQar nJtrExiS4zHGRfbPsoVVYVcMC4GakYQPOjtD7Lw7qaXNe/PsAKfGlRPLN vzoOEvYclhctCah9x2UMPcrgtULFrgS/VQEzGw1hdA1+HpxX4atWBYv1D g5kbmNbTJTzlg8LdxYOORQVz6yGK0Sfne1bTMSPITeSFb2w3fElPZL08c Vfcat0DrTFYZeHOJhHmxUeiJaBwRn68cjXhChcllse/2qaX+Pjjk5PuQV w==; X-CSE-ConnectionGUID: HwY+tuDCRlu4NBnmk8Utqw== X-CSE-MsgGUID: 3BH4l/8NQMCe1vgBIPKLvQ== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689953" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689953" 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:40:31 -0800 X-CSE-ConnectionGUID: 1JcGSuUsQHiwhLm+UNgIxA== X-CSE-MsgGUID: 7h2gpC2BS6GDQQ3w3SMvwg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83990113" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:40:26 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 58/60] cpu: Introduce qemu_early_init_vcpu() Date: Tue, 5 Nov 2024 01:24:06 -0500 Message-Id: <20241105062408.3533704-59-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Currently cpu->nr_cores and cpu->nr_threads are initialized in qemu_init_vcpu(), which is called a bit late in *cpu_realizefn() for each ARCHes. x86 arch would like to set CPUID_HT in env->features[FEAT_1_EDX] based on the value of cpu->nr_threads * cpu->nr_cores. It requires nr_cores and nr_threads being initialized earlier. Introdue qemu_early_init_vcpu() for this purpose. Signed-off-by: Xiaoyao Li --- accel/tcg/user-exec-stub.c | 4 ++++ include/hw/core/cpu.h | 8 ++++++++ system/cpus.c | 8 ++++++++ 3 files changed, 20 insertions(+) diff --git a/accel/tcg/user-exec-stub.c b/accel/tcg/user-exec-stub.c index 4fbe2dbdc883..64baf917b55c 100644 --- a/accel/tcg/user-exec-stub.c +++ b/accel/tcg/user-exec-stub.c @@ -10,6 +10,10 @@ void cpu_remove_sync(CPUState *cpu) { } +void qemu_early_init_vcpu(CPUState *cpu) +{ +} + void qemu_init_vcpu(CPUState *cpu) { } diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h index c3ca0babcb3f..854b244e1ad6 100644 --- a/include/hw/core/cpu.h +++ b/include/hw/core/cpu.h @@ -1063,6 +1063,14 @@ void start_exclusive(void); */ void end_exclusive(void); +/** + * qemu_early_init_vcpu: + * @cpu: The vCPU to initialize. + * + * Early initializes a vCPU. + */ +void qemu_early_init_vcpu(CPUState *cpu); + /** * qemu_init_vcpu: * @cpu: The vCPU to initialize. diff --git a/system/cpus.c b/system/cpus.c index 1c818ff6828c..98cb8aafa50b 100644 --- a/system/cpus.c +++ b/system/cpus.c @@ -662,6 +662,14 @@ const AccelOpsClass *cpus_get_accel(void) return cpus_accel; } +void qemu_early_init_vcpu(CPUState *cpu) +{ + MachineState *ms = MACHINE(qdev_get_machine()); + + cpu->nr_cores = machine_topo_get_cores_per_socket(ms); + cpu->nr_threads = ms->smp.threads; +} + void qemu_init_vcpu(CPUState *cpu) { MachineState *ms = MACHINE(qdev_get_machine()); From patchwork Tue Nov 5 06:24:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862467 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CE6481FE0F1 for ; Tue, 5 Nov 2024 06:40:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788836; cv=none; b=jEAEyeDxRZeLdYd9Fm4a4hxCKQfy671tleFNx8rLOELTRpPrqkJPvIsH911OngOkqrL66FtbBrJnqyORplKVNt0hj7BSWj+In1QvTUWs9lt9GfFKg7Nupu4aUjl8CEmNrLSeE+JzUe/zbkd/xDb6Uu8s9ZDdKtv6qHjTWqGC8JE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788836; c=relaxed/simple; bh=oRT7vJ7+68FbrD5uefrUrGWOgNBDYndBP8Hc2+4+qeY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mQBpJEt8MrGR9vGUR47wOTfrNSre/d8QTf1T1b4X80Zr1C74ure9DZo8ujOz08tvn0zjfBVxgIhTZTe5M6pyJyiDbKXeYMrV6Tja+sLj4nChHzc5JnyS0kgXhPyIQooVSNBGCOYLZSgJNdCz7qTqf1Mvk4ZOTIWD/kGIkPU2dIw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=YpJiD8Kd; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="YpJiD8Kd" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788835; x=1762324835; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=oRT7vJ7+68FbrD5uefrUrGWOgNBDYndBP8Hc2+4+qeY=; b=YpJiD8KdCeb0XfSOgvNA+KxmnhLK/z6S3jz4auqnZw/uyqsR5ojnuMrj 3e+Nk/n81o5ZF1ak5gdUAx0qrw9WbZLUjvKVnppDXECVxnGjZh8ifEk4L Y0SUr9baBJb2qhtNZSGEIyiM4eS7tSUUIlKfUvhaDVZN4RMamtsKoPOoJ zkBo+QBI2lu2RFNP1apu6kKwzYcjH8nQGVr+19MuKsGUIAcGXQFA4L49G tI8lOD1u4k8aX1ad4THpZiqCaENg21frHBCQPhjk5os6uY7F+xA0P2dUV MpcOI+FbDy9Smn9JLfOMd5Ywuz0fv31G11/3yo50w0W8J/WvJkr2zLoR7 A==; X-CSE-ConnectionGUID: dRKmdDvhSWG7w1whxm1lng== X-CSE-MsgGUID: 7J+FhkQCTDOkAYn33Xihow== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689964" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689964" 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:40:35 -0800 X-CSE-ConnectionGUID: LzAzyvrmS+yTZ8bmyu8w1Q== X-CSE-MsgGUID: 2ERfMEY6TBCvNuxBGJjdvw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83990148" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:40:31 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 59/60] i386/cpu: Set up CPUID_HT in x86_cpu_realizefn() instead of cpu_x86_cpuid() Date: Tue, 5 Nov 2024 01:24:07 -0500 Message-Id: <20241105062408.3533704-60-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Otherwise, it gets warnings like below when number of vcpus > 1: warning: TDX enforces set the feature: CPUID.01H:EDX.ht [bit 28] This is because x86_confidential_guest_check_features() checks env->features[] instead of the cpuid date set up by cpu_x86_cpuid() Signed-off-by: Xiaoyao Li --- target/i386/cpu.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/target/i386/cpu.c b/target/i386/cpu.c index 472ab206d8fe..214a1b00a815 100644 --- a/target/i386/cpu.c +++ b/target/i386/cpu.c @@ -6571,7 +6571,6 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *edx = env->features[FEAT_1_EDX]; if (threads_per_pkg > 1) { *ebx |= threads_per_pkg << 16; - *edx |= CPUID_HT; } if (!cpu->enable_pmu) { *ecx &= ~CPUID_EXT_PDCM; @@ -7784,6 +7783,8 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp) Error *local_err = NULL; unsigned requested_lbr_fmt; + qemu_early_init_vcpu(cs); + #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY) /* Use pc-relative instructions in system-mode */ tcg_cflags_set(cs, CF_PCREL); @@ -7851,6 +7852,14 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp) } } + /* + * It needs to called after feature filter because KVM doesn't report HT + * as supported + */ + if (cs->nr_cores * cs->nr_threads > 1) { + env->features[FEAT_1_EDX] |= CPUID_HT; + } + /* On AMD CPUs, some CPUID[8000_0001].EDX bits must match the bits on * CPUID[1].EDX. */ From patchwork Tue Nov 5 06:24:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13862468 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 32AB81FE0F7 for ; Tue, 5 Nov 2024 06:40:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788840; cv=none; b=SxfaSws+zrw861V3eJoT8WGJZh79aUBl1/xK8HjnCUnGqQ/bgUih+ognvJ5jzjeNkhZ/8ajYY8NU2+zwPkt/mX0kcP8qNi3v7ax9hJMV9bG+Dt11MJJcqpQt6VcDWNGuPUXZTFw9sZZY3nRWjOzO1dk9pmv78YCUEs8XmKHRZ8Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730788840; c=relaxed/simple; bh=prNelkC3OOqKiNVm+vMpEf1u/DtP+lyJhK2V/YRgjzw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eJMPV7ouYu2DIh8jjO3qsxK9dhjYFelIRBH+zcvvaDNDdNMfrbYb1edE05cJ52BMOuH49j6GICRsVTL3tHfkowX0QRPOPlWgBMd9Me2EEUtexmEOz2VvtoKdhDiwFo+GH8NeTY1NlJbjmwF/3lI/EBArSEr+u7gGaNkAIHLRsKg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=R23SNX0y; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="R23SNX0y" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730788839; x=1762324839; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=prNelkC3OOqKiNVm+vMpEf1u/DtP+lyJhK2V/YRgjzw=; b=R23SNX0yuEWcy2JzpvKbzAj6I4/IUrIdz9BbRFk08qzjgwAN2UamSAX+ IB4dJ9g/c5dhBMG5Amo2ps2MNYhO7rg5wBz09PUx0Nd7LmnCiJjlKRjPi P35YJYctXRT8cwn+P/aOzG54vlNWHYg3d3mZqo6Ptss9A/OqUHut1WVeD 2+onUuS50gbAMNZYIpphEsPEgmP95Pz4XQiKoyxRNPyCskDOKMlp8a0Hj t86MKvqkSfUg5EVMZs8WHqlItKjYxBBlLQxczPDLQoJ0N54VYvQWrjgQ1 h3YUHDtyj3fX+KUAGYrGp+VaefBydgJWyXwKQUQT2uFzjJ+W4yyF6NrxR w==; X-CSE-ConnectionGUID: PmpHLHdCRP+fyp+5lgHdLg== X-CSE-MsgGUID: ZaEsmD9JSlC1B1TR5/f1Ow== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="30689975" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="30689975" 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:40:39 -0800 X-CSE-ConnectionGUID: WC4TK0djRPSDZtiizpWwBg== X-CSE-MsgGUID: +YDffLFqRz2Vf56nozSFLA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,259,1725346800"; d="scan'208";a="83990158" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmviesa009.fm.intel.com with ESMTP; 04 Nov 2024 22:40:35 -0800 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_Mathieu-Daud=C3=A9?= , Yanan Wang , Cornelia Huck , =?utf-8?q?Daniel_P=2E_Berrang=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 60/60] docs: Add TDX documentation Date: Tue, 5 Nov 2024 01:24:08 -0500 Message-Id: <20241105062408.3533704-61-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add docs/system/i386/tdx.rst for TDX support, and add tdx in confidential-guest-support.rst Signed-off-by: Xiaoyao Li --- Changes in v6: - Add more information of "Feature configuration" - Mark TD Attestation as future work because KVM now drops the support of it. Changes in v5: - Add TD attestation section and update the QEMU parameter; Changes since v1: - Add prerequisite of private gmem; - update example command to launch TD; Changes since RFC v4: - add the restriction that kernel-irqchip must be split --- docs/system/confidential-guest-support.rst | 1 + docs/system/i386/tdx.rst | 155 +++++++++++++++++++++ docs/system/target-i386.rst | 1 + 3 files changed, 157 insertions(+) create mode 100644 docs/system/i386/tdx.rst diff --git a/docs/system/confidential-guest-support.rst b/docs/system/confidential-guest-support.rst index 0c490dbda2b7..66129fbab64c 100644 --- a/docs/system/confidential-guest-support.rst +++ b/docs/system/confidential-guest-support.rst @@ -38,6 +38,7 @@ Supported mechanisms Currently supported confidential guest mechanisms are: * AMD Secure Encrypted Virtualization (SEV) (see :doc:`i386/amd-memory-encryption`) +* Intel Trust Domain Extension (TDX) (see :doc:`i386/tdx`) * POWER Protected Execution Facility (PEF) (see :ref:`power-papr-protected-execution-facility-pef`) * s390x Protected Virtualization (PV) (see :doc:`s390x/protvirt`) diff --git a/docs/system/i386/tdx.rst b/docs/system/i386/tdx.rst new file mode 100644 index 000000000000..60106b29bf72 --- /dev/null +++ b/docs/system/i386/tdx.rst @@ -0,0 +1,155 @@ +Intel Trusted Domain eXtension (TDX) +==================================== + +Intel Trusted Domain eXtensions (TDX) refers to an Intel technology that extends +Virtual Machine Extensions (VMX) and Multi-Key Total Memory Encryption (MKTME) +with a new kind of virtual machine guest called a Trust Domain (TD). A TD runs +in a CPU mode that is designed to protect the confidentiality of its memory +contents and its CPU state from any other software, including the hosting +Virtual Machine Monitor (VMM), unless explicitly shared by the TD itself. + +Prerequisites +------------- + +To run TD, the physical machine needs to have TDX module loaded and initialized +while KVM hypervisor has TDX support and has TDX enabled. If those requirements +are met, the ``KVM_CAP_VM_TYPES`` will report the support of ``KVM_X86_TDX_VM``. + +Trust Domain Virtual Firmware (TDVF) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Trust Domain Virtual Firmware (TDVF) is required to provide TD services to boot +TD Guest OS. TDVF needs to be copied to guest private memory and measured before +the TD boots. + +KVM vcpu ioctl ``KVM_TDX_INIT_MEM_REGION`` can be used to populates the TDVF +content into its private memory. + +Since TDX doesn't support readonly memslot, TDVF cannot be mapped as pflash +device and it actually works as RAM. "-bios" option is chosen to load TDVF. + +OVMF is the opensource firmware that implements the TDVF support. Thus the +command line to specify and load TDVF is ``-bios OVMF.fd`` + +Feature Configuration +--------------------- + +Unlike non-TDX VM, the CPU features (enumerated by CPU or MSR) of a TD is not +under full control of VMM. VMM can only configure part of features of a TD on +``KVM_TDX_INIT_VM`` command of VM scope ``MEMORY_ENCRYPT_OP`` ioctl. + +The configurable features have three types: + +- Attributes: + - PKS (bit 30) controls whether Supervisor Protection Keys is exposed to TD, + which determines related CPUID bit and CR4 bit; + - PERFMON (bit 63) controls whether PMU is exposed to TD. + +- XSAVE related features (XFAM): + XFAM is a 64b mask, which has the same format as XCR0 or IA32_XSS MSR. It + determines the set of extended features available for use by the guest TD. + +- CPUID features: + Only some bits of some CPUID leaves are directly configurable by VMM. + +What features can be configured is reported via TDX capabilities. + +TDX capabilities +~~~~~~~~~~~~~~~~ + +The VM scope ``MEMORY_ENCRYPT_OP`` ioctl provides command ``KVM_TDX_CAPABILITIES`` +to get the TDX capabilities from KVM. It returns a data structure of +``struct kvm_tdx_capabilities``, which tells the supported configuration of +attributes, XFAM and CPUIDs. + +TD attributes +~~~~~~~~~~~~~ + +QEMU supports configuring raw 64-bit TD attributes directly via "attributes" +property of "tdx-guest" object. Note, it's users' responsibility to provide a +valid value because some bits may not supported by current QEMU or KVM yet. + +QEMU also supports the configuration of individual attribute bits that are +supported by it, via propertyies of "tdx-guest" object. +E.g., "sept-ve-disable" (bit 63). + +MSR based features +~~~~~~~~~~~~~~~~ + +Current KVM doesn't support MSR based feature (e.g., MSR_IA32_ARCH_CAPABILITIES) +configuration for TDX, and it's a future work to enable it in QEMU when KVM adds +support of it. + +Feature check +~~~~~~~~~~~~~ + +QEMU checks if the final (CPU) features, determined by given cpu model and +explicit feature adjustment of "+featureA/-featureB", can be supported or not. +It can produce feature not supported warnning like + + "warning: host doesn't support requested feature: CPUID.07H:EBX.intel-pt [bit 25]" + +It will also procude warning like + + "warning: TDX forcibly sets the feature: CPUID.80000007H:EDX.invtsc [bit 8]" + +if the fixed-1 feature is requested to be disabled explicitly. This is newly +added to QEMU for TDX because TDX has fixed-1 features that are enfored enabled +by TDX module and VMM cannot disable them. + +Launching a TD (TDX VM) +----------------------- + +To launch a TDX guest, below are new added and required: + +.. parsed-literal:: + + |qemu_system_x86| \\ + -object tdx-guest,id=tdx0 \\ + -machine ...,kernel-irqchip=split,confidential-guest-support=tdx0 \\ + -bios OVMF.fd \\ + +restrictions +------------ + + - kernel-irqchip must be split; + + - No readonly support for private memory; + + - No SMM support: SMM support requires manipulating the guset register states + which is not allowed; + +Debugging +--------- + +Bit 0 of TD attributes, is DEBUG bit, which decides if the TD runs in off-TD +debug mode. When in off-TD debug mode, TD's VCPU state and private memory are +accessible via given SEAMCALLs. This requires KVM to expose APIs to invoke those +SEAMCALLs and resonponding QEMU change. + +It's targeted as future work. + +TD attestation +-------------- + +In TD guest, the attestation process is used to verify the TDX guest +trustworthiness to other entities before provisioning secrets to the guest. + +TD attestation is initiated first by calling TDG.MR.REPORT inside TD to get the +REPORT. Then the REPORT data needs to be converted into a remotely verifiable +Quote by SGX Quoting Enclave (QE). + +It's a future work in QEMU to add support of TD attestation since it lacks +support in current KVM. + +Live Migration +-------------- + +Future work. + +References +---------- + +- `TDX Homepage `__ + +- `SGX QE `__ diff --git a/docs/system/target-i386.rst b/docs/system/target-i386.rst index ab7af1a75d6e..43b09c79d6be 100644 --- a/docs/system/target-i386.rst +++ b/docs/system/target-i386.rst @@ -31,6 +31,7 @@ Architectural features i386/kvm-pv i386/sgx i386/amd-memory-encryption + i386/tdx OS requirements ~~~~~~~~~~~~~~~