Skip to contentSkip to conversation

TSM: Secure VFIO, TDISP, SEV TIO

RFC PATCH In review Alexey Kardashevskiy 12 participants 96 messages Feb 18, 2025 – May 29, 2025

Subsystems (16)
Documentation x86 KVM PCI +12 more subsystems
lore
Share & tools
lore

Alexey Kardashevskiy

author maintainer
#0 lore raw

v2 cover TSM: Secure VFIO, TDISP, SEV TIO

Here are some patches to enable SEV-TIO on AMD Turin. It's been a while and got quiet and I kept fixing my tree and wondering if I am going in the right direction.

SEV-TIO allow a guest to establish trust in a device that supports TEE Device Interface Security Protocol (TDISP, defined in PCIe r6.0+) and then interact with the device via private memory.

These include both guest and host support. QEMU also requires changes. This is more to show what it takes on AMD EPYC to pass through TDISP devices, hence "RFC".

Components affected:
KVM
IOMMUFD
CCP (AMD)
SEV-GUEST (AMD)

New components:
PCI IDE
PCI TSM
VIRT CoCo TSM
VIRT CoCo TSM-HOST
VIRT CoCo TSM-GUEST

This is based on a merge of Lukas'es CMA and 1 week old upstream + some of Dan's patches:

https://github.com/aik/linux/tree/tsm
https://github.com/aik/qemu/tree/tsm

Not using "[PATCH 03/11] coco/tsm: Introduce a class device for TEE Security Managers" yet as may be (may be) my approach makes sense too. Tried to stick to the terminology. I have done some changes on top of that, these are on github, not posting here as I expect those to be addressed in that thread:
https://lore.kernel.org/linux-coco/173343739517.1074769.13134786548545925484.stgit@dwillia2-xfh.jf.intel.com/T/

SEV TIO spec:
https://www.amd.com/content/dam/amd/en/documents/epyc-technical-docs/specifications/58271_0_70.pdf
Whitepaper:
https://www.amd.com/content/dam/amd/en/documents/epyc-business-docs/white-papers/sev-tio-whitepaper.pdf

Acronyms:

TEE - Trusted Execution Environments, a concept of managing trust between the host
and devices
TSM - TEE Security Manager (TSM), an entity which ensures security on the host PSP - AMD platform secure processor (also "ASP", "AMD-SP"), acts as TSM on AMD. SEV TIO - the TIO protocol implemented by the PSP and used by the host GHCB - guest/host communication block - a protocol for guest-to-host communication
via a shared page
TDISP - TEE Device Interface Security Protocol (PCIe).

Flow:

- Boot host OS, load CCP and PCI TSM (they will load TSM-HOST too)
- PCI TSM creates sysfs nodes in "coco/tsm: Add tsm and tsm-host modules" for all TDISP-capable devices
- Enable IDE via "echo 0 > /sys/bus/pci/devices/0000:e1:00.0/tsm-dev/tdev:0000:e1:00.0/tsm_dev_connect"
- Examine certificates/measurements/status via sysfs

- run an SNP VM _without_ VFIO PCI device, wait till it is booted
- hotplug a TDISP-capable PCI function, IOMMUFD must be used (not a VFIO container)
- QEMU pins all guest memory via IOMMUFD map-from-fd ioctl()
- the VM detects a TDISP-capable device, creates sysfs nodes in "coco/tsm: Add tsm-guest module"
- the VM loads the device driver which goes as usual till enabling bus master (for convinience)
- TSM-GUEST modules listens for bus master event (hacked in "pci: Add BUS_NOTIFY_PCI_BUS_MASTER event")
- TSM-GUEST requests TDI ("trusted PCI VF") info, traps into QEMU
- QEMU binds the VF to the Coco VM in the secure fw (AMD PSP) via IOMMUFD ioctl
- QEMU reads certificates/measurements/interface report from the hosts sysfs and writes to the guest memory
- the guest receives all the data, examines it (not in this series though)
- the guest enables secure DMA and MMIO by calling GHCB which traps into QEMU
- QEMU calls IOMMUFD ioctl to enable secure DMA and MMIO
- the guest can now stop sharing memory for DMA (and expect DMA to encrypted memory to work) and start accessing validated MMIO with Cbit set.

Assumptions

This requires hotpligging into the VM vs passing the device via the command line as VFIO maps all guest memory as the device init step which is too soon as SNP LAUNCH UPDATE happens later and will fail if VFIO maps private memory before that.

This requires the BME hack as MMIO and BusMaster enable bits cannot be 0 after MMIO validation is done and there are moments in the guest OS booting process when this appens.

SVSM could help addressing these (not implemented).

QEMU advertises TEE-IO capability to the VM. An additional x-tio flag is added to vfio-pci.

Trying to avoid the device driver modification as much as possible at the moment as my test devices already exist in non-TDISP form and need to work without modification. Arguably this may not be always the case.

TODOs

Deal with PCI reset. Hot unplug+plug? Power states too. Actually collaborate with CMA.
Other tons of things.

The previous conversation is here:
https://lore.kernel.org/r/[email protected]

Changes:
v2:
* redid the whole thing pretty much
* RMPUPDATE API for QEMU
* switched to IOMMUFD
* mapping guest memory via IOMMUFD map-from-fd
* marking resouces as validated
* more modules
* moved tons to the userspace (QEMU), such as TDI bind and GHCB guest requests

Sean, get_maintainer.pl produced more than 100 emails for the entire patchset, should I have posted them all anyway?

Please comment. Thanks.

Alexey Kardashevskiy (22):
pci/doe: Define protocol types and make those public PCI/IDE: Fixes to make it work on AMD SNP-SEV PCI/IDE: Init IDs on all IDE streams beforehand iommu/amd: Report SEV-TIO support
crypto: ccp: Enable SEV-TIO feature in the PSP when supported
KVM: X86: Define tsm_get_vmid
coco/tsm: Add tsm and tsm-host modules
pci/tsm: Add PCI driver for TSM
crypto/ccp: Implement SEV TIO firmware interface
KVM: SVM: Add uAPI to change RMP for MMIO
KVM: SEV: Add TIO VMGEXIT
iommufd: Allow mapping from guest_memfd
iommufd: amd-iommu: Add vdevice support
iommufd: Add TIO calls
KVM: X86: Handle private MMIO as shared
coco/tsm: Add tsm-guest module
resource: Mark encrypted MMIO resource on validation coco/sev-guest: Implement the guest support for SEV TIO
RFC: pci: Add BUS_NOTIFY_PCI_BUS_MASTER event
sev-guest: Stop changing encrypted page state for TDISP devices
pci: Allow encrypted MMIO mapping via sysfs
pci: Define pci_iomap_range_encrypted

 drivers/crypto/ccp/Makefile                 |   13 +
 drivers/pci/Makefile                        |    3 +
 drivers/virt/coco/Makefile                  |    2 +
 drivers/virt/coco/guest/Makefile            |    3 +
 drivers/virt/coco/host/Makefile             |    6 +
 drivers/virt/coco/sev-guest/Makefile        |    2 +-
 arch/x86/include/asm/kvm-x86-ops.h          |    1 +
 arch/x86/include/asm/kvm_host.h             |    2 +
 arch/x86/include/asm/sev.h                  |   31 +
 arch/x86/include/uapi/asm/kvm.h             |   11 +
 arch/x86/include/uapi/asm/svm.h             |    2 +
 drivers/crypto/ccp/sev-dev-tio.h            |  111 ++
 drivers/crypto/ccp/sev-dev.h                |   19 +
 drivers/iommu/amd/amd_iommu_types.h         |    3 +
 drivers/iommu/iommufd/iommufd_private.h     |    3 +
 include/asm-generic/pci_iomap.h             |    4 +
 include/linux/amd-iommu.h                   |    2 +
 include/linux/device.h                      |    4 +
 include/linux/device/bus.h                  |    3 +
 include/linux/dma-direct.h                  |    8 +
 include/linux/ioport.h                      |    2 +
 include/linux/kvm_host.h                    |    2 +
 include/linux/pci-doe.h                     |    4 +
 include/linux/pci-ide.h                     |   19 +-
 include/linux/pci.h                         |    2 +-
 include/linux/psp-sev.h                     |   61 +-
 include/linux/swiotlb.h                     |    8 +
 include/linux/tsm.h                         |  315 ++++
 include/uapi/linux/iommufd.h                |   26 +
 include/uapi/linux/kvm.h                    |   24 +
 include/uapi/linux/pci_regs.h               |    5 +-
 include/uapi/linux/psp-sev.h                |    6 +-
 include/uapi/linux/sev-guest.h              |   39 +
 arch/x86/coco/sev/core.c                    |   19 +-
 arch/x86/kvm/mmu/mmu.c                      |    6 +-
 arch/x86/kvm/svm/sev.c                      |  205 +++
 arch/x86/kvm/svm/svm.c                      |   12 +
 arch/x86/mm/ioremap.c                       |    2 +
 arch/x86/mm/mem_encrypt.c                   |    6 +
 arch/x86/virt/svm/sev.c                     |   34 +-
 drivers/crypto/ccp/sev-dev-tio.c            | 1664 ++++++++++++++++++++
 drivers/crypto/ccp/sev-dev-tsm.c            |  709 +++++++++
 drivers/crypto/ccp/sev-dev.c                |   94 +-
 drivers/iommu/amd/init.c                    |    9 +
 drivers/iommu/amd/iommu.c                   |   60 +-
 drivers/iommu/iommufd/main.c                |    6 +
 drivers/iommu/iommufd/pages.c               |   88 +-
 drivers/iommu/iommufd/viommu.c              |  112 ++
 drivers/pci/doe.c                           |    2 -
 drivers/pci/ide.c                           |  103 +-
 drivers/pci/iomap.c                         |   24 +
 drivers/pci/mmap.c                          |   11 +-
 drivers/pci/pci-sysfs.c                     |   27 +-
 drivers/pci/pci.c                           |    3 +
 drivers/pci/proc.c                          |    2 +-
 drivers/pci/tsm.c                           |  233 +++
 drivers/virt/coco/guest/tsm-guest.c         |  326 ++++
 drivers/virt/coco/host/tsm-host.c           |  551 +++++++
 drivers/virt/coco/sev-guest/sev_guest.c     |   10 +
 drivers/virt/coco/sev-guest/sev_guest_tio.c |  738 +++++++++
 drivers/virt/coco/tsm.c                     |  638 ++++++++
 kernel/resource.c                           |   48 +
 virt/kvm/kvm_main.c                         |    6 +
 Documentation/virt/coco/tsm.rst             |  132 ++
 drivers/crypto/ccp/Kconfig                  |    2 +
 drivers/pci/Kconfig                         |   15 +
 drivers/virt/coco/Kconfig                   |   14 +
 drivers/virt/coco/guest/Kconfig             |    3 +
 drivers/virt/coco/host/Kconfig              |    6 +
 drivers/virt/coco/sev-guest/Kconfig         |    1 +
 70 files changed, 6614 insertions(+), 53 deletions(-)
 create mode 100644 drivers/virt/coco/host/Makefile
 create mode 100644 drivers/crypto/ccp/sev-dev-tio.h
 create mode 100644 drivers/crypto/ccp/sev-dev-tio.c
 create mode 100644 drivers/crypto/ccp/sev-dev-tsm.c
 create mode 100644 drivers/pci/tsm.c
 create mode 100644 drivers/virt/coco/guest/tsm-guest.c
 create mode 100644 drivers/virt/coco/host/tsm-host.c
 create mode 100644 drivers/virt/coco/sev-guest/sev_guest_tio.c
 create mode 100644 drivers/virt/coco/tsm.c
 create mode 100644 Documentation/virt/coco/tsm.rst
 create mode 100644 drivers/virt/coco/host/Kconfig
Signature · 1 line
2.47.1
author maintainer
#1 lore raw

RFC PATCH v2 1/22 pci/doe: Define protocol types and make those public

Already public pci_doe() takes a protocol type argument. PCIe 6.0 defines three, define them in a header for use with pci_doe().

---
 include/linux/pci-doe.h | 4 ++++
 drivers/pci/doe.c       | 2 --
 2 files changed, 4 insertions(+), 2 deletions(-)
include/linux/pci-doe.h +4 -0
index 0d3d7656c456..82e393ba5465 100644
@@ -13,6 +13,10 @@
 #ifndef LINUX_PCI_DOE_H
 #define LINUX_PCI_DOE_H
 
+#define PCI_DOE_PROTOCOL_DISCOVERY		0
+#define PCI_DOE_PROTOCOL_CMA_SPDM		1
+#define PCI_DOE_PROTOCOL_SECURED_CMA_SPDM	2
+
 struct pci_doe_mb;
 
 /* Max data object length is 2^18 dwords (including 2 dwords for header) */
drivers/pci/doe.c +0 -2
index e4b609f613da..98fd86fae8d8 100644
@@ -22,8 +22,6 @@
 
 #include "pci.h"
 
-#define PCI_DOE_PROTOCOL_DISCOVERY 0
-
 /* Timeout of 1 second from 6.30.2 Operation, PCI Spec r6.0 */
 #define PCI_DOE_TIMEOUT HZ
 #define PCI_DOE_POLL_INTERVAL	(PCI_DOE_TIMEOUT / 128)
Signature · 1 line
2.47.1
author maintainer
#3 lore raw

RFC PATCH v2 2/22 PCI/IDE: Fixes to make it work on AMD SNP-SEV

In reply to Alexey Kardashevskiy

The IDE proposed patches do setup of endpoints while they should focus on root port.

These are workarounds better be discussed in
"[PATCH 00/11] PCI/TSM: Core infrastructure for PCI device security (TDISP)"

---
 include/linux/pci-ide.h       | 19 +++--
 include/uapi/linux/pci_regs.h |  4 +-
 drivers/pci/ide.c             | 76 ++++++++++++++++----
 3 files changed, 78 insertions(+), 21 deletions(-)
include/linux/pci-ide.h +13 -6
index 24e08a413645..f784fb16cc88 100644
@@ -8,26 +8,33 @@
 
 #include <linux/range.h>
 
+enum pci_ide_flags {
+	PCI_IDE_SETUP_ROOT_PORT = BIT(0),
+	PCI_IDE_SETUP_ROOT_PORT_MEM = BIT(1),
+};
+
 struct pci_ide {
 	int domain;
 	u16 devid_start;
 	u16 devid_end;
+	u16 rpid_start;
+	u16 rpid_end;
 	int stream_id;
 	const char *name;
 	int nr_mem;
 	struct range mem[16];
+	unsigned int dev_sel_ctl;
+	unsigned int rootport_sel_ctl;
+	enum pci_ide_flags flags;
 };
 
 void pci_ide_stream_probe(struct pci_dev *pdev, struct pci_ide *ide);
 
-enum pci_ide_flags {
-	PCI_IDE_SETUP_ROOT_PORT = BIT(0),
-};
-
 int pci_ide_stream_setup(struct pci_dev *pdev, struct pci_ide *ide,
 			 enum pci_ide_flags flags);
-void pci_ide_stream_teardown(struct pci_dev *pdev, struct pci_ide *ide,
-			     enum pci_ide_flags flags);
+void pci_ide_stream_teardown(struct pci_dev *pdev, struct pci_ide *ide);
 void pci_ide_enable_stream(struct pci_dev *pdev, struct pci_ide *ide);
 void pci_ide_disable_stream(struct pci_dev *pdev, struct pci_ide *ide);
+int pci_ide_stream_state(struct pci_dev *pdev, struct pci_ide *ide, u32 *status, u32 *rpstatus);
+
 #endif /* __PCI_IDE_H__ */
include/uapi/linux/pci_regs.h +2 -2
index 498c6b298186..15bd8e2b3cf5 100644
@@ -1293,9 +1293,9 @@
 /* Selective IDE Address Association Register Block, up to PCI_IDE_SEL_CAP_ASSOC_NUM */
 #define  PCI_IDE_SEL_ADDR_1(x)			(20 + (x) * 12)
 #define   PCI_IDE_SEL_ADDR_1_VALID		0x1
-#define   PCI_IDE_SEL_ADDR_1_BASE_LOW_MASK	0x000fff0
+#define   PCI_IDE_SEL_ADDR_1_BASE_LOW_MASK	0x000fff00
 #define   PCI_IDE_SEL_ADDR_1_BASE_LOW_SHIFT	20
-#define   PCI_IDE_SEL_ADDR_1_LIMIT_LOW_MASK 	0xfff0000
+#define   PCI_IDE_SEL_ADDR_1_LIMIT_LOW_MASK	0xfff00000
 #define   PCI_IDE_SEL_ADDR_1_LIMIT_LOW_SHIFT	20
 /* IDE Address Association Register 2 is "Memory Limit Upper" */
 /* IDE Address Association Register 3 is "Memory Base Upper" */
drivers/pci/ide.c +63 -13

167 lines not on this page · raw message

Signature · 1 line
2.47.1
author maintainer
#4 lore raw

RFC PATCH v2 3/22 PCI/IDE: Init IDs on all IDE streams beforehand

In reply to Alexey Kardashevskiy

The PCIe spec defines two types of streams - selective and link. Each stream has an ID from the same bucket so a stream ID does not tell the type.
The spec defines an "enable" bit for every stream and required stream IDs to be unique among all enabled stream but there is no such requirement for disabled streams.

However, when IDE_KM is programming keys, an IDE-capable device needs to know the type of stream being programmed to write it directly to the hardware as keys are relatively large, possibly many of them and devices often struggle with keeping around rather big data not being used.

Walk through all streams on a device and initialize the IDs to some unique number, both link and selective.

Probably should be a quirk if it turns out not to be a common issue.

---
 drivers/pci/ide.c | 29 ++++++++++++++++++--
 1 file changed, 26 insertions(+), 3 deletions(-)
drivers/pci/ide.c +26 -3
index 3c53b27f8447..5f1d5385d3a8 100644
@@ -18,7 +18,7 @@ static int sel_ide_offset(u16 cap, int stream_id, int nr_ide_mem)
 void pci_ide_init(struct pci_dev *pdev)
 {
 	u16 ide_cap, sel_ide_cap;
-	int nr_ide_mem = 0;
+	int nr_ide_mem = 0, i, link_num, sel_num, offset;
 	u32 val = 0;
 
 	if (!pci_is_pcie(pdev))
@@ -33,6 +33,7 @@ void pci_ide_init(struct pci_dev *pdev)
 	 * require consistent number of address association blocks
 	 */
 	pci_read_config_dword(pdev, ide_cap + PCI_IDE_CAP, &val);
+
 	if ((val & PCI_IDE_CAP_SELECTIVE) == 0)
 		return;
 
@@ -43,6 +44,9 @@ void pci_ide_init(struct pci_dev *pdev)
 			return;
 	}
 
+	link_num = PCI_IDE_CAP_LINK_TC_NUM(val) + 1;
+	sel_num = PCI_IDE_CAP_SELECTIVE_STREAMS_NUM(val) + 1;
+
 	if (val & PCI_IDE_CAP_LINK)
 		sel_ide_cap = ide_cap + PCI_IDE_LINK_STREAM +
 			      (PCI_IDE_CAP_LINK_TC_NUM(val) + 1) *
@@ -50,12 +54,13 @@ void pci_ide_init(struct pci_dev *pdev)
 	else
 		sel_ide_cap = ide_cap + PCI_IDE_LINK_STREAM;
 
-	for (int i = 0; i < PCI_IDE_CAP_SELECTIVE_STREAMS_NUM(val) + 1; i++) {
+	for (i = 0; i < PCI_IDE_CAP_SELECTIVE_STREAMS_NUM(val) + 1; i++) {
 		if (i == 0) {
+			offset = 0;
 			pci_read_config_dword(pdev, sel_ide_cap, &val);
 			nr_ide_mem = PCI_IDE_SEL_CAP_ASSOC_NUM(val) + 1;
 		} else {
-			int offset = sel_ide_offset(sel_ide_cap, i, nr_ide_mem);
+			offset = sel_ide_offset(sel_ide_cap, i, nr_ide_mem);
 
 			pci_read_config_dword(pdev, offset, &val);
 
@@ -68,6 +73,24 @@ void pci_ide_init(struct pci_dev *pdev)
 				return;
 			}
 		}
+
+		/* Some devices insist on streamid to be unique even for not enabled streams */
+		val &= ~PCI_IDE_SEL_CTL_ID_MASK;
+		val |= FIELD_PREP(PCI_IDE_SEL_CTL_ID_MASK, i);
+		pci_write_config_dword(pdev, offset + PCI_IDE_SEL_CTL, val);
+	}
+
+	if (val & PCI_IDE_CAP_LINK) {
+		/* Some devices insist on streamid to be unique even for not enabled streams */
+		for (i = 0; i < link_num; ++i) {
+			offset = ide_cap + PCI_IDE_LINK_STREAM + i * PCI_IDE_LINK_BLOCK_SIZE;
+
+			pci_read_config_dword(pdev, offset, &val);
+			val &= ~PCI_IDE_LINK_CTL_ID_MASK;
+			val |= FIELD_PREP(PCI_IDE_LINK_CTL_ID_MASK, i + sel_num);
+
+			pci_write_config_dword(pdev, offset, val);
+		}
 	}
 
 	pdev->ide_cap = ide_cap;
Signature · 1 line
2.47.1
author maintainer
#5 lore raw

RFC PATCH v2 4/22 iommu/amd: Report SEV-TIO support

In reply to Alexey Kardashevskiy

The AMD BIOS'es "SEV-TIO" switch is reported to the OS via IOMMU Extended Feature 2 register (EFR2), bit 1.

Add helper to parse the bit and report the feature presense.

---
 drivers/iommu/amd/amd_iommu_types.h | 1 +
 include/linux/amd-iommu.h           | 2 ++
 drivers/iommu/amd/init.c            | 9 +++++++++
 3 files changed, 12 insertions(+)
drivers/iommu/amd/amd_iommu_types.h +1 -0
index 0bbda60d3cdc..b086fb632990 100644
@@ -108,6 +108,7 @@
 
 
 /* Extended Feature 2 Bits */
+#define FEATURE_SEVSNPIO_SUP	BIT_ULL(1)
 #define FEATURE_SNPAVICSUP	GENMASK_ULL(7, 5)
 #define FEATURE_SNPAVICSUP_GAM(x) \
 	(FIELD_GET(FEATURE_SNPAVICSUP, x) == 0x1)
include/linux/amd-iommu.h +2 -0
index 062fbd4c9b77..cb1b94fdb63c 100644
@@ -32,10 +32,12 @@ struct task_struct;
 struct pci_dev;
 
 extern void amd_iommu_detect(void);
+extern bool amd_iommu_sev_tio_supported(void);
 
 #else /* CONFIG_AMD_IOMMU */
 
 static inline void amd_iommu_detect(void) { }
+static inline bool amd_iommu_sev_tio_supported(void) { return false; }
 
 #endif /* CONFIG_AMD_IOMMU */
 
drivers/iommu/amd/init.c +9 -0
index c5cd92edada0..9f2756d3bd73 100644
@@ -2156,6 +2156,9 @@ static void print_iommu_info(void)
 		if (check_feature(FEATURE_SNP))
 			pr_cont(" SNP");
 
+		if (check_feature2(FEATURE_SEVSNPIO_SUP))
+			pr_cont(" SEV-TIO");
+
 		pr_cont("\n");
 	}
 
@@ -3856,4 +3859,10 @@ int amd_iommu_snp_disable(void)
 	return 0;
 }
 EXPORT_SYMBOL_GPL(amd_iommu_snp_disable);
+
+bool amd_iommu_sev_tio_supported(void)
+{
+	return check_feature2(FEATURE_SEVSNPIO_SUP);
+}
+EXPORT_SYMBOL_GPL(amd_iommu_sev_tio_supported);
 #endif
Signature · 1 line
2.47.1
author maintainer
#6 lore raw

RFC PATCH v2 5/22 crypto: ccp: Enable SEV-TIO feature in the PSP when supported

In reply to Alexey Kardashevskiy

The PSP advertises the SEV-TIO support via the FEATURE_INFO command support of which is advertised via SNP_PLATFORM_STATUS.

Add FEATURE_INFO and use it to detect the TIO support in the PSP. If present, enable TIO in the SNP_INIT_EX call.

While at this, add new bits to sev_data_snp_init_ex() from SEV-SNP 1.55.

Note that this tests the PSP firmware support but not if the feature is enabled in the BIOS.

While at this, add new sev_data_snp_shutdown_ex::x86_snp_shutdown

---
 drivers/crypto/ccp/sev-dev.h |  1 +
 include/linux/psp-sev.h      | 32 +++++++-
 include/uapi/linux/psp-sev.h |  4 +-
 drivers/crypto/ccp/sev-dev.c | 84 +++++++++++++++++++-
 4 files changed, 115 insertions(+), 6 deletions(-)
drivers/crypto/ccp/sev-dev.h +1 -0
index d382a265350b..c87a312f7da6 100644
@@ -71,6 +71,7 @@ struct sev_device {
 	struct fw_upload *fwl;
 	bool fw_cancel;
 #endif /* CONFIG_FW_UPLOAD */
+	bool tio_en;
 };
 
 bool sev_version_greater_or_equal(u8 maj, u8 min);
include/linux/psp-sev.h +29 -3
index 788505d46d25..103d9c161f41 100644
@@ -107,6 +107,7 @@ enum sev_cmd {
 	SEV_CMD_SNP_DOWNLOAD_FIRMWARE_EX = 0x0CA,
 	SEV_CMD_SNP_COMMIT		= 0x0CB,
 	SEV_CMD_SNP_VLEK_LOAD		= 0x0CD,
+	SEV_CMD_SNP_FEATURE_INFO	= 0x0CE,
 
 	SEV_CMD_MAX,
 };
@@ -146,6 +147,7 @@ struct sev_data_init_ex {
 } __packed;
 
 #define SEV_INIT_FLAGS_SEV_ES	0x01
+#define SEV_INIT_FLAGS_SEV_TIO_EN	BIT(2)
 
 /**
  * struct sev_data_pek_csr - PEK_CSR command parameters
@@ -601,6 +603,25 @@ struct sev_data_snp_addr {
 	u64 address;				/* In/Out */
 } __packed;
 
+/**
+ * struct sev_data_snp_feature_info - SEV_CMD_SNP_FEATURE_INFO command params
+ *
+ * @len: length of this struct
+ * @ecx_in: subfunction index of CPUID Fn8000_0024
+ * @feature_info_paddr: physical address of a page with sev_snp_feature_info
+ */
+#define SNP_FEATURE_FN8000_0024_EBX_X00_SEVTIO	1
+
+struct sev_snp_feature_info {
+	u32 eax, ebx, ecx, edx;			/* Out */
+} __packed;
+
+struct sev_data_snp_feature_info {
+	u32 length;				/* In */
+	u32 ecx_in;				/* In */
+	u64 feature_info_paddr;			/* In */
+} __packed;
+
 /**
  * struct sev_data_snp_launch_start - SNP_LAUNCH_START command params
  *
@@ -762,10 +783,14 @@ struct sev_data_snp_guest_request {
 struct sev_data_snp_init_ex {
 	u32 init_rmp:1;
 	u32 list_paddr_en:1;
-	u32 rsvd:30;
+	u32 rapl_dis:1;
+	u32 ciphertext_hiding_en:1;
+	u32 tio_en:1;
+	u32 rsvd:27;
 	u32 rsvd1;
 	u64 list_paddr;
-	u8  rsvd2[48];
+	u16 max_snp_asid;
+	u8  rsvd2[46];
 } __packed;
 
 /**
@@ -804,7 +829,8 @@ struct sev_data_range_list {
 struct sev_data_snp_shutdown_ex {
 	u32 len;
 	u32 iommu_snp_shutdown:1;
-	u32 rsvd1:31;
+	u32 x86_snp_shutdown:1;
+	u32 rsvd1:30;
 } __packed;
 
 /**
include/uapi/linux/psp-sev.h +3 -1
index b508b355a72e..affa65dcebd4 100644
@@ -189,6 +189,7 @@ struct sev_user_data_get_id2 {
  * @mask_chip_id: whether chip id is present in attestation reports or not
  * @mask_chip_key: whether attestation reports are signed or not
  * @vlek_en: VLEK (Version Loaded Endorsement Key) hashstick is loaded
+ * @feature_info: Indicates that the SNP_FEATURE_INFO command is available
  * @rsvd1: reserved
  * @guest_count: the number of guest currently managed by the firmware
  * @current_tcb_version: current TCB version
@@ -204,7 +205,8 @@ struct sev_user_data_snp_status {
 	__u32 mask_chip_id:1;		/* Out */
 	__u32 mask_chip_key:1;		/* Out */
 	__u32 vlek_en:1;		/* Out */
-	__u32 rsvd1:29;
+	__u32 feature_info:1;		/* Out */
+	__u32 rsvd1:28;
 	__u32 guest_count;		/* Out */
 	__u64 current_tcb_version;	/* Out */
 	__u64 reported_tcb_version;	/* Out */
drivers/crypto/ccp/sev-dev.c +82 -2

120 lines not on this page · raw message

Signature · 1 line
2.47.1
author maintainer
#9 lore raw

RFC PATCH v2 6/22 KVM: X86: Define tsm_get_vmid

In reply to Alexey Kardashevskiy

In order to add a PCI VF into a secure VM, the TSM module needs to perform a "TDI bind" operation. The secure module ("PSP" for AMD) reuqires a VM id to associate with a VM and KVM has it. Since KVM cannot directly bind a TDI (as it does not have all necesessary data such as host/guest PCI BDFn). QEMU and IOMMUFD do know the BDFns but they do not have a VM id recognisable by the PSP.

Add get_vmid() hook to KVM. Implement it for AMD SEV to return a sum of GCTX (a private page describing secure VM context) and ASID (required on unbind for IOMMU unfencing, when needed).

---
 arch/x86/include/asm/kvm-x86-ops.h |  1 +
 arch/x86/include/asm/kvm_host.h    |  2 ++
 include/linux/kvm_host.h           |  2 ++
 arch/x86/kvm/svm/svm.c             | 12 ++++++++++++
 virt/kvm/kvm_main.c                |  6 ++++++
 5 files changed, 23 insertions(+)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
index c35550581da0..63102a224cd7 100644
@@ -144,6 +144,7 @@ KVM_X86_OP_OPTIONAL(alloc_apic_backing_page)
 KVM_X86_OP_OPTIONAL_RET0(gmem_prepare)
 KVM_X86_OP_OPTIONAL_RET0(private_max_mapping_level)
 KVM_X86_OP_OPTIONAL(gmem_invalidate)
+KVM_X86_OP_OPTIONAL(tsm_get_vmid)
 
 #undef KVM_X86_OP
 #undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +2 -0
index b15cde0a9b5c..9330e8d4d29d 100644
@@ -1875,6 +1875,8 @@ struct kvm_x86_ops {
 	int (*gmem_prepare)(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order);
 	void (*gmem_invalidate)(kvm_pfn_t start, kvm_pfn_t end);
 	int (*private_max_mapping_level)(struct kvm *kvm, kvm_pfn_t pfn);
+
+	u64 (*tsm_get_vmid)(struct kvm *kvm);
 };
 
 struct kvm_x86_nested_ops {
include/linux/kvm_host.h +2 -0
index f34f4cfaa513..6cd351edb956 100644
@@ -2571,4 +2571,6 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,
 				    struct kvm_pre_fault_memory *range);
 #endif
 
+u64 kvm_arch_tsm_get_vmid(struct kvm *kvm);
+
 #endif
arch/x86/kvm/svm/svm.c +12 -0
index 7640a84e554a..0276d60c61d6 100644
@@ -4998,6 +4998,16 @@ static void *svm_alloc_apic_backing_page(struct kvm_vcpu *vcpu)
 	return page_address(page);
 }
 
+static u64 svm_tsm_get_vmid(struct kvm *kvm)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+
+	if (!sev->es_active)
+		return 0;
+
+	return ((u64) sev->snp_context) | sev->asid;
+}
+
 static struct kvm_x86_ops svm_x86_ops __initdata = {
 	.name = KBUILD_MODNAME,
 
@@ -5137,6 +5147,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = {
 	.gmem_prepare = sev_gmem_prepare,
 	.gmem_invalidate = sev_gmem_invalidate,
 	.private_max_mapping_level = sev_private_max_mapping_level,
+
+	.tsm_get_vmid = svm_tsm_get_vmid,
 };
 
 /*
virt/kvm/kvm_main.c +6 -0
index ba0327e2d0d3..90c3ff7c5c02 100644
@@ -6464,3 +6464,9 @@ void kvm_exit(void)
 	kvm_irqfd_exit();
 }
 EXPORT_SYMBOL_GPL(kvm_exit);
+
+u64 kvm_arch_tsm_get_vmid(struct kvm *kvm)
+{
+	return static_call(kvm_x86_tsm_get_vmid)(kvm);
+}
+EXPORT_SYMBOL_GPL(kvm_arch_tsm_get_vmid);
Signature · 1 line
2.47.1
author maintainer
#15 lore raw

RFC PATCH v2 7/22 coco/tsm: Add tsm and tsm-host modules

In reply to Alexey Kardashevskiy

The TSM module is a library to create sysfs nodes common for hypervisors and VMs. It also provides helpers to parse interface reports (required by VMs, visible to HVs). It registers 3 device classes:
- tsm: one per platform,
- tsm-dev: for physical functions, ("TDEV");
- tdm-tdi: for PCI functions being assigned to VMs ("TDI").

The library adds a child device of "tsm-dev" or/and "tsm-tdi" class for every capable PCI device. Note that the module is made bus-agnostic.

New device nodes provide sysfs interface for fetching device certificates and measurements and TDI interface reports.
Nodes with the "_user" suffix provide human-readable information, without that suffix it is raw binary data to be copied to a guest.

The TSM-HOST module adds hypervisor-only functionality on top. At the moment it is:
- "connect" to enable/disable IDE (a PCI link encryption);
- "TDI bind" to manage a PCI function passed through to a secure VM.

A platform is expected to register itself in TSM-HOST and provide necessary callbacks. No platform is added here, AMD SEV is coming in the next patches.

---
 drivers/virt/coco/Makefile        |   2 +
 drivers/virt/coco/host/Makefile   |   6 +
 include/linux/tsm.h               | 295 +++++++++
 drivers/virt/coco/host/tsm-host.c | 552 +++++++++++++++++
 drivers/virt/coco/tsm.c           | 636 ++++++++++++++++++++
 Documentation/virt/coco/tsm.rst   |  99 +++
 drivers/virt/coco/Kconfig         |  14 +
 drivers/virt/coco/host/Kconfig    |   6 +
 8 files changed, 1610 insertions(+)
drivers/virt/coco/Makefile +2 -0
index 885c9ef4e9fc..670f77c564e8 100644
@@ -2,9 +2,11 @@
 #
 # Confidential computing related collateral
 #
+obj-$(CONFIG_TSM)		+= tsm.o
 obj-$(CONFIG_EFI_SECRET)	+= efi_secret/
 obj-$(CONFIG_ARM_PKVM_GUEST)	+= pkvm-guest/
 obj-$(CONFIG_SEV_GUEST)		+= sev-guest/
 obj-$(CONFIG_INTEL_TDX_GUEST)	+= tdx-guest/
 obj-$(CONFIG_ARM_CCA_GUEST)	+= arm-cca-guest/
 obj-$(CONFIG_TSM_REPORTS)	+= guest/
+obj-$(CONFIG_TSM_HOST)          += host/
drivers/virt/coco/host/Makefile new file +6 -0
new file mode 100644
index 000000000000..c5e216b6cb1c
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# TSM (TEE Security Manager) Common infrastructure and host drivers
+
+obj-$(CONFIG_TSM_HOST) += tsm_host.o
+tsm_host-y += tsm-host.o
include/linux/tsm.h +295 -0

305 lines not on this page · raw message

drivers/virt/coco/host/tsm-host.c new file +552 -0

552 lines not on this page · raw message

drivers/virt/coco/tsm.c new file +636 -0

636 lines not on this page · raw message

Documentation/virt/coco/tsm.rst new file +99 -0

99 lines not on this page · raw message

drivers/virt/coco/Kconfig +14 -0
index 819a97e8ba99..e4385247440b 100644
@@ -3,6 +3,18 @@
 # Confidential computing related collateral
 #
 
+config TSM
+	tristate "Platform support for TEE Device Interface Security Protocol (TDISP)"
+	default m
+	depends on AMD_MEM_ENCRYPT
+	select PCI_DOE
+	select PCI_IDE
+	help
+	  Add a common place for user visible platform support for PCIe TDISP.
+	  TEE Device Interface Security Protocol (TDISP) from PCI-SIG,
+	  https://pcisig.com/tee-device-interface-security-protocol-tdisp
+	  This is prerequisite for host and guest support.
+
 source "drivers/virt/coco/efi_secret/Kconfig"
 
 source "drivers/virt/coco/pkvm-guest/Kconfig"
@@ -14,3 +26,5 @@ source "drivers/virt/coco/tdx-guest/Kconfig"
 source "drivers/virt/coco/arm-cca-guest/Kconfig"
 
 source "drivers/virt/coco/guest/Kconfig"
+
+source "drivers/virt/coco/host/Kconfig"
drivers/virt/coco/host/Kconfig new file +6 -0
new file mode 100644
index 000000000000..3bde38b91fd4
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# TSM (TEE Security Manager) Common infrastructure and host drivers
+#
+config TSM_HOST
+	tristate
Signature · 1 line
2.47.1
author maintainer
#19 lore raw

RFC PATCH v2 8/22 pci/tsm: Add PCI driver for TSM

In reply to Alexey Kardashevskiy

The PCI TSM module scans the PCI bus to initialize a TSM context for physical ("TDEV") and virtual ("TDI") functions. It also implements bus operations which at the moment is just an SPDM bouncer which talks to the PF's DOE mailboxes.

The purpose of this module is to keep drivers/virt/coco/(guest|host) unaware of PCI as much as possible (which is not always the case).

This module does not add new sysfs interfaces.

---
 drivers/pci/Makefile          |   3 +
 include/uapi/linux/pci_regs.h |   1 +
 drivers/pci/tsm.c             | 233 ++++++++++++++++++++
 drivers/pci/Kconfig           |  15 ++
 4 files changed, 252 insertions(+)
drivers/pci/Makefile +3 -0
index 8050b04cc350..59d774bf9c32 100644
@@ -43,6 +43,9 @@ obj-$(CONFIG_PCI_CMA)		+= cma.o cma.asn1.o
 $(obj)/cma.o:			$(obj)/cma.asn1.h
 $(obj)/cma.asn1.o:		$(obj)/cma.asn1.c $(obj)/cma.asn1.h
 
+tsm_pci-y			:= tsm.o
+obj-$(CONFIG_PCI_TSM)		+= tsm_pci.o
+
 # Endpoint library must be initialized before its users
 obj-$(CONFIG_PCI_ENDPOINT)	+= endpoint/
 
include/uapi/linux/pci_regs.h +1 -0
index 15bd8e2b3cf5..9c4a1995da8c 100644
@@ -499,6 +499,7 @@
 #define  PCI_EXP_DEVCAP_PWR_VAL	0x03fc0000 /* Slot Power Limit Value */
 #define  PCI_EXP_DEVCAP_PWR_SCL	0x0c000000 /* Slot Power Limit Scale */
 #define  PCI_EXP_DEVCAP_FLR     0x10000000 /* Function Level Reset */
+#define  PCI_EXP_DEVCAP_TEE     0x40000000 /* TEE I/O (TDISP) Support */
 #define PCI_EXP_DEVCTL		0x08	/* Device Control */
 #define  PCI_EXP_DEVCTL_CERE	0x0001	/* Correctable Error Reporting En. */
 #define  PCI_EXP_DEVCTL_NFERE	0x0002	/* Non-Fatal Error Reporting Enable */
drivers/pci/tsm.c new file +233 -0

233 lines not on this page · raw message

drivers/pci/Kconfig +15 -0
index 9212c0decdc5..9285e7511860 100644
@@ -137,6 +137,21 @@ config PCI_CMA
 config PCI_IDE
 	bool
 
+config PCI_TSM
+	tristate "TEE Security Manager for PCI Device Security"
+	select PCI_IDE
+	depends on TSM
+	default m
+	help
+	  The TEE (Trusted Execution Environment) Device Interface
+	  Security Protocol (TDISP) defines a "TSM" as a platform agent
+	  that manages device authentication, link encryption, link
+	  integrity protection, and assignment of PCI device functions
+	  (virtual or physical) to confidential computing VMs that can
+	  access (DMA) guest private memory.
+
+	  Enable a platform TSM driver to use this capability.
+
 config PCI_DOE
 	bool
 
Signature · 1 line
2.47.1
author maintainer
#21 lore raw

RFC PATCH v2 9/22 crypto/ccp: Implement SEV TIO firmware interface

In reply to Alexey Kardashevskiy

On AND SEV, the AMD PSP firmware acts as TSM (manages the security/trust). The CCP driver provides the interface to it and registers itself in the TSM-HOST subsystem.

Implement SEV TIO PSP command wrappers in sev-dev-tio.c, these make SPDM calls and store the data in the SEV-TIO-specific structs.

Implement TSM-HOST hooks in sev-dev-tsm.c.

---
 drivers/crypto/ccp/Makefile       |   13 +
 arch/x86/include/asm/sev.h        |   20 +
 drivers/crypto/ccp/sev-dev-tio.h  |  111 ++
 drivers/crypto/ccp/sev-dev.h      |   18 +
 include/linux/psp-sev.h           |   27 +
 include/uapi/linux/psp-sev.h      |    2 +
 drivers/crypto/ccp/sev-dev-tio.c  | 1664 ++++++++++++++++++++
 drivers/crypto/ccp/sev-dev-tsm.c  |  709 +++++++++
 drivers/crypto/ccp/sev-dev.c      |   10 +-
 drivers/virt/coco/host/tsm-host.c |    4 +-
 drivers/crypto/ccp/Kconfig        |    2 +
 11 files changed, 2576 insertions(+), 4 deletions(-)
drivers/crypto/ccp/Makefile +13 -0
index 5ce69134ec48..8868896f3fd5 100644
@@ -14,6 +14,19 @@ ccp-$(CONFIG_CRYPTO_DEV_SP_PSP) += psp-dev.o \
                                    platform-access.o \
                                    dbc.o \
                                    hsti.o
+
+ifeq ($(CONFIG_CRYPTO_DEV_SP_PSP)$(CONFIG_PCI_TSM),yy)
+ccp-y += sev-dev-tsm.o sev-dev-tio.o
+endif
+
+ifeq ($(CONFIG_CRYPTO_DEV_SP_PSP)$(CONFIG_PCI_TSM),ym)
+ccp-m += sev-dev-tsm.o sev-dev-tio.o
+endif
+
+ifeq ($(CONFIG_CRYPTO_DEV_SP_PSP)$(CONFIG_PCI_TSM),mm)
+ccp-m += sev-dev-tsm.o sev-dev-tio.o
+endif
+
 ccp-$(CONFIG_CRYPTO_DEV_SP_PSP_FW_UPLOAD) += sev-fw.o
 
 obj-$(CONFIG_CRYPTO_DEV_CCP_CRYPTO) += ccp-crypto.o
arch/x86/include/asm/sev.h +20 -0
index 46a7e5d45275..c5e9455df0dc 100644
@@ -146,6 +146,14 @@ enum msg_type {
 	SNP_MSG_ABSORB_RSP,
 	SNP_MSG_VMRK_REQ,
 	SNP_MSG_VMRK_RSP,
+	TIO_MSG_TDI_INFO_REQ        = 0x81,
+	TIO_MSG_TDI_INFO_RSP        = 0x01,
+	TIO_MSG_MMIO_VALIDATE_REQ   = 0x82,
+	TIO_MSG_MMIO_VALIDATE_RSP   = 0x02,
+	TIO_MSG_MMIO_CONFIG_REQ     = 0x83,
+	TIO_MSG_MMIO_CONFIG_RSP     = 0x03,
+	TIO_MSG_SDTE_WRITE_REQ      = 0x84,
+	TIO_MSG_SDTE_WRITE_RSP      = 0x04,
 
 	SNP_MSG_TSC_INFO_REQ = 17,
 	SNP_MSG_TSC_INFO_RSP,
@@ -209,6 +217,18 @@ struct snp_guest_req {
 	void *data;
 };
 
+/* SPDM algorithms used for TDISP, used in TIO_MSG_TDI_INFO_REQ */
+#define TIO_SPDM_ALGOS_DHE_SECP256R1			0
+#define TIO_SPDM_ALGOS_DHE_SECP384R1			1
+#define TIO_SPDM_ALGOS_AEAD_AES_128_GCM			(0<<8)
+#define TIO_SPDM_ALGOS_AEAD_AES_256_GCM			(1<<8)
+#define TIO_SPDM_ALGOS_ASYM_TPM_ALG_RSASSA_3072		(0<<16)
+#define TIO_SPDM_ALGOS_ASYM_TPM_ALG_ECDSA_ECC_NIST_P256	(1<<16)
+#define TIO_SPDM_ALGOS_ASYM_TPM_ALG_ECDSA_ECC_NIST_P384	(2<<16)
+#define TIO_SPDM_ALGOS_HASH_TPM_ALG_SHA_256		(0<<24)
+#define TIO_SPDM_ALGOS_HASH_TPM_ALG_SHA_384		(1<<24)
+#define TIO_SPDM_ALGOS_KEY_SCHED_SPDM_KEY_SCHEDULE	(0ULL<<32)
+
 /*
  * The secrets page contains 96-bytes of reserved field that can be used by
  * the guest OS. The guest OS uses the area to save the message sequence
drivers/crypto/ccp/sev-dev-tio.h new file +111 -0

111 lines not on this page · raw message

drivers/crypto/ccp/sev-dev.h +18 -0
index c87a312f7da6..342fcd42fa7c 100644
@@ -43,6 +43,8 @@ struct sev_misc_dev {
 	struct miscdevice misc;
 };
 
+struct sev_tio_status;
+
 struct sev_device {
 	struct device *dev;
 	struct psp_device *psp;
@@ -71,7 +73,13 @@ struct sev_device {
 	struct fw_upload *fwl;
 	bool fw_cancel;
 #endif /* CONFIG_FW_UPLOAD */
+
 	bool tio_en;
+#if defined(CONFIG_PCI_TSM) || defined(CONFIG_PCI_TSM_MODULE)
+	struct tsm_host_subsys *tsm;
+	struct tsm_bus_subsys *tsm_bus;
+	struct sev_tio_status *tio_status;
+#endif
 };
 
 bool sev_version_greater_or_equal(u8 maj, u8 min);
@@ -102,4 +110,14 @@ static inline void sev_snp_destroy_firmware_upload(struct sev_device *sev) { }
 static inline int sev_snp_synthetic_error(struct sev_device *sev, int *psp_ret) { return 0; }
 #endif /* CONFIG_CRYPTO_DEV_SP_PSP_FW_UPLOAD */
 
+#if defined(CONFIG_PCI_TSM) || defined(CONFIG_PCI_TSM_MODULE)
+void sev_tsm_init(struct sev_device *sev);
+void sev_tsm_uninit(struct sev_device *sev);
+int sev_tio_cmd_buffer_len(int cmd);
+#else
+static inline void sev_tsm_init(struct sev_device *sev) {}
+static inline void sev_tsm_uninit(struct sev_device *sev) {}
+static inline int sev_tio_cmd_buffer_len(int cmd) { return 0; }
+#endif
+
 #endif /* __SEV_DEV_H */
include/linux/psp-sev.h +27 -0
index 103d9c161f41..5d276e2c2112 100644
@@ -12,6 +12,7 @@
 #ifndef __PSP_SEV_H__
 #define __PSP_SEV_H__
 
+#include <linux/tsm.h>
 #include <uapi/linux/psp-sev.h>
 
 #define SEV_FW_BLOB_MAX_SIZE	0x4000	/* 16KB */
@@ -109,6 +110,27 @@ enum sev_cmd {
 	SEV_CMD_SNP_VLEK_LOAD		= 0x0CD,
 	SEV_CMD_SNP_FEATURE_INFO	= 0x0CE,
 
+	/* SEV-TIO commands */
+	SEV_CMD_TIO_STATUS		= 0x0D0,
+	SEV_CMD_TIO_INIT		= 0x0D1,
+	SEV_CMD_TIO_DEV_CREATE		= 0x0D2,
+	SEV_CMD_TIO_DEV_RECLAIM		= 0x0D3,
+	SEV_CMD_TIO_DEV_CONNECT		= 0x0D4,
+	SEV_CMD_TIO_DEV_DISCONNECT	= 0x0D5,
+	SEV_CMD_TIO_DEV_STATUS		= 0x0D6,
+	SEV_CMD_TIO_DEV_MEASUREMENTS	= 0x0D7,
+	SEV_CMD_TIO_DEV_CERTIFICATES	= 0x0D8,
+	SEV_CMD_TIO_TDI_CREATE		= 0x0DA,
+	SEV_CMD_TIO_TDI_RECLAIM		= 0x0DB,
+	SEV_CMD_TIO_TDI_BIND		= 0x0DC,
+	SEV_CMD_TIO_TDI_UNBIND		= 0x0DD,
+	SEV_CMD_TIO_TDI_REPORT		= 0x0DE,
+	SEV_CMD_TIO_TDI_STATUS		= 0x0DF,
+	SEV_CMD_TIO_GUEST_REQUEST	= 0x0E0,
+	SEV_CMD_TIO_ASID_FENCE_CLEAR	= 0x0E1,
+	SEV_CMD_TIO_ASID_FENCE_STATUS	= 0x0E2,
+	SEV_CMD_TIO_TDI_INFO		= 0x0E3,
+	SEV_CMD_TIO_ROLL_KEY		= 0x0E4,
 	SEV_CMD_MAX,
 };
 
@@ -770,6 +792,11 @@ struct sev_data_snp_guest_request {
 	u64 res_paddr;				/* In */
 } __packed;
 
+struct tio_guest_request {
+	struct sev_data_snp_guest_request data;
+	int fw_err;
+};
+
 /**
  * struct sev_data_snp_init_ex - SNP_INIT_EX structure
  *
include/uapi/linux/psp-sev.h +2 -0
index affa65dcebd4..a2fbc20c5db6 100644
@@ -88,6 +88,8 @@ typedef enum {
 	SEV_RET_RMP_INITIALIZATION_FAILED  = 0x0026,
 	SEV_RET_INVALID_KEY                = 0x0027,
 	SEV_RET_SHUTDOWN_INCOMPLETE        = 0x0028,
+	SEV_RET_INCORRECT_BUFFER_LENGTH	   = 0x0030,
+	SEV_RET_EXPAND_BUFFER_LENGTH_REQUEST = 0x0031,
 	SEV_RET_SPDM_REQUEST               = 0x0032,
 	SEV_RET_SPDM_ERROR                 = 0x0033,
 	SEV_RET_IN_USE                     = 0x003A,
drivers/crypto/ccp/sev-dev-tio.c new file +1664 -0

1,664 lines not on this page · raw message

drivers/crypto/ccp/sev-dev-tsm.c new file +709 -0

709 lines not on this page · raw message

drivers/crypto/ccp/sev-dev.c +9 -1
index b01e5f913727..d59d74d3aaca 100644
@@ -37,6 +37,7 @@
 
 #include "psp-dev.h"
 #include "sev-dev.h"
+#include "sev-dev-tio.h"
 
 #define DEVICE_NAME		"sev"
 #define SEV_FW_FILE		"amd/sev.fw"
@@ -234,7 +235,7 @@ static int sev_cmd_buffer_len(int cmd)
 	case SEV_CMD_SNP_COMMIT:		return sizeof(struct sev_data_snp_commit);
 	case SEV_CMD_SNP_FEATURE_INFO:		return sizeof(struct sev_data_snp_feature_info);
 	case SEV_CMD_SNP_DOWNLOAD_FIRMWARE_EX:	return sizeof(struct sev_data_download_firmware_ex);
-	default:				return 0;
+	default:				return sev_tio_cmd_buffer_len(cmd);
 	}
 
 	return 0;
@@ -2631,6 +2632,8 @@ void sev_pci_init(void)
 
 	atomic_notifier_chain_register(&panic_notifier_list,
 				       &snp_panic_notifier);
+	sev_tsm_init(sev);
+
 	return;
 
 err:
@@ -2647,6 +2650,11 @@ void sev_pci_exit(void)
 		return;
 
 	sev_firmware_shutdown(sev);
+	/*
+	 * sev_tsm_uninit needs to clear tio_en after sev_firmware_shutdown to let it
+	 * do proper cleanup.
+	 */
+	sev_tsm_uninit(sev);
 
 	atomic_notifier_chain_unregister(&panic_notifier_list,
 					 &snp_panic_notifier);
drivers/virt/coco/host/tsm-host.c +1 -3
index 80f3315fb195..5d23a3871009 100644
@@ -265,7 +265,7 @@ static char *spdm_algos_to_str(u64 algos, char *buf, size_t len)
 
 	buf[0] = 0;
 #define __ALGO(x) do {								\
-		if ((n < len) && (algos & (1ULL << (TSM_TDI_SPDM_ALGOS_##x))))	\
+		if ((n < len) && (algos & (1ULL << (TSM_SPDM_ALGOS_##x))))	\
 			n += snprintf(buf + n, len - n, #x" ");			\
 	} while (0)
 
@@ -287,7 +287,6 @@ static const char *tdisp_state_to_str(enum tsm_tdisp_state state)
 {
 	switch (state) {
 #define __ST(x) case TDISP_STATE_##x: return #x
-	case TDISP_STATE_UNAVAIL: return "TDISP state unavailable";
 	__ST(CONFIG_UNLOCKED);
 	__ST(CONFIG_LOCKED);
 	__ST(RUN);
@@ -475,7 +474,6 @@ void tsm_tdi_unbind(struct tsm_tdi *tdi)
 	}
 
 	tdi->guest_rid = 0;
-	tdi->dev.parent->tdi_enabled = false;
 }
 EXPORT_SYMBOL_GPL(tsm_tdi_unbind);
 
drivers/crypto/ccp/Kconfig +2 -0
index 40be991f15d2..459bc339e651 100644
@@ -25,6 +25,7 @@ config CRYPTO_DEV_CCP_CRYPTO
 	default m
 	depends on CRYPTO_DEV_CCP_DD
 	depends on CRYPTO_DEV_SP_CCP
+	depends on PCI_TSM
 	select CRYPTO_HASH
 	select CRYPTO_SKCIPHER
 	select CRYPTO_AUTHENC
@@ -39,6 +40,7 @@ config CRYPTO_DEV_SP_PSP
 	bool "Platform Security Processor (PSP) device"
 	default y
 	depends on CRYPTO_DEV_CCP_DD && X86_64 && AMD_IOMMU
+	select TSM_HOST
 	help
 	 Provide support for the AMD Platform Security Processor (PSP).
 	 The PSP is a dedicated processor that provides support for key
Signature · 1 line
2.47.1
author maintainer
#23 lore raw

RFC PATCH v2 10/22 KVM: SVM: Add uAPI to change RMP for MMIO

In reply to Alexey Kardashevskiy

The TDI bind operation moves the TDI into "RUN" state which means that TEE resources are now to be used as encrypted, or the device will refuse to operate. This requires RMP setup for MMIO BARs which is done in 2 steps:
- RMPUPDATE on the host to assign host's MMIO ranges to GPA (like RAM);
- validate the RMP entry which is done via TIO GUEST REQUEST GHCB message (unlike RAM for which the VM could just call PVALIDATE) but TDI bind must complete first to ensure the TDI is in the LOCKED state so the location of MMIO is fixed.

The bind happens on the first TIO GUEST REQUEST from the guest. At this point KVM does not have host TDI BDFn so it exits to QEMU which calls VFIO-IOMMUFD to bind the TDI.

Now, RMPUPDATE need to be done, in some place on the way back to the guest. Possible places are:
a) the VFIO-IOMMUFD bind handler (does not know GPAs);
b) QEMU (can mmapp MMIO and knows GPA);
c) the KVM handler which received the first TIO GUEST REQUEST (does not know host MMIO ranges or host BDFn).

The b) approach is taken. Add an KVM ioctl() to update RMP table for a given MMIO range. Lots of cut-n-paste.

The validation happens later on explicit guest requests.

---
 arch/x86/include/asm/sev.h      |   2 +
 arch/x86/include/uapi/asm/kvm.h |  11 ++
 arch/x86/kvm/svm/sev.c          | 135 ++++++++++++++++++++
 arch/x86/virt/svm/sev.c         |  34 ++++-
 4 files changed, 178 insertions(+), 4 deletions(-)
arch/x86/include/asm/sev.h +2 -0
index c5e9455df0dc..2cae72b618d0 100644
@@ -573,7 +573,9 @@ int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level);
 void snp_dump_hva_rmpentry(unsigned long address);
 int psmash(u64 pfn);
 int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, u32 asid, bool immutable);
+int rmp_make_private_mmio(u64 pfn, u64 gpa, enum pg_level level, u32 asid, bool immutable);
 int rmp_make_shared(u64 pfn, enum pg_level level);
+int rmp_make_shared_mmio(u64 pfn, enum pg_level level);
 void snp_leak_pages(u64 pfn, unsigned int npages);
 void kdump_sev_callback(void);
 void snp_fixup_e820_tables(void);
arch/x86/include/uapi/asm/kvm.h +11 -0
index 9e75da97bce0..1681bd8c5d33 100644
@@ -704,6 +704,7 @@ enum sev_cmd_id {
 	KVM_SEV_SNP_LAUNCH_START = 100,
 	KVM_SEV_SNP_LAUNCH_UPDATE,
 	KVM_SEV_SNP_LAUNCH_FINISH,
+	KVM_SEV_SNP_MMIO_RMP_UPDATE,
 
 	KVM_SEV_NR_MAX,
 };
@@ -874,6 +875,16 @@ struct kvm_sev_snp_launch_finish {
 	__u64 pad1[4];
 };
 
+#define KVM_SEV_SNP_RMP_FLAG_PRIVATE		BIT(0)
+
+struct kvm_sev_snp_rmp_update {
+	__u32 flags; /* KVM_SEV_SNP_RMP_FLAG_xxxx */
+	__u32 pad0;
+	__u64 useraddr;
+	__u64 gpa;
+	__u64 size;
+};
+
 #define KVM_X2APIC_API_USE_32BIT_IDS            (1ULL << 0)
 #define KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK  (1ULL << 1)
 
arch/x86/kvm/svm/sev.c +135 -0

153 lines not on this page · raw message

arch/x86/virt/svm/sev.c +30 -4
index 1dcc027ec77e..6e6bd3c2f7ec 100644
@@ -978,7 +978,7 @@ static int adjust_direct_map(u64 pfn, int rmp_level)
  * The optimal solution would be range locking to avoid locking disjoint
  * regions unnecessarily but there's no support for that yet.
  */
-static int rmpupdate(u64 pfn, struct rmp_state *state)
+static int rmpupdate(u64 pfn, struct rmp_state *state, bool mmio)
 {
 	unsigned long paddr = pfn << PAGE_SHIFT;
 	int ret, level;
@@ -988,7 +988,7 @@ static int rmpupdate(u64 pfn, struct rmp_state *state)
 
 	level = RMP_TO_PG_LEVEL(state->pagesize);
 
-	if (adjust_direct_map(pfn, level))
+	if (!mmio && adjust_direct_map(pfn, level))
 		return -EFAULT;
 
 	do {
@@ -1022,10 +1022,25 @@ int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, u32 asid, bool immut
 	state.gpa = gpa;
 	state.pagesize = PG_LEVEL_TO_RMP(level);
 
-	return rmpupdate(pfn, &state);
+	return rmpupdate(pfn, &state, false);
 }
 EXPORT_SYMBOL_GPL(rmp_make_private);
 
+int rmp_make_private_mmio(u64 pfn, u64 gpa, enum pg_level level, u32 asid, bool immutable)
+{
+	struct rmp_state state;
+
+	memset(&state, 0, sizeof(state));
+	state.assigned = 1;
+	state.asid = asid;
+	state.immutable = immutable;
+	state.gpa = gpa;
+	state.pagesize = PG_LEVEL_TO_RMP(level);
+
+	return rmpupdate(pfn, &state, true);
+}
+EXPORT_SYMBOL_GPL(rmp_make_private_mmio);
+
 /* Transition a page to hypervisor-owned/shared state in the RMP table. */
 int rmp_make_shared(u64 pfn, enum pg_level level)
 {
@@ -1034,10 +1049,21 @@ int rmp_make_shared(u64 pfn, enum pg_level level)
 	memset(&state, 0, sizeof(state));
 	state.pagesize = PG_LEVEL_TO_RMP(level);
 
-	return rmpupdate(pfn, &state);
+	return rmpupdate(pfn, &state, false);
 }
 EXPORT_SYMBOL_GPL(rmp_make_shared);
 
+int rmp_make_shared_mmio(u64 pfn, enum pg_level level)
+{
+	struct rmp_state state;
+
+	memset(&state, 0, sizeof(state));
+	state.pagesize = PG_LEVEL_TO_RMP(level);
+
+	return rmpupdate(pfn, &state, true);
+}
+EXPORT_SYMBOL_GPL(rmp_make_shared_mmio);
+
 void snp_leak_pages(u64 pfn, unsigned int npages)
 {
 	struct page *page = pfn_to_page(pfn);
Signature · 1 line
2.47.1
author maintainer
#26 lore raw

RFC PATCH v2 11/22 KVM: SEV: Add TIO VMGEXIT

In reply to Alexey Kardashevskiy

The SEV TIO spec defines a new TIO_GUEST_MESSAGE message to provide a secure communication channel between a SNP VM and the PSP, very similar to the GHCB GUEST MESSAGE. However the new call requires additional information about the host and guest PCI BDFn and the VM id (which is GCTX==guest context page). Since KVM does not know PCI, it exits to QEMU which has all the pieces to make the call to the PSP. This relies on necessary additional ioctl() are implemented separately, such as:
- IOMMUFD "TDI bind" to bind a secure VF to a CoCo VM;
- IOMMUFD "Guest Request" to manage secure DMA and MMIO;
- SEV KVM ioctl() to call RMPUPDATE on MMIO ranges.

Define new VMGEXIT code - SEV_TIO_GUEST_REQUEST. Define its parameters in kvm_run::kvm_user_vmgexit. These include:
- guest BDFn,
- GHCB request/response buffers (encrypted guest pages),
- space for certificate/measurements/interface repors (non encrypted guest pages).

Some numeric values are out of order because numbers in between have been used at different stages of KVM SEV-SNP upstreaming process.

---
 arch/x86/include/uapi/asm/svm.h |  2 +
 include/uapi/linux/kvm.h        | 24 +++++++
 arch/x86/kvm/svm/sev.c          | 70 ++++++++++++++++++++
 3 files changed, 96 insertions(+)
arch/x86/include/uapi/asm/svm.h +2 -0
index 1814b413fd57..ac90a69e6327 100644
@@ -116,6 +116,7 @@
 #define SVM_VMGEXIT_AP_CREATE			1
 #define SVM_VMGEXIT_AP_DESTROY			2
 #define SVM_VMGEXIT_SNP_RUN_VMPL		0x80000018
+#define SVM_VMGEXIT_SEV_TIO_GUEST_REQUEST	0x80000020
 #define SVM_VMGEXIT_HV_FEATURES			0x8000fffd
 #define SVM_VMGEXIT_TERM_REQUEST		0x8000fffe
 #define SVM_VMGEXIT_TERM_REASON(reason_set, reason_code)	\
@@ -237,6 +238,7 @@
 	{ SVM_VMGEXIT_GUEST_REQUEST,	"vmgexit_guest_request" }, \
 	{ SVM_VMGEXIT_EXT_GUEST_REQUEST, "vmgexit_ext_guest_request" }, \
 	{ SVM_VMGEXIT_AP_CREATION,	"vmgexit_ap_creation" }, \
+	{ SVM_VMGEXIT_SEV_TIO_GUEST_REQUEST, "vmgexit_sev_tio_guest_request" }, \
 	{ SVM_VMGEXIT_HV_FEATURES,	"vmgexit_hypervisor_feature" }, \
 	{ SVM_EXIT_ERR,         "invalid_guest_state" }
 
include/uapi/linux/kvm.h +24 -0
index 45e6d8fca9b9..cb3bc5b9c1e0 100644
@@ -135,6 +135,28 @@ struct kvm_xen_exit {
 	} u;
 };
 
+struct kvm_user_vmgexit {
+#define KVM_USER_VMGEXIT_TIO_REQ	4
+	__u32 type; /* KVM_USER_VMGEXIT_* type */
+	union {
+		struct {
+			__u32 guest_rid;	/* in */
+			__u16 ret;		/* out */
+#define KVM_USER_VMGEXIT_TIO_REQ_FLAG_STATUS		BIT(0)
+#define KVM_USER_VMGEXIT_TIO_REQ_FLAG_MMIO_VALIDATE	BIT(1)
+#define KVM_USER_VMGEXIT_TIO_REQ_FLAG_MMIO_CONFIG	BIT(2)
+			__u8  flags;		/* in */
+			__u8  tdi_status;	/* out */
+			__u64 data_gpa;		/* in */
+			__u64 data_npages;	/* in/out */
+			__u64 req_spa;		/* in */
+			__u64 rsp_spa;		/* in */
+			__u64 mmio_gpa;		/* in */
+			__s32 fw_err;		/* out */
+		} tio_req;
+	};
+} __packed;
+
 #define KVM_S390_GET_SKEYS_NONE   1
 #define KVM_S390_SKEYS_MAX        1048576
 
@@ -178,6 +200,7 @@ struct kvm_xen_exit {
 #define KVM_EXIT_NOTIFY           37
 #define KVM_EXIT_LOONGARCH_IOCSR  38
 #define KVM_EXIT_MEMORY_FAULT     39
+#define KVM_EXIT_VMGEXIT          40
 
 /* For KVM_EXIT_INTERNAL_ERROR */
 /* Emulate instruction failed. */
@@ -446,6 +469,7 @@ struct kvm_run {
 			__u64 gpa;
 			__u64 size;
 		} memory_fault;
+		struct kvm_user_vmgexit vmgexit;
 		/* Fix the size of the union. */
 		char padding[256];
 	};
arch/x86/kvm/svm/sev.c +70 -0

88 lines not on this page · raw message

Signature · 1 line
2.47.1
author maintainer
#27 lore raw

RFC PATCH v2 12/22 iommufd: Allow mapping from guest_memfd

In reply to Alexey Kardashevskiy

CoCo VMs get their private memory allocated from guest_memfd ("gmemfd") which is a KVM facility similar to memfd. At the moment gmemfds cannot mmap() so the usual GUP API does not work on these as expected.

Use the existing IOMMU_IOAS_MAP_FILE API to allow mapping from fd + offset. Detect the gmemfd case in pfn_reader_user_pin() and simplified mapping.

The long term plan is to ditch this workaround and follow the usual memfd path.

---
 drivers/iommu/iommufd/pages.c | 88 +++++++++++++++++++-
 1 file changed, 87 insertions(+), 1 deletion(-)
drivers/iommu/iommufd/pages.c +87 -1
index 3427749bc5ce..457d8eaacd2c 100644
@@ -53,6 +53,7 @@
 #include <linux/overflow.h>
 #include <linux/slab.h>
 #include <linux/sched/mm.h>
+#include <linux/pagemap.h>
 
 #include "double_span.h"
 #include "io_pagetable.h"
@@ -850,6 +851,88 @@ static long pin_memfd_pages(struct pfn_reader_user *user, unsigned long start,
 	return npages_out;
 }
 
+static bool is_guest_memfd(struct file *file)
+{
+	struct address_space *mapping = file_inode(file)->i_mapping;
+
+	return mapping_inaccessible(mapping) && mapping_unevictable(mapping);
+}
+
+static struct folio *guest_memfd_get_pfn(struct file *file, unsigned long index,
+					 unsigned long *pfn, int *max_order)
+{
+	struct folio *folio;
+	int ret = 0;
+
+	folio = filemap_grab_folio(file_inode(file)->i_mapping, index);
+
+	if (IS_ERR(folio))
+		return folio;
+
+	if (folio_test_hwpoison(folio)) {
+		folio_unlock(folio);
+		folio_put(folio);
+		return ERR_PTR(-EHWPOISON);
+	}
+
+	*pfn = folio_pfn(folio) + (index & (folio_nr_pages(folio) - 1));
+	if (!max_order)
+		goto unlock_exit;
+
+	/* Refs for unpin_user_page_range_dirty_lock->gup_put_folio(FOLL_PIN) */
+	ret = folio_add_pins(folio, 1);
+	folio_put(folio); /* Drop ref from filemap_grab_folio */
+
+unlock_exit:
+	folio_unlock(folio);
+	if (ret)
+		folio = ERR_PTR(ret);
+
+	return folio;
+}
+
+static long pin_guest_memfd_pages(struct pfn_reader_user *user, loff_t start, unsigned long npages,
+			       struct iopt_pages *pages)
+{
+	unsigned long offset = 0;
+	loff_t uptr = start;
+	long rc = 0;
+
+	for (unsigned long i = 0; i < npages; ++i, uptr += PAGE_SIZE) {
+		unsigned long gfn = 0, pfn = 0;
+		int max_order = 0;
+		struct folio *folio;
+
+		folio = guest_memfd_get_pfn(user->file, uptr >> PAGE_SHIFT, &pfn, &max_order);
+		if (IS_ERR(folio))
+			rc = PTR_ERR(folio);
+
+		if (rc == -EINVAL && i == 0) {
+			pr_err_once("Must be vfio mmio at gfn=%lx pfn=%lx, skipping\n", gfn, pfn);
+			return rc;
+		}
+
+		if (rc) {
+			pr_err("%s: %ld %ld %lx -> %lx\n", __func__,
+			       rc, i, (unsigned long) uptr, (unsigned long) pfn);
+			break;
+		}
+
+		if (i == 0)
+			offset = offset_in_folio(folio, start);
+
+		user->ufolios[i] = folio;
+	}
+
+	if (!rc) {
+		rc = npages;
+		user->ufolios_next = user->ufolios;
+		user->ufolios_offset = offset;
+	}
+
+	return rc;
+}
+
 static int pfn_reader_user_pin(struct pfn_reader_user *user,
 			       struct iopt_pages *pages,
 			       unsigned long start_index,
@@ -903,7 +986,10 @@ static int pfn_reader_user_pin(struct pfn_reader_user *user,
 
 	if (user->file) {
 		start = pages->start + (start_index * PAGE_SIZE);
-		rc = pin_memfd_pages(user, start, npages);
+		if (is_guest_memfd(user->file))
+			rc = pin_guest_memfd_pages(user, start, npages, pages);
+		else
+			rc = pin_memfd_pages(user, start, npages);
 	} else if (!remote_mm) {
 		uptr = (uintptr_t)(pages->uptr + start_index * PAGE_SIZE);
 		rc = pin_user_pages_fast(uptr, npages, user->gup_flags,
Signature · 1 line
2.47.1
author maintainer
#40 lore raw

RFC PATCH v2 13/22 iommufd: amd-iommu: Add vdevice support

In reply to Alexey Kardashevskiy

The new AMD SEV-TIO feature allows DMA to/from encrypted memory and encrypted MMIO for use in CoCo VMs (SEV-SNP). The secure part of IOMMU is called sDTE ("Secure DTE") which resides in private memory and controlled by the PSP. sDTEs of passed through devices are in the TCB of CoCo VMs and inaccessible by the host OS.

Implement vdevice in the AMD IOMMU host OS to represent the host instance of a secure IOMMU which is visible to the guest. This will be used for GHCB TIO GUEST REQUEST to manage secure sDTE and MMIO.

Most parts of insecure DTE move to sDTE so DTEs need to be adjusted. At the moment this includes "domain_id" (moves to sDTE) and "IOTLB enable" (should stay in sync with sDTE).

---
 drivers/iommu/amd/amd_iommu_types.h |  2 +
 include/uapi/linux/iommufd.h        |  1 +
 drivers/iommu/amd/iommu.c           | 60 +++++++++++++++++++-
 3 files changed, 61 insertions(+), 2 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +2 -0
index b086fb632990..b5513bf05b27 100644
@@ -593,6 +593,8 @@ struct protection_domain {
 
 	struct mmu_notifier mn;	/* mmu notifier for the SVA domain */
 	struct list_head dev_data_list; /* List of pdom_dev_data */
+
+	struct amd_viommu *aviommu;
 };
 
 /*
include/uapi/linux/iommufd.h +1 -0
index 78747b24bd0f..b346fa11955c 100644
@@ -939,6 +939,7 @@ struct iommu_fault_alloc {
 enum iommu_viommu_type {
 	IOMMU_VIOMMU_TYPE_DEFAULT = 0,
 	IOMMU_VIOMMU_TYPE_ARM_SMMUV3 = 1,
+	IOMMU_VIOMMU_TYPE_TSM = 2,
 };
 
 /**
drivers/iommu/amd/iommu.c +58 -2

95 lines not on this page · raw message

Signature · 1 line
2.47.1
author maintainer
#46 lore raw

RFC PATCH v2 14/22 iommufd: Add TIO calls

In reply to Alexey Kardashevskiy

When a TDISP-capable device is passed through, it is configured as a shared device to begin with. Later on when a VM probes the device, detects its TDISP capability (reported via the PCIe ExtCap bit called "TEE-IO"), performs the device attestation and transitions it to a secure state when the device can run encrypted DMA and respond to encrypted MMIO accesses.

Since KVM is out of the TCB, secure enablement is done in the secure firmware. The API requires PCI host/guest BDFns, a KVM id hence such calls are routed via IOMMUFD, primarily because allowing secure DMA is the major performance bottleneck and it is a function of IOMMU.

Add TDI bind to do the initial binding of a passed through PCI function to a VM. Add a forwarder for TIO GUEST REQUEST. These two call into the TSM which forwards the calls to the PSP.

---

Both enabling secure DMA (== "SDTE Write") and secure MMIO (== "MMIO validate") are TIO GUEST REQUEST messages. These are encrypted and the HV (==IOMMUFD or KVM or VFIO) cannot see them unless the guest shares some via kvm_run::kvm_user_vmgexit (and then QEMU passes those via ioctls).

This RFC routes all TIO GUEST REQUESTs via IOMMUFD which arguably should only do so only for "SDTE Write" and leave "MMIO validate" for VFIO.

---
 drivers/iommu/iommufd/iommufd_private.h |   3 +
 include/uapi/linux/iommufd.h            |  25 +++++
 drivers/iommu/iommufd/main.c            |   6 ++
 drivers/iommu/iommufd/viommu.c          | 112 ++++++++++++++++++++
 4 files changed, 146 insertions(+)
drivers/iommu/iommufd/iommufd_private.h +3 -0
index 0b1bafc7fd99..47a6fb5da253 100644
@@ -546,6 +546,8 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd);
 void iommufd_viommu_destroy(struct iommufd_object *obj);
 int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd);
 void iommufd_vdevice_destroy(struct iommufd_object *obj);
+int iommufd_vdevice_tsm_bind_ioctl(struct iommufd_ucmd *ucmd);
+int iommufd_vdevice_tsm_guest_request_ioctl(struct iommufd_ucmd *ucmd);
 
 struct iommufd_vdevice {
 	struct iommufd_object obj;
@@ -553,6 +555,7 @@ struct iommufd_vdevice {
 	struct iommufd_viommu *viommu;
 	struct device *dev;
 	u64 id; /* per-vIOMMU virtual ID */
+	bool tsm_bound;
 };
 
 #ifdef CONFIG_IOMMUFD_TEST
include/uapi/linux/iommufd.h +25 -0
index b346fa11955c..0af15dcabd23 100644
@@ -55,6 +55,8 @@ enum {
 	IOMMUFD_CMD_VIOMMU_ALLOC = 0x90,
 	IOMMUFD_CMD_VDEVICE_ALLOC = 0x91,
 	IOMMUFD_CMD_IOAS_CHANGE_PROCESS = 0x92,
+	IOMMUFD_CMD_VDEVICE_TSM_BIND = 0x93,
+	IOMMUFD_CMD_VDEVICE_TSM_GUEST_REQUEST = 0x94,
 };
 
 /**
@@ -1015,4 +1017,27 @@ struct iommu_ioas_change_process {
 #define IOMMU_IOAS_CHANGE_PROCESS \
 	_IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_CHANGE_PROCESS)
 
+struct iommu_vdevice_tsm_bind {
+	__u32 size;
+	__u32 viommu_id;
+	__u32 dev_id;
+	__u32 vdevice_id;
+	__s32 kvmfd;
+	__u32 pad;
+} __packed;
+#define IOMMU_VDEVICE_TSM_BIND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_BIND)
+
+struct iommu_vdevice_tsm_guest_request {
+	__u32 size;
+	__u32 viommu_id;
+	__u32 dev_id;
+	__u32 vdevice_id;
+	__u8 *req;
+	__u8 *rsp;
+	__u32 rsp_len;
+	__u32 req_len;
+	__s32 fw_err;
+} __packed;
+#define IOMMU_VDEVICE_TSM_GUEST_REQUEST _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_GUEST_REQUEST)
+
 #endif
drivers/iommu/iommufd/main.c +6 -0
index ccf616462a1c..c9152ef3dcab 100644
@@ -310,6 +310,8 @@ union ucmd_buffer {
 	struct iommu_vdevice_alloc vdev;
 	struct iommu_vfio_ioas vfio_ioas;
 	struct iommu_viommu_alloc viommu;
+	struct iommu_vdevice_tsm_bind bind;
+	struct iommu_vdevice_tsm_guest_request gr;
 #ifdef CONFIG_IOMMUFD_TEST
 	struct iommu_test_cmd test;
 #endif
@@ -367,6 +369,10 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = {
 		 __reserved),
 	IOCTL_OP(IOMMU_VIOMMU_ALLOC, iommufd_viommu_alloc_ioctl,
 		 struct iommu_viommu_alloc, out_viommu_id),
+	IOCTL_OP(IOMMU_VDEVICE_TSM_BIND, iommufd_vdevice_tsm_bind_ioctl,
+		 struct iommu_vdevice_tsm_bind, pad),
+	IOCTL_OP(IOMMU_VDEVICE_TSM_GUEST_REQUEST, iommufd_vdevice_tsm_guest_request_ioctl,
+		 struct iommu_vdevice_tsm_guest_request, fw_err),
 #ifdef CONFIG_IOMMUFD_TEST
 	IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last),
 #endif
drivers/iommu/iommufd/viommu.c +112 -0

127 lines not on this page · raw message

Signature · 1 line
2.47.1
author maintainer
#78 lore raw

RFC PATCH v2 15/22 KVM: X86: Handle private MMIO as shared

In reply to Alexey Kardashevskiy

Currently private MMIO nested page faults are not expected so when such fault occurs, KVM tries moving the faulted page from private to shared which is not going to work as private MMIO is not backed by memfd.

Handle private MMIO as shared: skip page state change and memfd page state tracking.

The MMIO KVM memory slot is still marked as shared as the guest can access it as private or shared so marking the MMIO slot as private is not going to help.

---
 arch/x86/kvm/mmu/mmu.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +5 -1
index 74c20dbb92da..32e27080b1c7 100644
@@ -4347,7 +4347,11 @@ static int __kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu,
 {
 	unsigned int foll = fault->write ? FOLL_WRITE : 0;
 
-	if (fault->is_private)
+	if (fault->slot && fault->is_private && !kvm_slot_can_be_private(fault->slot) &&
+	    (vcpu->kvm->arch.vm_type == KVM_X86_SNP_VM))
+		pr_warn("%s: private SEV TIO MMIO fault for fault->gfn=%llx\n",
+			__func__, fault->gfn);
+	else if (fault->is_private)
 		return kvm_mmu_faultin_pfn_private(vcpu, fault);
 
 	foll |= FOLL_NOWAIT;
Signature · 1 line
2.47.1
author maintainer
#81 lore raw

RFC PATCH v2 16/22 coco/tsm: Add tsm-guest module

In reply to Alexey Kardashevskiy

The "coco/tsm: Add tsm and tsm-host modules" added a common TSM library and the host module which initialises contexts for secure devices on the host.

Guests use some of the host sysfs interface and define their own, hence a new module - TSM GUEST.

Note that the module is made bus-agnostic, like TSM-HOST.

New device nodes provide sysfs interface for fetching device certificates and measurements and TDI interface reports.

A platform is expected to register itself in TSM-GUEST and provide necessary callbacks. No platform is added here, AMD SEV is coming in the next patches.

---
 drivers/virt/coco/guest/Makefile    |   3 +
 include/linux/device.h              |   4 +
 include/linux/tsm.h                 |  20 ++
 drivers/virt/coco/guest/tsm-guest.c | 291 ++++++++++++++++++++
 drivers/virt/coco/host/tsm-host.c   |   1 +
 drivers/virt/coco/tsm.c             |   2 +
 Documentation/virt/coco/tsm.rst     |  33 +++
 drivers/virt/coco/guest/Kconfig     |   3 +
 drivers/virt/coco/sev-guest/Kconfig |   1 +
 9 files changed, 358 insertions(+)
drivers/virt/coco/guest/Makefile +3 -0
index b3b217af77cf..60b688ab816a 100644
@@ -1,3 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_TSM_REPORTS)	+= tsm_report.o
 tsm_report-y := report.o
+
+obj-$(CONFIG_TSM_GUEST) += tsm_guest.o
+tsm_guest-y := tsm-guest.o
include/linux/device.h +4 -0
index 80a5b3268986..e813575b848b 100644
@@ -843,6 +843,10 @@ struct device {
 #ifdef CONFIG_IOMMU_DMA
 	bool			dma_iommu:1;
 #endif
+#if defined(CONFIG_TSM_GUEST) || defined(CONFIG_TSM_GUEST_MODULE)
+	bool			tdi_enabled:1;
+	bool			tdi_validated:1;
+#endif
 };
 
 /**
include/linux/tsm.h +20 -0
index 486e386d90fc..9e25b1a99c19 100644
@@ -353,6 +353,20 @@ struct tsm_hv_ops {
 	int (*tdi_status)(struct tsm_tdi *tdi, struct tsm_tdi_status *ts);
 };
 
+/* featuremask for tdi_validate */
+/* TODO: use it */
+#define TDI_VALIDATE_DMA	BIT(0)
+#define TDI_VALIDATE_MMIO	BIT(1)
+
+struct tsm_vm_ops {
+	int (*tdi_validate)(struct tsm_tdi *tdi, unsigned int featuremask,
+			    bool invalidate, void *private_data);
+	int (*tdi_mmio_config)(struct tsm_tdi *tdi, u64 start, u64 size,
+			       bool tee, void *private_data);
+	int (*tdi_status)(struct tsm_tdi *tdi, void *private_data,
+			  struct tsm_tdi_status *ts);
+};
+
 struct tsm_subsys {
 	struct device dev;
 	struct list_head tdi_head;
@@ -372,6 +386,12 @@ struct tsm_host_subsys;
 struct tsm_host_subsys *tsm_host_register(struct device *parent,
 					  struct tsm_hv_ops *hvops,
 					  void *private_data);
+struct tsm_guest_subsys;
+struct tsm_guest_subsys *tsm_guest_register(struct device *parent,
+					    struct tsm_vm_ops *vmops,
+					    void *private_data);
+void tsm_guest_unregister(struct tsm_guest_subsys *gsubsys);
+
 struct tsm_dev *tsm_dev_get(struct device *dev);
 void tsm_dev_put(struct tsm_dev *tdev);
 struct tsm_tdi *tsm_tdi_get(struct device *dev);
drivers/virt/coco/guest/tsm-guest.c new file +291 -0

291 lines not on this page · raw message

drivers/virt/coco/host/tsm-host.c +1 -0
index 5d23a3871009..7a37327fded8 100644
@@ -474,6 +474,7 @@ void tsm_tdi_unbind(struct tsm_tdi *tdi)
 	}
 
 	tdi->guest_rid = 0;
+	tdi->dev.parent->tdi_enabled = false;
 }
 EXPORT_SYMBOL_GPL(tsm_tdi_unbind);
 
drivers/virt/coco/tsm.c +2 -0
index b6235d1210ca..a6979d51f029 100644
@@ -442,6 +442,8 @@ int tsm_tdi_init(struct tsm_dev *tdev, struct device *parent)
 		goto free_exit;
 
 	tdi->tdev = tdev;
+	tdi->dev.parent->tdi_enabled = true;
+	tdi->dev.parent->tdi_validated = false;
 
 	return 0;
 
Documentation/virt/coco/tsm.rst +33 -0
index 7cb5f1862492..203cc9c8411d 100644
@@ -90,6 +90,39 @@ The sysfs example from a host with a TDISP capable device:
 /sys/devices/pci0000:a0/0000:a0:07.1/0000:a9:00.5/tsm/tsm0
 
 
+The sysfs example from a guest with a TDISP capable device:
+
+~> find /sys -iname "*tsm*"
+/sys/kernel/config/tsm
+/sys/class/tsm-tdi
+/sys/class/tsm
+/sys/class/tsm/tsm0
+/sys/class/tsm-dev
+/sys/devices/platform/sev-guest/tsm
+/sys/devices/platform/sev-guest/tsm/tsm0
+/sys/devices/pci0000:00/0000:00:02.0/0000:01:00.0/tsm_dev
+/sys/devices/pci0000:00/0000:00:02.0/0000:01:00.0/tsm-tdi
+/sys/devices/pci0000:00/0000:00:02.0/0000:01:00.0/tsm-tdi/tdi:0000:01:00.0/tsm_tdi_status
+/sys/devices/pci0000:00/0000:00:02.0/0000:01:00.0/tsm-tdi/tdi:0000:01:00.0/tsm_tdi_validate
+/sys/devices/pci0000:00/0000:00:02.0/0000:01:00.0/tsm-tdi/tdi:0000:01:00.0/tsm_tdi_status_user
+/sys/devices/pci0000:00/0000:00:02.0/0000:01:00.0/tsm-tdi/tdi:0000:01:00.0/tsm_report_user
+/sys/devices/pci0000:00/0000:00:02.0/0000:01:00.0/tsm-tdi/tdi:0000:01:00.0/tsm_report
+/sys/devices/pci0000:00/0000:00:02.0/0000:01:00.0/tsm-dev
+/sys/devices/pci0000:00/0000:00:02.0/0000:01:00.0/tsm-dev/tdev:0000:01:00.0/tsm_certs
+/sys/devices/pci0000:00/0000:00:02.0/0000:01:00.0/tsm-dev/tdev:0000:01:00.0/tsm_nonce
+/sys/devices/pci0000:00/0000:00:02.0/0000:01:00.0/tsm-dev/tdev:0000:01:00.0/tsm_meas_user
+/sys/devices/pci0000:00/0000:00:02.0/0000:01:00.0/tsm-dev/tdev:0000:01:00.0/tsm_certs_user
+/sys/devices/pci0000:00/0000:00:02.0/0000:01:00.0/tsm-dev/tdev:0000:01:00.0/tsm_meas
+/sys/module/tsm_pci
+/sys/module/sev_guest/parameters/tsm_vtom
+/sys/module/sev_guest/parameters/tsm_enable
+/sys/module/tsm_report
+/sys/module/tsm
+/sys/module/tsm/holders/tsm_pci
+/sys/module/tsm/holders/tsm_guest
+/sys/module/tsm_guest
+
+
 References
 ==========
 
drivers/virt/coco/guest/Kconfig +3 -0
index ed9bafbdd854..30f0235f0113 100644
@@ -5,3 +5,6 @@
 config TSM_REPORTS
 	select CONFIGFS_FS
 	tristate
+
+config TSM_GUEST
+	tristate
drivers/virt/coco/sev-guest/Kconfig +1 -0
index a6405ab6c2c3..148af36772ff 100644
@@ -3,6 +3,7 @@ config SEV_GUEST
 	default m
 	depends on AMD_MEM_ENCRYPT
 	select TSM_REPORTS
+	select TSM_GUEST
 	help
 	  SEV-SNP firmware provides the guest a mechanism to communicate with
 	  the PSP without risk from a malicious hypervisor who wishes to read,
Signature · 1 line
2.47.1
author maintainer
#83 lore raw

RFC PATCH v2 17/22 resource: Mark encrypted MMIO resource on validation

In reply to Alexey Kardashevskiy

In order to allow encrypted MMIO, a TDISP device needs to have it set up as "TEE" (means "trusted") and the platform needs to allow accessing it as encrypted (which is "pvalidate"'ed in AMD terms).

Once TDISP MMIO validation succeeded, a resource needs to be mapped as encrypted or the device will reject it.

Add encrypt_resource() which marks the resource as "validated". The TSM module is going to call this API.

Modify __ioremap_check_encrypted() to look for the new flag to allow ioremap() correctly map encrypted resources.

---

Should it also be IORESOURCE_BUSY?

---
 include/linux/ioport.h |  2 +
 arch/x86/mm/ioremap.c  |  2 +
 kernel/resource.c      | 48 ++++++++++++++++++++
 3 files changed, 52 insertions(+)
include/linux/ioport.h +2 -0
index 5385349f0b8a..f2e0b9f02373 100644
@@ -55,6 +55,7 @@ struct resource {
 #define IORESOURCE_MEM_64	0x00100000
 #define IORESOURCE_WINDOW	0x00200000	/* forwarded by bridge */
 #define IORESOURCE_MUXED	0x00400000	/* Resource is software muxed */
+#define IORESOURCE_VALIDATED	0x00800000	/* TDISP validated */
 
 #define IORESOURCE_EXT_TYPE_BITS 0x01000000	/* Resource extended types */
 #define IORESOURCE_SYSRAM	0x01000000	/* System RAM (modifier) */
@@ -248,6 +249,7 @@ extern int allocate_resource(struct resource *root, struct resource *new,
 struct resource *lookup_resource(struct resource *root, resource_size_t start);
 int adjust_resource(struct resource *res, resource_size_t start,
 		    resource_size_t size);
+int encrypt_resource(struct resource *res, unsigned int flags);
 resource_size_t resource_alignment(struct resource *res);
 
 /**
arch/x86/mm/ioremap.c +2 -0
index 38ff7791a9c7..748f39af127a 100644
@@ -100,6 +100,8 @@ static unsigned int __ioremap_check_encrypted(struct resource *res)
 	switch (res->desc) {
 	case IORES_DESC_NONE:
 	case IORES_DESC_RESERVED:
+		if (res->flags & IORESOURCE_VALIDATED)
+			return IORES_MAP_ENCRYPTED;
 		break;
 	default:
 		return IORES_MAP_ENCRYPTED;
kernel/resource.c +48 -0

60 lines not on this page · raw message

Signature · 1 line
2.47.1
author maintainer
#85 lore raw

RFC PATCH v2 18/22 coco/sev-guest: Implement the guest support for SEV TIO

In reply to Alexey Kardashevskiy

Define tsm_ops for the guest and forward the ops calls to the HV via SVM_VMGEXIT_SEV_TIO_GUEST_REQUEST.
Do the attestation report examination and enable MMIO.

In this RFC exercise, encrypted DMA supported for Coco VMs. Secure DMA is allowed to all memory below "vTOM" (set now to the possible maximum so all DMA is encrypted) after the VM performs "validation" via GHCB TIO GUEST REQUEST "sDTE write" call to the secure fw.

Encrypted MMIO is possible to TEE regions in the VF (==TDI) interface report and requires accepting MMIO ranges into the guest TCB which is done in 2 steps:
- the HV RMPUPDATEs the ranges;
- the guest accepts them via GHCB TIO GUEST REQUEST "MMIO validate" call to the secure fw (which is AMD's "pvalidate" for MMIO).

---
 drivers/virt/coco/sev-guest/Makefile        |   2 +-
 arch/x86/include/asm/sev.h                  |   9 +
 include/uapi/linux/sev-guest.h              |  39 ++
 arch/x86/coco/sev/core.c                    |  19 +-
 drivers/virt/coco/sev-guest/sev_guest.c     |  10 +
 drivers/virt/coco/sev-guest/sev_guest_tio.c | 738 ++++++++++++++++++++
 6 files changed, 815 insertions(+), 2 deletions(-)
drivers/virt/coco/sev-guest/Makefile +1 -1
index 2d7dffed7b2f..34ea9fab698b 100644
@@ -1,3 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-$(CONFIG_SEV_GUEST) += sev-guest.o
-sev-guest-y += sev_guest.o
+sev-guest-y += sev_guest.o sev_guest_tio.o
arch/x86/include/asm/sev.h +9 -0
index 2cae72b618d0..a396dbcdee68 100644
@@ -119,6 +119,8 @@ struct snp_req_data {
 	unsigned long resp_gpa;
 	unsigned long data_gpa;
 	unsigned int data_npages;
+	unsigned int guest_rid;
+	unsigned long param;
 };
 
 #define MAX_AUTHTAG_LEN		32
@@ -306,6 +308,11 @@ struct snp_guest_dev {
 		struct snp_derived_key_req derived_key;
 		struct snp_ext_report_req ext_report;
 	} req;
+
+#if defined(CONFIG_PCI_TSM) || defined(CONFIG_PCI_TSM_MODULE)
+	struct tsm_guest_subsys *tsm;
+	struct tsm_bus_subsys *tsm_bus;
+#endif
 };
 
 /*
@@ -516,6 +523,8 @@ void *snp_alloc_shared_pages(size_t sz);
 void snp_free_shared_pages(void *buf, size_t sz);
 int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req,
 			   u64 *exitinfo2);
+struct snp_guest_dev;
+void sev_guest_tsm_set_ops(bool set, struct snp_guest_dev *snp_dev);
 
 void __init snp_secure_tsc_prepare(void);
 void __init snp_secure_tsc_init(void);
include/uapi/linux/sev-guest.h +39 -0
index fcdfea767fca..48b6df3d3298 100644
@@ -13,6 +13,7 @@
 #define __UAPI_LINUX_SEV_GUEST_H_
 
 #include <linux/types.h>
+#include <linux/uuid.h>
 
 #define SNP_REPORT_USER_DATA_SIZE 64
 
@@ -96,4 +97,42 @@ struct snp_ext_report_req {
 #define SNP_GUEST_VMM_ERR_INVALID_LEN	1
 #define SNP_GUEST_VMM_ERR_BUSY		2
 
+/*
+ * TIO_GUEST_REQUEST's TIO_MSG_MMIO_VALIDATE_REQ
+ * encoding for MMIO in RDX:
+ *
+ * ........ ....GGGG GGGGGGGG GGGGGGGG GGGGGGGG GGGGGGGG GGGGOOOO OOOOTrrr
+ * Where:
+ *	G - guest physical address
+ *	O - order of 4K pages
+ *	T - TEE (valid for TIO_MSG_MMIO_CONFIG_REQ)
+ *	r - range id == BAR
+ */
+#define MMIO_VALIDATE_GPA(r)      ((r) & 0x000FFFFFFFFFF000ULL)
+#define MMIO_VALIDATE_LEN(r)      (1ULL << (12 + (((r) >> 4) & 0xFF)))
+#define MMIO_VALIDATE_RANGEID(r)  ((r) & 0x7)
+#define MMIO_VALIDATE_RESERVED(r) ((r) & 0xFFF0000000000000ULL)
+#define MMIO_CONFIG_TEE		  BIT(3)
+
+#define MMIO_MK_VALIDATE(start, size, range_id, tee) \
+	(MMIO_VALIDATE_GPA(start) | (get_order(size >> 12) << 4) | \
+	((range_id) & 0xFF) | ((tee)?MMIO_CONFIG_TEE:0))
+
+/* Optional Certificates/measurements/report data from TIO_GUEST_REQUEST */
+struct tio_blob_table_entry {
+	guid_t guid;
+	__u32 offset;
+	__u32 length;
+} __packed;
+
+/* Measurement’s blob: 5caa80c6-12ef-401a-b364-ec59a93abe3f */
+#define TIO_GUID_MEASUREMENTS \
+	GUID_INIT(0x5caa80c6, 0x12ef, 0x401a, 0xb3, 0x64, 0xec, 0x59, 0xa9, 0x3a, 0xbe, 0x3f)
+/* Certificates blob: 078ccb75-2644-49e8-afe7-5686c5cf72f1 */
+#define TIO_GUID_CERTIFICATES \
+	GUID_INIT(0x078ccb75, 0x2644, 0x49e8, 0xaf, 0xe7, 0x56, 0x86, 0xc5, 0xcf, 0x72, 0xf1)
+/* Attestation report: 70dc5b0e-0cc0-4cd5-97bb-ff0ba25bf320 */
+#define TIO_GUID_REPORT \
+	GUID_INIT(0x70dc5b0e, 0x0cc0, 0x4cd5, 0x97, 0xbb, 0xff, 0x0b, 0xa2, 0x5b, 0xf3, 0x20)
+
 #endif /* __UAPI_LINUX_SEV_GUEST_H_ */
arch/x86/coco/sev/core.c +18 -1
index c680057c13fa..c78a5db0feb5 100644
@@ -2592,6 +2592,11 @@ static int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input,
 	if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) {
 		ghcb_set_rax(ghcb, input->data_gpa);
 		ghcb_set_rbx(ghcb, input->data_npages);
+	} else if (exit_code == SVM_VMGEXIT_SEV_TIO_GUEST_REQUEST) {
+		ghcb_set_rax(ghcb, input->data_gpa);
+		ghcb_set_rbx(ghcb, input->data_npages);
+		ghcb_set_rcx(ghcb, input->guest_rid);
+		ghcb_set_rdx(ghcb, input->param);
 	}
 
 	ret = sev_es_ghcb_hv_call(ghcb, &ctxt, exit_code, input->req_gpa, input->resp_gpa);
@@ -2601,6 +2606,8 @@ static int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input,
 	*exitinfo2 = ghcb->save.sw_exit_info_2;
 	switch (*exitinfo2) {
 	case 0:
+		if (exit_code == SVM_VMGEXIT_SEV_TIO_GUEST_REQUEST)
+			input->param = ghcb_get_rdx(ghcb);
 		break;
 
 	case SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_BUSY):
@@ -2613,6 +2620,10 @@ static int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input,
 			input->data_npages = ghcb_get_rbx(ghcb);
 			ret = -ENOSPC;
 			break;
+		} else if (exit_code == SVM_VMGEXIT_SEV_TIO_GUEST_REQUEST) {
+			input->data_npages = ghcb_get_rbx(ghcb);
+			ret = -ENOSPC;
+			break;
 		}
 		fallthrough;
 	default:
@@ -2974,7 +2985,8 @@ static int verify_and_dec_payload(struct snp_msg_desc *mdesc, struct snp_guest_r
 		return -EBADMSG;
 
 	/* Verify response message type and version number. */
-	if (resp_msg_hdr->msg_type != (req_msg_hdr->msg_type + 1) ||
+	if ((resp_msg_hdr->msg_type != (req_msg_hdr->msg_type + 1) &&
+	     (resp_msg_hdr->msg_type != (req_msg_hdr->msg_type - 0x80))) ||
 	    resp_msg_hdr->msg_version != req_msg_hdr->msg_version)
 		return -EBADMSG;
 
@@ -3047,6 +3059,11 @@ static int __handle_guest_request(struct snp_msg_desc *mdesc, u64 exit_code,
 	rc = snp_issue_guest_request(exit_code, input, exitinfo2);
 	switch (rc) {
 	case -ENOSPC:
+		if (exit_code == SVM_VMGEXIT_SEV_TIO_GUEST_REQUEST) {
+			pr_warn("SVM_VMGEXIT_SEV_TIO_GUEST_REQUEST => -ENOSPC");
+			break;
+		}
+
 		/*
 		 * If the extended guest request fails due to having too
 		 * small of a certificate data buffer, retry the same
drivers/virt/coco/sev-guest/sev_guest.c +10 -0
index 4e79da7cb0d2..f8d5261595b9 100644
@@ -43,6 +43,10 @@ static int vmpck_id = -1;
 module_param(vmpck_id, int, 0444);
 MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP.");
 
+static bool tsm_enable = true;
+module_param(tsm_enable, bool, 0644);
+MODULE_PARM_DESC(tsm_enable, "Enable SEV TIO");
+
 static inline struct snp_guest_dev *to_snp_dev(struct file *file)
 {
 	struct miscdevice *dev = file->private_data;
@@ -635,6 +639,10 @@ static int __init sev_guest_probe(struct platform_device *pdev)
 	snp_dev->msg_desc = mdesc;
 	dev_info(dev, "Initialized SEV guest driver (using VMPCK%d communication key)\n",
 		 mdesc->vmpck_id);
+
+	if (tsm_enable)
+		sev_guest_tsm_set_ops(true, snp_dev);
+
 	return 0;
 
 e_msg_init:
@@ -648,6 +656,8 @@ static void __exit sev_guest_remove(struct platform_device *pdev)
 	struct snp_guest_dev *snp_dev = platform_get_drvdata(pdev);
 
 	snp_msg_free(snp_dev->msg_desc);
+	if (tsm_enable)
+		sev_guest_tsm_set_ops(false, snp_dev);
 	misc_deregister(&snp_dev->misc);
 }
 
drivers/virt/coco/sev-guest/sev_guest_tio.c new file +738 -0

738 lines not on this page · raw message

Signature · 1 line
2.47.1
author maintainer
#87 lore raw

RFC PATCH v2 19/22 RFC: pci: Add BUS_NOTIFY_PCI_BUS_MASTER event

In reply to Alexey Kardashevskiy

TDISP allows secure MMIO access to a validated MMIO range. The validation is done in the TSM and after that point changing the device's Memory Space enable (MSE) or Bus Master enable (BME) transitions the device into the error state.

For PCI device drivers which enable MSE, then BME, and then start using the device, enabling BME is a logical point to perform the MMIO range validation in the TSM.

Define new event for a bus. TSM is going to listen to it in the TVM and do the validation for TEE ranges.

This does not switch MMIO to private by default though as this is for the driver to decide (at least, for now).

---
 include/linux/device/bus.h          |  3 ++
 drivers/pci/pci.c                   |  3 ++
 drivers/virt/coco/guest/tsm-guest.c | 35 ++++++++++++++++++++
 3 files changed, 41 insertions(+)
include/linux/device/bus.h +3 -0
index f5a56efd2bd6..12f46ca69239 100644
@@ -278,8 +278,11 @@ enum bus_notifier_event {
 	BUS_NOTIFY_UNBIND_DRIVER,
 	BUS_NOTIFY_UNBOUND_DRIVER,
 	BUS_NOTIFY_DRIVER_NOT_BOUND,
+	BUS_NOTIFY_PCI_BUS_MASTER,
 };
 
+void bus_notify(struct device *dev, enum bus_notifier_event value);
+
 struct kset *bus_get_kset(const struct bus_type *bus);
 struct device *bus_get_dev_root(const struct bus_type *bus);
 
drivers/pci/pci.c +3 -0
index b462bab597f7..3aeaa583cd92 100644
@@ -4294,6 +4294,9 @@ static void __pci_set_master(struct pci_dev *dev, bool enable)
 		pci_write_config_word(dev, PCI_COMMAND, cmd);
 	}
 	dev->is_busmaster = enable;
+
+	if (enable)
+		bus_notify(&dev->dev, BUS_NOTIFY_PCI_BUS_MASTER);
 }
 
 /**
drivers/virt/coco/guest/tsm-guest.c +35 -0
index d3be089308e0..d30e49c154e0 100644
@@ -2,6 +2,7 @@
 
 #include <linux/module.h>
 #include <linux/tsm.h>
+#include <linux/pci.h>
 
 #define DRIVER_VERSION	"0.1"
 #define DRIVER_AUTHOR	"[email protected]"
@@ -241,6 +242,36 @@ static const struct attribute_group tdi_group = {
 	.attrs = tdi_attrs,
 };
 
+/* In case BUS_NOTIFY_PCI_BUS_MASTER is no good, a driver can call pci_dev_tdi_validate() */
+int pci_dev_tdi_validate(struct pci_dev *pdev, bool invalidate)
+{
+	struct tsm_tdi *tdi = tsm_tdi_get(&pdev->dev);
+	int ret;
+
+	if (!tdi)
+		return -EFAULT;
+
+	ret = tsm_tdi_validate(tdi, TDI_VALIDATE_DMA | TDI_VALIDATE_MMIO, invalidate);
+
+	tsm_tdi_put(tdi);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(pci_dev_tdi_validate);
+
+static int tsm_guest_pci_bus_notifier(struct notifier_block *nb, unsigned long action, void *data)
+{
+	switch (action) {
+	case BUS_NOTIFY_UNBOUND_DRIVER:
+		pci_dev_tdi_validate(to_pci_dev(data), true);
+		break;
+	case BUS_NOTIFY_PCI_BUS_MASTER:
+		pci_dev_tdi_validate(to_pci_dev(data), false);
+		break;
+	}
+
+	return NOTIFY_OK;
+}
+
 struct tsm_guest_subsys *tsm_guest_register(struct device *parent,
 					    struct tsm_vm_ops *vmops,
 					    void *private_data)
@@ -258,12 +289,16 @@ struct tsm_guest_subsys *tsm_guest_register(struct device *parent,
 	gsubsys->ops = vmops;
 	gsubsys->private_data = private_data;
 
+	gsubsys->notifier.notifier_call = tsm_guest_pci_bus_notifier;
+	bus_register_notifier(&pci_bus_type, &gsubsys->notifier);
+
 	return gsubsys;
 }
 EXPORT_SYMBOL_GPL(tsm_guest_register);
 
 void tsm_guest_unregister(struct tsm_guest_subsys *gsubsys)
 {
+	bus_unregister_notifier(&pci_bus_type, &gsubsys->notifier);
 	tsm_unregister(&gsubsys->base);
 }
 EXPORT_SYMBOL_GPL(tsm_guest_unregister);
Signature · 1 line
2.47.1
author maintainer
#89 lore raw

RFC PATCH v2 20/22 sev-guest: Stop changing encrypted page state for TDISP devices

In reply to Alexey Kardashevskiy

At the moment DMA is assumes insecure and either private memory is converted into shared for the duration of DMA, or SWIOTLB is used. With secure DMA enabled, neither is required.

Stop enforcing unencrypted DMA and SWIOTLB if the device is marked as TDI enabled.

---
 include/linux/dma-direct.h | 8 ++++++++
 include/linux/swiotlb.h    | 8 ++++++++
 arch/x86/mm/mem_encrypt.c  | 6 ++++++
 3 files changed, 22 insertions(+)
include/linux/dma-direct.h +8 -0
index d7e30d4f7503..3bd533d2e65d 100644
@@ -94,6 +94,14 @@ static inline dma_addr_t phys_to_dma_unencrypted(struct device *dev,
  */
 static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
 {
+#if defined(CONFIG_TSM_GUEST) || defined(CONFIG_TSM_GUEST_MODULE)
+	if (dev->tdi_enabled) {
+		dev_warn_once(dev, "(TIO) Disable SME");
+		if (!dev->tdi_validated)
+			dev_warn(dev, "TDI is not validated, DMA @%llx will fail", paddr);
+		return phys_to_dma_unencrypted(dev, paddr);
+	}
+#endif
 	return __sme_set(phys_to_dma_unencrypted(dev, paddr));
 }
 
include/linux/swiotlb.h +8 -0
index 3dae0f592063..67bea31fa42a 100644
@@ -173,6 +173,14 @@ static inline bool is_swiotlb_force_bounce(struct device *dev)
 {
 	struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
 
+#if defined(CONFIG_TSM_GUEST) || defined(CONFIG_TSM_GUEST_MODULE)
+	if (dev->tdi_enabled) {
+		dev_warn_once(dev, "(TIO) Disable SWIOTLB");
+		if (!dev->tdi_validated)
+			dev_warn(dev, "TDI is not validated");
+		return false;
+	}
+#endif
 	return mem && mem->force_bounce;
 }
 
arch/x86/mm/mem_encrypt.c +6 -0
index 95bae74fdab2..c9c99154bec9 100644
@@ -19,6 +19,12 @@
 /* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */
 bool force_dma_unencrypted(struct device *dev)
 {
+#if defined(CONFIG_TSM_GUEST) || defined(CONFIG_TSM_GUEST_MODULE)
+	if (dev->tdi_enabled) {
+		dev_warn_once(dev, "(TIO) Disable decryption");
+		return false;
+	}
+#endif
 	/*
 	 * For SEV, all DMA must be to unencrypted addresses.
 	 */
Signature · 1 line
2.47.1
author maintainer
#91 lore raw

RFC PATCH v2 21/22 pci: Allow encrypted MMIO mapping via sysfs

In reply to Alexey Kardashevskiy

Add another resource#d_enc to allow mapping MMIO as an encrypted/private region.

Unlike resourceN_wc, the node is added always as ability to map MMIO as private depends on negotiation with the TSM which happens quite late.

---
 include/linux/pci.h     |  2 +-
 drivers/pci/mmap.c      | 11 +++++++-
 drivers/pci/pci-sysfs.c | 27 +++++++++++++++-----
 drivers/pci/proc.c      |  2 +-
 4 files changed, 32 insertions(+), 10 deletions(-)
include/linux/pci.h +1 -1
index 073f8f3aece8..862f63ef9bf9 100644
@@ -2129,7 +2129,7 @@ pci_alloc_irq_vectors(struct pci_dev *dev, unsigned int min_vecs,
  */
 int pci_mmap_resource_range(struct pci_dev *dev, int bar,
 			    struct vm_area_struct *vma,
-			    enum pci_mmap_state mmap_state, int write_combine);
+			    enum pci_mmap_state mmap_state, int write_combine, int enc);
 
 #ifndef arch_can_pci_mmap_wc
 #define arch_can_pci_mmap_wc()		0
drivers/pci/mmap.c +10 -1
index 8da3347a95c4..4fd522aeb767 100644
@@ -23,7 +23,7 @@ static const struct vm_operations_struct pci_phys_vm_ops = {
 
 int pci_mmap_resource_range(struct pci_dev *pdev, int bar,
 			    struct vm_area_struct *vma,
-			    enum pci_mmap_state mmap_state, int write_combine)
+			    enum pci_mmap_state mmap_state, int write_combine, int enc)
 {
 	unsigned long size;
 	int ret;
@@ -46,6 +46,15 @@ int pci_mmap_resource_range(struct pci_dev *pdev, int bar,
 
 	vma->vm_ops = &pci_phys_vm_ops;
 
+	/*
+	 * Calling remap_pfn_range() directly as io_remap_pfn_range()
+	 * enforces shared mapping.
+	 */
+	if (enc)
+		return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
+				       vma->vm_end - vma->vm_start,
+				       vma->vm_page_prot);
+
 	return io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
 				  vma->vm_end - vma->vm_start,
 				  vma->vm_page_prot);
drivers/pci/pci-sysfs.c +20 -7
index 4136889011c9..7b03daa13879 100644
@@ -1062,7 +1062,7 @@ void pci_remove_legacy_files(struct pci_bus *b)
  * Use the regular PCI mapping routines to map a PCI resource into userspace.
  */
 static int pci_mmap_resource(struct kobject *kobj, const struct bin_attribute *attr,
-			     struct vm_area_struct *vma, int write_combine)
+			     struct vm_area_struct *vma, int write_combine, int enc)
 {
 	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
 	int bar = (unsigned long)attr->private;
@@ -1082,21 +1082,28 @@ static int pci_mmap_resource(struct kobject *kobj, const struct bin_attribute *a
 
 	mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
 
-	return pci_mmap_resource_range(pdev, bar, vma, mmap_type, write_combine);
+	return pci_mmap_resource_range(pdev, bar, vma, mmap_type, write_combine, enc);
 }
 
 static int pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
 				const struct bin_attribute *attr,
 				struct vm_area_struct *vma)
 {
-	return pci_mmap_resource(kobj, attr, vma, 0);
+	return pci_mmap_resource(kobj, attr, vma, 0, 0);
 }
 
 static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
 				const struct bin_attribute *attr,
 				struct vm_area_struct *vma)
 {
-	return pci_mmap_resource(kobj, attr, vma, 1);
+	return pci_mmap_resource(kobj, attr, vma, 1, 0);
+}
+
+static int pci_mmap_resource_enc(struct file *filp, struct kobject *kobj,
+				 const struct bin_attribute *attr,
+				 struct vm_area_struct *vma)
+{
+	return pci_mmap_resource(kobj, attr, vma, 0, 1);
 }
 
 static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,
@@ -1190,7 +1197,7 @@ static void pci_remove_resource_files(struct pci_dev *pdev)
 	}
 }
 
-static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
+static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine, int enc)
 {
 	/* allocate attribute structure, piggyback attribute name */
 	int name_len = write_combine ? 13 : 10;
@@ -1208,6 +1215,9 @@ static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
 	if (write_combine) {
 		sprintf(res_attr_name, "resource%d_wc", num);
 		res_attr->mmap = pci_mmap_resource_wc;
+	} else if (enc) {
+		sprintf(res_attr_name, "resource%d_enc", num);
+		res_attr->mmap = pci_mmap_resource_enc;
 	} else {
 		sprintf(res_attr_name, "resource%d", num);
 		if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
@@ -1264,11 +1274,14 @@ static int pci_create_resource_files(struct pci_dev *pdev)
 		if (!pci_resource_len(pdev, i))
 			continue;
 
-		retval = pci_create_attr(pdev, i, 0);
+		retval = pci_create_attr(pdev, i, 0, 0);
 		/* for prefetchable resources, create a WC mappable file */
 		if (!retval && arch_can_pci_mmap_wc() &&
 		    pdev->resource[i].flags & IORESOURCE_PREFETCH)
-			retval = pci_create_attr(pdev, i, 1);
+			retval = pci_create_attr(pdev, i, 1, 0);
+		/* Add node for private MMIO mapping */
+		if (!retval)
+			retval = pci_create_attr(pdev, i, 0, 1);
 		if (retval) {
 			pci_remove_resource_files(pdev);
 			return retval;
drivers/pci/proc.c +1 -1
index f967709082d6..62992c8234f1 100644
@@ -284,7 +284,7 @@ static int proc_bus_pci_mmap(struct file *file, struct vm_area_struct *vma)
 	/* Adjust vm_pgoff to be the offset within the resource */
 	vma->vm_pgoff -= start >> PAGE_SHIFT;
 	ret = pci_mmap_resource_range(dev, i, vma,
-				  fpriv->mmap_state, write_combine);
+				  fpriv->mmap_state, write_combine, 0);
 	if (ret < 0)
 		return ret;
 
Signature · 1 line
2.47.1
author maintainer
#93 lore raw

RFC PATCH v2 22/22 pci: Define pci_iomap_range_encrypted

In reply to Alexey Kardashevskiy

So far PCI BARs could not be mapped as encrypted so there was no need in API supporting encrypted mappings. TDISP is adding such support so add pci_iomap_range_encrypted() to allow PCI drivers do the encrypted mapping when needed.

---
 include/asm-generic/pci_iomap.h |  4 ++++
 drivers/pci/iomap.c             | 24 ++++++++++++++++++++
 2 files changed, 28 insertions(+)
include/asm-generic/pci_iomap.h +4 -0
index 8fbb0a55545d..d7b922c5ab86 100644
@@ -15,6 +15,10 @@ extern void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long ma
 extern void __iomem *pci_iomap_range(struct pci_dev *dev, int bar,
 				     unsigned long offset,
 				     unsigned long maxlen);
+extern void __iomem *pci_iomap_range_encrypted(struct pci_dev *dev,
+					       int bar,
+					       unsigned long offset,
+					       unsigned long maxlen);
 extern void __iomem *pci_iomap_wc_range(struct pci_dev *dev, int bar,
 					unsigned long offset,
 					unsigned long maxlen);
drivers/pci/iomap.c +24 -0
index 9fb7cacc15cd..97bada477336 100644
@@ -52,6 +52,30 @@ void __iomem *pci_iomap_range(struct pci_dev *dev,
 }
 EXPORT_SYMBOL(pci_iomap_range);
 
+void __iomem *pci_iomap_range_encrypted(struct pci_dev *dev,
+					int bar,
+					unsigned long offset,
+					unsigned long maxlen)
+{
+	resource_size_t start = pci_resource_start(dev, bar);
+	resource_size_t len = pci_resource_len(dev, bar);
+	unsigned long flags = pci_resource_flags(dev, bar);
+
+	if (len <= offset || !start)
+		return NULL;
+	len -= offset;
+	start += offset;
+	if (maxlen && len > maxlen)
+		len = maxlen;
+	if (flags & IORESOURCE_IO)
+		return NULL;
+	if ((flags & IORESOURCE_MEM) && (flags & IORESOURCE_VALIDATED))
+		return ioremap_encrypted(start, len);
+	/* What? */
+	return NULL;
+}
+EXPORT_SYMBOL(pci_iomap_range_encrypted);
+
 /**
  * pci_iomap_wc_range - create a virtual WC mapping cookie for a PCI BAR
  * @dev: PCI device that owns the BAR
Signature · 1 line
2.47.1
#28 lore raw

Re: [RFC PATCH v2 12/22] iommufd: Allow mapping from guest_memfd

In reply to Alexey Kardashevskiy
Quoted 11 lines · Alexey Kardashevskiy

CoCo VMs get their private memory allocated from guest_memfd ("gmemfd") which is a KVM facility similar to memfd. At the moment gmemfds cannot mmap() so the usual GUP API does not work on these as expected.

Use the existing IOMMU_IOAS_MAP_FILE API to allow mapping from fd + offset. Detect the gmemfd case in pfn_reader_user_pin() and simplified mapping.

The long term plan is to ditch this workaround and follow the usual memfd path.

How is that possible though?

Quoted 22 lines
drivers/iommu/iommufd/pages.c +22 -0
+static struct folio *guest_memfd_get_pfn(struct file *file, unsigned long index,
+					 unsigned long *pfn, int *max_order)
+{
+	struct folio *folio;
+	int ret = 0;
+
+	folio = filemap_grab_folio(file_inode(file)->i_mapping, index);
+
+	if (IS_ERR(folio))
+		return folio;
+
+	if (folio_test_hwpoison(folio)) {
+		folio_unlock(folio);
+		folio_put(folio);
+		return ERR_PTR(-EHWPOISON);
+	}
+
+	*pfn = folio_pfn(folio) + (index & (folio_nr_pages(folio) - 1));
+	if (!max_order)
+		goto unlock_exit;
+
+	/* Refs for unpin_user_page_range_dirty_lock->gup_put_folio(FOLL_PIN) */

drivers/iommu/iommufd/pages.c static long pin_memfd_pages(struct pfn_reader_user *user, unsigned long start, · line 892

Quoted 10 lines
drivers/iommu/iommufd/pages.c +10 -0
+	ret = folio_add_pins(folio, 1);
+	folio_put(folio); /* Drop ref from filemap_grab_folio */
+
+unlock_exit:
+	folio_unlock(folio);
+	if (ret)
+		folio = ERR_PTR(ret);
+
+	return folio;
+}

Connecting iommufd to guestmemfd through the FD is broadly the right idea, but I'm not sure this matches the design of guestmemfd regarding pinnability. IIRC they were adamant that the pages would not be pinned..

folio_add_pins() just prevents the folio from being freed, it doesn't prevent the guestmemfd code from messing with the filemap.

You should separate this from the rest of the series and discuss it directly with the guestmemfd maintainers.

As I understood it the requirement here is to have some kind of invalidation callback so that iommufd can drop mappings, but I don't really know and AFAIK AMD is special in wanting private pages mapped to the hypervisor iommu..

Jason

author maintainer
#29 lore raw

Re: [RFC PATCH v2 12/22] iommufd: Allow mapping from guest_memfd

Quoted 6 lines · Jason Gunthorpe
Quoted 5 lines · Alexey Kardashevskiy

CoCo VMs get their private memory allocated from guest_memfd ("gmemfd") which is a KVM facility similar to memfd. At the moment gmemfds cannot mmap() so the usual GUP API does not work on these as expected.

Quoted 8 lines
Quoted 6 lines

Use the existing IOMMU_IOAS_MAP_FILE API to allow mapping from fd + offset. Detect the gmemfd case in pfn_reader_user_pin() and simplified mapping.

The long term plan is to ditch this workaround and follow the usual memfd path.

How is that possible though?

dunno, things evolve over years and converge somehow :)

Quoted 29 lines
Quoted 29 lines
drivers/iommu/iommufd/pages.c +29 -0
+static struct folio *guest_memfd_get_pfn(struct file *file, unsigned long index,
+					 unsigned long *pfn, int *max_order)
+{
+	struct folio *folio;
+	int ret = 0;
+
+	folio = filemap_grab_folio(file_inode(file)->i_mapping, index);
+
+	if (IS_ERR(folio))
+		return folio;
+
+	if (folio_test_hwpoison(folio)) {
+		folio_unlock(folio);
+		folio_put(folio);
+		return ERR_PTR(-EHWPOISON);
+	}
+
+	*pfn = folio_pfn(folio) + (index & (folio_nr_pages(folio) - 1));
+	if (!max_order)
+		goto unlock_exit;
+
+	/* Refs for unpin_user_page_range_dirty_lock->gup_put_folio(FOLL_PIN) */
+	ret = folio_add_pins(folio, 1);
+	folio_put(folio); /* Drop ref from filemap_grab_folio */
+
+unlock_exit:
+	folio_unlock(folio);
+	if (ret)
+		folio = ERR_PTR(ret);
Quoted 8 lines

drivers/iommu/iommufd/pages.c static long pin_memfd_pages(struct pfn_reader_user *user, unsigned long start, · line 876

Quoted 3 lines
drivers/iommu/iommufd/pages.c +3 -0
+
+	return folio;
+}

Connecting iommufd to guestmemfd through the FD is broadly the right idea, but I'm not sure this matches the design of guestmemfd regarding pinnability. IIRC they were adamant that the pages would not be pinned..

uff I thought it was about "not mapped" rather than "non pinned".

Quoted 5 lines

folio_add_pins() just prevents the folio from being freed, it doesn't prevent the guestmemfd code from messing with the filemap.

You should separate this from the rest of the series and discuss it directly with the guestmemfd maintainers.

Alright, thanks for the suggestion.

Quoted 2 lines

As I understood it the requirement here is to have some kind of invalidation callback so that iommufd can drop mappings,

Since shared<->private conversion is an ioctl() (kvm/gmemfd) so it is ioctl() for iommufd then too. Oh well.

Quoted 3 lines

but I don't
really know and AFAIK AMD is special in wanting private pages mapped to the hypervisor iommu..

With in-place conversion, we could map the entire guest once in the HV IOMMU and control the Cbit via the guest's IOMMU table (when available). Thanks,

Signature · 1 line
Alexey
#30 lore raw

Re: [RFC PATCH v2 12/22] iommufd: Allow mapping from guest_memfd

Quoted 2 lines · Alexey Kardashevskiy

With in-place conversion, we could map the entire guest once in the HV IOMMU and control the Cbit via the guest's IOMMU table (when available). Thanks,

Isn't it more complicated than that? I understood you need to have a IOPTE boundary in the hypervisor at any point where the guest Cbit changes - so you can't just dump 1G hypervisor pages to cover the whole VM, you have to actively resize ioptes?

This was the whole motivation to adding the page size override kernel command line.

Jason

author maintainer
#31 lore raw

Re: [RFC PATCH v2 12/22] iommufd: Allow mapping from guest_memfd

Quoted 9 lines · Jason Gunthorpe
Quoted 2 lines · Alexey Kardashevskiy

With in-place conversion, we could map the entire guest once in the HV IOMMU and control the Cbit via the guest's IOMMU table (when available). Thanks,

Isn't it more complicated than that? I understood you need to have a IOPTE boundary in the hypervisor at any point where the guest Cbit changes - so you can't just dump 1G hypervisor pages to cover the whole VM, you have to actively resize ioptes?

When the guest Cbit changes, only AMD RMP table requires update but not necessaryly NPT or IOPTEs. (I may have misunderstood the question, what meaning does "dump 1G pages" have?).

Quoted 4 lines

This was the whole motivation to adding the page size override kernel command line.

Jason

Signature · 1 line
Alexey
Reviewed-by Vasant Hegde
#32 lore raw

Re: [RFC PATCH v2 12/22] iommufd: Allow mapping from guest_memfd

Quoted 11 lines · Alexey Kardashevskiy
Quoted 9 lines · Jason Gunthorpe
Quoted 2 lines · Alexey Kardashevskiy

With in-place conversion, we could map the entire guest once in the HV IOMMU and control the Cbit via the guest's IOMMU table (when available). Thanks,

Isn't it more complicated than that? I understood you need to have a IOPTE boundary in the hypervisor at any point where the guest Cbit changes - so you can't just dump 1G hypervisor pages to cover the whole VM, you have to actively resize ioptes?

Quoted 4 lines

When the guest Cbit changes, only AMD RMP table requires update but not necessaryly NPT or IOPTEs.
(I may have misunderstood the question, what meaning does "dump 1G pages" have?).

AFAIK that is not true, if there are mismatches in page size, ie the RMP is 2M and the IOPTE is 1G then things do not work properly.

It is why we had to do this:

Quoted 2 lines
Quoted 2 lines

This was the whole motivation to adding the page size override kernel command line.

commit f0295913c4b4f377c454e06f50c1a04f2f80d9df

Author: Joerg Roedel <[email protected]>
Date:   Thu Sep 5 09:22:40 2024 +0200

iommu/amd: Add kernel parameters to limit V1 page-sizes

Add two new kernel command line parameters to limit the page-sizes used for v1 page-tables:

nohugepages - Limits page-sizes to 4KiB

v2_pgsizes_only - Limits page-sizes to 4Kib/2Mib/1GiB; The
same as the sizes used with v2 page-tables

This is needed for multiple scenarios. When assigning devices to SEV-SNP guests the IOMMU page-sizes need to match the sizes in the RMP table, otherwise the device will not be able to access all shared memory.

Also, some ATS devices do not work properly with arbitrary IO page-sizes as supported by AMD-Vi, so limiting the sizes used by the driver is a suitable workaround.

All-in-all, these parameters are only workarounds until the IOMMU core and related APIs gather the ability to negotiate the page-sizes in a better way.

Jason

#33 lore raw

Re: [RFC PATCH v2 12/22] iommufd: Allow mapping from guest_memfd

Quoted 12 lines · Jason Gunthorpe
Quoted 11 lines · Alexey Kardashevskiy
Quoted 9 lines · Jason Gunthorpe
Quoted 2 lines · Alexey Kardashevskiy

With in-place conversion, we could map the entire guest once in the HV IOMMU and control the Cbit via the guest's IOMMU table (when available). Thanks,

Isn't it more complicated than that? I understood you need to have a IOPTE boundary in the hypervisor at any point where the guest Cbit changes - so you can't just dump 1G hypervisor pages to cover the whole VM, you have to actively resize ioptes?

Quoted 7 lines
Quoted 4 lines

When the guest Cbit changes, only AMD RMP table requires update but not necessaryly NPT or IOPTEs.
(I may have misunderstood the question, what meaning does "dump 1G pages" have?).

AFAIK that is not true, if there are mismatches in page size, ie the RMP is 2M and the IOPTE is 1G then things do not work properly.

Just for clarity: at least for normal/nested page table (but I'm assuming the same applies to IOMMU mappings), 1G mappings are handled similarly as 2MB mappings as far as RMP table checks are
concerned: each 2MB range is checked individually as if it were a separate 2MB mapping:

AMD Architecture Programmer's Manual Volume 2, 15.36.10, "RMP and VMPL Access Checks":

"Accesses to 1GB pages only install 2MB TLB entries when SEV-SNP is enabled, therefore this check treats 1GB accesses as 2MB accesses for purposes of this check."

So a 1GB mapping doesn't really impose more restrictions than a 2MB mapping (unless there's something different about how RMP checks are done for IOMMU).

But the point still stands for 4K RMP entries and 2MB mappings: a 2MB mapping either requires private page RMP entries to be 2MB, or in the case of 2MB mapping of shared pages, every page in the range must be shared according to the corresponding RMP entries.

Quoted 2 lines

It is why we had to do this:

I think, for the non-SEV-TIO use-case, it had more to do with inability to unmap a 4K range once a particular 4K page has been converted from shared to private if it was originally installed via a 2MB IOPTE, since the guest could actively be DMA'ing to other shared pages in the 2M range (but we can be assured it is not DMA'ing to a particular 4K page it has converted to private), and the IOMMU doesn't (AFAIK) have a way to atomically split an existing 2MB IOPTE to avoid this. So forcing everything to 4K ends up being necessary since we don't know in advance what ranges might contain 4K pages that will get converted to private in the future by the guest.

SEV-TIO might relax this restriction by making use of TMPM and the PSMASH_IO command to split/"smash" RMP entries and IOMMU mappings to 4K after-the-fact, but I'm not too familiar with the architecture/plans so Alexey can correct me on that.

-Mike

Quoted 36 lines
Quoted 3 lines
Quoted 3 lines

This was the whole motivation to adding the page size override kernel command line.

commit f0295913c4b4f377c454e06f50c1a04f2f80d9df

Author: Joerg Roedel <[email protected]>
Date:   Thu Sep 5 09:22:40 2024 +0200

iommu/amd: Add kernel parameters to limit V1 page-sizes

Add two new kernel command line parameters to limit the page-sizes used for v1 page-tables:

nohugepages - Limits page-sizes to 4KiB

v2_pgsizes_only - Limits page-sizes to 4Kib/2Mib/1GiB; The
same as the sizes used with v2 page-tables

This is needed for multiple scenarios. When assigning devices to SEV-SNP guests the IOMMU page-sizes need to match the sizes in the RMP table, otherwise the device will not be able to access all shared memory.

Also, some ATS devices do not work properly with arbitrary IO page-sizes as supported by AMD-Vi, so limiting the sizes used by the driver is a suitable workaround.

All-in-all, these parameters are only workarounds until the IOMMU core and related APIs gather the ability to negotiate the page-sizes in a better way.

Jason

#34 lore raw

Re: [RFC PATCH v2 12/22] iommufd: Allow mapping from guest_memfd

Quoted 5 lines · Michael Roth

Just for clarity: at least for normal/nested page table (but I'm assuming the same applies to IOMMU mappings), 1G mappings are handled similarly as 2MB mappings as far as RMP table checks are
concerned: each 2MB range is checked individually as if it were a separate 2MB mapping:

Well, IIRC we are dealing with the AMDv1 IO page table here which supports more sizes than 1G and we likely start to see things like 4M mappings and the like. So maybe there is some issue if the above special case really only applies to 1G and only 1G.

Quoted 4 lines

But the point still stands for 4K RMP entries and 2MB mappings: a 2MB mapping either requires private page RMP entries to be 2MB, or in the case of 2MB mapping of shared pages, every page in the range must be shared according to the corresponding RMP entries.

Is 4k RMP what people are running?

Quoted 2 lines

I think, for the non-SEV-TIO use-case, it had more to do with inability to unmap a 4K range once a particular 4K page has been converted

Yes, we don't support unmap or resize. The entire theory of operation has the IOPTEs cover the guest memory and remain static at VM boot time. The RMP alone controls access and handles the static/private.

Assuming the host used 2M pages the IOPTEs in an AMDv1 table will be sized around 2M,4M,8M just based around random luck.

So it sounds like you can get to a situation with a >=2M mapping in the IOPTE but the guest has split it into private/shared at lower granularity and the HW cannot handle this?

Quoted 5 lines

from shared to private if it was originally installed via a 2MB IOPTE, since the guest could actively be DMA'ing to other shared pages in the 2M range (but we can be assured it is not DMA'ing to a particular 4K page it has converted to private), and the IOMMU doesn't (AFAIK) have a way to atomically split an existing 2MB IOPTE to avoid this.

The iommu can split it (with SW help), I'm working on that infrastructure right now..

So you will get a notification that the guest has made a private/public split and the iommu page table can be atomically restructured to put an IOPTE boundary at the split.

Then the HW will not see IOPTEs that exceed the shared/private granularity of the VM.

Jason

#35 lore raw

Re: [RFC PATCH v2 12/22] iommufd: Allow mapping from guest_memfd

Quoted 11 lines · Jason Gunthorpe
Quoted 5 lines · Michael Roth

Just for clarity: at least for normal/nested page table (but I'm assuming the same applies to IOMMU mappings), 1G mappings are handled similarly as 2MB mappings as far as RMP table checks are
concerned: each 2MB range is checked individually as if it were a separate 2MB mapping:

Well, IIRC we are dealing with the AMDv1 IO page table here which supports more sizes than 1G and we likely start to see things like 4M mappings and the like. So maybe there is some issue if the above special case really only applies to 1G and only 1G.

I think the documentation only mentioned 1G specifically since that's the next level up in host/nested page table mappings, and that more generally anything mapping at a higher granularity than 2MB would be broken down into individual checks on each 2MB range within. But it's quite possible things are handled differently for IOMMU so definitely worth confirming.

Quoted 7 lines
Quoted 5 lines

But the point still stands for 4K RMP entries and 2MB mappings: a 2MB mapping either requires private page RMP entries to be 2MB, or in the case of 2MB mapping of shared pages, every page in the range must be shared according to the corresponding RMP entries.

Is 4k RMP what people are running?

Unfortunately yes, but that's mainly due to guest_memfd only handling 4K currently. Hopefully that will change soon, but in the meantime there's only experimental support for larger private page sizes that make use of 2MB RMP entries (via THP).

But regardless, we'll still end up dealing with 4K RMP entries since we'll need to split 2MB RMP entries in response to private->conversions that aren't 2MB aligned/sized.

Quoted 8 lines
Quoted 3 lines

I think, for the non-SEV-TIO use-case, it had more to do with inability to unmap a 4K range once a particular 4K page has been converted

Yes, we don't support unmap or resize. The entire theory of operation has the IOPTEs cover the guest memory and remain static at VM boot time. The RMP alone controls access and handles the static/private.

Quoted 6 lines

Assuming the host used 2M pages the IOPTEs in an AMDv1 table will be sized around 2M,4M,8M just based around random luck.

So it sounds like you can get to a situation with a >=2M mapping in the IOPTE but the guest has split it into private/shared at lower granularity and the HW cannot handle this?

Remembering more details: the situation is a bit more specific to guest_memfd. In general, for non-SEV-TIO, everything in the IOMMU will be always be for shared pages, and because of that the RMP checks don't impose any additional restrictions on mapping size (a shared page can be mapped 2MB even if the RMP entry is 4K (the RMP page-size bit only really applies for private pages)).

The issue with guest_memfd is that it is only used for private pages (at least until in-place conversion is supported), so when we "convert" shared pages to private we are essentially discarding those pages and re-allocating them via guest_memfd, so the mappings for those discarded pages become stale and need to be removed. But since this can happen at 4K granularities, we need to map as 4K because we don't have a way to split them later on (at least, not currently...).

The other approach is to not discard these shared pages after conversion and just not free them back, which ends up using more host memory, but allows for larger IOMMU mappings.

Quoted 10 lines
Quoted 6 lines

from shared to private if it was originally installed via a 2MB IOPTE, since the guest could actively be DMA'ing to other shared pages in the 2M range (but we can be assured it is not DMA'ing to a particular 4K page it has converted to private), and the IOMMU doesn't (AFAIK) have a way to atomically split an existing 2MB IOPTE to avoid this.

The iommu can split it (with SW help), I'm working on that infrastructure right now..

Quoted 6 lines

So you will get a notification that the guest has made a private/public split and the iommu page table can be atomically restructured to put an IOPTE boundary at the split.

Then the HW will not see IOPTEs that exceed the shared/private granularity of the VM.

That sounds very interesting. It would allow us to use larger IOMMU mappings even for guest_memfd as it exists today, while still supporting shared memory discard and avoiding the additional host memory usage mentioned above. Are there patches available publicly?

Thanks,

Mike

Quoted 2 lines

Jason

#36 lore raw

Re: [RFC PATCH v2 12/22] iommufd: Allow mapping from guest_memfd

Quoted 6 lines · Michael Roth

I think the documentation only mentioned 1G specifically since that's the next level up in host/nested page table mappings, and that more generally anything mapping at a higher granularity than 2MB would be broken down into individual checks on each 2MB range within. But it's quite possible things are handled differently for IOMMU so definitely worth confirming.

Hmm, well, I'd very much like it if we are all on the same page as to why the new kernel parameters were needed. Joerg was definitely seeing testing failures without them.

IMHO we should not require parameters like that, I expect the kernel to fix this stuff on its own.

Quoted 3 lines

But regardless, we'll still end up dealing with 4K RMP entries since we'll need to split 2MB RMP entries in response to private->conversions that aren't 2MB aligned/sized.

:( What is the point of even allowing < 2MP private/shared conversion?

Quoted 7 lines
Quoted 2 lines

Then the HW will not see IOPTEs that exceed the shared/private granularity of the VM.

That sounds very interesting. It would allow us to use larger IOMMU mappings even for guest_memfd as it exists today, while still supporting shared memory discard and avoiding the additional host memory usage mentioned above. Are there patches available publicly?

https://patch.msgid.link/r/[email protected]

I'm getting quite close to having something non-RFC that just does AMD and the bare minimum. I will add you two to the CC

Jason

author maintainer
#39 lore raw

Re: [RFC PATCH v2 12/22] iommufd: Allow mapping from guest_memfd

In reply to Jason Gunthorpe
Quoted 12 lines · Jason Gunthorpe
Quoted 11 lines · Alexey Kardashevskiy
Quoted 9 lines · Jason Gunthorpe
Quoted 2 lines · Alexey Kardashevskiy

With in-place conversion, we could map the entire guest once in the HV IOMMU and control the Cbit via the guest's IOMMU table (when available). Thanks,

Isn't it more complicated than that? I understood you need to have a IOPTE boundary in the hypervisor at any point where the guest Cbit changes - so you can't just dump 1G hypervisor pages to cover the whole VM, you have to actively resize ioptes?

Quoted 7 lines
Quoted 4 lines

When the guest Cbit changes, only AMD RMP table requires update but not necessaryly NPT or IOPTEs.
(I may have misunderstood the question, what meaning does "dump 1G pages" have?).

AFAIK that is not true, if there are mismatches in page size, ie the RMP is 2M and the IOPTE is 1G then things do not work properly.

Right, so I misunderstood. When I first replied, I assumed the current situation of 4K pages everywhere. IOPTEs larger than RMP entries are likely to cause failed RMP checks (confirming now, surprises sometime happen). Thanks,

Quoted 37 lines

It is why we had to do this:

Quoted 2 lines
Quoted 2 lines

This was the whole motivation to adding the page size override kernel command line.

commit f0295913c4b4f377c454e06f50c1a04f2f80d9df

Author: Joerg Roedel <[email protected]>
Date:   Thu Sep 5 09:22:40 2024 +0200

iommu/amd: Add kernel parameters to limit V1 page-sizes

Add two new kernel command line parameters to limit the page-sizes used for v1 page-tables:

nohugepages - Limits page-sizes to 4KiB

v2_pgsizes_only - Limits page-sizes to 4Kib/2Mib/1GiB; The
same as the sizes used with v2 page-tables

This is needed for multiple scenarios. When assigning devices to SEV-SNP guests the IOMMU page-sizes need to match the sizes in the RMP table, otherwise the device will not be able to access all shared memory.

Also, some ATS devices do not work properly with arbitrary IO page-sizes as supported by AMD-Vi, so limiting the sizes used by the driver is a suitable workaround.

All-in-all, these parameters are only workarounds until the IOMMU core and related APIs gather the ability to negotiate the page-sizes in a better way.

Jason

Signature · 1 line
Alexey
#47 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

In reply to Alexey Kardashevskiy
Quoted 11 lines · Alexey Kardashevskiy

When a TDISP-capable device is passed through, it is configured as a shared device to begin with. Later on when a VM probes the device, detects its TDISP capability (reported via the PCIe ExtCap bit called "TEE-IO"), performs the device attestation and transitions it to a secure state when the device can run encrypted DMA and respond to encrypted MMIO accesses.

Since KVM is out of the TCB, secure enablement is done in the secure firmware. The API requires PCI host/guest BDFns, a KVM id hence such calls are routed via IOMMUFD, primarily because allowing secure DMA is the major performance bottleneck and it is a function of IOMMU.

I still have concern about the vdevice interface for bind. Bind put the device to LOCKED state, so is more of a device configuration rather than an iommu configuration. So seems more reasonable put the API in VFIO?

Quoted 8 lines

Add TDI bind to do the initial binding of a passed through PCI function to a VM. Add a forwarder for TIO GUEST REQUEST. These two call into the TSM which forwards the calls to the PSP.

---

Quoted 8 lines

Both enabling secure DMA (== "SDTE Write") and secure MMIO (== "MMIO validate") are TIO GUEST REQUEST messages. These are encrypted and the HV (==IOMMUFD or KVM or VFIO) cannot see them unless the guest shares some via kvm_run::kvm_user_vmgexit (and then QEMU passes those via ioctls).

This RFC routes all TIO GUEST REQUESTs via IOMMUFD which arguably should only do so only for "SDTE Write" and leave "MMIO validate" for VFIO.

The fact is HV cannot see the guest requests, even I think HV never have to care about the guest requests. HV cares until bind, then no HV side MMIO & DMA access is possible, any operation/state after bind won't affect HV more. And HV could always unbind to rollback guest side thing.

That said guest requests are nothing to do with any host side component, iommu or vfio. It is just the message posting between VM & firmware. I suppose KVM could directly do it by calling TSM driver API.

Thanks,
Yilun

author maintainer
#48 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

Quoted 8 lines · Xu Yilun
Quoted 7 lines · Alexey Kardashevskiy

When a TDISP-capable device is passed through, it is configured as a shared device to begin with. Later on when a VM probes the device, detects its TDISP capability (reported via the PCIe ExtCap bit called "TEE-IO"), performs the device attestation and transitions it to a secure state when the device can run encrypted DMA and respond to encrypted MMIO accesses.

Quoted 8 lines
Quoted 4 lines

Since KVM is out of the TCB, secure enablement is done in the secure firmware. The API requires PCI host/guest BDFns, a KVM id hence such calls are routed via IOMMUFD, primarily because allowing secure DMA is the major performance bottleneck and it is a function of IOMMU.

I still have concern about the vdevice interface for bind. Bind put the device to LOCKED state, so is more of a device configuration rather than an iommu configuration. So seems more reasonable put the API in VFIO?

IOMMUFD means pretty much VFIO (in the same way "VFIO means KVM" as 95+% of VFIO users use it from KVM, although VFIO works fine without KVM) so not much difference where to put this API and can be done either way. VFIO is reasonable, the immediate problem is that IOMMUFD's vIOMMU knows the guest BDFn (well, for AMD) and VFIO PCI does not.

Quoted 16 lines
Quoted 7 lines

Add TDI bind to do the initial binding of a passed through PCI function to a VM. Add a forwarder for TIO GUEST REQUEST. These two call into the TSM which forwards the calls to the PSP.

---

Quoted 8 lines

Both enabling secure DMA (== "SDTE Write") and secure MMIO (== "MMIO validate") are TIO GUEST REQUEST messages. These are encrypted and the HV (==IOMMUFD or KVM or VFIO) cannot see them unless the guest shares some via kvm_run::kvm_user_vmgexit (and then QEMU passes those via ioctls).

This RFC routes all TIO GUEST REQUESTs via IOMMUFD which arguably should only do so only for "SDTE Write" and leave "MMIO validate" for VFIO.

Quoted 8 lines

The fact is HV cannot see the guest requests, even I think HV never have to care about the guest requests. HV cares until bind, then no HV side MMIO & DMA access is possible, any operation/state after bind won't affect HV more. And HV could always unbind to rollback guest side thing.

That said guest requests are nothing to do with any host side component, iommu or vfio. It is just the message posting between VM & firmware. I suppose KVM could directly do it by calling TSM driver API.

No, it could not as the HV needs to add the host BDFn to the guest's request before calling the firmware and KVM does not have that knowledge.

These guest requests are only partly encrypted as the guest needs cooperation from the HV. The guest BDFn comes unencrypted from the VM to let the HV find the host BDFn and do the bind.

Also, say, in order to enable MMIO range, the host needs to "rmpupdate" MMIOs first (and then the firmware does "pvalidate") so it needs to know the range which is in unencrypted part of guest request.

Here is a rough idea: https://github.com/aik/qemu/commit/f804b65aff5b

A TIO Guest request is made of:
- guest page with unencrypted header (msg type is essential) and encrypted body for consumption by the firmware;
- a couple of 64bit bit fields and RAX/RBX/... in shared GHCB page.

Thanks,

Quoted 2 lines

Thanks,
Yilun

Signature · 1 line
Alexey
#49 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

Quoted 16 lines · Alexey Kardashevskiy
Quoted 13 lines · Xu Yilun
Quoted 11 lines · Alexey Kardashevskiy

When a TDISP-capable device is passed through, it is configured as a shared device to begin with. Later on when a VM probes the device, detects its TDISP capability (reported via the PCIe ExtCap bit called "TEE-IO"), performs the device attestation and transitions it to a secure state when the device can run encrypted DMA and respond to encrypted MMIO accesses.

Since KVM is out of the TCB, secure enablement is done in the secure firmware. The API requires PCI host/guest BDFns, a KVM id hence such calls are routed via IOMMUFD, primarily because allowing secure DMA is the major performance bottleneck and it is a function of IOMMU.

Quoted 7 lines
Quoted 3 lines

I still have concern about the vdevice interface for bind. Bind put the device to LOCKED state, so is more of a device configuration rather than an iommu configuration. So seems more reasonable put the API in VFIO?

IOMMUFD means pretty much VFIO (in the same way "VFIO means KVM" as 95+% of VFIO users use it from KVM, although VFIO works fine without KVM) so not much difference where to put this API and can be done either way. VFIO is

Er... I cannot agree. There are clear responsibilities for VFIO/IOMMUFD/KVM each. They don't overlap each other. So I don't think either way is OK. VFIO still controls the overall device behavior and it is VFIO's decision to hand over user DMA setup to IOMMUFD. IIUC that's why VFIO_DEVICE_ATTACH_IOMMUFD_PT should be a VFIO API.

E.g. I don't think VFIO driver would expect its MMIO access suddenly failed without knowing what happened.

Quoted 2 lines

reasonable, the immediate problem is that IOMMUFD's vIOMMU knows the guest BDFn (well, for AMD) and VFIO PCI does not.

For Intel, it is host BDF. But I think this is TSM architecture difference that could be hidden in TSM framework. From TSM caller's POV, it could just be a magic number identifying the TDI.

Back to your concern, I don't think it is a problem. From your patch, vIOMMU doesn't know the guest BDFn by nature, it is just the user stores the id in vdevice via iommufd_vdevice_alloc_ioctl(). A proper VFIO API could also do this work.

I'm suggesting a VFIO API:

/*
* @tdi_id: A TSM recognizable TDI identifier
* On input, user suggests the TDI identifier number for TSM.
* On output, TSM's decision of the TDI identifier number.
*/

struct vfio_pci_tsm_bind {
	__u32 argsz;
	__u32 flags;
	__u32 tdi_id;
	__u32 pad;
};

#define VFIO_DEVICE_TSM_BIND _IO(VFIO_TYPE, VFIO_BASE + 22)

I need the tdi_id as output cause I don't want any outside TSM user and Guest to assume what the TDI id should be.

static int vfio_pci_ioctl_tsm_bind(struct vfio_pci_core_device *vdev,
void __user *arg)

{
	unsigned long minsz = offsetofend(struct vfio_pci_tsm_bind, tdi_id);
	struct pci_dev *pdev = vdev->pdev;
	struct kvm *kvm = vdev->vdev.kvm;
	struct vfio_pci_tsm_bind bind;

	if (copy_from_user(&bind, arg, minsz))
		return -EFAULT;

	ret = pci_tsm_dev_bind(pdev, kvm, &bind.tdi_id);

}

A call to TSM makes TSM driver know the tdi_id and could find the real device inside TSM via tdi_id. Following TSM call could directly use tdi_id as parameter.

The implementation is basically no difference from:

+ vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id,
+ IOMMUFD_OBJ_VDEVICE),

The real concern is the device owner, VFIO, should initiate the bind.

Quoted 23 lines
Quoted 23 lines
Quoted 9 lines

Add TDI bind to do the initial binding of a passed through PCI function to a VM. Add a forwarder for TIO GUEST REQUEST. These two call into the TSM which forwards the calls to the PSP.

---

Quoted 8 lines

Both enabling secure DMA (== "SDTE Write") and secure MMIO (== "MMIO validate") are TIO GUEST REQUEST messages. These are encrypted and the HV (==IOMMUFD or KVM or VFIO) cannot see them unless the guest shares some via kvm_run::kvm_user_vmgexit (and then QEMU passes those via ioctls).

This RFC routes all TIO GUEST REQUESTs via IOMMUFD which arguably should only do so only for "SDTE Write" and leave "MMIO validate" for VFIO.

The fact is HV cannot see the guest requests, even I think HV never have to care about the guest requests. HV cares until bind, then no HV side MMIO & DMA access is possible, any operation/state after bind won't affect HV more. And HV could always unbind to rollback guest side thing.

Quoted 6 lines
Quoted 3 lines

That said guest requests are nothing to do with any host side component, iommu or vfio. It is just the message posting between VM & firmware. I suppose KVM could directly do it by calling TSM driver API.

No, it could not as the HV needs to add the host BDFn to the guest's request before calling the firmware and KVM does not have that knowledge.

I think if TSM has knowledge about tdi_id, KVM doesn't have to know host BDFn. Just let TSM handle the vendor difference. Not sure if this solves all the problem.

Quoted 4 lines

These guest requests are only partly encrypted as the guest needs cooperation from the HV. The guest BDFn comes unencrypted from the VM to let the HV find the host BDFn and do the bind.

It is not about HV never touch any message content. It is about HV doesn't (and shouldn't, since some info is encrypted) influence any host behavior by executing guest request, so no need to route to any other component.

Thanks,
Yilun

Quoted 20 lines

Also, say, in order to enable MMIO range, the host needs to "rmpupdate" MMIOs first (and then the firmware does "pvalidate") so it needs to know the range which is in unencrypted part of guest request.

Here is a rough idea: https://github.com/aik/qemu/commit/f804b65aff5b

A TIO Guest request is made of:
- guest page with unencrypted header (msg type is essential) and encrypted body for consumption by the firmware;
- a couple of 64bit bit fields and RAX/RBX/... in shared GHCB page.

Thanks,

Quoted 2 lines

Thanks,
Yilun

Signature · 1 line
Alexey
#66 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

In reply to Alexey Kardashevskiy
Quoted 9 lines · Alexey Kardashevskiy
Quoted 3 lines

I still have concern about the vdevice interface for bind. Bind put the device to LOCKED state, so is more of a device configuration rather than an iommu configuration. So seems more reasonable put the API in VFIO?

IOMMUFD means pretty much VFIO (in the same way "VFIO means KVM" as 95+% of VFIO users use it from KVM, although VFIO works fine without KVM) so not much difference where to put this API and can be done either way. VFIO is reasonable, the immediate problem is that IOMMUFD's vIOMMU knows the guest BDFn (well, for AMD) and VFIO PCI does not.

I would re-enforce what I said before, VFIO & iommufd alone should be able to operate a TDISP device and get device encrpytion without requiring KVM.

It makes sense that if the secure firmware object handles (like the viommu, vdevice, vBDF) are accessed through iommufd then iommufd will relay operations against those handles.

Jason

#50 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

In reply to Xu Yilun
Quoted 2 lines · Xu Yilun

E.g. I don't think VFIO driver would expect its MMIO access suddenly failed without knowing what happened.

What do people expect to happen here anyhow? Do you still intend to mmap any of the MMIO into the hypervisor? No, right? It is all locked down?

So perhaps the answer is that the VFIO side has to put the device into CC mode which disables MMAP/etc, then the viommu/vdevice iommufd object can control it.

Quoted 4 lines

Back to your concern, I don't think it is a problem. From your patch, vIOMMU doesn't know the guest BDFn by nature, it is just the user stores the id in vdevice via iommufd_vdevice_alloc_ioctl(). A proper VFIO API could also do this work.

We don't want duplication though. If the viommu/vdevice/vbdf are owned and lifecycle controlled by iommufd then the operations against them must go through iommufd and through it's locking regime.

Quoted 7 lines

The implementation is basically no difference from:

(quoted diff) +2 -0
+       vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id,
+                                              IOMMUFD_OBJ_VDEVICE),

The real concern is the device owner, VFIO, should initiate the bind.

There is a big different, the above has correct locking, the other does not :)

Jason

author maintainer
#51 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

Quoted 8 lines · Jason Gunthorpe
Quoted 2 lines · Xu Yilun

E.g. I don't think VFIO driver would expect its MMIO access suddenly failed without knowing what happened.

What do people expect to happen here anyhow? Do you still intend to mmap any of the MMIO into the hypervisor? No, right? It is all locked down?

This patchset expects it to be mmap'able as this is how MMIO gets mapped in the NPT and SEV-SNP still works with that (and updates the RMPs on top), the host os is not expected to access these though. TDX will handle this somehow different. Thanks,

Quoted 25 lines

So perhaps the answer is that the VFIO side has to put the device into CC mode which disables MMAP/etc, then the viommu/vdevice iommufd object can control it.

Quoted 4 lines

Back to your concern, I don't think it is a problem. From your patch, vIOMMU doesn't know the guest BDFn by nature, it is just the user stores the id in vdevice via iommufd_vdevice_alloc_ioctl(). A proper VFIO API could also do this work.

We don't want duplication though. If the viommu/vdevice/vbdf are owned and lifecycle controlled by iommufd then the operations against them must go through iommufd and through it's locking regime.

Quoted 7 lines

The implementation is basically no difference from:

(quoted diff) +2 -0
+       vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id,
+                                              IOMMUFD_OBJ_VDEVICE),

The real concern is the device owner, VFIO, should initiate the bind.

There is a big different, the above has correct locking, the other does not :)

Jason

Signature · 1 line
Alexey
#55 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

In reply to Jason Gunthorpe
Quoted 7 lines · Jason Gunthorpe
Quoted 2 lines · Xu Yilun

E.g. I don't think VFIO driver would expect its MMIO access suddenly failed without knowing what happened.

What do people expect to happen here anyhow? Do you still intend to mmap any of the MMIO into the hypervisor? No, right? It is all locked

Not expecting mmap the MMIO, but I switched to another way. VFIO doesn't disallow mmap until bind, and if there is mmap on bind, bind failed. That's my understanding of your comments.

https://lore.kernel.org/kvm/Z4Hp9jvJbhW0cqWY@yilunxu-OptiPlex-7050/#t

Another concern is about dma-buf importer (e.g. KVM) mapping the MMIO. Recall we are working on the VFIO dma-buf solution, on bind/unbind the MMIO accessibility is being changed and importers should be notified to remove their mapping beforehand, and rebuild later if possible. An immediate requirement for Intel TDX is, KVM should remove secure EPT mapping for MMIO before unbind.

So I think device is all locked down into CC mode AFTER bind and BEFORE unbind. It doesn't seems viommu/vdevice could control bind/unbind.

There are other bus error handling cases, like AER when TDISP/SPDM/IDE state broken, that I don't have a clear solution now. But I cannot imagine they could be correctly handled without pci_driver support.

Quoted 17 lines

down?

So perhaps the answer is that the VFIO side has to put the device into CC mode which disables MMAP/etc, then the viommu/vdevice iommufd object can control it.

Quoted 4 lines

Back to your concern, I don't think it is a problem. From your patch, vIOMMU doesn't know the guest BDFn by nature, it is just the user stores the id in vdevice via iommufd_vdevice_alloc_ioctl(). A proper VFIO API could also do this work.

We don't want duplication though. If the viommu/vdevice/vbdf are owned and lifecycle controlled by iommufd then the operations against them must go through iommufd and through it's locking regime.

Quoted 3 lines

The implementation is basically no difference from:

Quoted 7 lines
Quoted 4 lines
(quoted diff) +2 -0
+       vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id,
+                                              IOMMUFD_OBJ_VDEVICE),

The real concern is the device owner, VFIO, should initiate the bind.

There is a big different, the above has correct locking, the other does not :)

Could you elaborate more on that? Any locking problem if we implement bind/unbind outside iommufd. Thanks in advance.

Thanks,
Yilun

Quoted 2 lines

Jason

maintainer
#95 lore raw
In reply to Alexey Kardashevskiy

FWIW,

I really appreciate this mail which explains to unenlightened people like me what this is all about.

Especially the Acronyms, Flow, Specs pointers etc. I wish I could see this type of writeups in all patchsets' 0th messages, leading in the reader into the topic while not expecting the latter to actually *know* all those things because, d0h, it is obvious. You can read my mind, right? :-)

So thanks for taking the time - it is very helpful!

Quoted 239 lines · Alexey Kardashevskiy

Here are some patches to enable SEV-TIO on AMD Turin. It's been a while and got quiet and I kept fixing my tree and wondering if I am going in the right direction.

SEV-TIO allow a guest to establish trust in a device that supports TEE Device Interface Security Protocol (TDISP, defined in PCIe r6.0+) and then interact with the device via private memory.

These include both guest and host support. QEMU also requires changes. This is more to show what it takes on AMD EPYC to pass through TDISP devices, hence "RFC".

Components affected:
KVM
IOMMUFD
CCP (AMD)
SEV-GUEST (AMD)

New components:
PCI IDE
PCI TSM
VIRT CoCo TSM
VIRT CoCo TSM-HOST
VIRT CoCo TSM-GUEST

This is based on a merge of Lukas'es CMA and 1 week old upstream + some of Dan's patches:

https://github.com/aik/linux/tree/tsm
https://github.com/aik/qemu/tree/tsm

Not using "[PATCH 03/11] coco/tsm: Introduce a class device for TEE Security Managers" yet as may be (may be) my approach makes sense too. Tried to stick to the terminology. I have done some changes on top of that, these are on github, not posting here as I expect those to be addressed in that thread:
https://lore.kernel.org/linux-coco/173343739517.1074769.13134786548545925484.stgit@dwillia2-xfh.jf.intel.com/T/

SEV TIO spec:
https://www.amd.com/content/dam/amd/en/documents/epyc-technical-docs/specifications/58271_0_70.pdf
Whitepaper:
https://www.amd.com/content/dam/amd/en/documents/epyc-business-docs/white-papers/sev-tio-whitepaper.pdf

Acronyms:

TEE - Trusted Execution Environments, a concept of managing trust between the host
and devices
TSM - TEE Security Manager (TSM), an entity which ensures security on the host PSP - AMD platform secure processor (also "ASP", "AMD-SP"), acts as TSM on AMD. SEV TIO - the TIO protocol implemented by the PSP and used by the host GHCB - guest/host communication block - a protocol for guest-to-host communication
via a shared page
TDISP - TEE Device Interface Security Protocol (PCIe).

Flow:

- Boot host OS, load CCP and PCI TSM (they will load TSM-HOST too)
- PCI TSM creates sysfs nodes in "coco/tsm: Add tsm and tsm-host modules" for all TDISP-capable devices
- Enable IDE via "echo 0 > /sys/bus/pci/devices/0000:e1:00.0/tsm-dev/tdev:0000:e1:00.0/tsm_dev_connect"
- Examine certificates/measurements/status via sysfs

- run an SNP VM _without_ VFIO PCI device, wait till it is booted
- hotplug a TDISP-capable PCI function, IOMMUFD must be used (not a VFIO container)
- QEMU pins all guest memory via IOMMUFD map-from-fd ioctl()
- the VM detects a TDISP-capable device, creates sysfs nodes in "coco/tsm: Add tsm-guest module"
- the VM loads the device driver which goes as usual till enabling bus master (for convinience)
- TSM-GUEST modules listens for bus master event (hacked in "pci: Add BUS_NOTIFY_PCI_BUS_MASTER event")
- TSM-GUEST requests TDI ("trusted PCI VF") info, traps into QEMU
- QEMU binds the VF to the Coco VM in the secure fw (AMD PSP) via IOMMUFD ioctl
- QEMU reads certificates/measurements/interface report from the hosts sysfs and writes to the guest memory
- the guest receives all the data, examines it (not in this series though)
- the guest enables secure DMA and MMIO by calling GHCB which traps into QEMU
- QEMU calls IOMMUFD ioctl to enable secure DMA and MMIO
- the guest can now stop sharing memory for DMA (and expect DMA to encrypted memory to work) and start accessing validated MMIO with Cbit set.

Assumptions

This requires hotpligging into the VM vs passing the device via the command line as VFIO maps all guest memory as the device init step which is too soon as SNP LAUNCH UPDATE happens later and will fail if VFIO maps private memory before that.

This requires the BME hack as MMIO and BusMaster enable bits cannot be 0 after MMIO validation is done and there are moments in the guest OS booting process when this appens.

SVSM could help addressing these (not implemented).

QEMU advertises TEE-IO capability to the VM. An additional x-tio flag is added to vfio-pci.

Trying to avoid the device driver modification as much as possible at the moment as my test devices already exist in non-TDISP form and need to work without modification. Arguably this may not be always the case.

TODOs

Deal with PCI reset. Hot unplug+plug? Power states too. Actually collaborate with CMA.
Other tons of things.

The previous conversation is here:
https://lore.kernel.org/r/[email protected]

Changes:
v2:
* redid the whole thing pretty much
* RMPUPDATE API for QEMU
* switched to IOMMUFD
* mapping guest memory via IOMMUFD map-from-fd
* marking resouces as validated
* more modules
* moved tons to the userspace (QEMU), such as TDI bind and GHCB guest requests

Sean, get_maintainer.pl produced more than 100 emails for the entire patchset, should I have posted them all anyway?

Please comment. Thanks.

Alexey Kardashevskiy (22):
pci/doe: Define protocol types and make those public PCI/IDE: Fixes to make it work on AMD SNP-SEV PCI/IDE: Init IDs on all IDE streams beforehand iommu/amd: Report SEV-TIO support
crypto: ccp: Enable SEV-TIO feature in the PSP when supported
KVM: X86: Define tsm_get_vmid
coco/tsm: Add tsm and tsm-host modules
pci/tsm: Add PCI driver for TSM
crypto/ccp: Implement SEV TIO firmware interface
KVM: SVM: Add uAPI to change RMP for MMIO
KVM: SEV: Add TIO VMGEXIT
iommufd: Allow mapping from guest_memfd
iommufd: amd-iommu: Add vdevice support
iommufd: Add TIO calls
KVM: X86: Handle private MMIO as shared
coco/tsm: Add tsm-guest module
resource: Mark encrypted MMIO resource on validation coco/sev-guest: Implement the guest support for SEV TIO
RFC: pci: Add BUS_NOTIFY_PCI_BUS_MASTER event
sev-guest: Stop changing encrypted page state for TDISP devices
pci: Allow encrypted MMIO mapping via sysfs
pci: Define pci_iomap_range_encrypted

 drivers/crypto/ccp/Makefile                 |   13 +
 drivers/pci/Makefile                        |    3 +
 drivers/virt/coco/Makefile                  |    2 +
 drivers/virt/coco/guest/Makefile            |    3 +
 drivers/virt/coco/host/Makefile             |    6 +
 drivers/virt/coco/sev-guest/Makefile        |    2 +-
 arch/x86/include/asm/kvm-x86-ops.h          |    1 +
 arch/x86/include/asm/kvm_host.h             |    2 +
 arch/x86/include/asm/sev.h                  |   31 +
 arch/x86/include/uapi/asm/kvm.h             |   11 +
 arch/x86/include/uapi/asm/svm.h             |    2 +
 drivers/crypto/ccp/sev-dev-tio.h            |  111 ++
 drivers/crypto/ccp/sev-dev.h                |   19 +
 drivers/iommu/amd/amd_iommu_types.h         |    3 +
 drivers/iommu/iommufd/iommufd_private.h     |    3 +
 include/asm-generic/pci_iomap.h             |    4 +
 include/linux/amd-iommu.h                   |    2 +
 include/linux/device.h                      |    4 +
 include/linux/device/bus.h                  |    3 +
 include/linux/dma-direct.h                  |    8 +
 include/linux/ioport.h                      |    2 +
 include/linux/kvm_host.h                    |    2 +
 include/linux/pci-doe.h                     |    4 +
 include/linux/pci-ide.h                     |   19 +-
 include/linux/pci.h                         |    2 +-
 include/linux/psp-sev.h                     |   61 +-
 include/linux/swiotlb.h                     |    8 +
 include/linux/tsm.h                         |  315 ++++
 include/uapi/linux/iommufd.h                |   26 +
 include/uapi/linux/kvm.h                    |   24 +
 include/uapi/linux/pci_regs.h               |    5 +-
 include/uapi/linux/psp-sev.h                |    6 +-
 include/uapi/linux/sev-guest.h              |   39 +
 arch/x86/coco/sev/core.c                    |   19 +-
 arch/x86/kvm/mmu/mmu.c                      |    6 +-
 arch/x86/kvm/svm/sev.c                      |  205 +++
 arch/x86/kvm/svm/svm.c                      |   12 +
 arch/x86/mm/ioremap.c                       |    2 +
 arch/x86/mm/mem_encrypt.c                   |    6 +
 arch/x86/virt/svm/sev.c                     |   34 +-
 drivers/crypto/ccp/sev-dev-tio.c            | 1664 ++++++++++++++++++++
 drivers/crypto/ccp/sev-dev-tsm.c            |  709 +++++++++
 drivers/crypto/ccp/sev-dev.c                |   94 +-
 drivers/iommu/amd/init.c                    |    9 +
 drivers/iommu/amd/iommu.c                   |   60 +-
 drivers/iommu/iommufd/main.c                |    6 +
 drivers/iommu/iommufd/pages.c               |   88 +-
 drivers/iommu/iommufd/viommu.c              |  112 ++
 drivers/pci/doe.c                           |    2 -
 drivers/pci/ide.c                           |  103 +-
 drivers/pci/iomap.c                         |   24 +
 drivers/pci/mmap.c                          |   11 +-
 drivers/pci/pci-sysfs.c                     |   27 +-
 drivers/pci/pci.c                           |    3 +
 drivers/pci/proc.c                          |    2 +-
 drivers/pci/tsm.c                           |  233 +++
 drivers/virt/coco/guest/tsm-guest.c         |  326 ++++
 drivers/virt/coco/host/tsm-host.c           |  551 +++++++
 drivers/virt/coco/sev-guest/sev_guest.c     |   10 +
 drivers/virt/coco/sev-guest/sev_guest_tio.c |  738 +++++++++
 drivers/virt/coco/tsm.c                     |  638 ++++++++
 kernel/resource.c                           |   48 +
 virt/kvm/kvm_main.c                         |    6 +
 Documentation/virt/coco/tsm.rst             |  132 ++
 drivers/crypto/ccp/Kconfig                  |    2 +
 drivers/pci/Kconfig                         |   15 +
 drivers/virt/coco/Kconfig                   |   14 +
 drivers/virt/coco/guest/Kconfig             |    3 +
 drivers/virt/coco/host/Kconfig              |    6 +
 drivers/virt/coco/sev-guest/Kconfig         |    1 +
 70 files changed, 6614 insertions(+), 53 deletions(-)
 create mode 100644 drivers/virt/coco/host/Makefile
 create mode 100644 drivers/crypto/ccp/sev-dev-tio.h
 create mode 100644 drivers/crypto/ccp/sev-dev-tio.c
 create mode 100644 drivers/crypto/ccp/sev-dev-tsm.c
 create mode 100644 drivers/pci/tsm.c
 create mode 100644 drivers/virt/coco/guest/tsm-guest.c
 create mode 100644 drivers/virt/coco/host/tsm-host.c
 create mode 100644 drivers/virt/coco/sev-guest/sev_guest_tio.c
 create mode 100644 drivers/virt/coco/tsm.c
 create mode 100644 Documentation/virt/coco/tsm.rst
 create mode 100644 drivers/virt/coco/host/Kconfig
Signature · 1 line
2.47.1
Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
maintainer
#90 lore raw

Re: [RFC PATCH v2 20/22] sev-guest: Stop changing encrypted page state for TDISP devices

In reply to Alexey Kardashevskiy
Quoted 43 lines · Alexey Kardashevskiy
include/linux/dma-direct.h +8 -0
index d7e30d4f7503..3bd533d2e65d 100644
@@ -94,6 +94,14 @@ static inline dma_addr_t phys_to_dma_unencrypted(struct device *dev,
  */
 static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
 {
+#if defined(CONFIG_TSM_GUEST) || defined(CONFIG_TSM_GUEST_MODULE)
+	if (dev->tdi_enabled) {
+		dev_warn_once(dev, "(TIO) Disable SME");
+		if (!dev->tdi_validated)
+			dev_warn(dev, "TDI is not validated, DMA @%llx will fail", paddr);
+		return phys_to_dma_unencrypted(dev, paddr);
+	}
+#endif
 	return __sme_set(phys_to_dma_unencrypted(dev, paddr));
 }
 
include/linux/swiotlb.h +8 -0
index 3dae0f592063..67bea31fa42a 100644
@@ -173,6 +173,14 @@ static inline bool is_swiotlb_force_bounce(struct device *dev)
 {
 	struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
 
+#if defined(CONFIG_TSM_GUEST) || defined(CONFIG_TSM_GUEST_MODULE)
+	if (dev->tdi_enabled) {
+		dev_warn_once(dev, "(TIO) Disable SWIOTLB");
+		if (!dev->tdi_validated)
+			dev_warn(dev, "TDI is not validated");
+		return false;
+	}
+#endif
 	return mem && mem->force_bounce;
 }
 
arch/x86/mm/mem_encrypt.c +0 -0
index 95bae74fdab2..c9c99154bec9 100644

arch/x86/mm/mem_encrypt.c line 27

Quoted 9 lines
arch/x86/mm/mem_encrypt.c +6 -0
@@ -19,6 +19,12 @@
 /* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */
 bool force_dma_unencrypted(struct device *dev)
 {
+#if defined(CONFIG_TSM_GUEST) || defined(CONFIG_TSM_GUEST_MODULE)
+	if (dev->tdi_enabled) {
+		dev_warn_once(dev, "(TIO) Disable decryption");
+		return false;
+	}
+#endif

Duplicated code with ugly ifdeffery. Perhaps do a helper which you call everywhere instead?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#52 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

In reply to Alexey Kardashevskiy
Quoted 8 lines · Alexey Kardashevskiy
Quoted 5 lines · Jason Gunthorpe
Quoted 2 lines · Xu Yilun

E.g. I don't think VFIO driver would expect its MMIO access suddenly failed without knowing what happened.

Quoted 8 lines
Quoted 3 lines

What do people expect to happen here anyhow? Do you still intend to mmap any of the MMIO into the hypervisor? No, right? It is all locked down?

This patchset expects it to be mmap'able as this is how MMIO gets mapped in the NPT and SEV-SNP still works with that (and updates the RMPs on top), the host os is not expected to access these though. TDX will handle this somehow different. Thanks,

I'm expecting you'll wrap that in a FD, since iommufd will not be accessing MMIO through mmaps.

Jason

#56 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

In reply to Xu Yilun
Quoted 6 lines · Xu Yilun
Quoted 5 lines · Jason Gunthorpe
Quoted 2 lines · Xu Yilun

E.g. I don't think VFIO driver would expect its MMIO access suddenly failed without knowing what happened.

Quoted 6 lines
Quoted 2 lines

What do people expect to happen here anyhow? Do you still intend to mmap any of the MMIO into the hypervisor? No, right? It is all locked

Not expecting mmap the MMIO, but I switched to another way. VFIO doesn't disallow mmap until bind, and if there is mmap on bind, bind failed. That's my understanding of your comments.

That seems reasonable

Quoted 6 lines

Another concern is about dma-buf importer (e.g. KVM) mapping the MMIO. Recall we are working on the VFIO dma-buf solution, on bind/unbind the MMIO accessibility is being changed and importers should be notified to remove their mapping beforehand, and rebuild later if possible. An immediate requirement for Intel TDX is, KVM should remove secure EPT mapping for MMIO before unbind.

dmabuf can do that..

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

The implementation is basically no difference from:

(quoted diff) +2 -0
+       vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id,
+                                              IOMMUFD_OBJ_VDEVICE),
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

The real concern is the device owner, VFIO, should initiate the bind.

There is a big different, the above has correct locking, the other does not :)

Could you elaborate more on that? Any locking problem if we implement bind/unbind outside iommufd. Thanks in advance.

You will be unable to access any information iommufd has in the viommu and vdevice objects. So you will not be able to pass a viommu ID or vBDF to the secure world unless you enter through an iommufd path, and use iommufd_get_object() to obtain the required locks.

I don't know what the API signatures are for all three platforms to tell if this is a problem or not.

Jason

#57 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

Quoted 16 lines · Jason Gunthorpe
Quoted 6 lines · Xu Yilun
Quoted 5 lines · Jason Gunthorpe
Quoted 2 lines · Xu Yilun

E.g. I don't think VFIO driver would expect its MMIO access suddenly failed without knowing what happened.

Quoted 6 lines
Quoted 2 lines

What do people expect to happen here anyhow? Do you still intend to mmap any of the MMIO into the hypervisor? No, right? It is all locked

Not expecting mmap the MMIO, but I switched to another way. VFIO doesn't disallow mmap until bind, and if there is mmap on bind, bind failed. That's my understanding of your comments.

That seems reasonable

Quoted 8 lines
Quoted 6 lines

Another concern is about dma-buf importer (e.g. KVM) mapping the MMIO. Recall we are working on the VFIO dma-buf solution, on bind/unbind the MMIO accessibility is being changed and importers should be notified to remove their mapping beforehand, and rebuild later if possible. An immediate requirement for Intel TDX is, KVM should remove secure EPT mapping for MMIO before unbind.

dmabuf can do that..

Yes, dmabuf can do that via notify. dmabuf is implemented in VFIO, so iommufd/vdevice couldn't operate on dmabuf and send the notify.

Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

The implementation is basically no difference from:

(quoted diff) +2 -0
+       vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id,
+                                              IOMMUFD_OBJ_VDEVICE),
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

The real concern is the device owner, VFIO, should initiate the bind.

There is a big different, the above has correct locking, the other does not :)

Could you elaborate more on that? Any locking problem if we implement bind/unbind outside iommufd. Thanks in advance.

Quoted 7 lines

You will be unable to access any information iommufd has in the viommu and vdevice objects. So you will not be able to pass a viommu ID or vBDF to the secure world unless you enter through an iommufd path, and use iommufd_get_object() to obtain the required locks.

I don't know what the API signatures are for all three platforms to tell if this is a problem or not.

Seems not a problem for Intel TDX. Basically secure DMA settings for TDX is just to build the secure IOMMUPT, only need host BDF. Also secure device setting needs no secure DMA info.

All these settings cannot really take function until guest verifies them and does TDISP start. Guest verification does not (should not) need host awareness.

Our solution is, separate the secure DMA setting and secure device setting in different components, iommufd & vfio.

Guest require bind:
- ioctl(iommufd, IOMMU_VIOMMU_ALLOC, {.type = IOMMU_VIOMMU_TYPE_KVM_VALID,

					.kvm_fd = kvm_fd,
					.out_viommu_id = &viommu_id});
  - ioctl(iommufd, IOMMU_HWPT_ALLOC, {.flag = IOMMU_HWPT_ALLOC_TRUSTED,
				      .pt_id = viommu_id,
				      .out_hwpt_id = &hwpt_id});

- ioctl(vfio_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, {.pt_id = hwpt_id})
- do secure DMA setting in Intel iommu driver.

- ioctl(vfio_fd, VFIO_DEVICE_TSM_BIND, ...)
- do bind in Intel TSM driver.

Thanks,
Yilun

Quoted 2 lines

Jason

author maintainer
#53 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

In reply to Jason Gunthorpe
Quoted 13 lines · Jason Gunthorpe
Quoted 12 lines · Alexey Kardashevskiy
Quoted 8 lines · Jason Gunthorpe
Quoted 2 lines · Xu Yilun

E.g. I don't think VFIO driver would expect its MMIO access suddenly failed without knowing what happened.

What do people expect to happen here anyhow? Do you still intend to mmap any of the MMIO into the hypervisor? No, right? It is all locked down?

Quoted 6 lines
Quoted 4 lines

This patchset expects it to be mmap'able as this is how MMIO gets mapped in the NPT and SEV-SNP still works with that (and updates the RMPs on top), the host os is not expected to access these though. TDX will handle this somehow different. Thanks,

I'm expecting you'll wrap that in a FD,

A KVM memslot from VFIO's fd similar to gmemfd's fd, and skip VMA? Doable but 1) creates a KVM->VFIO dependency to do gpa->hpa translation 2) is not necessary in the AMD case (although host-mmap of guest-assigned private BAR is way too easy way of shooting yourself in the foot).

Quoted 1 line

since iommufd will not be accessing MMIO through mmaps.

here I do not follow, why would iommufd care about MMIO? or it is about p2p DMA? Thanks,

Quoted 2 lines

Jason

Signature · 1 line
Alexey
#54 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

Quoted 21 lines · Alexey Kardashevskiy
Quoted 18 lines · Jason Gunthorpe
Quoted 16 lines · Alexey Kardashevskiy
Quoted 8 lines · Jason Gunthorpe
Quoted 2 lines · Xu Yilun

E.g. I don't think VFIO driver would expect its MMIO access suddenly failed without knowing what happened.

What do people expect to happen here anyhow? Do you still intend to mmap any of the MMIO into the hypervisor? No, right? It is all locked down?

This patchset expects it to be mmap'able as this is how MMIO gets mapped in the NPT and SEV-SNP still works with that (and updates the RMPs on top), the host os is not expected to access these though. TDX will handle this somehow different. Thanks,

Quoted 6 lines
Quoted 1 line

I'm expecting you'll wrap that in a FD,

A KVM memslot from VFIO's fd similar to gmemfd's fd, and skip VMA? Doable but 1) creates a KVM->VFIO dependency to do gpa->hpa translation 2) is not necessary in the AMD case (although host-mmap of guest-assigned private BAR is way too easy way of shooting yourself in the foot).

But since it is necessary in other cases, the code will be there and everyone should just use it..

Quoted 4 lines
Quoted 1 line

since iommufd will not be accessing MMIO through mmaps.

here I do not follow, why would iommufd care about MMIO? or it is about p2p DMA? Thanks,

Yes, p2p dma

Jason

#58 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

In reply to Xu Yilun
Quoted 14 lines · Xu Yilun

All these settings cannot really take function until guest verifies them and does TDISP start. Guest verification does not (should not) need host awareness.

Our solution is, separate the secure DMA setting and secure device setting in different components, iommufd & vfio.

Guest require bind:
- ioctl(iommufd, IOMMU_VIOMMU_ALLOC, {.type = IOMMU_VIOMMU_TYPE_KVM_VALID,

					.kvm_fd = kvm_fd,
					.out_viommu_id = &viommu_id});
  - ioctl(iommufd, IOMMU_HWPT_ALLOC, {.flag = IOMMU_HWPT_ALLOC_TRUSTED,
				      .pt_id = viommu_id,
				      .out_hwpt_id = &hwpt_id});
Quoted 5 lines

- ioctl(vfio_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, {.pt_id = hwpt_id})
- do secure DMA setting in Intel iommu driver.

- ioctl(vfio_fd, VFIO_DEVICE_TSM_BIND, ...)
- do bind in Intel TSM driver.

Except what do command do you issue to the secure world for TSM_BIND and what are it's argument? Again you can't include the vBDF or vIOMMU ID here.

vfio also can't validate that the hwpt is in the right state when it executes this function.

You could also issue the TSM bind against the idev on the iommufd side..

Part of my problem here is I don't see anyone who seems to have read all three specs and is trying to mush them together. Everyone is focused on their own spec. I know there are subtle differences :\

Jason

#59 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

Quoted 18 lines · Jason Gunthorpe
Quoted 17 lines · Xu Yilun

All these settings cannot really take function until guest verifies them and does TDISP start. Guest verification does not (should not) need host awareness.

Our solution is, separate the secure DMA setting and secure device setting in different components, iommufd & vfio.

Guest require bind:
- ioctl(iommufd, IOMMU_VIOMMU_ALLOC, {.type = IOMMU_VIOMMU_TYPE_KVM_VALID,

					.kvm_fd = kvm_fd,
					.out_viommu_id = &viommu_id});
  - ioctl(iommufd, IOMMU_HWPT_ALLOC, {.flag = IOMMU_HWPT_ALLOC_TRUSTED,
				      .pt_id = viommu_id,
				      .out_hwpt_id = &hwpt_id});

- ioctl(vfio_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, {.pt_id = hwpt_id})
- do secure DMA setting in Intel iommu driver.

Quoted 6 lines
Quoted 2 lines

- ioctl(vfio_fd, VFIO_DEVICE_TSM_BIND, ...)
- do bind in Intel TSM driver.

Except what do command do you issue to the secure world for TSM_BIND and what are it's argument? Again you can't include the vBDF or vIOMMU ID here.

Bind for TDX doesn't require vBDF or vIOMMU ID. The seamcall is like:

u64 tdh_devif_create(u64 stream_id,     // IDE stream ID, PF0 stuff
                     u64 devif_id,      // TDI ID, it is the host BDF
                     u64 tdr_pa,        // TDX VM core metadate page, TDX Connect uses it as CoCo-VM ID
                     u64 devifcs_pa)    // metadate page provide to firmware

While for AMD:
...
b.guest_device_id = guest_rid; //TDI ID, it is the vBDF
b.gctx_paddr = gctx_paddr; //AMDs CoCo-VM ID

ret = sev_tio_do_cmd(SEV_CMD_TIO_TDI_BIND, &b, ...

Neither of them use vIOMMU ID or any IOMMU info, so the only concern is vBDF.

Basically from host POV the two interfaces does the same thing, connect the CoCo-VM ID with the TDI ID, for which Intel uses host BDF while AMD uses vBDF. But AMD firmware cannot know anything meaningful about the vBDF, it is just a magic number to index TDI metadata.

So I don't think we have to introduce vBDF concept in kernel. AMD uses QEMU created vBDF as TDI ID, that's fine, QEMU should ensure the validity of the vBDF.

Quoted 3 lines

vfio also can't validate that the hwpt is in the right state when it executes this function.

Not sure if VFIO has to validate, or is there a requirement that secure DMA should be in right state before bind. TDX doesn't require this, and I didn't see the requirement in SEV-TIO spec. I.e. the bind firmware calls don't check DMA state.

In my opinion, TDI bind means put device in LOCKED state and related metadate management in firmware. After bind the DMA cannot work. It is the guest's resposibility to validate everything (including DMA) is in the right state, then issues RUN, then DMA works. I.e. guest tsm calls check DMA state. That's why I think Secure DMA configuration on host could be in a separated flow from bind.

Quoted 3 lines

You could also issue the TSM bind against the idev on the iommufd side..

But I cannot figure out how idev could ensure no mmap on VFIO, and how idev could call dma_buf_move_notify.

Thanks,
Yilun

Quoted 6 lines

Part of my problem here is I don't see anyone who seems to have read all three specs and is trying to mush them together. Everyone is focused on their own spec. I know there are subtle differences :\

Jason

#60 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

Quoted 10 lines · Xu Yilun

While for AMD:
...
b.guest_device_id = guest_rid; //TDI ID, it is the vBDF
b.gctx_paddr = gctx_paddr; //AMDs CoCo-VM ID

ret = sev_tio_do_cmd(SEV_CMD_TIO_TDI_BIND, &b, ...

Neither of them use vIOMMU ID or any IOMMU info, so the only concern is vBDF.

I think that is enough, we should not be putting this in VFIO if it cannot execute it for AMD :\

Quoted 5 lines
Quoted 2 lines

You could also issue the TSM bind against the idev on the iommufd side..

But I cannot figure out how idev could ensure no mmap on VFIO, and how idev could call dma_buf_move_notify.

I suggest you start out this way from the VFIO. Put the device in a CC mode which bans the mmap entirely and pass that CC capable as a flag into iommufd when creating the idev.

If it really needs to be dyanmic a VFIO feature could change the CC mode and that could call back to iommufd to synchronize if that is allowed.

Jason

author maintainer
#62 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

In reply to Xu Yilun
Quoted 26 lines · Xu Yilun
Quoted 18 lines · Jason Gunthorpe
Quoted 17 lines · Xu Yilun

All these settings cannot really take function until guest verifies them and does TDISP start. Guest verification does not (should not) need host awareness.

Our solution is, separate the secure DMA setting and secure device setting in different components, iommufd & vfio.

Guest require bind:
- ioctl(iommufd, IOMMU_VIOMMU_ALLOC, {.type = IOMMU_VIOMMU_TYPE_KVM_VALID,

					.kvm_fd = kvm_fd,
					.out_viommu_id = &viommu_id});
   - ioctl(iommufd, IOMMU_HWPT_ALLOC, {.flag = IOMMU_HWPT_ALLOC_TRUSTED,
				      .pt_id = viommu_id,
				      .out_hwpt_id = &hwpt_id});

- ioctl(vfio_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, {.pt_id = hwpt_id})
- do secure DMA setting in Intel iommu driver.

Quoted 6 lines
Quoted 2 lines

- ioctl(vfio_fd, VFIO_DEVICE_TSM_BIND, ...)
- do bind in Intel TSM driver.

Except what do command do you issue to the secure world for TSM_BIND and what are it's argument? Again you can't include the vBDF or vIOMMU ID here.

Quoted 6 lines

Bind for TDX doesn't require vBDF or vIOMMU ID. The seamcall is like:

u64 tdh_devif_create(u64 stream_id, // IDE stream ID, PF0 stuff

                      u64 devif_id,      // TDI ID, it is the host BDF
                      u64 tdr_pa,        // TDX VM core metadate page, TDX Connect uses it as CoCo-VM ID
                      u64 devifcs_pa)    // metadate page provide to firmware

(offtopic) is there a public spec with this command defined?

Quoted 38 lines

While for AMD:
...
b.guest_device_id = guest_rid; //TDI ID, it is the vBDF
b.gctx_paddr = gctx_paddr; //AMDs CoCo-VM ID

ret = sev_tio_do_cmd(SEV_CMD_TIO_TDI_BIND, &b, ...

Neither of them use vIOMMU ID or any IOMMU info, so the only concern is vBDF.

Basically from host POV the two interfaces does the same thing, connect the CoCo-VM ID with the TDI ID, for which Intel uses host BDF while AMD uses vBDF. But AMD firmware cannot know anything meaningful about the vBDF, it is just a magic number to index TDI metadata.

So I don't think we have to introduce vBDF concept in kernel. AMD uses QEMU created vBDF as TDI ID, that's fine, QEMU should ensure the validity of the vBDF.

Quoted 3 lines

vfio also can't validate that the hwpt is in the right state when it executes this function.

Not sure if VFIO has to validate, or is there a requirement that secure DMA should be in right state before bind. TDX doesn't require this, and I didn't see the requirement in SEV-TIO spec. I.e. the bind firmware calls don't check DMA state.

In my opinion, TDI bind means put device in LOCKED state and related metadate management in firmware. After bind the DMA cannot work. It is the guest's resposibility to validate everything (including DMA) is in the right state, then issues RUN, then DMA works. I.e. guest tsm calls check DMA state. That's why I think Secure DMA configuration on host could be in a separated flow from bind.

Quoted 1 line
Quoted 13 lines
Quoted 2 lines

You could also issue the TSM bind against the idev on the iommufd side..

But I cannot figure out how idev could ensure no mmap on VFIO, and how idev could call dma_buf_move_notify.

Thanks,
Yilun

Quoted 4 lines

Part of my problem here is I don't see anyone who seems to have read all three specs and is trying to mush them together. Everyone is focused on their own spec. I know there are subtle differences :\

One is SEV TIO (earlier version published), another one TDX Connect (which I do not have and asked above) and what is the third one here? Or is it 4 as ARM and RiscV both doing this now? Thanks,

Signature · 1 line
Alexey
#61 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

In reply to Jason Gunthorpe
Quoted 7 lines · Jason Gunthorpe
Quoted 5 lines · Xu Yilun

While for AMD:
...
b.guest_device_id = guest_rid; //TDI ID, it is the vBDF
b.gctx_paddr = gctx_paddr; //AMDs CoCo-VM ID

Quoted 8 lines
Quoted 5 lines
        ret = sev_tio_do_cmd(SEV_CMD_TIO_TDI_BIND, &b, ...

Neither of them use vIOMMU ID or any IOMMU info, so the only concern is vBDF.

I think that is enough, we should not be putting this in VFIO if it cannot execute it for AMD :\

OK. With these discussion, my understanding is it can execute for AMD but we don't duplicate the effort for vdevice->id.

We can swtich to vdevice and try to solve the rest problems.

Quoted 10 lines
Quoted 6 lines
Quoted 3 lines

You could also issue the TSM bind against the idev on the iommufd side..

But I cannot figure out how idev could ensure no mmap on VFIO, and how idev could call dma_buf_move_notify.

I suggest you start out this way from the VFIO. Put the device in a CC mode which bans the mmap entirely and pass that CC capable as a flag into iommufd when creating the idev.

IIUC, it basically switches back to my previous implementation for mmap.

https://lore.kernel.org/kvm/[email protected]/

I can do that.

Thanks,
yilun

Quoted 6 lines

If it really needs to be dyanmic a VFIO feature could change the CC mode and that could call back to iommufd to synchronize if that is allowed.

Jason

#63 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

In reply to Alexey Kardashevskiy
Quoted 7 lines · Alexey Kardashevskiy
Quoted 3 lines
Quoted 3 lines

Part of my problem here is I don't see anyone who seems to have read all three specs and is trying to mush them together. Everyone is focused on their own spec. I know there are subtle differences :\

One is SEV TIO (earlier version published), another one TDX Connect (which I do not have and asked above) and what is the third one here? Or is it 4 as ARM and RiscV both doing this now? Thanks,

ARM will come with a spec someday, I don't know about RISCV. Maybe it is 4..

Jason

#65 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

In reply to Alexey Kardashevskiy
Quoted 31 lines · Alexey Kardashevskiy
Quoted 28 lines · Xu Yilun
Quoted 18 lines · Jason Gunthorpe
Quoted 17 lines · Xu Yilun

All these settings cannot really take function until guest verifies them and does TDISP start. Guest verification does not (should not) need host awareness.

Our solution is, separate the secure DMA setting and secure device setting in different components, iommufd & vfio.

Guest require bind:
- ioctl(iommufd, IOMMU_VIOMMU_ALLOC, {.type = IOMMU_VIOMMU_TYPE_KVM_VALID,

					.kvm_fd = kvm_fd,
					.out_viommu_id = &viommu_id});
   - ioctl(iommufd, IOMMU_HWPT_ALLOC, {.flag = IOMMU_HWPT_ALLOC_TRUSTED,
				      .pt_id = viommu_id,
				      .out_hwpt_id = &hwpt_id});

- ioctl(vfio_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, {.pt_id = hwpt_id})
- do secure DMA setting in Intel iommu driver.

Quoted 6 lines
Quoted 2 lines

- ioctl(vfio_fd, VFIO_DEVICE_TSM_BIND, ...)
- do bind in Intel TSM driver.

Except what do command do you issue to the secure world for TSM_BIND and what are it's argument? Again you can't include the vBDF or vIOMMU ID here.

Bind for TDX doesn't require vBDF or vIOMMU ID. The seamcall is like:

Quoted 7 lines
Quoted 4 lines

u64 tdh_devif_create(u64 stream_id, // IDE stream ID, PF0 stuff

                      u64 devif_id,      // TDI ID, it is the host BDF
                      u64 tdr_pa,        // TDX VM core metadate page, TDX Connect uses it as CoCo-VM ID
                      u64 devifcs_pa)    // metadate page provide to firmware

(offtopic) is there a public spec with this command defined?

Sorry, there is no public TDX Connect SPEC yet.

Thanks,
Yilun

#64 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

In reply to Jason Gunthorpe
Quoted 11 lines · Jason Gunthorpe
Quoted 7 lines · Alexey Kardashevskiy
Quoted 3 lines
Quoted 3 lines

Part of my problem here is I don't see anyone who seems to have read all three specs and is trying to mush them together. Everyone is focused on their own spec. I know there are subtle differences :\

One is SEV TIO (earlier version published), another one TDX Connect (which I do not have and asked above) and what is the third one here? Or is it 4 as ARM and RiscV both doing this now? Thanks,

ARM will come with a spec someday, I don't know about RISCV. Maybe it is 4..

The Arm CCA DA (Device Assignment, as we call it) specs are available in Alpha stage here, under "Future version" section.

https://developer.arm.com/documentation/den0137/latest/

Cheers
Suzuki

Quoted 2 lines

Jason

#10 lore raw

Re: [RFC PATCH v2 06/22] KVM: X86: Define tsm_get_vmid

In reply to Alexey Kardashevskiy
Quoted 64 lines · Alexey Kardashevskiy

In order to add a PCI VF into a secure VM, the TSM module needs to perform a "TDI bind" operation. The secure module ("PSP" for AMD) reuqires a VM id to associate with a VM and KVM has it. Since KVM cannot directly bind a TDI (as it does not have all necesessary data such as host/guest PCI BDFn). QEMU and IOMMUFD do know the BDFns but they do not have a VM id recognisable by the PSP.

Add get_vmid() hook to KVM. Implement it for AMD SEV to return a sum of GCTX (a private page describing secure VM context) and ASID (required on unbind for IOMMU unfencing, when needed).

---
 arch/x86/include/asm/kvm-x86-ops.h |  1 +
 arch/x86/include/asm/kvm_host.h    |  2 ++
 include/linux/kvm_host.h           |  2 ++
 arch/x86/kvm/svm/svm.c             | 12 ++++++++++++
 virt/kvm/kvm_main.c                |  6 ++++++
 5 files changed, 23 insertions(+)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
index c35550581da0..63102a224cd7 100644
@@ -144,6 +144,7 @@ KVM_X86_OP_OPTIONAL(alloc_apic_backing_page)
 KVM_X86_OP_OPTIONAL_RET0(gmem_prepare)
 KVM_X86_OP_OPTIONAL_RET0(private_max_mapping_level)
 KVM_X86_OP_OPTIONAL(gmem_invalidate)
+KVM_X86_OP_OPTIONAL(tsm_get_vmid)
 
 #undef KVM_X86_OP
 #undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +2 -0
index b15cde0a9b5c..9330e8d4d29d 100644
@@ -1875,6 +1875,8 @@ struct kvm_x86_ops {
 	int (*gmem_prepare)(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order);
 	void (*gmem_invalidate)(kvm_pfn_t start, kvm_pfn_t end);
 	int (*private_max_mapping_level)(struct kvm *kvm, kvm_pfn_t pfn);
+
+	u64 (*tsm_get_vmid)(struct kvm *kvm);
 };
 
 struct kvm_x86_nested_ops {
include/linux/kvm_host.h +2 -0
index f34f4cfaa513..6cd351edb956 100644
@@ -2571,4 +2571,6 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,
 				    struct kvm_pre_fault_memory *range);
 #endif
 
+u64 kvm_arch_tsm_get_vmid(struct kvm *kvm);
+
 #endif
arch/x86/kvm/svm/svm.c +0 -0
index 7640a84e554a..0276d60c61d6 100644
@@ -4998,6 +4998,16 @@ static void *svm_alloc_apic_backing_page(struct kvm_vcpu *vcpu)
 	return page_address(page);
 }
 

arch/x86/kvm/svm/svm.c static void *svm_alloc_apic_backing_page(struct kvm_vcpu *vcpu) · line 5010

Quoted 10 lines
arch/x86/kvm/svm/svm.c +10 -0
+static u64 svm_tsm_get_vmid(struct kvm *kvm)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+
+	if (!sev->es_active)
+		return 0;
+
+	return ((u64) sev->snp_context) | sev->asid;
+}
+

Curious why KVM needs to be bothered by a new kvm_arch_tsm_get_vmid() and a vendor specific cookie "vmid" concept. In other words KVM never calls kvm_arch_tsm_get_vmid(), like other kvm_arch_*() support calls.

Is this due to a restriction that something like tsm_tdi_bind() is disallowed from doing to_kvm_svm() on an opaque @kvm pointer? Or otherwise asking an arch/x86/kvm/svm/svm.c to do the same?

Effectively low level TSM drivers are extensions of arch code that routinely performs "container_of(kvm, struct kvm_$arch, kvm)".

author maintainer
#11 lore raw

Re: [RFC PATCH v2 06/22] KVM: X86: Define tsm_get_vmid

Quoted 80 lines · Dan Williams
Quoted 64 lines · Alexey Kardashevskiy

In order to add a PCI VF into a secure VM, the TSM module needs to perform a "TDI bind" operation. The secure module ("PSP" for AMD) reuqires a VM id to associate with a VM and KVM has it. Since KVM cannot directly bind a TDI (as it does not have all necesessary data such as host/guest PCI BDFn). QEMU and IOMMUFD do know the BDFns but they do not have a VM id recognisable by the PSP.

Add get_vmid() hook to KVM. Implement it for AMD SEV to return a sum of GCTX (a private page describing secure VM context) and ASID (required on unbind for IOMMU unfencing, when needed).

---
  arch/x86/include/asm/kvm-x86-ops.h |  1 +
  arch/x86/include/asm/kvm_host.h    |  2 ++
  include/linux/kvm_host.h           |  2 ++
  arch/x86/kvm/svm/svm.c             | 12 ++++++++++++
  virt/kvm/kvm_main.c                |  6 ++++++
  5 files changed, 23 insertions(+)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
index c35550581da0..63102a224cd7 100644
@@ -144,6 +144,7 @@ KVM_X86_OP_OPTIONAL(alloc_apic_backing_page)
  KVM_X86_OP_OPTIONAL_RET0(gmem_prepare)
  KVM_X86_OP_OPTIONAL_RET0(private_max_mapping_level)
  KVM_X86_OP_OPTIONAL(gmem_invalidate)
+KVM_X86_OP_OPTIONAL(tsm_get_vmid)
 
  #undef KVM_X86_OP
  #undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +2 -0
index b15cde0a9b5c..9330e8d4d29d 100644
@@ -1875,6 +1875,8 @@ struct kvm_x86_ops {
  	int (*gmem_prepare)(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order);
  	void (*gmem_invalidate)(kvm_pfn_t start, kvm_pfn_t end);
  	int (*private_max_mapping_level)(struct kvm *kvm, kvm_pfn_t pfn);
+
+	u64 (*tsm_get_vmid)(struct kvm *kvm);
  };
 
  struct kvm_x86_nested_ops {
include/linux/kvm_host.h +2 -0
index f34f4cfaa513..6cd351edb956 100644
@@ -2571,4 +2571,6 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,
  				    struct kvm_pre_fault_memory *range);
  #endif
 
+u64 kvm_arch_tsm_get_vmid(struct kvm *kvm);
+
  #endif
arch/x86/kvm/svm/svm.c +0 -0
index 7640a84e554a..0276d60c61d6 100644
@@ -4998,6 +4998,16 @@ static void *svm_alloc_apic_backing_page(struct kvm_vcpu *vcpu)
  	return page_address(page);
  }
 

arch/x86/kvm/svm/svm.c static void *svm_alloc_apic_backing_page(struct kvm_vcpu *vcpu) · line 5010

Quoted 10 lines
arch/x86/kvm/svm/svm.c +10 -0
+static u64 svm_tsm_get_vmid(struct kvm *kvm)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+
+	if (!sev->es_active)
+		return 0;
+
+	return ((u64) sev->snp_context) | sev->asid;
+}
+

Curious why KVM needs to be bothered by a new kvm_arch_tsm_get_vmid() and a vendor specific cookie "vmid" concept. In other words KVM never calls kvm_arch_tsm_get_vmid(), like other kvm_arch_*() support calls.

Quoted 3 lines

Is this due to a restriction that something like tsm_tdi_bind() is disallowed from doing to_kvm_svm() on an opaque @kvm pointer? Or otherwise asking an arch/x86/kvm/svm/svm.c to do the same?

I saw someone already doing some sort of VMID thing and thought it is a good way of not spilling KVM details outside KVM.

Quoted 2 lines

Effectively low level TSM drivers are extensions of arch code that routinely performs "container_of(kvm, struct kvm_$arch, kvm)".

The arch code is CCP and so far it avoided touching KVM, KVM calls CCP when it needs but not vice versa. Thanks,

Signature · 1 line
Alexey
author maintainer
#37 lore raw

Re: [RFC PATCH v2 12/22] iommufd: Allow mapping from guest_memfd

In reply to Jason Gunthorpe
Quoted 39 lines · Jason Gunthorpe
Quoted 5 lines · Michael Roth

Just for clarity: at least for normal/nested page table (but I'm assuming the same applies to IOMMU mappings), 1G mappings are handled similarly as 2MB mappings as far as RMP table checks are
concerned: each 2MB range is checked individually as if it were a separate 2MB mapping:

Well, IIRC we are dealing with the AMDv1 IO page table here which supports more sizes than 1G and we likely start to see things like 4M mappings and the like. So maybe there is some issue if the above special case really only applies to 1G and only 1G.

Quoted 4 lines

But the point still stands for 4K RMP entries and 2MB mappings: a 2MB mapping either requires private page RMP entries to be 2MB, or in the case of 2MB mapping of shared pages, every page in the range must be shared according to the corresponding RMP entries.

Is 4k RMP what people are running?

Quoted 2 lines

I think, for the non-SEV-TIO use-case, it had more to do with inability to unmap a 4K range once a particular 4K page has been converted

Yes, we don't support unmap or resize. The entire theory of operation has the IOPTEs cover the guest memory and remain static at VM boot time. The RMP alone controls access and handles the static/private.

Assuming the host used 2M pages the IOPTEs in an AMDv1 table will be sized around 2M,4M,8M just based around random luck.

So it sounds like you can get to a situation with a >=2M mapping in the IOPTE but the guest has split it into private/shared at lower granularity and the HW cannot handle this?

Quoted 5 lines

from shared to private if it was originally installed via a 2MB IOPTE, since the guest could actively be DMA'ing to other shared pages in the 2M range (but we can be assured it is not DMA'ing to a particular 4K page it has converted to private), and the IOMMU doesn't (AFAIK) have a way to atomically split an existing 2MB IOPTE to avoid this.

Quoted 6 lines

The iommu can split it (with SW help), I'm working on that infrastructure right now..

So you will get a notification that the guest has made a private/public split and the iommu page table can be atomically restructured to put an IOPTE boundary at the split.

About this atomical restructure - I looked at yours iommu-pt branch on github but __cut_mapping()->pt_table_install64() only atomically swaps the PDE but it does not do IOMMU TLB invalidate, have I missed it? And if it did so, that would not be atomic but it won't matter as long as we do not destroy the old PDE before invalidating IOMMU TLB, is this the idea? Thanks,

Quoted 5 lines

Then the HW will not see IOPTEs that exceed the shared/private granularity of the VM.

Jason

Signature · 1 line
Alexey
#70 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

In reply to Alexey Kardashevskiy

drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 178

Quoted 10 lines
drivers/iommu/iommufd/viommu.c +10 -0
+int iommufd_vdevice_tsm_bind_ioctl(struct iommufd_ucmd *ucmd)
+{
+	struct iommu_vdevice_tsm_bind *cmd = ucmd->cmd;
+	struct iommufd_viommu *viommu;
+	struct iommufd_vdevice *vdev;
+	struct iommufd_device *idev;
+	struct tsm_tdi *tdi;
+	int rc = 0;
+
+	viommu = iommufd_get_viommu(ucmd, cmd->viommu_id);

Why need user to input viommu_id? And why get viommu here? The viommu is always available after vdevice is allocated, is it?

int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd)

{
	...

	vdev->viommu = viommu;
	refcount_inc(&viommu->obj.users);
	...
}

drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 191

Quoted 13 lines
drivers/iommu/iommufd/viommu.c +13 -0
+	if (IS_ERR(viommu))
+		return PTR_ERR(viommu);
+
+	idev = iommufd_get_device(ucmd, cmd->dev_id);
+	if (IS_ERR(idev)) {
+		rc = PTR_ERR(idev);
+		goto out_put_viommu;
+	}
+
+	vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id,
+					       IOMMUFD_OBJ_VDEVICE),
+			    struct iommufd_vdevice, obj);
+	if (IS_ERR(idev)) {
                   ^

vdev?

drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 196

Quoted 5 lines
drivers/iommu/iommufd/viommu.c +5 -0
+		rc = PTR_ERR(idev);
+		goto out_put_dev;
+	}
+
+	tdi = tsm_tdi_get(idev->dev);

And do we still need dev_id for the struct device *? vdevice also has this info.

int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd)

{
        ...
	vdev->dev = idev->dev;
	get_device(idev->dev);
        ...
}
Quoted 12 lines
drivers/iommu/iommufd/viommu.c +12 -0
+	if (!tdi) {
+		rc = -ENODEV;
+		goto out_put_vdev;
+	}
+
+	rc = tsm_tdi_bind(tdi, vdev->id, cmd->kvmfd);
+	if (rc)
+		goto out_put_tdi;
+
+	vdev->tsm_bound = true;
+
+	rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));

drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 218

Quoted 10 lines
drivers/iommu/iommufd/viommu.c +10 -0
+out_put_tdi:
+	tsm_tdi_put(tdi);
+out_put_vdev:
+	iommufd_put_object(ucmd->ictx, &vdev->obj);
+out_put_dev:
+	iommufd_put_object(ucmd->ictx, &idev->obj);
+out_put_viommu:
+	iommufd_put_object(ucmd->ictx, &viommu->obj);
+	return rc;
+}

Another concern is do we need an unbind ioctl? We don't bind on vdevice create so it seems not symmetrical we only unbind on vdevice destroy.

Thanks,
Yilun

#12 lore raw

Re: [RFC PATCH v2 06/22] KVM: X86: Define tsm_get_vmid

In reply to Alexey Kardashevskiy
Quoted 83 lines · Alexey Kardashevskiy
Quoted 80 lines · Dan Williams
Quoted 64 lines · Alexey Kardashevskiy

In order to add a PCI VF into a secure VM, the TSM module needs to perform a "TDI bind" operation. The secure module ("PSP" for AMD) reuqires a VM id to associate with a VM and KVM has it. Since KVM cannot directly bind a TDI (as it does not have all necesessary data such as host/guest PCI BDFn). QEMU and IOMMUFD do know the BDFns but they do not have a VM id recognisable by the PSP.

Add get_vmid() hook to KVM. Implement it for AMD SEV to return a sum of GCTX (a private page describing secure VM context) and ASID (required on unbind for IOMMU unfencing, when needed).

---
  arch/x86/include/asm/kvm-x86-ops.h |  1 +
  arch/x86/include/asm/kvm_host.h    |  2 ++
  include/linux/kvm_host.h           |  2 ++
  arch/x86/kvm/svm/svm.c             | 12 ++++++++++++
  virt/kvm/kvm_main.c                |  6 ++++++
  5 files changed, 23 insertions(+)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
index c35550581da0..63102a224cd7 100644
@@ -144,6 +144,7 @@ KVM_X86_OP_OPTIONAL(alloc_apic_backing_page)
  KVM_X86_OP_OPTIONAL_RET0(gmem_prepare)
  KVM_X86_OP_OPTIONAL_RET0(private_max_mapping_level)
  KVM_X86_OP_OPTIONAL(gmem_invalidate)
+KVM_X86_OP_OPTIONAL(tsm_get_vmid)
 
  #undef KVM_X86_OP
  #undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +2 -0
index b15cde0a9b5c..9330e8d4d29d 100644
@@ -1875,6 +1875,8 @@ struct kvm_x86_ops {
  	int (*gmem_prepare)(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order);
  	void (*gmem_invalidate)(kvm_pfn_t start, kvm_pfn_t end);
  	int (*private_max_mapping_level)(struct kvm *kvm, kvm_pfn_t pfn);
+
+	u64 (*tsm_get_vmid)(struct kvm *kvm);
  };
 
  struct kvm_x86_nested_ops {
include/linux/kvm_host.h +2 -0
index f34f4cfaa513..6cd351edb956 100644
@@ -2571,4 +2571,6 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,
  				    struct kvm_pre_fault_memory *range);
  #endif
 
+u64 kvm_arch_tsm_get_vmid(struct kvm *kvm);
+
  #endif
arch/x86/kvm/svm/svm.c +0 -0
index 7640a84e554a..0276d60c61d6 100644
@@ -4998,6 +4998,16 @@ static void *svm_alloc_apic_backing_page(struct kvm_vcpu *vcpu)
  	return page_address(page);
  }
 

arch/x86/kvm/svm/svm.c static void *svm_alloc_apic_backing_page(struct kvm_vcpu *vcpu) · line 5010

Quoted 10 lines
arch/x86/kvm/svm/svm.c +10 -0
+static u64 svm_tsm_get_vmid(struct kvm *kvm)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+
+	if (!sev->es_active)
+		return 0;
+
+	return ((u64) sev->snp_context) | sev->asid;
+}
+

Curious why KVM needs to be bothered by a new kvm_arch_tsm_get_vmid() and a vendor specific cookie "vmid" concept. In other words KVM never calls kvm_arch_tsm_get_vmid(), like other kvm_arch_*() support calls.

Quoted 5 lines
Quoted 3 lines

Is this due to a restriction that something like tsm_tdi_bind() is disallowed from doing to_kvm_svm() on an opaque @kvm pointer? Or otherwise asking an arch/x86/kvm/svm/svm.c to do the same?

I saw someone already doing some sort of VMID thing

Reference?

Quoted 1 line

and thought it is a good way of not spilling KVM details outside KVM.

...but it is not a KVM detail. It is an arch specific TSM cookie derived from arch specific data that wraps 'struct kvm'. Now if the rationale is some least privelege concern about what code can have a container_of() relationship with an opaque 'struct kvm *' pointer, let's have that discussion. As it stands nothing in KVM cares about kvm_arch_tsm_get_vmid(), and I expect 'vmid' does not cover all the ways in which modular TSM drivers may interact with arch/.../kvm/ code.

For example TDX Connect needs to share some data from 'struct kvm_tdx', and it does that with an export from arch/x86/kvm/vmx/tdx.c, not an indirection through virt/kvm/kvm_main.c.

Quoted 5 lines
Quoted 2 lines

Effectively low level TSM drivers are extensions of arch code that routinely performs "container_of(kvm, struct kvm_$arch, kvm)".

The arch code is CCP and so far it avoided touching KVM, KVM calls CCP when it needs but not vice versa. Thanks,

Right, and the observation is that you don't need to touch virt/kvm/kvm_main.c at all to meet this data sharing requirement.

#16 lore raw

Re: [RFC PATCH v2 07/22] coco/tsm: Add tsm and tsm-host modules

In reply to Alexey Kardashevskiy
Quoted 9 lines · Alexey Kardashevskiy

The TSM module is a library to create sysfs nodes common for hypervisors and VMs. It also provides helpers to parse interface reports (required by VMs, visible to HVs). It registers 3 device classes:
- tsm: one per platform,
- tsm-dev: for physical functions, ("TDEV");
- tdm-tdi: for PCI functions being assigned to VMs ("TDI").

The library adds a child device of "tsm-dev" or/and "tsm-tdi" class for every capable PCI device. Note that the module is made bus-agnostic.

There was some discussion on the merits of "TDEV" and "TDI" objects in the PCI/TSM thread [1], I will summarize the main objections here:

* PCI device security is a PCI device property. That security property is not strictly limited to the platform TEE Security Manager (TSM) case. The PCI device authentication enabling, mentioned as "Lukas's CMA" in the cover letter, adds a TSM-independent authentication and device measurement collection ABI. The PCI/TSM proposal simply aims to reuse that ABI and existing PCI device object lifecycle expectations.

* PCI device security is a PCI specification [2]. The acronym soup of PCI device security (TDISP, IDE, CMA, SPDM, DOE) is deeply entangled with PCI specifics. If other buses grow the ability to add devices to a confidential VM's TCB that future enabling need not be encumbered by premature adherence to the TDEV+TDI object model, the bus can do what makes sense for its specific mechanisms. The kernel can abstract common attributes and ABI without the burden of a new object model.

[1]: http://lore.kernel.org/[email protected]
[2]: http://lore.kernel.org/[email protected]

Quoted 15 lines

New device nodes provide sysfs interface for fetching device certificates and measurements and TDI interface reports.
Nodes with the "_user" suffix provide human-readable information, without that suffix it is raw binary data to be copied to a guest.

The TSM-HOST module adds hypervisor-only functionality on top. At the moment it is:
- "connect" to enable/disable IDE (a PCI link encryption);
- "TDI bind" to manage a PCI function passed through to a secure VM.

A platform is expected to register itself in TSM-HOST and provide necessary callbacks. No platform is added here, AMD SEV is coming in the next patches.

[..]

Quoted 5 lines
drivers/virt/coco/host/tsm-host.c new file +0 -0
new file mode 100644
index 000000000000..80f3315fb195

[..]

drivers/virt/coco/host/tsm-host.c line 211

Quoted 6 lines
drivers/virt/coco/host/tsm-host.c new file +6 -0
+static ssize_t tsm_dev_status_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+	struct tsm_dev *tdev = container_of(dev, struct tsm_dev, dev);
+	struct tsm_dev_status s = { 0 };
+	int ret = tsm_dev_status(tdev, &s);
+	ssize_t ret1;

I know this is just an RFC, but...

drivers/virt/coco/host/tsm-host.c line 213

Quoted 2 lines
drivers/virt/coco/host/tsm-host.c new file +2 -0
+
+	ret1 = sysfs_emit(buf, "ret=%d\n"

What does "ret" mean to userspace?

drivers/virt/coco/host/tsm-host.c line 214

Quoted 1 line
drivers/virt/coco/host/tsm-host.c new file +1 -0
+			  "ctx_state=%x\n"

This violates the one property per file sysfs expectation.

drivers/virt/coco/host/tsm-host.c line 215

Quoted 1 line
drivers/virt/coco/host/tsm-host.c new file +1 -0
+			  "tc_mask=%x\n"

Is this the Link IDE traffic class?

drivers/virt/coco/host/tsm-host.c line 218

Quoted 3 lines
drivers/virt/coco/host/tsm-host.c new file +3 -0
+			  "certs_slot=%x\n"
+			  "device_id=%x:%x.%d\n"
+			  "segment_id=%x\n"

These last 2 lines are all redundant information relative to the PCI device name, right?

drivers/virt/coco/host/tsm-host.c line 230

Quoted 12 lines
drivers/virt/coco/host/tsm-host.c new file +12 -0
+			  "no_fw_update=%x\n",
+			  ret,
+			  s.ctx_state,
+			  s.tc_mask,
+			  s.certs_slot,
+			  (s.device_id >> 8) & 0xff,
+			  (s.device_id >> 3) & 0x1f,
+			  s.device_id & 0x07,
+			  s.segment_id,
+			  s.no_fw_update);
+
+	tsm_dev_put(tdev);

I would not expect sysfs to need to manage device references. If the device is registered sysfs is live and the reference is already elevated. If the device is unregistered, sysfs is disabled and attribute handlers are no longer executing.

[..]

Quoted 48 lines
drivers/virt/coco/host/tsm-host.c new file +48 -0
+static ssize_t tsm_tdi_status_user_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct tsm_tdi *tdi = container_of(dev, struct tsm_tdi, dev);
+	struct tsm_dev *tdev = tdi->tdev;
+	struct tsm_host_subsys *hsubsys = (struct tsm_host_subsys *) tdev->tsm;
+	struct tsm_tdi_status ts = { 0 };
+	char algos[256] = "";
+	unsigned int n, m;
+	int ret;
+
+	ret = tsm_tdi_status(tdi, hsubsys->private_data, &ts);
+	if (ret < 0)
+		return sysfs_emit(buf, "ret=%d\n\n", ret);
+
+	if (!ts.valid)
+		return sysfs_emit(buf, "ret=%d\nstate=%d:%s\n",
+				  ret, ts.state, tdisp_state_to_str(ts.state));
+
+	n = snprintf(buf, PAGE_SIZE,
+		     "ret=%d\n"
+		     "state=%d:%s\n"
+		     "meas_digest_fresh=%x\n"
+		     "meas_digest_valid=%x\n"
+		     "all_request_redirect=%x\n"
+		     "bind_p2p=%x\n"
+		     "lock_msix=%x\n"
+		     "no_fw_update=%x\n"
+		     "cache_line_size=%d\n"
+		     "algos=%#llx:%s\n"
+		     "report_counter=%lld\n"
+		     ,
+		     ret,
+		     ts.state, tdisp_state_to_str(ts.state),
+		     ts.meas_digest_fresh,
+		     ts.meas_digest_valid,
+		     ts.all_request_redirect,
+		     ts.bind_p2p,
+		     ts.lock_msix,
+		     ts.no_fw_update,
+		     ts.cache_line_size,
+		     ts.spdm_algos, spdm_algos_to_str(ts.spdm_algos, algos, sizeof(algos) - 1),
+		     ts.intf_report_counter);
+
+	n += snprintf(buf + n, PAGE_SIZE - n, "Certs digest: ");
+	m = hex_dump_to_buffer(ts.certs_digest, sizeof(ts.certs_digest), 32, 1,
+			       buf + n, PAGE_SIZE - n, false);

drivers/virt/coco/host/tsm-host.c line 357

Quoted 10 lines
drivers/virt/coco/host/tsm-host.c new file +10 -0
+	n += min(PAGE_SIZE - n, m);
+	n += snprintf(buf + n, PAGE_SIZE - n, "...\nMeasurements digest: ");
+	m = hex_dump_to_buffer(ts.meas_digest, sizeof(ts.meas_digest), 32, 1,
+			       buf + n, PAGE_SIZE - n, false);
+	n += min(PAGE_SIZE - n, m);
+	n += snprintf(buf + n, PAGE_SIZE - n, "...\nInterface report digest: ");
+	m = hex_dump_to_buffer(ts.interface_report_digest, sizeof(ts.interface_report_digest),
+			       32, 1, buf + n, PAGE_SIZE - n, false);
+	n += min(PAGE_SIZE - n, m);
+	n += snprintf(buf + n, PAGE_SIZE - n, "...\n");

More sysfs expectation violations...

Let's start working on what the Plumbers feedback to Lukas on his attempt to export PCI CMA device evidence through sysfs means for the TSM side. I do not expect it will all be strictly reusable but the transport and some record formats should be unified. Specifically I want to start the discussion about collaboration and differences among PCI-CMA-netlink and PCI-TSM-netlink. For example, device measurements are ostensibly common, but interface reports are unique to the TSM flow.

author maintainer
#71 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

In reply to Xu Yilun
Quoted 5 lines · Xu Yilun
Quoted 5 lines
drivers/iommu/iommufd/viommu.c +5 -0
+int iommufd_vdevice_tsm_bind_ioctl(struct iommufd_ucmd *ucmd)
+{
+	struct iommu_vdevice_tsm_bind *cmd = ucmd->cmd;
+	struct iommufd_viommu *viommu;
+	struct iommufd_vdevice *vdev;
Quoted 8 lines

drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 178

Quoted 5 lines
drivers/iommu/iommufd/viommu.c +5 -0
+	struct iommufd_device *idev;
+	struct tsm_tdi *tdi;
+	int rc = 0;
+
+	viommu = iommufd_get_viommu(ucmd, cmd->viommu_id);

Why need user to input viommu_id? And why get viommu here? The viommu is always available after vdevice is allocated, is it?

I thought it may be a good idea to hold a reference while doing tsm_tdi_bind(), likely not needed.

Quoted 23 lines

int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd)

{
	...

	vdev->viommu = viommu;
	refcount_inc(&viommu->obj.users);
	...
}

drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 191

Quoted 13 lines
drivers/iommu/iommufd/viommu.c +13 -0
+	if (IS_ERR(viommu))
+		return PTR_ERR(viommu);
+
+	idev = iommufd_get_device(ucmd, cmd->dev_id);
+	if (IS_ERR(idev)) {
+		rc = PTR_ERR(idev);
+		goto out_put_viommu;
+	}
+
+	vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id,
+					       IOMMUFD_OBJ_VDEVICE),
+			    struct iommufd_vdevice, obj);
+	if (IS_ERR(idev)) {
                    ^
Quoted 1 line

vdev?

yes.

Quoted 9 lines

drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 196

Quoted 6 lines
drivers/iommu/iommufd/viommu.c +5 -0
+		rc = PTR_ERR(idev);
+		goto out_put_dev;
+	}
+
+	tdi = tsm_tdi_get(idev->dev);

And do we still need dev_id for the struct device *? vdevice also has this info.

Oh, likely no. Probably leftover from multiple rebases, or me not fully following what nature these IDs are of (some are just numbers, some are guest bdfn).

Quoted 26 lines

int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd)

{
         ...
	vdev->dev = idev->dev;
	get_device(idev->dev);
         ...
}
Quoted 17 lines
drivers/iommu/iommufd/viommu.c +17 -0
+	if (!tdi) {
+		rc = -ENODEV;
+		goto out_put_vdev;
+	}
+
+	rc = tsm_tdi_bind(tdi, vdev->id, cmd->kvmfd);
+	if (rc)
+		goto out_put_tdi;
+
+	vdev->tsm_bound = true;
+
+	rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));
+out_put_tdi:
+	tsm_tdi_put(tdi);
+out_put_vdev:
+	iommufd_put_object(ucmd->ictx, &vdev->obj);
+out_put_dev:
Quoted 8 lines

drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 218

Quoted 5 lines
drivers/iommu/iommufd/viommu.c +5 -0
+	iommufd_put_object(ucmd->ictx, &idev->obj);
+out_put_viommu:
+	iommufd_put_object(ucmd->ictx, &viommu->obj);
+	return rc;
+}

Another concern is do we need an unbind ioctl? We don't bind on vdevice create so it seems not symmetrical we only unbind on vdevice destroy.

I'll add it as we progress. Just for now I have no flow to exercise it - I accept the device into my SNP VM and that's it but if something in the VM is unhappy about the device report, then we'll need to unbind and continue using the device as untrusted. Thanks,

(sorry for late response, still going through all comments here and in Dan's threads)

Quoted 5 lines

Thanks,
Yilun

Signature · 1 line
Alexey
author maintainer
#13 lore raw

Re: [RFC PATCH v2 06/22] KVM: X86: Define tsm_get_vmid

In reply to Dan Williams
Quoted 84 lines · Dan Williams
Quoted 83 lines · Alexey Kardashevskiy
Quoted 80 lines · Dan Williams
Quoted 64 lines · Alexey Kardashevskiy

In order to add a PCI VF into a secure VM, the TSM module needs to perform a "TDI bind" operation. The secure module ("PSP" for AMD) reuqires a VM id to associate with a VM and KVM has it. Since KVM cannot directly bind a TDI (as it does not have all necesessary data such as host/guest PCI BDFn). QEMU and IOMMUFD do know the BDFns but they do not have a VM id recognisable by the PSP.

Add get_vmid() hook to KVM. Implement it for AMD SEV to return a sum of GCTX (a private page describing secure VM context) and ASID (required on unbind for IOMMU unfencing, when needed).

---
   arch/x86/include/asm/kvm-x86-ops.h |  1 +
   arch/x86/include/asm/kvm_host.h    |  2 ++
   include/linux/kvm_host.h           |  2 ++
   arch/x86/kvm/svm/svm.c             | 12 ++++++++++++
   virt/kvm/kvm_main.c                |  6 ++++++
   5 files changed, 23 insertions(+)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
index c35550581da0..63102a224cd7 100644
@@ -144,6 +144,7 @@ KVM_X86_OP_OPTIONAL(alloc_apic_backing_page)
   KVM_X86_OP_OPTIONAL_RET0(gmem_prepare)
   KVM_X86_OP_OPTIONAL_RET0(private_max_mapping_level)
   KVM_X86_OP_OPTIONAL(gmem_invalidate)
+KVM_X86_OP_OPTIONAL(tsm_get_vmid)
 
   #undef KVM_X86_OP
   #undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +2 -0
index b15cde0a9b5c..9330e8d4d29d 100644
@@ -1875,6 +1875,8 @@ struct kvm_x86_ops {
   	int (*gmem_prepare)(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order);
   	void (*gmem_invalidate)(kvm_pfn_t start, kvm_pfn_t end);
   	int (*private_max_mapping_level)(struct kvm *kvm, kvm_pfn_t pfn);
+
+	u64 (*tsm_get_vmid)(struct kvm *kvm);
   };
 
   struct kvm_x86_nested_ops {
include/linux/kvm_host.h +2 -0
index f34f4cfaa513..6cd351edb956 100644
@@ -2571,4 +2571,6 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,
   				    struct kvm_pre_fault_memory *range);
   #endif
 
+u64 kvm_arch_tsm_get_vmid(struct kvm *kvm);
+
   #endif
arch/x86/kvm/svm/svm.c +0 -0
index 7640a84e554a..0276d60c61d6 100644
@@ -4998,6 +4998,16 @@ static void *svm_alloc_apic_backing_page(struct kvm_vcpu *vcpu)
   	return page_address(page);
   }
 

arch/x86/kvm/svm/svm.c static void *svm_alloc_apic_backing_page(struct kvm_vcpu *vcpu) · line 5010

Quoted 10 lines
arch/x86/kvm/svm/svm.c +10 -0
+static u64 svm_tsm_get_vmid(struct kvm *kvm)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+
+	if (!sev->es_active)
+		return 0;
+
+	return ((u64) sev->snp_context) | sev->asid;
+}
+

Curious why KVM needs to be bothered by a new kvm_arch_tsm_get_vmid() and a vendor specific cookie "vmid" concept. In other words KVM never calls kvm_arch_tsm_get_vmid(), like other kvm_arch_*() support calls.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Is this due to a restriction that something like tsm_tdi_bind() is disallowed from doing to_kvm_svm() on an opaque @kvm pointer? Or otherwise asking an arch/x86/kvm/svm/svm.c to do the same?

I saw someone already doing some sort of VMID thing

Reference?

Cannot find it now, RiscV and ARM have this concept but internally and it probably should stay this way.

Quoted 10 lines
Quoted 1 line

and thought it is a good way of not spilling KVM details outside KVM.

...but it is not a KVM detail. It is an arch specific TSM cookie derived from arch specific data that wraps 'struct kvm'. Now if the rationale is some least privelege concern about what code can have a container_of() relationship with an opaque 'struct kvm *' pointer, let's have that discussion. As it stands nothing in KVM cares about kvm_arch_tsm_get_vmid(), and I expect 'vmid' does not cover all the ways in which modular TSM drivers may interact with arch/.../kvm/ code.

Quoted 3 lines

For example TDX Connect needs to share some data from 'struct kvm_tdx', and it does that with an export from arch/x86/kvm/vmx/tdx.c, not an indirection through virt/kvm/kvm_main.c.

a) KVM-AMD uses CCP's exports now, and if I add exports to KVM-AMD for CCP - it is cross-reference so I'll need what, KVM-AMD-TIO module, to untangle this?

b) I could include arch/x86/kvm/svm/svm.h in drivers/crypto/ccp/ which is... meh?

c) Or move parts of struct kvm_sev_info/kvm_svm from arch/x86/kvm/svm/svm.h to arch/x86/include/asm/svm.h and do some trick to get kvm_sev_info from struct kvm.

d) In my RFC v1, I simply called tsm_tdi_bind() from KVM-AMD with this cookie but that assumed KVM knowledge of PCI which I dropped in this RFC so the bind request travels via QEMU between the guest and the PSP.

All doable though.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Effectively low level TSM drivers are extensions of arch code that routinely performs "container_of(kvm, struct kvm_$arch, kvm)".

The arch code is CCP and so far it avoided touching KVM, KVM calls CCP when it needs but not vice versa. Thanks,

Right, and the observation is that you don't need to touch virt/kvm/kvm_main.c at all to meet this data sharing requirement.

These are all valid points. I like neither of a)..d) in particular and I am AMD-centric (as you correctly noticed :) ) and for this exercise I only needed kvmfd->guest_context_page, hence this proposal. Thanks,

Signature · 1 line
Alexey
#24 lore raw

Re: [RFC PATCH v2 10/22] KVM: SVM: Add uAPI to change RMP for MMIO

In reply to Alexey Kardashevskiy
Quoted 10 lines · Alexey Kardashevskiy

The TDI bind operation moves the TDI into "RUN" state which means that TEE resources are now to be used as encrypted, or the device will refuse to operate. This requires RMP setup for MMIO BARs which is done in 2 steps:
- RMPUPDATE on the host to assign host's MMIO ranges to GPA (like RAM);
- validate the RMP entry which is done via TIO GUEST REQUEST GHCB message (unlike RAM for which the VM could just call PVALIDATE) but TDI bind must complete first to ensure the TDI is in the LOCKED state so the location of MMIO is fixed.

Quoted 8 lines

The bind happens on the first TIO GUEST REQUEST from the guest. At this point KVM does not have host TDI BDFn so it exits to QEMU which calls VFIO-IOMMUFD to bind the TDI.

Now, RMPUPDATE need to be done, in some place on the way back to the guest. Possible places are:
a) the VFIO-IOMMUFD bind handler (does not know GPAs);
b) QEMU (can mmapp MMIO and knows GPA);

Given the guest_memfd momentum to keep private memory unmapped from the host side do you expect to align with the DMABUF effort [1] to teach KVM about convertible MMIO where the expectation is that convertible MMIO need never be mmapped on the host side?

[1]: http://lore.kernel.org/[email protected]

#67 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

In reply to Jason Gunthorpe
Quoted 11 lines · Jason Gunthorpe
Quoted 9 lines · Alexey Kardashevskiy
Quoted 3 lines

I still have concern about the vdevice interface for bind. Bind put the device to LOCKED state, so is more of a device configuration rather than an iommu configuration. So seems more reasonable put the API in VFIO?

IOMMUFD means pretty much VFIO (in the same way "VFIO means KVM" as 95+% of VFIO users use it from KVM, although VFIO works fine without KVM) so not much difference where to put this API and can be done either way. VFIO is reasonable, the immediate problem is that IOMMUFD's vIOMMU knows the guest BDFn (well, for AMD) and VFIO PCI does not.

Quoted 3 lines

I would re-enforce what I said before, VFIO & iommufd alone should be able to operate a TDISP device and get device encrpytion without requiring KVM.

Without requiring KVM, but still requiring a TVM context per TDISP expectations?

I.e. I am still trying to figure out if you are talking about device-authentication and encryption without KVM, TDISP without a TVM (not sure what that is), or TDISP state management relative to a shared concept of a "TVM context" that KVM also references.

Quoted 3 lines

It makes sense that if the secure firmware object handles (like the viommu, vdevice, vBDF) are accessed through iommufd then iommufd will relay operations against those handles.

Yes, that tracks.

author maintainer
#68 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

Quoted 16 lines · Dan Williams
Quoted 11 lines · Jason Gunthorpe
Quoted 9 lines · Alexey Kardashevskiy
Quoted 3 lines

I still have concern about the vdevice interface for bind. Bind put the device to LOCKED state, so is more of a device configuration rather than an iommu configuration. So seems more reasonable put the API in VFIO?

IOMMUFD means pretty much VFIO (in the same way "VFIO means KVM" as 95+% of VFIO users use it from KVM, although VFIO works fine without KVM) so not much difference where to put this API and can be done either way. VFIO is reasonable, the immediate problem is that IOMMUFD's vIOMMU knows the guest BDFn (well, for AMD) and VFIO PCI does not.

Quoted 3 lines

I would re-enforce what I said before, VFIO & iommufd alone should be able to operate a TDISP device and get device encrpytion without requiring KVM.

Quoted 6 lines

Without requiring KVM, but still requiring a TVM context per TDISP expectations?

I.e. I am still trying to figure out if you are talking about device-authentication and encryption without KVM, TDISP without a TVM (not sure what that is),

It is about accessing MMIO with Cbit set, and running DMA to/from private memory, in DPDK. On AMD, if we want the PCI's Tbit in MMIO, the Cbit needs to be set in some page table. "TVM" in this case is a few private pages (+ bunch of PSP calls to bring to the right state) pretending to be a CVM which does not need to run. Thanks,

Quoted 8 lines

or TDISP state management relative to a
shared concept of a "TVM context" that KVM also references.

Quoted 3 lines

It makes sense that if the secure firmware object handles (like the viommu, vdevice, vBDF) are accessed through iommufd then iommufd will relay operations against those handles.

Yes, that tracks.

Signature · 1 line
Alexey
#38 lore raw

Re: [RFC PATCH v2 12/22] iommufd: Allow mapping from guest_memfd

In reply to Alexey Kardashevskiy
Quoted 3 lines · Alexey Kardashevskiy

About this atomical restructure - I looked at yours iommu-pt branch on github but __cut_mapping()->pt_table_install64() only atomically swaps the PDE but it does not do IOMMU TLB invalidate, have I missed it?

That branch doesn't have the invalidation wired in, there is another branch that has invalidation but not cut yet.. It is a journey

Quoted 3 lines

And if it did so, that would not be atomic but it won't matter as long as we do not destroy the old PDE before invalidating IOMMU TLB, is this the idea? Thanks,

When splitting the change in the PDE->PTE doesn't change the translation in effect.

So if the IOTLB has cached the PDE, the SW will update it to an array of PTEs of same address, any concurrent DMA will continue to hit the same address, then when we invalidate the IOTLB the PDE will get dropped from cache and the next DMA will load PTEs.

When I say atomic I mean from the perspective of the DMA initator there is no visible alteration. Perhaps I should say hitless.

Jason

#7 lore raw

Re: [RFC PATCH v2 05/22] crypto: ccp: Enable SEV-TIO feature in the PSP when supported

In reply to Alexey Kardashevskiy
Quoted 6 lines · Alexey Kardashevskiy
include/linux/psp-sev.h +2 -0
@@ -601,6 +603,25 @@ struct sev_data_snp_addr {
 	u64 address;				/* In/Out */
 } __packed;
 
+/**
+ * struct sev_data_snp_feature_info - SEV_CMD_SNP_FEATURE_INFO
Quoted 8 lines

command params
+ *
+ * @len: length of this struct
+ * @ecx_in: subfunction index of CPUID Fn8000_0024
+ * @feature_info_paddr: physical address of a page with sev_snp_feature_info
+ */
+#define SNP_FEATURE_FN8000_0024_EBX_X00_SEVTIO 1

According to the SNP firmware ABI spec, support for SEV TIO commands is indicated by bit 1 (bit 0 is for SEV legacy commands).

drivers/crypto/ccp/sev-dev.c int sev_snp_guest_decommission(int asid, int *psp_ret) · line 1278

Quoted 10 lines

+static int snp_get_feature_info(struct sev_device *sev, u32 ecx, struct sev_snp_feature_info *fi)

drivers/crypto/ccp/sev-dev.c +8 -0
+{
+	struct sev_user_data_snp_status status = { 0 };
+	int psp_ret = 0, ret;
+
+	ret = snp_platform_status_locked(sev, &status, &psp_ret);
+	if (ret)
+		return ret;
+	if (ret != SEV_RET_SUCCESS)

s/ret/psp_ret/

drivers/crypto/ccp/sev-dev.c int sev_snp_guest_decommission(int asid, int *psp_ret) · line 1286

Quoted 8 lines
drivers/crypto/ccp/sev-dev.c +8 -0
+		return -EFAULT;
+	if (!status.feature_info)
+		return -ENOENT;
+
+	ret = snp_feature_info_locked(sev, ecx, fi, &psp_ret);
+	if (ret)
+		return ret;
+	if (ret != SEV_RET_SUCCESS)

Same here

#22 lore raw

Re: [RFC PATCH v2 09/22] crypto/ccp: Implement SEV TIO firmware interface

In reply to Alexey Kardashevskiy
Quoted 36 lines · Alexey Kardashevskiy
drivers/crypto/ccp/sev-dev-tio.c new file +19 -0
new file mode 100644
index 000000000000..bd55ad6c5fb3
@@ -0,0 +1,1664 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+// Interface to PSP for CCP/SEV-TIO/SNP-VM
+
+#include <linux/pci.h>
+#include <linux/tsm.h>
+#include <linux/psp.h>
+#include <linux/file.h>
+#include <linux/vmalloc.h>
+
+#include <asm/sev-common.h>
+#include <asm/sev.h>
+#include <asm/page.h>
+
+#include "psp-dev.h"
+#include "sev-dev.h"
+#include "sev-dev-tio.h"
+
+static void *__prep_data_pg(struct tsm_dev_tio *dev_data, size_t

len)

drivers/crypto/ccp/sev-dev-tio.c new file +9 -0
+{
+	void *r = dev_data->data_pg;
+
+	if (snp_reclaim_pages(virt_to_phys(r), 1, false))
+		return NULL;
+
+	memset(r, 0, len);
+
+	if (rmp_make_private(page_to_pfn(virt_to_page(r)), 0,
Quoted 1 line

PG_LEVEL_4K, 0, true))

We have virt_to_pfn().

Quoted 12 lines
drivers/crypto/ccp/sev-dev-tio.c new file +12 -0
+static struct sla_addr_t sla_alloc(size_t len, bool firmware_state)
+{
+	unsigned long i, npages = PAGE_ALIGN(len) >> PAGE_SHIFT;
+	struct sla_addr_t *scatter = NULL;
+	struct sla_addr_t ret = SLA_NULL;
+	struct sla_buffer_hdr *buf;
+	struct page *pg;
+
+	if (npages == 0)
+		return ret;
+
+	if (WARN_ON_ONCE(npages > ((PAGE_SIZE / sizeof(struct
Quoted 1 line

sla_addr_t)) + 1)))

This should be (npages + 1 > (...)), because we need to fit npages SLAs plus the final SLA_EOL.

Quoted 30 lines
drivers/crypto/ccp/sev-dev-tio.c new file +4 -0
+/* Expands a buffer, only firmware owned buffers allowed for now */
+static int sla_expand(struct sla_addr_t *sla, size_t *len)
+{
+	struct sla_buffer_hdr *oldbuf = sla_buffer_map(*sla),

*newbuf;

drivers/crypto/ccp/sev-dev-tio.c new file +9 -0
+	struct sla_addr_t oldsla = *sla, newsla;
+	size_t oldlen = *len, newlen;
+
+	if (!oldbuf)
+		return -EFAULT;
+
+	newlen = oldbuf->capacity_sz;
+	if (oldbuf->capacity_sz == oldlen) {
+		/* This buffer does not require expansion, must be

another buffer */

drivers/crypto/ccp/sev-dev-tio.c new file +5 -0
+		sla_buffer_unmap(oldsla, oldbuf);
+		return 1;
+	}
+
+	pr_notice("Expanding BUFFER from %ld to %ld bytes\n",

oldlen, newlen);

drivers/crypto/ccp/sev-dev-tio.c new file +9 -0
+
+	newsla = sla_alloc(newlen, true);
+	if (IS_SLA_NULL(newsla))
+		return -ENOMEM;
+
+	newbuf = sla_buffer_map(newsla);
+	if (!newbuf) {
+		sla_free(newsla, newlen, true);
+		return -EFAULT;

drivers/crypto/ccp/sev-dev-tio.c line 817

Quoted 10 lines
drivers/crypto/ccp/sev-dev-tio.c new file +10 -0
+	}
+
+	memcpy(newbuf, oldbuf, oldlen);
+
+	sla_buffer_unmap(newsla, newbuf);
+	sla_free(oldsla, oldlen, true);
+	*sla = newsla;
+	*len = newlen;
+
+	return 0;

Return values are inconsistent with how this function is used in sev_tio_do_cmd(): a zero value should indicate that expansion is not required.

author maintainer
#8 lore raw

Re: [RFC PATCH v2 05/22] crypto: ccp: Enable SEV-TIO feature in the PSP when supported

In reply to Francesco Lavra
Quoted 5 lines · Francesco Lavra
Quoted 4 lines · Alexey Kardashevskiy
include/linux/psp-sev.h +0 -0
@@ -601,6 +603,25 @@ struct sev_data_snp_addr {
  	u64 address;				/* In/Out */
  } __packed;
 
Quoted 13 lines
Quoted 10 lines
(quoted diff) +2 -0
+/**
+ * struct sev_data_snp_feature_info - SEV_CMD_SNP_FEATURE_INFO

command params
+ *
+ * @len: length of this struct
+ * @ecx_in: subfunction index of CPUID Fn8000_0024
+ * @feature_info_paddr: physical address of a page with sev_snp_feature_info
+ */
+#define SNP_FEATURE_FN8000_0024_EBX_X00_SEVTIO 1

According to the SNP firmware ABI spec, support for SEV TIO commands is indicated by bit 1 (bit 0 is for SEV legacy commands).

well, I wanted a bit number (which is 1) but this is wrong nevertheless:

present = (fi.ebx & SNP_FEATURE_FN8000_0024_EBX_X00_SEVTIO) != 0;

should be:

present = (fi.ebx & BIT(SNP_FEATURE_FN8000_0024_EBX_X00_SEVTIO)) != 0;

good spotting!

Quoted 13 lines

drivers/crypto/ccp/sev-dev.c int sev_snp_guest_decommission(int asid, int *psp_ret) · line 1278

Quoted 11 lines

+static int snp_get_feature_info(struct sev_device *sev, u32 ecx, struct sev_snp_feature_info *fi)

drivers/crypto/ccp/sev-dev.c +8 -0
+{
+	struct sev_user_data_snp_status status = { 0 };
+	int psp_ret = 0, ret;
+
+	ret = snp_platform_status_locked(sev, &status, &psp_ret);
+	if (ret)
+		return ret;
+	if (ret != SEV_RET_SUCCESS)

s/ret/psp_ret/

yeah I noticed this after posting. Thanks,

Quoted 11 lines

drivers/crypto/ccp/sev-dev.c int sev_snp_guest_decommission(int asid, int *psp_ret) · line 1286

Quoted 9 lines
drivers/crypto/ccp/sev-dev.c +8 -0
+		return -EFAULT;
+	if (!status.feature_info)
+		return -ENOENT;
+
+	ret = snp_feature_info_locked(sev, ecx, fi, &psp_ret);
+	if (ret)
+		return ret;
+	if (ret != SEV_RET_SUCCESS)

Same here

Signature · 1 line
Alexey
author maintainer
#25 lore raw

Re: [RFC PATCH v2 10/22] KVM: SVM: Add uAPI to change RMP for MMIO

In reply to Dan Williams
Quoted 20 lines · Dan Williams
Quoted 10 lines · Alexey Kardashevskiy

The TDI bind operation moves the TDI into "RUN" state which means that TEE resources are now to be used as encrypted, or the device will refuse to operate. This requires RMP setup for MMIO BARs which is done in 2 steps:
- RMPUPDATE on the host to assign host's MMIO ranges to GPA (like RAM);
- validate the RMP entry which is done via TIO GUEST REQUEST GHCB message (unlike RAM for which the VM could just call PVALIDATE) but TDI bind must complete first to ensure the TDI is in the LOCKED state so the location of MMIO is fixed.

Quoted 8 lines

The bind happens on the first TIO GUEST REQUEST from the guest. At this point KVM does not have host TDI BDFn so it exits to QEMU which calls VFIO-IOMMUFD to bind the TDI.

Now, RMPUPDATE need to be done, in some place on the way back to the guest. Possible places are:
a) the VFIO-IOMMUFD bind handler (does not know GPAs);
b) QEMU (can mmapp MMIO and knows GPA);

Quoted 4 lines

Given the guest_memfd momentum to keep private memory unmapped from the host side do you expect to align with the DMABUF effort [1] to teach KVM about convertible MMIO where the expectation is that convertible MMIO need never be mmapped on the host side?

Well, I need to do better than the horrendous "[RFC PATCH v2 15/22] KVM: X86: Handle private MMIO as shared" and this one fits the purpose so yes. Thanks,

Quoted 2 lines

[1]: http://lore.kernel.org/[email protected]

Signature · 1 line
Alexey
maintainer
#72 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

In reply to Alexey Kardashevskiy

Alexey Kardashevskiy <[email protected]> writes:

....

drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 180

Quoted 13 lines
drivers/iommu/iommufd/viommu.c +12 -0
+int iommufd_vdevice_tsm_bind_ioctl(struct iommufd_ucmd *ucmd)
+{
+	struct iommu_vdevice_tsm_bind *cmd = ucmd->cmd;
+	struct iommufd_viommu *viommu;
+	struct iommufd_vdevice *vdev;
+	struct iommufd_device *idev;
+	struct tsm_tdi *tdi;
+	int rc = 0;
+
+	viommu = iommufd_get_viommu(ucmd, cmd->viommu_id);
+	if (IS_ERR(viommu))
+		return PTR_ERR(viommu);

Would this require an IOMMU_HWPT_ALLOC_NEST_PARENT page table allocation? How would this work in cases where there's no need to set up Stage 1 IOMMU tables?

Alternatively, should we allocate an IOMMU_HWPT_ALLOC_NEST_PARENT with a Stage 1 disabled translation config? (In the ARM case, this could mean marking STE entries as Stage 1 bypass and Stage 2 translate.)

Also, if a particular setup doesn't require creating IOMMU entries because the entire guest RAM is identity-mapped in the IOMMU, do we still need to make tsm_tdi_bind use this abstraction in iommufd?

Quoted 28 lines
drivers/iommu/iommufd/viommu.c +28 -0
+
+	idev = iommufd_get_device(ucmd, cmd->dev_id);
+	if (IS_ERR(idev)) {
+		rc = PTR_ERR(idev);
+		goto out_put_viommu;
+	}
+
+	vdev = container_of(iommufd_get_object(ucmd->ictx, cmd->vdevice_id,
+					       IOMMUFD_OBJ_VDEVICE),
+			    struct iommufd_vdevice, obj);
+	if (IS_ERR(idev)) {
+		rc = PTR_ERR(idev);
+		goto out_put_dev;
+	}
+
+	tdi = tsm_tdi_get(idev->dev);
+	if (!tdi) {
+		rc = -ENODEV;
+		goto out_put_vdev;
+	}
+
+	rc = tsm_tdi_bind(tdi, vdev->id, cmd->kvmfd);
+	if (rc)
+		goto out_put_tdi;
+
+	vdev->tsm_bound = true;
+
+	rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));

drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 218

Quoted 10 lines
drivers/iommu/iommufd/viommu.c +10 -0
+out_put_tdi:
+	tsm_tdi_put(tdi);
+out_put_vdev:
+	iommufd_put_object(ucmd->ictx, &vdev->obj);
+out_put_dev:
+	iommufd_put_object(ucmd->ictx, &idev->obj);
+out_put_viommu:
+	iommufd_put_object(ucmd->ictx, &viommu->obj);
+	return rc;
+}

-aneesh

#69 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

In reply to Alexey Kardashevskiy
Quoted 5 lines · Alexey Kardashevskiy

It is about accessing MMIO with Cbit set, and running DMA to/from private memory, in DPDK. On AMD, if we want the PCI's Tbit in MMIO, the Cbit needs to be set in some page table. "TVM" in this case is a few private pages (+ bunch of PSP calls to bring to the right state) pretending to be a CVM which does not need to run. Thanks,

Yeah, though that may be infeasible on other platforms. I'm pretty sure ARM and Intel route the Tbit packets directly to their secure worlds so there is no possibility for VFIO to use them without also using the secure world to create a VM.

Maybe AMD is different, IDK.

Jason

#73 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

In reply to Aneesh Kumar K.V
Quoted 8 lines · Aneesh Kumar K.V
Quoted 8 lines
drivers/iommu/iommufd/viommu.c +8 -0
+int iommufd_vdevice_tsm_bind_ioctl(struct iommufd_ucmd *ucmd)
+{
+	struct iommu_vdevice_tsm_bind *cmd = ucmd->cmd;
+	struct iommufd_viommu *viommu;
+	struct iommufd_vdevice *vdev;
+	struct iommufd_device *idev;
+	struct tsm_tdi *tdi;
+	int rc = 0;
Quoted 8 lines

drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 180

Quoted 5 lines
drivers/iommu/iommufd/viommu.c +4 -0
+
+	viommu = iommufd_get_viommu(ucmd, cmd->viommu_id);
+	if (IS_ERR(viommu))
+		return PTR_ERR(viommu);

Would this require an IOMMU_HWPT_ALLOC_NEST_PARENT page table allocation?

Probably. That flag is what forces a S2 page table.

Quoted 2 lines

How would this work in cases where there's no need to set up Stage 1 IOMMU tables?

Either attach the raw HWPT of the IOMMU_HWPT_ALLOC_NEST_PARENT or:

Quoted 3 lines

Alternatively, should we allocate an IOMMU_HWPT_ALLOC_NEST_PARENT with a Stage 1 disabled translation config? (In the ARM case, this could mean marking STE entries as Stage 1 bypass and Stage 2 translate.)

For arm you mean IOMMU_HWPT_DATA_ARM_SMMUV3.. But yes, this can work too and is mandatory if you want the various viommu linked features to work.

Quoted 3 lines

Also, if a particular setup doesn't require creating IOMMU entries because the entire guest RAM is identity-mapped in the IOMMU, do we still need to make tsm_tdi_bind use this abstraction in iommufd?

Even if the viommu will not be exposed to the guest I'm expecting that iommufd will have a viommu object, just not use various features. We are using viommu as the handle for the KVM, vmid and other things that are likely important here.

Jason

#41 lore raw

Re: [RFC PATCH v2 13/22] iommufd: amd-iommu: Add vdevice support

In reply to Alexey Kardashevskiy

include/uapi/linux/iommufd.h struct iommu_fault_alloc { · line 942

Quoted 5 lines · Alexey Kardashevskiy
include/uapi/linux/iommufd.h +1 -0
@@ -939,6 +939,7 @@ struct iommu_fault_alloc {
 enum iommu_viommu_type {
 	IOMMU_VIOMMU_TYPE_DEFAULT = 0,
 	IOMMU_VIOMMU_TYPE_ARM_SMMUV3 = 1,
+	IOMMU_VIOMMU_TYPE_TSM = 2,

This should probably be some kind of AMD_TSM and the driver data blob should carry any additional data needed to create the vIOMMU that is visible to the guest.

drivers/iommu/amd/iommu.c static void set_dte_entry(struct amd_iommu *iommu, · line 2073

Quoted 7 lines
drivers/iommu/amd/iommu.c +2 -1
@@ -2068,7 +2069,18 @@ static void set_dte_entry(struct amd_iommu *iommu,
 		new.data[1] |= DTE_FLAG_IOTLB;
 
 	old_domid = READ_ONCE(dte->data[1]) & DEV_DOMID_MASK;
-	new.data[1] |= domid;
+
+	if (domain->aviommu) {

AMD should be implementing viommu natively without CC as well, try to structure things so it fits together better. This should only trigger for the CC viommu type..

Quoted 14 lines
drivers/iommu/amd/iommu.c +10 -0
+		/*
+		 * This runs when VFIO is bound to a device but TDI is not yet.
+		 * Ideally TSM should change DTE only when TDI is bound.
+		 */
+		dev_info(dev_data->dev, "Skip DomainID=%x and set bit96\n", domid);
+		new.data[1] |= 1ULL << (96 - 64);
+	} else {
+		dev_info(dev_data->dev, "Not skip DomainID=%x and not set bit96\n", domid);
+		new.data[1] |= domid;
+	}
 
 	/*
 	 * Restore cached persistent DTE bits, which can be set by information

drivers/iommu/amd/iommu.c amd_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags, · line 2567

Quoted 7 lines
drivers/iommu/amd/iommu.c +3 -0
@@ -2549,12 +2561,15 @@ amd_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags,
 {
 	struct amd_iommu *iommu = get_amd_iommu_from_dev(dev);
 	const u32 supported_flags = IOMMU_HWPT_ALLOC_DIRTY_TRACKING |
+						IOMMU_HWPT_ALLOC_PASID |
+						IOMMU_HWPT_ALLOC_NEST_PARENT;
+	const u32 supported_flags2 = IOMMU_HWPT_ALLOC_DIRTY_TRACKING |
 						IOMMU_HWPT_ALLOC_PASID;

Just ignore NEST_PARENT? That seems wrong, it should force a V1 page table??

Quoted 6 lines
drivers/iommu/amd/iommu.c +6 -0
+static struct iommufd_viommu *amd_viommu_alloc(struct device *dev,
+					       struct iommu_domain *parent,
+					       struct iommufd_ctx *ictx,
+					       unsigned int viommu_type)
+{
+	struct amd_viommu *aviommu;

drivers/iommu/amd/iommu.c static int amd_iommu_dev_disable_feature(struct device *dev, · line 3067

Quoted 10 lines
drivers/iommu/amd/iommu.c +10 -0
+	struct protection_domain *domain = to_pdomain(parent);
+
+	if (viommu_type != IOMMU_VIOMMU_TYPE_TSM)
+		return ERR_PTR(-EOPNOTSUPP);
+
+	aviommu = iommufd_viommu_alloc(ictx, struct amd_viommu, core, &amd_viommu_ops);
+	if (IS_ERR(aviommu))
+		return ERR_CAST(aviommu);
+
+	aviommu->domain = domain;

This is not OK, the parent domain of the viommu can be used with multiple viommu objects, it can't just have a naked back reference like this.

You can get 1:1 domain objects linked to the viommu by creating the 'S1' type domains, maybe that is what you want here. A special domain type that is TSM that has a special DTE.

Though I'd really rather see the domain attach logic and DTE formation in the AMD driver be fixed up before we made it more complex :\

It would be nice to see normal nesting and viommu support first too :\

Jason

#76 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

In reply to Alexey Kardashevskiy
Quoted 7 lines · Alexey Kardashevskiy

When a TDISP-capable device is passed through, it is configured as a shared device to begin with. Later on when a VM probes the device, detects its TDISP capability (reported via the PCIe ExtCap bit called "TEE-IO"), performs the device attestation and transitions it to a secure state when the device can run encrypted DMA and respond to encrypted MMIO accesses.

Quoted 8 lines

Since KVM is out of the TCB, secure enablement is done in the secure firmware. The API requires PCI host/guest BDFns, a KVM id hence such calls are routed via IOMMUFD, primarily because allowing secure DMA is the major performance bottleneck and it is a function of IOMMU.

Add TDI bind to do the initial binding of a passed through PCI function to a VM. Add a forwarder for TIO GUEST REQUEST. These two call into the TSM which forwards the calls to the PSP.

Can you list here what the basic flow of iommufd calls is to create a CC VM, with no vIOMMU, and a CC capable vPCI device?

I'd like the other arches to review this list and see how their arches fit

Thanks
Jason

author maintainer
#77 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

Quoted 13 lines · Jason Gunthorpe
Quoted 12 lines · Alexey Kardashevskiy

When a TDISP-capable device is passed through, it is configured as a shared device to begin with. Later on when a VM probes the device, detects its TDISP capability (reported via the PCIe ExtCap bit called "TEE-IO"), performs the device attestation and transitions it to a secure state when the device can run encrypted DMA and respond to encrypted MMIO accesses.

Since KVM is out of the TCB, secure enablement is done in the secure firmware. The API requires PCI host/guest BDFns, a KVM id hence such calls are routed via IOMMUFD, primarily because allowing secure DMA is the major performance bottleneck and it is a function of IOMMU.

Quoted 6 lines
Quoted 3 lines

Add TDI bind to do the initial binding of a passed through PCI function to a VM. Add a forwarder for TIO GUEST REQUEST. These two call into the TSM which forwards the calls to the PSP.

Can you list here what the basic flow of iommufd calls is to create a CC VM, with no vIOMMU, and a CC capable vPCI device?

I do this in QEMU in additional to the usual VFIO setup:

iommufd_cdev_autodomains_get() [1]:

1. iommufd_backend_alloc_viommu
2. iommufd_backend_alloc_vdev

kvm_handle_vmgexit_tio_req() in KVM [2]:

1. (IOMMUFD) tio_bind(pdev, kvm_vmfd(kvm_state))
2. (KVM) kvm_set_memory_attributes_private(mmio region)
3. (SEV) sev_ioctl(/dev/sev, KVM_SEV_SNP_MMIO_RMP_UPDATE)
4. (IOMMUFD) tio_guest_request() /* enable DMA/MMIO in secure world */

Quoted 2 lines

I'd like the other arches to review this list and see how their arches fit

Well, I have it all here: https://github.com/aik/qemu/tree/tsm
Raw stuff so I did not post it even as RFC but may be it'd help if I did? Thanks,

[1] https://github.com/aik/qemu/commit/da86ba11e71f10d48dd40a8d71a2ff595f04bb2d
[2] https://github.com/aik/qemu/commit/f804b65aff5b28f6f0430a5abca07cbac73f70bc

Signature · 1 line
Alexey
#82 lore raw

Re: [RFC PATCH v2 16/22] coco/tsm: Add tsm-guest module

In reply to Alexey Kardashevskiy
Quoted 34 lines · Alexey Kardashevskiy
drivers/virt/coco/guest/tsm-guest.c new file +19 -0
new file mode 100644
index 000000000000..d3be089308e0
@@ -0,0 +1,291 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/module.h>
+#include <linux/tsm.h>
+
+#define DRIVER_VERSION	"0.1"
+#define DRIVER_AUTHOR	"[email protected]"
+#define DRIVER_DESC	"TSM guest library"
+
+struct tsm_guest_subsys {
+	struct tsm_subsys base;
+	struct tsm_vm_ops *ops;
+	void *private_data;
+	struct notifier_block notifier;
+};
+
+static int tsm_tdi_measurements_locked(struct tsm_dev *tdev)
+{
+	struct tsm_guest_subsys *gsubsys = (struct tsm_guest_subsys

*) tdev->tsm;

drivers/virt/coco/guest/tsm-guest.c new file +7 -0
+	struct tsm_tdi_status tstmp = { 0 };
+	struct tsm_tdi *tdi = tsm_tdi_get(tdev->physdev);
+
+	if (!tdi)
+		return -EFAULT;
+
+	return gsubsys->ops->tdi_status(tdi, gsubsys->private_data,
Quoted 1 line

&tstmp);

Missing call to tsm_tdi_put().

#84 lore raw

Re: [RFC PATCH v2 17/22] resource: Mark encrypted MMIO resource on validation

In reply to Alexey Kardashevskiy
Quoted 7 lines · Alexey Kardashevskiy
include/linux/ioport.h +0 -0
index 5385349f0b8a..f2e0b9f02373 100644
@@ -55,6 +55,7 @@ struct resource {
 #define IORESOURCE_MEM_64	0x00100000
 #define IORESOURCE_WINDOW	0x00200000	/* forwarded by
Quoted 5 lines

bridge */
#define IORESOURCE_MUXED 0x00400000 /* Resource is
software muxed */
+#define IORESOURCE_VALIDATED 0x00800000 /* TDISP validated
*/

You may want to remove the reference to TDISP, as this flag could be reused for non-PCI devices in the future.

Quoted 6 lines
include/linux/ioport.h +1 -0
@@ -1085,6 +1092,47 @@ int adjust_resource(struct resource *res,
 resource_size_t start,
 }
 EXPORT_SYMBOL(adjust_resource);
 
+int encrypt_resource(struct resource *res, unsigned int flags)

include/linux/ioport.h int adjust_resource(struct resource *res, · line 1106

Quoted 10 lines
include/linux/ioport.h +10 -0
+{
+	struct resource *p;
+	int result = 0;
+
+	if (!res)
+		return -EINVAL;
+
+	write_lock(&resource_lock);
+
+	for_each_resource(&iomem_resource, p, false) {

I don't think this function should walk the iomem_resource list, it can simply modify res->flags, which is consistent with what is done by the other *_resource() functions that take a pointer to a resource that is expected to be in the list.
Also, the name of this function is unrelated to the name of the affected flag, you may want to make these names more consistent.

#86 lore raw

Re: [RFC PATCH v2 18/22] coco/sev-guest: Implement the guest support for SEV TIO

In reply to Alexey Kardashevskiy
Quoted 33 lines · Alexey Kardashevskiy

+static int handle_tio_guest_request(struct snp_guest_dev *snp_dev, u8 type,

(quoted diff) +1 -0
+				   void *req_buf, size_t req_sz,

void *resp_buf, u32 resp_sz,

(quoted diff) +1 -0
+				   void *pt, u64 *npages, u64 *bdfn,

u64 *param, u64 *fw_err)

drivers/virt/coco/sev-guest/sev_guest_tio.c new file +26 -0
+{
+	struct snp_msg_desc *mdesc = snp_dev->msg_desc;
+	struct snp_guest_req req = {
+		.msg_version = TIO_MESSAGE_VERSION,
+	};
+	u64 exitinfo2 = 0;
+	int ret;
+
+	req.msg_type = type;
+	req.vmpck_id = mdesc->vmpck_id;
+	req.req_buf = req_buf;
+	req.req_sz = req_sz;
+	req.resp_buf = resp_buf;
+	req.resp_sz = resp_sz;
+	req.exit_code = SVM_VMGEXIT_SEV_TIO_GUEST_REQUEST;
+
+	req.input.guest_rid = 0;
+	req.input.param = 0;
+
+	if (pt && npages) {
+		req.data = pt;
+		req.input.data_npages = *npages;
+	}
+	if (bdfn)
+		req.input.guest_rid = *bdfn;
+	if (param)

drivers/virt/coco/sev-guest/sev_guest_tio.c line 103

Quoted 10 lines
drivers/virt/coco/sev-guest/sev_guest_tio.c new file +10 -0
+		req.input.param = *param;
+
+	ret = snp_send_guest_request(mdesc, &req, &exitinfo2);
+
+	if (param)
+		*param = req.input.param;
+
+	*fw_err = exitinfo2;
+
+	return ret;

The logic to update *npages is missing.

Quoted 27 lines
arch/x86/coco/sev/core.c +3 -0
+}
+
+static int guest_request_tio_data(struct snp_guest_dev *snp_dev, u8

type,

(quoted diff) +1 -0
+				   void *req_buf, size_t req_sz,

void *resp_buf, u32 resp_sz,

(quoted diff) +1 -0
+				   u64 bdfn, enum tsm_tdisp_state

*state,

(quoted diff) +1 -0
+				   struct tsm_blob **certs, struct

tsm_blob **meas,

(quoted diff) +1 -0
+				   struct tsm_blob **report, u64

*fw_err)

drivers/virt/coco/sev-guest/sev_guest_tio.c new file +11 -0
+{
+	u64 npages = SZ_32K >> PAGE_SHIFT, c1, param = 0;
+	struct tio_blob_table_entry *pt;
+	int rc;
+
+	pt = snp_alloc_shared_pages(npages << PAGE_SHIFT);
+	if (!pt)
+		return -ENOMEM;
+
+	c1 = npages;
+	rc = handle_tio_guest_request(snp_dev, type, req_buf,

req_sz, resp_buf, resp_sz,

(quoted diff) +1 -0
+				      pt, &c1, &bdfn, state ? &param

: NULL, fw_err);

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	if (c1 > SZ_32K) {

c1 is supposed to be a number of pages, not a number of bytes.

Quoted 70 lines

+static int tio_tdi_status(struct tsm_tdi *tdi, struct snp_guest_dev *snp_dev,

drivers/virt/coco/sev-guest/sev_guest_tio.c new file +4 -0
+			  struct tsm_tdi_status *ts)
+{
+	struct snp_msg_desc *mdesc = snp_dev->msg_desc;
+	size_t resp_len = sizeof(struct tio_msg_tdi_info_rsp) +

mdesc->ctx->authsize;

(quoted diff) +1 -0
+	struct tio_msg_tdi_info_rsp *rsp = kzalloc(resp_len,

GFP_KERNEL);

drivers/virt/coco/sev-guest/sev_guest_tio.c new file +12 -0
+	struct tio_msg_tdi_info_req req = {
+		.guest_device_id = pci_dev_id(tdi_to_pci_dev(tdi)),
+	};
+	u64 fw_err = 0;
+	int rc;
+	enum tsm_tdisp_state state = 0;
+
+	dev_notice(&tdi->dev, "TDI info");
+	if (!rsp)
+		return -ENOMEM;
+
+	rc = guest_request_tio_data(snp_dev, TIO_MSG_TDI_INFO_REQ,

&req,

drivers/virt/coco/sev-guest/sev_guest_tio.c new file +2 -0
+				     sizeof(req), rsp, resp_len,
+				    

pci_dev_id(tdi_to_pci_dev(tdi)), &state,

(quoted diff) +1 -0
+				     &tdi->tdev->certs, &tdi->tdev-
Quoted 1 line

meas,

drivers/virt/coco/sev-guest/sev_guest_tio.c new file +20 -0
+				     &tdi->report, &fw_err);
+	if (rc)
+		goto free_exit;
+
+	ts->meas_digest_valid = rsp->meas_digest_valid;
+	ts->meas_digest_fresh = rsp->meas_digest_fresh;
+	ts->no_fw_update = rsp->no_fw_update;
+	ts->cache_line_size = rsp->cache_line_size == 0 ? 64 : 128;
+	ts->lock_msix = rsp->lock_msix;
+	ts->bind_p2p = rsp->bind_p2p;
+	ts->all_request_redirect = rsp->all_request_redirect;
+#define __ALGO(x, n, y) \
+	((((x) & (0xFFUL << (n))) == TIO_SPDM_ALGOS_##y) ? \
+	 (1ULL << TSM_SPDM_ALGOS_##y) : 0)
+	ts->spdm_algos =
+		__ALGO(rsp->spdm_algos, 0, DHE_SECP256R1) |
+		__ALGO(rsp->spdm_algos, 0, DHE_SECP384R1) |
+		__ALGO(rsp->spdm_algos, 8, AEAD_AES_128_GCM) |
+		__ALGO(rsp->spdm_algos, 8, AEAD_AES_256_GCM) |
+		__ALGO(rsp->spdm_algos, 16,

ASYM_TPM_ALG_RSASSA_3072) |

(quoted diff) +1 -0
+		__ALGO(rsp->spdm_algos, 16,

ASYM_TPM_ALG_ECDSA_ECC_NIST_P256) |

(quoted diff) +1 -0
+		__ALGO(rsp->spdm_algos, 16,

ASYM_TPM_ALG_ECDSA_ECC_NIST_P384) |

drivers/virt/coco/sev-guest/sev_guest_tio.c new file +3 -0
+		__ALGO(rsp->spdm_algos, 24, HASH_TPM_ALG_SHA_256) |
+		__ALGO(rsp->spdm_algos, 24, HASH_TPM_ALG_SHA_384) |
+		__ALGO(rsp->spdm_algos, 32,

KEY_SCHED_SPDM_KEY_SCHEDULE);

(quoted diff) +2 -0
+#undef __ALGO
+	memcpy(ts->certs_digest, rsp->certs_digest, sizeof(ts-
Quoted 1 line

certs_digest));

(quoted diff) +1 -0
+	memcpy(ts->meas_digest, rsp->meas_digest, sizeof(ts-
Quoted 1 line

meas_digest));

(quoted diff) +1 -0
+	memcpy(ts->interface_report_digest, rsp-
Quoted 1 line

interface_report_digest,

drivers/virt/coco/sev-guest/sev_guest_tio.c new file +6 -0
+	       sizeof(ts->interface_report_digest));
+	ts->intf_report_counter = rsp->tdi_report_count;
+
+	ts->valid = true;
+	ts->state = state;
+	/* The response buffer contains the sensitive data,

explicitly clear it. */

Review

Quoted 2 lines
(quoted diff) +2 -0
+free_exit:
+	memzero_explicit(&rsp, sizeof(resp_len));

The first argument should be rsp, not &rsp. This issue is also present in the other memzero_explicit() calls in this patch.

Quoted 15 lines

+static int sev_guest_tdi_validate(struct tsm_tdi *tdi, unsigned int featuremask,

(quoted diff) +1 -0
+				  bool invalidate, void

*private_data)

drivers/virt/coco/sev-guest/sev_guest_tio.c new file +10 -0
+{
+	struct snp_guest_dev *snp_dev = private_data;
+	struct tsm_tdi_status ts = { 0 };
+	int ret;
+
+	if (!tdi->report) {
+		ret = tio_tdi_status(tdi, snp_dev, &ts);
+
+		if (ret || !tdi->report) {
+			dev_err(&tdi->dev, "No report available,

ret=%d", ret);

drivers/virt/coco/sev-guest/sev_guest_tio.c line 697

Quoted 1 line
drivers/virt/coco/sev-guest/sev_guest_tio.c new file +1 -0
+			if (!ret && tdi->report)

This cannot happen, I think you meant (!ret && !tdi->report)

Quoted 1 line
maintainer
#74 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

In reply to Jason Gunthorpe
Quoted 25 lines · Jason Gunthorpe
Quoted 8 lines · Aneesh Kumar K.V
Quoted 8 lines
drivers/iommu/iommufd/viommu.c +8 -0
+int iommufd_vdevice_tsm_bind_ioctl(struct iommufd_ucmd *ucmd)
+{
+	struct iommu_vdevice_tsm_bind *cmd = ucmd->cmd;
+	struct iommufd_viommu *viommu;
+	struct iommufd_vdevice *vdev;
+	struct iommufd_device *idev;
+	struct tsm_tdi *tdi;
+	int rc = 0;
Quoted 8 lines

drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 180

Quoted 5 lines
drivers/iommu/iommufd/viommu.c +4 -0
+
+	viommu = iommufd_get_viommu(ucmd, cmd->viommu_id);
+	if (IS_ERR(viommu))
+		return PTR_ERR(viommu);

Would this require an IOMMU_HWPT_ALLOC_NEST_PARENT page table allocation?

Probably. That flag is what forces a S2 page table.

Quoted 2 lines

How would this work in cases where there's no need to set up Stage 1 IOMMU tables?

Either attach the raw HWPT of the IOMMU_HWPT_ALLOC_NEST_PARENT or:

Quoted 8 lines
Quoted 3 lines

Alternatively, should we allocate an IOMMU_HWPT_ALLOC_NEST_PARENT with a Stage 1 disabled translation config? (In the ARM case, this could mean marking STE entries as Stage 1 bypass and Stage 2 translate.)

For arm you mean IOMMU_HWPT_DATA_ARM_SMMUV3.. But yes, this can work too and is mandatory if you want the various viommu linked features to work.

I was trying to prototype this using kvmtool and I have run into some issues. First i needed the below change for vIOMMU alloc to work

modified drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
@@ -4405,6 +4405,8 @@ static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu)
reg = readl_relaxed(smmu->base + ARM_SMMU_IDR3);
if (FIELD_GET(IDR3_RIL, reg))
smmu->features |= ARM_SMMU_FEAT_RANGE_INV;
+ if (FIELD_GET(IDR3_FWB, reg))
+ smmu->features |= ARM_SMMU_FEAT_S2FWB;

/* IDR5 */
reg = readl_relaxed(smmu->base + ARM_SMMU_IDR5);

Also current code don't allow a Stage 1 bypass, Stage2 translation when allocating HWPT.

arm_vsmmu_alloc_domain_nested -> arm_smmu_validate_vste ->

	cfg = FIELD_GET(STRTAB_STE_0_CFG, le64_to_cpu(arg->ste[0]));
	if (cfg != STRTAB_STE_0_CFG_ABORT && cfg != STRTAB_STE_0_CFG_BYPASS &&
	    cfg != STRTAB_STE_0_CFG_S1_TRANS)
		return -EIO;

This only allow a abort or bypass or stage1 translate/stage2 bypass config

Also if we don't need stage1 table, what will
iommufd_viommu_alloc_hwpt_nested() return?

Quoted 11 lines
Quoted 4 lines

Also, if a particular setup doesn't require creating IOMMU entries because the entire guest RAM is identity-mapped in the IOMMU, do we still need to make tsm_tdi_bind use this abstraction in iommufd?

Even if the viommu will not be exposed to the guest I'm expecting that iommufd will have a viommu object, just not use various features. We are using viommu as the handle for the KVM, vmid and other things that are likely important here.

Jason

-aneesh

#75 lore raw

Re: [RFC PATCH v2 14/22] iommufd: Add TIO calls

Review static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu) · line 4412

Quoted 13 lines · Aneesh Kumar K.V

I was trying to prototype this using kvmtool and I have run into some issues. First i needed the below change for vIOMMU alloc to work

modified drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c

(quoted diff) +2 -0
@@ -4405,6 +4405,8 @@ static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu)
 	reg = readl_relaxed(smmu->base + ARM_SMMU_IDR3);
 	if (FIELD_GET(IDR3_RIL, reg))
 		smmu->features |= ARM_SMMU_FEAT_RANGE_INV;
+	if (FIELD_GET(IDR3_FWB, reg))
+		smmu->features |= ARM_SMMU_FEAT_S2FWB;
 
 	/* IDR5 */
 	reg = readl_relaxed(smmu->base + ARM_SMMU_IDR5);

Oh wow, I don't know what happened there that the IDR3 got dropped maybe a rebase mistake? It was in earlier versions of the patch at least :\ Please send a formal patch!!

Quoted 11 lines

Also current code don't allow a Stage 1 bypass, Stage2 translation when allocating HWPT.

arm_vsmmu_alloc_domain_nested -> arm_smmu_validate_vste ->

	cfg = FIELD_GET(STRTAB_STE_0_CFG, le64_to_cpu(arg->ste[0]));
	if (cfg != STRTAB_STE_0_CFG_ABORT && cfg != STRTAB_STE_0_CFG_BYPASS &&
	    cfg != STRTAB_STE_0_CFG_S1_TRANS)
		return -EIO;

This only allow a abort or bypass or stage1 translate/stage2 bypass config

The above is for the vSTE, the cfg is not copied as is to the host STE. See how arm_smmu_make_nested_domain_ste() transforms it.

STRTAB_STE_0_CFG_ABORT blocks all DMA STRTAB_STE_0_CFG_BYPASS "bypass" for the VM is S2 translation only STRTAB_STE_0_CFG_S1_TRANS "s1 only" for the VM is S1 & S1 translation

Quoted 2 lines

Also if we don't need stage1 table, what will
iommufd_viommu_alloc_hwpt_nested() return?

A wrapper around whatever STE configuration that userspace requested logically linked to the viommu.

Jason

author maintainer
#42 lore raw

Re: [RFC PATCH v2 13/22] iommufd: amd-iommu: Add vdevice support

In reply to Jason Gunthorpe
Quoted 34 lines · Jason Gunthorpe

include/uapi/linux/iommufd.h struct iommu_fault_alloc { · line 942

Quoted 5 lines · Alexey Kardashevskiy
include/uapi/linux/iommufd.h +1 -0
@@ -939,6 +939,7 @@ struct iommu_fault_alloc {
  enum iommu_viommu_type {
  	IOMMU_VIOMMU_TYPE_DEFAULT = 0,
  	IOMMU_VIOMMU_TYPE_ARM_SMMUV3 = 1,
+	IOMMU_VIOMMU_TYPE_TSM = 2,

This should probably be some kind of AMD_TSM and the driver data blob should carry any additional data needed to create the vIOMMU that is visible to the guest.

drivers/iommu/amd/iommu.c static void set_dte_entry(struct amd_iommu *iommu, · line 2073

Quoted 7 lines
drivers/iommu/amd/iommu.c +2 -1
@@ -2068,7 +2069,18 @@ static void set_dte_entry(struct amd_iommu *iommu,
  		new.data[1] |= DTE_FLAG_IOTLB;
 
  	old_domid = READ_ONCE(dte->data[1]) & DEV_DOMID_MASK;
-	new.data[1] |= domid;
+
+	if (domain->aviommu) {

AMD should be implementing viommu natively without CC as well, try to structure things so it fits together better. This should only trigger for the CC viommu type..

Quoted 11 lines
drivers/iommu/amd/iommu.c +10 -0
+		/*
+		 * This runs when VFIO is bound to a device but TDI is not yet.
+		 * Ideally TSM should change DTE only when TDI is bound.
+		 */
+		dev_info(dev_data->dev, "Skip DomainID=%x and set bit96\n", domid);
+		new.data[1] |= 1ULL << (96 - 64);
+	} else {
+		dev_info(dev_data->dev, "Not skip DomainID=%x and not set bit96\n", domid);
+		new.data[1] |= domid;
+	}
 
Quoted 13 lines

drivers/iommu/amd/iommu.c amd_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags, · line 2567

Quoted 10 lines
drivers/iommu/amd/iommu.c +3 -0
  	/*
  	 * Restore cached persistent DTE bits, which can be set by information
@@ -2549,12 +2561,15 @@ amd_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags,
  {
  	struct amd_iommu *iommu = get_amd_iommu_from_dev(dev);
  	const u32 supported_flags = IOMMU_HWPT_ALLOC_DIRTY_TRACKING |
+						IOMMU_HWPT_ALLOC_PASID |
+						IOMMU_HWPT_ALLOC_NEST_PARENT;
+	const u32 supported_flags2 = IOMMU_HWPT_ALLOC_DIRTY_TRACKING |
  						IOMMU_HWPT_ALLOC_PASID;

Just ignore NEST_PARENT? That seems wrong, it should force a V1 page table??

Ahhh... This is because I still have troubles with what IOMMU_DOMAIN_NESTED means (and iommufd.rst does not help me). There is one device, one IOMMU table buuut 2 domains? Uh.

Quoted 22 lines
Quoted 7 lines
drivers/iommu/amd/iommu.c +6 -0
+static struct iommufd_viommu *amd_viommu_alloc(struct device *dev,
+					       struct iommu_domain *parent,
+					       struct iommufd_ctx *ictx,
+					       unsigned int viommu_type)
+{
+	struct amd_viommu *aviommu;

drivers/iommu/amd/iommu.c static int amd_iommu_dev_disable_feature(struct device *dev, · line 3067

Quoted 10 lines
drivers/iommu/amd/iommu.c +10 -0
+	struct protection_domain *domain = to_pdomain(parent);
+
+	if (viommu_type != IOMMU_VIOMMU_TYPE_TSM)
+		return ERR_PTR(-EOPNOTSUPP);
+
+	aviommu = iommufd_viommu_alloc(ictx, struct amd_viommu, core, &amd_viommu_ops);
+	if (IS_ERR(aviommu))
+		return ERR_CAST(aviommu);
+
+	aviommu->domain = domain;

This is not OK, the parent domain of the viommu can be used with multiple viommu objects, it can't just have a naked back reference like this.

Quoted 3 lines

You can get 1:1 domain objects linked to the viommu by creating the 'S1' type domains, maybe that is what you want here. A special domain type that is TSM that has a special DTE.

Should not IOMMU_DOMAIN_NESTED be that "S1" domain? And what does "S1" mean here? Currently the domain in the hunk above is __IOMMU_DOMAIN_PAGING.

Quoted 4 lines

Though I'd really rather see the domain attach logic and DTE formation in the AMD driver be fixed up before we made it more complex :\

It would be nice to see normal nesting and viommu support first too :\

It is in the works too. Thanks,

Signature · 1 line
Alexey
maintainer
#43 lore raw

RE: [RFC PATCH v2 13/22] iommufd: amd-iommu: Add vdevice support

Quoted 16 lines

From: Alexey Kardashevskiy <[email protected]>
Sent: Thursday, April 10, 2025 2:40 PM

Quoted 1 line

Review amd_iommu_domain_alloc_paging_flags(struct

Quoted 1 line
(quoted diff) +0 -0
@@ -2549,12 +2561,15 @@ amd_iommu_domain_alloc_paging_flags(struct

device *dev, u32 flags,

Quoted 3 lines

drivers/iommu/amd/iommu.c amd_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags,

Quoted 3 lines
drivers/iommu/amd/iommu.c +0 -0
  {
  	struct amd_iommu *iommu = get_amd_iommu_from_dev(dev);
  	const u32 supported_flags = IOMMU_HWPT_ALLOC_DIRTY_TRACKING

|

Quoted 1 line

Review amd_iommu_domain_alloc_paging_flags(struct · line 2564

Quoted 1 line
(quoted diff) +1 -0
+						IOMMU_HWPT_ALLOC_PASID

|

Quoted 1 line

Review amd_iommu_domain_alloc_paging_flags(struct · line 2565

Quoted 1 line
(quoted diff) +1 -0
+
	IOMMU_HWPT_ALLOC_NEST_PARENT;
Quoted 1 line

Review amd_iommu_domain_alloc_paging_flags(struct · line 2566

Quoted 1 line
(quoted diff) +1 -0
+	const u32 supported_flags2 = IOMMU_HWPT_ALLOC_DIRTY_TRACKING

|

Quoted 2 lines
Quoted 1 line
drivers/iommu/amd/iommu.c +0 -0
  						IOMMU_HWPT_ALLOC_PASID;
Quoted 7 lines
Quoted 2 lines

Just ignore NEST_PARENT? That seems wrong, it should force a V1 page table??

Ahhh... This is because I still have troubles with what IOMMU_DOMAIN_NESTED means (and iommufd.rst does not help me). There is one device, one IOMMU table buuut 2 domains? Uh.

(not yet catch up with the whole thread. so just for basics)

one device can be attached to only one domain. When the attached domain is NESTED, the output from that domain will be further translated by another domain (PARENT). so yes, 2 domains could be involved and two IOMMU page tables chained in translation.

In that configuration, the NESTED domain is also called stage-1/s1 and its parent domain is called stage-2/s2.

Typically seen in a setup where the guest sees a vIOMMU and manages its own I/O page tables (translating guest IOVA to GPA). Then the GPA is further translated by a host-managed I/O paging table (PAGING domain, GPA->HPA).

a special case of s1 is 1:1 identity mapping (or passthrough), with which effectively only one domain (s2) manages translation but the concept of nested translation still holds.

#44 lore raw

Re: [RFC PATCH v2 13/22] iommufd: amd-iommu: Add vdevice support

In reply to Alexey Kardashevskiy
Quoted 13 lines · Alexey Kardashevskiy
Quoted 11 lines

drivers/iommu/amd/iommu.c amd_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags, · line 2567

Quoted 8 lines
drivers/iommu/amd/iommu.c +3 -0
@@ -2549,12 +2561,15 @@ amd_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags,
  {
  	struct amd_iommu *iommu = get_amd_iommu_from_dev(dev);
  	const u32 supported_flags = IOMMU_HWPT_ALLOC_DIRTY_TRACKING |
+						IOMMU_HWPT_ALLOC_PASID |
+						IOMMU_HWPT_ALLOC_NEST_PARENT;
+	const u32 supported_flags2 = IOMMU_HWPT_ALLOC_DIRTY_TRACKING |
  						IOMMU_HWPT_ALLOC_PASID;

Just ignore NEST_PARENT? That seems wrong, it should force a V1 page table??

Quoted 3 lines

Ahhh... This is because I still have troubles with what IOMMU_DOMAIN_NESTED means (and iommufd.rst does not help me). There is one device, one IOMMU table buuut 2 domains? Uh.

It means whatever you want it to mean, so long as it holds a reference to a NEST_PARENT :)

Quoted 5 lines
Quoted 3 lines

You can get 1:1 domain objects linked to the viommu by creating the 'S1' type domains, maybe that is what you want here. A special domain type that is TSM that has a special DTE.

Should not IOMMU_DOMAIN_NESTED be that "S1" domain?

Yes that is how ARM is doing it.

Minimally IOMMU_DOMAIN_NESTED on AMD should refere to a partial DTE fragment that sets the GCR3 information and other guest controlled bits from the vDTE. It should hold a reference to the viommu and the S2 NEST_PARENT.

From that basis then you'd try to fit in the CC stuff.

Quoted 6 lines
Quoted 4 lines

Though I'd really rather see the domain attach logic and DTE formation in the AMD driver be fixed up before we made it more complex :\

It would be nice to see normal nesting and viommu support first too :\

It is in the works too. Thanks,

I think your work will be easier to understand when viewed on top of working basic nesting support as it is just a special case of that

Jason

author maintainer
#45 lore raw

Re: [RFC PATCH v2 13/22] iommufd: amd-iommu: Add vdevice support

Quoted 14 lines · Jason Gunthorpe
Quoted 13 lines · Alexey Kardashevskiy
Quoted 11 lines

drivers/iommu/amd/iommu.c amd_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags, · line 2567

Quoted 8 lines
drivers/iommu/amd/iommu.c +3 -0
@@ -2549,12 +2561,15 @@ amd_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags,
   {
   	struct amd_iommu *iommu = get_amd_iommu_from_dev(dev);
   	const u32 supported_flags = IOMMU_HWPT_ALLOC_DIRTY_TRACKING |
+						IOMMU_HWPT_ALLOC_PASID |
+						IOMMU_HWPT_ALLOC_NEST_PARENT;
+	const u32 supported_flags2 = IOMMU_HWPT_ALLOC_DIRTY_TRACKING |
   						IOMMU_HWPT_ALLOC_PASID;

Just ignore NEST_PARENT? That seems wrong, it should force a V1 page table??

Quoted 6 lines
Quoted 3 lines

Ahhh... This is because I still have troubles with what IOMMU_DOMAIN_NESTED means (and iommufd.rst does not help me). There is one device, one IOMMU table buuut 2 domains? Uh.

It means whatever you want it to mean, so long as it holds a reference to a NEST_PARENT :)

ahhhh ;)

Quoted 18 lines
Quoted 5 lines
Quoted 3 lines

You can get 1:1 domain objects linked to the viommu by creating the 'S1' type domains, maybe that is what you want here. A special domain type that is TSM that has a special DTE.

Should not IOMMU_DOMAIN_NESTED be that "S1" domain?

Yes that is how ARM is doing it.

Minimally IOMMU_DOMAIN_NESTED on AMD should refere to a partial DTE fragment that sets the GCR3 information and other guest controlled bits from the vDTE. It should hold a reference to the viommu and the S2 NEST_PARENT.

From that basis then you'd try to fit in the CC stuff.

Quoted 3 lines
Quoted 3 lines

Though I'd really rather see the domain attach logic and DTE formation in the AMD driver be fixed up before we made it more complex :\

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

It would be nice to see normal nesting and viommu support first too :\

It is in the works too. Thanks,

I think your work will be easier to understand when viewed on top of working basic nesting support as it is just a special case of that

Really not sure about that "easier" thing :)

GCR3 is orthogonal to what I am doing here right now - this exercise does not use any additional guest table, instead it tells the host IOMMU (yeah, via the PSP) how to treat all IOVAs - private or shared (a bar called "vTOM" == virtual top of memory, below that bar everything is private, above - shared, I set it to the maximum). So even when we get vIOMMU in SNP VMs, unenlightened VM will be still using vTOM (SVSM == privileged VM FW which will talk to the PSP about vTOM).

This vTOM is very limited vIOMMU really (communicated just an address limit), not what people usually think when read "vIOMMU" with guest tables and 2 level translation.

Signature · 1 line
Alexey
#2 lore raw

Re: [RFC PATCH v2 01/22] pci/doe: Define protocol types and make those public

In reply to Alexey Kardashevskiy

Match capitalization of subject from previous history.

Quoted 2 lines · Alexey Kardashevskiy

Already public pci_doe() takes a protocol type argument. PCIe 6.0 defines three, define them in a header for use with pci_doe().

Add section number to spec reference, rewrap paragraph to fill 75 columns.

Bjorn

#20 lore raw

Re: [RFC PATCH v2 08/22] pci/tsm: Add PCI driver for TSM

In reply to Alexey Kardashevskiy

Match subject capitalization style of history.

Drop second "PCI", mostly redundant.

Quoted 4 lines · Alexey Kardashevskiy

The PCI TSM module scans the PCI bus to initialize a TSM context for physical ("TDEV") and virtual ("TDI") functions. It also implements bus operations which at the moment is just an SPDM bouncer which talks to the PF's DOE mailboxes.

Expand "TSM" once here and maybe in the subject.

Quoted 1 line

+ * Copyright(c) 2024 Intel Corporation. All rights reserved.

2025 now.

drivers/pci/tsm.c line 89

Quoted 6 lines
drivers/pci/tsm.c new file +6 -0
+static int tsm_pci_dev_init(struct tsm_bus_subsys *tsm_bus,
+			    struct pci_dev *pdev,
+			    struct tsm_dev **ptdev)
+{
+	struct tsm_pci_dev_data *tdata;
+	int ret = tsm_dev_init(tsm_bus, &pdev->dev, sizeof(*tdata), ptdev);

Move the tsm_dev_init() out of the automatic variable list. Doing it in the list is OK for trivial things, but this is kind of the meat of the function.

Quoted 14 lines
drivers/pci/tsm.c new file +14 -0
+	if (ret)
+		return ret;
+
+	tdata = tsm_dev_to_bdata(*ptdev);
+
+	tdata->doe_mb = pci_find_doe_mailbox(pdev,
+					     PCI_VENDOR_ID_PCI_SIG,
+					     PCI_DOE_PROTOCOL_CMA_SPDM);
+	tdata->doe_mb_sec = pci_find_doe_mailbox(pdev,
+						 PCI_VENDOR_ID_PCI_SIG,
+						 PCI_DOE_PROTOCOL_SECURED_CMA_SPDM);
+
+	if (tdata->doe_mb || tdata->doe_mb_sec)
+		pci_notice(pdev, "DOE SPDM=%s SecuredSPDM=%s\n",

drivers/pci/tsm.c line 113

Quoted 10 lines
drivers/pci/tsm.c new file +9 -0
+			   tdata->doe_mb ? "yes":"no", tdata->doe_mb_sec ? "yes":"no");
+
+	return ret;
+}
 
+
+static int tsm_pci_alloc_device(struct tsm_bus_subsys *tsm_bus,
+				struct pci_dev *pdev)
+{
+	int ret = 0;

Unnecessary initialization.

Quoted 40 lines
drivers/pci/tsm.c new file +39 -0
+	/* Set up TDIs for HV (physical functions) and VM (all functions) */
+	if ((pdev->devcap & PCI_EXP_DEVCAP_TEE) &&
+	    (((pdev->is_physfn && (PCI_FUNC(pdev->devfn) == 0)) ||
+	      (!pdev->is_physfn && !pdev->is_virtfn)))) {
+
+		struct tsm_dev *tdev = NULL;
+
+		if (!is_physical_endpoint(pdev))
+			return 0;
+
+		ret = tsm_pci_dev_init(tsm_bus, pdev, &tdev);
+		if (ret)
+			return ret;
+
+		ret = tsm_tdi_init(tdev, &pdev->dev);
+		tsm_dev_put(tdev);
+		return ret;
+	}
+
+	/* Set up TDIs for HV (virtual functions), should do nothing in VMs */
+	if (pdev->is_virtfn) {
+		struct pci_dev *pf0 = pci_get_slot(pdev->physfn->bus,
+						   pdev->physfn->devfn & ~7);
+
+		if (pf0 && (pf0->devcap & PCI_EXP_DEVCAP_TEE)) {
+			struct tsm_dev *tdev = tsm_dev_get(&pf0->dev);
+
+			if (!is_endpoint(pdev))
+				return 0;
+
+			ret = tsm_tdi_init(tdev, &pdev->dev);
+			tsm_dev_put(tdev);
+			return ret;
+		}
+	}
+
+	return 0;
+}
 
+

drivers/pci/tsm.c line 163

Quoted 10 lines
drivers/pci/tsm.c new file +10 -0
+static void tsm_pci_dev_free(struct pci_dev *pdev)
+{
+	struct tsm_tdi *tdi = tsm_tdi_get(&pdev->dev);
+
+	if (tdi) {
+		tsm_tdi_put(tdi);
+		tsm_tdi_free(tdi);
+	}
+
+	struct tsm_dev *tdev = tsm_dev_get(&pdev->dev);

Move at least the declaration to automatic list at entry.

drivers/pci/tsm.c line 173

Quoted 9 lines
drivers/pci/tsm.c new file +9 -0
+	if (tdev) {
+		tsm_dev_put(tdev);
+		tsm_dev_free(tdev);
+	}
+
+	WARN_ON(!tdi && tdev);
+}
+
+static int tsm_pci_bus_notifier(struct notifier_block *nb, unsigned long action, void *data)

Wrap to fit in 80 columns like the rest of drivers/pci/

Quoted 17 lines
drivers/pci/tsm.c new file +17 -0
+{
+	struct tsm_bus_subsys *tsm_bus = container_of(nb, struct tsm_bus_subsys, notifier);
+
+	switch (action) {
+	case BUS_NOTIFY_ADD_DEVICE:
+		tsm_pci_alloc_device(tsm_bus, to_pci_dev(data));
+		break;
+	case BUS_NOTIFY_DEL_DEVICE:
+		tsm_pci_dev_free(to_pci_dev(data));
+		break;
+	}
+
+	return NOTIFY_OK;
+}
+
+struct tsm_bus_subsys *pci_tsm_register(struct tsm_subsys *tsm)
+{

drivers/pci/tsm.c line 200

Quoted 10 lines
drivers/pci/tsm.c new file +10 -0
+	struct tsm_bus_subsys *tsm_bus = kzalloc(sizeof(*tsm_bus), GFP_KERNEL);
+	struct pci_dev *pdev = NULL;
+
+	pr_info("Scan TSM PCI\n");
+	tsm_bus->ops = &tsm_pci_ops;
+	tsm_bus->tsm = tsm;
+	tsm_bus->notifier.notifier_call = tsm_pci_bus_notifier;
+	for_each_pci_dev(pdev)
+		tsm_pci_alloc_device(tsm_bus, pdev);
+	bus_register_notifier(&pci_bus_type, &tsm_bus->notifier);

Looks racy that we iterate through PCI devs before registering the notifier.

drivers/pci/tsm.c line 224

Quoted 12 lines
drivers/pci/tsm.c new file +11 -0
+	return tsm_bus;
+}
 
+static int __init tsm_pci_init(void)
+{
+	pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
+	return 0;
+}
+
+static void __exit tsm_pci_cleanup(void)
+{
+	pr_info(DRIVER_DESC " version: " DRIVER_VERSION " unload\n");

Both init and cleanup messages are OK for debug, but probably not for upstream.

drivers/pci/Kconfig config PCI_CMA · line 147

Quoted 8 lines
drivers/pci/Kconfig +8 -0
+config PCI_TSM
+	tristate "TEE Security Manager for PCI Device Security"
+	select PCI_IDE
+	depends on TSM
+	default m
+	help
+	  The TEE (Trusted Execution Environment) Device Interface
+	  Security Protocol (TDISP) defines a "TSM" as a platform agent

Expand "TSM" here. From menu line above, I guess it's "TEE Security Manager"?

Quoted 13 lines
drivers/pci/Kconfig +7 -0
+	  that manages device authentication, link encryption, link
+	  integrity protection, and assignment of PCI device functions
+	  (virtual or physical) to confidential computing VMs that can
+	  access (DMA) guest private memory.
+
+	  Enable a platform TSM driver to use this capability.
+
 config PCI_DOE
 	bool
Signature · 2 lines
-- 
2.47.1
#88 lore raw

Re: [RFC PATCH v2 19/22] RFC: pci: Add BUS_NOTIFY_PCI_BUS_MASTER event

In reply to Alexey Kardashevskiy

Match subject capitalization style.

Quoted 9 lines · Alexey Kardashevskiy

TDISP allows secure MMIO access to a validated MMIO range. The validation is done in the TSM and after that point changing the device's Memory Space enable (MSE) or Bus Master enable (BME) transitions the device into the error state.

For PCI device drivers which enable MSE, then BME, and then start using the device, enabling BME is a logical point to perform the MMIO range validation in the TSM.

Quoted 5 lines

Define new event for a bus. TSM is going to listen to it in the TVM and do the validation for TEE ranges.

This does not switch MMIO to private by default though as this is for the driver to decide (at least, for now).

Wrap all to fill 75 columns.

#92 lore raw

Re: [RFC PATCH v2 21/22] pci: Allow encrypted MMIO mapping via sysfs

In reply to Alexey Kardashevskiy
Quoted 2 lines · Alexey Kardashevskiy

Add another resource#d_enc to allow mapping MMIO as an encrypted/private region.

I guess this means a sysfs file. Document alongside the others.

Quoted 3 lines

Unlike resourceN_wc, the node is added always as ability to map MMIO as private depends on negotiation with the TSM which happens quite late.

Match capitalization (subject) and wrap to fill 75 columns.

include/linux/pci.h pci_alloc_irq_vectors(struct pci_dev *dev, unsigned int min_vecs, · line 2132

Quoted 7 lines

+++ b/include/linux/pci.h

include/linux/pci.h +1 -1
@@ -2129,7 +2129,7 @@ pci_alloc_irq_vectors(struct pci_dev *dev, unsigned int min_vecs,
  */
 int pci_mmap_resource_range(struct pci_dev *dev, int bar,
 			    struct vm_area_struct *vma,
-			    enum pci_mmap_state mmap_state, int write_combine);
+			    enum pci_mmap_state mmap_state, int write_combine, int enc);

Wrap to fit in 80 columns.

drivers/pci/mmap.c static const struct vm_operations_struct pci_phys_vm_ops = { · line 26

Quoted 13 lines
include/linux/pci.h +0 -0
 #ifndef arch_can_pci_mmap_wc
 #define arch_can_pci_mmap_wc()		0
drivers/pci/mmap.c +1 -1
index 8da3347a95c4..4fd522aeb767 100644
@@ -23,7 +23,7 @@ static const struct vm_operations_struct pci_phys_vm_ops = {
 
 int pci_mmap_resource_range(struct pci_dev *pdev, int bar,
 			    struct vm_area_struct *vma,
-			    enum pci_mmap_state mmap_state, int write_combine)
+			    enum pci_mmap_state mmap_state, int write_combine, int enc)

Ditto.

drivers/pci/mmap.c int pci_mmap_resource_range(struct pci_dev *pdev, int bar, · line 51

Quoted 10 lines
drivers/pci/mmap.c +3 -0
 {
 	unsigned long size;
 	int ret;
@@ -46,6 +46,15 @@ int pci_mmap_resource_range(struct pci_dev *pdev, int bar,
 
 	vma->vm_ops = &pci_phys_vm_ops;
 
+	/*
+	 * Calling remap_pfn_range() directly as io_remap_pfn_range()
+	 * enforces shared mapping.

s/Calling/Call/

Quoted 9 lines
drivers/pci/mmap.c +6 -0
+	 */
+	if (enc)
+		return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
+				       vma->vm_end - vma->vm_start,
+				       vma->vm_page_prot);
+
 	return io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
 				  vma->vm_end - vma->vm_start,
 				  vma->vm_page_prot);
#94 lore raw

Re: [RFC PATCH v2 22/22] pci: Define pci_iomap_range_encrypted

In reply to Alexey Kardashevskiy
Quoted 4 lines · Alexey Kardashevskiy

So far PCI BARs could not be mapped as encrypted so there was no need in API supporting encrypted mappings. TDISP is adding such support so add pci_iomap_range_encrypted() to allow PCI drivers do the encrypted mapping when needed.

Match subject capitalization, rewrap.

Quoted 10 lines
drivers/pci/iomap.c +10 -0
+void __iomem *pci_iomap_range_encrypted(struct pci_dev *dev,
+					int bar,
+					unsigned long offset,
+					unsigned long maxlen)
+{
+	resource_size_t start = pci_resource_start(dev, bar);
+	resource_size_t len = pci_resource_len(dev, bar);
+	unsigned long flags = pci_resource_flags(dev, bar);
+
+	if (len <= offset || !start)

drivers/pci/iomap.c void __iomem *pci_iomap_range(struct pci_dev *dev, · line 74

Quoted 10 lines
drivers/pci/iomap.c +10 -0
+		return NULL;
+	len -= offset;
+	start += offset;
+	if (maxlen && len > maxlen)
+		len = maxlen;
+	if (flags & IORESOURCE_IO)
+		return NULL;
+	if ((flags & IORESOURCE_MEM) && (flags & IORESOURCE_VALIDATED))
+		return ioremap_encrypted(start, len);
+	/* What? */

"What?" indeed. This could be removed or made to say something intelligible.

Quoted 10 lines
drivers/pci/iomap.c +4 -0
+	return NULL;
+}
+EXPORT_SYMBOL(pci_iomap_range_encrypted);
+
 /**
  * pci_iomap_wc_range - create a virtual WC mapping cookie for a PCI BAR
  * @dev: PCI device that owns the BAR
Signature · 1 line
2.47.1
author maintainer
#14 lore raw

Re: [RFC PATCH v2 06/22] KVM: X86: Define tsm_get_vmid

In reply to Alexey Kardashevskiy
Quoted 107 lines · Alexey Kardashevskiy
Quoted 80 lines · Dan Williams
Quoted 79 lines · Alexey Kardashevskiy
Quoted 76 lines · Dan Williams
Quoted 60 lines · Alexey Kardashevskiy

In order to add a PCI VF into a secure VM, the TSM module needs to perform a "TDI bind" operation. The secure module ("PSP" for AMD) reuqires a VM id to associate with a VM and KVM has it. Since KVM cannot directly bind a TDI (as it does not have all necesessary data such as host/guest PCI BDFn). QEMU and IOMMUFD do know the BDFns but they do not have a VM id recognisable by the PSP.

Add get_vmid() hook to KVM. Implement it for AMD SEV to return a sum of GCTX (a private page describing secure VM context) and ASID (required on unbind for IOMMU unfencing, when needed).

---
   arch/x86/include/asm/kvm-x86-ops.h |  1 + arch/x86/include/asm/kvm_host.h    |  2 ++ include/linux/kvm_host.h           |  2 ++ arch/x86/kvm/svm/svm.c             | 12 ++++++++++++ virt/kvm/kvm_main.c                |  6 ++++++

   5 files changed, 23 insertions(+)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
index c35550581da0..63102a224cd7 100644
@@ -144,6 +144,7 @@ KVM_X86_OP_OPTIONAL(alloc_apic_backing_page)
    KVM_X86_OP_OPTIONAL_RET0(gmem_prepare)
    KVM_X86_OP_OPTIONAL_RET0(private_max_mapping_level)
    KVM_X86_OP_OPTIONAL(gmem_invalidate)
+KVM_X86_OP_OPTIONAL(tsm_get_vmid)
    #undef KVM_X86_OP
    #undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +2 -0
index b15cde0a9b5c..9330e8d4d29d 100644
@@ -1875,6 +1875,8 @@ struct kvm_x86_ops {
        int (*gmem_prepare)(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order);
        void (*gmem_invalidate)(kvm_pfn_t start, kvm_pfn_t end);
        int (*private_max_mapping_level)(struct kvm *kvm, kvm_pfn_t pfn);
+
+    u64 (*tsm_get_vmid)(struct kvm *kvm);
    };
    struct kvm_x86_nested_ops {
include/linux/kvm_host.h +2 -0
index f34f4cfaa513..6cd351edb956 100644
@@ -2571,4 +2571,6 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,
                        struct kvm_pre_fault_memory *range);
    #endif
+u64 kvm_arch_tsm_get_vmid(struct kvm *kvm);
+
    #endif
arch/x86/kvm/svm/svm.c +0 -0
index 7640a84e554a..0276d60c61d6 100644
@@ -4998,6 +4998,16 @@ static void *svm_alloc_apic_backing_page(struct kvm_vcpu *vcpu)
        return page_address(page);
    }

arch/x86/kvm/svm/svm.c static void *svm_alloc_apic_backing_page(struct kvm_vcpu *vcpu) · line 5009

Quoted 10 lines
arch/x86/kvm/svm/svm.c +10 -0
+static u64 svm_tsm_get_vmid(struct kvm *kvm)
+{
+    struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+
+    if (!sev->es_active)
+        return 0;
+
+    return ((u64) sev->snp_context) | sev->asid;
+}
+

Curious why KVM needs to be bothered by a new kvm_arch_tsm_get_vmid() and a vendor specific cookie "vmid" concept. In other words KVM never calls kvm_arch_tsm_get_vmid(), like other kvm_arch_*() support calls.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Is this due to a restriction that something like tsm_tdi_bind() is disallowed from doing to_kvm_svm() on an opaque @kvm pointer? Or otherwise asking an arch/x86/kvm/svm/svm.c to do the same?

I saw someone already doing some sort of VMID thing

Reference?

Cannot find it now, RiscV and ARM have this concept but internally and it probably should stay this way.

Quoted 10 lines
Quoted 1 line

and thought it is a good way of not spilling KVM details outside KVM.

...but it is not a KVM detail. It is an arch specific TSM cookie derived from arch specific data that wraps 'struct kvm'. Now if the rationale is some least privelege concern about what code can have a container_of() relationship with an opaque 'struct kvm *' pointer, let's have that discussion.  As it stands nothing in KVM cares about kvm_arch_tsm_get_vmid(), and I expect 'vmid' does not cover all the ways in which modular TSM drivers may interact with arch/.../kvm/ code.

Quoted 3 lines

For example TDX Connect needs to share some data from 'struct kvm_tdx', and it does that with an export from arch/x86/kvm/vmx/tdx.c, not an indirection through virt/kvm/kvm_main.c.

Quoted 6 lines

a) KVM-AMD uses CCP's exports now, and if I add exports to KVM-AMD for CCP - it is cross-reference so I'll need what, KVM-AMD-TIO module, to untangle this?

b) I could include arch/x86/kvm/svm/svm.h in drivers/crypto/ccp/ which is... meh?

c) Or move parts of struct kvm_sev_info/kvm_svm from arch/x86/kvm/svm/svm.h to arch/x86/include/asm/svm.h and do some trick to get kvm_sev_info from struct kvm.

Thanks for the suggestion, I ended up doing this and ditched the whole tsm_get_vmid() thing, looks semi-acceptable (at least contained to the AMD code) to keep going. Thanks,

Quoted 17 lines

d) In my RFC v1, I simply called tsm_tdi_bind() from KVM-AMD with this cookie but that assumed KVM knowledge of PCI which I dropped in this RFC so the bind request travels via QEMU between the guest and the PSP.

All doable though.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Effectively low level TSM drivers are extensions of arch code that routinely performs "container_of(kvm, struct kvm_$arch, kvm)".

The arch code is CCP and so far it avoided touching KVM, KVM calls CCP when it needs but not vice versa. Thanks,

Right, and the observation is that you don't need to touch virt/kvm/kvm_main.c at all to meet this data sharing requirement.

These are all valid points. I like neither of a)..d) in particular and I am AMD-centric (as you correctly noticed :) ) and for this exercise I only needed kvmfd->guest_context_page, hence this proposal. Thanks,

Signature · 1 line
Alexey
#17 lore raw

Re: [RFC PATCH v2 07/22] coco/tsm: Add tsm and tsm-host modules

In reply to Alexey Kardashevskiy
Quoted 119 lines · On Tue, 18 Feb 2025 22:09:54 +1100 Alexey Kardashevskiy <[email protected]> wrote:

The TSM module is a library to create sysfs nodes common for hypervisors and VMs. It also provides helpers to parse interface reports (required by VMs, visible to HVs). It registers 3 device classes:
- tsm: one per platform,
- tsm-dev: for physical functions, ("TDEV");
- tdm-tdi: for PCI functions being assigned to VMs ("TDI").

The library adds a child device of "tsm-dev" or/and "tsm-tdi" class for every capable PCI device. Note that the module is made bus-agnostic.

New device nodes provide sysfs interface for fetching device certificates and measurements and TDI interface reports. Nodes with the "_user" suffix provide human-readable information, without that suffix it is raw binary data to be copied to a guest.

The TSM-HOST module adds hypervisor-only functionality on top. At the moment it is:
- "connect" to enable/disable IDE (a PCI link encryption);
- "TDI bind" to manage a PCI function passed through to a secure VM.

A platform is expected to register itself in TSM-HOST and provide necessary callbacks. No platform is added here, AMD SEV is coming in the next patches.

---
 drivers/virt/coco/Makefile        |   2 +
 drivers/virt/coco/host/Makefile   |   6 +
 include/linux/tsm.h               | 295 +++++++++
 drivers/virt/coco/host/tsm-host.c | 552 +++++++++++++++++
 drivers/virt/coco/tsm.c           | 636 ++++++++++++++++++++
 Documentation/virt/coco/tsm.rst   |  99 +++
 drivers/virt/coco/Kconfig         |  14 +
 drivers/virt/coco/host/Kconfig    |   6 +
 8 files changed, 1610 insertions(+)
drivers/virt/coco/Makefile +2 -0
index 885c9ef4e9fc..670f77c564e8 100644
@@ -2,9 +2,11 @@
 #
 # Confidential computing related collateral
 #
+obj-$(CONFIG_TSM)		+= tsm.o
 obj-$(CONFIG_EFI_SECRET)	+= efi_secret/
 obj-$(CONFIG_ARM_PKVM_GUEST)	+= pkvm-guest/
 obj-$(CONFIG_SEV_GUEST)		+= sev-guest/
 obj-$(CONFIG_INTEL_TDX_GUEST)	+= tdx-guest/
 obj-$(CONFIG_ARM_CCA_GUEST)	+= arm-cca-guest/
 obj-$(CONFIG_TSM_REPORTS)	+= guest/
+obj-$(CONFIG_TSM_HOST)          += host/
(quoted diff) +0 -0

b/drivers/virt/coco/host/Makefile new file mode 100644 index 000000000000..c5e216b6cb1c

drivers/virt/coco/host/Makefile new file +6 -0
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# TSM (TEE Security Manager) Common infrastructure and host drivers
+
+obj-$(CONFIG_TSM_HOST) += tsm_host.o
+tsm_host-y += tsm-host.o
include/linux/tsm.h +30 -0
index 431054810dca..486e386d90fc 100644
@@ -5,6 +5,11 @@
 #include <linux/sizes.h>
 #include <linux/types.h>
 #include <linux/uuid.h>
+#include <linux/device.h>
+#include <linux/slab.h>
+#include <linux/mutex.h>
+#include <linux/device.h>
+#include <linux/bitfield.h>
 
 #define TSM_REPORT_INBLOB_MAX 64
 #define TSM_REPORT_OUTBLOB_MAX SZ_32K
@@ -109,4 +114,294 @@ struct tsm_report_ops {
 
 int tsm_report_register(const struct tsm_report_ops *ops, void
 *priv); int tsm_report_unregister(const struct tsm_report_ops *ops);
+
+/* SPDM control structure for DOE */
+struct tsm_spdm {
+	unsigned long req_len;
+	void *req;
+	unsigned long rsp_len;
+	void *rsp;
+};
+
+/* Data object for measurements/certificates/attestationreport */
+struct tsm_blob {
+	void *data;
+	size_t len;
+};
+
+struct tsm_blob *tsm_blob_new(void *data, size_t len);
+static inline void tsm_blob_free(struct tsm_blob *b)
+{
+	kfree(b);
+}
+
+/**
+ * struct tdisp_interface_id - TDISP INTERFACE_ID Definition
+ *
+ * @function_id: Identifies the function of the device hosting the

TDI
+ * 15:0: @rid: Requester ID
+ * 23:16: @rseg: Requester Segment (Reserved if Requester Segment
Valid is Clear)
+ * 24: @rseg_valid: Requester Segment Valid
+ * 31:25 – Reserved
+ * 8B - Reserved
+ */

include/linux/tsm.h struct tsm_report_ops { · line 156

Quoted 9 lines
include/linux/tsm.h +9 -0
+struct tdisp_interface_id {
+	u32 function_id; /* TSM_TDISP_IID_xxxx */
+	u8 reserved[8];
+} __packed;
+
+#define TSM_TDISP_IID_REQUESTER_ID	GENMASK(15, 0)
+#define TSM_TDISP_IID_RSEG		GENMASK(23, 16)
+#define TSM_TDISP_IID_RSEG_VALID	BIT(24)
+

I would suggest that we have separate header files for spec definitions. E.g. tdisp_defs and spdm_defs.h. from the maintainability perspective.

Quoted 9 lines
include/linux/tsm.h +9 -0
+/*
+ * Measurement block as defined in SPDM DSP0274.
+ */
+struct spdm_measurement_block_header {
+	u8 index;
+	u8 spec; /* MeasurementSpecification */
+	u16 size;
+} __packed;
+

....

Quoted 13 lines
include/linux/tsm.h +4 -0
+struct tsm_hv_ops {
+	int (*dev_connect)(struct tsm_dev *tdev, void *private_data);
+	int (*dev_disconnect)(struct tsm_dev *tdev);
+	int (*dev_status)(struct tsm_dev *tdev, struct

tsm_dev_status *s);

include/linux/tsm.h +4 -0
+	int (*dev_measurements)(struct tsm_dev *tdev);
+	int (*tdi_bind)(struct tsm_tdi *tdi, u32 bdfn, u64 vmid);
+	int (*tdi_unbind)(struct tsm_tdi *tdi);
+	int (*guest_request)(struct tsm_tdi *tdi, u8 __user *req,

size_t reqlen,

include/linux/tsm.h +1 -0
+			     u8 __user *rsp, size_t rsplen, int

*fw_err);

include/linux/tsm.h +1 -0
+	int (*tdi_status)(struct tsm_tdi *tdi, struct tsm_tdi_status
Quoted 2 lines

*ts); +};
+

1) Looks we have two more callbacks besides TDI verbs, I think they are fine to be in TSM driver ops.

For guest_request(), anyway, we need an entry point for QEMU to reach the TSM services in the kernel. Looks like almost all the platform (Intel/AMD/ARM) have TVM-HOST paths, which will exit to QEMU from KVM, and QEMU reaches the TSM services and return to the TVM. I think they can all leverage the entry point (IOMMUFD) via the guest request ioctl. And IOMMUFD almost have all the stuff QEMU needs.

Or we would end up with QEMU reaches to different entry points in per-vendor code path, which was not preferable, backing to the period when enabling CC in QEMU.

2) Also, it is better that we have separate the tsm_guest and tsm_host headers since the beginning.

3) How do you trigger the TDI_BIND from the guest in the late-bind model? Was looking at tsm_vm_ops, but seems not found yet.

Quoted 115 lines
include/linux/tsm.h +5 -0
+struct tsm_subsys {
+	struct device dev;
+	struct list_head tdi_head;
+	struct mutex lock;
+	const struct attribute_group *tdev_groups[3]; /* Common,

host/guest, NULL */

include/linux/tsm.h +1 -0
+	const struct attribute_group *tdi_groups[3]; /* Common,

host/guest, NULL */

include/linux/tsm.h +5 -0
+	int (*update_measurements)(struct tsm_dev *tdev);
+};
+
+struct tsm_subsys *tsm_register(struct device *parent, size_t extra,
+				const struct attribute_group

*tdev_ag,

include/linux/tsm.h +2 -0
+				const struct attribute_group *tdi_ag,
+				int (*update_measurements)(struct

tsm_dev *tdev)); +void tsm_unregister(struct tsm_subsys *subsys);

include/linux/tsm.h +13 -0
+
+struct tsm_host_subsys;
+struct tsm_host_subsys *tsm_host_register(struct device *parent,
+					  struct tsm_hv_ops *hvops,
+					  void *private_data);
+struct tsm_dev *tsm_dev_get(struct device *dev);
+void tsm_dev_put(struct tsm_dev *tdev);
+struct tsm_tdi *tsm_tdi_get(struct device *dev);
+void tsm_tdi_put(struct tsm_tdi *tdi);
+
+struct pci_dev;
+int pci_dev_tdi_validate(struct pci_dev *pdev, bool invalidate);
+int pci_dev_tdi_mmio_config(struct pci_dev *pdev, u32 range_id, bool

tee); +
+int tsm_dev_init(struct tsm_bus_subsys *tsm_bus, struct device *parent,

include/linux/tsm.h +9 -0
+		 size_t busdatalen, struct tsm_dev **ptdev);
+void tsm_dev_free(struct tsm_dev *tdev);
+int tsm_tdi_init(struct tsm_dev *tdev, struct device *dev);
+void tsm_tdi_free(struct tsm_tdi *tdi);
+
+/* IOMMUFD vIOMMU helpers */
+int tsm_tdi_bind(struct tsm_tdi *tdi, u32 guest_rid, int kvmfd);
+void tsm_tdi_unbind(struct tsm_tdi *tdi);
+int tsm_guest_request(struct tsm_tdi *tdi, u8 __user *req, size_t

reqlen,

include/linux/tsm.h +7 -0
+		      u8 __user *res, size_t reslen, int *fw_err);
+
+/* Debug */
+ssize_t tsm_report_gen(struct tsm_blob *report, char *b, size_t len);
+
+/* IDE */
+int tsm_create_link(struct tsm_subsys *tsm, struct device *dev,

const char *name); +void tsm_remove_link(struct tsm_subsys *tsm, const char *name); +#define tsm_register_ide_stream(tdev, ide) \

include/linux/tsm.h +4 -0
+	tsm_create_link((tdev)->tsm, &(tdev)->dev, (ide)->name)
+#define tsm_unregister_ide_stream(tdev, ide) \
+	tsm_remove_link((tdev)->tsm, (ide)->name)
+
 #endif /* __TSM_H */
(quoted diff) +0 -0

b/drivers/virt/coco/host/tsm-host.c new file mode 100644 index 000000000000..80f3315fb195

drivers/virt/coco/host/tsm-host.c new file +20 -0
@@ -0,0 +1,552 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/module.h>
+#include <linux/tsm.h>
+#include <linux/file.h>
+#include <linux/kvm_host.h>
+
+#define DRIVER_VERSION	"0.1"
+#define DRIVER_AUTHOR	"[email protected]"
+#define DRIVER_DESC	"TSM host library"
+
+struct tsm_host_subsys {
+	struct tsm_subsys base;
+	struct tsm_hv_ops *ops;
+	void *private_data;
+};
+
+static int tsm_dev_connect(struct tsm_dev *tdev)
+{
+	struct tsm_host_subsys *hsubsys = (struct tsm_host_subsys *)

tdev->tsm;

drivers/virt/coco/host/tsm-host.c new file +11 -0
+	int ret;
+
+	if (WARN_ON(!hsubsys->ops->dev_connect))
+		return -EPERM;
+
+	if (WARN_ON(!tdev->tsm_bus))
+		return -EPERM;
+
+	mutex_lock(&tdev->spdm_mutex);
+	while (1) {
+		ret = hsubsys->ops->dev_connect(tdev,

hsubsys->private_data);

drivers/virt/coco/host/tsm-host.c new file +4 -0
+		if (ret <= 0)
+			break;
+
+		ret = tdev->tsm_bus->ops->spdm_forward(&tdev->spdm,

ret);

drivers/virt/coco/host/tsm-host.c new file +13 -0
+		if (ret < 0)
+			break;
+	}
+	mutex_unlock(&tdev->spdm_mutex);
+
+	tdev->connected = (ret == 0);
+
+	return ret;
+}
+
+static int tsm_dev_reclaim(struct tsm_dev *tdev)
+{
+	struct tsm_host_subsys *hsubsys = (struct tsm_host_subsys *)

tdev->tsm;

drivers/virt/coco/host/tsm-host.c line 57

Quoted 9 lines
drivers/virt/coco/host/tsm-host.c new file +9 -0
+	int ret;
+
+	if (WARN_ON(!hsubsys->ops->dev_disconnect))
+		return -EPERM;
+
+	/* Do not disconnect with active TDIs */
+	if (tdev->bound)
+		return -EBUSY;
+

Can this replace a lock? refcount is to track the life cycle, lock is to avoid racing. Think that we just pass here tdev->bound == 0, take the spdm_mutex and request the TSM to talk to the device for disconnection, while someone is calling tdi_bind and pass the tdev->connected check and waiting for the spdm_mutex to do the tdi_bind. The device might see a TDI_BIND after a DEVICE_DISCONNECT.

Z.

Quoted 974 lines
drivers/virt/coco/host/tsm-host.c new file +7 -0
+	mutex_lock(&tdev->spdm_mutex);
+	while (1) {
+		ret = hsubsys->ops->dev_disconnect(tdev);
+		if (ret <= 0)
+			break;
+
+		ret = tdev->tsm_bus->ops->spdm_forward(&tdev->spdm,

ret);

drivers/virt/coco/host/tsm-host.c new file +12 -0
+		if (ret < 0)
+			break;
+	}
+	mutex_unlock(&tdev->spdm_mutex);
+
+	if (!ret)
+		tdev->connected = false;
+
+	return ret;
+}
+
+static int tsm_dev_status(struct tsm_dev *tdev, struct

tsm_dev_status *s) +{

drivers/virt/coco/host/tsm-host.c new file +1 -0
+	struct tsm_host_subsys *hsubsys = (struct tsm_host_subsys *)

tdev->tsm; +

drivers/virt/coco/host/tsm-host.c new file +9 -0
+	if (WARN_ON(!hsubsys->ops->dev_status))
+		return -EPERM;
+
+	return hsubsys->ops->dev_status(tdev, s);
+}
+
+static int tsm_tdi_measurements_locked(struct tsm_dev *tdev)
+{
+	struct tsm_host_subsys *hsubsys = (struct tsm_host_subsys *)

tdev->tsm;

drivers/virt/coco/host/tsm-host.c new file +8 -0
+	int ret;
+
+	while (1) {
+		ret = hsubsys->ops->dev_measurements(tdev);
+		if (ret <= 0)
+			break;
+
+		ret = tdev->tsm_bus->ops->spdm_forward(&tdev->spdm,

ret);

drivers/virt/coco/host/tsm-host.c new file +11 -0
+		if (ret < 0)
+			break;
+	}
+
+	return ret;
+}
+
+static void tsm_tdi_reclaim(struct tsm_tdi *tdi)
+{
+	struct tsm_dev *tdev = tdi->tdev;
+	struct tsm_host_subsys *hsubsys = (struct tsm_host_subsys *)

tdev->tsm;

drivers/virt/coco/host/tsm-host.c new file +12 -0
+	int ret;
+
+	if (WARN_ON(!hsubsys->ops->tdi_unbind))
+		return;
+
+	mutex_lock(&tdi->tdev->spdm_mutex);
+	while (1) {
+		ret = hsubsys->ops->tdi_unbind(tdi);
+		if (ret <= 0)
+			break;
+
+		ret =

tdi->tdev->tsm_bus->ops->spdm_forward(&tdi->tdev->spdm, ret);

drivers/virt/coco/host/tsm-host.c new file +7 -0
+		if (ret < 0)
+			break;
+	}
+	mutex_unlock(&tdi->tdev->spdm_mutex);
+}
+
+static int tsm_tdi_status(struct tsm_tdi *tdi, void *private_data,

struct tsm_tdi_status *ts) +{

drivers/virt/coco/host/tsm-host.c new file +3 -0
+	struct tsm_tdi_status tstmp = { 0 };
+	struct tsm_dev *tdev = tdi->tdev;
+	struct tsm_host_subsys *hsubsys = (struct tsm_host_subsys *)

tdev->tsm;

drivers/virt/coco/host/tsm-host.c new file +9 -0
+	int ret;
+
+	mutex_lock(&tdi->tdev->spdm_mutex);
+	while (1) {
+		ret = hsubsys->ops->tdi_status(tdi, &tstmp);
+		if (ret <= 0)
+			break;
+
+		ret =

tdi->tdev->tsm_bus->ops->spdm_forward(&tdi->tdev->spdm, ret);

drivers/virt/coco/host/tsm-host.c new file +12 -0
+		if (ret < 0)
+			break;
+	}
+	mutex_unlock(&tdi->tdev->spdm_mutex);
+
+	if (!ret)
+		*ts = tstmp;
+
+	return ret;
+}
+
+static ssize_t tsm_cert_slot_store(struct device *dev, struct

device_attribute *attr,

drivers/virt/coco/host/tsm-host.c new file +3 -0
+				   const char *buf, size_t count)
+{
+	struct tsm_dev *tdev = container_of(dev, struct tsm_dev,

dev);

drivers/virt/coco/host/tsm-host.c new file +12 -0
+	ssize_t ret = count;
+	unsigned long val;
+
+	if (kstrtoul(buf, 0, &val) < 0)
+		ret = -EINVAL;
+	else
+		tdev->cert_slot = val;
+
+	return ret;
+}
+
+static ssize_t tsm_cert_slot_show(struct device *dev, struct

device_attribute *attr, char *buf) +{

drivers/virt/coco/host/tsm-host.c new file +1 -0
+	struct tsm_dev *tdev = container_of(dev, struct tsm_dev,

dev);

drivers/virt/coco/host/tsm-host.c new file +8 -0
+	ssize_t ret = sysfs_emit(buf, "%u\n", tdev->cert_slot);
+
+	return ret;
+}
+
+static DEVICE_ATTR_RW(tsm_cert_slot);
+
+static ssize_t tsm_dev_connect_store(struct device *dev, struct

device_attribute *attr,

drivers/virt/coco/host/tsm-host.c new file +3 -0
+				     const char *buf, size_t count)
+{
+	struct tsm_dev *tdev = container_of(dev, struct tsm_dev,

dev);

drivers/virt/coco/host/tsm-host.c new file +17 -0
+	unsigned long val;
+	ssize_t ret = -EIO;
+
+	if (kstrtoul(buf, 0, &val) < 0)
+		ret = -EINVAL;
+	else if (val && !tdev->connected)
+		ret = tsm_dev_connect(tdev);
+	else if (!val && tdev->connected)
+		ret = tsm_dev_reclaim(tdev);
+
+	if (!ret)
+		ret = count;
+
+	return ret;
+}
+
+static ssize_t tsm_dev_connect_show(struct device *dev, struct

device_attribute *attr, char *buf) +{

drivers/virt/coco/host/tsm-host.c new file +1 -0
+	struct tsm_dev *tdev = container_of(dev, struct tsm_dev,

dev);

drivers/virt/coco/host/tsm-host.c new file +8 -0
+	ssize_t ret = sysfs_emit(buf, "%u\n", tdev->connected);
+
+	return ret;
+}
+
+static DEVICE_ATTR_RW(tsm_dev_connect);
+
+static ssize_t tsm_dev_status_show(struct device *dev, struct

device_attribute *attr, char *buf) +{

drivers/virt/coco/host/tsm-host.c new file +1 -0
+	struct tsm_dev *tdev = container_of(dev, struct tsm_dev,

dev);

drivers/virt/coco/host/tsm-host.c new file +38 -0
+	struct tsm_dev_status s = { 0 };
+	int ret = tsm_dev_status(tdev, &s);
+	ssize_t ret1;
+
+	ret1 = sysfs_emit(buf, "ret=%d\n"
+			  "ctx_state=%x\n"
+			  "tc_mask=%x\n"
+			  "certs_slot=%x\n"
+			  "device_id=%x:%x.%d\n"
+			  "segment_id=%x\n"
+			  "no_fw_update=%x\n",
+			  ret,
+			  s.ctx_state,
+			  s.tc_mask,
+			  s.certs_slot,
+			  (s.device_id >> 8) & 0xff,
+			  (s.device_id >> 3) & 0x1f,
+			  s.device_id & 0x07,
+			  s.segment_id,
+			  s.no_fw_update);
+
+	tsm_dev_put(tdev);
+	return ret1;
+}
+
+static DEVICE_ATTR_RO(tsm_dev_status);
+
+static struct attribute *host_dev_attrs[] = {
+	&dev_attr_tsm_cert_slot.attr,
+	&dev_attr_tsm_dev_connect.attr,
+	&dev_attr_tsm_dev_status.attr,
+	NULL,
+};
+static const struct attribute_group host_dev_group = {
+	.attrs = host_dev_attrs,
+};
+
+static ssize_t tsm_tdi_bind_show(struct device *dev, struct

device_attribute *attr, char *buf) +{

drivers/virt/coco/host/tsm-host.c new file +20 -0
+	struct tsm_tdi *tdi = container_of(dev, struct tsm_tdi, dev);
+
+	if (!tdi->kvm)
+		return sysfs_emit(buf, "not bound\n");
+
+	return sysfs_emit(buf, "VM=%p BDFn=%x:%x.%d\n",
+			  tdi->kvm,
+			  (tdi->guest_rid >> 8) & 0xff,
+			  (tdi->guest_rid >> 3) & 0x1f,
+			  tdi->guest_rid & 0x07);
+}
+
+static DEVICE_ATTR_RO(tsm_tdi_bind);
+
+static char *spdm_algos_to_str(u64 algos, char *buf, size_t len)
+{
+	size_t n = 0;
+
+	buf[0] = 0;
+#define __ALGO(x) do {
			\
drivers/virt/coco/host/tsm-host.c new file +1 -0
+		if ((n < len) && (algos & (1ULL <<

(TSM_TDI_SPDM_ALGOS_##x)))) \

drivers/virt/coco/host/tsm-host.c new file +1 -0
+			n += snprintf(buf + n, len - n, #x"

"); \

drivers/virt/coco/host/tsm-host.c new file +32 -0
+	} while (0)
+
+	__ALGO(DHE_SECP256R1);
+	__ALGO(DHE_SECP384R1);
+	__ALGO(AEAD_AES_128_GCM);
+	__ALGO(AEAD_AES_256_GCM);
+	__ALGO(ASYM_TPM_ALG_RSASSA_3072);
+	__ALGO(ASYM_TPM_ALG_ECDSA_ECC_NIST_P256);
+	__ALGO(ASYM_TPM_ALG_ECDSA_ECC_NIST_P384);
+	__ALGO(HASH_TPM_ALG_SHA_256);
+	__ALGO(HASH_TPM_ALG_SHA_384);
+	__ALGO(KEY_SCHED_SPDM_KEY_SCHEDULE);
+#undef __ALGO
+	return buf;
+}
+
+static const char *tdisp_state_to_str(enum tsm_tdisp_state state)
+{
+	switch (state) {
+#define __ST(x) case TDISP_STATE_##x: return #x
+	case TDISP_STATE_UNAVAIL: return "TDISP state unavailable";
+	__ST(CONFIG_UNLOCKED);
+	__ST(CONFIG_LOCKED);
+	__ST(RUN);
+	__ST(ERROR);
+#undef __ST
+	default: return "unknown";
+	}
+}
+
+static ssize_t tsm_tdi_status_user_show(struct device *dev,
+					struct device_attribute

*attr,

drivers/virt/coco/host/tsm-host.c new file +5 -0
+					char *buf)
+{
+	struct tsm_tdi *tdi = container_of(dev, struct tsm_tdi, dev);
+	struct tsm_dev *tdev = tdi->tdev;
+	struct tsm_host_subsys *hsubsys = (struct tsm_host_subsys *)

tdev->tsm;

drivers/virt/coco/host/tsm-host.c new file +12 -0
+	struct tsm_tdi_status ts = { 0 };
+	char algos[256] = "";
+	unsigned int n, m;
+	int ret;
+
+	ret = tsm_tdi_status(tdi, hsubsys->private_data, &ts);
+	if (ret < 0)
+		return sysfs_emit(buf, "ret=%d\n\n", ret);
+
+	if (!ts.valid)
+		return sysfs_emit(buf, "ret=%d\nstate=%d:%s\n",
+				  ret, ts.state,

tdisp_state_to_str(ts.state)); +

drivers/virt/coco/host/tsm-host.c new file +23 -0
+	n = snprintf(buf, PAGE_SIZE,
+		     "ret=%d\n"
+		     "state=%d:%s\n"
+		     "meas_digest_fresh=%x\n"
+		     "meas_digest_valid=%x\n"
+		     "all_request_redirect=%x\n"
+		     "bind_p2p=%x\n"
+		     "lock_msix=%x\n"
+		     "no_fw_update=%x\n"
+		     "cache_line_size=%d\n"
+		     "algos=%#llx:%s\n"
+		     "report_counter=%lld\n"
+		     ,
+		     ret,
+		     ts.state, tdisp_state_to_str(ts.state),
+		     ts.meas_digest_fresh,
+		     ts.meas_digest_valid,
+		     ts.all_request_redirect,
+		     ts.bind_p2p,
+		     ts.lock_msix,
+		     ts.no_fw_update,
+		     ts.cache_line_size,
+		     ts.spdm_algos, spdm_algos_to_str(ts.spdm_algos,

algos, sizeof(algos) - 1),

drivers/virt/coco/host/tsm-host.c new file +4 -0
+		     ts.intf_report_counter);
+
+	n += snprintf(buf + n, PAGE_SIZE - n, "Certs digest: ");
+	m = hex_dump_to_buffer(ts.certs_digest,

sizeof(ts.certs_digest), 32, 1,

drivers/virt/coco/host/tsm-host.c new file +3 -0
+			       buf + n, PAGE_SIZE - n, false);
+	n += min(PAGE_SIZE - n, m);
+	n += snprintf(buf + n, PAGE_SIZE - n, "...\nMeasurements

digest: ");

drivers/virt/coco/host/tsm-host.c new file +1 -0
+	m = hex_dump_to_buffer(ts.meas_digest,

sizeof(ts.meas_digest), 32, 1,

drivers/virt/coco/host/tsm-host.c new file +3 -0
+			       buf + n, PAGE_SIZE - n, false);
+	n += min(PAGE_SIZE - n, m);
+	n += snprintf(buf + n, PAGE_SIZE - n, "...\nInterface report

digest: ");

drivers/virt/coco/host/tsm-host.c new file +1 -0
+	m = hex_dump_to_buffer(ts.interface_report_digest,

sizeof(ts.interface_report_digest),

drivers/virt/coco/host/tsm-host.c new file +10 -0
+			       32, 1, buf + n, PAGE_SIZE - n, false);
+	n += min(PAGE_SIZE - n, m);
+	n += snprintf(buf + n, PAGE_SIZE - n, "...\n");
+
+	return n;
+}
+
+static DEVICE_ATTR_RO(tsm_tdi_status_user);
+
+static ssize_t tsm_tdi_status_show(struct device *dev, struct

device_attribute *attr, char *buf) +{

drivers/virt/coco/host/tsm-host.c new file +3 -0
+	struct tsm_tdi *tdi = container_of(dev, struct tsm_tdi, dev);
+	struct tsm_dev *tdev = tdi->tdev;
+	struct tsm_host_subsys *hsubsys = (struct tsm_host_subsys *)

tdev->tsm;

drivers/virt/coco/host/tsm-host.c new file +31 -0
+	struct tsm_tdi_status ts = { 0 };
+	u8 state;
+	int ret;
+
+	ret = tsm_tdi_status(tdi, hsubsys->private_data, &ts);
+	if (ret)
+		return ret;
+
+	state = ts.state;
+	memcpy(buf, &state, sizeof(state));
+
+	return sizeof(state);
+}
+
+static DEVICE_ATTR_RO(tsm_tdi_status);
+
+static struct attribute *host_tdi_attrs[] = {
+	&dev_attr_tsm_tdi_bind.attr,
+	&dev_attr_tsm_tdi_status_user.attr,
+	&dev_attr_tsm_tdi_status.attr,
+	NULL,
+};
+
+static const struct attribute_group host_tdi_group = {
+	.attrs = host_tdi_attrs,
+};
+
+int tsm_tdi_bind(struct tsm_tdi *tdi, u32 guest_rid, int kvmfd)
+{
+	struct tsm_dev *tdev = tdi->tdev;
+	struct tsm_host_subsys *hsubsys = (struct tsm_host_subsys *)

tdev->tsm;

drivers/virt/coco/host/tsm-host.c new file +45 -0
+	struct fd f = fdget(kvmfd);
+	struct kvm *kvm;
+	u64 vmid;
+	int ret;
+
+	if (!fd_file(f))
+		return -EBADF;
+
+	if (!file_is_kvm(fd_file(f))) {
+		ret = -EBADF;
+		goto out_fput;
+	}
+
+	kvm = fd_file(f)->private_data;
+	if (!kvm || !kvm_get_kvm_safe(kvm)) {
+		ret = -EFAULT;
+		goto out_fput;
+	}
+
+	vmid = kvm_arch_tsm_get_vmid(kvm);
+	if (!vmid) {
+		ret = -EFAULT;
+		goto out_kvm_put;
+	}
+
+	if (WARN_ON(!hsubsys->ops->tdi_bind)) {
+		ret = -EPERM;
+		goto out_kvm_put;
+	}
+
+	if (!tdev->connected) {
+		ret = -EIO;
+		goto out_kvm_put;
+	}
+
+	mutex_lock(&tdi->tdev->spdm_mutex);
+	while (1) {
+		ret = hsubsys->ops->tdi_bind(tdi, guest_rid, vmid);
+		if (ret < 0)
+			break;
+
+		if (!ret)
+			break;
+
+		ret =

tdi->tdev->tsm_bus->ops->spdm_forward(&tdi->tdev->spdm, ret);

drivers/virt/coco/host/tsm-host.c new file +38 -0
+		if (ret < 0)
+			break;
+	}
+	mutex_unlock(&tdi->tdev->spdm_mutex);
+
+	if (ret) {
+		tsm_tdi_unbind(tdi);
+		goto out_kvm_put;
+	}
+
+	tdi->guest_rid = guest_rid;
+	tdi->kvm = kvm;
+	++tdi->tdev->bound;
+	goto out_fput;
+
+out_kvm_put:
+	kvm_put_kvm(kvm);
+out_fput:
+	fdput(f);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(tsm_tdi_bind);
+
+void tsm_tdi_unbind(struct tsm_tdi *tdi)
+{
+	if (tdi->kvm) {
+		tsm_tdi_reclaim(tdi);
+		--tdi->tdev->bound;
+		kvm_put_kvm(tdi->kvm);
+		tdi->kvm = NULL;
+	}
+
+	tdi->guest_rid = 0;
+	tdi->dev.parent->tdi_enabled = false;
+}
+EXPORT_SYMBOL_GPL(tsm_tdi_unbind);
+
+int tsm_guest_request(struct tsm_tdi *tdi, u8 __user *req, size_t

reqlen,

drivers/virt/coco/host/tsm-host.c new file +4 -0
+		      u8 __user *res, size_t reslen, int *fw_err)
+{
+	struct tsm_dev *tdev = tdi->tdev;
+	struct tsm_host_subsys *hsubsys = (struct tsm_host_subsys *)

tdev->tsm;

drivers/virt/coco/host/tsm-host.c new file +9 -0
+	int ret;
+
+	if (!hsubsys->ops->guest_request)
+		return -EPERM;
+
+	mutex_lock(&tdi->tdev->spdm_mutex);
+	while (1) {
+		ret = hsubsys->ops->guest_request(tdi, req, reqlen,
+						  res, reslen,

fw_err);

drivers/virt/coco/host/tsm-host.c new file +4 -0
+		if (ret <= 0)
+			break;
+
+		ret =

tdi->tdev->tsm_bus->ops->spdm_forward(&tdi->tdev->spdm,

drivers/virt/coco/host/tsm-host.c new file +16 -0
+							    ret);
+		if (ret < 0)
+			break;
+	}
+
+	mutex_unlock(&tdi->tdev->spdm_mutex);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(tsm_guest_request);
+
+struct tsm_host_subsys *tsm_host_register(struct device *parent,
+					  struct tsm_hv_ops *hvops,
+					  void *private_data)
+{
+	struct tsm_subsys *subsys = tsm_register(parent,

sizeof(struct tsm_host_subsys),

drivers/virt/coco/host/tsm-host.c new file +1 -0
+						 &host_dev_group,

&host_tdi_group,
+
tsm_tdi_measurements_locked);

drivers/virt/coco/host/tsm-host.c new file +26 -0
+	struct tsm_host_subsys *hsubsys;
+
+	hsubsys = (struct tsm_host_subsys *) subsys;
+
+	if (IS_ERR(hsubsys))
+		return hsubsys;
+
+	hsubsys->ops = hvops;
+	hsubsys->private_data = private_data;
+
+	return hsubsys;
+}
+EXPORT_SYMBOL_GPL(tsm_host_register);
+
+static int __init tsm_init(void)
+{
+	int ret = 0;
+
+	pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
+
+	return ret;
+}
+
+static void __exit tsm_exit(void)
+{
+	pr_info(DRIVER_DESC " version: " DRIVER_VERSION "

shutdown\n"); +}

drivers/virt/coco/host/tsm-host.c new file +8 -0
+
+module_init(tsm_init);
+module_exit(tsm_exit);
+
+MODULE_VERSION(DRIVER_VERSION);
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR(DRIVER_AUTHOR);
+MODULE_DESCRIPTION(DRIVER_DESC);
drivers/virt/coco/tsm.c new file +54 -0
new file mode 100644
index 000000000000..b6235d1210ca
@@ -0,0 +1,636 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/module.h>
+#include <linux/tsm.h>
+
+#define DRIVER_VERSION	"0.1"
+#define DRIVER_AUTHOR	"[email protected]"
+#define DRIVER_DESC	"TSM library"
+
+static struct class *tsm_class, *tdev_class, *tdi_class;
+
+/* snprintf does not check for the size, hence this wrapper */
+static int tsmprint(char *buf, size_t size, const char *fmt, ...)
+{
+	va_list args;
+	size_t i;
+
+	if (!size)
+		return 0;
+
+	va_start(args, fmt);
+	i = vsnprintf(buf, size, fmt, args);
+	va_end(args);
+
+	return min(i, size);
+}
+
+struct tsm_blob *tsm_blob_new(void *data, size_t len)
+{
+	struct tsm_blob *b;
+
+	if (!len || !data)
+		return NULL;
+
+	b = kzalloc(sizeof(*b) + len, GFP_KERNEL);
+	if (!b)
+		return NULL;
+
+	b->data = (void *)b + sizeof(*b);
+	b->len = len;
+	memcpy(b->data, data, len);
+
+	return b;
+}
+EXPORT_SYMBOL_GPL(tsm_blob_new);
+
+static int match_class(struct device *dev, const void *data)
+{
+	return dev->class == data;
+}
+
+struct tsm_dev *tsm_dev_get(struct device *parent)
+{
+	struct device *dev = device_find_child(parent, tdev_class,

match_class); +

drivers/virt/coco/tsm.c new file +2 -0
+	if (!dev) {
+		dev = device_find_child(parent, tdi_class,

match_class);

drivers/virt/coco/tsm.c new file +2 -0
+		if (dev) {
+			struct tsm_tdi *tdi = container_of(dev,

struct tsm_tdi, dev); +

drivers/virt/coco/tsm.c new file +21 -0
+			dev = &tdi->tdev->dev;
+		}
+	}
+
+	if (!dev)
+		return NULL;
+
+	/* device_find_child() does get_device() */
+	return container_of(dev, struct tsm_dev, dev);
+}
+EXPORT_SYMBOL_GPL(tsm_dev_get);
+
+void tsm_dev_put(struct tsm_dev *tdev)
+{
+	put_device(&tdev->dev);
+}
+EXPORT_SYMBOL_GPL(tsm_dev_put);
+
+struct tsm_tdi *tsm_tdi_get(struct device *parent)
+{
+	struct device *dev = device_find_child(parent, tdi_class,

match_class); +

drivers/virt/coco/tsm.c new file +31 -0
+	if (!dev)
+		return NULL;
+
+	/* device_find_child() does get_device() */
+	return container_of(dev, struct tsm_tdi, dev);
+}
+EXPORT_SYMBOL_GPL(tsm_tdi_get);
+
+void tsm_tdi_put(struct tsm_tdi *tdi)
+{
+	put_device(&tdi->dev);
+}
+EXPORT_SYMBOL_GPL(tsm_tdi_put);
+
+static ssize_t blob_show(struct tsm_blob *blob, char *buf)
+{
+	unsigned int n, m;
+	size_t sz = PAGE_SIZE - 1;
+
+	if (!blob)
+		return sysfs_emit(buf, "none\n");
+
+	n = tsmprint(buf, sz, "%lu %u\n", blob->len);
+	m = hex_dump_to_buffer(blob->data, blob->len, 32, 1,
+			       buf + n, sz - n, false);
+	n += min(sz - n, m);
+	n += tsmprint(buf + n, sz - n, "...\n");
+	return n;
+}
+
+static ssize_t tsm_certs_gen(struct tsm_blob *certs, char *buf,

size_t len) +{

drivers/virt/coco/tsm.c new file +6 -0
+	struct spdm_certchain_block_header *h;
+	unsigned int n = 0, m, i, off, o2;
+	u8 *p;
+
+	for (i = 0, off = 0; off < certs->len; ++i) {
+		h = (struct spdm_certchain_block_header *) ((u8

*)certs->data + off);

drivers/virt/coco/tsm.c new file +4 -0
+		if (WARN_ON_ONCE(h->length > certs->len - off))
+			return 0;
+
+		n += tsmprint(buf + n, len - n, "[%d] len=%d:\n", i,

h->length); +

drivers/virt/coco/tsm.c new file +1 -0
+		for (o2 = 0, p = (u8 *)&h[1]; o2 < h->length; o2 +=

32) {

drivers/virt/coco/tsm.c new file +1 -0
+			m = hex_dump_to_buffer(p + o2, h->length -

o2, 32, 1,

drivers/virt/coco/tsm.c new file +1 -0
+					       buf + n, len - n,

true);

drivers/virt/coco/tsm.c new file +19 -0
+			n += min(len - n, m);
+			n += tsmprint(buf + n, len - n, "\n");
+		}
+
+		off += h->length; /* Includes the header */
+	}
+
+	return n;
+}
+
 
+
+void tsm_dev_free(struct tsm_dev *tdev)
+{
+	dev_notice(&tdev->dev, "Freeing tdevice\n");
+	device_unregister(&tdev->dev);
+}
+EXPORT_SYMBOL_GPL(tsm_dev_free);
+
+int tsm_create_link(struct tsm_subsys *tsm, struct device *dev,

const char *name) +{

drivers/virt/coco/tsm.c new file +11 -0
+	return sysfs_create_link(&tsm->dev.kobj, &dev->kobj, name);
+}
+EXPORT_SYMBOL_GPL(tsm_create_link);
+
+void tsm_remove_link(struct tsm_subsys *tsm, const char *name)
+{
+	sysfs_remove_link(&tsm->dev.kobj, name);
+}
+EXPORT_SYMBOL_GPL(tsm_remove_link);
+
+static struct tsm_subsys *alloc_tsm_subsys(struct device *parent,

size_t size) +{

drivers/virt/coco/tsm.c new file +19 -0
+	struct tsm_subsys *subsys;
+	struct device *dev;
+
+	if (WARN_ON_ONCE(size < sizeof(*subsys)))
+		return ERR_PTR(-EINVAL);
+
+	subsys = kzalloc(size, GFP_KERNEL);
+	if (!subsys)
+		return ERR_PTR(-ENOMEM);
+
+	dev = &subsys->dev;
+	dev->parent = parent;
+	dev->class = tsm_class;
+	device_initialize(dev);
+	return subsys;
+}
+
+struct tsm_subsys *tsm_register(struct device *parent, size_t size,
+				const struct attribute_group

*tdev_ag,

drivers/virt/coco/tsm.c new file +2 -0
+				const struct attribute_group *tdi_ag,
+				int (*update_measurements)(struct

tsm_dev *tdev)) +{

drivers/virt/coco/tsm.c new file +35 -0
+	struct tsm_subsys *subsys = alloc_tsm_subsys(parent, size);
+	struct device *dev;
+	int rc;
+
+	if (IS_ERR(subsys))
+		return subsys;
+
+	dev = &subsys->dev;
+	rc = dev_set_name(dev, "tsm0");
+	if (rc)
+		return ERR_PTR(rc);
+
+	rc = device_add(dev);
+	if (rc)
+		return ERR_PTR(rc);
+
+	subsys->tdev_groups[0] = &dev_group;
+	subsys->tdev_groups[1] = tdev_ag;
+	subsys->tdi_groups[0] = &tdi_group;
+	subsys->tdi_groups[1] = tdi_ag;
+	subsys->update_measurements = update_measurements;
+
+	return subsys;
+}
+EXPORT_SYMBOL_GPL(tsm_register);
+
+void tsm_unregister(struct tsm_subsys *subsys)
+{
+	device_unregister(&subsys->dev);
+}
+EXPORT_SYMBOL_GPL(tsm_unregister);
+
+static void tsm_release(struct device *dev)
+{
+	struct tsm_subsys *tsm = container_of(dev, typeof(*tsm),

dev); +

drivers/virt/coco/tsm.c new file +18 -0
+	dev_info(&tsm->dev, "Releasing TSM\n");
+	kfree(tsm);
+}
+
+static void tdev_release(struct device *dev)
+{
+	struct tsm_dev *tdev = container_of(dev, typeof(*tdev), dev);
+
+	dev_info(&tdev->dev, "Releasing %s TDEV\n",
+		 tdev->connected ? "connected":"disconnected");
+	kfree(tdev);
+}
+
+static void tdi_release(struct device *dev)
+{
+	struct tsm_tdi *tdi = container_of(dev, typeof(*tdi), dev);
+
+	dev_info(&tdi->dev, "Releasing %s TDI\n", tdi->kvm ? "bound"

: "unbound");

drivers/virt/coco/tsm.c new file +31 -0
+	sysfs_remove_link(&tdi->dev.parent->kobj, "tsm_dev");
+	kfree(tdi);
+}
+
+static int __init tsm_init(void)
+{
+	int ret = 0;
+
+	pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
+
+	tsm_class = class_create("tsm");
+	if (IS_ERR(tsm_class))
+		return PTR_ERR(tsm_class);
+	tsm_class->dev_release = tsm_release;
+
+	tdev_class = class_create("tsm-dev");
+	if (IS_ERR(tdev_class))
+		return PTR_ERR(tdev_class);
+	tdev_class->dev_release = tdev_release;
+
+	tdi_class = class_create("tsm-tdi");
+	if (IS_ERR(tdi_class))
+		return PTR_ERR(tdi_class);
+	tdi_class->dev_release = tdi_release;
+
+	return ret;
+}
+
+static void __exit tsm_exit(void)
+{
+	pr_info(DRIVER_DESC " version: " DRIVER_VERSION "

shutdown\n");

drivers/virt/coco/tsm.c new file +12 -0
+	class_destroy(tdi_class);
+	class_destroy(tdev_class);
+	class_destroy(tsm_class);
+}
+
+module_init(tsm_init);
+module_exit(tsm_exit);
+
+MODULE_VERSION(DRIVER_VERSION);
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR(DRIVER_AUTHOR);
+MODULE_DESCRIPTION(DRIVER_DESC);
(quoted diff) +0 -0

b/Documentation/virt/coco/tsm.rst new file mode 100644 index 000000000000..7cb5f1862492

Documentation/virt/coco/tsm.rst new file +6 -0
@@ -0,0 +1,99 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+What it is
+==========
+
+This is for PCI passthrough in confidential computing (CoCo:

SEV-SNP, TDX, CoVE). +Currently passing through PCI devices to a CoCo VM uses SWIOTLB to pre-shared +memory buffers.

Documentation/virt/coco/tsm.rst new file +2 -0
+
+PCIe IDE (Integrity and Data Encryption) and TDISP (TEE Device

Interface Security +Protocol) are protocols to enable encryption over PCIe link and DMA to encrypted +memory. This doc is focused to DMAing to encrypted VM, the encrypted host memory is +out of scope.

Documentation/virt/coco/tsm.rst new file +6 -0
+
+
+Protocols
+=========
+
+PCIe r6 DOE is a mailbox protocol to read/write object from/to

device. +Objects are of plain SPDM or secure SPDM type. SPDM is responsible for authenticating +devices, creating a secure link between a device and TSM. +IDE_KM manages PCIe link encryption keys, it works on top of secure SPDM. +TDISP manages a passed through PCI function state, also works on top on secure SPDM. +Additionally, PCIe defines IDE capability which provides the host OS a way +to enable streams on the PCIe link. +

Documentation/virt/coco/tsm.rst new file +7 -0
+
+TSM modules
+===========
+
+TSM is a library, shared among hosts and guests.
+
+TSM-HOST contains host-specific bits, controls IDE and TDISP

bindings. +
+TSM-GUEST contains guest-specific bits, controls enablement of encrypted DMA and +MMIO.

Documentation/virt/coco/tsm.rst new file +2 -0
+
+TSM-PCI is PCI binding for TSM, calls the above libraries for

setting up +sysfs nodes and corresponding data structures.

Documentation/virt/coco/tsm.rst new file +6 -0
+
+
+Flow
+====
+
+At the boot time the tsm.ko scans the PCI bus to find and setup

TDISP-cabable +devices; it also listens to hotplug events. If setup was successful, tsm-prefixed +nodes will appear in sysfs.

Documentation/virt/coco/tsm.rst new file +2 -0
+
+Then, the user enables IDE by writing to

/sys/bus/pci/devices/0000:e1:00.0/tsm_dev_connect +and this is how PCIe encryption is enabled. +

Documentation/virt/coco/tsm.rst new file +3 -0
+To pass the device through, a modifined VMM is required.
+
+In the VM, the same tsm.ko loads. In addition to the host's setup,

the VM wants +to receive the report and enable secure DMA or/and secure MMIO, via some VM<->HV +protocol (such as AMD GHCB). Once this is done, a VM can access validated MMIO +with the Cbit set and the device can DMA to encrypted memory. +

Documentation/virt/coco/tsm.rst new file +41 -0
+The sysfs example from a host with a TDISP capable device:
+
+~> find /sys -iname "*tsm*"  
+/sys/class/tsm-tdi
+/sys/class/tsm
+/sys/class/tsm/tsm0
+/sys/class/tsm-dev
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.1/tsm_dev
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.1/tsm-tdi
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.1/tsm-tdi/tdi:0000:e1:00.1/tsm_tdi_bind
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.1/tsm-tdi/tdi:0000:e1:00.1/tsm_tdi_status
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.1/tsm-tdi/tdi:0000:e1:00.1/tsm_tdi_status_user
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.1/tsm-tdi/tdi:0000:e1:00.1/tsm_report_user
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.1/tsm-tdi/tdi:0000:e1:00.1/tsm_report
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm_dev
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-tdi
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-tdi/tdi:0000:e1:00.0/tsm_tdi_bind
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-tdi/tdi:0000:e1:00.0/tsm_tdi_status
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-tdi/tdi:0000:e1:00.0/tsm_tdi_status_user
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-tdi/tdi:0000:e1:00.0/tsm_report_user
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-tdi/tdi:0000:e1:00.0/tsm_report
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-dev
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-dev/tdev:0000:e1:00.0/tsm_certs
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-dev/tdev:0000:e1:00.0/tsm_nonce
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-dev/tdev:0000:e1:00.0/tsm_meas_user
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-dev/tdev:0000:e1:00.0/tsm_certs_user
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-dev/tdev:0000:e1:00.0/tsm_dev_status
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-dev/tdev:0000:e1:00.0/tsm_cert_slot
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-dev/tdev:0000:e1:00.0/tsm_dev_connect
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-dev/tdev:0000:e1:00.0/tsm_meas
+/sys/devices/pci0000:a0/0000:a0:07.1/0000:a9:00.5/tsm
+/sys/devices/pci0000:a0/0000:a0:07.1/0000:a9:00.5/tsm/tsm0
+
+
+References
+==========
+
+[1] TEE Device Interface Security Protocol - TDISP - v2022-07-27
+https://members.pcisig.com/wg/PCI-SIG/document/18268?downloadRevision=21500
+[2] Security Protocol and Data Model (SPDM)
+https://www.dmtf.org/sites/default/files/standards/documents/DSP0274_1.2.1.pdf
drivers/virt/coco/Kconfig +2 -0
index 819a97e8ba99..e4385247440b 100644
@@ -3,6 +3,18 @@
 # Confidential computing related collateral
 #
 
+config TSM
+	tristate "Platform support for TEE Device Interface Security

Protocol (TDISP)"

drivers/virt/coco/Kconfig +6 -0
+	default m
+	depends on AMD_MEM_ENCRYPT
+	select PCI_DOE
+	select PCI_IDE
+	help
+	  Add a common place for user visible platform support for

PCIe TDISP.

drivers/virt/coco/Kconfig +1 -0
+	  TEE Device Interface Security Protocol (TDISP) from

PCI-SIG,
+
https://pcisig.com/tee-device-interface-security-protocol-tdisp

drivers/virt/coco/Kconfig +4 -0
+	  This is prerequisite for host and guest support.
+
 source "drivers/virt/coco/efi_secret/Kconfig"
 
 source "drivers/virt/coco/pkvm-guest/Kconfig"
@@ -14,3 +26,5 @@ source "drivers/virt/coco/tdx-guest/Kconfig"
 source "drivers/virt/coco/arm-cca-guest/Kconfig"
 
 source "drivers/virt/coco/guest/Kconfig"
+
+source "drivers/virt/coco/host/Kconfig"
(quoted diff) +0 -0

b/drivers/virt/coco/host/Kconfig new file mode 100644 index 000000000000..3bde38b91fd4

drivers/virt/coco/host/Kconfig new file +6 -0
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# TSM (TEE Security Manager) Common infrastructure and host drivers
+#
+config TSM_HOST
+	tristate
#79 lore raw

Re: [RFC PATCH v2 15/22] KVM: X86: Handle private MMIO as shared

In reply to Alexey Kardashevskiy
Quoted 32 lines · On Tue, 18 Feb 2025 22:10:02 +1100 Alexey Kardashevskiy <[email protected]> wrote:

Currently private MMIO nested page faults are not expected so when such fault occurs, KVM tries moving the faulted page from private to shared which is not going to work as private MMIO is not backed by memfd.

Handle private MMIO as shared: skip page state change and memfd page state tracking.

The MMIO KVM memory slot is still marked as shared as the guest can access it as private or shared so marking the MMIO slot as private is not going to help.

---
 arch/x86/kvm/mmu/mmu.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +1 -1
index 74c20dbb92da..32e27080b1c7 100644
@@ -4347,7 +4347,11 @@ static int __kvm_mmu_faultin_pfn(struct
 kvm_vcpu *vcpu, {
 	unsigned int foll = fault->write ? FOLL_WRITE : 0;
 
-	if (fault->is_private)
+	if (fault->slot && fault->is_private &&

!kvm_slot_can_be_private(fault->slot) &&

arch/x86/kvm/mmu/mmu.c +2 -0
+	    (vcpu->kvm->arch.vm_type == KVM_X86_SNP_VM))
+		pr_warn("%s: private SEV TIO MMIO fault for

fault->gfn=%llx\n",

arch/x86/kvm/mmu/mmu.c static int __kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu, · line 4355

Quoted 3 lines
arch/x86/kvm/mmu/mmu.c +2 -0
+			__func__, fault->gfn);
+	else if (fault->is_private)
 		return kvm_mmu_faultin_pfn_private(vcpu, fault);

Let's fold this in a macro and make this more informative with comments.

Quoted 1 line

foll |= FOLL_NOWAIT;

author maintainer
#80 lore raw

Re: [RFC PATCH v2 15/22] KVM: X86: Handle private MMIO as shared

Quoted 28 lines · Zhi Wang
Quoted 25 lines · On Tue, 18 Feb 2025 22:10:02 +1100 Alexey Kardashevskiy <[email protected]> wrote:

Currently private MMIO nested page faults are not expected so when such fault occurs, KVM tries moving the faulted page from private to shared which is not going to work as private MMIO is not backed by memfd.

Handle private MMIO as shared: skip page state change and memfd page state tracking.

The MMIO KVM memory slot is still marked as shared as the guest can access it as private or shared so marking the MMIO slot as private is not going to help.

---
  arch/x86/kvm/mmu/mmu.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +0 -0
index 74c20dbb92da..32e27080b1c7 100644
@@ -4347,7 +4347,11 @@ static int __kvm_mmu_faultin_pfn(struct
 kvm_vcpu *vcpu, {
  	unsigned int foll = fault->write ? FOLL_WRITE : 0;
 
Quoted 12 lines

arch/x86/kvm/mmu/mmu.c static int __kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu, · line 4355

Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +1 -1
-	if (fault->is_private)
+	if (fault->slot && fault->is_private &&

!kvm_slot_can_be_private(fault->slot) &&

arch/x86/kvm/mmu/mmu.c +2 -0
+	    (vcpu->kvm->arch.vm_type == KVM_X86_SNP_VM))
+		pr_warn("%s: private SEV TIO MMIO fault for

fault->gfn=%llx\n",

arch/x86/kvm/mmu/mmu.c +2 -0
+			__func__, fault->gfn);
+	else if (fault->is_private)
  		return kvm_mmu_faultin_pfn_private(vcpu, fault);

Let's fold this in a macro and make this more informative with comments.

Rather than this, https://lore.kernel.org/r/[email protected] seems to be the way to go. Thanks,

Quoted 3 lines
Quoted 2 lines
  	foll |= FOLL_NOWAIT;
Signature · 1 line
Alexey
author maintainer
#18 lore raw

Re: [RFC PATCH v2 07/22] coco/tsm: Add tsm and tsm-host modules

In reply to Zhi Wang
Quoted 127 lines · Zhi Wang
Quoted 124 lines · On Tue, 18 Feb 2025 22:09:54 +1100 Alexey Kardashevskiy <[email protected]> wrote:

The TSM module is a library to create sysfs nodes common for hypervisors and VMs. It also provides helpers to parse interface reports (required by VMs, visible to HVs). It registers 3 device classes:
- tsm: one per platform,
- tsm-dev: for physical functions, ("TDEV");
- tdm-tdi: for PCI functions being assigned to VMs ("TDI").

The library adds a child device of "tsm-dev" or/and "tsm-tdi" class for every capable PCI device. Note that the module is made bus-agnostic.

New device nodes provide sysfs interface for fetching device certificates and measurements and TDI interface reports. Nodes with the "_user" suffix provide human-readable information, without that suffix it is raw binary data to be copied to a guest.

The TSM-HOST module adds hypervisor-only functionality on top. At the moment it is:
- "connect" to enable/disable IDE (a PCI link encryption);
- "TDI bind" to manage a PCI function passed through to a secure VM.

A platform is expected to register itself in TSM-HOST and provide necessary callbacks. No platform is added here, AMD SEV is coming in the next patches.

---
  drivers/virt/coco/Makefile        |   2 +
  drivers/virt/coco/host/Makefile   |   6 +
  include/linux/tsm.h               | 295 +++++++++
  drivers/virt/coco/host/tsm-host.c | 552 +++++++++++++++++
  drivers/virt/coco/tsm.c           | 636 ++++++++++++++++++++
  Documentation/virt/coco/tsm.rst   |  99 +++
  drivers/virt/coco/Kconfig         |  14 +
  drivers/virt/coco/host/Kconfig    |   6 +
  8 files changed, 1610 insertions(+)
drivers/virt/coco/Makefile +2 -0
index 885c9ef4e9fc..670f77c564e8 100644
@@ -2,9 +2,11 @@
  #
  # Confidential computing related collateral
  #
+obj-$(CONFIG_TSM)		+= tsm.o
  obj-$(CONFIG_EFI_SECRET)	+= efi_secret/
  obj-$(CONFIG_ARM_PKVM_GUEST)	+= pkvm-guest/
  obj-$(CONFIG_SEV_GUEST)		+= sev-guest/
  obj-$(CONFIG_INTEL_TDX_GUEST)	+= tdx-guest/
  obj-$(CONFIG_ARM_CCA_GUEST)	+= arm-cca-guest/
  obj-$(CONFIG_TSM_REPORTS)	+= guest/
+obj-$(CONFIG_TSM_HOST)          += host/
(quoted diff) +0 -0

b/drivers/virt/coco/host/Makefile new file mode 100644 index 000000000000..c5e216b6cb1c

drivers/virt/coco/host/Makefile new file +6 -0
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# TSM (TEE Security Manager) Common infrastructure and host drivers
+
+obj-$(CONFIG_TSM_HOST) += tsm_host.o
+tsm_host-y += tsm-host.o
include/linux/tsm.h +30 -0
index 431054810dca..486e386d90fc 100644
@@ -5,6 +5,11 @@
  #include <linux/sizes.h>
  #include <linux/types.h>
  #include <linux/uuid.h>
+#include <linux/device.h>
+#include <linux/slab.h>
+#include <linux/mutex.h>
+#include <linux/device.h>
+#include <linux/bitfield.h>
 
  #define TSM_REPORT_INBLOB_MAX 64
  #define TSM_REPORT_OUTBLOB_MAX SZ_32K
@@ -109,4 +114,294 @@ struct tsm_report_ops {
 
  int tsm_report_register(const struct tsm_report_ops *ops, void
 *priv); int tsm_report_unregister(const struct tsm_report_ops *ops);
+
+/* SPDM control structure for DOE */
+struct tsm_spdm {
+	unsigned long req_len;
+	void *req;
+	unsigned long rsp_len;
+	void *rsp;
+};
+
+/* Data object for measurements/certificates/attestationreport */
+struct tsm_blob {
+	void *data;
+	size_t len;
+};
+
+struct tsm_blob *tsm_blob_new(void *data, size_t len);
+static inline void tsm_blob_free(struct tsm_blob *b)
+{
+	kfree(b);
+}
+
+/**
+ * struct tdisp_interface_id - TDISP INTERFACE_ID Definition
+ *
+ * @function_id: Identifies the function of the device hosting the

TDI
+ * 15:0: @rid: Requester ID
+ * 23:16: @rseg: Requester Segment (Reserved if Requester Segment
Valid is Clear)
+ * 24: @rseg_valid: Requester Segment Valid
+ * 31:25 – Reserved
+ * 8B - Reserved
+ */

include/linux/tsm.h +5 -0
+struct tdisp_interface_id {
+	u32 function_id; /* TSM_TDISP_IID_xxxx */
+	u8 reserved[8];
+} __packed;
+
Quoted 8 lines

include/linux/tsm.h struct tsm_report_ops { · line 156

Quoted 4 lines
include/linux/tsm.h +4 -0
+#define TSM_TDISP_IID_REQUESTER_ID	GENMASK(15, 0)
+#define TSM_TDISP_IID_RSEG		GENMASK(23, 16)
+#define TSM_TDISP_IID_RSEG_VALID	BIT(24)
+

I would suggest that we have separate header files for spec definitions. E.g. tdisp_defs and spdm_defs.h. from the maintainability perspective.

True as this shares bits and pieces with Lukas'es CMA.

Quoted 38 lines
Quoted 9 lines
include/linux/tsm.h +9 -0
+/*
+ * Measurement block as defined in SPDM DSP0274.
+ */
+struct spdm_measurement_block_header {
+	u8 index;
+	u8 spec; /* MeasurementSpecification */
+	u16 size;
+} __packed;
+

....

Quoted 13 lines
include/linux/tsm.h +4 -0
+struct tsm_hv_ops {
+	int (*dev_connect)(struct tsm_dev *tdev, void *private_data);
+	int (*dev_disconnect)(struct tsm_dev *tdev);
+	int (*dev_status)(struct tsm_dev *tdev, struct

tsm_dev_status *s);

include/linux/tsm.h +4 -0
+	int (*dev_measurements)(struct tsm_dev *tdev);
+	int (*tdi_bind)(struct tsm_tdi *tdi, u32 bdfn, u64 vmid);
+	int (*tdi_unbind)(struct tsm_tdi *tdi);
+	int (*guest_request)(struct tsm_tdi *tdi, u8 __user *req,

size_t reqlen,

include/linux/tsm.h +1 -0
+			     u8 __user *rsp, size_t rsplen, int

*fw_err);

include/linux/tsm.h +1 -0
+	int (*tdi_status)(struct tsm_tdi *tdi, struct tsm_tdi_status
Quoted 2 lines

*ts); +};
+

1) Looks we have two more callbacks besides TDI verbs, I think they are fine to be in TSM driver ops.

For guest_request(), anyway, we need an entry point for QEMU to reach the TSM services in the kernel. Looks like almost all the platform (Intel/AMD/ARM) have TVM-HOST paths, which will exit to QEMU from KVM, and QEMU reaches the TSM services and return to the TVM. I think they can all leverage the entry point (IOMMUFD) via the guest request ioctl. And IOMMUFD almost have all the stuff QEMU needs.

Quoted 6 lines

Or we would end up with QEMU reaches to different entry points in per-vendor code path, which was not preferable, backing to the period when enabling CC in QEMU.

2) Also, it is better that we have separate the tsm_guest and tsm_host headers since the beginning.

+1.

Quoted 2 lines

3) How do you trigger the TDI_BIND from the guest in the late-bind model? Was looking at tsm_vm_ops, but seems not found yet.

It is implicit - to trigger TDI_BIND, the SNP VM needs to request TDI info (via GHCB) which exits to QEMU and QEMU then binds the TDI (if not bound already). This is made so in assumption that the VM is not just curious "what devices can be possibly trused" (the VM knows it from the PCIe capability) so it works.

How exactly the VM is going to trigger is a big questions, right now I have a hack to do that when the guest driver enables bus master and the TSM module(s) need to be loaded first. (we discussed elsewhere, just keeping it here for a record).

Quoted 123 lines
Quoted 123 lines
include/linux/tsm.h +5 -0
+struct tsm_subsys {
+	struct device dev;
+	struct list_head tdi_head;
+	struct mutex lock;
+	const struct attribute_group *tdev_groups[3]; /* Common,

host/guest, NULL */

include/linux/tsm.h +1 -0
+	const struct attribute_group *tdi_groups[3]; /* Common,

host/guest, NULL */

include/linux/tsm.h +5 -0
+	int (*update_measurements)(struct tsm_dev *tdev);
+};
+
+struct tsm_subsys *tsm_register(struct device *parent, size_t extra,
+				const struct attribute_group

*tdev_ag,

include/linux/tsm.h +2 -0
+				const struct attribute_group *tdi_ag,
+				int (*update_measurements)(struct

tsm_dev *tdev)); +void tsm_unregister(struct tsm_subsys *subsys);

include/linux/tsm.h +13 -0
+
+struct tsm_host_subsys;
+struct tsm_host_subsys *tsm_host_register(struct device *parent,
+					  struct tsm_hv_ops *hvops,
+					  void *private_data);
+struct tsm_dev *tsm_dev_get(struct device *dev);
+void tsm_dev_put(struct tsm_dev *tdev);
+struct tsm_tdi *tsm_tdi_get(struct device *dev);
+void tsm_tdi_put(struct tsm_tdi *tdi);
+
+struct pci_dev;
+int pci_dev_tdi_validate(struct pci_dev *pdev, bool invalidate);
+int pci_dev_tdi_mmio_config(struct pci_dev *pdev, u32 range_id, bool

tee); +
+int tsm_dev_init(struct tsm_bus_subsys *tsm_bus, struct device *parent,

include/linux/tsm.h +9 -0
+		 size_t busdatalen, struct tsm_dev **ptdev);
+void tsm_dev_free(struct tsm_dev *tdev);
+int tsm_tdi_init(struct tsm_dev *tdev, struct device *dev);
+void tsm_tdi_free(struct tsm_tdi *tdi);
+
+/* IOMMUFD vIOMMU helpers */
+int tsm_tdi_bind(struct tsm_tdi *tdi, u32 guest_rid, int kvmfd);
+void tsm_tdi_unbind(struct tsm_tdi *tdi);
+int tsm_guest_request(struct tsm_tdi *tdi, u8 __user *req, size_t

reqlen,

include/linux/tsm.h +7 -0
+		      u8 __user *res, size_t reslen, int *fw_err);
+
+/* Debug */
+ssize_t tsm_report_gen(struct tsm_blob *report, char *b, size_t len);
+
+/* IDE */
+int tsm_create_link(struct tsm_subsys *tsm, struct device *dev,

const char *name); +void tsm_remove_link(struct tsm_subsys *tsm, const char *name); +#define tsm_register_ide_stream(tdev, ide) \

include/linux/tsm.h +4 -0
+	tsm_create_link((tdev)->tsm, &(tdev)->dev, (ide)->name)
+#define tsm_unregister_ide_stream(tdev, ide) \
+	tsm_remove_link((tdev)->tsm, (ide)->name)
+
  #endif /* __TSM_H */
(quoted diff) +0 -0

b/drivers/virt/coco/host/tsm-host.c new file mode 100644 index 000000000000..80f3315fb195

drivers/virt/coco/host/tsm-host.c new file +20 -0
@@ -0,0 +1,552 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/module.h>
+#include <linux/tsm.h>
+#include <linux/file.h>
+#include <linux/kvm_host.h>
+
+#define DRIVER_VERSION	"0.1"
+#define DRIVER_AUTHOR	"[email protected]"
+#define DRIVER_DESC	"TSM host library"
+
+struct tsm_host_subsys {
+	struct tsm_subsys base;
+	struct tsm_hv_ops *ops;
+	void *private_data;
+};
+
+static int tsm_dev_connect(struct tsm_dev *tdev)
+{
+	struct tsm_host_subsys *hsubsys = (struct tsm_host_subsys *)

tdev->tsm;

drivers/virt/coco/host/tsm-host.c new file +11 -0
+	int ret;
+
+	if (WARN_ON(!hsubsys->ops->dev_connect))
+		return -EPERM;
+
+	if (WARN_ON(!tdev->tsm_bus))
+		return -EPERM;
+
+	mutex_lock(&tdev->spdm_mutex);
+	while (1) {
+		ret = hsubsys->ops->dev_connect(tdev,

hsubsys->private_data);

drivers/virt/coco/host/tsm-host.c new file +4 -0
+		if (ret <= 0)
+			break;
+
+		ret = tdev->tsm_bus->ops->spdm_forward(&tdev->spdm,

ret);

drivers/virt/coco/host/tsm-host.c new file +13 -0
+		if (ret < 0)
+			break;
+	}
+	mutex_unlock(&tdev->spdm_mutex);
+
+	tdev->connected = (ret == 0);
+
+	return ret;
+}
+
+static int tsm_dev_reclaim(struct tsm_dev *tdev)
+{
+	struct tsm_host_subsys *hsubsys = (struct tsm_host_subsys *)

tdev->tsm;

drivers/virt/coco/host/tsm-host.c new file +8 -0
+	int ret;
+
+	if (WARN_ON(!hsubsys->ops->dev_disconnect))
+		return -EPERM;
+
+	/* Do not disconnect with active TDIs */
+	if (tdev->bound)
+		return -EBUSY;
Quoted 8 lines
Quoted 1 line

+

Can this replace a lock? refcount is to track the life cycle, lock is to avoid racing. Think that we just pass here tdev->bound == 0, take the spdm_mutex and request the TSM to talk to the device for disconnection, while someone is calling tdi_bind and pass the tdev->connected check and waiting for the spdm_mutex to do the tdi_bind. The device might see a TDI_BIND after a DEVICE_DISCONNECT.

You're right, requires fixing. Thanks,

Quoted 977 lines

Z.

Quoted 974 lines
drivers/virt/coco/host/tsm-host.c new file +7 -0
+	mutex_lock(&tdev->spdm_mutex);
+	while (1) {
+		ret = hsubsys->ops->dev_disconnect(tdev);
+		if (ret <= 0)
+			break;
+
+		ret = tdev->tsm_bus->ops->spdm_forward(&tdev->spdm,

ret);

drivers/virt/coco/host/tsm-host.c new file +12 -0
+		if (ret < 0)
+			break;
+	}
+	mutex_unlock(&tdev->spdm_mutex);
+
+	if (!ret)
+		tdev->connected = false;
+
+	return ret;
+}
+
+static int tsm_dev_status(struct tsm_dev *tdev, struct

tsm_dev_status *s) +{

drivers/virt/coco/host/tsm-host.c new file +1 -0
+	struct tsm_host_subsys *hsubsys = (struct tsm_host_subsys *)

tdev->tsm; +

drivers/virt/coco/host/tsm-host.c new file +9 -0
+	if (WARN_ON(!hsubsys->ops->dev_status))
+		return -EPERM;
+
+	return hsubsys->ops->dev_status(tdev, s);
+}
+
+static int tsm_tdi_measurements_locked(struct tsm_dev *tdev)
+{
+	struct tsm_host_subsys *hsubsys = (struct tsm_host_subsys *)

tdev->tsm;

drivers/virt/coco/host/tsm-host.c new file +8 -0
+	int ret;
+
+	while (1) {
+		ret = hsubsys->ops->dev_measurements(tdev);
+		if (ret <= 0)
+			break;
+
+		ret = tdev->tsm_bus->ops->spdm_forward(&tdev->spdm,

ret);

drivers/virt/coco/host/tsm-host.c new file +11 -0
+		if (ret < 0)
+			break;
+	}
+
+	return ret;
+}
+
+static void tsm_tdi_reclaim(struct tsm_tdi *tdi)
+{
+	struct tsm_dev *tdev = tdi->tdev;
+	struct tsm_host_subsys *hsubsys = (struct tsm_host_subsys *)

tdev->tsm;

drivers/virt/coco/host/tsm-host.c new file +12 -0
+	int ret;
+
+	if (WARN_ON(!hsubsys->ops->tdi_unbind))
+		return;
+
+	mutex_lock(&tdi->tdev->spdm_mutex);
+	while (1) {
+		ret = hsubsys->ops->tdi_unbind(tdi);
+		if (ret <= 0)
+			break;
+
+		ret =

tdi->tdev->tsm_bus->ops->spdm_forward(&tdi->tdev->spdm, ret);

drivers/virt/coco/host/tsm-host.c new file +7 -0
+		if (ret < 0)
+			break;
+	}
+	mutex_unlock(&tdi->tdev->spdm_mutex);
+}
+
+static int tsm_tdi_status(struct tsm_tdi *tdi, void *private_data,

struct tsm_tdi_status *ts) +{

drivers/virt/coco/host/tsm-host.c new file +3 -0
+	struct tsm_tdi_status tstmp = { 0 };
+	struct tsm_dev *tdev = tdi->tdev;
+	struct tsm_host_subsys *hsubsys = (struct tsm_host_subsys *)

tdev->tsm;

drivers/virt/coco/host/tsm-host.c new file +9 -0
+	int ret;
+
+	mutex_lock(&tdi->tdev->spdm_mutex);
+	while (1) {
+		ret = hsubsys->ops->tdi_status(tdi, &tstmp);
+		if (ret <= 0)
+			break;
+
+		ret =

tdi->tdev->tsm_bus->ops->spdm_forward(&tdi->tdev->spdm, ret);

drivers/virt/coco/host/tsm-host.c new file +12 -0
+		if (ret < 0)
+			break;
+	}
+	mutex_unlock(&tdi->tdev->spdm_mutex);
+
+	if (!ret)
+		*ts = tstmp;
+
+	return ret;
+}
+
+static ssize_t tsm_cert_slot_store(struct device *dev, struct

device_attribute *attr,

drivers/virt/coco/host/tsm-host.c new file +3 -0
+				   const char *buf, size_t count)
+{
+	struct tsm_dev *tdev = container_of(dev, struct tsm_dev,

dev);

drivers/virt/coco/host/tsm-host.c new file +12 -0
+	ssize_t ret = count;
+	unsigned long val;
+
+	if (kstrtoul(buf, 0, &val) < 0)
+		ret = -EINVAL;
+	else
+		tdev->cert_slot = val;
+
+	return ret;
+}
+
+static ssize_t tsm_cert_slot_show(struct device *dev, struct

device_attribute *attr, char *buf) +{

drivers/virt/coco/host/tsm-host.c new file +1 -0
+	struct tsm_dev *tdev = container_of(dev, struct tsm_dev,

dev);

drivers/virt/coco/host/tsm-host.c new file +8 -0
+	ssize_t ret = sysfs_emit(buf, "%u\n", tdev->cert_slot);
+
+	return ret;
+}
+
+static DEVICE_ATTR_RW(tsm_cert_slot);
+
+static ssize_t tsm_dev_connect_store(struct device *dev, struct

device_attribute *attr,

drivers/virt/coco/host/tsm-host.c new file +3 -0
+				     const char *buf, size_t count)
+{
+	struct tsm_dev *tdev = container_of(dev, struct tsm_dev,

dev);

drivers/virt/coco/host/tsm-host.c new file +17 -0
+	unsigned long val;
+	ssize_t ret = -EIO;
+
+	if (kstrtoul(buf, 0, &val) < 0)
+		ret = -EINVAL;
+	else if (val && !tdev->connected)
+		ret = tsm_dev_connect(tdev);
+	else if (!val && tdev->connected)
+		ret = tsm_dev_reclaim(tdev);
+
+	if (!ret)
+		ret = count;
+
+	return ret;
+}
+
+static ssize_t tsm_dev_connect_show(struct device *dev, struct

device_attribute *attr, char *buf) +{

drivers/virt/coco/host/tsm-host.c new file +1 -0
+	struct tsm_dev *tdev = container_of(dev, struct tsm_dev,

dev);

drivers/virt/coco/host/tsm-host.c new file +8 -0
+	ssize_t ret = sysfs_emit(buf, "%u\n", tdev->connected);
+
+	return ret;
+}
+
+static DEVICE_ATTR_RW(tsm_dev_connect);
+
+static ssize_t tsm_dev_status_show(struct device *dev, struct

device_attribute *attr, char *buf) +{

drivers/virt/coco/host/tsm-host.c new file +1 -0
+	struct tsm_dev *tdev = container_of(dev, struct tsm_dev,

dev);

drivers/virt/coco/host/tsm-host.c new file +38 -0
+	struct tsm_dev_status s = { 0 };
+	int ret = tsm_dev_status(tdev, &s);
+	ssize_t ret1;
+
+	ret1 = sysfs_emit(buf, "ret=%d\n"
+			  "ctx_state=%x\n"
+			  "tc_mask=%x\n"
+			  "certs_slot=%x\n"
+			  "device_id=%x:%x.%d\n"
+			  "segment_id=%x\n"
+			  "no_fw_update=%x\n",
+			  ret,
+			  s.ctx_state,
+			  s.tc_mask,
+			  s.certs_slot,
+			  (s.device_id >> 8) & 0xff,
+			  (s.device_id >> 3) & 0x1f,
+			  s.device_id & 0x07,
+			  s.segment_id,
+			  s.no_fw_update);
+
+	tsm_dev_put(tdev);
+	return ret1;
+}
+
+static DEVICE_ATTR_RO(tsm_dev_status);
+
+static struct attribute *host_dev_attrs[] = {
+	&dev_attr_tsm_cert_slot.attr,
+	&dev_attr_tsm_dev_connect.attr,
+	&dev_attr_tsm_dev_status.attr,
+	NULL,
+};
+static const struct attribute_group host_dev_group = {
+	.attrs = host_dev_attrs,
+};
+
+static ssize_t tsm_tdi_bind_show(struct device *dev, struct

device_attribute *attr, char *buf) +{

drivers/virt/coco/host/tsm-host.c new file +20 -0
+	struct tsm_tdi *tdi = container_of(dev, struct tsm_tdi, dev);
+
+	if (!tdi->kvm)
+		return sysfs_emit(buf, "not bound\n");
+
+	return sysfs_emit(buf, "VM=%p BDFn=%x:%x.%d\n",
+			  tdi->kvm,
+			  (tdi->guest_rid >> 8) & 0xff,
+			  (tdi->guest_rid >> 3) & 0x1f,
+			  tdi->guest_rid & 0x07);
+}
+
+static DEVICE_ATTR_RO(tsm_tdi_bind);
+
+static char *spdm_algos_to_str(u64 algos, char *buf, size_t len)
+{
+	size_t n = 0;
+
+	buf[0] = 0;
+#define __ALGO(x) do {
			\
drivers/virt/coco/host/tsm-host.c new file +1 -0
+		if ((n < len) && (algos & (1ULL <<

(TSM_TDI_SPDM_ALGOS_##x)))) \

drivers/virt/coco/host/tsm-host.c new file +1 -0
+			n += snprintf(buf + n, len - n, #x"

"); \

drivers/virt/coco/host/tsm-host.c new file +32 -0
+	} while (0)
+
+	__ALGO(DHE_SECP256R1);
+	__ALGO(DHE_SECP384R1);
+	__ALGO(AEAD_AES_128_GCM);
+	__ALGO(AEAD_AES_256_GCM);
+	__ALGO(ASYM_TPM_ALG_RSASSA_3072);
+	__ALGO(ASYM_TPM_ALG_ECDSA_ECC_NIST_P256);
+	__ALGO(ASYM_TPM_ALG_ECDSA_ECC_NIST_P384);
+	__ALGO(HASH_TPM_ALG_SHA_256);
+	__ALGO(HASH_TPM_ALG_SHA_384);
+	__ALGO(KEY_SCHED_SPDM_KEY_SCHEDULE);
+#undef __ALGO
+	return buf;
+}
+
+static const char *tdisp_state_to_str(enum tsm_tdisp_state state)
+{
+	switch (state) {
+#define __ST(x) case TDISP_STATE_##x: return #x
+	case TDISP_STATE_UNAVAIL: return "TDISP state unavailable";
+	__ST(CONFIG_UNLOCKED);
+	__ST(CONFIG_LOCKED);
+	__ST(RUN);
+	__ST(ERROR);
+#undef __ST
+	default: return "unknown";
+	}
+}
+
+static ssize_t tsm_tdi_status_user_show(struct device *dev,
+					struct device_attribute

*attr,

drivers/virt/coco/host/tsm-host.c new file +5 -0
+					char *buf)
+{
+	struct tsm_tdi *tdi = container_of(dev, struct tsm_tdi, dev);
+	struct tsm_dev *tdev = tdi->tdev;
+	struct tsm_host_subsys *hsubsys = (struct tsm_host_subsys *)

tdev->tsm;

drivers/virt/coco/host/tsm-host.c new file +12 -0
+	struct tsm_tdi_status ts = { 0 };
+	char algos[256] = "";
+	unsigned int n, m;
+	int ret;
+
+	ret = tsm_tdi_status(tdi, hsubsys->private_data, &ts);
+	if (ret < 0)
+		return sysfs_emit(buf, "ret=%d\n\n", ret);
+
+	if (!ts.valid)
+		return sysfs_emit(buf, "ret=%d\nstate=%d:%s\n",
+				  ret, ts.state,

tdisp_state_to_str(ts.state)); +

drivers/virt/coco/host/tsm-host.c new file +23 -0
+	n = snprintf(buf, PAGE_SIZE,
+		     "ret=%d\n"
+		     "state=%d:%s\n"
+		     "meas_digest_fresh=%x\n"
+		     "meas_digest_valid=%x\n"
+		     "all_request_redirect=%x\n"
+		     "bind_p2p=%x\n"
+		     "lock_msix=%x\n"
+		     "no_fw_update=%x\n"
+		     "cache_line_size=%d\n"
+		     "algos=%#llx:%s\n"
+		     "report_counter=%lld\n"
+		     ,
+		     ret,
+		     ts.state, tdisp_state_to_str(ts.state),
+		     ts.meas_digest_fresh,
+		     ts.meas_digest_valid,
+		     ts.all_request_redirect,
+		     ts.bind_p2p,
+		     ts.lock_msix,
+		     ts.no_fw_update,
+		     ts.cache_line_size,
+		     ts.spdm_algos, spdm_algos_to_str(ts.spdm_algos,

algos, sizeof(algos) - 1),

drivers/virt/coco/host/tsm-host.c new file +4 -0
+		     ts.intf_report_counter);
+
+	n += snprintf(buf + n, PAGE_SIZE - n, "Certs digest: ");
+	m = hex_dump_to_buffer(ts.certs_digest,

sizeof(ts.certs_digest), 32, 1,

drivers/virt/coco/host/tsm-host.c new file +3 -0
+			       buf + n, PAGE_SIZE - n, false);
+	n += min(PAGE_SIZE - n, m);
+	n += snprintf(buf + n, PAGE_SIZE - n, "...\nMeasurements

digest: ");

drivers/virt/coco/host/tsm-host.c new file +1 -0
+	m = hex_dump_to_buffer(ts.meas_digest,

sizeof(ts.meas_digest), 32, 1,

drivers/virt/coco/host/tsm-host.c new file +3 -0
+			       buf + n, PAGE_SIZE - n, false);
+	n += min(PAGE_SIZE - n, m);
+	n += snprintf(buf + n, PAGE_SIZE - n, "...\nInterface report

digest: ");

drivers/virt/coco/host/tsm-host.c new file +1 -0
+	m = hex_dump_to_buffer(ts.interface_report_digest,

sizeof(ts.interface_report_digest),

drivers/virt/coco/host/tsm-host.c new file +10 -0
+			       32, 1, buf + n, PAGE_SIZE - n, false);
+	n += min(PAGE_SIZE - n, m);
+	n += snprintf(buf + n, PAGE_SIZE - n, "...\n");
+
+	return n;
+}
+
+static DEVICE_ATTR_RO(tsm_tdi_status_user);
+
+static ssize_t tsm_tdi_status_show(struct device *dev, struct

device_attribute *attr, char *buf) +{

drivers/virt/coco/host/tsm-host.c new file +3 -0
+	struct tsm_tdi *tdi = container_of(dev, struct tsm_tdi, dev);
+	struct tsm_dev *tdev = tdi->tdev;
+	struct tsm_host_subsys *hsubsys = (struct tsm_host_subsys *)

tdev->tsm;

drivers/virt/coco/host/tsm-host.c new file +31 -0
+	struct tsm_tdi_status ts = { 0 };
+	u8 state;
+	int ret;
+
+	ret = tsm_tdi_status(tdi, hsubsys->private_data, &ts);
+	if (ret)
+		return ret;
+
+	state = ts.state;
+	memcpy(buf, &state, sizeof(state));
+
+	return sizeof(state);
+}
+
+static DEVICE_ATTR_RO(tsm_tdi_status);
+
+static struct attribute *host_tdi_attrs[] = {
+	&dev_attr_tsm_tdi_bind.attr,
+	&dev_attr_tsm_tdi_status_user.attr,
+	&dev_attr_tsm_tdi_status.attr,
+	NULL,
+};
+
+static const struct attribute_group host_tdi_group = {
+	.attrs = host_tdi_attrs,
+};
+
+int tsm_tdi_bind(struct tsm_tdi *tdi, u32 guest_rid, int kvmfd)
+{
+	struct tsm_dev *tdev = tdi->tdev;
+	struct tsm_host_subsys *hsubsys = (struct tsm_host_subsys *)

tdev->tsm;

drivers/virt/coco/host/tsm-host.c new file +45 -0
+	struct fd f = fdget(kvmfd);
+	struct kvm *kvm;
+	u64 vmid;
+	int ret;
+
+	if (!fd_file(f))
+		return -EBADF;
+
+	if (!file_is_kvm(fd_file(f))) {
+		ret = -EBADF;
+		goto out_fput;
+	}
+
+	kvm = fd_file(f)->private_data;
+	if (!kvm || !kvm_get_kvm_safe(kvm)) {
+		ret = -EFAULT;
+		goto out_fput;
+	}
+
+	vmid = kvm_arch_tsm_get_vmid(kvm);
+	if (!vmid) {
+		ret = -EFAULT;
+		goto out_kvm_put;
+	}
+
+	if (WARN_ON(!hsubsys->ops->tdi_bind)) {
+		ret = -EPERM;
+		goto out_kvm_put;
+	}
+
+	if (!tdev->connected) {
+		ret = -EIO;
+		goto out_kvm_put;
+	}
+
+	mutex_lock(&tdi->tdev->spdm_mutex);
+	while (1) {
+		ret = hsubsys->ops->tdi_bind(tdi, guest_rid, vmid);
+		if (ret < 0)
+			break;
+
+		if (!ret)
+			break;
+
+		ret =

tdi->tdev->tsm_bus->ops->spdm_forward(&tdi->tdev->spdm, ret);

drivers/virt/coco/host/tsm-host.c new file +38 -0
+		if (ret < 0)
+			break;
+	}
+	mutex_unlock(&tdi->tdev->spdm_mutex);
+
+	if (ret) {
+		tsm_tdi_unbind(tdi);
+		goto out_kvm_put;
+	}
+
+	tdi->guest_rid = guest_rid;
+	tdi->kvm = kvm;
+	++tdi->tdev->bound;
+	goto out_fput;
+
+out_kvm_put:
+	kvm_put_kvm(kvm);
+out_fput:
+	fdput(f);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(tsm_tdi_bind);
+
+void tsm_tdi_unbind(struct tsm_tdi *tdi)
+{
+	if (tdi->kvm) {
+		tsm_tdi_reclaim(tdi);
+		--tdi->tdev->bound;
+		kvm_put_kvm(tdi->kvm);
+		tdi->kvm = NULL;
+	}
+
+	tdi->guest_rid = 0;
+	tdi->dev.parent->tdi_enabled = false;
+}
+EXPORT_SYMBOL_GPL(tsm_tdi_unbind);
+
+int tsm_guest_request(struct tsm_tdi *tdi, u8 __user *req, size_t

reqlen,

drivers/virt/coco/host/tsm-host.c new file +4 -0
+		      u8 __user *res, size_t reslen, int *fw_err)
+{
+	struct tsm_dev *tdev = tdi->tdev;
+	struct tsm_host_subsys *hsubsys = (struct tsm_host_subsys *)

tdev->tsm;

drivers/virt/coco/host/tsm-host.c new file +9 -0
+	int ret;
+
+	if (!hsubsys->ops->guest_request)
+		return -EPERM;
+
+	mutex_lock(&tdi->tdev->spdm_mutex);
+	while (1) {
+		ret = hsubsys->ops->guest_request(tdi, req, reqlen,
+						  res, reslen,

fw_err);

drivers/virt/coco/host/tsm-host.c new file +4 -0
+		if (ret <= 0)
+			break;
+
+		ret =

tdi->tdev->tsm_bus->ops->spdm_forward(&tdi->tdev->spdm,

drivers/virt/coco/host/tsm-host.c new file +16 -0
+							    ret);
+		if (ret < 0)
+			break;
+	}
+
+	mutex_unlock(&tdi->tdev->spdm_mutex);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(tsm_guest_request);
+
+struct tsm_host_subsys *tsm_host_register(struct device *parent,
+					  struct tsm_hv_ops *hvops,
+					  void *private_data)
+{
+	struct tsm_subsys *subsys = tsm_register(parent,

sizeof(struct tsm_host_subsys),

drivers/virt/coco/host/tsm-host.c new file +1 -0
+						 &host_dev_group,

&host_tdi_group,
+
tsm_tdi_measurements_locked);

drivers/virt/coco/host/tsm-host.c new file +26 -0
+	struct tsm_host_subsys *hsubsys;
+
+	hsubsys = (struct tsm_host_subsys *) subsys;
+
+	if (IS_ERR(hsubsys))
+		return hsubsys;
+
+	hsubsys->ops = hvops;
+	hsubsys->private_data = private_data;
+
+	return hsubsys;
+}
+EXPORT_SYMBOL_GPL(tsm_host_register);
+
+static int __init tsm_init(void)
+{
+	int ret = 0;
+
+	pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
+
+	return ret;
+}
+
+static void __exit tsm_exit(void)
+{
+	pr_info(DRIVER_DESC " version: " DRIVER_VERSION "

shutdown\n"); +}

drivers/virt/coco/host/tsm-host.c new file +8 -0
+
+module_init(tsm_init);
+module_exit(tsm_exit);
+
+MODULE_VERSION(DRIVER_VERSION);
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR(DRIVER_AUTHOR);
+MODULE_DESCRIPTION(DRIVER_DESC);
drivers/virt/coco/tsm.c new file +54 -0
new file mode 100644
index 000000000000..b6235d1210ca
@@ -0,0 +1,636 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/module.h>
+#include <linux/tsm.h>
+
+#define DRIVER_VERSION	"0.1"
+#define DRIVER_AUTHOR	"[email protected]"
+#define DRIVER_DESC	"TSM library"
+
+static struct class *tsm_class, *tdev_class, *tdi_class;
+
+/* snprintf does not check for the size, hence this wrapper */
+static int tsmprint(char *buf, size_t size, const char *fmt, ...)
+{
+	va_list args;
+	size_t i;
+
+	if (!size)
+		return 0;
+
+	va_start(args, fmt);
+	i = vsnprintf(buf, size, fmt, args);
+	va_end(args);
+
+	return min(i, size);
+}
+
+struct tsm_blob *tsm_blob_new(void *data, size_t len)
+{
+	struct tsm_blob *b;
+
+	if (!len || !data)
+		return NULL;
+
+	b = kzalloc(sizeof(*b) + len, GFP_KERNEL);
+	if (!b)
+		return NULL;
+
+	b->data = (void *)b + sizeof(*b);
+	b->len = len;
+	memcpy(b->data, data, len);
+
+	return b;
+}
+EXPORT_SYMBOL_GPL(tsm_blob_new);
+
+static int match_class(struct device *dev, const void *data)
+{
+	return dev->class == data;
+}
+
+struct tsm_dev *tsm_dev_get(struct device *parent)
+{
+	struct device *dev = device_find_child(parent, tdev_class,

match_class); +

drivers/virt/coco/tsm.c new file +2 -0
+	if (!dev) {
+		dev = device_find_child(parent, tdi_class,

match_class);

drivers/virt/coco/tsm.c new file +2 -0
+		if (dev) {
+			struct tsm_tdi *tdi = container_of(dev,

struct tsm_tdi, dev); +

drivers/virt/coco/tsm.c new file +21 -0
+			dev = &tdi->tdev->dev;
+		}
+	}
+
+	if (!dev)
+		return NULL;
+
+	/* device_find_child() does get_device() */
+	return container_of(dev, struct tsm_dev, dev);
+}
+EXPORT_SYMBOL_GPL(tsm_dev_get);
+
+void tsm_dev_put(struct tsm_dev *tdev)
+{
+	put_device(&tdev->dev);
+}
+EXPORT_SYMBOL_GPL(tsm_dev_put);
+
+struct tsm_tdi *tsm_tdi_get(struct device *parent)
+{
+	struct device *dev = device_find_child(parent, tdi_class,

match_class); +

drivers/virt/coco/tsm.c new file +31 -0
+	if (!dev)
+		return NULL;
+
+	/* device_find_child() does get_device() */
+	return container_of(dev, struct tsm_tdi, dev);
+}
+EXPORT_SYMBOL_GPL(tsm_tdi_get);
+
+void tsm_tdi_put(struct tsm_tdi *tdi)
+{
+	put_device(&tdi->dev);
+}
+EXPORT_SYMBOL_GPL(tsm_tdi_put);
+
+static ssize_t blob_show(struct tsm_blob *blob, char *buf)
+{
+	unsigned int n, m;
+	size_t sz = PAGE_SIZE - 1;
+
+	if (!blob)
+		return sysfs_emit(buf, "none\n");
+
+	n = tsmprint(buf, sz, "%lu %u\n", blob->len);
+	m = hex_dump_to_buffer(blob->data, blob->len, 32, 1,
+			       buf + n, sz - n, false);
+	n += min(sz - n, m);
+	n += tsmprint(buf + n, sz - n, "...\n");
+	return n;
+}
+
+static ssize_t tsm_certs_gen(struct tsm_blob *certs, char *buf,

size_t len) +{

drivers/virt/coco/tsm.c new file +6 -0
+	struct spdm_certchain_block_header *h;
+	unsigned int n = 0, m, i, off, o2;
+	u8 *p;
+
+	for (i = 0, off = 0; off < certs->len; ++i) {
+		h = (struct spdm_certchain_block_header *) ((u8

*)certs->data + off);

drivers/virt/coco/tsm.c new file +4 -0
+		if (WARN_ON_ONCE(h->length > certs->len - off))
+			return 0;
+
+		n += tsmprint(buf + n, len - n, "[%d] len=%d:\n", i,

h->length); +

drivers/virt/coco/tsm.c new file +1 -0
+		for (o2 = 0, p = (u8 *)&h[1]; o2 < h->length; o2 +=

32) {

drivers/virt/coco/tsm.c new file +1 -0
+			m = hex_dump_to_buffer(p + o2, h->length -

o2, 32, 1,

drivers/virt/coco/tsm.c new file +1 -0
+					       buf + n, len - n,

true);

drivers/virt/coco/tsm.c new file +19 -0
+			n += min(len - n, m);
+			n += tsmprint(buf + n, len - n, "\n");
+		}
+
+		off += h->length; /* Includes the header */
+	}
+
+	return n;
+}
+
 
+
+void tsm_dev_free(struct tsm_dev *tdev)
+{
+	dev_notice(&tdev->dev, "Freeing tdevice\n");
+	device_unregister(&tdev->dev);
+}
+EXPORT_SYMBOL_GPL(tsm_dev_free);
+
+int tsm_create_link(struct tsm_subsys *tsm, struct device *dev,

const char *name) +{

drivers/virt/coco/tsm.c new file +11 -0
+	return sysfs_create_link(&tsm->dev.kobj, &dev->kobj, name);
+}
+EXPORT_SYMBOL_GPL(tsm_create_link);
+
+void tsm_remove_link(struct tsm_subsys *tsm, const char *name)
+{
+	sysfs_remove_link(&tsm->dev.kobj, name);
+}
+EXPORT_SYMBOL_GPL(tsm_remove_link);
+
+static struct tsm_subsys *alloc_tsm_subsys(struct device *parent,

size_t size) +{

drivers/virt/coco/tsm.c new file +19 -0
+	struct tsm_subsys *subsys;
+	struct device *dev;
+
+	if (WARN_ON_ONCE(size < sizeof(*subsys)))
+		return ERR_PTR(-EINVAL);
+
+	subsys = kzalloc(size, GFP_KERNEL);
+	if (!subsys)
+		return ERR_PTR(-ENOMEM);
+
+	dev = &subsys->dev;
+	dev->parent = parent;
+	dev->class = tsm_class;
+	device_initialize(dev);
+	return subsys;
+}
+
+struct tsm_subsys *tsm_register(struct device *parent, size_t size,
+				const struct attribute_group

*tdev_ag,

drivers/virt/coco/tsm.c new file +2 -0
+				const struct attribute_group *tdi_ag,
+				int (*update_measurements)(struct

tsm_dev *tdev)) +{

drivers/virt/coco/tsm.c new file +35 -0
+	struct tsm_subsys *subsys = alloc_tsm_subsys(parent, size);
+	struct device *dev;
+	int rc;
+
+	if (IS_ERR(subsys))
+		return subsys;
+
+	dev = &subsys->dev;
+	rc = dev_set_name(dev, "tsm0");
+	if (rc)
+		return ERR_PTR(rc);
+
+	rc = device_add(dev);
+	if (rc)
+		return ERR_PTR(rc);
+
+	subsys->tdev_groups[0] = &dev_group;
+	subsys->tdev_groups[1] = tdev_ag;
+	subsys->tdi_groups[0] = &tdi_group;
+	subsys->tdi_groups[1] = tdi_ag;
+	subsys->update_measurements = update_measurements;
+
+	return subsys;
+}
+EXPORT_SYMBOL_GPL(tsm_register);
+
+void tsm_unregister(struct tsm_subsys *subsys)
+{
+	device_unregister(&subsys->dev);
+}
+EXPORT_SYMBOL_GPL(tsm_unregister);
+
+static void tsm_release(struct device *dev)
+{
+	struct tsm_subsys *tsm = container_of(dev, typeof(*tsm),

dev); +

drivers/virt/coco/tsm.c new file +18 -0
+	dev_info(&tsm->dev, "Releasing TSM\n");
+	kfree(tsm);
+}
+
+static void tdev_release(struct device *dev)
+{
+	struct tsm_dev *tdev = container_of(dev, typeof(*tdev), dev);
+
+	dev_info(&tdev->dev, "Releasing %s TDEV\n",
+		 tdev->connected ? "connected":"disconnected");
+	kfree(tdev);
+}
+
+static void tdi_release(struct device *dev)
+{
+	struct tsm_tdi *tdi = container_of(dev, typeof(*tdi), dev);
+
+	dev_info(&tdi->dev, "Releasing %s TDI\n", tdi->kvm ? "bound"

: "unbound");

drivers/virt/coco/tsm.c new file +31 -0
+	sysfs_remove_link(&tdi->dev.parent->kobj, "tsm_dev");
+	kfree(tdi);
+}
+
+static int __init tsm_init(void)
+{
+	int ret = 0;
+
+	pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
+
+	tsm_class = class_create("tsm");
+	if (IS_ERR(tsm_class))
+		return PTR_ERR(tsm_class);
+	tsm_class->dev_release = tsm_release;
+
+	tdev_class = class_create("tsm-dev");
+	if (IS_ERR(tdev_class))
+		return PTR_ERR(tdev_class);
+	tdev_class->dev_release = tdev_release;
+
+	tdi_class = class_create("tsm-tdi");
+	if (IS_ERR(tdi_class))
+		return PTR_ERR(tdi_class);
+	tdi_class->dev_release = tdi_release;
+
+	return ret;
+}
+
+static void __exit tsm_exit(void)
+{
+	pr_info(DRIVER_DESC " version: " DRIVER_VERSION "

shutdown\n");

drivers/virt/coco/tsm.c new file +12 -0
+	class_destroy(tdi_class);
+	class_destroy(tdev_class);
+	class_destroy(tsm_class);
+}
+
+module_init(tsm_init);
+module_exit(tsm_exit);
+
+MODULE_VERSION(DRIVER_VERSION);
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR(DRIVER_AUTHOR);
+MODULE_DESCRIPTION(DRIVER_DESC);
(quoted diff) +0 -0

b/Documentation/virt/coco/tsm.rst new file mode 100644 index 000000000000..7cb5f1862492

Documentation/virt/coco/tsm.rst new file +6 -0
@@ -0,0 +1,99 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+What it is
+==========
+
+This is for PCI passthrough in confidential computing (CoCo:

SEV-SNP, TDX, CoVE). +Currently passing through PCI devices to a CoCo VM uses SWIOTLB to pre-shared +memory buffers.

Documentation/virt/coco/tsm.rst new file +2 -0
+
+PCIe IDE (Integrity and Data Encryption) and TDISP (TEE Device

Interface Security +Protocol) are protocols to enable encryption over PCIe link and DMA to encrypted +memory. This doc is focused to DMAing to encrypted VM, the encrypted host memory is +out of scope.

Documentation/virt/coco/tsm.rst new file +6 -0
+
+
+Protocols
+=========
+
+PCIe r6 DOE is a mailbox protocol to read/write object from/to

device. +Objects are of plain SPDM or secure SPDM type. SPDM is responsible for authenticating +devices, creating a secure link between a device and TSM. +IDE_KM manages PCIe link encryption keys, it works on top of secure SPDM. +TDISP manages a passed through PCI function state, also works on top on secure SPDM. +Additionally, PCIe defines IDE capability which provides the host OS a way +to enable streams on the PCIe link. +

Documentation/virt/coco/tsm.rst new file +7 -0
+
+TSM modules
+===========
+
+TSM is a library, shared among hosts and guests.
+
+TSM-HOST contains host-specific bits, controls IDE and TDISP

bindings. +
+TSM-GUEST contains guest-specific bits, controls enablement of encrypted DMA and +MMIO.

Documentation/virt/coco/tsm.rst new file +2 -0
+
+TSM-PCI is PCI binding for TSM, calls the above libraries for

setting up +sysfs nodes and corresponding data structures.

Documentation/virt/coco/tsm.rst new file +6 -0
+
+
+Flow
+====
+
+At the boot time the tsm.ko scans the PCI bus to find and setup

TDISP-cabable +devices; it also listens to hotplug events. If setup was successful, tsm-prefixed +nodes will appear in sysfs.

Documentation/virt/coco/tsm.rst new file +2 -0
+
+Then, the user enables IDE by writing to

/sys/bus/pci/devices/0000:e1:00.0/tsm_dev_connect +and this is how PCIe encryption is enabled. +

Documentation/virt/coco/tsm.rst new file +3 -0
+To pass the device through, a modifined VMM is required.
+
+In the VM, the same tsm.ko loads. In addition to the host's setup,

the VM wants +to receive the report and enable secure DMA or/and secure MMIO, via some VM<->HV +protocol (such as AMD GHCB). Once this is done, a VM can access validated MMIO +with the Cbit set and the device can DMA to encrypted memory. +

Documentation/virt/coco/tsm.rst new file +41 -0
+The sysfs example from a host with a TDISP capable device:
+
+~> find /sys -iname "*tsm*"
+/sys/class/tsm-tdi
+/sys/class/tsm
+/sys/class/tsm/tsm0
+/sys/class/tsm-dev
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.1/tsm_dev
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.1/tsm-tdi
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.1/tsm-tdi/tdi:0000:e1:00.1/tsm_tdi_bind
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.1/tsm-tdi/tdi:0000:e1:00.1/tsm_tdi_status
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.1/tsm-tdi/tdi:0000:e1:00.1/tsm_tdi_status_user
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.1/tsm-tdi/tdi:0000:e1:00.1/tsm_report_user
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.1/tsm-tdi/tdi:0000:e1:00.1/tsm_report
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm_dev
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-tdi
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-tdi/tdi:0000:e1:00.0/tsm_tdi_bind
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-tdi/tdi:0000:e1:00.0/tsm_tdi_status
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-tdi/tdi:0000:e1:00.0/tsm_tdi_status_user
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-tdi/tdi:0000:e1:00.0/tsm_report_user
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-tdi/tdi:0000:e1:00.0/tsm_report
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-dev
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-dev/tdev:0000:e1:00.0/tsm_certs
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-dev/tdev:0000:e1:00.0/tsm_nonce
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-dev/tdev:0000:e1:00.0/tsm_meas_user
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-dev/tdev:0000:e1:00.0/tsm_certs_user
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-dev/tdev:0000:e1:00.0/tsm_dev_status
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-dev/tdev:0000:e1:00.0/tsm_cert_slot
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-dev/tdev:0000:e1:00.0/tsm_dev_connect
+/sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/tsm-dev/tdev:0000:e1:00.0/tsm_meas
+/sys/devices/pci0000:a0/0000:a0:07.1/0000:a9:00.5/tsm
+/sys/devices/pci0000:a0/0000:a0:07.1/0000:a9:00.5/tsm/tsm0
+
+
+References
+==========
+
+[1] TEE Device Interface Security Protocol - TDISP - v2022-07-27
+https://members.pcisig.com/wg/PCI-SIG/document/18268?downloadRevision=21500
+[2] Security Protocol and Data Model (SPDM)
+https://www.dmtf.org/sites/default/files/standards/documents/DSP0274_1.2.1.pdf
drivers/virt/coco/Kconfig +2 -0
index 819a97e8ba99..e4385247440b 100644
@@ -3,6 +3,18 @@
  # Confidential computing related collateral
  #
 
+config TSM
+	tristate "Platform support for TEE Device Interface Security

Protocol (TDISP)"

drivers/virt/coco/Kconfig +6 -0
+	default m
+	depends on AMD_MEM_ENCRYPT
+	select PCI_DOE
+	select PCI_IDE
+	help
+	  Add a common place for user visible platform support for

PCIe TDISP.

drivers/virt/coco/Kconfig +1 -0
+	  TEE Device Interface Security Protocol (TDISP) from

PCI-SIG,
+
https://pcisig.com/tee-device-interface-security-protocol-tdisp

drivers/virt/coco/Kconfig +4 -0
+	  This is prerequisite for host and guest support.
+
  source "drivers/virt/coco/efi_secret/Kconfig"
 
  source "drivers/virt/coco/pkvm-guest/Kconfig"
@@ -14,3 +26,5 @@ source "drivers/virt/coco/tdx-guest/Kconfig"
  source "drivers/virt/coco/arm-cca-guest/Kconfig"
 
  source "drivers/virt/coco/guest/Kconfig"
+
+source "drivers/virt/coco/host/Kconfig"
(quoted diff) +0 -0

b/drivers/virt/coco/host/Kconfig new file mode 100644 index 000000000000..3bde38b91fd4

drivers/virt/coco/host/Kconfig new file +6 -0
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# TSM (TEE Security Manager) Common infrastructure and host drivers
+#
+config TSM_HOST
+	tristate
Signature · 1 line
Alexey