mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Dave Hansen <dave.hansen@intel.com>
To: Kai Huang <kai.huang@intel.com>,
	kirill.shutemov@linux.intel.com, tglx@linutronix.de,
	bp@alien8.de, peterz@infradead.org, mingo@redhat.com,
	hpa@zytor.com, dan.j.williams@intel.com, seanjc@google.com,
	pbonzini@redhat.com
Cc: x86@kernel.org, linux-kernel@vger.kernel.org,
	kvm@vger.kernel.org, rick.p.edgecombe@intel.com,
	isaku.yamahata@intel.com, adrian.hunter@intel.com,
	nik.borisov@suse.com
Subject: Re: [PATCH v5 0/8] TDX host: metadata reading tweaks, bug fix and info dump
Date: Tue, 15 Oct 2024 08:30:01 -0700	[thread overview]
Message-ID: <f25673ea-08c5-474b-a841-095656820b67@intel.com> (raw)
In-Reply-To: <cover.1728903647.git.kai.huang@intel.com>

[-- Attachment #1: Type: text/plain, Size: 1793 bytes --]

I'm having one of those "I hate this all" moments.  Look at what we say
in the code:

>   * See the "global_metadata.json" in the "TDX 1.5 ABI definitions".

Basically step one in verifying that this is all right is: Hey, humans,
please go parse a machine-readable format.  That's insanity.  If Intel
wants to publish JSON as the canonical source of truth, that's fine.
It's great, actually.  But let's stop playing human JSON parser and make
the computers do it for us, OK?

Let's just generate the code.  Basically, as long as the generated C is
marginally readable, I'm OK with it.  The most important things are:

 1. Adding a field is dirt simple
 2. Using the generated C is simple

In 99% of the cases, nobody ends up having to ever look at the generated
code.

Take a look at the attached python program and generated C file.  I
think they qualify.  We can check the script into tools/scripts/ and it
can get re-run when new json comes out or when a new field is needed.
You'd could call the generated code like this:

#include <generated.h>

	read_gunk(&tgm);

and use it like this:

	foo = tgm.BUILD_NUM;
	bar = tgm.BUILD_DATE;

Any field you want to add is a single addition to the python list and
re-running the script.  There's not even any need to do:

#define TDX_FOO_BAR_BUILD_DATE 0x8800000200000001

because it's unnecessary when you have:

	ret |= read_...(0x8800000200000001, &tgm.BUILD_DATE);

that links the magic number and the "BUILD_DATE" so closely together
anyway.  We also don't need type safety *here* at the "read" because
it's machine generated in the first place.  If there's a type mismatch
between "0x8800000200000001" and "tgm.BUILD_DATE" we have bigger
problems on our hands.

All the type checking comes when the code consumes tgm.BUILD_DATE (or
whatever).

[-- Attachment #2: tdx.py --]
[-- Type: text/x-python, Size: 853 bytes --]

#!/usr/bin/python3
import json
import sys

filefd = open(sys.argv[1])
jsonstr = filefd.read()
filefd.close()

j = json.loads(jsonstr)

print("static struct tdx_global_metadata tgm")
print("{")

def find_field(name):
	for f in j['Fields']:
		if f['Field Name'] == name:
			return f
	return None

fields = """
TDX_FEATURES0
BUILD_DATE
BUILD_NUM
MINOR_VERSION
""".strip().split("\n")

for fn in fields:
	f = find_field(fn)
	name = f['Field Name']
	element_bytes = int(f['Element Size (Bytes)'])
	element_bits = element_bytes * 8
	print("\tu%d %s;" % (element_bits, name))

print("}")


print("static void read_gunk()")
print("{")
print("\tint ret = 0;")
print("")
for fn in fields:
	f = find_field(fn)
	print("\tret |= read_sys_metadata_field(%s, &tgm.%s);" %
			(f['Base FIELD_ID (Hex)'],
			 f['Field Name']))
print("")
print("\treturn ret;")
print("}")

[-- Attachment #3: tdxm.c --]
[-- Type: text/x-csrc, Size: 457 bytes --]

static struct tdx_global_metadata tgm
{
	u64 TDX_FEATURES0;
	u32 BUILD_DATE;
	u16 BUILD_NUM;
	u16 MINOR_VERSION;
}
static void read_gunk()
{
	int ret = 0;

	ret |= read_sys_metadata_field(0x0A00000300000008, &tgm.TDX_FEATURES0);
	ret |= read_sys_metadata_field(0x8800000200000001, &tgm.BUILD_DATE);
	ret |= read_sys_metadata_field(0x8800000100000002, &tgm.BUILD_NUM);
	ret |= read_sys_metadata_field(0x0800000100000003, &tgm.MINOR_VERSION);

	return ret;
}

  parent reply	other threads:[~2024-10-15 15:30 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-14 11:31 Kai Huang
2024-10-14 11:31 ` [PATCH v5 1/8] x86/virt/tdx: Rename 'struct tdx_tdmr_sysinfo' to reflect the spec better Kai Huang
2024-10-14 11:31 ` [PATCH v5 2/8] x86/virt/tdx: Rework TD_SYSINFO_MAP to support build-time verification Kai Huang
2024-10-14 15:56   ` Dave Hansen
2024-10-14 19:13     ` Dan Williams
2024-10-15 11:34       ` Huang, Kai
2024-10-14 11:31 ` [PATCH v5 3/8] x86/virt/tdx: Prepare to support reading other global metadata fields Kai Huang
2024-10-14 11:31 ` [PATCH v5 4/8] x86/virt/tdx: Refine a comment to reflect the latest TDX spec Kai Huang
2024-10-14 11:31 ` [PATCH v5 5/8] x86/virt/tdx: Start to track all global metadata in one structure Kai Huang
2024-10-14 11:31 ` [PATCH v5 6/8] x86/virt/tdx: Print TDX module version Kai Huang
2024-10-14 11:31 ` [PATCH v5 7/8] x86/virt/tdx: Require the module to assert it has the NO_RBP_MOD mitigation Kai Huang
2024-10-14 11:31 ` [PATCH v5 8/8] x86/virt/tdx: Reduce TDMR's reserved areas by using CMRs to find memory holes Kai Huang
2024-10-15 15:30 ` Dave Hansen [this message]
2024-10-15 16:29   ` [PATCH v5 0/8] TDX host: metadata reading tweaks, bug fix and info dump Paolo Bonzini
2024-10-15 19:04     ` Dan Williams
2024-10-15 21:11       ` Huang, Kai
2024-10-28 12:07     ` 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=f25673ea-08c5-474b-a841-095656820b67@intel.com \
    --to=dave.hansen@intel.com \
    --cc=adrian.hunter@intel.com \
    --cc=bp@alien8.de \
    --cc=dan.j.williams@intel.com \
    --cc=hpa@zytor.com \
    --cc=isaku.yamahata@intel.com \
    --cc=kai.huang@intel.com \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=kvm@vger.kernel.org \
    --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®