mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Xu Yilun <yilun.xu@linux.intel.com>
To: x86@kernel.org, dave.hansen@linux.intel.com, kas@kernel.org,
	linux-kernel@vger.kernel.org
Cc: chao.gao@intel.com, rick.p.edgecombe@intel.com,
	dan.j.williams@intel.com, baolu.lu@linux.intel.com,
	yilun.xu@linux.intel.com, yilun.xu@intel.com,
	zhenzhong.duan@intel.com, kvm@vger.kernel.org,
	adrian.hunter@intel.com
Subject: [PATCH 4/6] x86/virt/tdx: Sanity check the size of each metadata field
Date: Tue,  2 Dec 2025 13:08:42 +0800	[thread overview]
Message-ID: <20251202050844.2520762-5-yilun.xu@linux.intel.com> (raw)
In-Reply-To: <20251202050844.2520762-1-yilun.xu@linux.intel.com>

Add the build-time check to ensure the manually input field mappings
have consistent size definitions. I.e. for each mapping entry, the size
code in field_id should match the corresponding struct member type. This
type safe check prevents wrong interpretation of the readout value.

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Kai Huang <kai.huang@intel.com>
Signed-off-by: Xu Yilun <yilun.xu@linux.intel.com>
---
 arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 24 ++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/arch/x86/virt/vmx/tdx/tdx_global_metadata.c b/arch/x86/virt/vmx/tdx/tdx_global_metadata.c
index 3db87c4accd6..836d97166a7a 100644
--- a/arch/x86/virt/vmx/tdx/tdx_global_metadata.c
+++ b/arch/x86/virt/vmx/tdx/tdx_global_metadata.c
@@ -13,10 +13,32 @@ struct field_mapping {
 	int size;
 };
 
+/*
+ * Size of field abstracted from field ID.
+ *
+ * See Table "MD_FIELD_ID (Metadata Field Identifier / Sequence Header)
+ * Definition", TDX module 1.5 ABI spec.
+ *
+ *  - Bit 33:32: ELEMENT_SIZE_CODE -- size of a single element of metadata
+ *
+ *	0: 8 bits
+ *	1: 16 bits
+ *	2: 32 bits
+ *	3: 64 bits
+ */
+#define MD_FIELD_SIZE_CODE(_field_id)	\
+	(((_field_id) & GENMASK_ULL(33, 32)) >> 32)
+
+#define MD_FIELD_SIZE(_field_id) (1 << MD_FIELD_SIZE_CODE(_field_id))
+
+#define TD_SYSINFO_CHECK_SIZE(_field_id, _size)		\
+	__builtin_choose_expr(MD_FIELD_SIZE(_field_id) == (_size), _size, (void)0)
+
 #define TD_SYSINFO_MAP(_field_id, _member)				\
 	{ .field_id = _field_id,					\
 	  .offset = offsetof(struct tdx_sys_info, _member),		\
-	  .size = sizeof_field(struct tdx_sys_info, _member) }
+	  .size = TD_SYSINFO_CHECK_SIZE(_field_id,			\
+					sizeof_field(struct tdx_sys_info, _member)) }
 
 /* Map TD_SYSINFO fields into 'struct tdx_sys_info': */
 static const struct field_mapping mappings[] = {
-- 
2.25.1


  parent reply	other threads:[~2025-12-02  5:24 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-02  5:08 [PATCH 0/6] TDX: Stop metadata auto-generation, improve readability Xu Yilun
2025-12-02  5:08 ` [PATCH 1/6] x86/virt/tdx: Move bit definitions of TDX_FEATURES0 to public header Xu Yilun
2025-12-02  5:08 ` [PATCH 2/6] x86/virt/tdx: Move read_sys_metadata_field() to where it is called Xu Yilun
2025-12-02  5:08 ` [PATCH 3/6] x86/virt/tdx: Refactor metadata reading with a clearer for loop Xu Yilun
2025-12-02  5:08 ` Xu Yilun [this message]
2025-12-02  5:08 ` [PATCH 5/6] x86/virt/tdx: Add generic support for reading array-typed metadata Xu Yilun
2025-12-02  5:08 ` [PATCH 6/6] x86/virt/tdx: Skip unsupported metadata by querying tdx_feature0 Xu Yilun
2025-12-07  7:44 ` [PATCH 0/6] TDX: Stop metadata auto-generation, improve readability dan.j.williams
2025-12-08 10:17   ` Xu Yilun

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251202050844.2520762-5-yilun.xu@linux.intel.com \
    --to=yilun.xu@linux.intel.com \
    --cc=adrian.hunter@intel.com \
    --cc=baolu.lu@linux.intel.com \
    --cc=chao.gao@intel.com \
    --cc=dan.j.williams@intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=kas@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rick.p.edgecombe@intel.com \
    --cc=x86@kernel.org \
    --cc=yilun.xu@intel.com \
    --cc=zhenzhong.duan@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®