From: "Huang, Kai" <kai.huang@intel.com>
To: "Hansen, Dave" <dave.hansen@intel.com>,
"seanjc@google.com" <seanjc@google.com>,
"bp@alien8.de" <bp@alien8.de>,
"peterz@infradead.org" <peterz@infradead.org>,
"hpa@zytor.com" <hpa@zytor.com>,
"mingo@redhat.com" <mingo@redhat.com>,
"kirill.shutemov@linux.intel.com"
<kirill.shutemov@linux.intel.com>,
"tglx@linutronix.de" <tglx@linutronix.de>,
"pbonzini@redhat.com" <pbonzini@redhat.com>,
"Williams, Dan J" <dan.j.williams@intel.com>
Cc: "nik.borisov@suse.com" <nik.borisov@suse.com>,
"Hunter, Adrian" <adrian.hunter@intel.com>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"Edgecombe, Rick P" <rick.p.edgecombe@intel.com>,
"x86@kernel.org" <x86@kernel.org>,
"Yamahata, Isaku" <isaku.yamahata@intel.com>
Subject: Re: [PATCH v8 3/9] x86/virt/tdx: Use auto-generated code to read global metadata
Date: Fri, 13 Dec 2024 11:17:45 +0000 [thread overview]
Message-ID: <762a50133300710771337398284567b299a86f67.camel@intel.com> (raw)
In-Reply-To: <daace74503d012cadd2e0b9c06ea39f60e0c2ad5.1731498635.git.kai.huang@intel.com>
[-- Attachment #1: Type: text/plain, Size: 900 bytes --]
On Thu, 2024-11-14 at 00:57 +1300, Kai Huang wrote:
> The tdx_global_metadata.{hc} can be generated by running below:
>
> #python tdx_global_metadata.py global_metadata.json \
> tdx_global_metadata.h tdx_global_metadata.c
>
> .. where the 'tdx_global_metadata.py' can be found in [4] and the
> 'global_metadata.json' can be fetched from [3].
>
[...]
> Link: https://cdrdv2.intel.com/v1/dl/getContent/795381 [3]
> Link: https://lore.kernel.org/d5aed06ae4b46df5db97fdbac9c01843920a2f96.camel@intel.com/ [4]
Hi Dave,
I'll remove the bugfix (patch 8) and the CMR reading code but only keep what is
required for KVM TDX in the next version.
I updated the script and attached here so that I can have a lore link of the
script which can be used to reproduce the generated code in the next version.
It only has CMR part removed thus is not interesting to read. Thanks.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: tdx_global_metadata.py --]
[-- Type: text/x-python3; name="tdx_global_metadata.py", Size: 6106 bytes --]
#! /usr/bin/env python3
import json
import sys
# Note: this script does not run as part of the build process.
# It is used to generate structs from the TDX global_metadata.json
# file, and functions to fill in said structs. Rerun it if
# you need more fields.
TDX_STRUCTS = {
"features": [
"TDX_FEATURES0"
],
"tdmr": [
"MAX_TDMRS",
"MAX_RESERVED_PER_TDMR",
"PAMT_4K_ENTRY_SIZE",
"PAMT_2M_ENTRY_SIZE",
"PAMT_1G_ENTRY_SIZE",
],
}
STRUCT_PREFIX = "tdx_sys_info"
FUNC_PREFIX = "get_tdx_sys_info"
STRVAR_PREFIX = "sysinfo"
def print_class_struct_field(field_name, element_bytes, num_fields, num_elements, file):
element_type = "u%s" % (element_bytes * 8)
element_array = ""
if num_fields > 1:
element_array += "[%d]" % (num_fields)
if num_elements > 1:
element_array += "[%d]" % (num_elements)
print("\t%s %s%s;" % (element_type, field_name, element_array), file=file)
def print_class_struct(class_name, fields, file):
struct_name = "%s_%s" % (STRUCT_PREFIX, class_name)
print("struct %s {" % (struct_name), file=file)
for f in fields:
print_class_struct_field(
f["Field Name"].lower(),
int(f["Element Size (Bytes)"]),
int(f["Num Fields"]),
int(f["Num Elements"]),
file=file)
print("};", file=file)
def print_read_field(field_id, struct_var, struct_member, indent, file):
print(
"%sif (!ret && !(ret = read_sys_metadata_field(%s, &val)))\n%s\t%s->%s = val;"
% (indent, field_id, indent, struct_var, struct_member),
file=file,
)
def print_class_function(class_name, fields, file):
func_name = "%s_%s" % (FUNC_PREFIX, class_name)
struct_name = "%s_%s" % (STRUCT_PREFIX, class_name)
struct_var = "%s_%s" % (STRVAR_PREFIX, class_name)
print("static int %s(struct %s *%s)" % (func_name, struct_name, struct_var), file=file)
print("{", file=file)
print("\tint ret = 0;", file=file)
print("\tu64 val;", file=file)
has_i = 0
has_j = 0
for f in fields:
num_fields = int(f["Num Fields"])
num_elements = int(f["Num Elements"])
if num_fields > 1:
has_i = 1
if num_elements > 1:
has_j = 1
if has_i == 1 and has_j == 1:
print("\tint i, j;", file=file)
elif has_i == 1:
print("\tint i;", file=file)
print(file=file)
for f in fields:
fname = f["Field Name"]
field_id = f["Base FIELD_ID (Hex)"]
num_fields = int(f["Num Fields"])
num_elements = int(f["Num Elements"])
struct_member = fname.lower()
indent = "\t"
if num_fields > 1:
if fname == "CMR_BASE" or fname == "CMR_SIZE":
limit = "%s_%s->num_cmrs" %(STRVAR_PREFIX, "cmr")
elif fname == "CPUID_CONFIG_LEAVES" or fname == "CPUID_CONFIG_VALUES":
limit = "%s_%s->num_cpuid_config" %(STRVAR_PREFIX, "td_conf")
else:
limit = "%d" %(num_fields)
print("%sfor (i = 0; i < %s; i++)" % (indent, limit), file=file)
indent += "\t"
field_id += " + i"
struct_member += "[i]"
if num_elements > 1:
print("%sfor (j = 0; j < %d; j++)" % (indent, num_elements), file=file)
indent += "\t"
field_id += " * %d + j" % (num_elements)
struct_member += "[j]"
print_read_field(
field_id,
struct_var,
struct_member,
indent,
file=file,
)
print(file=file)
print("\treturn ret;", file=file)
print("}", file=file)
def print_main_struct(file):
print("struct tdx_sys_info {", file=file)
for class_name, field_names in TDX_STRUCTS.items():
struct_name = "%s_%s" % (STRUCT_PREFIX, class_name)
struct_var = class_name
print("\tstruct %s %s;" % (struct_name, struct_var), file=file)
print("};", file=file)
def print_main_function(file):
print("static int get_tdx_sys_info(struct tdx_sys_info *sysinfo)", file=file)
print("{", file=file)
print("\tint ret = 0;", file=file)
print(file=file)
for class_name, field_names in TDX_STRUCTS.items():
func_name = "%s_%s" % (FUNC_PREFIX, class_name)
struct_var = class_name
print("\tret = ret ?: %s(&sysinfo->%s);" % (func_name, struct_var), file=file)
print(file=file)
print("\treturn ret;", file=file)
print("}", file=file)
jsonfile = sys.argv[1]
hfile = sys.argv[2]
cfile = sys.argv[3]
hfileifdef = hfile.replace(".", "_")
with open(jsonfile, "r") as f:
json_in = json.load(f)
fields = {x["Field Name"]: x for x in json_in["Fields"]}
with open(hfile, "w") as f:
print("/* SPDX-License-Identifier: GPL-2.0 */", file=f)
print("/* Automatically generated TDX global metadata structures. */", file=f)
print("#ifndef _X86_VIRT_TDX_AUTO_GENERATED_" + hfileifdef.upper(), file=f)
print("#define _X86_VIRT_TDX_AUTO_GENERATED_" + hfileifdef.upper(), file=f)
print(file=f)
print("#include <linux/types.h>", file=f)
print(file=f)
for class_name, field_names in TDX_STRUCTS.items():
print_class_struct(class_name, [fields[x] for x in field_names], file=f)
print(file=f)
print_main_struct(file=f)
print(file=f)
print("#endif", file=f)
with open(cfile, "w") as f:
print("// SPDX-License-Identifier: GPL-2.0", file=f)
print("/*", file=f)
print(" * Automatically generated functions to read TDX global metadata.", file=f)
print(" *", file=f)
print(" * This file doesn't compile on its own as it lacks of inclusion", file=f)
print(" * of SEAMCALL wrapper primitive which reads global metadata.", file=f)
print(" * Include this file to other C file instead.", file=f)
print(" */", file=f)
for class_name, field_names in TDX_STRUCTS.items():
print(file=f)
print_class_function(class_name, [fields[x] for x in field_names], file=f)
print(file=f)
print_main_function(file=f)
next prev parent reply other threads:[~2024-12-13 11:18 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-13 11:57 [PATCH v8 0/9] TDX host: metadata reading tweaks and bug fixes Kai Huang
2024-11-13 11:57 ` [PATCH v8 1/9] x86/virt/tdx: Rename 'struct tdx_tdmr_sysinfo' to reflect the spec better Kai Huang
2024-11-13 11:57 ` [PATCH v8 2/9] x86/virt/tdx: Start to track all global metadata in one structure Kai Huang
2024-11-13 11:57 ` [PATCH v8 3/9] x86/virt/tdx: Use auto-generated code to read global metadata Kai Huang
2024-12-13 11:17 ` Huang, Kai [this message]
2024-11-13 11:57 ` [PATCH v8 4/9] x86/virt/tdx: Use dedicated struct members for PAMT entry sizes Kai Huang
2024-11-13 11:57 ` [PATCH v8 5/9] x86/virt/tdx: Add missing header file inclusion to local tdx.h Kai Huang
2024-11-13 11:57 ` [PATCH v8 6/9] x86/virt/tdx: Switch to use auto-generated global metadata reading code Kai Huang
2024-11-13 11:57 ` [PATCH v8 7/9] x86/virt/tdx: Trim away tail null CMRs Kai Huang
2024-11-13 11:57 ` [PATCH v8 8/9] x86/virt/tdx: Reduce TDMR's reserved areas by using CMRs to find memory holes Kai Huang
2024-12-04 14:22 ` Huang, Kai
2024-12-05 12:45 ` Huang, Kai
2024-12-05 12:40 ` [PATCH v8 8.1/9] " Kai Huang
2024-12-05 18:10 ` Dave Hansen
2024-12-06 2:45 ` Huang, Kai
2024-12-09 6:57 ` Huang, Kai
2024-12-09 6:50 ` [PATCH v8 8.2/9] " Kai Huang
2024-12-09 22:54 ` Dave Hansen
2024-12-10 2:26 ` Huang, Kai
2024-12-10 2:46 ` Dan Williams
2024-12-10 4:24 ` Huang, Kai
2024-12-10 16:58 ` Dave Hansen
2024-12-11 4:34 ` Huang, Kai
2024-11-13 11:57 ` [PATCH v8 9/9] x86/virt/tdx: Require the module to assert it has the NO_RBP_MOD mitigation Kai Huang
2024-11-13 22:25 ` [PATCH v8 0/9] TDX host: metadata reading tweaks and bug fixes Edgecombe, Rick P
2024-11-13 22:40 ` Huang, Kai
2024-11-13 22:53 ` Edgecombe, Rick P
2024-11-13 23:35 ` Huang, Kai
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=762a50133300710771337398284567b299a86f67.camel@intel.com \
--to=kai.huang@intel.com \
--cc=adrian.hunter@intel.com \
--cc=bp@alien8.de \
--cc=dan.j.williams@intel.com \
--cc=dave.hansen@intel.com \
--cc=hpa@zytor.com \
--cc=isaku.yamahata@intel.com \
--cc=kirill.shutemov@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=nik.borisov@suse.com \
--cc=pbonzini@redhat.com \
--cc=peterz@infradead.org \
--cc=rick.p.edgecombe@intel.com \
--cc=seanjc@google.com \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
/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®