From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (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 CD76D4F7999; Fri, 18 Sep 2026 13:31:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789738303; cv=none; b=EwXLas5LeP5LgosVq7hLRAneGkKSKqRxAq3zE1dMtHwkmn0w/Td9IcZLsAFL2pVIiqOVMqqtK2ticzHO1l4h3yHtz35TQ6DHnxAtCyHI+xn7qO8MYT3WUsvlNO88X9WlH6m2xR/OYOQH7ePEhQMvoeh9pNnXVCrjC8iSmNJTMKo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789738303; c=relaxed/simple; bh=G4W1Gz9y/omE+2l1vuHu0Bqtn4tce5ZQs8RGWGI79u8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jDHcZWCvcbVdSwJ7sBHRQ6mcWco6QY7+NAeqioG1iPJFSQE3XuOsSgC7YedGNSf/8qZgsvRpPA/2ZzHEPtRo3o45fSyqkR+0YpHeTu3WJNcnFTbiO2IMMFvWsbVOmsvWD5wyxjzJsxkdh4UtO7bY8AsNLW+9pNRR9oLLdwEaGYA= 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=GaPnyJ8L; arc=none smtp.client-ip=192.198.163.7 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="GaPnyJ8L" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1789738301; x=1821274301; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=G4W1Gz9y/omE+2l1vuHu0Bqtn4tce5ZQs8RGWGI79u8=; b=GaPnyJ8LhqPeZNqwggANzeHv4vOvELMU+cLTHSm3Iso+hIDl0PZZf0eX NYLgHWRKCNpX1NG2fcaPGlrKYowkMFgeyd/1Yu1nhsvYeRCoBTXIUheq8 35K1g8gY8A0WoyQ7an8LcRBPjjCq7b2Ro9ENsUVnPqdV9fF1AuiH342Q5 83g1yKYox1/5TChT1th/4A/92Pl9Qx+bQKvhl0TFPHoxHCuwkgHkpL9WW 4nqIyo6XAZ8vnSsJ/+Zumj/vD9On4P6B4WdrJ/KmhqNqtnvOgcJghJsE4 Tc1jLRVM0mcjMWAOsbQ3mySFN/O4OsXXt2XCS/3IsxKz3ONI8T3sscfPV A==; X-CSE-ConnectionGUID: wyVzc+cSQOK4HZqElXOKyw== X-CSE-MsgGUID: kjjx4lXuQhyaFhy5kqH+vQ== X-IronPort-AV: E=McAfee;i="6800,10657,11908"; a="115776102" X-IronPort-AV: E=Sophos;i="6.27,109,1787036400"; d="scan'208";a="115776102" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Sep 2026 06:31:39 -0700 X-CSE-ConnectionGUID: iw3MmNclQ3GjhIToxMwrdw== X-CSE-MsgGUID: 2AbmezB2TmOQJ18eM4OnEg== X-ExtLoop1: 1 Received: from 984fee019967.jf.intel.com ([10.23.153.244]) by fmviesa003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Sep 2026 06:31:38 -0700 From: Chao Gao To: linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev, kvm@vger.kernel.org Cc: yilun.xu@linux.intel.com, Chao Gao , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Kiryl Shutsemau , Rick Edgecombe Subject: [RFC PATCH v2 01/10] x86/virt/tdx: Add a helper to read a table of metadata fields Date: Fri, 18 Sep 2026 06:29:20 -0700 Message-ID: <20260918132946.76533-2-chao.gao@intel.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260918132946.76533-1-chao.gao@intel.com> References: <20260918132946.76533-1-chao.gao@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The metadata field readers get_tdx_sys_info_() in tdx_global_metadata.c were generated by an out-of-tree script. That has not worked out: the JSON file they were generated from is neither stable nor authoritative enough [1]. The goal now is to maintain the readers by hand and to establish one standard way of adding a metadata field. Take get_tdx_sys_info_version() as an example: if (!ret && !(ret = read_sys_metadata_field(0x0800000100000003, &val))) sysinfo_version->minor_version = val; if (!ret && !(ret = read_sys_metadata_field(0x0800000100000004, &val))) sysinfo_version->major_version = val; if (!ret && !(ret = read_sys_metadata_field(0x0800000100000005, &val))) sysinfo_version->update_version = val; Two patterns stand out: the read-check-store sequence repeats once per field, and the error of each read is chained into the reads that follow. Neither is common in hand-written code. Eliminate both with a loop that reads each field, stores the value into its structure member, and returns on the first error. Add 'struct field_mapping' to describe one field as its ID plus the offset and size of the member that receives its value. Add TDX_SYSINFO_MAP() to build such an entry from a field ID name, a structure type and a member name. Add __read_sys_metadata_table() to read every field in a table. Annotate the helper __maybe_unused as there is no caller right now. Following changes will convert the existing readers to use the new helper. AI was used under supervision to review code and workshop logs. Signed-off-by: Chao Gao Link: https://lore.kernel.org/kvm/1e7bcbad-eb26-44b7-97ca-88ab53467212@intel.com/ # [1] --- arch/x86/virt/vmx/tdx/tdx.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index 063574ed8625..5cd9c6eb98c2 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -395,6 +395,42 @@ static int read_sys_metadata_field(u64 field_id, u64 *data) return 0; } +/* + * Map a TDX global metadata field to a structure member. + * @field_id: The TDX global metadata field ID. + * @size: The size of the structure member. + * @offset: The member's offset within its containing structure. + */ +struct field_mapping { + u64 field_id; + size_t size; + int offset; +}; + +/* Read each metadata field listed in @mappings[] into @data. */ +static int __maybe_unused __read_sys_metadata_table(const struct field_mapping *mappings, + int num_mappings, void *data) +{ + int i, ret; + u64 val; + + for (i = 0; i < num_mappings; i++) { + ret = read_sys_metadata_field(mappings[i].field_id, &val); + if (ret) + return ret; + memcpy((char *)data + mappings[i].offset, &val, mappings[i].size); + } + + return 0; +} + +#define TDX_SYSINFO_MAP(_field, _type, _member) \ +{ \ + .field_id = TDX_MD_FIELD_ID_##_field, \ + .offset = offsetof(_type, _member), \ + .size = sizeof_field(_type, _member), \ +} + #include "tdx_global_metadata.c" static __init int check_features(struct tdx_sys_info *sysinfo) -- 2.52.0