TDX: Enable PCIe link encryption for trusted I/O
Subsystems (4)
cover TDX: Enable PCIe link encryption for trusted I/O
Hi all,
This series adds PCIe link encryption on TDX hosts. Yilun previously posted one large series with all the changes needed for this [1]. That series has been split up, and the parts are being posted separately. This RFC is posted for early review. It is not yet seeking review from the x86 maintainers.
The SEAMCALL wrappers this series uses are in a separate series, not yet posted. So, their names and parameters may still change.
Today, userspace sets up link encryption by writing to the PCI/TSM "connect" sysfs attribute. A proposal under discussion [2] adds CTLV attestation commands [3] to this flow, so that userspace can select and verify the device's certificates before the IDE stream is set up. This series uses only the "connect" attribute. I will convert it to the new flow once the discussion settles.
== Background ==
With shared I/O, devices cannot access a confidential VM's private memory, so the VM bounces I/O data through memory it shares with the host. With trusted I/O, the VM establishes trust in a device and then lets it access private memory directly, which avoids the bouncing.
The device's MMIO and DMA traffic then carries private data across PCIe links and switches. A physical attacker on a link, or a malicious switch, could read, modify or replay that traffic. PCIe link encryption protects the traffic between the root port and the device against these attacks, which keeps switches out of the VM's TCB.
Link encryption builds on:
- Integrity and Data Encryption (IDE) [4], the PCIe feature that encrypts and integrity-protects data exchanged between the root port (part of the CPU's root complex) and the device.
- The Security Protocol and Data Model (SPDM) [5], a protocol for authenticating a device and setting up a secure session with it.
- Data Object Exchange (DOE) [6], a PCIe feature that provides a mailbox in the device's config space.
At a high level, setting up link encryption means opening an SPDM session with the device through its DOE mailbox, configuring an IDE stream on both the root port and the device, and programming the stream's keys into the root port and sending them to the device over the SPDM session.
The TEE Security Manager (TSM) is a trusted platform component, such as the TDX module or AMD's Secure Processor firmware. The work is split between the TSM and the host kernel. The TSM runs the SPDM session and handles the keys. It doesn't own the device, so the host kernel configures the device's IDE settings and relays SPDM messages between the TSM and the device.
== This series ==
This series adds a TDX-TSM driver to the TDX host services module (tdx-host.ko). The driver registers the TDX module as a TSM with the PCI/TSM framework [7] and implements its connect and disconnect operations. PCI/TSM calls them when userspace writes to a device's "connect" and "disconnect" sysfs attributes.
Connect sets up the SPDM session, the IDE stream and its keys through SEAMCALLs, and disconnect tears them down.
During some SEAMCALLs, the TDX module may need to exchange several SPDM messages with the device. The kernel calls such a SEAMCALL in a loop. Each time it returns an SPDM message with a dedicated status code, the kernel sends the message through the device's DOE mailbox and calls the SEAMCALL again with the device's reply, until the SEAMCALL completes.
== Points for discussion ==
1. Raw DOE frames (See patch 3)
The TDX module produces and consumes whole DOE objects, header included, but the PCI DOE API takes payloads only. The TDX-TSM driver therefore strips and rebuilds the DOE header itself. The code is small, but it puts DOE format details in a TSM driver, while the DOE core already handles them. A PCI DOE API that takes whole objects would keep them in the core, but the TDX-TSM driver would be its only user. Is such an API worth adding, or can we live with the DOE format handling in the TDX-TSM driver?
2. Default IDE Stream (See patch 5)
TDISP requires each device to have one default IDE stream [8]. The PCI core does not set Default Stream, so each TSM driver sets it itself, by looking up the device's IDE settings with pci_ide_to_settings() and setting default_stream. AMD's driver already does this, and the TDX-TSM driver does the same. Since the requirement is shared, the PCI core could instead set Default Stream on the device's TC0 stream, which is what TDISP expects. Should this move into the PCI core now, or can it be a later cleanup?
== Limitations ==
The TDX module configures the root port's IDE settings and accepts only one address range for the stream. The root port therefore encrypts only traffic to the device's prefetchable MMIO range, and a device with no prefetchable range cannot be connected. We are evaluating whether this can be lifted.
Link encryption needs the root complex to enter a TDX mode, in which only the TDX module can configure the root port's IDE settings. The kernel opts in to this mode with TDH.IOMMU.SETUP. As its name suggests, this SEAMCALL also puts the IOMMU into TDX mode, which changes the IOMMU's behavior. Unfortunately, the two cannot be enabled separately, even though link encryption does not need IOMMU TDX mode.
== Dependencies ==
The series depends on the following series:
- TDX module extensions [9]
- IOMMU TDX mode [10]
- SEAMCALL wrappers for PCIe Link Encryption [not yet posted]
The full stack is available at:
https://github.com/gaochaointel/linux-dev/tree/link-encryption-rfc-v1
== Testing ==
Tested that link encryption can be enabled and disabled through PCI/TSM:
# echo tsm0 > /sys/bus/pci/devices/$DEVICE/tsm/connect # echo tsm0 > /sys/bus/pci/devices/$DEVICE/tsm/disconnect
Note that I disabled DPAMT for this testing as this series does not yet install DPAMT for the SPDM metadata, which will be added later.
== Changes since Yilun's version ==
- Rebased onto the latest tip/x86/tdx branch.
- Rewrote the cover letter and commit messages
- Took over the patches from Zhenzhong and Yilun, and credited them in the changelogs.
- Dropped the Reviewed-by tags collected as patches are changed significantly.
- Allocated struct spdm_config_info_t with kzalloc_obj() and added a static_assert() on its size, which removes the casts on allocation and free.
- Fixed a leak of the SPDM message buffers (tlink->in_msg and tlink->out_msg), and moved their allocation into tdx_spdm_create(), next to the SPDM metadata.
- Dropped the SPDM session key update patch.
- Made TDX_HOST_SERVICES depend on PCI and select PCI_TSM directly. Otherwise tdx-host fails to build with CONFIG_PCI=n. A TDX host without PCI support is not a realistic configuration
- Replaced the bit operations and endian conversions in the DOE header handling with struct pci_doe_header and struct pci_doe_msg.
- Returned -EIO instead of -EFAULT when TDH.IDE.STREAM.CREATE fails.
- Reworded changelog to describe the TDX module's limit on concurrent TDH.SPDM.CONNECT. i.e., why tdx_spdm_lock.
- Clarified that having an IDE default stream is required by TDISP spec.
- Fixed checkpatch warnings
== AI Usage ==
AI was used under supervision to review code, refine logs and explore ways to improve readability. I wrote the first draft of this cover letter, which then changed significantly over several rounds of review with AI and was trimmed a lot for brevity. The idea for simplifying the DOE format handling in patch 3 came from AI.
== References ==
[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://lore.kernel.org/all/[email protected]/
[3]: https://lore.kernel.org/all/[email protected]/
[4]: PCIe Specification r7.0, section 6.33 "Integrity & Data Encryption (IDE)" [5]: https://www.dmtf.org/sites/default/files/standards/documents/DSP0274_1.3.0.pdf
[6]: PCIe Specification r7.0, section 6.30 "Data Object Exchange (DOE)"
[7]: https://lwn.net/Articles/1043262/
[8]: Intel TDX Connect Architecture Specification
https://cdrdv2-public.intel.com/773614/intel-tdx-connect-architecture-specification.pdf
[9]: https://lore.kernel.org/all/[email protected]/
[10]: https://lore.kernel.org/kvm/[email protected]/
Chao Gao (7):
coco/tdx-host: Create metadata for an SPDM session coco/tdx-host: Establish an SPDM session with the device coco/tdx-host: Exchange SPDM messages through DOE coco/tdx-host: Pass a device info buffer to tdh_spdm_connect() coco/tdx-host: Configure an IDE stream coco/tdx-host: Program IDE keys and enable the IDE stream x86/virt/tdx: Enable PCIe link encryption
arch/x86/include/asm/shared/tdx_errno.h | 1 + arch/x86/include/asm/tdx.h | 2 +- arch/x86/include/asm/tdx_global_metadata.h | 2 + arch/x86/virt/vmx/tdx/tdx.c | 2 +- arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 12 + drivers/pci/ide.c | 5 +- drivers/virt/coco/tdx-host/main.c | 496 +++++++++++++++++++- include/linux/pci-ide.h | 2 + 8 files changed, 516 insertions(+), 6 deletions(-)
Signature · 1 line
2.52.0
RFC PATCH 1/7 coco/tdx-host: Create metadata for an SPDM session
The TDX module talks to the device over a Security Protocol and Data Model (SPDM) session. It needs host memory for each session's metadata, and reports the number of pages required in its global metadata.
When setting up an SPDM session, allocate the memory and hand it to the TDX module with TDH.SPDM.CREATE. Keep the returned session ID for later SEAMCALLs. When tearing the session down, reclaim the memory with TDH.SPDM.DELETE. If that fails, leak the memory, as the TDX module may still be using it.
Based on work by Yilun and Zhenzhong.
--- arch/x86/include/asm/tdx_global_metadata.h | 1 + arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 6 ++ drivers/virt/coco/tdx-host/main.c | 104 +++++++++++++++++++- 3 files changed, 109 insertions(+), 2 deletions(-)
arch/x86/include/asm/tdx_global_metadata.h +1 -0
@@ -52,6 +52,7 @@ struct tdx_sys_info_ext { };struct tdx_sys_info_connect { +u16 spdm_mt_page_count;u16 iommu_mt_page_count; };
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +6 -0
@@ -144,6 +144,12 @@ static __init int get_tdx_sys_info_connect(struct tdx_sys_info_connect *sysinfo_int ret;u64 val; + ret =read_sys_metadata_field (0x3000000100000002 , &val); +if (ret) +return ret; + + sysinfo_connect->spdm_mt_page_count = val; + ret =read_sys_metadata_field (0x3000000100000003 , &val);if (ret)return ret;
drivers/virt/coco/tdx-host/main.c +102 -2
143 lines not on this page · raw message
Signature · 1 line
2.52.0
Edgecombe, Rick P
maintainerRe: [RFC PATCH 1/7] coco/tdx-host: Create metadata for an SPDM session
Quoted 3 lines · Chao Gao
The TDX module talks to the device over a Security Protocol and Data Model (SPDM) session. It needs host memory for each session's metadata, and reports the number of pages required in its global metadata.
Since we usually talk about metadata and mean the global metadata, we will probably need to explain what session metadata is. Is it just data?
Quoted 5 lines
When setting up an SPDM session, allocate the memory and hand it to the TDX module with TDH.SPDM.CREATE. Keep the returned session ID for later SEAMCALLs.
This is getting to be a pretty familiar concept. TDX needs per-something scoped memory. Give it to the TDX module per some seamcall. Can we make it sounds especially ordinary and then just call out any thing that is different?
Quoted 3 lines
When tearing the session down, reclaim the memory with
TDH.SPDM.DELETE. If that fails, leak the memory, as the TDX module may still be using it.
When would it fail?
Quoted 27 lines
Based on work by Yilun and Zhenzhong.
Signed-off-by Chao Gao <[email protected]>--- arch/x86/include/asm/tdx_global_metadata.h | 1 + arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 6 ++ drivers/virt/coco/tdx-host/main.c | 104 +++++++++++++++++++- 3 files changed, 109 insertions(+), 2 deletions(-)arch/x86/include/asm/tdx_global_metadata.h +1
-0@@ -52,6 +52,7 @@ struct tdx_sys_info_ext { };struct tdx_sys_info_connect { +u16 spdm_mt_page_count;u16 iommu_mt_page_count; };arch/x86/virt/vmx/tdx/tdx_global_metadata.c +0
-0
arch/x86/virt/vmx/tdx/tdx_global_metadata.c static __init int get_tdx_sys_info_connect(struct tdx_sys_info_connect *sysinfo_ · line 152
Quoted 9 lines
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +6
-0@@ -144,6 +144,12 @@ static __init int get_tdx_sys_info_connect(struct tdx_sys_info_connect *sysinfo_int ret;u64 val; + ret =read_sys_metadata_field (0x3000000100000002 , &val); +if (ret) +return ret; + + sysinfo_connect->spdm_mt_page_count = val; +
We'll hopefully see this hunk get updated once the metadata cleanup series lands.
Quoted 25 lines
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +0
-0ret =read_sys_metadata_field (0x3000000100000003 , &val);if (ret)return ret;drivers/virt/coco/tdx-host/main.c +7
-0@@ -5,6 +5,7 @@* Copyright (C) 2025 Intel Corporation */ +#include <linux/bitfield.h> #include <linux/device/faux.h> #include <linux/firmware.h> #include <linux/module.h> @@ -26,8 +27,30 @@ static const struct x86_cpu_id tdx_host_ids[] = { };MODULE_DEVICE_TABLE (x86cpu, tdx_host_ids); +#define TDISP_FUNC_ID GENMASK(15, 0) +#define TDISP_FUNC_ID_SEGMENT GENMASK(23, 16) +#define TDISP_FUNC_ID_SEG_VALID BIT(24) + +static inline u32 tdisp_func_id (struct pci_dev *pdev) +{
drivers/virt/coco/tdx-host/main.c static const struct x86_cpu_id tdx_host_ids[] = { · line 45
Quoted 10 lines
drivers/virt/coco/tdx-host/main.c +10
-0+u32 func_id; + + func_id =FIELD_PREP (TDISP_FUNC_ID_SEGMENT ,pci_domain_nr (pdev->bus)); +if (func_id) + func_id |=TDISP_FUNC_ID_SEG_VALID ; + func_id |=FIELD_PREP (TDISP_FUNC_ID , +PCI_DEVID (pdev->bus->number, pdev->devfn)); + +return func_id; +}
Is this TDISP specific encoding or a TDX-TDISP specific encoding? I think TDISP. There isn't a more general place to put it?
Quoted 21 lines
drivers/virt/coco/tdx-host/main.c +12
-0+struct tdx_tsm_link {struct pci_tsm_pf0 pci; +u32 func_id; + +u64 spdm_id; +unsigned int spdm_mt_nr_pages; +void *spdm_mt; };static struct tdx_tsm_link *to_tdx_tsm_link (struct pci_tsm *tsm)@@ -35,21 +58,97 @@ static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm) return container_of(tsm, struct tdx_tsm_link, pci.base_tsm); } +static int tdx_spdm_create (struct tdx_tsm_link *tlink) +{ +const struct tdx_sys_info *tdx_sysinfo =tdx_get_sysinfo (); +struct tdx_hpa_list_info info; +unsigned int nr_pages; +u64 spdm_id, sret;
drivers/virt/coco/tdx-host/main.c static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm) · line 76
Quoted 10 lines
drivers/virt/coco/tdx-host/main.c +10
-0+void *spdm_mt; +int ret; + +if (!tdx_sysinfo) +return -ENXIO ; + + nr_pages = tdx_sysinfo->tdx_connect.spdm_mt_page_count; + + spdm_mt =alloc_pages_exact (nr_pages *PAGE_SIZE , +GFP_KERNEL | __GFP_ZERO);
Why zero?
Quoted 34 lines
drivers/virt/coco/tdx-host/main.c +34
-0+if (!spdm_mt) +return -ENOMEM ; + + ret =tdx_hpa_list_info_setup (&info, spdm_mt, nr_pages); +if (ret) + goto out_free_spdm_mt; + + sret =tdh_spdm_create (tlink->func_id, &info, &spdm_id); + +tdx_hpa_list_info_free (&info); + +if (sret) { + ret = -EIO ; + goto out_free_spdm_mt; + } + + tlink->spdm_id = spdm_id; + tlink->spdm_mt = spdm_mt; + tlink->spdm_mt_nr_pages = nr_pages; +return 0 ; + +out_free_spdm_mt: +free_pages_exact (spdm_mt, nr_pages *PAGE_SIZE ); +return ret; +} + +static void tdx_spdm_delete (struct tdx_tsm_link *tlink) +{ +struct pci_dev *pdev = tlink->pci.base_tsm.pdev; +u64 sret; + + sret =tdh_spdm_delete (tlink->spdm_id); +if (sret) { +/* leak the metadata pages */
drivers/virt/coco/tdx-host/main.c static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm) · line 120
Quoted 10 lines
drivers/virt/coco/tdx-host/main.c +10
-0+pci_err (pdev,"fail to delete spdm 0x%llx\n" , sret); +return ; + } + +free_pages_exact (tlink->spdm_mt, tlink->spdm_mt_nr_pages *PAGE_SIZE ); +} + +static int tdx_spdm_session_setup (struct tdx_tsm_link *tlink) +{ +return tdx_spdm_create (tlink);
Hmm, its not clear from this patch why we would need so many levels of wrapper for the setup/teardown seamcall. But it grows in later patches. But it looks weird in this patch and will probably throw off the tip review.
Quoted 37 lines
drivers/virt/coco/tdx-host/main.c +18
-2+} + +static void tdx_spdm_session_teardown (struct tdx_tsm_link *tlink) +{ +tdx_spdm_delete (tlink); +} +static int tdx_tsm_link_connect (struct pci_dev *pdev) { +struct tdx_tsm_link *tlink =to_tdx_tsm_link (pdev->tsm);int ret; ret =tdx_iommu_get (pdev);if (ret)return ret; -/* TDISP connect is not implemented yet. */ + ret =tdx_spdm_session_setup (tlink); +if (ret) + goto out_iommu_put; +return 0 ; + +out_iommu_put:tdx_iommu_put (pdev); -return -ENXIO ; +return ret; }static void tdx_tsm_link_disconnect (struct pci_dev *pdev) { +struct tdx_tsm_link *tlink =to_tdx_tsm_link (pdev->tsm); + +tdx_spdm_session_teardown (tlink);tdx_iommu_put (pdev); }
drivers/virt/coco/tdx-host/main.c static struct pci_tsm *tdx_tsm_link_pf0_probe(struct tsm_dev *tsm_dev, · line 168
Quoted 4 lines
drivers/virt/coco/tdx-host/main.c +1
-0@@ -66,6 +165,7 @@ static struct pci_tsm *tdx_tsm_link_pf0_probe(struct tsm_dev *tsm_dev,if (rc)return NULL ; + tlink->func_id =tdisp_func_id (pdev);
Why is this here instead of tdx_spdm_create()?
Quoted 3 lines
drivers/virt/coco/tdx-host/main.c +0
-0return &no_free_ptr (tlink)->pci.base_tsm; }
Re: [RFC PATCH 1/7] coco/tdx-host: Create metadata for an SPDM session
Quoted 7 lines · Rick P
Quoted 3 lines · Chao Gao
The TDX module talks to the device over a Security Protocol and Data Model (SPDM) session. It needs host memory for each session's metadata, and reports the number of pages required in its global metadata.
Since we usually talk about metadata and mean the global metadata, we will probably need to explain what session metadata is. Is it just data?
Yes. It is used to keep the TDX module's own state for one SPDM session, for example, the session keys.
Quoted 9 lines
Quoted 5 lines
When setting up an SPDM session, allocate the memory and hand it to the TDX module with TDH.SPDM.CREATE. Keep the returned session ID for later SEAMCALLs.
This is getting to be a pretty familiar concept. TDX needs per-something scoped memory. Give it to the TDX module per some seamcall. Can we make it sounds especially ordinary and then just call out any thing that is different?
How about:
To set up link encryption, the TDX module talks to the device over a Security Protocol and Data Model (SPDM) session.
An SPDM session is an object like a TD or a vCPU. They are managed by the TDX module in the same way. Each has SEAMCALLs to create and delete it, and the TDX module keeps its state for the object in memory provided by the host.
Allocate the memory when setting up a session and give it to the TDX module. Get it back when tearing the session down.
Creating a session returns a session ID, which later SEAMCALLs use to identify the session. Store it.
Quoted 5 lines
Quoted 3 lines
When tearing the session down, reclaim the memory with
TDH.SPDM.DELETE. If that fails, leak the memory, as the TDX module may still be using it.When would it fail?
It shouldn't in practice. Per the spec, it fails if the input is wrong, if the session is still in use (by another SEAMCALL or by an IDE stream that has not been torn down), or if the TDX module extension hit a fatal error. Any of them would be unexpected. They mean a bug in the kernel or in the TDX module, or an earlier failure to tear down the IDE stream.
I would just say in the changelog that TDH.SPDM.DELETE should not fail, and WARN_ON() and leak the memory if it does.
Quoted 9 lines
Quoted 9 lines
drivers/virt/coco/tdx-host/main.c +9
-0+#define TDISP_FUNC_ID_SEG_VALID BIT(24) + +static inline u32 tdisp_func_id (struct pci_dev *pdev) +{ +u32 func_id; + + func_id =FIELD_PREP (TDISP_FUNC_ID_SEGMENT ,pci_domain_nr (pdev->bus)); +if (func_id) + func_id |=TDISP_FUNC_ID_SEG_VALID ;
Quoted 8 lines
drivers/virt/coco/tdx-host/main.c static const struct x86_cpu_id tdx_host_ids[] = { · line 45
Quoted 5 lines
drivers/virt/coco/tdx-host/main.c +5
-0+ func_id |=FIELD_PREP (TDISP_FUNC_ID , +PCI_DEVID (pdev->bus->number, pdev->devfn)); + +return func_id; +}Is this TDISP specific encoding or a TDX-TDISP specific encoding? I think TDISP. There isn't a more general place to put it?
TDISP defines it. There is no TDISP header for such definitions yet. AMD passes the segment and the BDF to its firmware separately. So it looks like TDX is the first user of this encoding. I will add a pci-tdisp.h for it.
Quoted 8 lines
drivers/virt/coco/tdx-host/main.c static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm) · line 76
Quoted 6 lines
drivers/virt/coco/tdx-host/main.c +6
-0+return -ENXIO ; + + nr_pages = tdx_sysinfo->tdx_connect.spdm_mt_page_count; + + spdm_mt =alloc_pages_exact (nr_pages *PAGE_SIZE , +GFP_KERNEL | __GFP_ZERO);Why zero?
It is not needed. Will drop it.
Quoted 7 lines
drivers/virt/coco/tdx-host/main.c static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm) · line 120
Quoted 3 lines
drivers/virt/coco/tdx-host/main.c +3
-0+static int tdx_spdm_session_setup (struct tdx_tsm_link *tlink) +{ +return tdx_spdm_create (tlink);Hmm, its not clear from this patch why we would need so many levels of wrapper for the setup/teardown seamcall. But it grows in later patches. But it looks weird in this patch and will probably throw off the tip review.
Indeed. I will drop the wrappers from this patch and add them in the later patches (if necessary).
Quoted 9 lines
drivers/virt/coco/tdx-host/main.c static struct pci_tsm *tdx_tsm_link_pf0_probe(struct tsm_dev *tsm_dev, · line 168
Quoted 7 lines
}
drivers/virt/coco/tdx-host/main.c +1
-0@@ -66,6 +165,7 @@ static struct pci_tsm *tdx_tsm_link_pf0_probe(struct tsm_dev *tsm_dev,if (rc)return NULL ; + tlink->func_id =tdisp_func_id (pdev);Why is this here instead of
tdx_spdm_create()?
I would drop the cached value. The ID can always be derived from the pci_dev, so tdx_spdm_create() can compute it when needed.
Edgecombe, Rick P
maintainerRe: [RFC PATCH 1/7] coco/tdx-host: Create metadata for an SPDM session
Quoted 32 lines · Chao Gao
Quoted 7 lines · Rick P
Quoted 3 lines · Chao Gao
The TDX module talks to the device over a Security Protocol and Data Model (SPDM) session. It needs host memory for each session's metadata, and reports the number of pages required in its global metadata.
Since we usually talk about metadata and mean the global metadata, we will probably need to explain what session metadata is. Is it just data?
Yes. It is used to keep the TDX module's own state for one SPDM session, for example, the session keys.
Quoted 9 lines
Quoted 5 lines
When setting up an SPDM session, allocate the memory and hand it to the TDX module with TDH.SPDM.CREATE. Keep the returned session ID for later SEAMCALLs.
This is getting to be a pretty familiar concept. TDX needs per-something scoped memory. Give it to the TDX module per some seamcall. Can we make it sounds especially ordinary and then just call out any thing that is different?
How about:
To set up link encryption, the TDX module talks to the device over a Security Protocol and Data Model (SPDM) session.
An SPDM session is an object like a TD or a vCPU. They are managed by the TDX module in the same way. Each has SEAMCALLs to create and delete it, and the TDX module keeps its state for the object in memory provided by the host.
Quoted 5 lines
Allocate the memory when setting up a session and give it to the TDX module. Get it back when tearing the session down.
Creating a session returns a session ID, which later SEAMCALLs use to identify the session. Store it.
Looks good.
Quoted 14 lines
Quoted 6 lines
Quoted 4 lines
When tearing the session down, reclaim the memory with
TDH.SPDM.DELETE. If that fails, leak the memory, as the TDX module may still be using it.When would it fail?
It shouldn't in practice. Per the spec, it fails if the input is wrong, if the session is still in use (by another SEAMCALL or by an IDE stream that has not been torn down), or if the TDX module extension hit a fatal error. Any of them would be unexpected. They mean a bug in the kernel or in the TDX module, or an earlier failure to tear down the IDE stream.
Quoted 2 lines
I would just say in the changelog that TDH.SPDM.DELETE should not fail, and
WARN_ON()and leak the memory if it does.
I think we really need to strip these patches down to the bare minimum for the initial upstreaming. We see this again and again and just need lots of revisions to get them down to the true bare minimum. So let's just start there.
In this case we could just WARN and have have no special logic around surviving the bug. It is the simplest way to not have it look weird ignoring the error.
Quoted 1 line
Quoted 1 line
Re: [RFC PATCH 1/7] coco/tdx-host: Create metadata for an SPDM session
Quoted 13 lines · Rick P
Quoted 13 lines
Quoted 5 lines
Quoted 3 lines
When tearing the session down, reclaim the memory with
TDH.SPDM.DELETE. If that fails, leak the memory, as the TDX module may still be using it.When would it fail?
It shouldn't in practice. Per the spec, it fails if the input is wrong, if the session is still in use (by another SEAMCALL or by an IDE stream that has not been torn down), or if the TDX module extension hit a fatal error. Any of them would be unexpected. They mean a bug in the kernel or in the TDX module, or an earlier failure to tear down the IDE stream.
Quoted 6 lines
Quoted 2 lines
I would just say in the changelog that TDH.SPDM.DELETE should not fail, and
WARN_ON()and leak the memory if it does.I think we really need to strip these patches down to the bare minimum for the initial upstreaming. We see this again and again and just need lots of revisions to get them down to the true bare minimum. So let's just start there.
Agreed. I will go through the series again and drop everything that is not needed for the initial upstreaming.
Quoted 3 lines
In this case we could just WARN and have have no special logic around surviving the bug. It is the simplest way to not have it look weird ignoring the error.
Will do.
Re: [RFC PATCH 1/7] coco/tdx-host: Create metadata for an SPDM session
In reply to Chao GaoQuoted 22 lines · Chao Gao
Quoted 7 lines · Rick P
Quoted 3 lines · Chao Gao
The TDX module talks to the device over a Security Protocol and Data Model (SPDM) session. It needs host memory for each session's metadata, and reports the number of pages required in its global metadata.
Since we usually talk about metadata and mean the global metadata, we will probably need to explain what session metadata is. Is it just data?
Yes. It is used to keep the TDX module's own state for one SPDM session, for example, the session keys.
Quoted 9 lines
Quoted 5 lines
When setting up an SPDM session, allocate the memory and hand it to the TDX module with TDH.SPDM.CREATE. Keep the returned session ID for later SEAMCALLs.
This is getting to be a pretty familiar concept. TDX needs per-something scoped memory. Give it to the TDX module per some seamcall. Can we make it sounds especially ordinary and then just call out any thing that is different?
Quoted 6 lines
How about:
To set up link encryption, the TDX module talks to the device over a Security Protocol and Data Model (SPDM) session.
An SPDM session is an object like a TD or a vCPU.
This sentence alone is quite confusing.
Quoted 2 lines
They are managed
by the TDX module in the same way.
How about:
The TDX module manages an SPDM session in the same way as a TD or a vCPU.
Quoted 10 lines
Each has SEAMCALLs to create and
delete it, and the TDX module keeps its state for the object in memory provided by the host.Allocate the memory when setting up a session and give it to the TDX module. Get it back when tearing the session down.
Creating a session returns a session ID, which later SEAMCALLs use to identify the session. Store it.
[...]
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
drivers/virt/coco/tdx-host/main.c +9
-0+#define TDISP_FUNC_ID_SEG_VALID BIT(24) + +static inline u32 tdisp_func_id (struct pci_dev *pdev) +{ +u32 func_id; + + func_id =FIELD_PREP (TDISP_FUNC_ID_SEGMENT ,pci_domain_nr (pdev->bus)); +if (func_id) + func_id |=TDISP_FUNC_ID_SEG_VALID ;Quoted 8 lines
drivers/virt/coco/tdx-host/main.c static const struct x86_cpu_id tdx_host_ids[] = { · line 45
Quoted 5 lines
drivers/virt/coco/tdx-host/main.c +5
-0+ func_id |=FIELD_PREP (TDISP_FUNC_ID , +PCI_DEVID (pdev->bus->number, pdev->devfn)); + +return func_id; +}Is this TDISP specific encoding or a TDX-TDISP specific encoding? I think TDISP. There isn't a more general place to put it?
Quoted 4 lines
TDISP defines it. There is no TDISP header for such definitions yet. AMD passes the segment and the BDF to its firmware separately. So it looks like TDX is the first user of this encoding. I will add a
pci-tdisp.hfor it.
BTW: another patch [*] also adds standard TDISP definitions (device interface report) in drivers/pci/tsm.c, also suggest gather them in pci-tdisp.h?
RFC PATCH 2/7 coco/tdx-host: Establish an SPDM session with the device
In reply to Chao GaoSetting up PCIe link encryption requires a secure channel to the device to exchange keys. This channel is an SPDM session over the device's PCI DOE mailbox.
The TDX module cannot access the DOE mailbox but needs to exchange SPDM messages with the device, so the host relays SPDM messages between the two.
TDH.SPDM.CONNECT establishes the session and TDH.SPDM.DISCONNECT tears it down. Both may need several message exchanges with the device. Whenever the TDX module has a message for the device, the SEAMCALL returns with a dedicated status code. The host sends the message to the device and calls the SEAMCALL again with the device's response, until the SEAMCALL completes.
Call both SEAMCALLs in such a loop. The DOE transfer itself is a stub for now.
Pass the messages through two pages per device, one for each direction. One pair is enough because PCI/TSM serializes operations on a device.
The TDX module limits how many TDH.SPDM.CONNECT and TDH.SPDM.DISCONNECT calls can run at the same time. The limit is implementation-specific, and a call over the limit fails with TDX_OPERAND_BUSY. For simplicity, serialize them across all devices with a mutex.
Based on work by Yilun.
--- arch/x86/include/asm/shared/tdx_errno.h | 1 + drivers/virt/coco/tdx-host/main.c | 131 +++++++++++++++++++++++- 2 files changed, 130 insertions(+), 2 deletions(-)
arch/x86/include/asm/shared/tdx_errno.h +1 -0
@@ -27,6 +27,7 @@#define TDX_EPT_WALK_FAILED 0xC0000B0000000000ULL#define TDX_EPT_ENTRY_STATE_INCORRECT 0xC0000B0D00000000ULL#define TDX_METADATA_FIELD_NOT_READABLE 0xC0000C0200000000ULL +#define TDX_SPDM_REQUEST 0x00000F5700000000ULL/* * TDX module operand ID, appears in 31:0 part of error code as
drivers/virt/coco/tdx-host/main.c +129 -2
188 lines not on this page · raw message
Signature · 1 line
2.52.0
Edgecombe, Rick P
maintainerRe: [RFC PATCH 2/7] coco/tdx-host: Establish an SPDM session with the device
Quoted 3 lines · Chao Gao
Setting up PCIe link encryption requires a secure channel to the device to exchange keys. This channel is an SPDM session over the device's PCI DOE mailbox.
Probably SPDM and DOE mailbox need some 10,000ft summary.
Quoted 11 lines
The TDX module cannot access the DOE mailbox but needs to exchange SPDM messages with the device, so the host relays SPDM messages between the two.
TDH.SPDM.CONNECT establishes the session and TDH.SPDM.DISCONNECT tears it down. Both may need several message exchanges with the device. Whenever the TDX module has a message for the device, the SEAMCALL returns with a dedicated status code. The host sends the message to the device and calls the SEAMCALL again with the device's response, until the SEAMCALL completes.
SEAMCALL completes? Could mean the full extension operation or a single seamcall execution.
Quoted 3 lines
Call both SEAMCALLs in such a loop. The DOE transfer itself is a stub for now.
I'd put something higher up explaining this is laying the ground work, rather than announcing the gaps at the end.
Quoted 3 lines
Pass the messages through two pages per device, one for each direction. One pair is enough because PCI/TSM serializes operations on a device.
This probably needs more explanation.
Quoted 34 lines
The TDX module limits how many TDH.SPDM.CONNECT and TDH.SPDM.DISCONNECT calls can run at the same time. The limit is implementation-specific, and a call over the limit fails with
TDX_OPERAND_BUSY. For simplicity, serialize them across all devices with a mutex.Based on work by Yilun.
Signed-off-by Chao Gao <[email protected]>--- arch/x86/include/asm/shared/tdx_errno.h | 1 + drivers/virt/coco/tdx-host/main.c | 131 +++++++++++++++++++++++- 2 files changed, 130 insertions(+), 2 deletions(-)arch/x86/include/asm/shared/tdx_errno.h +1
-0@@ -27,6 +27,7 @@#define TDX_EPT_WALK_FAILED 0xC0000B0000000000ULL#define TDX_EPT_ENTRY_STATE_INCORRECT 0xC0000B0D00000000ULL#define TDX_METADATA_FIELD_NOT_READABLE 0xC0000C0200000000ULL +#define TDX_SPDM_REQUEST 0x00000F5700000000ULL/* * TDX module operand ID, appears in 31:0 part of error code as drivers/virt/coco/tdx-host/main.c +0
-0@@ -44,11 +44,26 @@ static inline u32 tdisp_func_id(struct pci_dev *pdev)return func_id; }
drivers/virt/coco/tdx-host/main.c static inline u32 tdisp_func_id(struct pci_dev *pdev) · line 56
Quoted 10 lines
drivers/virt/coco/tdx-host/main.c +10
-0+#define SPDM_CAP_HBEAT BIT(13) +#define SPDM_CAP_KEY_UPD BIT(14) + +struct spdm_config_info_t { +u32 vmm_spdm_cap; +u8 spdm_session_policy; +u8 certificate_slot_mask; +u8 raw_bitstream_requested; +u8 reserved[4089 ]; +} __packed;
This looks like more SPDM specific stuff in TDX. Do really know other vendors need it? We should at least group it in a header such that it is clear which things are SPDM spec and which are TDX.
drivers/virt/coco/tdx-host/main.c static inline u32 tdisp_func_id(struct pci_dev *pdev) · line 66
Quoted 10 lines
drivers/virt/coco/tdx-host/main.c +5
-0+ +static_assert (sizeof (struct spdm_config_info_t ) ==4096 ); +struct tdx_tsm_link {struct pci_tsm_pf0 pci;u32 func_id;u64 spdm_id; +struct page *in_msg; +struct page *out_msg;
SNP and TDX both have the DOE mailbox shuffling done by the host, right? So why doesn't TSM allocate and provide these buffers?
Oh looking at the code I'm now very confused. The request for SNP is in private memory? The SNP commit has very little info to explain what is going on there.
Quoted 12 lines
drivers/virt/coco/tdx-host/main.c +5
-0unsigned int spdm_mt_nr_pages;void *spdm_mt; };@@ -58,9 +73,89 @@ static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm) return container_of(tsm, struct tdx_tsm_link, pci.base_tsm); } +static int tdx_spdm_msg_exchange (struct tdx_tsm_link *tlink) +{ +/* TODO: implement the PCI DOE transfer */ +return 0 ; +}
drivers/virt/coco/tdx-host/main.c static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm) · line 90
Quoted 10 lines
drivers/virt/coco/tdx-host/main.c +10
-0+ +/* +* When serving SEAMCALLs, the TDX module extensions work like service thread +* pools. The number of service threads is limited per service type (or +* "per extension" in terms of TDX). When the host issues a request via +* SEAMCALL but the TDX module can't find an available thread, it returns +* TDX_OPERAND_BUSY. Currently the thread number limit for the TDISP extension +* is 1. So a mutex could work to prevent SEAMCALL busy returns. +*/ +static DEFINE_MUTEX (tdx_spdm_lock);
Yea I think this is a good solution to start. We can support enumerating multiple sessions in follow on work.
Quoted 22 lines
drivers/virt/coco/tdx-host/main.c +22
-0+ +static int tdx_spdm_xfer_handler (struct tdx_tsm_link *tlink,u64 sret, +u64 out_msg_sz) +{ +int ret; + +if (sret ==TDX_SPDM_REQUEST ) { + ret =tdx_spdm_msg_exchange (tlink); +if (ret <0 ) +return ret; + +return -EAGAIN ; + } + +if (sret ==TDX_SUCCESS ) +return 0 ; + +return -EIO ; +} + +static int tdx_spdm_session_connect (struct tdx_tsm_link *tlink) +{
drivers/virt/coco/tdx-host/main.c static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm) · line 122
Quoted 10 lines
drivers/virt/coco/tdx-host/main.c +10
-0+struct spdm_config_info_t *spdm_conf; +u64 sret, out_msg_sz; +int ret; + +/* create & fill SPDM configuration buffer - a single page */ + spdm_conf =kzalloc_obj (*spdm_conf); +if (!spdm_conf) +return -ENOMEM ; + + spdm_conf->certificate_slot_mask =0xff ;
Can we not have the magic number? I guess it means any?
Quoted 13 lines
drivers/virt/coco/tdx-host/main.c +13
-0+ +do { +mutex_lock (&tdx_spdm_lock); + sret =tdh_spdm_connect (tlink->spdm_id, +virt_to_page (spdm_conf), + tlink->in_msg, tlink->out_msg, +NULL , &out_msg_sz); +mutex_unlock (&tdx_spdm_lock); + ret =tdx_spdm_xfer_handler (tlink, sret, out_msg_sz); + }while (ret == -EAGAIN ); + +kfree (spdm_conf); +
drivers/virt/coco/tdx-host/main.c static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm) · line 145
Quoted 10 lines
drivers/virt/coco/tdx-host/main.c +10
-0+return ret; +} + +static void tdx_spdm_session_disconnect (struct tdx_tsm_link *tlink) +{ +u64 sret, out_msg_sz; +int ret; + + do { + mutex_lock(&tdx_spdm_lock);
The fact that TSM protects us from the buffers getting clobbered by another request, but we still need the mutex only around the call is a bit hidden. I'd think it at least needs a comment. But since the tdx_spdm_lock is really a TDX module specific limitation, why not put it inside the SEAMCALL wrappers? It seems more related to core TDX module setup type things.
Quoted 19 lines
drivers/virt/coco/tdx-host/main.c +10
-0+ sret =tdh_spdm_disconnect (tlink->spdm_id, tlink->in_msg, + tlink->out_msg, &out_msg_sz); +mutex_unlock (&tdx_spdm_lock); + ret =tdx_spdm_xfer_handler (tlink, sret, out_msg_sz); + }while (ret == -EAGAIN ); + +WARN_ON (ret); +} +static int tdx_spdm_create (struct tdx_tsm_link *tlink) {const struct tdx_sys_info *tdx_sysinfo =tdx_get_sysinfo (); +struct page *in_msg_page, *out_msg_page;struct tdx_hpa_list_info info;unsigned int nr_pages;u64 spdm_id, sret;@@ -70,12 +165,21 @@ static int tdx_spdm_create(struct tdx_tsm_link *tlink)if (!tdx_sysinfo)return -ENXIO ;
drivers/virt/coco/tdx-host/main.c static int tdx_spdm_create(struct tdx_tsm_link *tlink) · line 176
Quoted 10 lines
drivers/virt/coco/tdx-host/main.c +9
-0+ ret = -ENOMEM ; + in_msg_page =alloc_page (GFP_KERNEL | __GFP_ZERO); +if (!in_msg_page) +return ret; + + out_msg_page =alloc_page (GFP_KERNEL | __GFP_ZERO); +if (!out_msg_page) + goto out_free_in_msg; +
Maybe dumb question, but why do we need two buffers for this? After we post the request can't we just read it back the response to the same buffer? It doesn't seem DOE code is setup for that though.
Quoted 44 lines
drivers/virt/coco/tdx-host/main.c +13
-2nr_pages = tdx_sysinfo->tdx_connect.spdm_mt_page_count; spdm_mt =alloc_pages_exact (nr_pages *PAGE_SIZE ,GFP_KERNEL | __GFP_ZERO);if (!spdm_mt) -return -ENOMEM ; + goto out_free_out_msg; ret =tdx_hpa_list_info_setup (&info, spdm_mt, nr_pages);if (ret)@@ -92,11 +196,17 @@ static int tdx_spdm_create(struct tdx_tsm_link *tlink) tlink->spdm_id = spdm_id; tlink->spdm_mt = spdm_mt; + tlink->in_msg = in_msg_page; + tlink->out_msg = out_msg_page; tlink->spdm_mt_nr_pages = nr_pages;return 0 ; out_free_spdm_mt:free_pages_exact (spdm_mt, nr_pages *PAGE_SIZE ); +out_free_out_msg: +__free_page (out_msg_page); +out_free_in_msg: +__free_page (in_msg_page);return ret; }@@ -112,16 +222,33 @@ static void tdx_spdm_delete(struct tdx_tsm_link *tlink)return ; } +__free_page (tlink->in_msg); +__free_page (tlink->out_msg);free_pages_exact (tlink->spdm_mt, tlink->spdm_mt_nr_pages *PAGE_SIZE ); }static int tdx_spdm_session_setup (struct tdx_tsm_link *tlink) { -return tdx_spdm_create (tlink); +int ret; + + ret =tdx_spdm_create (tlink); +if (ret)
drivers/virt/coco/tdx-host/main.c static void tdx_spdm_delete(struct tdx_tsm_link *tlink) · line 245
Quoted 10 lines
drivers/virt/coco/tdx-host/main.c +10
-0+return ret; + + ret =tdx_spdm_session_connect (tlink); +if (ret) + goto out_spdm_delete; + +return 0 ; + +out_spdm_delete: +tdx_spdm_delete (tlink);
We might some basics explanation of the SPDM operations are. create, disconnect, delete, etc. What about adding some comments across the patches that make it clear when and where each needs to be called?
Quoted 9 lines
drivers/virt/coco/tdx-host/main.c +2
-0+return ret; }static void tdx_spdm_session_teardown (struct tdx_tsm_link *tlink) { +tdx_spdm_session_disconnect (tlink);tdx_spdm_delete (tlink); }
Re: [RFC PATCH 2/7] coco/tdx-host: Establish an SPDM session with the device
Quoted 6 lines · Rick P
Quoted 3 lines · Chao Gao
Setting up PCIe link encryption requires a secure channel to the device to exchange keys. This channel is an SPDM session over the device's PCI DOE mailbox.
Probably SPDM and DOE mailbox need some 10,000ft summary.
Does this sound better?
SPDM is an industry-standard protocol for establishing trust between components. For TDX trusted I/O, the TDX module uses it to authenticate the device, collect its measurements and exchange IDE keys with it.
DOE is a mailbox in the device's configuration space, through which software exchanges messages with the device. DOE is only a transport and can carry different protocols. SPDM is one of them.
Quoted 7 lines
Quoted 4 lines
Whenever the TDX module has a message for the device, the SEAMCALL returns with a dedicated status code. The host sends the message to the device and calls the SEAMCALL again with the device's response, until the SEAMCALL completes.
SEAMCALL completes? Could mean the full extension operation or a single seamcall execution.
I meant the whole operation, i.e. setting up or tearing down the SPDM session. Will reword to make this clear.
Quoted 5 lines
Quoted 2 lines
Call both SEAMCALLs in such a loop. The DOE transfer itself is a stub for now.
I'd put something higher up explaining this is laying the ground work, rather than announcing the gaps at the end.
OK. Will add one sentence right after the background to say that this patch lays the groundwork.
Quoted 4 lines
Quoted 2 lines
Pass the messages through two pages per device, one for each direction. One pair is enough because PCI/TSM serializes operations on a device.
This probably needs more explanation.
Several functions of a device may want to talk to the TDX module at the same time, and each of them may need SPDM message exchanges. But PCI/TSM serializes all operations on a device, so the pages are allocated once per device and not once per function.
@Yilun, let me know if this understanding is wrong.
Quoted 6 lines
Quoted 6 lines
drivers/virt/coco/tdx-host/main.c +6
-0+#define SPDM_CAP_HBEAT BIT(13) +#define SPDM_CAP_KEY_UPD BIT(14) + +struct spdm_config_info_t { +u32 vmm_spdm_cap; +u8 spdm_session_policy;
Quoted 8 lines
drivers/virt/coco/tdx-host/main.c static inline u32 tdisp_func_id(struct pci_dev *pdev) · line 56
Quoted 4 lines
drivers/virt/coco/tdx-host/main.c +4
-0+u8 certificate_slot_mask; +u8 raw_bitstream_requested; +u8 reserved[4089 ]; +} __packed;This looks like more SPDM specific stuff in TDX. Do really know other vendors need it? We should at least group it in a header such that it is clear which things are SPDM spec and which are TDX.
This structure is not from the SPDM spec. It is an ABI structure defined by the TDX module, and it is passed to TDH.SPDM.CONNECT.
But yes, there is some SPDM-specific stuff in this series. I will try to separate it from the TDX stuff.
Quoted 8 lines
drivers/virt/coco/tdx-host/main.c static inline u32 tdisp_func_id(struct pci_dev *pdev) · line 66
Quoted 2 lines
drivers/virt/coco/tdx-host/main.c +2
-0+struct page *in_msg; +struct page *out_msg;SNP and TDX both have the DOE mailbox shuffling done by the host, right? So why doesn't TSM allocate and provide these buffers?
Oh looking at the code I'm now very confused. The request for SNP is in private memory? The SNP commit has very little info to explain what is going on there.
TDX and SNP have different requirements for the buffers. TDX needs no special handling, and both pages are normal host memory. As far as I can tell from the code, SNP makes its request buffer a firmware-owned page. So it is not guest private memory, but it is not a normal host page either. I guess that is part of the reason why each TSM driver allocates its own buffers.
Quoted 3 lines
drivers/virt/coco/tdx-host/main.c static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm) · line 122
Quoted 1 line
drivers/virt/coco/tdx-host/main.c +1
-0+ spdm_conf->certificate_slot_mask =0xff ;Can we not have the magic number? I guess it means any?
Sure, will remove the magic number. 0xff means getting the certificates in all slots (0-7) of the device.
As a heads-up, this code will go away anyway when we switch to the new CTLV attestation framework.
Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ do { + mutex_lock(&tdx_spdm_lock);The fact that TSM protects us from the buffers getting clobbered by another request, but we still need the mutex only around the call is a bit hidden. I'd think it at least needs a comment. But since the
tdx_spdm_lockis really a TDX module specific limitation, why not put it inside the SEAMCALL wrappers? It seems more related to core TDX module setup type things.
I like this idea. The concurrency limit is on the SEAMCALLs themselves, not on the SPDM setup/teardown flow, so the lock fits better in the SEAMCALL wrappers.
Quoted 4 lines
Quoted 4 lines
drivers/virt/coco/tdx-host/main.c +4
-0+ ret = -ENOMEM ; + in_msg_page =alloc_page (GFP_KERNEL | __GFP_ZERO); +if (!in_msg_page) +return ret;
Quoted 8 lines
drivers/virt/coco/tdx-host/main.c static int tdx_spdm_create(struct tdx_tsm_link *tlink) · line 175
Quoted 4 lines
drivers/virt/coco/tdx-host/main.c +4
-0+ + out_msg_page =alloc_page (GFP_KERNEL | __GFP_ZERO); +if (!out_msg_page) + goto out_free_in_msg;Maybe dumb question, but why do we need two buffers for this? After we post the request can't we just read it back the response to the same buffer? It doesn't seem DOE code is setup for that though.
pci_doe() takes separate buffers for the request and the response, and so does TDH.SPDM.CONNECT.
Quoted 6 lines
drivers/virt/coco/tdx-host/main.c static void tdx_spdm_delete(struct tdx_tsm_link *tlink) · line 245
Quoted 2 lines
drivers/virt/coco/tdx-host/main.c +2
-0+out_spdm_delete: +tdx_spdm_delete (tlink);We might some basics explanation of the SPDM operations are. create, disconnect, delete, etc. What about adding some comments across the patches that make it clear when and where each needs to be called?
Yes. Will add a high-level description of the SPDM operations to the changelog, and comments above the functions where needed.
Edgecombe, Rick P
maintainerRe: [RFC PATCH 2/7] coco/tdx-host: Establish an SPDM session with the device
Quoted 6 lines · Chao Gao
Quoted 5 lines · Rick P
Quoted 3 lines · Chao Gao
Setting up PCIe link encryption requires a secure channel to the device to exchange keys. This channel is an SPDM session over the device's PCI DOE mailbox.
Quoted 8 lines
Quoted 1 line
Probably SPDM and DOE mailbox need some 10,000ft summary.
Does this sound better?
SPDM is an industry-standard protocol for establishing trust between components. For TDX trusted I/O, the TDX module uses it to authenticate the device, collect its measurements and exchange IDE keys with it.
Yea.
Quoted 4 lines
DOE is a mailbox in the device's configuration space, through which software exchanges messages with the device. DOE is only a transport and can carry different protocols. SPDM is one of them.
Can we explain that it is a PCIe thing too?
Quoted 11 lines
Quoted 8 lines
Quoted 5 lines
Whenever the TDX module has a message for the device, the SEAMCALL returns with a dedicated status code. The host sends the message to the device and calls the SEAMCALL again with the device's response, until the SEAMCALL completes.
SEAMCALL completes? Could mean the full extension operation or a single seamcall execution.
I meant the whole operation, i.e. setting up or tearing down the SPDM session. Will reword to make this clear.
Lets make it clear then.
Quoted 13 lines
Quoted 6 lines
Quoted 3 lines
Call both SEAMCALLs in such a loop. The DOE transfer itself is a stub for now.
I'd put something higher up explaining this is laying the ground work, rather than announcing the gaps at the end.
OK. Will add one sentence right after the background to say that this patch lays the groundwork.
Quoted 3 lines
Quoted 2 lines
Pass the messages through two pages per device, one for each direction. One pair is enough because PCI/TSM serializes operations on a device.
Quoted 6 lines
Quoted 1 line
This probably needs more explanation.
Several functions of a device may want to talk to the TDX module at the same time, and each of them may need SPDM message exchanges. But PCI/TSM serializes all operations on a device, so the pages are allocated once per device and not once per function.
I mean there is a buffer for request and a separate buffer for the response. Once the request is sent, why can't the same buffer be used to read back the response? This may be a lack of understanding by me of DOE.
Quoted 18 lines
@Yilun, let me know if this understanding is wrong.
Quoted 6 lines
Quoted 6 lines
drivers/virt/coco/tdx-host/main.c +6
-0+#define SPDM_CAP_HBEAT BIT(13) +#define SPDM_CAP_KEY_UPD BIT(14) + +struct spdm_config_info_t { +u32 vmm_spdm_cap; +u8 spdm_session_policy;Quoted 8 lines
drivers/virt/coco/tdx-host/main.c static inline u32 tdisp_func_id(struct pci_dev *pdev) · line 56
Quoted 4 lines
drivers/virt/coco/tdx-host/main.c +4
-0+u8 certificate_slot_mask; +u8 raw_bitstream_requested; +u8 reserved[4089 ]; +} __packed;This looks like more SPDM specific stuff in TDX. Do really know other vendors need it? We should at least group it in a header such that it is clear which things are SPDM spec and which are TDX.
Quoted 2 lines
This structure is not from the SPDM spec. It is an ABI structure defined by the TDX module, and it is passed to TDH.SPDM.CONNECT.
Ah ok, let's name it tdx something then.
Quoted 13 lines
But yes, there is some SPDM-specific stuff in this series. I will try to separate it from the TDX stuff.
Quoted 8 lines
drivers/virt/coco/tdx-host/main.c static inline u32 tdisp_func_id(struct pci_dev *pdev) · line 66
Quoted 2 lines
drivers/virt/coco/tdx-host/main.c +2
-0+struct page *in_msg; +struct page *out_msg;SNP and TDX both have the DOE mailbox shuffling done by the host, right? So why doesn't TSM allocate and provide these buffers?
Oh looking at the code I'm now very confused. The request for SNP is in private memory? The SNP commit has very little info to explain what is going on there.
Quoted 6 lines
TDX and SNP have different requirements for the buffers. TDX needs no special handling, and both pages are normal host memory. As far as I can tell from the code, SNP makes its request buffer a firmware-owned page. So it is not guest private memory, but it is not a normal host page either. I guess that is part of the reason why each TSM driver allocates its own buffers.
Yea that is how it looked to me too.
Quoted 49 lines
Quoted 4 lines
drivers/virt/coco/tdx-host/main.c static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm) · line 122
Quoted 2 lines
drivers/virt/coco/tdx-host/main.c +1
-0+ spdm_conf->certificate_slot_mask =0xff ;Can we not have the magic number? I guess it means any?
Sure, will remove the magic number.
0xffmeans getting the certificates in all slots (0-7) of the device.As a heads-up, this code will go away anyway when we switch to the new CTLV attestation framework.
Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ do { + mutex_lock(&tdx_spdm_lock);The fact that TSM protects us from the buffers getting clobbered by another request, but we still need the mutex only around the call is a bit hidden. I'd think it at least needs a comment. But since the
tdx_spdm_lockis really a TDX module specific limitation, why not put it inside the SEAMCALL wrappers? It seems more related to core TDX module setup type things.I like this idea. The concurrency limit is on the SEAMCALLs themselves, not on the SPDM setup/teardown flow, so the lock fits better in the SEAMCALL wrappers.
Quoted 4 lines
Quoted 4 lines
drivers/virt/coco/tdx-host/main.c +4
-0+ ret = -ENOMEM ; + in_msg_page =alloc_page (GFP_KERNEL | __GFP_ZERO); +if (!in_msg_page) +return ret;Quoted 8 lines
drivers/virt/coco/tdx-host/main.c static int tdx_spdm_create(struct tdx_tsm_link *tlink) · line 175
Quoted 4 lines
drivers/virt/coco/tdx-host/main.c +4
-0+ + out_msg_page =alloc_page (GFP_KERNEL | __GFP_ZERO); +if (!out_msg_page) + goto out_free_in_msg;Maybe dumb question, but why do we need two buffers for this? After we post the request can't we just read it back the response to the same buffer? It doesn't seem DOE code is setup for that though.
pci_doe()takes separate buffers for the request and the response, and so does TDH.SPDM.CONNECT.Quoted 6 lines
drivers/virt/coco/tdx-host/main.c static void tdx_spdm_delete(struct tdx_tsm_link *tlink) · line 245
Quoted 2 lines
drivers/virt/coco/tdx-host/main.c +2
-0+out_spdm_delete: +tdx_spdm_delete (tlink);We might some basics explanation of the SPDM operations are. create, disconnect, delete, etc. What about adding some comments across the patches that make it clear when and where each needs to be called?
Yes. Will add a high-level description of the SPDM operations to the changelog, and comments above the functions where needed.
Re: [RFC PATCH 2/7] coco/tdx-host: Establish an SPDM session with the device
Quoted 9 lines
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
Pass the messages through two pages per device, one for each direction. One pair is enough because PCI/TSM serializes operations on a device.
This probably needs more explanation.
Several functions of a device may want to talk to the TDX module at the same time, and each of them may need SPDM message exchanges. But PCI/TSM serializes all operations on a device, so the pages are allocated once per device and not once per function.
Correct. But we don't have to mention "per function" serialization for now, this is for secure passthrough of the TDI. For link encryption, the connect/disconnect operations only targets the device (physical function 0).
Though this is not the answer to Rick's concern...
Quoted 4 lines
I mean there is a buffer for request and a separate buffer for the response. Once the request is sent, why can't the same buffer be used to read back the response? This may be a lack of understanding by me of DOE.
I believe it is possible. The DOE protocol defines serialized request/response flow. And the pci_doe() code does so, the entire request payload must be sent before receiving any response payload.
I didn't try on real TDX module if tdh_spdm_connect/disconnect/mng can work with one buffer, but don't see reason why it can't.
Quoted 4 lines
Quoted 4 lines
@Yilun, let me know if this understanding is wrong.
[...]
Quoted 19 lines
Quoted 19 lines
Quoted 4 lines
Quoted 4 lines
drivers/virt/coco/tdx-host/main.c +4
-0+ ret = -ENOMEM ; + in_msg_page =alloc_page (GFP_KERNEL | __GFP_ZERO); +if (!in_msg_page) +return ret;Quoted 8 lines
drivers/virt/coco/tdx-host/main.c static int tdx_spdm_create(struct tdx_tsm_link *tlink) · line 175
Quoted 4 lines
drivers/virt/coco/tdx-host/main.c +4
-0+ + out_msg_page =alloc_page (GFP_KERNEL | __GFP_ZERO); +if (!out_msg_page) + goto out_free_in_msg;Maybe dumb question, but why do we need two buffers for this? After we post the request can't we just read it back the response to the same buffer? It doesn't seem DOE code is setup for that though.
pci_doe()takes separate buffers for the request and the response, and so does TDH.SPDM.CONNECT.Quoted 3 lines
Quoted 2 lines
drivers/virt/coco/tdx-host/main.c +2
-0+out_spdm_delete: +tdx_spdm_delete (tlink);
Re: [RFC PATCH 2/7] coco/tdx-host: Establish an SPDM session with the device
Quoted 6 lines · Xu Yilun
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
Pass the messages through two pages per device, one for each direction. One pair is enough because PCI/TSM serializes operations on a device.
This probably needs more explanation.
Though this is not the answer to Rick's concern...
Right, I misread Rick's comment as asking why one pair is enough.
Quoted 10 lines
Quoted 3 lines
I mean there is a buffer for request and a separate buffer for the response. Once the request is sent, why can't the same buffer be used to read back the response? This may be a lack of understanding by me of DOE.
I believe it is possible. The DOE protocol defines serialized request/response flow. And the
pci_doe()code does so, the entire request payload must be sent before receiving any response payload.I didn't try on real TDX module if tdh_spdm_connect/disconnect/mng can work with one buffer, but don't see reason why it can't.
Using the same buffer may work, but I do not think it is worth doing now. Both pci_doe() and the TDX module's SPDM SEAMCALLs take separate request and response buffers, and neither says that the two may overlap. The saving would only be one page per device. And the AMD implementation and the upcoming Arm series [1] use two buffers as well.
Re: [RFC PATCH 2/7] coco/tdx-host: Establish an SPDM session with the device
Quoted 18 lines · Chao Gao
Quoted 6 lines · Xu Yilun
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
Pass the messages through two pages per device, one for each direction. One pair is enough because PCI/TSM serializes operations on a device.
This probably needs more explanation.
Though this is not the answer to Rick's concern...
Right, I misread Rick's comment as asking why one pair is enough.
Quoted 8 lines
Quoted 3 lines
I mean there is a buffer for request and a separate buffer for the response. Once the request is sent, why can't the same buffer be used to read back the response? This may be a lack of understanding by me of DOE.
I believe it is possible. The DOE protocol defines serialized request/response flow. And the
pci_doe()code does so, the entire request payload must be sent before receiving any response payload.
Quoted 8 lines
Quoted 2 lines
I didn't try on real TDX module if tdh_spdm_connect/disconnect/mng can work with one buffer, but don't see reason why it can't.
Using the same buffer may work, but I do not think it is worth doing now. Both
pci_doe()and the TDX module's SPDM SEAMCALLs take separate request and response buffers, and neither says that the two may overlap. The saving would only be one page per device. And the AMD implementation and the upcoming Arm series [1] use two buffers as well.
I looked into the history of pci_doe() and the AMD implementation again, so here is a more complete answer.
Nobody has discussed why pci_doe() accepts two buffers on the list. The API has taken separate request and response buffers since its first posting [1].
AMD SEV-TIO [2] cannot share them. The firmware writes the request, so that buffer must be a firmware-owned page, which the host cannot write. The host writes the response, so that buffer must be host-writable. The pci_doe() was not designed for this, but it fits.
TDX has no such constraint, and both buffers are normal host pages. One buffer would likely work. But it would rely on pci_doe() sending the whole request before storing any of the response, and on the TDX module accepting the same page for both directions. Neither is documented. And the code would get harder to follow. The same page would be passed as both the const request and the response, which needs a comment to explain why it is safe. The SEAMCALL wrappers would either take the same page twice or drop a parameter and no longer match the SEAMCALL ABI. All of that would save one page per device, so I would keep two buffers.
Please let me know if you see it differently.
[1]: https://lore.kernel.org/linux-pci/7ced46eaf68bed71b6414a93ac41f26cfd54a991.1669608950.git.lukas@wunner.de/
[2]: https://docs.amd.com/v/u/en-US/58271_PUB_SEV-TIO Chapter 5.1
Edgecombe, Rick P
maintainerRe: [RFC PATCH 2/7] coco/tdx-host: Establish an SPDM session with the device
Quoted 11 lines · Chao Gao
I looked into the history of
pci_doe()and the AMD implementation again, so here is a more complete answer.Nobody has discussed why
pci_doe()accepts two buffers on the list. The API has taken separate request and response buffers since its first posting [1].AMD SEV-TIO [2] cannot share them. The firmware writes the request, so that buffer must be a firmware-owned page, which the host cannot write. The host writes the response, so that buffer must be host-writable. The
pci_doe()was not designed for this, but it fits.
So there was no discussion, but there is a good reason to have two different ones on the DOE side. What happens if we just pass the same buffer into the request and response?
Quoted 8 lines
TDX has no such constraint, and both buffers are normal host pages. One buffer would likely work. But it would rely on
pci_doe()sending the whole request before storing any of the response, and on the TDX module accepting the same page for both directions. Neither is documented. And the code would get harder to follow. The same page would be passed as both the const request and the response
Ah, ok. Yea that is too weird. Please ignore the above.
Quoted 4 lines
, which needs a comment to
explain why it is safe. The SEAMCALL wrappers would either take the same page twice or drop a parameter and no longer match the SEAMCALL ABI. All of that would save one page per device, so I would keep two buffers.
How about just add a log blurb like:
The DOE subsystem requires different request and response buffers, which is necessary to work with SNP.[n] Allocate two buffers on the TDX side to avoid introducing divergent DOE flows unified.
Quoted 5 lines
Please let me know if you see it differently.
[1]: https://lore.kernel.org/linux-pci/7ced46eaf68bed71b6414a93ac41f26cfd54a991.1669608950.git.lukas@wunner.de/
[2]: https://docs.amd.com/v/u/en-US/58271_PUB_SEV-TIO Chapter 5.1
Re: [RFC PATCH 2/7] coco/tdx-host: Establish an SPDM session with the device
In reply to Chao GaoQuoted 20 lines
Quoted 6 lines
Quoted 6 lines
drivers/virt/coco/tdx-host/main.c +6
-0+#define SPDM_CAP_HBEAT BIT(13) +#define SPDM_CAP_KEY_UPD BIT(14) + +struct spdm_config_info_t { +u32 vmm_spdm_cap; +u8 spdm_session_policy;Quoted 8 lines
drivers/virt/coco/tdx-host/main.c static inline u32 tdisp_func_id(struct pci_dev *pdev) · line 56
Quoted 4 lines
drivers/virt/coco/tdx-host/main.c +4
-0+u8 certificate_slot_mask; +u8 raw_bitstream_requested; +u8 reserved[4089 ]; +} __packed;This looks like more SPDM specific stuff in TDX. Do really know other vendors need it? We should at least group it in a header such that it is clear which things are SPDM spec and which are TDX.
This structure is not from the SPDM spec. It is an ABI structure defined by the TDX module, and it is passed to TDH.SPDM.CONNECT.
But yes, there is some SPDM-specific stuff in this series. I will try to separate it from the TDX stuff.
[...]
Quoted 9 lines
Quoted 3 lines
drivers/virt/coco/tdx-host/main.c static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm) · line 122
Quoted 1 line
drivers/virt/coco/tdx-host/main.c +1
-0+ spdm_conf->certificate_slot_mask =0xff ;Can we not have the magic number? I guess it means any?
Sure, will remove the magic number.
0xffmeans getting the certificates in all slots (0-7) of the device.As a heads-up, this code will go away anyway when we switch to the new CTLV attestation framework.
So it seems we don't need any spdm_config_info_t definition for now. Should we keep the definition or just to provide a zero page to avoid unnecessary explanations here?
Re: [RFC PATCH 2/7] coco/tdx-host: Establish an SPDM session with the device
Quoted 7 lines · Xu Yilun
Quoted 7 lines
Quoted 3 lines
drivers/virt/coco/tdx-host/main.c static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm) · line 122
Quoted 1 line
drivers/virt/coco/tdx-host/main.c +1
-0+ spdm_conf->certificate_slot_mask =0xff ;Can we not have the magic number? I guess it means any?
Sure, will remove the magic number.
0xffmeans getting the certificates in all slots (0-7) of the device.
Quoted 6 lines
Quoted 2 lines
As a heads-up, this code will go away anyway when we switch to the new CTLV attestation framework.
So it seems we don't need any
spdm_config_info_tdefinition for now. Should we keep the definition or just to provide a zero page to avoid unnecessary explanations here?
The certificate_slot_mask should be 0x1 so that we can get the certificate in slot 0, right?
If so, I would keep the definition and pass 0x1.
Re: [RFC PATCH 2/7] coco/tdx-host: Establish an SPDM session with the device
Quoted 11 lines · Chao Gao
Quoted 10 lines · Xu Yilun
Quoted 9 lines
Quoted 3 lines
drivers/virt/coco/tdx-host/main.c static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm) · line 122
Quoted 1 line
drivers/virt/coco/tdx-host/main.c +1
-0+ spdm_conf->certificate_slot_mask =0xff ;Can we not have the magic number? I guess it means any?
Sure, will remove the magic number.
0xffmeans getting the certificates in all slots (0-7) of the device.As a heads-up, this code will go away anyway when we switch to the new CTLV attestation framework.
Quoted 6 lines
Quoted 3 lines
So it seems we don't need any
spdm_config_info_tdefinition for now. Should we keep the definition or just to provide a zero page to avoid unnecessary explanations here?The
certificate_slot_maskshould be0x1so that we can get the certificate in slot 0, right?
No. I think Jason's point is that, the certifications that didn't get signed/verified during SPDM negociation should not been returned. The TDX module manages SPDM negociation. So instead of the VMM asks for the slots it wants, the TDX module should return all available signed slots, which is slot0 for now.
I think we don't need this certificate_slot_mask param any more.
Quoted 2 lines
If so, I would keep the definition and pass
0x1.
RFC PATCH 3/7 coco/tdx-host: Exchange SPDM messages through DOE
In reply to Chao GaoThe TDX module cannot access a device's DOE mailbox, so the kernel relays SPDM messages between the TDX module and the device. The TDX module produces and consumes whole DOE data objects, header included, but pci_tsm_doe_transfer() takes only the payload and builds the header itself.
Define struct pci_doe_header and struct pci_doe_msg to describe a DOE data object. Strip the header from each request before passing the payload to pci_tsm_doe_transfer(), and rebuild the header around the device's response before handing it back to the TDX module.
An alternative is to add a PCI DOE API that takes whole data objects. This would avoid the conversion, but the TDX-TSM driver would be its only user.
Based on work by Yilun.
AI suggested defining the DOE header and object structures in place of the bit operations, and dropping the endian conversions.
--- drivers/virt/coco/tdx-host/main.c | 49 ++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-)
drivers/virt/coco/tdx-host/main.c +48 -1
@@ -11,6 +11,7 @@#include <linux/module.h> #include <linux/sysfs.h> #include <linux/pci.h> +#include <linux/pci-doe.h> #include <linux/pci-tsm.h> #include <linux/tsm.h>
@@ -73,9 +74,55 @@ static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm) return container_of(tsm, struct tdx_tsm_link, pci.base_tsm); } +struct pci_doe_header { +u16 vendor; +u8 type; +u8 rsvd0; +u32 length:18 ;/* in DWORDs */ +u32 rsvd1:14 ; +} __packed; + +struct pci_doe_msg { +struct pci_doe_header header; +u8 payload[]; +} __packed; + +/* +* The TDX module produces and consumes complete DOE data objects, but +* pci_doe() takes only the payload and builds the header itself. Strip +* the header from the request and rebuild it for the response. +*/ static int tdx_spdm_msg_exchange (struct tdx_tsm_link *tlink) { -/* TODO: implement the PCI DOE transfer */ +struct pci_dev *pdev = tlink->pci.base_tsm.pdev; +struct pci_doe_msg *req =page_address (tlink->out_msg); +struct pci_doe_msg *resp =page_address (tlink->in_msg); + +size_t req_sz = req->header.length *sizeof (u32); +size_t resp_sz =PAGE_SIZE ; + +size_t req_payload_sz = req_sz -sizeof (struct pci_doe_header ); +size_t resp_payload_sz = resp_sz -sizeof (struct pci_doe_header ); + +void *resp_payload_addr = resp->payload; +void *req_payload_addr = req->payload; + +u8 type = req->header.type; + +int ret; + + ret =pci_tsm_doe_transfer (pdev, type, + req_payload_addr, req_payload_sz, + resp_payload_addr, resp_payload_sz); +if (ret <0 ) { +pci_err (pdev,"spdm msg exchange fail %d\n" , ret); +return ret; + } + + resp->header.vendor =PCI_VENDOR_ID_PCI_SIG ; + resp->header.type = type; + resp->header.length = (ret +sizeof (struct pci_doe_header )) /sizeof (u32); +return 0 ; }
Signature · 1 line
2.52.0
Edgecombe, Rick P
maintainerRe: [RFC PATCH 3/7] coco/tdx-host: Exchange SPDM messages through DOE
drivers/virt/coco/tdx-host/main.c static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm) · line 88
Quoted 12 lines · Chao Gao
drivers/virt/coco/tdx-host/main.c +12
-0+struct pci_doe_header { +u16 vendor; +u8 type; +u8 rsvd0; +u32 length:18 ;/* in DWORDs */ +u32 rsvd1:14 ; +} __packed; + +struct pci_doe_msg { +struct pci_doe_header header; +u8 payload[]; +} __packed;
You mentioned DOE rebuilds the header. It looks like it doesn't have structs like these because it uses FIELD_PREP with masks. So then we just have duplicate definitions of the format. Ugh.
Quoted 15 lines
drivers/virt/coco/tdx-host/main.c +6
-0+ +/* +* The TDX module produces and consumes complete DOE data objects, but +* pci_doe() takes only the payload and builds the header itself. Strip +* the header from the request and rebuild it for the response. +*/ static int
tdx_spdm_msg_exchange(struct tdx_tsm_link *tlink){drivers/virt/coco/tdx-host/main.c +6
-1-/* TODO: implement the PCI DOE transfer */ +struct pci_dev *pdev = tlink->pci.base_tsm.pdev; + struct pci_doe_msg *req = page_address(tlink->out_msg); +struct pci_doe_msg *resp =page_address (tlink->in_msg); + +size_t req_sz = req->header.length *sizeof (u32); +size_t resp_sz =PAGE_SIZE ;
drivers/virt/coco/tdx-host/main.c static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm) · line 112
Quoted 10 lines
drivers/virt/coco/tdx-host/main.c +10
-0+ + size_t req_payload_sz = req_sz - sizeof(struct pci_doe_header); +size_t resp_payload_sz = resp_sz -sizeof (struct pci_doe_header ); + +void *resp_payload_addr = resp->payload; +void *req_payload_addr = req->payload; + +u8 type = req->header.type; + +int ret;
Yea, this is just really unfortunate like you pointed in the coverletter. We unwrap the header using all of this gunk to pass it through and then rebuild the header on the other side.
resp_payload_addr, and req_payload_addr seem unnecessary. It's not any shorter in the function over casing to (void *).
Quoted 16 lines
drivers/virt/coco/tdx-host/main.c +13
-0+ + ret =pci_tsm_doe_transfer (pdev, type, + req_payload_addr, req_payload_sz, + resp_payload_addr, resp_payload_sz); +if (ret <0 ) { +pci_err (pdev,"spdm msg exchange fail %d\n" , ret); +return ret; + } + + resp->header.vendor =PCI_VENDOR_ID_PCI_SIG ; + resp->header.type = type; + resp->header.length = (ret +sizeof (struct pci_doe_header )) /sizeof (u32); +return 0;
}
Re: [RFC PATCH 3/7] coco/tdx-host: Exchange SPDM messages through DOE
Quoted 17 lines · Rick P
drivers/virt/coco/tdx-host/main.c static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm) · line 88
Quoted 12 lines · Chao Gao
drivers/virt/coco/tdx-host/main.c +12
-0+struct pci_doe_header { +u16 vendor; +u8 type; +u8 rsvd0; +u32 length:18 ;/* in DWORDs */ +u32 rsvd1:14 ; +} __packed; + +struct pci_doe_msg { +struct pci_doe_header header; +u8 payload[]; +} __packed;You mentioned DOE rebuilds the header. It looks like it doesn't have structs like these because it uses
FIELD_PREPwith masks. So then we just have duplicate definitions of the format. Ugh.
Yes. The earlier version [1] used the masks, and I found all the le32_to_cpu() and FIELD_PREP() hard to read. I would prefer to keep the structs as long as this code stays in the TDX-TSM driver.
Quoted 3 lines
Yea, this is just really unfortunate like you pointed in the coverletter. We unwrap the header using all of this gunk to pass it through and then rebuild the header on the other side.
I am now leaning towards moving this code into PCI/DOE, even though TDX would be its only user. Stripping and rebuilding the header is DOE format handling, and the PCI/DOE core already has the definitions and the code for it. A TSM driver should not need a second copy, no matter how many users there are. That would also remove the duplicate definitions above.
Quoted 5 lines
drivers/virt/coco/tdx-host/main.c static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm) · line 108
Quoted 2 lines
drivers/virt/coco/tdx-host/main.c +2
-0+void *resp_payload_addr = resp->payload; +void *req_payload_addr = req->payload;
resp_payload_addr, andreq_payload_addrseem unnecessary. It's not any shorter in the function over casing to (void *).
Sure, will do.
Edgecombe, Rick P
maintainerRe: [RFC PATCH 3/7] coco/tdx-host: Exchange SPDM messages through DOE
Quoted 14 lines · Chao Gao
Quoted 4 lines
You mentioned DOE rebuilds the header. It looks like it doesn't have structs like these because it uses
FIELD_PREPwith masks. So then we just have duplicate
definitions of the format. Ugh.Yes. The earlier version [1] used the masks, and I found all the
le32_to_cpu()andFIELD_PREP()hard to read. I would prefer to keep the structs as long as this code stays in the TDX-TSM driver.Quoted 4 lines
Yea, this is just really unfortunate like you pointed in the coverletter. We unwrap the header using all of this gunk to pass it through and then rebuild the
header on the other side.
Quoted 6 lines
I am now leaning towards moving this code into PCI/DOE, even though TDX would be its only user. Stripping and rebuilding the header is DOE format handling, and the PCI/DOE core already has the definitions and the code for it. A TSM driver should not need a second copy, no matter how many users there are. That would also remove the duplicate definitions above.
Can you elaborate on your proposal? What exactly moves to DOE and how does it interop with the other DOE callers?
Re: [RFC PATCH 3/7] coco/tdx-host: Exchange SPDM messages through DOE
Quoted 10 lines · Rick P
Quoted 6 lines · Chao Gao
I am now leaning towards moving this code into PCI/DOE, even though TDX would be its only user. Stripping and rebuilding the header is DOE format handling, and the PCI/DOE core already has the definitions and the code for it. A TSM driver should not need a second copy, no matter how many users there are. That would also remove the duplicate definitions above.
Can you elaborate on your proposal? What exactly moves to DOE and how does it interop with the other DOE callers?
The header stripping and rebuilding moves to drivers/pci/doe.c, as a new helper (e.g., pci_doe_raw()). struct pci_doe_header and struct pci_doe_msg defined in this patch go away. This avoids a second definition of the DOE format.
pci_doe_raw() is a wrapper around pci_doe(). The other callers still use pci_doe() and are not affected.
I explored the idea with AI. It generated:
int pci_doe_raw(struct pci_doe_mb *doe_mb, const void *request,
size_t request_sz, void *response, size_t response_sz)
{
const __le32 *req_hdr = request;
__le32 *resp_hdr = response;
size_t hdr_sz = 2 * sizeof(__le32);
size_t length;
u32 hdr1;
int rc;
if (request_sz < hdr_sz || response_sz < hdr_sz)
return -EINVAL;
hdr1 = le32_to_cpu(req_hdr[0]);
length = FIELD_GET(PCI_DOE_DATA_OBJECT_HEADER_2_LENGTH,
le32_to_cpu(req_hdr[1]));
/* A value of 0x0 indicates max data object length */
if (!length)
length = PCI_DOE_MAX_LENGTH;
if (length < 2 || length * sizeof(__le32) > request_sz)
return -EINVAL;
rc = pci_doe(doe_mb, FIELD_GET(PCI_DOE_DATA_OBJECT_HEADER_1_VID, hdr1),
FIELD_GET(PCI_DOE_DATA_OBJECT_HEADER_1_TYPE, hdr1),
request + hdr_sz, length * sizeof(__le32) - hdr_sz,
response + hdr_sz, response_sz - hdr_sz);
if (rc < 0)
return rc;
/* pci_doe() has verified that the response has the same VID and Type */
length = 2 + DIV_ROUND_UP(rc, sizeof(__le32));
resp_hdr[0] = cpu_to_le32(hdr1 & (PCI_DOE_DATA_OBJECT_HEADER_1_VID |
PCI_DOE_DATA_OBJECT_HEADER_1_TYPE));
resp_hdr[1] = cpu_to_le32(FIELD_PREP(PCI_DOE_DATA_OBJECT_HEADER_2_LENGTH,
length % PCI_DOE_MAX_LENGTH));
return rc + hdr_sz;
}tdx_spdm_msg_exchange() becomes:
static int tdx_spdm_msg_exchange(struct tdx_tsm_link *tlink)
{
struct pci_dev *pdev = tlink->pci.base_tsm.pdev;
int ret;
ret = pci_doe_raw(tlink->pci.doe_mb,
page_address(tlink->out_msg), PAGE_SIZE,
page_address(tlink->in_msg), PAGE_SIZE);
if (ret < 0) {
pci_err(pdev, "spdm msg exchange fail %d\n", ret);
return ret;
}
return 0;
}Edgecombe, Rick P
maintainerRe: [RFC PATCH 3/7] coco/tdx-host: Exchange SPDM messages through DOE
Quoted 15 lines · Chao Gao
static int
tdx_spdm_msg_exchange(struct tdx_tsm_link *tlink){ struct pci_dev *pdev = tlink->pci.base_tsm.pdev; int ret; ret = pci_doe_raw(tlink->pci.doe_mb, page_address(tlink->out_msg), PAGE_SIZE, page_address(tlink->in_msg), PAGE_SIZE); if (ret < 0) { pci_err(pdev, "spdm msg exchange fail %d\n", ret); return ret; } return 0; }
If the DOE people are ok with this, it does seem much better to me on our side.
Re: [RFC PATCH 3/7] coco/tdx-host: Exchange SPDM messages through DOE
Quoted 14 lines · Rick P
Quoted 13 lines · Chao Gao
static int
tdx_spdm_msg_exchange(struct tdx_tsm_link *tlink){ struct pci_dev *pdev = tlink->pci.base_tsm.pdev; int ret; ret = pci_doe_raw(tlink->pci.doe_mb, page_address(tlink->out_msg), PAGE_SIZE, page_address(tlink->in_msg), PAGE_SIZE); if (ret < 0) { pci_err(pdev, "spdm msg exchange fail %d\n", ret); return ret; }
Quoted 4 lines
Quoted 2 lines
return 0; }If the DOE people are ok with this, it does seem much better to me on our side.
I'm not the DOE people but I have some concern about this raw transfering interface. The kernel manages the DOE layer, the PCI DOE capability and the discovery protocol.. The firmware has no access these info. It is questionable that the firmware constructs the DOE packet without knowing all the info. Even today the firmware can blindly fill the packet right, no guarantee it still can as the discovery protocol version advances.
I lean to not having TDX module create the DOE header. Let the kernel do it.
Edgecombe, Rick P
maintainerRe: [RFC PATCH 3/7] coco/tdx-host: Exchange SPDM messages through DOE
Quoted 20 lines · Xu Yilun
Quoted 14 lines · Rick P
Quoted 13 lines · Chao Gao
static int
tdx_spdm_msg_exchange(struct tdx_tsm_link *tlink){
structpci_dev*pdev =tlink->pci.base_tsm.pdev; int ret;ret =
pci_doe_raw(tlink->pci.doe_mb, page_address(tlink->out_msg), PAGE_SIZE, page_address(tlink->in_msg), PAGE_SIZE); if (ret < 0) {
pci_err(pdev, "spdm msg exchange fail %d\n", ret); return ret;
}Quoted 4 lines
Quoted 2 lines
return 0;
}If the DOE people are ok with this, it does seem much better to me on our side.
Quoted 7 lines
I'm not the DOE people but I have some concern about this raw transfering interface. The kernel manages the DOE layer, the PCI DOE capability and the discovery protocol.. The firmware has no access these info. It is questionable that the firmware constructs the DOE packet without knowing all the info. Even today the firmware can blindly fill the packet right, no guarantee it still can as the discovery protocol version advances.
Hmm yes this seems like good reasoning.
Quoted 3 lines
I lean to not having TDX module create the DOE header. Let the kernel do it.
Im ok with it.
Re: [RFC PATCH 3/7] coco/tdx-host: Exchange SPDM messages through DOE
In reply to Chao GaoQuoted 1 line
static int
tdx_spdm_msg_exchange(struct tdx_tsm_link *tlink)
I remember there is a comment in internal review that suggests we use struct pci_dev *pdev for this function, to explicit this is purly about PCI, nothing to do with TSM firmware. I accept it at that time and still feel good today. How do you think?
Quoted 38 lines
{
drivers/virt/coco/tdx-host/main.c +29
-1-/* TODO: implement the PCI DOE transfer */ +struct pci_dev *pdev = tlink->pci.base_tsm.pdev; +struct pci_doe_msg *req =page_address (tlink->out_msg); +struct pci_doe_msg *resp =page_address (tlink->in_msg); + +size_t req_sz = req->header.length *sizeof (u32); +size_t resp_sz =PAGE_SIZE ; + +size_t req_payload_sz = req_sz -sizeof (struct pci_doe_header ); +size_t resp_payload_sz = resp_sz -sizeof (struct pci_doe_header ); + +void *resp_payload_addr = resp->payload; +void *req_payload_addr = req->payload; + +u8 type = req->header.type; + +int ret; + + ret =pci_tsm_doe_transfer (pdev, type, + req_payload_addr, req_payload_sz, + resp_payload_addr, resp_payload_sz); +if (ret <0 ) { +pci_err (pdev,"spdm msg exchange fail %d\n" , ret); +return ret; + } + + resp->header.vendor =PCI_VENDOR_ID_PCI_SIG ; + resp->header.type = type; + resp->header.length = (ret +sizeof (struct pci_doe_header )) /sizeof (u32); +return 0 ; }Signature · 2 lines
-- 2.52.0
Re: [RFC PATCH 3/7] coco/tdx-host: Exchange SPDM messages through DOE
Quoted 6 lines · Xu Yilun
Quoted 1 line
static int
tdx_spdm_msg_exchange(struct tdx_tsm_link *tlink)I remember there is a comment in internal review that suggests we use struct
pci_dev*pdev for this function, to explicit this is purly about PCI, nothing to do with TSM firmware. I accept it at that time and still feel good today. How do you think?
I would keep the tlink for this static function. The pdev and both message buffers come from it, so with a pdev parameter the caller has to pull every argument out of the tlink. The function relays the TDX module's messages, so it cannot be made completely unrelated to the TSM firmware. If other code without a tlink needs to call it later, making it generic then is fine.
RFC PATCH 4/7 coco/tdx-host: Pass a device info buffer to tdh_spdm_connect()
In reply to Chao GaoTDH.SPDM.CONNECT requires a host-provided buffer, into which the TDX module writes information about the device.
TDs will use the information to attest the device, which is not supported yet. The kernel doesn't use it for now, so treat it as opaque data.
Take the buffer size from the TDX global metadata, and keep the buffer for the lifetime of the SPDM session.
Based on work by Yilun.
[This patch will be reworked when the series moves to the CTLV
attestation flow.]
--- arch/x86/include/asm/tdx_global_metadata.h | 1 + arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 6 +++++ drivers/virt/coco/tdx-host/main.c | 27 +++++++++++++++++++-- 3 files changed, 32 insertions(+), 2 deletions(-)
arch/x86/include/asm/tdx_global_metadata.h +1 -0
@@ -54,6 +54,7 @@ struct tdx_sys_info_ext {struct tdx_sys_info_connect {u16 spdm_mt_page_count;u16 iommu_mt_page_count; +u16 spdm_max_dev_info_pages; };struct tdx_sys_info {
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +6 -0
@@ -156,6 +156,12 @@ static __init int get_tdx_sys_info_connect(struct tdx_sys_info_connect *sysinfo_ sysinfo_connect->iommu_mt_page_count = val; + ret =read_sys_metadata_field (0x3000000100000007 , &val); +if (ret) +return ret; + + sysinfo_connect->spdm_max_dev_info_pages = val; +return 0 ; }
drivers/virt/coco/tdx-host/main.c +25 -2
@@ -67,6 +67,8 @@ struct tdx_tsm_link {struct page *out_msg;unsigned int spdm_mt_nr_pages;void *spdm_mt; +unsigned int dev_info_nr_pages; +void *dev_info; };static struct tdx_tsm_link *to_tdx_tsm_link (struct pci_tsm *tsm)
@@ -158,6 +160,7 @@ static int tdx_spdm_xfer_handler(struct tdx_tsm_link *tlink, u64 sret,static int tdx_spdm_session_connect (struct tdx_tsm_link *tlink) {struct spdm_config_info_t *spdm_conf; +struct tdx_hpa_list_info info;u64 sret, out_msg_sz;int ret;
@@ -168,16 +171,23 @@ static int tdx_spdm_session_connect(struct tdx_tsm_link *tlink) spdm_conf->certificate_slot_mask =0xff ; + ret =tdx_hpa_list_info_setup (&info, tlink->dev_info, tlink->dev_info_nr_pages); +if (ret) + goto out_free_spdm_conf; +do {mutex_lock (&tdx_spdm_lock); sret =tdh_spdm_connect (tlink->spdm_id,virt_to_page (spdm_conf), tlink->in_msg, tlink->out_msg, -NULL , &out_msg_sz); + &info, &out_msg_sz);mutex_unlock (&tdx_spdm_lock); ret =tdx_spdm_xfer_handler (tlink, sret, out_msg_sz); }while (ret == -EAGAIN ); +tdx_hpa_list_info_free (&info); + +out_free_spdm_conf:kfree (spdm_conf);return ret;
@@ -203,9 +213,11 @@ static int tdx_spdm_create(struct tdx_tsm_link *tlink) {const struct tdx_sys_info *tdx_sysinfo =tdx_get_sysinfo ();struct page *in_msg_page, *out_msg_page; +unsigned int dev_info_nr_pages;struct tdx_hpa_list_info info;unsigned int nr_pages;u64 spdm_id, sret; +void *dev_info;void *spdm_mt;int ret;
@@ -222,11 +234,17 @@ static int tdx_spdm_create(struct tdx_tsm_link *tlink) goto out_free_in_msg; nr_pages = tdx_sysinfo->tdx_connect.spdm_mt_page_count; + dev_info_nr_pages = tdx_sysinfo->tdx_connect.spdm_max_dev_info_pages; + + dev_info =alloc_pages_exact (dev_info_nr_pages *PAGE_SIZE , +GFP_KERNEL | __GFP_ZERO); +if (!dev_info) + goto out_free_out_msg; spdm_mt =alloc_pages_exact (nr_pages *PAGE_SIZE ,GFP_KERNEL | __GFP_ZERO);if (!spdm_mt) - goto out_free_out_msg; + goto out_free_dev_info; ret =tdx_hpa_list_info_setup (&info, spdm_mt, nr_pages);if (ret)
@@ -246,10 +264,14 @@ static int tdx_spdm_create(struct tdx_tsm_link *tlink) tlink->in_msg = in_msg_page; tlink->out_msg = out_msg_page; tlink->spdm_mt_nr_pages = nr_pages; + tlink->dev_info = dev_info; + tlink->dev_info_nr_pages = dev_info_nr_pages;return 0 ; out_free_spdm_mt:free_pages_exact (spdm_mt, nr_pages *PAGE_SIZE ); +out_free_dev_info: +free_pages_exact (dev_info, dev_info_nr_pages *PAGE_SIZE ); out_free_out_msg:__free_page (out_msg_page); out_free_in_msg:
@@ -272,6 +294,7 @@ static void tdx_spdm_delete(struct tdx_tsm_link *tlink)__free_page (tlink->in_msg);__free_page (tlink->out_msg);free_pages_exact (tlink->spdm_mt, tlink->spdm_mt_nr_pages *PAGE_SIZE ); +free_pages_exact (tlink->dev_info, tlink->dev_info_nr_pages *PAGE_SIZE ); }static int tdx_spdm_session_setup (struct tdx_tsm_link *tlink)
Signature · 1 line
2.52.0
RFC PATCH 5/7 coco/tdx-host: Configure an IDE stream
In reply to Chao GaoEstablishing a PCIe Integrity and Data Encryption (IDE) stream has two parts. The stream is first provisioned in the config space of both Partner Ports (the root port and the device), and the keys are then programmed via IDE Key Management (IDE_KM).
Commit 1e4d2ff3ae45 ("PCI/IDE: Add IDE establishment helpers") added the pci_ide_stream_*() helpers for provisioning. They split it into fine-grained allocate, register and setup steps to accommodate differences between SEV-TIO and TDX Connect, such as who programs the root port and who allocates the stream ID.
Provision the stream with these helpers. IDE_KM and enabling the stream are left to later changes.
The TDX module owns the root port's IDE registers and provides TDH.IDE.STREAM.CREATE for the host to program them. Configure the root port with the SEAMCALL, passing the register values as parameters, and write only the device's IDE registers from the host.
An IDE stream can be associated with address ranges, so that traffic to different ranges, such as those of different devices, goes through different streams with different keys. A stream can be associated with multiple ranges, but TDX supports only one. Keep the prefetchable range and drop the non-prefetchable one. Export pci_ide_stream_to_regs() to generate the root port's register values from the adjusted settings.
TDH.IDE.STREAM.CREATE also returns the stream ID that the TDX module assigns. Configure the device with the same ID.
Based on work by Yilun.
--- drivers/pci/ide.c | 5 +- drivers/virt/coco/tdx-host/main.c | 147 ++++++++++++++++++++++++++++++ include/linux/pci-ide.h | 2 + 3 files changed, 152 insertions(+), 2 deletions(-)
drivers/pci/ide.c +3 -2
@@ -550,8 +550,8 @@ static void mem_assoc_to_regs(struct pci_bus_region *region,* @ide: registered IDE settings descriptor * @regs: output register values */ -static void pci_ide_stream_to_regs (struct pci_dev *pdev,struct pci_ide *ide, -struct pci_ide_regs *regs) +void pci_ide_stream_to_regs (struct pci_dev *pdev,struct pci_ide *ide, +struct pci_ide_regs *regs) {struct pci_ide_partner *settings =pci_ide_to_settings (pdev, ide);int assoc_idx =0 ;
@@ -580,6 +580,7 @@ static void pci_ide_stream_to_regs(struct pci_dev *pdev, struct pci_ide *ide, regs->nr_addr = assoc_idx; } +EXPORT_SYMBOL_GPL (pci_ide_stream_to_regs);/** * pci_ide_stream_setup() - program settings to Selective IDE Stream registers
drivers/virt/coco/tdx-host/main.c +147 -0
179 lines not on this page · raw message
include/linux/pci-ide.h +2 -0
@@ -104,6 +104,8 @@ struct pci_ide {void pci_ide_set_nr_streams (struct pci_host_bridge *hb,u16 nr);struct pci_ide_partner *pci_ide_to_settings (struct pci_dev *pdev,struct pci_ide *ide); +void pci_ide_stream_to_regs (struct pci_dev *pdev,struct pci_ide *ide, +struct pci_ide_regs *regs);struct pci_ide *pci_ide_stream_alloc (struct pci_dev *pdev);void pci_ide_stream_free (struct pci_ide *ide);int pci_ide_stream_register (struct pci_ide *ide);
Signature · 1 line
2.52.0
Re: [RFC PATCH 5/7] coco/tdx-host: Configure an IDE stream
Quoted 6 lines
drivers/virt/coco/tdx-host/main.c +6
-0+static int tdx_ide_stream_setup (struct tdx_tsm_link *tlink) +{ +struct pci_dev *pdev = tlink->pci.base_tsm.pdev; +struct pci_ide *ide; +int ret; +
drivers/virt/coco/tdx-host/main.c static void tdx_spdm_session_teardown(struct tdx_tsm_link *tlink) · line 429
Quoted 10 lines
drivers/virt/coco/tdx-host/main.c +10
-0+ ide =pci_ide_stream_alloc (pdev); +if (!ide) +return -ENOMEM ; + +/* Configure IDE capability for RP & get stream_id */ + ret =tdx_ide_stream_create (tlink, ide); +if (ret) + goto out_pci_ide_stream_free; + + ide->stream_id = tlink->stream_id;
Nits:
Maybe we don't save stream_id in tlink, it can later be accessed by tlink->ide->stream_id.
And the function could be like:
ret = tdx_ide_stream_create(tlink, ide, &stream_id);
which aligns better with the comment "get stream_id".
Re: [RFC PATCH 5/7] coco/tdx-host: Configure an IDE stream
Quoted 9 lines · Xu Yilun
drivers/virt/coco/tdx-host/main.c static void tdx_spdm_session_teardown(struct tdx_tsm_link *tlink) · line 429
Quoted 6 lines
drivers/virt/coco/tdx-host/main.c +6
-0+/* Configure IDE capability for RP & get stream_id */ + ret =tdx_ide_stream_create (tlink, ide); +if (ret) + goto out_pci_ide_stream_free; + + ide->stream_id = tlink->stream_id;Nits:
Quoted 8 lines
Maybe we don't save
stream_idin tlink, it can later be accessed bytlink->ide->stream_id.And the function could be like:
ret = tdx_ide_stream_create(tlink, ide, &stream_id);which aligns better with the comment "get
stream_id".
Looks good to me. Caching the stream_id in tlink needs a justification, and I don't see a good one.
As I said in [1], I plan to drop func_id as well, because it can be derived from the pci_dev at runtime.
RFC PATCH 6/7 coco/tdx-host: Program IDE keys and enable the IDE stream
In reply to Chao GaoWith the IDE stream provisioned on root ports and the device, the stream keys are next programmed via IDE_KM, and then the stream is enabled. The trusted firmware (TSM) programs the keys into the root port, and sends them to the device as IDE_KM messages over the SPDM session.
On TDX, TDH.IDE.STREAM.KM does both, and also enables the stream on the root port. Like TDH.SPDM.CONNECT, it returns to the host whenever it has an IDE_KM message for the device. Call it in the same loop that relays the messages through the DOE mailbox.
Unlike TDH.SPDM.CONNECT, TDH.IDE.STREAM.KM is not subject to the TDX module's concurrency limit, so don't serialize it with the mutex.
After the stream gets enabled on the root port, enable the stream on the device from the host.
Teardown reverses these steps.
Based on work by Yilun.
--- drivers/virt/coco/tdx-host/main.c | 48 +++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)
drivers/virt/coco/tdx-host/main.c +48 -0
@@ -327,6 +327,37 @@ static void tdx_spdm_session_teardown(struct tdx_tsm_link *tlink)tdx_spdm_delete (tlink); } +enum tdx_ide_stream_km_op { +TDX_IDE_STREAM_KM_SETUP =0 , +TDX_IDE_STREAM_KM_STOP =2 , +}; + +static int tdx_ide_stream_km (struct tdx_tsm_link *tlink, +enum tdx_ide_stream_km_op op) +{ +u64 sret, out_msg_sz; +int ret; + +do { + sret =tdh_ide_stream_km (tlink->spdm_id, tlink->stream_id, op, + tlink->in_msg, tlink->out_msg, + &out_msg_sz); + ret =tdx_spdm_xfer_handler (tlink, sret, out_msg_sz); + }while (ret == -EAGAIN ); + +return ret; +} + +static int tdx_ide_stream_key_program (struct tdx_tsm_link *tlink) +{ +return tdx_ide_stream_km (tlink,TDX_IDE_STREAM_KM_SETUP ); +} + +static void tdx_ide_stream_key_stop (struct tdx_tsm_link *tlink) +{ +tdx_ide_stream_km (tlink,TDX_IDE_STREAM_KM_STOP ); +} +static void sel_stream_block_regs (struct pci_dev *pdev,struct pci_ide *ide,struct pci_ide_regs *regs) {
@@ -439,10 +470,25 @@ static int tdx_ide_stream_setup(struct tdx_tsm_link *tlink) ide->partner[PCI_IDE_EP ].default_stream =1 ;pci_ide_stream_setup (pdev, ide); +/* Key Programming for RP & target device, enable IDE stream for RP */ + ret =tdx_ide_stream_key_program (tlink); +if (ret) + goto out_pci_ide_stream_teardown; + +/* Enable IDE stream for target device */ + ret =pci_ide_stream_enable (pdev, ide); +if (ret) + goto out_tdx_ide_stream_key_stop; + tlink->ide = ide;return 0 ; +out_tdx_ide_stream_key_stop: +tdx_ide_stream_key_stop (tlink); +out_pci_ide_stream_teardown: +pci_ide_stream_teardown (pdev, ide); +pci_ide_stream_unregister (ide); out_tdx_ide_stream_delete:tdx_ide_stream_delete (tlink); out_pci_ide_stream_free:
@@ -455,6 +501,8 @@ static void tdx_ide_stream_teardown(struct tdx_tsm_link *tlink) {struct pci_ide *ide = tlink->ide; +pci_ide_stream_disable (ide->pdev, ide); +tdx_ide_stream_key_stop (tlink);pci_ide_stream_teardown (ide->pdev, ide);pci_ide_stream_unregister (ide);tdx_ide_stream_delete (tlink);
Signature · 1 line
2.52.0
RFC PATCH 7/7 x86/virt/tdx: Enable PCIe link encryption
In reply to Chao GaoThe basic SPDM session and IDE functionalities are all implemented, configure the TDX Module to enable the TDISP feature and in turn enable PCIe link encryption.
Based on work by Yilun.
--- arch/x86/include/asm/tdx.h | 2 +- arch/x86/virt/vmx/tdx/tdx.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/tdx.h +1 -1
@@ -147,7 +147,7 @@ static inline bool tdx_supports_tdisp(const struct tdx_sys_info *sysinfo)if (!(sysinfo->features.tdx_features0 &TDX_FEATURES0_TDXCONNECT ))return false; -return false;/* To be enabled when kernel is ready */ +return true; }int tdx_guest_keyid_alloc (void );
arch/x86/virt/vmx/tdx/tdx.c +1 -1
@@ -1043,7 +1043,7 @@ struct tdmr_info_pa_array { };/* List all kernel-supported add-on features0 bits here */ -#define TDX_KERNEL_SUPPORTED_ADDON_FEATURES0 (0) +#define TDX_KERNEL_SUPPORTED_ADDON_FEATURES0 (TDX_FEATURES0_TDXCONNECT)static __init u64 get_tdx_usable_addon_features0 (void ) {
Signature · 1 line
2.52.0
Edgecombe, Rick P
maintainerQuoted 4 lines · Chao Gao
== Points for discussion ==
1. Raw DOE frames (See patch 3)
Quoted 8 lines
The TDX module produces and consumes whole DOE objects, header included, but the PCI DOE API takes payloads only. The TDX-TSM driver therefore strips and rebuilds the DOE header itself. The code is small, but it puts DOE format details in a TSM driver, while the DOE core already handles them. A PCI DOE API that takes whole objects would keep them in the core, but the TDX-TSM driver would be its only user. Is such an API worth adding, or can we live with the DOE format handling in the TDX-TSM driver?
Why can't the TDX module return what we need? I guess AMD's firmware must already return just the payload. Any idea what ARM will do?
Quoted 11 lines
2. Default IDE Stream (See patch 5)
TDISP requires each device to have one default IDE stream [8]. The PCI core does not set Default Stream, so each TSM driver sets it itself, by looking up the device's IDE settings with
pci_ide_to_settings()and settingdefault_stream. AMD's driver already does this, and the TDX-TSM driver does the same. Since the requirement is shared, the PCI core could instead set Default Stream on the device's TC0 stream, which is what TDISP expects. Should this move into the PCI core now, or can it be a later cleanup?
Not very sure on this point. Do you think the TSM drivers would never want to pick a different default stream? If they did is there a point to moving it? Might be better to wait to see how it shakes out.
Quoted 8 lines
== Limitations ==
The TDX module configures the root port's IDE settings and accepts only one address range for the stream. The root port therefore encrypts only traffic to the device's prefetchable MMIO range, and a device with no prefetchable range cannot be connected. We are evaluating whether this can be lifted.
Can you describe how it impacts the initial support here? Is it about enabling more devices, or simplifying the initial code?
Quoted 7 lines
Link encryption needs the root complex to enter a TDX mode, in which only the TDX module can configure the root port's IDE settings. The kernel opts in to this mode with TDH.IOMMU.SETUP. As its name suggests, this SEAMCALL also puts the IOMMU into TDX mode, which changes the IOMMU's behavior. Unfortunately, the two cannot be enabled separately, even though link encryption does not need IOMMU TDX mode.
Hmm, usually TDX is not easy to enable on a system. So users are not likely to accidentally hit the existing limitations. So I'm thinking we just do the same here. Document the limits and not add a switch. But the limits should be cleanly handled and understood. Which is a WIP.
Quoted 12 lines · Rick P
Quoted 10 lines · Chao Gao
1. Raw DOE frames (See patch 3)
The TDX module produces and consumes whole DOE objects, header included, but the PCI DOE API takes payloads only. The TDX-TSM driver therefore strips and rebuilds the DOE header itself. The code is small, but it puts DOE format details in a TSM driver, while the DOE core already handles them. A PCI DOE API that takes whole objects would keep them in the core, but the TDX-TSM driver would be its only user. Is such an API worth adding, or can we live with the DOE format handling in the TDX-TSM driver?
Quoted 2 lines
Why can't the TDX module return what we need? I guess AMD's firmware must already return just the payload. Any idea what ARM will do?
The kernel needs the type and the length of a DOE payload to send it to the device, and each vendor passes them in its own way. AMD's firmware puts a header of its own in front of the payload, which its driver parses and strips before calling pci_doe(). Arm's firmware returns them as two separate fields [1]. The TDX module uses the DOE header.
The TDX module could do the same as Arm's firmware, which is likely the easiest for the kernel. The kernel would get the type and the length and pass them straight to pci_doe(). I am just not sure that the code it saves is worth an ABI change.
Quoted 11 lines
Quoted 10 lines
2. Default IDE Stream (See patch 5)
TDISP requires each device to have one default IDE stream [8]. The PCI core does not set Default Stream, so each TSM driver sets it itself, by looking up the device's IDE settings with
pci_ide_to_settings()and settingdefault_stream. AMD's driver already does this, and the TDX-TSM driver does the same. Since the requirement is shared, the PCI core could instead set Default Stream on the device's TC0 stream, which is what TDISP expects. Should this move into the PCI core now, or can it be a later cleanup?
Quoted 3 lines
Not very sure on this point. Do you think the TSM drivers would never want to pick a different default stream? If they did is there a point to moving it? Might be better to wait to see how it shakes out.
There is not much for a TSM driver to pick. TDISP wants exactly one default stream, and it has to be the stream for TC0 (traffic class 0). The PCIe spec (r7.0, sec 11.3.8, LOCK_INTERFACE_REQUEST) says the device must fail the request if:
- The default Stream does not match the Stream_ID indicated
- Multiple IDE configuration registers in the device have been configured as the default stream
- The default Stream is associated with a TC other than TC0
No TSM driver sets up more than one stream for a device today, so that stream is always the default one.
What bothers me is that the reason is not obvious from the code, and each driver explains it differently. AMD's driver has no comment on it, and the Arm patches [2] and an earlier version of the TDX patches
[3] describe it as a quirk of some test devices.
If the PCI core cannot set the default stream for TSM drivers, a helper in drivers/pci/tsm.c that marks a given stream as the default would also do. The TSM driver still picks the stream, and the explanation lives in one place.
Quoted 8 lines
Quoted 5 lines
The TDX module configures the root port's IDE settings and accepts only one address range for the stream. The root port therefore encrypts only traffic to the device's prefetchable MMIO range, and a device with no prefetchable range cannot be connected. We are evaluating whether this can be lifted.
Can you describe how it impacts the initial support here? Is it about enabling more devices, or simplifying the initial code?
Simplifying the code. The TDX-TSM driver has to take the address ranges that the PCI core prepared and drop all but one before passing them to the TDX module. If the limit is lifted, that trimming and the explanation it needs go away.
Quoted 11 lines
Quoted 6 lines
Link encryption needs the root complex to enter a TDX mode, in which only the TDX module can configure the root port's IDE settings. The kernel opts in to this mode with TDH.IOMMU.SETUP. As its name suggests, this SEAMCALL also puts the IOMMU into TDX mode, which changes the IOMMU's behavior. Unfortunately, the two cannot be enabled separately, even though link encryption does not need IOMMU TDX mode.
Hmm, usually TDX is not easy to enable on a system. So users are not likely to accidentally hit the existing limitations. So I'm thinking we just do the same here. Document the limits and not add a switch. But the limits should be cleanly handled and understood. Which is a WIP.
Agreed.
[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://lore.kernel.org/all/[email protected]/
[3]: https://lore.kernel.org/all/[email protected]/