[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <0939e830f94003406dd3ac24126ed28c825c60cf.1741952958.git.legion@kernel.org>
Date: Fri, 14 Mar 2025 12:56:27 +0100
From: Alexey Gladkov <legion@...nel.org>
To: x86@...nel.org
Cc: "Alexey Gladkov (Intel)" <legion@...nel.org>,
linux-kernel@...r.kernel.org,
linux-coco@...ts.linux.dev,
Alexey Gladkov <alexey.gladkov@...el.com>,
"H . Peter Anvin" <hpa@...or.com>,
Joerg Roedel <jroedel@...e.de>,
Juergen Gross <jgross@...e.com>,
"Kirill A . Shutemov" <kirill.shutemov@...ux.intel.com>,
Larry.Dewey@....com,
Nikunj A Dadhania <nikunj@....com>,
Tom Lendacky <thomas.lendacky@....com>
Subject: [RFC PATCH v1 2/3] x86/tdx: Make TDX metadata available on guest via SYSFS
From: "Alexey Gladkov (Intel)" <legion@...nel.org>
Expose information about the TDX module to guest-side. TDX module
information (version, supported features, etc) is crucial for bug
reporting.
Signed-off-by: Alexey Gladkov (Intel) <legion@...nel.org>
---
arch/x86/Kconfig | 1 +
arch/x86/coco/tdx/tdx.c | 92 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 93 insertions(+)
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 516f3539d0c7..60f482edb1af 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -906,6 +906,7 @@ config INTEL_TDX_GUEST
select X86_MEM_ENCRYPT
select X86_MCE
select UNACCEPTED_MEMORY
+ select SYS_HYPERVISOR
help
Support running as a guest under Intel TDX. Without this support,
the guest kernel can not boot or run under TDX.
diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c
index 32809a06dab4..86108735aaf1 100644
--- a/arch/x86/coco/tdx/tdx.c
+++ b/arch/x86/coco/tdx/tdx.c
@@ -8,6 +8,7 @@
#include <linux/export.h>
#include <linux/io.h>
#include <linux/kexec.h>
+#include <linux/kobject.h>
#include <asm/coco.h>
#include <asm/tdx.h>
#include <asm/vmx.h>
@@ -1051,6 +1052,97 @@ static __init void tdx_announce(void)
tdx_dump_td_ctls(controls);
}
+#ifdef CONFIG_SYSFS
+static u64 tdx_read_sys_metadata_field(u64 field_id, u64 *data)
+{
+ struct tdx_module_args args = {};
+ u64 ret;
+
+ /*
+ * TDH.SYS.RD -- reads one global metadata field
+ * - RDX (in): the field to read
+ * - R8 (out): the field data
+ */
+ args.rdx = field_id;
+ ret = __tdcall_ret(TDG_SYS_RD, &args);
+
+ if (ret) {
+ pr_err("failed reading TDX field %llx, return %llx\n", field_id, ret);
+ return ret;
+ }
+
+ *data = args.r8;
+
+ return 0;
+}
+
+#define TDX_SYSFS_ATTR(_field, _name, fmt) \
+static ssize_t _name ## _show( \
+ struct kobject *kobj, struct kobj_attribute *attr, char *buf) \
+{ \
+ u64 value = 0; \
+ tdx_read_sys_metadata_field(_field, &value); \
+ return sprintf(buf, fmt, value); \
+} \
+static struct kobj_attribute _name ## _attr = __ATTR_RO(_name)
+
+TDX_SYSFS_ATTR(TDX_SYS_MINOR_FID, minor, "%lld\n");
+TDX_SYSFS_ATTR(TDX_SYS_MAJOR_FID, major, "%lld\n");
+TDX_SYSFS_ATTR(TDX_SYS_FEATURES0_FID, features0, "%llx\n");
+
+static struct attribute *version_attrs[] = {
+ &minor_attr.attr,
+ &major_attr.attr,
+ NULL,
+};
+
+static const struct attribute_group version_attr_group = {
+ .name = "version",
+ .attrs = version_attrs,
+};
+
+static struct attribute *properties_attrs[] = {
+ &features0_attr.attr,
+ NULL,
+};
+
+static const struct attribute_group properties_attr_group = {
+ .name = "properties",
+ .attrs = properties_attrs,
+};
+
+static int tdx_sysfs_init(void)
+{
+ struct kobject *tdx_kobj;
+ int ret;
+
+ if (!hypervisor_kobj)
+ return -ENOMEM;
+
+ tdx_kobj = kobject_create_and_add("tdx", hypervisor_kobj);
+
+ if (!tdx_kobj)
+ return -ENOMEM;
+
+ ret = sysfs_create_group(tdx_kobj, &version_attr_group);
+ if (ret)
+ pr_err("sysfs exporting tdx module version failed %d\n", ret);
+
+ if (!ret) {
+ ret = sysfs_create_group(tdx_kobj, &properties_attr_group);
+ if (ret)
+ pr_err("sysfs exporting tdx module properties failed %d\n", ret);
+ }
+
+ if (ret)
+ kobject_put(tdx_kobj);
+
+ return ret;
+}
+
+arch_initcall(tdx_sysfs_init);
+#endif // CONFIG_SYSFS
+
void __init tdx_early_init(void)
{
u64 cc_mask;
--
2.48.1
Powered by blists - more mailing lists